1 /* $Id: port-solaris.c,v 1.4 2010/11/05 01:03:05 dtucker Exp $ */
4 * Copyright (c) 2006 Chad Mynhier.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
24 #include <sys/types.h>
26 #include <sys/param.h>
36 #include <libcontract.h>
37 #include <sys/contract/process.h>
42 #define CT_TEMPLATE CTFS_ROOT "/process/template"
43 #define CT_LATEST CTFS_ROOT "/process/latest"
45 static int tmpl_fd = -1;
47 /* Lookup the latest process contract */
49 get_active_process_contract_id(void)
55 if ((stat_fd = open64(CT_LATEST, O_RDONLY)) == -1) {
56 error("%s: Error opening 'latest' process "
57 "contract: %s", __func__, strerror(errno));
60 if (ct_status_read(stat_fd, CTD_COMMON, &stathdl) != 0) {
61 error("%s: Error reading process contract "
62 "status: %s", __func__, strerror(errno));
65 if ((ctid = ct_status_get_id(stathdl)) < 0) {
66 error("%s: Error getting process contract id: %s",
67 __func__, strerror(errno));
71 ct_status_free(stathdl);
78 solaris_contract_pre_fork(void)
80 if ((tmpl_fd = open64(CT_TEMPLATE, O_RDWR)) == -1) {
81 error("%s: open %s: %s", __func__,
82 CT_TEMPLATE, strerror(errno));
86 debug2("%s: setting up process contract template on fd %d",
89 /* First we set the template parameters and event sets. */
90 if (ct_pr_tmpl_set_param(tmpl_fd, CT_PR_PGRPONLY) != 0) {
91 error("%s: Error setting process contract parameter set "
92 "(pgrponly): %s", __func__, strerror(errno));
95 if (ct_pr_tmpl_set_fatal(tmpl_fd, CT_PR_EV_HWERR) != 0) {
96 error("%s: Error setting process contract template "
97 "fatal events: %s", __func__, strerror(errno));
100 if (ct_tmpl_set_critical(tmpl_fd, 0) != 0) {
101 error("%s: Error setting process contract template "
102 "critical events: %s", __func__, strerror(errno));
105 if (ct_tmpl_set_informative(tmpl_fd, CT_PR_EV_HWERR) != 0) {
106 error("%s: Error setting process contract template "
107 "informative events: %s", __func__, strerror(errno));
111 /* Now make this the active template for this process. */
112 if (ct_tmpl_activate(tmpl_fd) != 0) {
113 error("%s: Error activating process contract "
114 "template: %s", __func__, strerror(errno));
127 solaris_contract_post_fork_child()
129 debug2("%s: clearing process contract template on fd %d",
132 /* Clear the active template. */
133 if (ct_tmpl_clear(tmpl_fd) != 0)
134 error("%s: Error clearing active process contract "
135 "template: %s", __func__, strerror(errno));
142 solaris_contract_post_fork_parent(pid_t pid)
146 int r, ctl_fd = -1, stat_fd = -1;
148 debug2("%s: clearing template (fd %d)", __func__, tmpl_fd);
153 /* First clear the active template. */
154 if ((r = ct_tmpl_clear(tmpl_fd)) != 0)
155 error("%s: Error clearing active process contract "
156 "template: %s", __func__, strerror(errno));
162 * If either the fork didn't succeed (pid < 0), or clearing
163 * th active contract failed (r != 0), then we have nothing
166 if (r != 0 || pid <= 0)
169 /* Now lookup and abandon the contract we've created. */
170 ctid = get_active_process_contract_id();
172 debug2("%s: abandoning contract id %ld", __func__, ctid);
174 snprintf(ctl_path, sizeof(ctl_path),
175 CTFS_ROOT "/process/%ld/ctl", ctid);
176 if ((ctl_fd = open64(ctl_path, O_WRONLY)) < 0) {
177 error("%s: Error opening process contract "
178 "ctl file: %s", __func__, strerror(errno));
181 if (ct_ctl_abandon(ctl_fd) < 0) {
182 error("%s: Error abandoning process contract: %s",
183 __func__, strerror(errno));
201 #ifdef USE_SOLARIS_PROJECTS
202 #include <sys/task.h>
206 * Get/set solaris default project.
207 * If we fail, just run along gracefully.
210 solaris_set_default_project(struct passwd *pw)
212 struct project *defaultproject;
213 struct project tempproject;
216 /* get default project, if we fail just return gracefully */
217 if ((defaultproject = getdefaultproj(pw->pw_name, &tempproject, &buf,
219 /* set default project */
220 if (setproject(defaultproject->pj_name, pw->pw_name,
222 debug("setproject(%s): %s", defaultproject->pj_name,
225 /* debug on getdefaultproj() error */
226 debug("getdefaultproj(%s): %s", pw->pw_name, strerror(errno));
229 #endif /* USE_SOLARIS_PROJECTS */
231 #ifdef USE_SOLARIS_PRIVS
237 solaris_basic_privset(void)
241 #ifdef HAVE_PRIV_BASICSET
242 if ((pset = priv_allocset()) == NULL) {
243 error("priv_allocset: %s", strerror(errno));
248 if ((pset = priv_str_to_set("basic", ",", NULL)) == NULL) {
249 error("priv_str_to_set: %s", strerror(errno));
257 solaris_drop_privs_pinfo_net_fork_exec(void)
259 priv_set_t *pset = NULL, *npset = NULL;
262 * Note: this variant avoids dropping DAC filesystem rights, in case
263 * the process calling it is running as root and should have the
264 * ability to read/write/chown any file on the system.
266 * We start with the basic set, then *add* the DAC rights to it while
267 * taking away other parts of BASIC we don't need. Then we intersect
268 * this with our existing PERMITTED set. In this way we keep any
269 * DAC rights we had before, while otherwise reducing ourselves to
270 * the minimum set of privileges we need to proceed.
272 * This also means we drop any other parts of "root" that we don't
273 * need (e.g. the ability to kill any process, create new device nodes
277 if ((pset = priv_allocset()) == NULL)
278 fatal("priv_allocset: %s", strerror(errno));
279 if ((npset = solaris_basic_privset()) == NULL)
280 fatal("solaris_basic_privset: %s", strerror(errno));
282 if (priv_addset(npset, PRIV_FILE_CHOWN) != 0 ||
283 priv_addset(npset, PRIV_FILE_DAC_READ) != 0 ||
284 priv_addset(npset, PRIV_FILE_DAC_SEARCH) != 0 ||
285 priv_addset(npset, PRIV_FILE_DAC_WRITE) != 0 ||
286 priv_addset(npset, PRIV_FILE_OWNER) != 0)
287 fatal("priv_addset: %s", strerror(errno));
289 if (priv_delset(npset, PRIV_FILE_LINK_ANY) != 0 ||
290 #ifdef PRIV_NET_ACCESS
291 priv_delset(npset, PRIV_NET_ACCESS) != 0 ||
293 priv_delset(npset, PRIV_PROC_EXEC) != 0 ||
294 priv_delset(npset, PRIV_PROC_FORK) != 0 ||
295 priv_delset(npset, PRIV_PROC_INFO) != 0 ||
296 priv_delset(npset, PRIV_PROC_SESSION) != 0)
297 fatal("priv_delset: %s", strerror(errno));
299 if (getppriv(PRIV_PERMITTED, pset) != 0)
300 fatal("getppriv: %s", strerror(errno));
302 priv_intersect(pset, npset);
304 if (setppriv(PRIV_SET, PRIV_PERMITTED, npset) != 0 ||
305 setppriv(PRIV_SET, PRIV_LIMIT, npset) != 0 ||
306 setppriv(PRIV_SET, PRIV_INHERITABLE, npset) != 0)
307 fatal("setppriv: %s", strerror(errno));
314 solaris_drop_privs_root_pinfo_net(void)
316 priv_set_t *pset = NULL;
318 /* Start with "basic" and drop everything we don't need. */
319 if ((pset = solaris_basic_privset()) == NULL)
320 fatal("solaris_basic_privset: %s", strerror(errno));
322 if (priv_delset(pset, PRIV_FILE_LINK_ANY) != 0 ||
323 #ifdef PRIV_NET_ACCESS
324 priv_delset(pset, PRIV_NET_ACCESS) != 0 ||
326 priv_delset(pset, PRIV_PROC_INFO) != 0 ||
327 priv_delset(pset, PRIV_PROC_SESSION) != 0)
328 fatal("priv_delset: %s", strerror(errno));
330 if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) != 0 ||
331 setppriv(PRIV_SET, PRIV_LIMIT, pset) != 0 ||
332 setppriv(PRIV_SET, PRIV_INHERITABLE, pset) != 0)
333 fatal("setppriv: %s", strerror(errno));
339 solaris_drop_privs_root_pinfo_net_exec(void)
341 priv_set_t *pset = NULL;
344 /* Start with "basic" and drop everything we don't need. */
345 if ((pset = solaris_basic_privset()) == NULL)
346 fatal("solaris_basic_privset: %s", strerror(errno));
348 if (priv_delset(pset, PRIV_FILE_LINK_ANY) != 0 ||
349 #ifdef PRIV_NET_ACCESS
350 priv_delset(pset, PRIV_NET_ACCESS) != 0 ||
352 priv_delset(pset, PRIV_PROC_EXEC) != 0 ||
353 priv_delset(pset, PRIV_PROC_INFO) != 0 ||
354 priv_delset(pset, PRIV_PROC_SESSION) != 0)
355 fatal("priv_delset: %s", strerror(errno));
357 if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) != 0 ||
358 setppriv(PRIV_SET, PRIV_LIMIT, pset) != 0 ||
359 setppriv(PRIV_SET, PRIV_INHERITABLE, pset) != 0)
360 fatal("setppriv: %s", strerror(errno));