Disallow writes to filesystems mounted read-only via NULLFS. In this case
[dragonfly.git] / sys / kern / kern_prot.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $
753fd850 40 * $DragonFly: src/sys/kern/kern_prot.c,v 1.26 2006/06/05 07:26:10 dillon Exp $
984263bc
MD
41 */
42
43/*
44 * System calls related to processes and protection
45 */
46
47#include "opt_compat.h"
48
49#include <sys/param.h>
50#include <sys/acct.h>
51#include <sys/systm.h>
52#include <sys/sysproto.h>
53#include <sys/kernel.h>
3b564f1f 54#include <sys/lock.h>
984263bc
MD
55#include <sys/proc.h>
56#include <sys/malloc.h>
57#include <sys/pioctl.h>
58#include <sys/resourcevar.h>
41c20dac 59#include <sys/jail.h>
508ceb09 60#include <sys/lockf.h>
d9b2033e
MD
61#include <sys/spinlock.h>
62
63#include <sys/thread2.h>
64#include <sys/spinlock2.h>
984263bc
MD
65
66static MALLOC_DEFINE(M_CRED, "cred", "credentials");
67
984263bc
MD
68/*
69 * NOT MP SAFE due to p_pptr access
70 */
71/* ARGSUSED */
72int
753fd850 73sys_getpid(struct getpid_args *uap)
984263bc 74{
41c20dac 75 struct proc *p = curproc;
984263bc 76
c7114eea 77 uap->sysmsg_fds[0] = p->p_pid;
984263bc 78#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
c7114eea 79 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
984263bc
MD
80#endif
81 return (0);
82}
83
984263bc
MD
84/* ARGSUSED */
85int
753fd850 86sys_getppid(struct getppid_args *uap)
984263bc 87{
41c20dac 88 struct proc *p = curproc;
984263bc 89
c7114eea 90 uap->sysmsg_result = p->p_pptr->p_pid;
984263bc
MD
91 return (0);
92}
93
94/*
95 * Get process group ID; note that POSIX getpgrp takes no parameter
96 *
97 * MP SAFE
98 */
984263bc 99int
753fd850 100sys_getpgrp(struct getpgrp_args *uap)
984263bc 101{
41c20dac 102 struct proc *p = curproc;
984263bc 103
c7114eea 104 uap->sysmsg_result = p->p_pgrp->pg_id;
984263bc
MD
105 return (0);
106}
107
a64ba182
MD
108/*
109 * Get an arbitary pid's process group id
110 */
984263bc 111int
753fd850 112sys_getpgid(struct getpgid_args *uap)
984263bc 113{
41c20dac 114 struct proc *p = curproc;
984263bc
MD
115 struct proc *pt;
116
117 pt = p;
118 if (uap->pid == 0)
119 goto found;
120
121 if ((pt = pfind(uap->pid)) == 0)
122 return ESRCH;
123found:
c7114eea 124 uap->sysmsg_result = pt->p_pgrp->pg_id;
984263bc
MD
125 return 0;
126}
127
128/*
129 * Get an arbitary pid's session id.
130 */
984263bc 131int
753fd850 132sys_getsid(struct getsid_args *uap)
984263bc 133{
41c20dac 134 struct proc *p = curproc;
984263bc
MD
135 struct proc *pt;
136
137 pt = p;
138 if (uap->pid == 0)
139 goto found;
140
141 if ((pt = pfind(uap->pid)) == 0)
142 return ESRCH;
143found:
c7114eea 144 uap->sysmsg_result = pt->p_session->s_sid;
984263bc
MD
145 return 0;
146}
147
148
149/*
150 * getuid() - MP SAFE
151 */
984263bc
MD
152/* ARGSUSED */
153int
753fd850 154sys_getuid(struct getuid_args *uap)
984263bc 155{
41c20dac 156 struct proc *p = curproc;
984263bc 157
c7114eea 158 uap->sysmsg_fds[0] = p->p_ucred->cr_ruid;
984263bc 159#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
c7114eea 160 uap->sysmsg_fds[1] = p->p_ucred->cr_uid;
984263bc
MD
161#endif
162 return (0);
163}
164
165/*
166 * geteuid() - MP SAFE
167 */
984263bc
MD
168/* ARGSUSED */
169int
753fd850 170sys_geteuid(struct geteuid_args *uap)
984263bc 171{
41c20dac 172 struct proc *p = curproc;
984263bc 173
c7114eea 174 uap->sysmsg_result = p->p_ucred->cr_uid;
984263bc
MD
175 return (0);
176}
177
178/*
179 * getgid() - MP SAFE
180 */
984263bc
MD
181/* ARGSUSED */
182int
753fd850 183sys_getgid(struct getgid_args *uap)
984263bc 184{
41c20dac 185 struct proc *p = curproc;
984263bc 186
c7114eea 187 uap->sysmsg_fds[0] = p->p_ucred->cr_rgid;
984263bc 188#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
c7114eea 189 uap->sysmsg_fds[1] = p->p_ucred->cr_groups[0];
984263bc
MD
190#endif
191 return (0);
192}
193
194/*
195 * Get effective group ID. The "egid" is groups[0], and could be obtained
196 * via getgroups. This syscall exists because it is somewhat painful to do
197 * correctly in a library function.
6ad3ed15
DR
198 *
199 * MP SAFE
984263bc 200 */
984263bc
MD
201/* ARGSUSED */
202int
753fd850 203sys_getegid(struct getegid_args *uap)
984263bc 204{
41c20dac 205 struct proc *p = curproc;
984263bc 206
c7114eea 207 uap->sysmsg_result = p->p_ucred->cr_groups[0];
984263bc
MD
208 return (0);
209}
210
984263bc 211int
753fd850 212sys_getgroups(struct getgroups_args *uap)
984263bc 213{
41c20dac
MD
214 struct proc *p = curproc;
215 struct ucred *cr;
216 u_int ngrp;
984263bc
MD
217 int error;
218
41c20dac
MD
219 if (p == NULL) /* API enforcement */
220 return(EPERM);
221 cr = p->p_ucred;
222
984263bc 223 if ((ngrp = uap->gidsetsize) == 0) {
c7114eea 224 uap->sysmsg_result = cr->cr_ngroups;
984263bc
MD
225 return (0);
226 }
41c20dac 227 if (ngrp < cr->cr_ngroups)
984263bc 228 return (EINVAL);
41c20dac
MD
229 ngrp = cr->cr_ngroups;
230 if ((error = copyout((caddr_t)cr->cr_groups,
984263bc
MD
231 (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
232 return (error);
c7114eea 233 uap->sysmsg_result = ngrp;
984263bc
MD
234 return (0);
235}
236
984263bc
MD
237/* ARGSUSED */
238int
753fd850 239sys_setsid(struct setsid_args *uap)
984263bc 240{
41c20dac 241 struct proc *p = curproc;
984263bc
MD
242
243 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
244 return (EPERM);
245 } else {
246 (void)enterpgrp(p, p->p_pid, 1);
c7114eea 247 uap->sysmsg_result = p->p_pid;
984263bc
MD
248 return (0);
249 }
250}
251
252/*
253 * set process group (setpgid/old setpgrp)
254 *
255 * caller does setpgid(targpid, targpgid)
256 *
257 * pid must be caller or child of caller (ESRCH)
258 * if a child
259 * pid must be in same session (EPERM)
260 * pid can't have done an exec (EACCES)
261 * if pgid != pid
262 * there must exist some pid in same session having pgid (EPERM)
263 * pid must not be session leader (EPERM)
264 */
984263bc
MD
265/* ARGSUSED */
266int
753fd850 267sys_setpgid(struct setpgid_args *uap)
984263bc 268{
41c20dac
MD
269 struct proc *curp = curproc;
270 struct proc *targp; /* target process */
271 struct pgrp *pgrp; /* target pgrp */
984263bc
MD
272
273 if (uap->pgid < 0)
274 return (EINVAL);
275 if (uap->pid != 0 && uap->pid != curp->p_pid) {
276 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
277 return (ESRCH);
278 if (targp->p_pgrp == NULL || targp->p_session != curp->p_session)
279 return (EPERM);
280 if (targp->p_flag & P_EXEC)
281 return (EACCES);
282 } else
283 targp = curp;
284 if (SESS_LEADER(targp))
285 return (EPERM);
286 if (uap->pgid == 0)
287 uap->pgid = targp->p_pid;
288 else if (uap->pgid != targp->p_pid)
289 if ((pgrp = pgfind(uap->pgid)) == 0 ||
290 pgrp->pg_session != curp->p_session)
291 return (EPERM);
292 return (enterpgrp(targp, uap->pgid, 0));
293}
294
295/*
296 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
9a7c6212 297 * compatible. It says that setting the uid/gid to euid/egid is a special
984263bc
MD
298 * case of "appropriate privilege". Once the rules are expanded out, this
299 * basically means that setuid(nnn) sets all three id's, in all permitted
300 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
301 * does not set the saved id - this is dangerous for traditional BSD
302 * programs. For this reason, we *really* do not want to set
303 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
304 */
305#define POSIX_APPENDIX_B_4_2_2
306
984263bc
MD
307/* ARGSUSED */
308int
753fd850 309sys_setuid(struct setuid_args *uap)
984263bc 310{
41c20dac
MD
311 struct proc *p = curproc;
312 struct ucred *cr;
313 uid_t uid;
984263bc
MD
314 int error;
315
41c20dac
MD
316 if (p == NULL) /* API enforcement */
317 return(EPERM);
318 cr = p->p_ucred;
319
984263bc
MD
320 /*
321 * See if we have "permission" by POSIX 1003.1 rules.
322 *
323 * Note that setuid(geteuid()) is a special case of
324 * "appropriate privileges" in appendix B.4.2.2. We need
9a7c6212 325 * to use this clause to be compatible with traditional BSD
984263bc
MD
326 * semantics. Basically, it means that "setuid(xx)" sets all
327 * three id's (assuming you have privs).
328 *
329 * Notes on the logic. We do things in three steps.
330 * 1: We determine if the euid is going to change, and do EPERM
331 * right away. We unconditionally change the euid later if this
332 * test is satisfied, simplifying that part of the logic.
333 * 2: We determine if the real and/or saved uid's are going to
334 * change. Determined by compile options.
335 * 3: Change euid last. (after tests in #2 for "appropriate privs")
336 */
337 uid = uap->uid;
41c20dac 338 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
984263bc 339#ifdef _POSIX_SAVED_IDS
41c20dac 340 uid != crc->cr_svuid && /* allow setuid(saved gid) */
984263bc
MD
341#endif
342#ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
41c20dac 343 uid != cr->cr_uid && /* allow setuid(geteuid()) */
984263bc 344#endif
dadab5e9 345 (error = suser_cred(cr, PRISON_ROOT)))
984263bc
MD
346 return (error);
347
348#ifdef _POSIX_SAVED_IDS
349 /*
350 * Do we have "appropriate privileges" (are we root or uid == euid)
351 * If so, we are changing the real uid and/or saved uid.
352 */
353 if (
354#ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
41c20dac 355 uid == cr->cr_uid ||
984263bc 356#endif
dadab5e9 357 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
984263bc
MD
358#endif
359 {
360 /*
361 * Set the real uid and transfer proc count to new user.
362 */
41c20dac
MD
363 if (uid != cr->cr_ruid) {
364 change_ruid(uid);
365 setsugid();
984263bc
MD
366 }
367 /*
368 * Set saved uid
369 *
370 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
371 * the security of seteuid() depends on it. B.4.2.2 says it
372 * is important that we should do this.
373 */
41c20dac 374 if (cr->cr_svuid != uid) {
115ccd83 375 cr = cratom(&p->p_ucred);
41c20dac
MD
376 cr->cr_svuid = uid;
377 setsugid();
984263bc
MD
378 }
379 }
380
381 /*
382 * In all permitted cases, we are changing the euid.
383 * Copy credentials so other references do not see our changes.
384 */
41c20dac
MD
385 if (cr->cr_uid != uid) {
386 change_euid(uid);
387 setsugid();
984263bc
MD
388 }
389 return (0);
390}
391
984263bc
MD
392/* ARGSUSED */
393int
753fd850 394sys_seteuid(struct seteuid_args *uap)
984263bc 395{
41c20dac
MD
396 struct proc *p = curproc;
397 struct ucred *cr;
398 uid_t euid;
984263bc
MD
399 int error;
400
41c20dac
MD
401 if (p == NULL) /* API enforcement */
402 return(EPERM);
403
404 cr = p->p_ucred;
984263bc 405 euid = uap->euid;
41c20dac
MD
406 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
407 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
dadab5e9 408 (error = suser_cred(cr, PRISON_ROOT)))
984263bc
MD
409 return (error);
410 /*
411 * Everything's okay, do it. Copy credentials so other references do
412 * not see our changes.
413 */
41c20dac
MD
414 if (cr->cr_uid != euid) {
415 change_euid(euid);
416 setsugid();
984263bc
MD
417 }
418 return (0);
419}
420
984263bc
MD
421/* ARGSUSED */
422int
753fd850 423sys_setgid(struct setgid_args *uap)
984263bc 424{
41c20dac
MD
425 struct proc *p = curproc;
426 struct ucred *cr;
427 gid_t gid;
984263bc
MD
428 int error;
429
41c20dac
MD
430 if (p == NULL) /* API enforcement */
431 return(EPERM);
432 cr = p->p_ucred;
433
984263bc
MD
434 /*
435 * See if we have "permission" by POSIX 1003.1 rules.
436 *
437 * Note that setgid(getegid()) is a special case of
438 * "appropriate privileges" in appendix B.4.2.2. We need
9a7c6212 439 * to use this clause to be compatible with traditional BSD
984263bc
MD
440 * semantics. Basically, it means that "setgid(xx)" sets all
441 * three id's (assuming you have privs).
442 *
443 * For notes on the logic here, see setuid() above.
444 */
445 gid = uap->gid;
41c20dac 446 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
984263bc 447#ifdef _POSIX_SAVED_IDS
41c20dac 448 gid != cr->cr_svgid && /* allow setgid(saved gid) */
984263bc
MD
449#endif
450#ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
41c20dac 451 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
984263bc 452#endif
dadab5e9 453 (error = suser_cred(cr, PRISON_ROOT)))
984263bc
MD
454 return (error);
455
456#ifdef _POSIX_SAVED_IDS
457 /*
458 * Do we have "appropriate privileges" (are we root or gid == egid)
459 * If so, we are changing the real uid and saved gid.
460 */
461 if (
462#ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
41c20dac 463 gid == cr->cr_groups[0] ||
984263bc 464#endif
dadab5e9 465 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
984263bc
MD
466#endif
467 {
468 /*
469 * Set real gid
470 */
41c20dac 471 if (cr->cr_rgid != gid) {
115ccd83 472 cr = cratom(&p->p_ucred);
41c20dac
MD
473 cr->cr_rgid = gid;
474 setsugid();
984263bc
MD
475 }
476 /*
477 * Set saved gid
478 *
479 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
480 * the security of setegid() depends on it. B.4.2.2 says it
481 * is important that we should do this.
482 */
41c20dac 483 if (cr->cr_svgid != gid) {
115ccd83 484 cr = cratom(&p->p_ucred);
41c20dac
MD
485 cr->cr_svgid = gid;
486 setsugid();
984263bc
MD
487 }
488 }
489 /*
490 * In all cases permitted cases, we are changing the egid.
491 * Copy credentials so other references do not see our changes.
492 */
41c20dac 493 if (cr->cr_groups[0] != gid) {
e9a372eb 494 cr = cratom(&p->p_ucred);
41c20dac
MD
495 cr->cr_groups[0] = gid;
496 setsugid();
984263bc
MD
497 }
498 return (0);
499}
500
984263bc
MD
501/* ARGSUSED */
502int
753fd850 503sys_setegid(struct setegid_args *uap)
984263bc 504{
41c20dac
MD
505 struct proc *p = curproc;
506 struct ucred *cr;
507 gid_t egid;
984263bc
MD
508 int error;
509
41c20dac
MD
510 if (p == NULL) /* API enforcement */
511 return(EPERM);
512 cr = p->p_ucred;
513
984263bc 514 egid = uap->egid;
41c20dac
MD
515 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
516 egid != cr->cr_svgid && /* allow setegid(saved gid) */
dadab5e9 517 (error = suser_cred(cr, PRISON_ROOT)))
984263bc 518 return (error);
41c20dac 519 if (cr->cr_groups[0] != egid) {
e9a372eb 520 cr = cratom(&p->p_ucred);
41c20dac
MD
521 cr->cr_groups[0] = egid;
522 setsugid();
984263bc
MD
523 }
524 return (0);
525}
526
984263bc
MD
527/* ARGSUSED */
528int
753fd850 529sys_setgroups(struct setgroups_args *uap)
984263bc 530{
41c20dac
MD
531 struct proc *p = curproc;
532 struct ucred *cr;
533 u_int ngrp;
984263bc
MD
534 int error;
535
41c20dac
MD
536 if (p == NULL) /* API enforcement */
537 return(EPERM);
538 cr = p->p_ucred;
539
dadab5e9 540 if ((error = suser_cred(cr, PRISON_ROOT)))
984263bc
MD
541 return (error);
542 ngrp = uap->gidsetsize;
543 if (ngrp > NGROUPS)
544 return (EINVAL);
545 /*
546 * XXX A little bit lazy here. We could test if anything has
e9a372eb 547 * changed before cratom() and setting P_SUGID.
984263bc 548 */
e9a372eb 549 cr = cratom(&p->p_ucred);
984263bc
MD
550 if (ngrp < 1) {
551 /*
552 * setgroups(0, NULL) is a legitimate way of clearing the
553 * groups vector on non-BSD systems (which generally do not
554 * have the egid in the groups[0]). We risk security holes
555 * when running non-BSD software if we do not do the same.
556 */
41c20dac 557 cr->cr_ngroups = 1;
984263bc
MD
558 } else {
559 if ((error = copyin((caddr_t)uap->gidset,
41c20dac 560 (caddr_t)cr->cr_groups, ngrp * sizeof(gid_t))))
984263bc 561 return (error);
41c20dac 562 cr->cr_ngroups = ngrp;
984263bc 563 }
41c20dac 564 setsugid();
984263bc
MD
565 return (0);
566}
567
984263bc
MD
568/* ARGSUSED */
569int
753fd850 570sys_setreuid(struct setreuid_args *uap)
984263bc 571{
41c20dac
MD
572 struct proc *p = curproc;
573 struct ucred *cr;
574 uid_t ruid, euid;
984263bc
MD
575 int error;
576
41c20dac
MD
577 if (p == NULL) /* API enforcement */
578 return(EPERM);
579 cr = p->p_ucred;
580
984263bc
MD
581 ruid = uap->ruid;
582 euid = uap->euid;
41c20dac
MD
583 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
584 (euid != (uid_t)-1 && euid != cr->cr_uid &&
585 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
dadab5e9 586 (error = suser_cred(cr, PRISON_ROOT)) != 0)
984263bc
MD
587 return (error);
588
41c20dac
MD
589 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
590 change_euid(euid);
591 setsugid();
984263bc 592 }
41c20dac
MD
593 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
594 change_ruid(ruid);
595 setsugid();
984263bc 596 }
41c20dac
MD
597 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
598 cr->cr_svuid != cr->cr_uid) {
e9a372eb 599 cr = cratom(&p->p_ucred);
41c20dac
MD
600 cr->cr_svuid = cr->cr_uid;
601 setsugid();
984263bc
MD
602 }
603 return (0);
604}
605
984263bc
MD
606/* ARGSUSED */
607int
753fd850 608sys_setregid(struct setregid_args *uap)
984263bc 609{
41c20dac
MD
610 struct proc *p = curproc;
611 struct ucred *cr;
612 gid_t rgid, egid;
984263bc
MD
613 int error;
614
41c20dac
MD
615 if (p == NULL) /* API enforcement */
616 return(EPERM);
617 cr = p->p_ucred;
618
984263bc
MD
619 rgid = uap->rgid;
620 egid = uap->egid;
41c20dac
MD
621 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
622 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
623 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
dadab5e9 624 (error = suser_cred(cr, PRISON_ROOT)) != 0)
984263bc
MD
625 return (error);
626
41c20dac 627 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
e9a372eb 628 cr = cratom(&p->p_ucred);
41c20dac
MD
629 cr->cr_groups[0] = egid;
630 setsugid();
984263bc 631 }
41c20dac 632 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
e9a372eb 633 cr = cratom(&p->p_ucred);
41c20dac
MD
634 cr->cr_rgid = rgid;
635 setsugid();
984263bc 636 }
41c20dac
MD
637 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
638 cr->cr_svgid != cr->cr_groups[0]) {
e9a372eb 639 cr = cratom(&p->p_ucred);
41c20dac
MD
640 cr->cr_svgid = cr->cr_groups[0];
641 setsugid();
984263bc
MD
642 }
643 return (0);
644}
645
646/*
647 * setresuid(ruid, euid, suid) is like setreuid except control over the
648 * saved uid is explicit.
649 */
650
984263bc
MD
651/* ARGSUSED */
652int
753fd850 653sys_setresuid(struct setresuid_args *uap)
984263bc 654{
41c20dac
MD
655 struct proc *p = curproc;
656 struct ucred *cr;
657 uid_t ruid, euid, suid;
984263bc
MD
658 int error;
659
41c20dac 660 cr = p->p_ucred;
984263bc
MD
661 ruid = uap->ruid;
662 euid = uap->euid;
663 suid = uap->suid;
41c20dac
MD
664 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
665 ruid != cr->cr_uid) ||
666 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
667 euid != cr->cr_uid) ||
668 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
669 suid != cr->cr_uid)) &&
dadab5e9 670 (error = suser_cred(cr, PRISON_ROOT)) != 0)
984263bc 671 return (error);
41c20dac
MD
672 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
673 change_euid(euid);
674 setsugid();
984263bc 675 }
41c20dac
MD
676 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
677 change_ruid(ruid);
678 setsugid();
984263bc 679 }
41c20dac 680 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
e9a372eb 681 cr = cratom(&p->p_ucred);
41c20dac
MD
682 cr->cr_svuid = suid;
683 setsugid();
984263bc
MD
684 }
685 return (0);
686}
687
688/*
689 * setresgid(rgid, egid, sgid) is like setregid except control over the
690 * saved gid is explicit.
691 */
692
984263bc
MD
693/* ARGSUSED */
694int
753fd850 695sys_setresgid(struct setresgid_args *uap)
984263bc 696{
41c20dac
MD
697 struct proc *p = curproc;
698 struct ucred *cr;
699 gid_t rgid, egid, sgid;
984263bc
MD
700 int error;
701
41c20dac 702 cr = p->p_ucred;
984263bc
MD
703 rgid = uap->rgid;
704 egid = uap->egid;
705 sgid = uap->sgid;
41c20dac
MD
706 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
707 rgid != cr->cr_groups[0]) ||
708 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
709 egid != cr->cr_groups[0]) ||
710 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
711 sgid != cr->cr_groups[0])) &&
dadab5e9 712 (error = suser_cred(cr, PRISON_ROOT)) != 0)
984263bc
MD
713 return (error);
714
41c20dac 715 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
e9a372eb 716 cr = cratom(&p->p_ucred);
41c20dac
MD
717 cr->cr_groups[0] = egid;
718 setsugid();
984263bc 719 }
41c20dac 720 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
e9a372eb 721 cr = cratom(&p->p_ucred);
41c20dac
MD
722 cr->cr_rgid = rgid;
723 setsugid();
984263bc 724 }
41c20dac 725 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
e9a372eb 726 cr = cratom(&p->p_ucred);
41c20dac
MD
727 cr->cr_svgid = sgid;
728 setsugid();
984263bc
MD
729 }
730 return (0);
731}
732
984263bc
MD
733/* ARGSUSED */
734int
753fd850 735sys_getresuid(struct getresuid_args *uap)
984263bc 736{
41c20dac
MD
737 struct proc *p = curproc;
738 struct ucred *cr = p->p_ucred;
984263bc
MD
739 int error1 = 0, error2 = 0, error3 = 0;
740
741 if (uap->ruid)
41c20dac
MD
742 error1 = copyout((caddr_t)&cr->cr_ruid,
743 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
984263bc 744 if (uap->euid)
41c20dac
MD
745 error2 = copyout((caddr_t)&cr->cr_uid,
746 (caddr_t)uap->euid, sizeof(cr->cr_uid));
984263bc 747 if (uap->suid)
41c20dac
MD
748 error3 = copyout((caddr_t)&cr->cr_svuid,
749 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
984263bc
MD
750 return error1 ? error1 : (error2 ? error2 : error3);
751}
752
984263bc
MD
753/* ARGSUSED */
754int
753fd850 755sys_getresgid(struct getresgid_args *uap)
984263bc 756{
41c20dac
MD
757 struct proc *p = curproc;
758 struct ucred *cr = p->p_ucred;
984263bc
MD
759 int error1 = 0, error2 = 0, error3 = 0;
760
761 if (uap->rgid)
41c20dac
MD
762 error1 = copyout((caddr_t)&cr->cr_rgid,
763 (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
984263bc 764 if (uap->egid)
41c20dac
MD
765 error2 = copyout((caddr_t)&cr->cr_groups[0],
766 (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
984263bc 767 if (uap->sgid)
41c20dac
MD
768 error3 = copyout((caddr_t)&cr->cr_svgid,
769 (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
984263bc
MD
770 return error1 ? error1 : (error2 ? error2 : error3);
771}
772
773
984263bc
MD
774/* ARGSUSED */
775int
753fd850 776sys_issetugid(struct issetugid_args *uap)
984263bc 777{
41c20dac 778 struct proc *p = curproc;
984263bc
MD
779 /*
780 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
781 * we use P_SUGID because we consider changing the owners as
782 * "tainting" as well.
783 * This is significant for procs that start as root and "become"
784 * a user without an exec - programs cannot know *everything*
785 * that libc *might* have put in their data segment.
786 */
c7114eea 787 uap->sysmsg_result = (p->p_flag & P_SUGID) ? 1 : 0;
984263bc
MD
788 return (0);
789}
790
791/*
792 * Check if gid is a member of the group set.
793 */
794int
41c20dac 795groupmember(gid_t gid, struct ucred *cred)
984263bc 796{
41c20dac 797 gid_t *gp;
984263bc
MD
798 gid_t *egp;
799
800 egp = &(cred->cr_groups[cred->cr_ngroups]);
4f645dbb 801 for (gp = cred->cr_groups; gp < egp; gp++) {
984263bc
MD
802 if (*gp == gid)
803 return (1);
4f645dbb 804 }
984263bc
MD
805 return (0);
806}
807
808/*
809 * Test whether the specified credentials imply "super-user"
810 * privilege; if so, and we have accounting info, set the flag
dadab5e9
MD
811 * indicating use of super-powers. A kernel thread without a process
812 * context is assumed to have super user capabilities. In situations
813 * where the caller always expect a cred to exist, the cred should be
814 * passed separately and suser_cred()should be used instead of suser().
815 *
984263bc
MD
816 * Returns 0 or error.
817 */
818int
dadab5e9 819suser(struct thread *td)
984263bc 820{
dadab5e9 821 struct proc *p = td->td_proc;
41c20dac
MD
822
823 if (p != NULL) {
dadab5e9 824 return suser_cred(p->p_ucred, 0);
41c20dac 825 } else {
dadab5e9 826 return (0);
41c20dac 827 }
984263bc
MD
828}
829
e4700d00
JH
830/*
831 * A non-null credential is expected unless NULL_CRED_OKAY is set.
832 */
984263bc 833int
dadab5e9 834suser_cred(struct ucred *cred, int flag)
984263bc 835{
e4700d00
JH
836 KASSERT(cred != NULL || flag & NULL_CRED_OKAY,
837 ("suser_cred: NULL cred!"));
41c20dac 838
e4700d00
JH
839 if (cred == NULL) {
840 if (flag & NULL_CRED_OKAY)
841 return (0);
842 else
843 return (EPERM);
844 }
984263bc
MD
845 if (cred->cr_uid != 0)
846 return (EPERM);
41c20dac 847 if (cred->cr_prison && !(flag & PRISON_ROOT))
984263bc 848 return (EPERM);
dadab5e9 849 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
984263bc
MD
850 return (0);
851}
852
853/*
854 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
855 */
984263bc 856int
41c20dac 857p_trespass(struct ucred *cr1, struct ucred *cr2)
984263bc 858{
41c20dac 859 if (cr1 == cr2)
984263bc 860 return (0);
41c20dac 861 if (!PRISON_CHECK(cr1, cr2))
984263bc 862 return (ESRCH);
41c20dac 863 if (cr1->cr_ruid == cr2->cr_ruid)
984263bc 864 return (0);
41c20dac 865 if (cr1->cr_uid == cr2->cr_ruid)
984263bc 866 return (0);
41c20dac 867 if (cr1->cr_ruid == cr2->cr_uid)
984263bc 868 return (0);
41c20dac 869 if (cr1->cr_uid == cr2->cr_uid)
984263bc 870 return (0);
dadab5e9 871 if (suser_cred(cr1, PRISON_ROOT) == 0)
984263bc
MD
872 return (0);
873 return (EPERM);
874}
875
d9b2033e
MD
876/*
877 * MPSAFE
878 */
879static __inline void
880_crinit(struct ucred *cr)
881{
882 bzero(cr, sizeof(*cr));
883 cr->cr_ref = 1;
884 spin_init(&cr->cr_spin);
885}
886
887/*
888 * MPSAFE
889 */
890void
891crinit(struct ucred *cr)
892{
893 _crinit(cr);
894}
895
984263bc
MD
896/*
897 * Allocate a zeroed cred structure.
d9b2033e
MD
898 *
899 * MPSAFE
984263bc
MD
900 */
901struct ucred *
77153250 902crget(void)
984263bc 903{
1fd87d54 904 struct ucred *cr;
984263bc
MD
905
906 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
d9b2033e 907 _crinit(cr);
984263bc
MD
908 return (cr);
909}
910
911/*
e9a372eb
MD
912 * Claim another reference to a ucred structure. Can be used with special
913 * creds.
d9b2033e
MD
914 *
915 * It must be possible to call this routine with spinlocks held, meaning
916 * that this routine itself cannot obtain a spinlock.
917 *
918 * MPSAFE
984263bc 919 */
dadab5e9 920struct ucred *
41c20dac 921crhold(struct ucred *cr)
984263bc 922{
e9a372eb 923 if (cr != NOCRED && cr != FSCRED)
d9b2033e 924 atomic_add_int(&cr->cr_ref, 1);
dadab5e9 925 return(cr);
984263bc
MD
926}
927
928/*
d9b2033e
MD
929 * Drop a reference from the cred structure, free it if the reference count
930 * reaches 0.
931 *
932 * NOTE: because we used atomic_add_int() above, without a spinlock, we
933 * must also use atomic_subtract_int() below. A spinlock is required
934 * in crfree() to handle multiple callers racing the refcount to 0.
935 *
936 * MPALMOSTSAFE - acquires mplock on 1->0 transition of ref count
984263bc
MD
937 */
938void
41c20dac 939crfree(struct ucred *cr)
984263bc 940{
d9b2033e 941 if (cr->cr_ref <= 0)
984263bc 942 panic("Freeing already free credential! %p", cr);
d9b2033e
MD
943 spin_lock_wr(&cr->cr_spin);
944 atomic_subtract_int(&cr->cr_ref, 1);
945 if (cr->cr_ref == 0) {
946 spin_unlock_wr(&cr->cr_spin);
984263bc
MD
947 /*
948 * Some callers of crget(), such as nfs_statfs(),
949 * allocate a temporary credential, but don't
950 * allocate a uidinfo structure.
951 */
d9b2033e 952 get_mplock();
792033e7
MD
953 if (cr->cr_uidinfo != NULL) {
954 uidrop(cr->cr_uidinfo);
955 cr->cr_uidinfo = NULL;
956 }
957 if (cr->cr_ruidinfo != NULL) {
958 uidrop(cr->cr_ruidinfo);
959 cr->cr_ruidinfo = NULL;
960 }
41c20dac
MD
961
962 /*
963 * Destroy empty prisons
964 */
b40e316c
JS
965 if (jailed(cr))
966 prison_free(cr->cr_prison);
41c20dac
MD
967 cr->cr_prison = NULL; /* safety */
968
984263bc 969 FREE((caddr_t)cr, M_CRED);
d9b2033e
MD
970 rel_mplock();
971 } else {
972 spin_unlock_wr(&cr->cr_spin);
984263bc
MD
973 }
974}
975
e9a372eb
MD
976/*
977 * Atomize a cred structure so it can be modified without polluting
978 * other references to it.
979 */
980struct ucred *
981cratom(struct ucred **pcr)
982{
983 struct ucred *oldcr;
984 struct ucred *newcr;
985
986 oldcr = *pcr;
987 if (oldcr->cr_ref == 1)
988 return (oldcr);
989 newcr = crget();
990 *newcr = *oldcr;
991 if (newcr->cr_uidinfo)
992 uihold(newcr->cr_uidinfo);
993 if (newcr->cr_ruidinfo)
994 uihold(newcr->cr_ruidinfo);
b40e316c
JS
995 if (jailed(newcr))
996 prison_hold(newcr->cr_prison);
e9a372eb
MD
997 newcr->cr_ref = 1;
998 crfree(oldcr);
999 *pcr = newcr;
1000 return (newcr);
1001}
1002
1003#if 0 /* no longer used but keep around for a little while */
984263bc
MD
1004/*
1005 * Copy cred structure to a new one and free the old one.
1006 */
1007struct ucred *
41c20dac 1008crcopy(struct ucred *cr)
984263bc
MD
1009{
1010 struct ucred *newcr;
1011
1012 if (cr->cr_ref == 1)
1013 return (cr);
1014 newcr = crget();
1015 *newcr = *cr;
41c20dac
MD
1016 if (newcr->cr_uidinfo)
1017 uihold(newcr->cr_uidinfo);
1018 if (newcr->cr_ruidinfo)
1019 uihold(newcr->cr_ruidinfo);
b40e316c
JS
1020 if (jailed(newcr))
1021 prison_hold(newcr->cr_prison);
984263bc 1022 newcr->cr_ref = 1;
41c20dac 1023 crfree(cr);
984263bc
MD
1024 return (newcr);
1025}
e9a372eb 1026#endif
984263bc
MD
1027
1028/*
1029 * Dup cred struct to a new held one.
1030 */
1031struct ucred *
77153250 1032crdup(struct ucred *cr)
984263bc
MD
1033{
1034 struct ucred *newcr;
1035
1036 newcr = crget();
1037 *newcr = *cr;
41c20dac
MD
1038 if (newcr->cr_uidinfo)
1039 uihold(newcr->cr_uidinfo);
1040 if (newcr->cr_ruidinfo)
1041 uihold(newcr->cr_ruidinfo);
b40e316c
JS
1042 if (jailed(newcr))
1043 prison_hold(newcr->cr_prison);
984263bc
MD
1044 newcr->cr_ref = 1;
1045 return (newcr);
1046}
1047
1048/*
1049 * Fill in a struct xucred based on a struct ucred.
1050 */
1051void
77153250 1052cru2x(struct ucred *cr, struct xucred *xcr)
984263bc
MD
1053{
1054
1055 bzero(xcr, sizeof(*xcr));
1056 xcr->cr_version = XUCRED_VERSION;
1057 xcr->cr_uid = cr->cr_uid;
1058 xcr->cr_ngroups = cr->cr_ngroups;
1059 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1060}
1061
1062/*
1063 * Get login name, if available.
1064 */
984263bc
MD
1065/* ARGSUSED */
1066int
753fd850 1067sys_getlogin(struct getlogin_args *uap)
984263bc 1068{
41c20dac 1069 struct proc *p = curproc;
984263bc
MD
1070
1071 if (uap->namelen > MAXLOGNAME)
1072 uap->namelen = MAXLOGNAME;
1073 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1074 (caddr_t) uap->namebuf, uap->namelen));
1075}
1076
1077/*
1078 * Set login name.
1079 */
984263bc
MD
1080/* ARGSUSED */
1081int
753fd850 1082sys_setlogin(struct setlogin_args *uap)
984263bc 1083{
41c20dac 1084 struct proc *p = curproc;
984263bc
MD
1085 int error;
1086 char logintmp[MAXLOGNAME];
1087
41c20dac 1088 KKASSERT(p != NULL);
dadab5e9 1089 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
984263bc
MD
1090 return (error);
1091 error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1092 sizeof(logintmp), (size_t *)0);
1093 if (error == ENAMETOOLONG)
1094 error = EINVAL;
1095 else if (!error)
1096 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1097 sizeof(logintmp));
1098 return (error);
1099}
1100
1101void
77153250 1102setsugid(void)
984263bc 1103{
41c20dac
MD
1104 struct proc *p = curproc;
1105
1106 KKASSERT(p != NULL);
984263bc
MD
1107 p->p_flag |= P_SUGID;
1108 if (!(p->p_pfsflags & PF_ISUGID))
1109 p->p_stops = 0;
1110}
1111
1112/*
1113 * Helper function to change the effective uid of a process
1114 */
1115void
41c20dac 1116change_euid(uid_t euid)
984263bc 1117{
41c20dac
MD
1118 struct proc *p = curproc;
1119 struct ucred *cr;
984263bc 1120
41c20dac 1121 KKASSERT(p != NULL);
94507578 1122 lf_count_adjust(p, 0);
e9a372eb 1123 cr = cratom(&p->p_ucred);
41c20dac 1124 cr->cr_uid = euid;
792033e7 1125 uireplace(&cr->cr_uidinfo, uifind(euid));
94507578 1126 lf_count_adjust(p, 1);
984263bc
MD
1127}
1128
1129/*
1130 * Helper function to change the real uid of a process
1131 *
1132 * The per-uid process count for this process is transfered from
1133 * the old uid to the new uid.
1134 */
1135void
41c20dac 1136change_ruid(uid_t ruid)
984263bc 1137{
41c20dac
MD
1138 struct proc *p = curproc;
1139 struct ucred *cr;
984263bc 1140
41c20dac
MD
1141 KKASSERT(p != NULL);
1142
e9a372eb 1143 cr = cratom(&p->p_ucred);
41c20dac 1144 (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
984263bc 1145 /* It is assumed that pcred is not shared between processes */
41c20dac 1146 cr->cr_ruid = ruid;
792033e7 1147 uireplace(&cr->cr_ruidinfo, uifind(ruid));
41c20dac 1148 (void)chgproccnt(cr->cr_ruidinfo, 1, 0);
984263bc 1149}