kern_prot.c revision 1.88.10.5 1 /* $NetBSD: kern_prot.c,v 1.88.10.5 2006/03/14 02:52:47 elad 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.88.10.5 2006/03/14 02:52:47 elad 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
59 #include <sys/mount.h>
60 #include <sys/sa.h>
61 #include <sys/syscallargs.h>
62
63 #include <sys/malloc.h>
64
65 POOL_INIT(cred_pool, sizeof(struct ucred), 0, 0, 0, "credpl",
66 &pool_allocator_nointr);
67
68 int sys_getpid(struct lwp *, void *, register_t *);
69 int sys_getpid_with_ppid(struct lwp *, void *, register_t *);
70 int sys_getuid(struct lwp *, void *, register_t *);
71 int sys_getuid_with_euid(struct lwp *, void *, register_t *);
72 int sys_getgid(struct lwp *, void *, register_t *);
73 int sys_getgid_with_egid(struct lwp *, void *, register_t *);
74
75 static int grsortu(gid_t *, int);
76
77 /* ARGSUSED */
78 int
79 sys_getpid(struct lwp *l, void *v, register_t *retval)
80 {
81 struct proc *p = l->l_proc;
82
83 *retval = p->p_pid;
84 return (0);
85 }
86
87 /* ARGSUSED */
88 int
89 sys_getpid_with_ppid(struct lwp *l, void *v, register_t *retval)
90 {
91 struct proc *p = l->l_proc;
92
93 retval[0] = p->p_pid;
94 retval[1] = p->p_pptr->p_pid;
95 return (0);
96 }
97
98 /* ARGSUSED */
99 int
100 sys_getppid(struct lwp *l, void *v, register_t *retval)
101 {
102 struct proc *p = l->l_proc;
103
104 *retval = p->p_pptr->p_pid;
105 return (0);
106 }
107
108 /* Get process group ID; note that POSIX getpgrp takes no parameter */
109 int
110 sys_getpgrp(struct lwp *l, void *v, register_t *retval)
111 {
112 struct proc *p = l->l_proc;
113
114 *retval = p->p_pgrp->pg_id;
115 return (0);
116 }
117
118 /*
119 * Return the process group ID of the session leader (session ID)
120 * for the specified process.
121 */
122 int
123 sys_getsid(struct lwp *l, void *v, register_t *retval)
124 {
125 struct sys_getsid_args /* {
126 syscalldarg(pid_t) pid;
127 } */ *uap = v;
128 struct proc *p = l->l_proc;
129
130 if (SCARG(uap, pid) == 0)
131 goto found;
132 if ((p = pfind(SCARG(uap, pid))) == 0)
133 return (ESRCH);
134 found:
135 *retval = p->p_session->s_sid;
136 return (0);
137 }
138
139 int
140 sys_getpgid(struct lwp *l, void *v, register_t *retval)
141 {
142 struct sys_getpgid_args /* {
143 syscallarg(pid_t) pid;
144 } */ *uap = v;
145 struct proc *p = l->l_proc;
146
147 if (SCARG(uap, pid) == 0)
148 goto found;
149 if ((p = pfind(SCARG(uap, pid))) == 0)
150 return (ESRCH);
151 found:
152 *retval = p->p_pgid;
153 return (0);
154 }
155
156 /* ARGSUSED */
157 int
158 sys_getuid(struct lwp *l, void *v, register_t *retval)
159 {
160 struct proc *p = l->l_proc;
161
162 *retval = kauth_cred_getuid(p->p_cred);
163 return (0);
164 }
165
166 /* ARGSUSED */
167 int
168 sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
169 {
170 struct proc *p = l->l_proc;
171
172 retval[0] = kauth_cred_getuid(p->p_cred);
173 retval[1] = kauth_cred_geteuid(p->p_cred);
174 return (0);
175 }
176
177 /* ARGSUSED */
178 int
179 sys_geteuid(struct lwp *l, void *v, register_t *retval)
180 {
181 struct proc *p = l->l_proc;
182
183 *retval = kauth_cred_geteuid(p->p_cred);
184 return (0);
185 }
186
187 /* ARGSUSED */
188 int
189 sys_getgid(struct lwp *l, void *v, register_t *retval)
190 {
191 struct proc *p = l->l_proc;
192
193 *retval = kauth_cred_getgid(p->p_cred);
194 return (0);
195 }
196
197 /* ARGSUSED */
198 int
199 sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
200 {
201 struct proc *p = l->l_proc;
202
203 retval[0] = kauth_cred_getgid(p->p_cred);
204 retval[1] = kauth_cred_getegid(p->p_cred);
205 return (0);
206 }
207
208 /*
209 * Get effective group ID. The "egid" is groups[0], and could be obtained
210 * via getgroups. This syscall exists because it is somewhat painful to do
211 * correctly in a library function.
212 */
213 /* ARGSUSED */
214 int
215 sys_getegid(struct lwp *l, void *v, register_t *retval)
216 {
217 struct proc *p = l->l_proc;
218
219 *retval = kauth_cred_getegid(p->p_cred);
220 return (0);
221 }
222
223 int
224 sys_getgroups(struct lwp *l, void *v, register_t *retval)
225 {
226 struct sys_getgroups_args /* {
227 syscallarg(int) gidsetsize;
228 syscallarg(gid_t *) gidset;
229 } */ *uap = v;
230 struct proc *p = l->l_proc;
231 kauth_cred_t pc = p->p_cred;
232 u_int ngrp;
233 int error;
234 gid_t *grbuf;
235
236 if (SCARG(uap, gidsetsize) == 0) {
237 *retval = kauth_cred_ngroups(pc);
238 return (0);
239 } else if (SCARG(uap, gidsetsize) < 0)
240 return (EINVAL);
241 ngrp = SCARG(uap, gidsetsize);
242 if (ngrp < kauth_cred_ngroups(pc))
243 return (EINVAL);
244 ngrp = kauth_cred_ngroups(pc);
245
246 grbuf = malloc(ngrp * sizeof(*grbuf), M_TEMP, M_WAITOK);
247 kauth_cred_getgroups(pc, grbuf, ngrp);
248 error = copyout(grbuf, (caddr_t)SCARG(uap, gidset),
249 ngrp * sizeof(gid_t));
250 free(grbuf, M_TEMP);
251 if (error)
252 return (error);
253 *retval = ngrp;
254 return (0);
255 }
256
257 /* ARGSUSED */
258 int
259 sys_setsid(struct lwp *l, void *v, register_t *retval)
260 {
261 struct proc *p = l->l_proc;
262
263 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
264 return (EPERM);
265 } else {
266 (void)enterpgrp(p, p->p_pid, 1);
267 *retval = p->p_pid;
268 return (0);
269 }
270 }
271
272 /*
273 * set process group (setpgid/old setpgrp)
274 *
275 * caller does setpgid(targpid, targpgid)
276 *
277 * pgid must be in valid range (EINVAL)
278 * pid must be caller or child of caller (ESRCH)
279 * if a child
280 * pid must be in same session (EPERM)
281 * pid can't have done an exec (EACCES)
282 * if pgid != pid
283 * there must exist some pid in same session having pgid (EPERM)
284 * pid must not be session leader (EPERM)
285 *
286 * Permission checks now in enterpgrp()
287 */
288 /* ARGSUSED */
289 int
290 sys_setpgid(struct lwp *l, void *v, register_t *retval)
291 {
292 struct sys_setpgid_args /* {
293 syscallarg(int) pid;
294 syscallarg(int) pgid;
295 } */ *uap = v;
296 struct proc *curp = l->l_proc;
297 struct proc *targp; /* target process */
298
299 if (SCARG(uap, pgid) < 0)
300 return EINVAL;
301
302 /* XXX MP - there is a horrid race here with targp exiting! */
303 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
304 targp = pfind(SCARG(uap, pid));
305 if (targp == NULL)
306 return ESRCH;
307 } else
308 targp = curp;
309
310 if (SCARG(uap, pgid) == 0)
311 SCARG(uap, pgid) = targp->p_pid;
312 return enterpgrp(targp, SCARG(uap, pgid), 0);
313 }
314
315 /*
316 * Set real, effective and saved uids to the requested values.
317 * non-root callers can only ever change uids to values that match
318 * one of the processes current uid values.
319 * This is further restricted by the flags argument.
320 */
321
322 int
323 do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
324 {
325 int error;
326 struct proc *p = l->l_proc;
327 kauth_cred_t cred = p->p_cred;
328
329 /* Superuser can do anything it wants to.... */
330 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag);
331 if (error) {
332 /* Otherwise check new value is one of the allowed
333 existing values. */
334 if (r != -1 && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
335 && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
336 && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred)))
337 return error;
338 if (e != -1 && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
339 && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
340 && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred)))
341 return error;
342 if (sv != -1 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
343 && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
344 && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))
345 return error;
346 }
347
348 /* If nothing has changed, short circuit the request */
349 if ((r == -1 || r == kauth_cred_getuid(cred))
350 && (e == -1 || e == kauth_cred_geteuid(cred))
351 && (sv == -1 || sv == kauth_cred_getsvuid(cred)))
352 /* nothing to do */
353 return 0;
354
355 /* The pcred structure is not actually shared... */
356 if (r != -1 && r != kauth_cred_getuid(cred)) {
357 /* Update count of processes for this user */
358 (void)chgproccnt(kauth_cred_getuid(cred), -1);
359 (void)chgproccnt(r, 1);
360 kauth_cred_setuid(cred, r);
361 }
362 if (sv != -1)
363 kauth_cred_setsvuid(cred, sv);
364 if (e != -1 && e != kauth_cred_geteuid(cred)) {
365 /* Update a clone of the current credentials */
366 cred = kauth_cred_copy(cred);
367 kauth_cred_seteuid(cred, e);
368 p->p_cred = cred;
369 }
370
371 /* Mark process as having changed credentials, stops tracing etc */
372 p_sugid(p);
373 return 0;
374 }
375
376 /*
377 * Set real, effective and saved gids to the requested values.
378 * non-root callers can only ever change gids to values that match
379 * one of the processes current gid values.
380 * This is further restricted by the flags argument.
381 */
382
383 int
384 do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
385 {
386 int error;
387 struct proc *p = l->l_proc;
388 kauth_cred_t cred = p->p_cred;
389
390 /* Superuser can do anything it wants to.... */
391 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag);
392 if (error) {
393 /* Otherwise check new value is one of the allowed
394 existing values. */
395 if (r != -1 && !((flags & ID_R_EQ_R) && r == kauth_cred_getgid(cred))
396 && !((flags & ID_R_EQ_E) && r == kauth_cred_getegid(cred))
397 && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvgid(cred)))
398 return error;
399 if (e != -1 && !((flags & ID_E_EQ_R) && e == kauth_cred_getgid(cred))
400 && !((flags & ID_E_EQ_E) && e == kauth_cred_getegid(cred))
401 && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvgid(cred)))
402 return error;
403 if (sv != -1 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getgid(cred))
404 && !((flags & ID_S_EQ_E) && sv == kauth_cred_getegid(cred))
405 && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvgid(cred)))
406 return error;
407 }
408
409 /* If nothing has changed, short circuit the request */
410 if ((r == -1 || r == kauth_cred_getgid(cred))
411 && (e == -1 || e == kauth_cred_getegid(cred))
412 && (sv == -1 || sv == kauth_cred_getsvgid(cred)))
413 /* nothing to do */
414 return 0;
415
416 /* The pcred structure is not actually shared... */
417 if (r != -1)
418 kauth_cred_setgid(cred, r);
419 if (sv != -1)
420 kauth_cred_setsvgid(cred, sv);
421 if (e != -1 && e != kauth_cred_getegid(cred)) {
422 /* Update a clone of the current credentials */
423 cred = kauth_cred_copy(cred);
424 kauth_cred_setegid(cred, e);
425 p->p_cred = cred;
426 }
427
428 /* Mark process as having changed credentials, stops tracing etc */
429 p_sugid(p);
430 return 0;
431 }
432
433 /* ARGSUSED */
434 int
435 sys_setuid(struct lwp *l, void *v, register_t *retval)
436 {
437 struct sys_setuid_args /* {
438 syscallarg(uid_t) uid;
439 } */ *uap = v;
440 uid_t uid = SCARG(uap, uid);
441
442 return do_setresuid(l, uid, uid, uid,
443 ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
444 }
445
446 /* ARGSUSED */
447 int
448 sys_seteuid(struct lwp *l, void *v, register_t *retval)
449 {
450 struct sys_seteuid_args /* {
451 syscallarg(uid_t) euid;
452 } */ *uap = v;
453
454 return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
455 }
456
457 int
458 sys_setreuid(struct lwp *l, void *v, register_t *retval)
459 {
460 struct sys_setreuid_args /* {
461 syscallarg(uid_t) ruid;
462 syscallarg(uid_t) euid;
463 } */ *uap = v;
464 struct proc *p = l->l_proc;
465 uid_t ruid, euid, svuid;
466
467 ruid = SCARG(uap, ruid);
468 euid = SCARG(uap, euid);
469 if (ruid == -1)
470 ruid = kauth_cred_getuid(p->p_cred);
471 if (euid == -1)
472 euid = kauth_cred_geteuid(p->p_cred);
473 /* Saved uid is set to the new euid if the ruid changed */
474 svuid = (ruid == kauth_cred_getuid(p->p_cred)) ? -1 : euid;
475
476 return do_setresuid(l, ruid, euid, svuid,
477 ID_R_EQ_R | ID_R_EQ_E |
478 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
479 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
480 }
481
482 /* ARGSUSED */
483 int
484 sys_setgid(struct lwp *l, void *v, register_t *retval)
485 {
486 struct sys_setgid_args /* {
487 syscallarg(gid_t) gid;
488 } */ *uap = v;
489 gid_t gid = SCARG(uap, gid);
490
491 return do_setresgid(l, gid, gid, gid,
492 ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
493 }
494
495 /* ARGSUSED */
496 int
497 sys_setegid(struct lwp *l, void *v, register_t *retval)
498 {
499 struct sys_setegid_args /* {
500 syscallarg(gid_t) egid;
501 } */ *uap = v;
502
503 return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
504 }
505
506 int
507 sys_setregid(struct lwp *l, void *v, register_t *retval)
508 {
509 struct sys_setregid_args /* {
510 syscallarg(gid_t) rgid;
511 syscallarg(gid_t) egid;
512 } */ *uap = v;
513 struct proc *p = l->l_proc;
514 gid_t rgid, egid, svgid;
515
516 rgid = SCARG(uap, rgid);
517 egid = SCARG(uap, egid);
518 if (rgid == -1)
519 rgid = kauth_cred_getgid(p->p_cred);
520 if (egid == -1)
521 egid = kauth_cred_getegid(p->p_cred);
522 /* Saved gid is set to the new egid if the rgid changed */
523 svgid = rgid == kauth_cred_getgid(p->p_cred) ? -1 : egid;
524
525 return do_setresgid(l, rgid, egid, svgid,
526 ID_R_EQ_R | ID_R_EQ_E |
527 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
528 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
529 }
530
531 int
532 sys_issetugid(struct lwp *l, void *v, register_t *retval)
533 {
534 struct proc *p = l->l_proc;
535
536 /*
537 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
538 * we use P_SUGID because we consider changing the owners as
539 * "tainting" as well.
540 * This is significant for procs that start as root and "become"
541 * a user without an exec - programs cannot know *everything*
542 * that libc *might* have put in their data segment.
543 */
544 *retval = (p->p_flag & P_SUGID) != 0;
545 return (0);
546 }
547
548 /*
549 * sort -u for groups.
550 */
551 static int
552 grsortu(gid_t *grp, int ngrp)
553 {
554 const gid_t *src, *end;
555 gid_t *dst;
556 gid_t group;
557 int i, j;
558
559 /* bubble sort */
560 for (i = 0; i < ngrp; i++)
561 for (j = i + 1; j < ngrp; j++)
562 if (grp[i] > grp[j]) {
563 gid_t tmp = grp[i];
564 grp[i] = grp[j];
565 grp[j] = tmp;
566 }
567
568 /* uniq */
569 end = grp + ngrp;
570 src = grp;
571 dst = grp;
572 while (src < end) {
573 group = *src++;
574 while (src < end && *src == group)
575 src++;
576 *dst++ = group;
577 }
578
579 #ifdef DIAGNOSTIC
580 /* zero out the rest of the array */
581 (void)memset(dst, 0, sizeof(*grp) * (end - dst));
582 #endif
583
584 return dst - grp;
585 }
586
587 /* ARGSUSED */
588 int
589 sys_setgroups(struct lwp *l, void *v, register_t *retval)
590 {
591 struct sys_setgroups_args /* {
592 syscallarg(int) gidsetsize;
593 syscallarg(const gid_t *) gidset;
594 } */ *uap = v;
595 struct proc *p = l->l_proc;
596 kauth_cred_t pc = p->p_cred;
597 int ngrp;
598 int error;
599 gid_t grp[NGROUPS];
600 size_t grsize;
601
602 if ((error = kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER,
603 &p->p_acflag)) != 0)
604 return (error);
605
606 ngrp = SCARG(uap, gidsetsize);
607 if ((u_int)ngrp > NGROUPS)
608 return (EINVAL);
609
610 grsize = ngrp * sizeof(gid_t);
611 error = copyin(SCARG(uap, gidset), grp, grsize);
612 if (error)
613 return (error);
614
615 ngrp = grsortu(grp, ngrp);
616
617 pc = kauth_cred_copy(pc);
618 p->p_cred = pc;
619
620 kauth_cred_setgroups(p->p_cred, grp, ngrp, -1);
621
622 p_sugid(p);
623 return (0);
624 }
625
626 /*
627 * Check if gid is a member of the group set.
628 */
629 int
630 groupmember(gid_t gid, const struct ucred *cred)
631 {
632 const gid_t *gp;
633 const gid_t *egp;
634
635 egp = &(cred->cr_groups[cred->cr_ngroups]);
636 for (gp = cred->cr_groups; gp < egp; gp++)
637 if (*gp == gid)
638 return (1);
639 return (0);
640 }
641
642 /*
643 * Test whether the specified credentials imply "super-user"
644 * privilege; if so, and we have accounting info, set the flag
645 * indicating use of super-powers.
646 * Returns 0 or error.
647 */
648 int
649 suser(const struct ucred *cred, u_short *acflag)
650 {
651
652 if (cred->cr_uid == 0) {
653 if (acflag)
654 *acflag |= ASU;
655 return (0);
656 }
657 return (EPERM);
658 }
659
660 /*
661 * Allocate a zeroed cred structure.
662 */
663 struct ucred *
664 crget(void)
665 {
666 struct ucred *cr;
667
668 cr = pool_get(&cred_pool, PR_WAITOK);
669 memset(cr, 0, sizeof(*cr));
670 simple_lock_init(&cr->cr_lock);
671 cr->cr_ref = 1;
672 return (cr);
673 }
674
675 /*
676 * Free a cred structure.
677 * Throws away space when ref count gets to 0.
678 */
679 void
680 crfree(struct ucred *cr)
681 {
682 int n;
683
684 simple_lock(&cr->cr_lock);
685 n = --cr->cr_ref;
686 simple_unlock(&cr->cr_lock);
687 if (n == 0)
688 pool_put(&cred_pool, cr);
689 }
690
691 /*
692 * Compare cred structures and return 0 if they match
693 */
694 int
695 crcmp(const struct ucred *cr1, const struct uucred *cr2)
696 {
697 /* FIXME: The group lists should be compared element by element,
698 * as the order of groups may be different in the two lists.
699 * Currently this function can return a non-zero value for
700 * equivalent group lists. */
701 return cr1->cr_uid != cr2->cr_uid ||
702 cr1->cr_gid != cr2->cr_gid ||
703 cr1->cr_ngroups != (uint32_t)cr2->cr_ngroups ||
704 memcmp(cr1->cr_groups, cr2->cr_groups,
705 sizeof(cr1->cr_groups[0]) * cr1->cr_ngroups);
706 }
707
708 /*
709 * Copy cred structure to a new one and free the old one.
710 */
711 struct ucred *
712 crcopy(struct ucred *cr)
713 {
714 struct ucred *newcr;
715
716 if (cr->cr_ref == 1)
717 return (cr);
718
719 newcr = crget();
720 memcpy(&newcr->cr_startcopy, &cr->cr_startcopy,
721 sizeof(struct ucred) - offsetof(struct ucred, cr_startcopy));
722 crfree(cr);
723 return (newcr);
724 }
725
726 /*
727 * Dup cred struct to a new held one.
728 */
729 struct ucred *
730 crdup(const struct ucred *cr)
731 {
732 struct ucred *newcr;
733
734 newcr = crget();
735 memcpy(&newcr->cr_startcopy, &cr->cr_startcopy,
736 sizeof(struct ucred) - offsetof(struct ucred, cr_startcopy));
737 return (newcr);
738 }
739
740 /*
741 * convert from userland credentials to kernel one
742 */
743 void
744 crcvt(struct ucred *uc, const struct uucred *uuc)
745 {
746
747 uc->cr_ref = 0;
748 uc->cr_uid = uuc->cr_uid;
749 uc->cr_gid = uuc->cr_gid;
750 uc->cr_ngroups = uuc->cr_ngroups;
751 (void)memcpy(uc->cr_groups, uuc->cr_groups, sizeof(uuc->cr_groups));
752 }
753
754 /*
755 * Get login name, if available.
756 */
757 /* ARGSUSED */
758 int
759 sys___getlogin(struct lwp *l, void *v, register_t *retval)
760 {
761 struct sys___getlogin_args /* {
762 syscallarg(char *) namebuf;
763 syscallarg(size_t) namelen;
764 } */ *uap = v;
765 struct proc *p = l->l_proc;
766
767 if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
768 SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
769 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
770 (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
771 }
772
773 /*
774 * Set login name.
775 */
776 /* ARGSUSED */
777 int
778 sys___setlogin(struct lwp *l, void *v, register_t *retval)
779 {
780 struct sys___setlogin_args /* {
781 syscallarg(const char *) namebuf;
782 } */ *uap = v;
783 struct proc *p = l->l_proc;
784 struct session *s = p->p_pgrp->pg_session;
785 char newname[sizeof s->s_login + 1];
786 int error;
787
788 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
789 &p->p_acflag)) != 0)
790 return (error);
791 error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
792 if (error != 0)
793 return (error == ENAMETOOLONG ? EINVAL : error);
794
795 if (s->s_flags & S_LOGIN_SET && p->p_pid != s->s_sid &&
796 strncmp(newname, s->s_login, sizeof s->s_login) != 0)
797 log(LOG_WARNING, "%s (pid %d) changing logname from "
798 "%.*s to %s\n", p->p_comm, p->p_pid,
799 (int)sizeof s->s_login, s->s_login, newname);
800 s->s_flags |= S_LOGIN_SET;
801 strncpy(s->s_login, newname, sizeof s->s_login);
802 return (0);
803 }
804