kern_prot.c revision 1.61 1 /* $NetBSD: kern_prot.c,v 1.61 2000/12/01 20:34:17 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_prot.c 8.9 (Berkeley) 2/14/95
41 */
42
43 /*
44 * System calls related to processes and protection
45 */
46
47 #include "opt_compat_43.h"
48
49 #include <sys/param.h>
50 #include <sys/acct.h>
51 #include <sys/systm.h>
52 #include <sys/ucred.h>
53 #include <sys/proc.h>
54 #include <sys/timeb.h>
55 #include <sys/times.h>
56 #include <sys/malloc.h>
57
58 #include <sys/mount.h>
59 #include <sys/syscallargs.h>
60
61 /* ARGSUSED */
62 int
63 sys_getpid(p, v, retval)
64 struct proc *p;
65 void *v;
66 register_t *retval;
67 {
68
69 *retval = p->p_pid;
70 #ifndef COMPAT_43
71 if (p->p_emul->e_flags & EMUL_GETPID_PASS_PPID)
72 #endif
73
74 retval[1] = p->p_pptr->p_pid;
75 return (0);
76 }
77
78 /* ARGSUSED */
79 int
80 sys_getppid(p, v, retval)
81 struct proc *p;
82 void *v;
83 register_t *retval;
84 {
85
86 *retval = p->p_pptr->p_pid;
87 return (0);
88 }
89
90 /* Get process group ID; note that POSIX getpgrp takes no parameter */
91 int
92 sys_getpgrp(p, v, retval)
93 struct proc *p;
94 void *v;
95 register_t *retval;
96 {
97
98 *retval = p->p_pgrp->pg_id;
99 return (0);
100 }
101
102 /*
103 * Return the process group ID of the session leader (session ID)
104 * for the specified process.
105 */
106 int
107 sys_getsid(p, v, retval)
108 struct proc *p;
109 void *v;
110 register_t *retval;
111 {
112 struct sys_getsid_args /* {
113 syscalldarg(pid_t) pid;
114 } */ *uap = v;
115
116 if (SCARG(uap, pid) == 0)
117 goto found;
118 if ((p = pfind(SCARG(uap, pid))) == 0)
119 return (ESRCH);
120 found:
121 *retval = p->p_session->s_sid;
122 return (0);
123 }
124
125 int
126 sys_getpgid(p, v, retval)
127 struct proc *p;
128 void *v;
129 register_t *retval;
130 {
131 struct sys_getpgid_args /* {
132 syscallarg(pid_t) pid;
133 } */ *uap = v;
134
135 if (SCARG(uap, pid) == 0)
136 goto found;
137 if ((p = pfind(SCARG(uap, pid))) == 0)
138 return (ESRCH);
139 found:
140 *retval = p->p_pgid;
141 return (0);
142 }
143
144 /* ARGSUSED */
145 int
146 sys_getuid(p, v, retval)
147 struct proc *p;
148 void *v;
149 register_t *retval;
150 {
151
152 *retval = p->p_cred->p_ruid;
153 #ifndef COMPAT_43
154 if (p->p_emul->e_flags & EMUL_GETID_PASS_EID)
155 #endif
156 retval[1] = p->p_ucred->cr_uid;
157 return (0);
158 }
159
160 /* ARGSUSED */
161 int
162 sys_geteuid(p, v, retval)
163 struct proc *p;
164 void *v;
165 register_t *retval;
166 {
167
168 *retval = p->p_ucred->cr_uid;
169 return (0);
170 }
171
172 /* ARGSUSED */
173 int
174 sys_getgid(p, v, retval)
175 struct proc *p;
176 void *v;
177 register_t *retval;
178 {
179
180 *retval = p->p_cred->p_rgid;
181 #ifndef COMPAT_43
182 if (p->p_emul->e_flags & EMUL_GETID_PASS_EID)
183 #endif
184 retval[1] = p->p_ucred->cr_gid;
185 return (0);
186 }
187
188 /*
189 * Get effective group ID. The "egid" is groups[0], and could be obtained
190 * via getgroups. This syscall exists because it is somewhat painful to do
191 * correctly in a library function.
192 */
193 /* ARGSUSED */
194 int
195 sys_getegid(p, v, retval)
196 struct proc *p;
197 void *v;
198 register_t *retval;
199 {
200
201 *retval = p->p_ucred->cr_gid;
202 return (0);
203 }
204
205 int
206 sys_getgroups(p, v, retval)
207 struct proc *p;
208 void *v;
209 register_t *retval;
210 {
211 struct sys_getgroups_args /* {
212 syscallarg(int) gidsetsize;
213 syscallarg(gid_t *) gidset;
214 } */ *uap = v;
215 struct pcred *pc = p->p_cred;
216 int ngrp;
217 int error;
218
219 ngrp = SCARG(uap, gidsetsize);
220 if (ngrp == 0) {
221 *retval = pc->pc_ucred->cr_ngroups;
222 return (0);
223 }
224 if (ngrp < pc->pc_ucred->cr_ngroups)
225 return (EINVAL);
226 ngrp = pc->pc_ucred->cr_ngroups;
227 error = copyout((caddr_t)pc->pc_ucred->cr_groups,
228 (caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
229 if (error)
230 return (error);
231 *retval = ngrp;
232 return (0);
233 }
234
235 /* ARGSUSED */
236 int
237 sys_setsid(p, v, retval)
238 struct proc *p;
239 void *v;
240 register_t *retval;
241 {
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);
247 *retval = p->p_pid;
248 return (0);
249 }
250 }
251
252 /*
253 * set process group (setpgid/old setpgrp)
254 *
255 * caller does setpgid(targpid, targpgid)
256 *
257 * pgid must be in valid range (EINVAL)
258 * pid must be caller or child of caller (ESRCH)
259 * if a child
260 * pid must be in same session (EPERM)
261 * pid can't have done an exec (EACCES)
262 * if pgid != pid
263 * there must exist some pid in same session having pgid (EPERM)
264 * pid must not be session leader (EPERM)
265 */
266 /* ARGSUSED */
267 int
268 sys_setpgid(curp, v, retval)
269 struct proc *curp;
270 void *v;
271 register_t *retval;
272 {
273 struct sys_setpgid_args /* {
274 syscallarg(int) pid;
275 syscallarg(int) pgid;
276 } */ *uap = v;
277 struct proc *targp; /* target process */
278 struct pgrp *pgrp; /* target pgrp */
279
280 if (SCARG(uap, pgid) < 0)
281 return (EINVAL);
282
283 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
284 if ((targp = pfind(SCARG(uap, pid))) == 0
285 || !inferior(targp, curp))
286 return (ESRCH);
287 if (targp->p_session != curp->p_session)
288 return (EPERM);
289 if (targp->p_flag & P_EXEC)
290 return (EACCES);
291 } else
292 targp = curp;
293 if (SESS_LEADER(targp))
294 return (EPERM);
295 if (SCARG(uap, pgid) == 0)
296 SCARG(uap, pgid) = targp->p_pid;
297 else if (SCARG(uap, pgid) != targp->p_pid)
298 if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
299 pgrp->pg_session != curp->p_session)
300 return (EPERM);
301 return (enterpgrp(targp, SCARG(uap, pgid), 0));
302 }
303
304 /* ARGSUSED */
305 int
306 sys_setuid(p, v, retval)
307 struct proc *p;
308 void *v;
309 register_t *retval;
310 {
311 struct sys_setuid_args /* {
312 syscallarg(uid_t) uid;
313 } */ *uap = v;
314 struct pcred *pc = p->p_cred;
315 uid_t uid;
316 int error;
317
318 uid = SCARG(uap, uid);
319 if (uid != pc->p_ruid &&
320 (error = suser(pc->pc_ucred, &p->p_acflag)))
321 return (error);
322 /*
323 * Check if we are all set, and this is a no-op.
324 */
325 if (pc->p_ruid == uid && pc->p_svuid == uid &&
326 pc->pc_ucred->cr_uid == uid)
327 return (0);
328 /*
329 * Everything's okay, do it.
330 * Transfer proc count to new user.
331 * Copy credentials so other references do not see our changes.
332 */
333 (void)chgproccnt(pc->p_ruid, -1);
334 (void)chgproccnt(uid, 1);
335 pc->pc_ucred = crcopy(pc->pc_ucred);
336 pc->pc_ucred->cr_uid = uid;
337 pc->p_ruid = uid;
338 pc->p_svuid = uid;
339 p_sugid(p);
340 return (0);
341 }
342
343 /* ARGSUSED */
344 int
345 sys_seteuid(p, v, retval)
346 struct proc *p;
347 void *v;
348 register_t *retval;
349 {
350 struct sys_seteuid_args /* {
351 syscallarg(uid_t) euid;
352 } */ *uap = v;
353 struct pcred *pc = p->p_cred;
354 uid_t euid;
355 int error;
356
357 euid = SCARG(uap, euid);
358 if (euid != pc->p_ruid && euid != pc->p_svuid &&
359 (error = suser(pc->pc_ucred, &p->p_acflag)))
360 return (error);
361 /*
362 * Check if we are all set, and this is a no-op.
363 */
364 if (pc->pc_ucred->cr_uid == euid)
365 return (0);
366
367 /*
368 * Everything's okay, do it. Copy credentials so other references do
369 * not see our changes.
370 */
371 pc->pc_ucred = crcopy(pc->pc_ucred);
372 pc->pc_ucred->cr_uid = euid;
373 p_sugid(p);
374 return (0);
375 }
376
377 int
378 sys_setreuid(p, v, retval)
379 struct proc *p;
380 void *v;
381 register_t *retval;
382 {
383 struct sys_setreuid_args /* {
384 syscallarg(uid_t) ruid;
385 syscallarg(uid_t) euid;
386 } */ *uap = v;
387 struct pcred *pc = p->p_cred;
388 uid_t ruid, euid;
389 int error, changed = 0;
390
391 ruid = SCARG(uap, ruid);
392 euid = SCARG(uap, euid);
393
394 if (ruid != (uid_t)-1 &&
395 ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
396 (error = suser(pc->pc_ucred, &p->p_acflag)))
397 return (error);
398
399 if (euid != (uid_t)-1 &&
400 euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
401 euid != pc->p_svuid &&
402 (error = suser(pc->pc_ucred, &p->p_acflag)))
403 return (error);
404
405 if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
406 pc->pc_ucred = crcopy(pc->pc_ucred);
407 pc->pc_ucred->cr_uid = euid;
408 changed++;
409 }
410
411 if (ruid != (uid_t)-1 &&
412 (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
413 (void)chgproccnt(pc->p_ruid, -1);
414 (void)chgproccnt(ruid, 1);
415 pc->p_ruid = ruid;
416 pc->p_svuid = pc->pc_ucred->cr_uid;
417 changed++;
418 }
419
420 if (changed)
421 p_sugid(p);
422 return (0);
423 }
424
425 /* ARGSUSED */
426 int
427 sys_setgid(p, v, retval)
428 struct proc *p;
429 void *v;
430 register_t *retval;
431 {
432 struct sys_setgid_args /* {
433 syscallarg(gid_t) gid;
434 } */ *uap = v;
435 struct pcred *pc = p->p_cred;
436 gid_t gid;
437 int error;
438
439 gid = SCARG(uap, gid);
440 if (gid != pc->p_rgid &&
441 (error = suser(pc->pc_ucred, &p->p_acflag)))
442 return (error);
443 /*
444 * Check if we are all set, and this is a no-op.
445 */
446 if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
447 pc->p_svgid == gid)
448 return (0);
449
450 pc->pc_ucred = crcopy(pc->pc_ucred);
451 pc->pc_ucred->cr_gid = gid;
452 pc->p_rgid = gid;
453 pc->p_svgid = gid;
454 p_sugid(p);
455 return (0);
456 }
457
458 /* ARGSUSED */
459 int
460 sys_setegid(p, v, retval)
461 struct proc *p;
462 void *v;
463 register_t *retval;
464 {
465 struct sys_setegid_args /* {
466 syscallarg(gid_t) egid;
467 } */ *uap = v;
468 struct pcred *pc = p->p_cred;
469 gid_t egid;
470 int error;
471
472 egid = SCARG(uap, egid);
473 if (egid != pc->p_rgid && egid != pc->p_svgid &&
474 (error = suser(pc->pc_ucred, &p->p_acflag)))
475 return (error);
476 /*
477 * Check if we are all set, and this is a no-op.
478 */
479 if (pc->pc_ucred->cr_gid == egid)
480 return (0);
481
482 pc->pc_ucred = crcopy(pc->pc_ucred);
483 pc->pc_ucred->cr_gid = egid;
484 p_sugid(p);
485 return (0);
486 }
487
488 int
489 sys_setregid(p, v, retval)
490 struct proc *p;
491 void *v;
492 register_t *retval;
493 {
494 struct sys_setregid_args /* {
495 syscallarg(gid_t) rgid;
496 syscallarg(gid_t) egid;
497 } */ *uap = v;
498 struct pcred *pc = p->p_cred;
499 gid_t rgid, egid;
500 int error, changed = 0;
501
502 rgid = SCARG(uap, rgid);
503 egid = SCARG(uap, egid);
504
505 if (rgid != (gid_t)-1 &&
506 rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
507 (error = suser(pc->pc_ucred, &p->p_acflag)))
508 return (error);
509
510 if (egid != (gid_t)-1 &&
511 egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
512 egid != pc->p_svgid &&
513 (error = suser(pc->pc_ucred, &p->p_acflag)))
514 return (error);
515
516 if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
517 pc->pc_ucred = crcopy(pc->pc_ucred);
518 pc->pc_ucred->cr_gid = egid;
519 changed++;
520 }
521
522 if (rgid != (gid_t)-1 &&
523 (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
524 pc->p_rgid = rgid;
525 pc->p_svgid = pc->pc_ucred->cr_gid;
526 changed++;
527 }
528
529 if (changed)
530 p_sugid(p);
531 return (0);
532 }
533
534 int
535 sys_issetugid(p, v, retval)
536 struct proc *p;
537 void *v;
538 register_t *retval;
539 {
540 /*
541 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
542 * we use P_SUGID because we consider changing the owners as
543 * "tainting" as well.
544 * This is significant for procs that start as root and "become"
545 * a user without an exec - programs cannot know *everything*
546 * that libc *might* have put in their data segment.
547 */
548 *retval = (p->p_flag & P_SUGID) != 0;
549 return (0);
550 }
551
552 /* ARGSUSED */
553 int
554 sys_setgroups(p, v, retval)
555 struct proc *p;
556 void *v;
557 register_t *retval;
558 {
559 struct sys_setgroups_args /* {
560 syscallarg(int) gidsetsize;
561 syscallarg(const gid_t *) gidset;
562 } */ *uap = v;
563 struct pcred *pc = p->p_cred;
564 int ngrp;
565 int error;
566 gid_t grp[NGROUPS];
567 size_t grsize;
568
569 if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
570 return (error);
571
572 ngrp = SCARG(uap, gidsetsize);
573 if ((u_int)ngrp > NGROUPS)
574 return (EINVAL);
575
576 grsize = ngrp * sizeof(gid_t);
577 error = copyin(SCARG(uap, gidset), grp, grsize);
578 if (error)
579 return (error);
580 /*
581 * Check if this is a no-op.
582 */
583 if (pc->pc_ucred->cr_ngroups == ngrp &&
584 memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
585 return (0);
586
587 pc->pc_ucred = crcopy(pc->pc_ucred);
588 (void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
589 pc->pc_ucred->cr_ngroups = ngrp;
590 p_sugid(p);
591 return (0);
592 }
593
594 /*
595 * Check if gid is a member of the group set.
596 */
597 int
598 groupmember(gid, cred)
599 gid_t gid;
600 struct ucred *cred;
601 {
602 gid_t *gp;
603 gid_t *egp;
604
605 egp = &(cred->cr_groups[cred->cr_ngroups]);
606 for (gp = cred->cr_groups; gp < egp; gp++)
607 if (*gp == gid)
608 return (1);
609 return (0);
610 }
611
612 /*
613 * Test whether the specified credentials imply "super-user"
614 * privilege; if so, and we have accounting info, set the flag
615 * indicating use of super-powers.
616 * Returns 0 or error.
617 */
618 int
619 suser(cred, acflag)
620 struct ucred *cred;
621 u_short *acflag;
622 {
623 if (cred->cr_uid == 0) {
624 if (acflag)
625 *acflag |= ASU;
626 return (0);
627 }
628 return (EPERM);
629 }
630
631 /*
632 * Allocate a zeroed cred structure.
633 */
634 struct ucred *
635 crget()
636 {
637 struct ucred *cr;
638
639 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
640 memset((caddr_t)cr, 0, sizeof(*cr));
641 cr->cr_ref = 1;
642 return (cr);
643 }
644
645 /*
646 * Free a cred structure.
647 * Throws away space when ref count gets to 0.
648 */
649 void
650 crfree(cr)
651 struct ucred *cr;
652 {
653 int s;
654
655 s = splimp(); /* ??? */
656 if (--cr->cr_ref == 0)
657 FREE((caddr_t)cr, M_CRED);
658 (void) splx(s);
659 }
660
661 /*
662 * Copy cred structure to a new one and free the old one.
663 */
664 struct ucred *
665 crcopy(cr)
666 struct ucred *cr;
667 {
668 struct ucred *newcr;
669
670 if (cr->cr_ref == 1)
671 return (cr);
672 newcr = crget();
673 *newcr = *cr;
674 crfree(cr);
675 newcr->cr_ref = 1;
676 return (newcr);
677 }
678
679 /*
680 * Dup cred struct to a new held one.
681 */
682 struct ucred *
683 crdup(cr)
684 struct ucred *cr;
685 {
686 struct ucred *newcr;
687
688 newcr = crget();
689 *newcr = *cr;
690 newcr->cr_ref = 1;
691 return (newcr);
692 }
693
694 /*
695 * Get login name, if available.
696 */
697 /* ARGSUSED */
698 int
699 sys___getlogin(p, v, retval)
700 struct proc *p;
701 void *v;
702 register_t *retval;
703 {
704 struct sys___getlogin_args /* {
705 syscallarg(char *) namebuf;
706 syscallarg(size_t) namelen;
707 } */ *uap = v;
708
709 if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
710 SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
711 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
712 (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
713 }
714
715 /*
716 * Set login name.
717 */
718 /* ARGSUSED */
719 int
720 sys_setlogin(p, v, retval)
721 struct proc *p;
722 void *v;
723 register_t *retval;
724 {
725 struct sys_setlogin_args /* {
726 syscallarg(const char *) namebuf;
727 } */ *uap = v;
728 int error;
729
730 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
731 return (error);
732 error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
733 sizeof(p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
734 if (error == ENAMETOOLONG)
735 error = EINVAL;
736 return (error);
737 }
738