kern_prot.c revision 1.93.4.2 1 /* $NetBSD: kern_prot.c,v 1.93.4.2 2006/11/17 16:34:36 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.93.4.2 2006/11/17 16:34:36 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 rw_enter(&proclist_lock, RW_READER);
93 retval[1] = p->p_pptr->p_pid;
94 rw_exit(&proclist_lock);
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 rw_enter(&proclist_lock, RW_READER);
105 *retval = p->p_pptr->p_pid;
106 rw_exit(&proclist_lock);
107 return (0);
108 }
109
110 /* Get process group ID; note that POSIX getpgrp takes no parameter */
111 int
112 sys_getpgrp(struct lwp *l, void *v, register_t *retval)
113 {
114 struct proc *p = l->l_proc;
115
116 rw_enter(&proclist_lock, RW_READER);
117 *retval = p->p_pgrp->pg_id;
118 rw_exit(&proclist_lock);
119 return (0);
120 }
121
122 /*
123 * Return the process group ID of the session leader (session ID)
124 * for the specified process.
125 */
126 int
127 sys_getsid(struct lwp *l, void *v, register_t *retval)
128 {
129 struct sys_getsid_args /* {
130 syscalldarg(pid_t) pid;
131 } */ *uap = v;
132 pid_t pid = SCARG(uap, pid);
133 struct proc *p;
134
135 rw_enter(&proclist_lock, RW_READER);
136 if (pid != 0)
137 p = l->l_proc;
138 else if ((p = p_find(pid, PFIND_LOCKED | PFIND_UNLOCK_FAIL)) == NULL)
139 return (ESRCH);
140 *retval = p->p_session->s_sid;
141 rw_exit(&proclist_lock);
142 return (0);
143 }
144
145 int
146 sys_getpgid(struct lwp *l, void *v, register_t *retval)
147 {
148 struct sys_getpgid_args /* {
149 syscallarg(pid_t) pid;
150 } */ *uap = v;
151 pid_t pid = SCARG(uap, pid);
152 struct proc *p;
153
154 rw_enter(&proclist_lock, RW_READER);
155 if (pid != 0)
156 p = l->l_proc;
157 else if ((p = p_find(pid, PFIND_LOCKED | PFIND_UNLOCK_FAIL)) == NULL)
158 return (ESRCH);
159 *retval = p->p_pgid;
160 rw_exit(&proclist_lock);
161 return (0);
162 }
163
164 /* ARGSUSED */
165 int
166 sys_getuid(struct lwp *l, void *v, register_t *retval)
167 {
168
169 *retval = kauth_cred_getuid(l->l_cred);
170 return (0);
171 }
172
173 /* ARGSUSED */
174 int
175 sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
176 {
177
178 retval[0] = kauth_cred_getuid(l->l_cred);
179 retval[1] = kauth_cred_geteuid(l->l_cred);
180 return (0);
181 }
182
183 /* ARGSUSED */
184 int
185 sys_geteuid(struct lwp *l, void *v, register_t *retval)
186 {
187
188 *retval = kauth_cred_geteuid(l->l_cred);
189 return (0);
190 }
191
192 /* ARGSUSED */
193 int
194 sys_getgid(struct lwp *l, void *v, register_t *retval)
195 {
196
197 *retval = kauth_cred_getgid(l->l_cred);
198 return (0);
199 }
200
201 /* ARGSUSED */
202 int
203 sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
204 {
205
206 retval[0] = kauth_cred_getgid(l->l_cred);
207 retval[1] = kauth_cred_getegid(l->l_cred);
208 return (0);
209 }
210
211 /*
212 * Get effective group ID. The "egid" is groups[0], and could be obtained
213 * via getgroups. This syscall exists because it is somewhat painful to do
214 * correctly in a library function.
215 */
216 /* ARGSUSED */
217 int
218 sys_getegid(struct lwp *l, void *v, register_t *retval)
219 {
220
221 *retval = kauth_cred_getegid(l->l_cred);
222 return (0);
223 }
224
225 int
226 sys_getgroups(struct lwp *l, void *v, register_t *retval)
227 {
228 struct sys_getgroups_args /* {
229 syscallarg(int) gidsetsize;
230 syscallarg(gid_t *) gidset;
231 } */ *uap = v;
232 kauth_cred_t cred = l->l_cred;
233 u_int ngrp;
234 int error;
235 gid_t *grbuf;
236
237 if (SCARG(uap, gidsetsize) == 0) {
238 *retval = kauth_cred_ngroups(cred);
239 return (0);
240 } else if (SCARG(uap, gidsetsize) < 0)
241 return (EINVAL);
242 ngrp = SCARG(uap, gidsetsize);
243 if (ngrp < kauth_cred_ngroups(cred))
244 return (EINVAL);
245 ngrp = kauth_cred_ngroups(cred);
246
247 grbuf = malloc(ngrp * sizeof(*grbuf), M_TEMP, M_WAITOK);
248 kauth_cred_getgroups(cred, grbuf, ngrp);
249 error = copyout(grbuf, (caddr_t)SCARG(uap, gidset),
250 ngrp * sizeof(gid_t));
251 free(grbuf, M_TEMP);
252 if (error)
253 return (error);
254 *retval = ngrp;
255 return (0);
256 }
257
258 /* ARGSUSED */
259 int
260 sys_setsid(struct lwp *l, void *v, register_t *retval)
261 {
262 struct proc *p = l->l_proc;
263 int error;
264
265 error = enterpgrp(p, p->p_pid, p->p_pid, 1);
266 *retval = p->p_pid;
267 return (error);
268 }
269
270
271 /*
272 * set process group (setpgid/old setpgrp)
273 *
274 * caller does setpgid(targpid, targpgid)
275 *
276 * pgid must be in valid range (EINVAL)
277 * pid must be caller or child of caller (ESRCH)
278 * if a child
279 * pid must be in same session (EPERM)
280 * pid can't have done an exec (EACCES)
281 * if pgid != pid
282 * there must exist some pid in same session having pgid (EPERM)
283 * pid must not be session leader (EPERM)
284 *
285 * Permission checks now in enterpgrp()
286 */
287 /* ARGSUSED */
288 int
289 sys_setpgid(struct lwp *l, void *v, register_t *retval)
290 {
291 struct sys_setpgid_args /* {
292 syscallarg(int) pid;
293 syscallarg(int) pgid;
294 } */ *uap = v;
295 struct proc *p = l->l_proc;
296 pid_t targp, pgid;
297
298 if (SCARG(uap, pgid) < 0)
299 return EINVAL;
300 if ((targp = SCARG(uap, pid)) == 0)
301 targp = p->p_pid;
302 if ((pgid = SCARG(uap, pgid)) == 0)
303 pgid = targp;
304
305 return enterpgrp(p, targp, pgid, 0);
306 }
307
308 /*
309 * Set real, effective and saved uids to the requested values.
310 * non-root callers can only ever change uids to values that match
311 * one of the processes current uid values.
312 * This is further restricted by the flags argument.
313 */
314
315 int
316 do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
317 {
318 struct proc *p = l->l_proc;
319 kauth_cred_t cred, ncred;
320
321 ncred = kauth_cred_alloc();
322
323 /* Get a write lock on the process credential. */
324 proc_crmod_enter();
325 cred = p->p_cred;
326
327 /*
328 * Check that the new value is one of the allowed existing values,
329 * or that we have root privilege.
330 */
331 if ((r != -1
332 && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
333 && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
334 && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred))) ||
335 (e != -1
336 && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
337 && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
338 && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred))) ||
339 (sv != -1
340 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
341 && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
342 && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))) {
343 int error;
344
345 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
346 &l->l_acflag);
347 if (error != 0) {
348 proc_crmod_leave(cred, ncred);
349 return error;
350 }
351 }
352
353 /* If nothing has changed, short circuit the request */
354 if ((r == -1 || r == kauth_cred_getuid(cred))
355 && (e == -1 || e == kauth_cred_geteuid(cred))
356 && (sv == -1 || sv == kauth_cred_getsvuid(cred))) {
357 proc_crmod_leave(cred, ncred);
358 return 0;
359 }
360
361 kauth_cred_clone(cred, ncred);
362
363 if (r != -1 && r != kauth_cred_getuid(ncred)) {
364 /* Update count of processes for this user */
365 (void)chgproccnt(kauth_cred_getuid(ncred), -1);
366 (void)chgproccnt(r, 1);
367 kauth_cred_setuid(ncred, r);
368 }
369 if (sv != -1)
370 kauth_cred_setsvuid(ncred, sv);
371 if (e != -1)
372 kauth_cred_seteuid(ncred, e);
373
374 /* Mark process as having changed credentials, stops tracing etc. */
375 p_sugid(p);
376
377 /* Broadcast our credentials to the process and other LWPs. */
378 proc_crmod_leave(ncred, cred);
379
380 return 0;
381 }
382
383 /*
384 * Set real, effective and saved gids to the requested values.
385 * non-root callers can only ever change gids to values that match
386 * one of the processes current gid values.
387 * This is further restricted by the flags argument.
388 */
389
390 int
391 do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
392 {
393 struct proc *p = l->l_proc;
394 kauth_cred_t cred, ncred;
395
396 ncred = kauth_cred_alloc();
397
398 /* Get a write lock on the process credential. */
399 proc_crmod_enter();
400 cred = p->p_cred;
401
402 /*
403 * check new value is one of the allowed existing values.
404 * otherwise, check if we have root privilege.
405 */
406 if ((r != -1
407 && !((flags & ID_R_EQ_R) && r == kauth_cred_getgid(cred))
408 && !((flags & ID_R_EQ_E) && r == kauth_cred_getegid(cred))
409 && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvgid(cred))) ||
410 (e != -1
411 && !((flags & ID_E_EQ_R) && e == kauth_cred_getgid(cred))
412 && !((flags & ID_E_EQ_E) && e == kauth_cred_getegid(cred))
413 && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvgid(cred))) ||
414 (sv != -1
415 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getgid(cred))
416 && !((flags & ID_S_EQ_E) && sv == kauth_cred_getegid(cred))
417 && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvgid(cred)))) {
418 int error;
419
420 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
421 &l->l_acflag);
422 if (error != 0) {
423 proc_crmod_leave(cred, ncred);
424 return error;
425 }
426 }
427
428 /* If nothing has changed, short circuit the request */
429 if ((r == -1 || r == kauth_cred_getgid(cred))
430 && (e == -1 || e == kauth_cred_getegid(cred))
431 && (sv == -1 || sv == kauth_cred_getsvgid(cred))) {
432 proc_crmod_leave(cred, ncred);
433 return 0;
434 }
435
436 kauth_cred_clone(cred, ncred);
437
438 if (r != -1)
439 kauth_cred_setgid(ncred, r);
440 if (sv != -1)
441 kauth_cred_setsvgid(ncred, sv);
442 if (e != -1)
443 kauth_cred_setegid(ncred, e);
444
445 /* Mark process as having changed credentials, stops tracing etc. */
446 p_sugid(p);
447
448 /* Broadcast our credentials to the process and other LWPs. */
449 proc_crmod_leave(ncred, cred);
450
451 return 0;
452 }
453
454 /* ARGSUSED */
455 int
456 sys_setuid(struct lwp *l, void *v, register_t *retval)
457 {
458 struct sys_setuid_args /* {
459 syscallarg(uid_t) uid;
460 } */ *uap = v;
461 uid_t uid = SCARG(uap, uid);
462
463 return do_setresuid(l, uid, uid, uid,
464 ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
465 }
466
467 /* ARGSUSED */
468 int
469 sys_seteuid(struct lwp *l, void *v, register_t *retval)
470 {
471 struct sys_seteuid_args /* {
472 syscallarg(uid_t) euid;
473 } */ *uap = v;
474
475 return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
476 }
477
478 int
479 sys_setreuid(struct lwp *l, void *v, register_t *retval)
480 {
481 struct sys_setreuid_args /* {
482 syscallarg(uid_t) ruid;
483 syscallarg(uid_t) euid;
484 } */ *uap = v;
485 kauth_cred_t cred = l->l_cred;
486 uid_t ruid, euid, svuid;
487
488 ruid = SCARG(uap, ruid);
489 euid = SCARG(uap, euid);
490
491 if (ruid == -1)
492 ruid = kauth_cred_getuid(cred);
493 if (euid == -1)
494 euid = kauth_cred_geteuid(cred);
495
496 /* Saved uid is set to the new euid if the ruid changed */
497 svuid = (ruid == kauth_cred_getuid(cred)) ? -1 : euid;
498
499 return do_setresuid(l, ruid, euid, svuid,
500 ID_R_EQ_R | ID_R_EQ_E |
501 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
502 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
503 }
504
505 /* ARGSUSED */
506 int
507 sys_setgid(struct lwp *l, void *v, register_t *retval)
508 {
509 struct sys_setgid_args /* {
510 syscallarg(gid_t) gid;
511 } */ *uap = v;
512 gid_t gid = SCARG(uap, gid);
513
514 return do_setresgid(l, gid, gid, gid,
515 ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
516 }
517
518 /* ARGSUSED */
519 int
520 sys_setegid(struct lwp *l, void *v, register_t *retval)
521 {
522 struct sys_setegid_args /* {
523 syscallarg(gid_t) egid;
524 } */ *uap = v;
525
526 return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
527 }
528
529 int
530 sys_setregid(struct lwp *l, void *v, register_t *retval)
531 {
532 struct sys_setregid_args /* {
533 syscallarg(gid_t) rgid;
534 syscallarg(gid_t) egid;
535 } */ *uap = v;
536 kauth_cred_t cred = l->l_cred;
537 gid_t rgid, egid, svgid;
538
539 rgid = SCARG(uap, rgid);
540 egid = SCARG(uap, egid);
541
542 if (rgid == -1)
543 rgid = kauth_cred_getgid(cred);
544 if (egid == -1)
545 egid = kauth_cred_getegid(cred);
546
547 /* Saved gid is set to the new egid if the rgid changed */
548 svgid = rgid == kauth_cred_getgid(cred) ? -1 : egid;
549
550 return do_setresgid(l, rgid, egid, svgid,
551 ID_R_EQ_R | ID_R_EQ_E |
552 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
553 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
554 }
555
556 int
557 sys_issetugid(struct lwp *l, void *v, register_t *retval)
558 {
559 struct proc *p = l->l_proc;
560
561 /*
562 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
563 * we use P_SUGID because we consider changing the owners as
564 * "tainting" as well.
565 * This is significant for procs that start as root and "become"
566 * a user without an exec - programs cannot know *everything*
567 * that libc *might* have put in their data segment.
568 */
569 *retval = (p->p_flag & P_SUGID) != 0;
570 return (0);
571 }
572
573 /*
574 * sort -u for groups.
575 */
576 static int
577 grsortu(gid_t *grp, int ngrp)
578 {
579 const gid_t *src, *end;
580 gid_t *dst;
581 gid_t group;
582 int i, j;
583
584 /* bubble sort */
585 for (i = 0; i < ngrp; i++)
586 for (j = i + 1; j < ngrp; j++)
587 if (grp[i] > grp[j]) {
588 gid_t tmp = grp[i];
589 grp[i] = grp[j];
590 grp[j] = tmp;
591 }
592
593 /* uniq */
594 end = grp + ngrp;
595 src = grp;
596 dst = grp;
597 while (src < end) {
598 group = *src++;
599 while (src < end && *src == group)
600 src++;
601 *dst++ = group;
602 }
603
604 #ifdef DIAGNOSTIC
605 /* zero out the rest of the array */
606 (void)memset(dst, 0, sizeof(*grp) * (end - dst));
607 #endif
608
609 return dst - grp;
610 }
611
612 /* ARGSUSED */
613 int
614 sys_setgroups(struct lwp *l, void *v, register_t *retval)
615 {
616 struct sys_setgroups_args /* {
617 syscallarg(int) gidsetsize;
618 syscallarg(const gid_t *) gidset;
619 } */ *uap = v;
620 kauth_cred_t cred, ncred;
621 struct proc *p = l->l_proc;
622 int ngrp;
623 int error;
624 gid_t grp[NGROUPS];
625 size_t grsize;
626
627 ncred = kauth_cred_alloc();
628
629 proc_crmod_enter();
630 cred = p->p_cred;
631
632 if ((error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
633 &l->l_acflag)) != 0)
634 goto bad;
635
636 ngrp = SCARG(uap, gidsetsize);
637 if ((u_int)ngrp > NGROUPS) {
638 error = EINVAL;
639 goto bad;
640 }
641
642 grsize = ngrp * sizeof(gid_t);
643 error = copyin(SCARG(uap, gidset), grp, grsize);
644 if (error)
645 goto bad;
646
647 ngrp = grsortu(grp, ngrp);
648 kauth_cred_clone(cred, ncred);
649 kauth_cred_setgroups(ncred, grp, ngrp, -1);
650
651 /* Mark process as having changed credentials, stops tracing etc. */
652 p_sugid(p);
653
654 /* Broadcast our credentials to the process and other LWPs. */
655 proc_crmod_leave(ncred, cred);
656
657 return (0);
658 bad:
659 proc_crmod_leave(cred, ncred);
660 return (error);
661 }
662
663 /*
664 * Get login name, if available.
665 */
666 /* ARGSUSED */
667 int
668 sys___getlogin(struct lwp *l, void *v, register_t *retval)
669 {
670 struct sys___getlogin_args /* {
671 syscallarg(char *) namebuf;
672 syscallarg(size_t) namelen;
673 } */ *uap = v;
674 struct proc *p = l->l_proc;
675 char login[sizeof(p->p_session->s_login)];
676 int namelen = SCARG(uap, namelen);
677
678 if (namelen > sizeof(login))
679 namelen = sizeof(login);
680 rw_enter(&proclist_lock, RW_WRITER);
681 memcpy(login, p->p_session->s_login, namelen);
682 rw_exit(&proclist_lock);
683 return (copyout(login, (void *)SCARG(uap, namebuf), namelen));
684 }
685
686 /*
687 * Set login name.
688 */
689 /* ARGSUSED */
690 int
691 sys___setlogin(struct lwp *l, void *v, register_t *retval)
692 {
693 struct sys___setlogin_args /* {
694 syscallarg(const char *) namebuf;
695 } */ *uap = v;
696 struct proc *p = l->l_proc;
697 struct session *sp;
698 char newname[sizeof sp->s_login + 1];
699 int error;
700
701 if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
702 &l->l_acflag)) != 0)
703 return (error);
704 error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
705 if (error != 0)
706 return (error == ENAMETOOLONG ? EINVAL : error);
707
708 rw_enter(&proclist_lock, RW_WRITER);
709 sp = p->p_session;
710 if (sp->s_flags & S_LOGIN_SET && p->p_pid != sp->s_sid &&
711 strncmp(newname, sp->s_login, sizeof sp->s_login) != 0)
712 log(LOG_WARNING, "%s (pid %d) changing logname from "
713 "%.*s to %s\n", p->p_comm, p->p_pid,
714 (int)sizeof sp->s_login, sp->s_login, newname);
715 sp->s_flags |= S_LOGIN_SET;
716 strncpy(sp->s_login, newname, sizeof sp->s_login);
717 rw_exit(&proclist_lock);
718 return (0);
719 }
720
721