2 * Copyright (c) 2006 Chad Mynhier.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
33 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
35 #include <libcontract.h>
36 #include <sys/contract/process.h>
39 #define CT_TEMPLATE CTFS_ROOT "/process/template"
40 #define CT_LATEST CTFS_ROOT "/process/latest"
42 static int tmpl_fd = -1;
44 /* Lookup the latest process contract */
46 get_active_process_contract_id(void)
52 if ((stat_fd = open64(CT_LATEST, O_RDONLY)) == -1) {
53 error("%s: Error opening 'latest' process "
54 "contract: %s", __func__, strerror(errno));
57 if (ct_status_read(stat_fd, CTD_COMMON, &stathdl) != 0) {
58 error("%s: Error reading process contract "
59 "status: %s", __func__, strerror(errno));
62 if ((ctid = ct_status_get_id(stathdl)) < 0) {
63 error("%s: Error getting process contract id: %s",
64 __func__, strerror(errno));
68 ct_status_free(stathdl);
75 solaris_contract_pre_fork(void)
77 if ((tmpl_fd = open64(CT_TEMPLATE, O_RDWR)) == -1) {
78 error("%s: open %s: %s", __func__,
79 CT_TEMPLATE, strerror(errno));
83 debug2("%s: setting up process contract template on fd %d",
86 /* First we set the template parameters and event sets. */
87 if (ct_pr_tmpl_set_param(tmpl_fd, CT_PR_PGRPONLY) != 0) {
88 error("%s: Error setting process contract parameter set "
89 "(pgrponly): %s", __func__, strerror(errno));
92 if (ct_pr_tmpl_set_fatal(tmpl_fd, CT_PR_EV_HWERR) != 0) {
93 error("%s: Error setting process contract template "
94 "fatal events: %s", __func__, strerror(errno));
97 if (ct_tmpl_set_critical(tmpl_fd, 0) != 0) {
98 error("%s: Error setting process contract template "
99 "critical events: %s", __func__, strerror(errno));
102 if (ct_tmpl_set_informative(tmpl_fd, CT_PR_EV_HWERR) != 0) {
103 error("%s: Error setting process contract template "
104 "informative events: %s", __func__, strerror(errno));
108 /* Now make this the active template for this process. */
109 if (ct_tmpl_activate(tmpl_fd) != 0) {
110 error("%s: Error activating process contract "
111 "template: %s", __func__, strerror(errno));
124 solaris_contract_post_fork_child()
126 debug2("%s: clearing process contract template on fd %d",
129 /* Clear the active template. */
130 if (ct_tmpl_clear(tmpl_fd) != 0)
131 error("%s: Error clearing active process contract "
132 "template: %s", __func__, strerror(errno));
139 solaris_contract_post_fork_parent(pid_t pid)
143 int r, ctl_fd = -1, stat_fd = -1;
145 debug2("%s: clearing template (fd %d)", __func__, tmpl_fd);
150 /* First clear the active template. */
151 if ((r = ct_tmpl_clear(tmpl_fd)) != 0)
152 error("%s: Error clearing active process contract "
153 "template: %s", __func__, strerror(errno));
159 * If either the fork didn't succeed (pid < 0), or clearing
160 * th active contract failed (r != 0), then we have nothing
163 if (r != 0 || pid <= 0)
166 /* Now lookup and abandon the contract we've created. */
167 ctid = get_active_process_contract_id();
169 debug2("%s: abandoning contract id %ld", __func__, ctid);
171 snprintf(ctl_path, sizeof(ctl_path),
172 CTFS_ROOT "/process/%ld/ctl", ctid);
173 if ((ctl_fd = open64(ctl_path, O_WRONLY)) < 0) {
174 error("%s: Error opening process contract "
175 "ctl file: %s", __func__, strerror(errno));
178 if (ct_ctl_abandon(ctl_fd) < 0) {
179 error("%s: Error abandoning process contract: %s",
180 __func__, strerror(errno));
198 #ifdef USE_SOLARIS_PROJECTS
199 #include <sys/task.h>
203 * Get/set solaris default project.
204 * If we fail, just run along gracefully.
207 solaris_set_default_project(struct passwd *pw)
209 struct project *defaultproject;
210 struct project tempproject;
213 /* get default project, if we fail just return gracefully */
214 if ((defaultproject = getdefaultproj(pw->pw_name, &tempproject, &buf,
215 sizeof(buf))) != NULL) {
216 /* set default project */
217 if (setproject(defaultproject->pj_name, pw->pw_name,
219 debug("setproject(%s): %s", defaultproject->pj_name,
222 /* debug on getdefaultproj() error */
223 debug("getdefaultproj(%s): %s", pw->pw_name, strerror(errno));
226 #endif /* USE_SOLARIS_PROJECTS */
228 #ifdef USE_SOLARIS_PRIVS
234 solaris_basic_privset(void)
238 #ifdef HAVE_PRIV_BASICSET
239 if ((pset = priv_allocset()) == NULL) {
240 error("priv_allocset: %s", strerror(errno));
245 if ((pset = priv_str_to_set("basic", ",", NULL)) == NULL) {
246 error("priv_str_to_set: %s", strerror(errno));
254 solaris_drop_privs_pinfo_net_fork_exec(void)
256 priv_set_t *pset = NULL, *npset = NULL;
259 * Note: this variant avoids dropping DAC filesystem rights, in case
260 * the process calling it is running as root and should have the
261 * ability to read/write/chown any file on the system.
263 * We start with the basic set, then *add* the DAC rights to it while
264 * taking away other parts of BASIC we don't need. Then we intersect
265 * this with our existing PERMITTED set. In this way we keep any
266 * DAC rights we had before, while otherwise reducing ourselves to
267 * the minimum set of privileges we need to proceed.
269 * This also means we drop any other parts of "root" that we don't
270 * need (e.g. the ability to kill any process, create new device nodes
274 if ((pset = priv_allocset()) == NULL)
275 fatal("priv_allocset: %s", strerror(errno));
276 if ((npset = solaris_basic_privset()) == NULL)
277 fatal("solaris_basic_privset: %s", strerror(errno));
279 if (priv_addset(npset, PRIV_FILE_CHOWN) != 0 ||
280 priv_addset(npset, PRIV_FILE_DAC_READ) != 0 ||
281 priv_addset(npset, PRIV_FILE_DAC_SEARCH) != 0 ||
282 priv_addset(npset, PRIV_FILE_DAC_WRITE) != 0 ||
283 priv_addset(npset, PRIV_FILE_OWNER) != 0)
284 fatal("priv_addset: %s", strerror(errno));
286 if (priv_delset(npset, PRIV_PROC_EXEC) != 0 ||
287 #ifdef PRIV_NET_ACCESS
288 priv_delset(npset, PRIV_NET_ACCESS) != 0 ||
290 priv_delset(npset, PRIV_PROC_FORK) != 0 ||
291 priv_delset(npset, PRIV_PROC_INFO) != 0 ||
292 priv_delset(npset, PRIV_PROC_SESSION) != 0)
293 fatal("priv_delset: %s", strerror(errno));
295 if (getppriv(PRIV_PERMITTED, pset) != 0)
296 fatal("getppriv: %s", strerror(errno));
298 priv_intersect(pset, npset);
300 if (setppriv(PRIV_SET, PRIV_PERMITTED, npset) != 0 ||
301 setppriv(PRIV_SET, PRIV_LIMIT, npset) != 0 ||
302 setppriv(PRIV_SET, PRIV_INHERITABLE, npset) != 0)
303 fatal("setppriv: %s", strerror(errno));
310 solaris_drop_privs_root_pinfo_net(void)
312 priv_set_t *pset = NULL;
314 /* Start with "basic" and drop everything we don't need. */
315 if ((pset = solaris_basic_privset()) == NULL)
316 fatal("solaris_basic_privset: %s", strerror(errno));
318 if (priv_delset(pset, PRIV_FILE_LINK_ANY) != 0 ||
319 #ifdef PRIV_NET_ACCESS
320 priv_delset(pset, PRIV_NET_ACCESS) != 0 ||
322 priv_delset(pset, PRIV_PROC_INFO) != 0 ||
323 priv_delset(pset, PRIV_PROC_SESSION) != 0)
324 fatal("priv_delset: %s", strerror(errno));
326 if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) != 0 ||
327 setppriv(PRIV_SET, PRIV_LIMIT, pset) != 0 ||
328 setppriv(PRIV_SET, PRIV_INHERITABLE, pset) != 0)
329 fatal("setppriv: %s", strerror(errno));
335 solaris_drop_privs_root_pinfo_net_exec(void)
337 priv_set_t *pset = NULL;
340 /* Start with "basic" and drop everything we don't need. */
341 if ((pset = solaris_basic_privset()) == NULL)
342 fatal("solaris_basic_privset: %s", strerror(errno));
344 if (priv_delset(pset, PRIV_FILE_LINK_ANY) != 0 ||
345 #ifdef PRIV_NET_ACCESS
346 priv_delset(pset, PRIV_NET_ACCESS) != 0 ||
348 priv_delset(pset, PRIV_PROC_EXEC) != 0 ||
349 priv_delset(pset, PRIV_PROC_INFO) != 0)
350 fatal("priv_delset: %s", strerror(errno));
352 if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) != 0 ||
353 setppriv(PRIV_SET, PRIV_LIMIT, pset) != 0 ||
354 setppriv(PRIV_SET, PRIV_INHERITABLE, pset) != 0)
355 fatal("setppriv: %s", strerror(errno));