kern_prot.c revision 1.92 1 /* $NetBSD: kern_prot.c,v 1.92 2006/07/19 21:11:38 ad 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_prot.c 8.9 (Berkeley) 2/14/95
37 */
38
39 /*
40 * System calls related to processes and protection
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.92 2006/07/19 21:11:38 ad Exp $");
45
46 #include "opt_compat_43.h"
47
48 #include <sys/param.h>
49 #include <sys/acct.h>
50 #include <sys/systm.h>
51 #include <sys/ucred.h>
52 #include <sys/proc.h>
53 #include <sys/timeb.h>
54 #include <sys/times.h>
55 #include <sys/pool.h>
56 #include <sys/syslog.h>
57 #include <sys/resourcevar.h>
58 #include <sys/kauth.h>
59
60 #include <sys/mount.h>
61 #include <sys/sa.h>
62 #include <sys/syscallargs.h>
63
64 #include <sys/malloc.h>
65
66 int sys_getpid(struct lwp *, void *, register_t *);
67 int sys_getpid_with_ppid(struct lwp *, void *, register_t *);
68 int sys_getuid(struct lwp *, void *, register_t *);
69 int sys_getuid_with_euid(struct lwp *, void *, register_t *);
70 int sys_getgid(struct lwp *, void *, register_t *);
71 int sys_getgid_with_egid(struct lwp *, void *, register_t *);
72
73 static int grsortu(gid_t *, int);
74
75 /* ARGSUSED */
76 int
77 sys_getpid(struct lwp *l, void *v, register_t *retval)
78 {
79 struct proc *p = l->l_proc;
80
81 *retval = p->p_pid;
82 return (0);
83 }
84
85 /* ARGSUSED */
86 int
87 sys_getpid_with_ppid(struct lwp *l, void *v, register_t *retval)
88 {
89 struct proc *p = l->l_proc;
90
91 retval[0] = p->p_pid;
92 retval[1] = p->p_pptr->p_pid;
93 return (0);
94 }
95
96 /* ARGSUSED */
97 int
98 sys_getppid(struct lwp *l, void *v, register_t *retval)
99 {
100 struct proc *p = l->l_proc;
101
102 *retval = p->p_pptr->p_pid;
103 return (0);
104 }
105
106 /* Get process group ID; note that POSIX getpgrp takes no parameter */
107 int
108 sys_getpgrp(struct lwp *l, void *v, register_t *retval)
109 {
110 struct proc *p = l->l_proc;
111
112 *retval = p->p_pgrp->pg_id;
113 return (0);
114 }
115
116 /*
117 * Return the process group ID of the session leader (session ID)
118 * for the specified process.
119 */
120 int
121 sys_getsid(struct lwp *l, void *v, register_t *retval)
122 {
123 struct sys_getsid_args /* {
124 syscalldarg(pid_t) pid;
125 } */ *uap = v;
126 struct proc *p = l->l_proc;
127
128 if (SCARG(uap, pid) == 0)
129 goto found;
130 if ((p = pfind(SCARG(uap, pid))) == 0)
131 return (ESRCH);
132 found:
133 *retval = p->p_session->s_sid;
134 return (0);
135 }
136
137 int
138 sys_getpgid(struct lwp *l, void *v, register_t *retval)
139 {
140 struct sys_getpgid_args /* {
141 syscallarg(pid_t) pid;
142 } */ *uap = v;
143 struct proc *p = l->l_proc;
144
145 if (SCARG(uap, pid) == 0)
146 goto found;
147 if ((p = pfind(SCARG(uap, pid))) == 0)
148 return (ESRCH);
149 found:
150 *retval = p->p_pgid;
151 return (0);
152 }
153
154 /* ARGSUSED */
155 int
156 sys_getuid(struct lwp *l, void *v, register_t *retval)
157 {
158
159 *retval = kauth_cred_getuid(l->l_cred);
160 return (0);
161 }
162
163 /* ARGSUSED */
164 int
165 sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
166 {
167
168 retval[0] = kauth_cred_getuid(l->l_cred);
169 retval[1] = kauth_cred_geteuid(l->l_cred);
170 return (0);
171 }
172
173 /* ARGSUSED */
174 int
175 sys_geteuid(struct lwp *l, void *v, register_t *retval)
176 {
177
178 *retval = kauth_cred_geteuid(l->l_cred);
179 return (0);
180 }
181
182 /* ARGSUSED */
183 int
184 sys_getgid(struct lwp *l, void *v, register_t *retval)
185 {
186
187 *retval = kauth_cred_getgid(l->l_cred);
188 return (0);
189 }
190
191 /* ARGSUSED */
192 int
193 sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
194 {
195
196 retval[0] = kauth_cred_getgid(l->l_cred);
197 retval[1] = kauth_cred_getegid(l->l_cred);
198 return (0);
199 }
200
201 /*
202 * Get effective group ID. The "egid" is groups[0], and could be obtained
203 * via getgroups. This syscall exists because it is somewhat painful to do
204 * correctly in a library function.
205 */
206 /* ARGSUSED */
207 int
208 sys_getegid(struct lwp *l, void *v, register_t *retval)
209 {
210
211 *retval = kauth_cred_getegid(l->l_cred);
212 return (0);
213 }
214
215 int
216 sys_getgroups(struct lwp *l, void *v, register_t *retval)
217 {
218 struct sys_getgroups_args /* {
219 syscallarg(int) gidsetsize;
220 syscallarg(gid_t *) gidset;
221 } */ *uap = v;
222 kauth_cred_t cred = l->l_cred;
223 u_int ngrp;
224 int error;
225 gid_t *grbuf;
226
227 if (SCARG(uap, gidsetsize) == 0) {
228 *retval = kauth_cred_ngroups(cred);
229 return (0);
230 } else if (SCARG(uap, gidsetsize) < 0)
231 return (EINVAL);
232 ngrp = SCARG(uap, gidsetsize);
233 if (ngrp < kauth_cred_ngroups(cred))
234 return (EINVAL);
235 ngrp = kauth_cred_ngroups(cred);
236
237 grbuf = malloc(ngrp * sizeof(*grbuf), M_TEMP, M_WAITOK);
238 kauth_cred_getgroups(cred, grbuf, ngrp);
239 error = copyout(grbuf, (caddr_t)SCARG(uap, gidset),
240 ngrp * sizeof(gid_t));
241 free(grbuf, M_TEMP);
242 if (error)
243 return (error);
244 *retval = ngrp;
245 return (0);
246 }
247
248 /* ARGSUSED */
249 int
250 sys_setsid(struct lwp *l, void *v, register_t *retval)
251 {
252 struct proc *p = l->l_proc;
253
254 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
255 return (EPERM);
256 } else {
257 (void)enterpgrp(p, p->p_pid, 1);
258 *retval = p->p_pid;
259 return (0);
260 }
261 }
262
263 /*
264 * set process group (setpgid/old setpgrp)
265 *
266 * caller does setpgid(targpid, targpgid)
267 *
268 * pgid must be in valid range (EINVAL)
269 * pid must be caller or child of caller (ESRCH)
270 * if a child
271 * pid must be in same session (EPERM)
272 * pid can't have done an exec (EACCES)
273 * if pgid != pid
274 * there must exist some pid in same session having pgid (EPERM)
275 * pid must not be session leader (EPERM)
276 *
277 * Permission checks now in enterpgrp()
278 */
279 /* ARGSUSED */
280 int
281 sys_setpgid(struct lwp *l, void *v, register_t *retval)
282 {
283 struct sys_setpgid_args /* {
284 syscallarg(int) pid;
285 syscallarg(int) pgid;
286 } */ *uap = v;
287 struct proc *curp = l->l_proc;
288 struct proc *targp; /* target process */
289
290 if (SCARG(uap, pgid) < 0)
291 return EINVAL;
292
293 /* XXX MP - there is a horrid race here with targp exiting! */
294 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
295 targp = pfind(SCARG(uap, pid));
296 if (targp == NULL)
297 return ESRCH;
298 } else
299 targp = curp;
300
301 if (SCARG(uap, pgid) == 0)
302 SCARG(uap, pgid) = targp->p_pid;
303 return enterpgrp(targp, SCARG(uap, pgid), 0);
304 }
305
306 /*
307 * Set real, effective and saved uids to the requested values.
308 * non-root callers can only ever change uids to values that match
309 * one of the processes current uid values.
310 * This is further restricted by the flags argument.
311 */
312
313 int
314 do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
315 {
316 kauth_cred_t cred = l->l_cred;
317
318 /*
319 * check new value is one of the allowed existing values.
320 * otherwise, check if we have root privilege.
321 */
322 if ((r != -1
323 && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
324 && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
325 && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred))) ||
326 (e != -1
327 && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
328 && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
329 && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred))) ||
330 (sv != -1
331 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
332 && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
333 && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))) {
334 int error;
335
336 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
337 &l->l_acflag);
338 if (error != 0) {
339 return error;
340 }
341 }
342
343 /* If nothing has changed, short circuit the request */
344 if ((r == -1 || r == kauth_cred_getuid(cred))
345 && (e == -1 || e == kauth_cred_geteuid(cred))
346 && (sv == -1 || sv == kauth_cred_getsvuid(cred))) {
347 /* nothing to do */
348 return 0;
349 }
350
351 cred = kauth_cred_copy(cred);
352 l->l_cred = cred;
353
354 if (r != -1 && r != kauth_cred_getuid(cred)) {
355 /* Update count of processes for this user */
356 (void)chgproccnt(kauth_cred_getuid(cred), -1);
357 (void)chgproccnt(r, 1);
358 kauth_cred_setuid(cred, r);
359 }
360 if (sv != -1)
361 kauth_cred_setsvuid(cred, sv);
362 if (e != -1)
363 kauth_cred_seteuid(cred, e);
364
365 /* Broadcast our credentials to the process and other LWPs. */
366 lwp_broadcast_creds(l);
367
368 /* Mark process as having changed credentials, stops tracing etc */
369 p_sugid(l->l_proc);
370 return 0;
371 }
372
373 /*
374 * Set real, effective and saved gids to the requested values.
375 * non-root callers can only ever change gids to values that match
376 * one of the processes current gid values.
377 * This is further restricted by the flags argument.
378 */
379
380 int
381 do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
382 {
383 kauth_cred_t cred = l->l_cred;
384
385 /*
386 * check new value is one of the allowed existing values.
387 * otherwise, check if we have root privilege.
388 */
389 if ((r != -1
390 && !((flags & ID_R_EQ_R) && r == kauth_cred_getgid(cred))
391 && !((flags & ID_R_EQ_E) && r == kauth_cred_getegid(cred))
392 && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvgid(cred))) ||
393 (e != -1
394 && !((flags & ID_E_EQ_R) && e == kauth_cred_getgid(cred))
395 && !((flags & ID_E_EQ_E) && e == kauth_cred_getegid(cred))
396 && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvgid(cred))) ||
397 (sv != -1
398 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getgid(cred))
399 && !((flags & ID_S_EQ_E) && sv == kauth_cred_getegid(cred))
400 && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvgid(cred)))) {
401 int error;
402
403 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
404 &l->l_acflag);
405 if (error != 0) {
406 return error;
407 }
408 }
409
410 /* If nothing has changed, short circuit the request */
411 if ((r == -1 || r == kauth_cred_getgid(cred))
412 && (e == -1 || e == kauth_cred_getegid(cred))
413 && (sv == -1 || sv == kauth_cred_getsvgid(cred))) {
414 /* nothing to do */
415 return 0;
416 }
417
418 cred = kauth_cred_copy(cred);
419 l->l_cred = cred;
420
421 if (r != -1)
422 kauth_cred_setgid(cred, r);
423 if (sv != -1)
424 kauth_cred_setsvgid(cred, sv);
425 if (e != -1)
426 kauth_cred_setegid(cred, e);
427
428 /* Broadcast our credentials to the process and other LWPs. */
429 lwp_broadcast_creds(l);
430
431 /* Mark process as having changed credentials, stops tracing etc */
432 p_sugid(l->l_proc);
433 return 0;
434 }
435
436 /* ARGSUSED */
437 int
438 sys_setuid(struct lwp *l, void *v, register_t *retval)
439 {
440 struct sys_setuid_args /* {
441 syscallarg(uid_t) uid;
442 } */ *uap = v;
443 uid_t uid = SCARG(uap, uid);
444
445 return do_setresuid(l, uid, uid, uid,
446 ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
447 }
448
449 /* ARGSUSED */
450 int
451 sys_seteuid(struct lwp *l, void *v, register_t *retval)
452 {
453 struct sys_seteuid_args /* {
454 syscallarg(uid_t) euid;
455 } */ *uap = v;
456
457 return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
458 }
459
460 int
461 sys_setreuid(struct lwp *l, void *v, register_t *retval)
462 {
463 struct sys_setreuid_args /* {
464 syscallarg(uid_t) ruid;
465 syscallarg(uid_t) euid;
466 } */ *uap = v;
467 kauth_cred_t cred = l->l_cred;
468 uid_t ruid, euid, svuid;
469
470 ruid = SCARG(uap, ruid);
471 euid = SCARG(uap, euid);
472
473 if (ruid == -1)
474 ruid = kauth_cred_getuid(cred);
475 if (euid == -1)
476 euid = kauth_cred_geteuid(cred);
477
478 /* Saved uid is set to the new euid if the ruid changed */
479 svuid = (ruid == kauth_cred_getuid(cred)) ? -1 : euid;
480
481 return do_setresuid(l, ruid, euid, svuid,
482 ID_R_EQ_R | ID_R_EQ_E |
483 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
484 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
485 }
486
487 /* ARGSUSED */
488 int
489 sys_setgid(struct lwp *l, void *v, register_t *retval)
490 {
491 struct sys_setgid_args /* {
492 syscallarg(gid_t) gid;
493 } */ *uap = v;
494 gid_t gid = SCARG(uap, gid);
495
496 return do_setresgid(l, gid, gid, gid,
497 ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
498 }
499
500 /* ARGSUSED */
501 int
502 sys_setegid(struct lwp *l, void *v, register_t *retval)
503 {
504 struct sys_setegid_args /* {
505 syscallarg(gid_t) egid;
506 } */ *uap = v;
507
508 return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
509 }
510
511 int
512 sys_setregid(struct lwp *l, void *v, register_t *retval)
513 {
514 struct sys_setregid_args /* {
515 syscallarg(gid_t) rgid;
516 syscallarg(gid_t) egid;
517 } */ *uap = v;
518 kauth_cred_t cred = l->l_cred;
519 gid_t rgid, egid, svgid;
520
521 rgid = SCARG(uap, rgid);
522 egid = SCARG(uap, egid);
523
524 if (rgid == -1)
525 rgid = kauth_cred_getgid(cred);
526 if (egid == -1)
527 egid = kauth_cred_getegid(cred);
528
529 /* Saved gid is set to the new egid if the rgid changed */
530 svgid = rgid == kauth_cred_getgid(cred) ? -1 : egid;
531
532 return do_setresgid(l, rgid, egid, svgid,
533 ID_R_EQ_R | ID_R_EQ_E |
534 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
535 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
536 }
537
538 int
539 sys_issetugid(struct lwp *l, void *v, register_t *retval)
540 {
541 struct proc *p = l->l_proc;
542
543 /*
544 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
545 * we use P_SUGID because we consider changing the owners as
546 * "tainting" as well.
547 * This is significant for procs that start as root and "become"
548 * a user without an exec - programs cannot know *everything*
549 * that libc *might* have put in their data segment.
550 */
551 *retval = (p->p_flag & P_SUGID) != 0;
552 return (0);
553 }
554
555 /*
556 * sort -u for groups.
557 */
558 static int
559 grsortu(gid_t *grp, int ngrp)
560 {
561 const gid_t *src, *end;
562 gid_t *dst;
563 gid_t group;
564 int i, j;
565
566 /* bubble sort */
567 for (i = 0; i < ngrp; i++)
568 for (j = i + 1; j < ngrp; j++)
569 if (grp[i] > grp[j]) {
570 gid_t tmp = grp[i];
571 grp[i] = grp[j];
572 grp[j] = tmp;
573 }
574
575 /* uniq */
576 end = grp + ngrp;
577 src = grp;
578 dst = grp;
579 while (src < end) {
580 group = *src++;
581 while (src < end && *src == group)
582 src++;
583 *dst++ = group;
584 }
585
586 #ifdef DIAGNOSTIC
587 /* zero out the rest of the array */
588 (void)memset(dst, 0, sizeof(*grp) * (end - dst));
589 #endif
590
591 return dst - grp;
592 }
593
594 /* ARGSUSED */
595 int
596 sys_setgroups(struct lwp *l, void *v, register_t *retval)
597 {
598 struct sys_setgroups_args /* {
599 syscallarg(int) gidsetsize;
600 syscallarg(const gid_t *) gidset;
601 } */ *uap = v;
602 kauth_cred_t cred = l->l_cred;
603 int ngrp;
604 int error;
605 gid_t grp[NGROUPS];
606 size_t grsize;
607
608 if ((error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
609 &l->l_acflag)) != 0)
610 return (error);
611
612 ngrp = SCARG(uap, gidsetsize);
613 if ((u_int)ngrp > NGROUPS)
614 return (EINVAL);
615
616 grsize = ngrp * sizeof(gid_t);
617 error = copyin(SCARG(uap, gidset), grp, grsize);
618 if (error)
619 return (error);
620
621 ngrp = grsortu(grp, ngrp);
622
623 cred = kauth_cred_copy(cred);
624 l->l_cred = cred;
625 kauth_cred_setgroups(cred, grp, ngrp, -1);
626
627 /* Broadcast our credentials to the process and other LWPs. */
628 lwp_broadcast_creds(l);
629
630 /* Mark process as having changed credentials, stops tracing etc. */
631 p_sugid(l->l_proc);
632 return (0);
633 }
634
635 /*
636 * Get login name, if available.
637 */
638 /* ARGSUSED */
639 int
640 sys___getlogin(struct lwp *l, void *v, register_t *retval)
641 {
642 struct sys___getlogin_args /* {
643 syscallarg(char *) namebuf;
644 syscallarg(size_t) namelen;
645 } */ *uap = v;
646 struct proc *p = l->l_proc;
647
648 if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
649 SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
650 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
651 (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
652 }
653
654 /*
655 * Set login name.
656 */
657 /* ARGSUSED */
658 int
659 sys___setlogin(struct lwp *l, void *v, register_t *retval)
660 {
661 struct sys___setlogin_args /* {
662 syscallarg(const char *) namebuf;
663 } */ *uap = v;
664 struct proc *p = l->l_proc;
665 struct session *s = p->p_pgrp->pg_session;
666 char newname[sizeof s->s_login + 1];
667 int error;
668
669 if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
670 &l->l_acflag)) != 0)
671 return (error);
672 error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
673 if (error != 0)
674 return (error == ENAMETOOLONG ? EINVAL : error);
675
676 if (s->s_flags & S_LOGIN_SET && p->p_pid != s->s_sid &&
677 strncmp(newname, s->s_login, sizeof s->s_login) != 0)
678 log(LOG_WARNING, "%s (pid %d) changing logname from "
679 "%.*s to %s\n", p->p_comm, p->p_pid,
680 (int)sizeof s->s_login, s->s_login, newname);
681 s->s_flags |= S_LOGIN_SET;
682 strncpy(s->s_login, newname, sizeof s->s_login);
683 return (0);
684 }
685