4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <sys/sysmacros.h>
28 #include <sys/systm.h>
29 #include <sys/cred_impl.h>
30 #include <sys/errno.h>
31 #include <sys/pathname.h>
34 #include <sys/priv_impl.h>
35 #include <sys/policy.h>
37 #include <sys/thread.h>
38 #include <sys/cmn_err.h>
42 * System call support for manipulating privileges.
45 * setppriv(2) - set process privilege set
46 * getppriv(2) - get process privilege set
47 * getprivimplinfo(2) - get process privilege implementation information
48 * setpflags(2) - set process (privilege) flags
49 * getpflags(2) - get process (privilege) flags
53 * setppriv (priv_op_t, priv_ptype_t, priv_set_t)
56 setppriv(priv_op_t op
, priv_ptype_t type
, priv_set_t
*in_pset
)
58 priv_set_t pset
, *target
;
61 boolean_t donocd
= B_FALSE
;
63 if (!PRIV_VALIDSET(type
) || !PRIV_VALIDOP(op
))
64 return (set_errno(EINVAL
));
66 if (copyin(in_pset
, &pset
, sizeof (priv_set_t
)))
67 return (set_errno(EFAULT
));
69 p
= ttoproc(curthread
);
71 mutex_enter(&p
->p_crlock
);
77 audit_setppriv(op
, type
, &pset
, pcr
);
80 * Filter out unallowed request (bad op and bad type)
86 * Turning on privileges; the limit set cannot grow,
87 * other sets can but only as long as they remain subsets
88 * of P. Only immediately after exec holds that P <= L.
90 if (type
== PRIV_LIMIT
&&
91 !priv_issubset(&pset
, &CR_LPRIV(pcr
))) {
92 mutex_exit(&p
->p_crlock
);
94 return (set_errno(EPERM
));
96 if (!priv_issubset(&pset
, &CR_OPPRIV(pcr
)) &&
97 !priv_issubset(&pset
, priv_getset(pcr
, type
))) {
98 mutex_exit(&p
->p_crlock
);
99 /* Policy override should not grow beyond L either */
100 if (type
!= PRIV_INHERITABLE
||
101 !priv_issubset(&pset
, &CR_LPRIV(pcr
)) ||
102 secpolicy_require_privs(CRED(), &pset
) != 0) {
104 return (set_errno(EPERM
));
106 mutex_enter(&p
->p_crlock
);
107 if (pcr
!= p
->p_cred
)
114 /* PRIV_OFF is always allowed */
119 * OK! everything is cool.
125 * If we change the effective, permitted or limit set, we attain
126 * "privilege awareness".
128 if (type
!= PRIV_INHERITABLE
)
131 target
= &(CR_PRIVS(cr
)->crprivs
[type
]);
135 priv_union(&pset
, target
);
139 priv_intersect(target
, &pset
);
142 * Fall-thru to set target and change other process
151 * Take privileges no longer permitted out
152 * of other effective sets as well.
153 * Limit set is enforced at exec() time.
155 if (type
== PRIV_PERMITTED
)
156 priv_intersect(&pset
, &CR_EPRIV(cr
));
161 * When we give up privileges not in the inheritable set,
162 * set SNOCD if not already set; first we compute the
163 * privileges removed from P using Diff = (~P') & P
164 * and then we check whether the removed privileges are
165 * a subset of I. If we retain uid 0, all privileges
166 * are required anyway so don't set SNOCD.
168 if (type
== PRIV_PERMITTED
&& (p
->p_flag
& SNOCD
) == 0 &&
169 cr
->cr_uid
!= 0 && cr
->cr_ruid
!= 0 && cr
->cr_suid
!= 0) {
170 priv_set_t diff
= CR_OPPRIV(cr
);
172 priv_intersect(&CR_OPPRIV(pcr
), &diff
);
173 donocd
= !priv_issubset(&diff
, &CR_IPRIV(cr
));
177 mutex_exit(&p
->p_crlock
);
180 mutex_enter(&p
->p_lock
);
182 mutex_exit(&p
->p_lock
);
186 * The basic_test privilege should not be removed from E;
187 * if that has happened, then some programmer typically set the E/P to
188 * empty. That is not portable.
190 if ((type
== PRIV_EFFECTIVE
|| type
== PRIV_PERMITTED
) &&
191 priv_basic_test
>= 0 && !PRIV_ISMEMBER(target
, priv_basic_test
)) {
193 pid_t pid
= p
->p_pid
;
194 char *fn
= PTOU(p
)->u_comm
;
196 cmn_err(CE_WARN
, "%s[%d]: setppriv: basic_test privilege "
197 "removed from E/P", fn
, pid
);
200 crset(p
, cr
); /* broadcast to process threads */
206 * getppriv (priv_ptype_t, priv_set_t *)
209 getppriv(priv_ptype_t type
, priv_set_t
*pset
)
211 if (!PRIV_VALIDSET(type
))
212 return (set_errno(EINVAL
));
214 if (copyout(priv_getset(CRED(), type
), pset
, sizeof (priv_set_t
)) != 0)
215 return (set_errno(EFAULT
));
221 getprivimplinfo(void *buf
, size_t bufsize
)
225 err
= copyout(priv_hold_implinfo(), buf
, min(bufsize
, privinfosize
));
227 priv_release_implinfo();
230 return (set_errno(EFAULT
));
236 * Set process flags in the given target cred. If NULL is specified, then
237 * CRED() is used; otherwise the cred is assumed to be modifiable (i.e. newly
238 * crdup'ed, or equivalent). Some flags are set in the proc rather than cred;
239 * for these, curproc is always used.
241 * For now we cheat: the flags are actually bit masks so we can simplify
242 * some; we do make sure that the arguments are valid, though.
246 setpflags(uint_t flag
, uint_t val
, cred_t
*tcr
)
251 boolean_t use_curcred
= (tcr
== NULL
);
253 if (val
> 1 || (flag
!= PRIV_DEBUG
&& flag
!= PRIV_AWARE
&&
254 flag
!= __PROC_PROTECT
&& flag
!= PRIV_XPOLICY
&&
255 flag
!= PRIV_AWARE_RESET
&& flag
!= PRIV_PFEXEC
)) {
259 if (flag
== __PROC_PROTECT
) {
260 mutex_enter(&p
->p_lock
);
265 mutex_exit(&p
->p_lock
);
271 mutex_enter(&p
->p_crlock
);
277 newflags
= CR_FLAGS(pcr
);
280 if (flag
== PRIV_AWARE
)
281 newflags
&= ~PRIV_AWARE_RESET
;
288 if (CR_FLAGS(pcr
) == newflags
) {
290 mutex_exit(&p
->p_crlock
);
296 /* Trying to unset PA; if we can't, return an error */
297 if (flag
== PRIV_AWARE
&& val
== 0 && !priv_can_clear_PA(pcr
)) {
299 mutex_exit(&p
->p_crlock
);
305 /* Committed to changing the flag */
308 if (flag
== PRIV_AWARE
) {
314 CR_FLAGS(cr
) = newflags
;
318 * Unsetting the flag has as side effect getting rid of
319 * the per-credential policy.
321 if (flag
== PRIV_XPOLICY
&& val
== 0)
322 crsetcrklpd(cr
, NULL
);
326 mutex_exit(&p
->p_crlock
);
334 * Getpflags. Currently only implements single bit flags.
337 getpflags(uint_t flag
, const cred_t
*cr
)
339 if (flag
!= PRIV_DEBUG
&& flag
!= PRIV_AWARE
&&
340 flag
!= PRIV_XPOLICY
&& flag
!= PRIV_PFEXEC
&&
341 flag
!= PRIV_AWARE_RESET
)
344 return ((CR_FLAGS(cr
) & flag
) != 0);
348 * Privilege system call entry point
351 privsys(int code
, priv_op_t op
, priv_ptype_t type
, void *buf
, size_t bufsize
,
355 extern int issetugid(void);
358 case PRIVSYS_SETPPRIV
:
359 if (bufsize
< sizeof (priv_set_t
))
360 return (set_errno(ENOMEM
));
361 return (setppriv(op
, type
, buf
));
362 case PRIVSYS_GETPPRIV
:
363 if (bufsize
< sizeof (priv_set_t
))
364 return (set_errno(ENOMEM
));
365 return (getppriv(type
, buf
));
366 case PRIVSYS_GETIMPLINFO
:
367 return (getprivimplinfo(buf
, bufsize
));
368 case PRIVSYS_SETPFLAGS
:
369 retv
= setpflags((uint_t
)op
, (uint_t
)type
, NULL
);
370 return (retv
!= 0 ? set_errno(retv
) : 0);
371 case PRIVSYS_GETPFLAGS
:
372 retv
= (int)getpflags((uint_t
)op
, CRED());
373 return (retv
== -1 ? set_errno(EINVAL
) : retv
);
374 case PRIVSYS_ISSETUGID
:
375 return (issetugid());
376 case PRIVSYS_KLPD_REG
:
377 if (bufsize
< sizeof (priv_set_t
))
378 return (set_errno(ENOMEM
));
379 return ((int)klpd_reg((int)op
, (idtype_t
)itype
, (id_t
)type
,
381 case PRIVSYS_KLPD_UNREG
:
382 return ((int)klpd_unreg((int)op
, (idtype_t
)itype
, (id_t
)type
));
383 case PRIVSYS_PFEXEC_REG
:
384 return ((int)pfexec_reg((int)op
));
385 case PRIVSYS_PFEXEC_UNREG
:
386 return ((int)pfexec_unreg((int)op
));
388 return (set_errno(EINVAL
));
391 #ifdef _SYSCALL32_IMPL
393 privsys32(int code
, priv_op_t op
, priv_ptype_t type
, caddr32_t buf
,
394 size32_t bufsize
, int itype
)
396 return (privsys(code
, op
, type
, (void *)(uintptr_t)buf
,
397 (size_t)bufsize
, itype
));