kern_prot.c revision 1.120 1 1.120 christos /* $NetBSD: kern_prot.c,v 1.120 2016/11/12 19:42:47 christos Exp $ */
2 1.16 cgd
3 1.14 cgd /*
4 1.15 cgd * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
5 1.15 cgd * The Regents of the University of California. All rights reserved.
6 1.14 cgd * (c) UNIX System Laboratories, Inc.
7 1.14 cgd * All or some portions of this file are derived from material licensed
8 1.14 cgd * to the University of California by American Telephone and Telegraph
9 1.14 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.14 cgd * the permission of UNIX System Laboratories, Inc.
11 1.14 cgd *
12 1.14 cgd * Redistribution and use in source and binary forms, with or without
13 1.14 cgd * modification, are permitted provided that the following conditions
14 1.14 cgd * are met:
15 1.14 cgd * 1. Redistributions of source code must retain the above copyright
16 1.14 cgd * notice, this list of conditions and the following disclaimer.
17 1.14 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.14 cgd * notice, this list of conditions and the following disclaimer in the
19 1.14 cgd * documentation and/or other materials provided with the distribution.
20 1.80 agc * 3. Neither the name of the University nor the names of its contributors
21 1.14 cgd * may be used to endorse or promote products derived from this software
22 1.14 cgd * without specific prior written permission.
23 1.14 cgd *
24 1.14 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.14 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.14 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.14 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.14 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.14 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.14 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.14 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.14 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.14 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.14 cgd * SUCH DAMAGE.
35 1.14 cgd *
36 1.44 fvdl * @(#)kern_prot.c 8.9 (Berkeley) 2/14/95
37 1.14 cgd */
38 1.14 cgd
39 1.14 cgd /*
40 1.14 cgd * System calls related to processes and protection
41 1.14 cgd */
42 1.65 lukem
43 1.65 lukem #include <sys/cdefs.h>
44 1.120 christos __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.120 2016/11/12 19:42:47 christos Exp $");
45 1.45 thorpej
46 1.119 pooka #ifdef _KERNEL_OPT
47 1.51 christos #include "opt_compat_43.h"
48 1.119 pooka #endif
49 1.14 cgd
50 1.14 cgd #include <sys/param.h>
51 1.14 cgd #include <sys/acct.h>
52 1.14 cgd #include <sys/systm.h>
53 1.14 cgd #include <sys/ucred.h>
54 1.14 cgd #include <sys/proc.h>
55 1.14 cgd #include <sys/timeb.h>
56 1.14 cgd #include <sys/times.h>
57 1.82 simonb #include <sys/pool.h>
58 1.102 dsl #include <sys/prot.h>
59 1.72 dsl #include <sys/syslog.h>
60 1.108 pooka #include <sys/uidinfo.h>
61 1.89 elad #include <sys/kauth.h>
62 1.14 cgd
63 1.19 cgd #include <sys/mount.h>
64 1.19 cgd #include <sys/syscallargs.h>
65 1.71 thorpej
66 1.105 dsl int sys_getpid(struct lwp *, const void *, register_t *);
67 1.105 dsl int sys_getpid_with_ppid(struct lwp *, const void *, register_t *);
68 1.105 dsl int sys_getuid(struct lwp *, const void *, register_t *);
69 1.105 dsl int sys_getuid_with_euid(struct lwp *, const void *, register_t *);
70 1.105 dsl int sys_getgid(struct lwp *, const void *, register_t *);
71 1.105 dsl int sys_getgid_with_egid(struct lwp *, const void *, register_t *);
72 1.32 christos
73 1.14 cgd /* ARGSUSED */
74 1.32 christos int
75 1.105 dsl sys_getpid(struct lwp *l, const void *v, register_t *retval)
76 1.14 cgd {
77 1.70 thorpej struct proc *p = l->l_proc;
78 1.14 cgd
79 1.14 cgd *retval = p->p_pid;
80 1.62 mycroft return (0);
81 1.62 mycroft }
82 1.62 mycroft
83 1.120 christos static pid_t
84 1.120 christos kern_getppid(struct lwp *l)
85 1.120 christos {
86 1.120 christos struct proc *p = l->l_proc;
87 1.120 christos pid_t ppid;
88 1.120 christos
89 1.120 christos mutex_enter(proc_lock);
90 1.120 christos mutex_enter(p->p_lock);
91 1.120 christos ppid = (p->p_slflag & PSL_TRACED) && p->p_opptr ? p->p_opptr->p_pid :
92 1.120 christos p->p_pptr->p_pid;
93 1.120 christos mutex_exit(p->p_lock);
94 1.120 christos mutex_exit(proc_lock);
95 1.120 christos return ppid;
96 1.120 christos }
97 1.120 christos
98 1.62 mycroft /* ARGSUSED */
99 1.62 mycroft int
100 1.105 dsl sys_getpid_with_ppid(struct lwp *l, const void *v, register_t *retval)
101 1.62 mycroft {
102 1.70 thorpej struct proc *p = l->l_proc;
103 1.62 mycroft
104 1.62 mycroft retval[0] = p->p_pid;
105 1.120 christos retval[1] = kern_getppid(l);
106 1.14 cgd return (0);
107 1.14 cgd }
108 1.14 cgd
109 1.14 cgd /* ARGSUSED */
110 1.32 christos int
111 1.105 dsl sys_getppid(struct lwp *l, const void *v, register_t *retval)
112 1.14 cgd {
113 1.120 christos *retval = kern_getppid(l);
114 1.14 cgd return (0);
115 1.14 cgd }
116 1.14 cgd
117 1.14 cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
118 1.32 christos int
119 1.105 dsl sys_getpgrp(struct lwp *l, const void *v, register_t *retval)
120 1.14 cgd {
121 1.70 thorpej struct proc *p = l->l_proc;
122 1.14 cgd
123 1.106 ad mutex_enter(proc_lock);
124 1.14 cgd *retval = p->p_pgrp->pg_id;
125 1.106 ad mutex_exit(proc_lock);
126 1.14 cgd return (0);
127 1.43 thorpej }
128 1.43 thorpej
129 1.43 thorpej /*
130 1.43 thorpej * Return the process group ID of the session leader (session ID)
131 1.43 thorpej * for the specified process.
132 1.43 thorpej */
133 1.43 thorpej int
134 1.105 dsl sys_getsid(struct lwp *l, const struct sys_getsid_args *uap, register_t *retval)
135 1.43 thorpej {
136 1.105 dsl /* {
137 1.43 thorpej syscalldarg(pid_t) pid;
138 1.105 dsl } */
139 1.97 ad pid_t pid = SCARG(uap, pid);
140 1.97 ad struct proc *p;
141 1.97 ad int error = 0;
142 1.97 ad
143 1.106 ad mutex_enter(proc_lock);
144 1.97 ad if (pid == 0)
145 1.97 ad *retval = l->l_proc->p_session->s_sid;
146 1.110 rmind else if ((p = proc_find(pid)) != NULL)
147 1.97 ad *retval = p->p_session->s_sid;
148 1.97 ad else
149 1.97 ad error = ESRCH;
150 1.106 ad mutex_exit(proc_lock);
151 1.43 thorpej
152 1.97 ad return error;
153 1.36 mrg }
154 1.36 mrg
155 1.36 mrg int
156 1.105 dsl sys_getpgid(struct lwp *l, const struct sys_getpgid_args *uap, register_t *retval)
157 1.36 mrg {
158 1.105 dsl /* {
159 1.36 mrg syscallarg(pid_t) pid;
160 1.105 dsl } */
161 1.97 ad pid_t pid = SCARG(uap, pid);
162 1.97 ad struct proc *p;
163 1.97 ad int error = 0;
164 1.97 ad
165 1.106 ad mutex_enter(proc_lock);
166 1.97 ad if (pid == 0)
167 1.97 ad *retval = l->l_proc->p_pgid;
168 1.110 rmind else if ((p = proc_find(pid)) != NULL)
169 1.97 ad *retval = p->p_pgid;
170 1.97 ad else
171 1.97 ad error = ESRCH;
172 1.106 ad mutex_exit(proc_lock);
173 1.36 mrg
174 1.97 ad return error;
175 1.14 cgd }
176 1.14 cgd
177 1.14 cgd /* ARGSUSED */
178 1.32 christos int
179 1.105 dsl sys_getuid(struct lwp *l, const void *v, register_t *retval)
180 1.14 cgd {
181 1.14 cgd
182 1.92 ad *retval = kauth_cred_getuid(l->l_cred);
183 1.62 mycroft return (0);
184 1.62 mycroft }
185 1.62 mycroft
186 1.62 mycroft /* ARGSUSED */
187 1.62 mycroft int
188 1.105 dsl sys_getuid_with_euid(struct lwp *l, const void *v, register_t *retval)
189 1.62 mycroft {
190 1.62 mycroft
191 1.92 ad retval[0] = kauth_cred_getuid(l->l_cred);
192 1.92 ad retval[1] = kauth_cred_geteuid(l->l_cred);
193 1.14 cgd return (0);
194 1.14 cgd }
195 1.14 cgd
196 1.14 cgd /* ARGSUSED */
197 1.32 christos int
198 1.105 dsl sys_geteuid(struct lwp *l, const void *v, register_t *retval)
199 1.14 cgd {
200 1.14 cgd
201 1.92 ad *retval = kauth_cred_geteuid(l->l_cred);
202 1.14 cgd return (0);
203 1.14 cgd }
204 1.14 cgd
205 1.14 cgd /* ARGSUSED */
206 1.32 christos int
207 1.105 dsl sys_getgid(struct lwp *l, const void *v, register_t *retval)
208 1.14 cgd {
209 1.14 cgd
210 1.92 ad *retval = kauth_cred_getgid(l->l_cred);
211 1.62 mycroft return (0);
212 1.62 mycroft }
213 1.62 mycroft
214 1.62 mycroft /* ARGSUSED */
215 1.62 mycroft int
216 1.105 dsl sys_getgid_with_egid(struct lwp *l, const void *v, register_t *retval)
217 1.62 mycroft {
218 1.62 mycroft
219 1.92 ad retval[0] = kauth_cred_getgid(l->l_cred);
220 1.92 ad retval[1] = kauth_cred_getegid(l->l_cred);
221 1.14 cgd return (0);
222 1.14 cgd }
223 1.14 cgd
224 1.14 cgd /*
225 1.14 cgd * Get effective group ID. The "egid" is groups[0], and could be obtained
226 1.14 cgd * via getgroups. This syscall exists because it is somewhat painful to do
227 1.14 cgd * correctly in a library function.
228 1.14 cgd */
229 1.14 cgd /* ARGSUSED */
230 1.32 christos int
231 1.105 dsl sys_getegid(struct lwp *l, const void *v, register_t *retval)
232 1.14 cgd {
233 1.75 enami
234 1.92 ad *retval = kauth_cred_getegid(l->l_cred);
235 1.14 cgd return (0);
236 1.14 cgd }
237 1.14 cgd
238 1.32 christos int
239 1.105 dsl sys_getgroups(struct lwp *l, const struct sys_getgroups_args *uap, register_t *retval)
240 1.29 thorpej {
241 1.105 dsl /* {
242 1.41 thorpej syscallarg(int) gidsetsize;
243 1.19 cgd syscallarg(gid_t *) gidset;
244 1.105 dsl } */
245 1.102 dsl
246 1.102 dsl *retval = kauth_cred_ngroups(l->l_cred);
247 1.102 dsl if (SCARG(uap, gidsetsize) == 0)
248 1.102 dsl return 0;
249 1.113 martin if (SCARG(uap, gidsetsize) < (int)*retval)
250 1.102 dsl return EINVAL;
251 1.14 cgd
252 1.103 dsl return kauth_cred_getgroups(l->l_cred, SCARG(uap, gidset), *retval,
253 1.103 dsl UIO_USERSPACE);
254 1.14 cgd }
255 1.14 cgd
256 1.32 christos int
257 1.105 dsl sys_setsid(struct lwp *l, const void *v, register_t *retval)
258 1.14 cgd {
259 1.70 thorpej struct proc *p = l->l_proc;
260 1.97 ad int error;
261 1.14 cgd
262 1.109 rmind error = proc_enterpgrp(p, p->p_pid, p->p_pid, true);
263 1.97 ad *retval = p->p_pid;
264 1.97 ad return (error);
265 1.14 cgd }
266 1.14 cgd
267 1.97 ad
268 1.14 cgd /*
269 1.14 cgd * set process group (setpgid/old setpgrp)
270 1.14 cgd *
271 1.14 cgd * caller does setpgid(targpid, targpgid)
272 1.14 cgd *
273 1.39 mikel * pgid must be in valid range (EINVAL)
274 1.14 cgd * pid must be caller or child of caller (ESRCH)
275 1.14 cgd * if a child
276 1.14 cgd * pid must be in same session (EPERM)
277 1.14 cgd * pid can't have done an exec (EACCES)
278 1.14 cgd * if pgid != pid
279 1.14 cgd * there must exist some pid in same session having pgid (EPERM)
280 1.14 cgd * pid must not be session leader (EPERM)
281 1.77 dsl *
282 1.109 rmind * Permission checks now in proc_enterpgrp()
283 1.14 cgd */
284 1.32 christos int
285 1.109 rmind sys_setpgid(struct lwp *l, const struct sys_setpgid_args *uap,
286 1.109 rmind register_t *retval)
287 1.29 thorpej {
288 1.105 dsl /* {
289 1.19 cgd syscallarg(int) pid;
290 1.19 cgd syscallarg(int) pgid;
291 1.105 dsl } */
292 1.97 ad struct proc *p = l->l_proc;
293 1.97 ad pid_t targp, pgid;
294 1.14 cgd
295 1.39 mikel if (SCARG(uap, pgid) < 0)
296 1.77 dsl return EINVAL;
297 1.97 ad if ((targp = SCARG(uap, pid)) == 0)
298 1.97 ad targp = p->p_pid;
299 1.97 ad if ((pgid = SCARG(uap, pgid)) == 0)
300 1.97 ad pgid = targp;
301 1.14 cgd
302 1.109 rmind return proc_enterpgrp(p, targp, pgid, false);
303 1.14 cgd }
304 1.14 cgd
305 1.76 dsl /*
306 1.76 dsl * Set real, effective and saved uids to the requested values.
307 1.76 dsl * non-root callers can only ever change uids to values that match
308 1.76 dsl * one of the processes current uid values.
309 1.76 dsl * This is further restricted by the flags argument.
310 1.76 dsl */
311 1.76 dsl
312 1.76 dsl int
313 1.76 dsl do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
314 1.76 dsl {
315 1.93 ad struct proc *p = l->l_proc;
316 1.97 ad kauth_cred_t cred, ncred;
317 1.97 ad
318 1.97 ad ncred = kauth_cred_alloc();
319 1.93 ad
320 1.93 ad /* Get a write lock on the process credential. */
321 1.97 ad proc_crmod_enter();
322 1.93 ad cred = p->p_cred;
323 1.76 dsl
324 1.90 yamt /*
325 1.93 ad * Check that the new value is one of the allowed existing values,
326 1.93 ad * or that we have root privilege.
327 1.90 yamt */
328 1.90 yamt if ((r != -1
329 1.90 yamt && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
330 1.90 yamt && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
331 1.90 yamt && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred))) ||
332 1.90 yamt (e != -1
333 1.90 yamt && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
334 1.90 yamt && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
335 1.90 yamt && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred))) ||
336 1.90 yamt (sv != -1
337 1.90 yamt && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
338 1.90 yamt && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
339 1.90 yamt && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))) {
340 1.90 yamt int error;
341 1.90 yamt
342 1.94 elad error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
343 1.94 elad p, NULL, NULL, NULL);
344 1.90 yamt if (error != 0) {
345 1.99 thorpej proc_crmod_leave(cred, ncred, false);
346 1.76 dsl return error;
347 1.90 yamt }
348 1.76 dsl }
349 1.76 dsl
350 1.76 dsl /* If nothing has changed, short circuit the request */
351 1.89 elad if ((r == -1 || r == kauth_cred_getuid(cred))
352 1.89 elad && (e == -1 || e == kauth_cred_geteuid(cred))
353 1.90 yamt && (sv == -1 || sv == kauth_cred_getsvuid(cred))) {
354 1.99 thorpej proc_crmod_leave(cred, ncred, false);
355 1.76 dsl return 0;
356 1.90 yamt }
357 1.76 dsl
358 1.97 ad kauth_cred_clone(cred, ncred);
359 1.91 ad
360 1.97 ad if (r != -1 && r != kauth_cred_getuid(ncred)) {
361 1.117 rmind u_long nlwps;
362 1.117 rmind
363 1.117 rmind /* Update count of processes for this user. */
364 1.97 ad (void)chgproccnt(kauth_cred_getuid(ncred), -1);
365 1.76 dsl (void)chgproccnt(r, 1);
366 1.115 christos
367 1.117 rmind /* The first LWP of a process is excluded. */
368 1.117 rmind KASSERT(mutex_owned(p->p_lock));
369 1.117 rmind nlwps = p->p_nlwps - 1;
370 1.116 christos (void)chglwpcnt(kauth_cred_getuid(ncred), -nlwps);
371 1.115 christos (void)chglwpcnt(r, nlwps);
372 1.115 christos
373 1.97 ad kauth_cred_setuid(ncred, r);
374 1.76 dsl }
375 1.76 dsl if (sv != -1)
376 1.97 ad kauth_cred_setsvuid(ncred, sv);
377 1.91 ad if (e != -1)
378 1.97 ad kauth_cred_seteuid(ncred, e);
379 1.93 ad
380 1.92 ad /* Broadcast our credentials to the process and other LWPs. */
381 1.99 thorpej proc_crmod_leave(ncred, cred, true);
382 1.92 ad
383 1.76 dsl return 0;
384 1.76 dsl }
385 1.76 dsl
386 1.76 dsl /*
387 1.76 dsl * Set real, effective and saved gids to the requested values.
388 1.76 dsl * non-root callers can only ever change gids to values that match
389 1.76 dsl * one of the processes current gid values.
390 1.76 dsl * This is further restricted by the flags argument.
391 1.76 dsl */
392 1.76 dsl
393 1.76 dsl int
394 1.76 dsl do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
395 1.76 dsl {
396 1.93 ad struct proc *p = l->l_proc;
397 1.97 ad kauth_cred_t cred, ncred;
398 1.97 ad
399 1.97 ad ncred = kauth_cred_alloc();
400 1.93 ad
401 1.93 ad /* Get a write lock on the process credential. */
402 1.97 ad proc_crmod_enter();
403 1.93 ad cred = p->p_cred;
404 1.76 dsl
405 1.90 yamt /*
406 1.90 yamt * check new value is one of the allowed existing values.
407 1.90 yamt * otherwise, check if we have root privilege.
408 1.90 yamt */
409 1.90 yamt if ((r != -1
410 1.90 yamt && !((flags & ID_R_EQ_R) && r == kauth_cred_getgid(cred))
411 1.90 yamt && !((flags & ID_R_EQ_E) && r == kauth_cred_getegid(cred))
412 1.90 yamt && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvgid(cred))) ||
413 1.90 yamt (e != -1
414 1.90 yamt && !((flags & ID_E_EQ_R) && e == kauth_cred_getgid(cred))
415 1.90 yamt && !((flags & ID_E_EQ_E) && e == kauth_cred_getegid(cred))
416 1.90 yamt && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvgid(cred))) ||
417 1.90 yamt (sv != -1
418 1.90 yamt && !((flags & ID_S_EQ_R) && sv == kauth_cred_getgid(cred))
419 1.90 yamt && !((flags & ID_S_EQ_E) && sv == kauth_cred_getegid(cred))
420 1.90 yamt && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvgid(cred)))) {
421 1.90 yamt int error;
422 1.90 yamt
423 1.94 elad error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
424 1.94 elad p, NULL, NULL, NULL);
425 1.90 yamt if (error != 0) {
426 1.99 thorpej proc_crmod_leave(cred, ncred, false);
427 1.76 dsl return error;
428 1.90 yamt }
429 1.76 dsl }
430 1.76 dsl
431 1.76 dsl /* If nothing has changed, short circuit the request */
432 1.89 elad if ((r == -1 || r == kauth_cred_getgid(cred))
433 1.89 elad && (e == -1 || e == kauth_cred_getegid(cred))
434 1.90 yamt && (sv == -1 || sv == kauth_cred_getsvgid(cred))) {
435 1.99 thorpej proc_crmod_leave(cred, ncred, false);
436 1.76 dsl return 0;
437 1.90 yamt }
438 1.76 dsl
439 1.97 ad kauth_cred_clone(cred, ncred);
440 1.91 ad
441 1.76 dsl if (r != -1)
442 1.97 ad kauth_cred_setgid(ncred, r);
443 1.76 dsl if (sv != -1)
444 1.97 ad kauth_cred_setsvgid(ncred, sv);
445 1.91 ad if (e != -1)
446 1.97 ad kauth_cred_setegid(ncred, e);
447 1.93 ad
448 1.92 ad /* Broadcast our credentials to the process and other LWPs. */
449 1.99 thorpej proc_crmod_leave(ncred, cred, true);
450 1.92 ad
451 1.76 dsl return 0;
452 1.76 dsl }
453 1.76 dsl
454 1.14 cgd /* ARGSUSED */
455 1.32 christos int
456 1.105 dsl sys_setuid(struct lwp *l, const struct sys_setuid_args *uap, register_t *retval)
457 1.29 thorpej {
458 1.105 dsl /* {
459 1.19 cgd syscallarg(uid_t) uid;
460 1.105 dsl } */
461 1.76 dsl uid_t uid = SCARG(uap, uid);
462 1.14 cgd
463 1.76 dsl return do_setresuid(l, uid, uid, uid,
464 1.76 dsl ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
465 1.14 cgd }
466 1.14 cgd
467 1.14 cgd /* ARGSUSED */
468 1.32 christos int
469 1.105 dsl sys_seteuid(struct lwp *l, const struct sys_seteuid_args *uap, register_t *retval)
470 1.29 thorpej {
471 1.105 dsl /* {
472 1.19 cgd syscallarg(uid_t) euid;
473 1.105 dsl } */
474 1.59 christos
475 1.76 dsl return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
476 1.14 cgd }
477 1.14 cgd
478 1.35 mycroft int
479 1.105 dsl sys_setreuid(struct lwp *l, const struct sys_setreuid_args *uap, register_t *retval)
480 1.35 mycroft {
481 1.105 dsl /* {
482 1.35 mycroft syscallarg(uid_t) ruid;
483 1.35 mycroft syscallarg(uid_t) euid;
484 1.105 dsl } */
485 1.92 ad kauth_cred_t cred = l->l_cred;
486 1.76 dsl uid_t ruid, euid, svuid;
487 1.35 mycroft
488 1.35 mycroft ruid = SCARG(uap, ruid);
489 1.35 mycroft euid = SCARG(uap, euid);
490 1.92 ad
491 1.76 dsl if (ruid == -1)
492 1.92 ad ruid = kauth_cred_getuid(cred);
493 1.76 dsl if (euid == -1)
494 1.92 ad euid = kauth_cred_geteuid(cred);
495 1.92 ad
496 1.76 dsl /* Saved uid is set to the new euid if the ruid changed */
497 1.92 ad svuid = (ruid == kauth_cred_getuid(cred)) ? -1 : euid;
498 1.76 dsl
499 1.76 dsl return do_setresuid(l, ruid, euid, svuid,
500 1.76 dsl ID_R_EQ_R | ID_R_EQ_E |
501 1.76 dsl ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
502 1.76 dsl ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
503 1.35 mycroft }
504 1.35 mycroft
505 1.14 cgd /* ARGSUSED */
506 1.32 christos int
507 1.105 dsl sys_setgid(struct lwp *l, const struct sys_setgid_args *uap, register_t *retval)
508 1.29 thorpej {
509 1.105 dsl /* {
510 1.19 cgd syscallarg(gid_t) gid;
511 1.105 dsl } */
512 1.76 dsl gid_t gid = SCARG(uap, gid);
513 1.14 cgd
514 1.76 dsl return do_setresgid(l, gid, gid, gid,
515 1.76 dsl ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
516 1.14 cgd }
517 1.14 cgd
518 1.14 cgd /* ARGSUSED */
519 1.32 christos int
520 1.105 dsl sys_setegid(struct lwp *l, const struct sys_setegid_args *uap, register_t *retval)
521 1.29 thorpej {
522 1.105 dsl /* {
523 1.19 cgd syscallarg(gid_t) egid;
524 1.105 dsl } */
525 1.14 cgd
526 1.76 dsl return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
527 1.35 mycroft }
528 1.35 mycroft
529 1.35 mycroft int
530 1.105 dsl sys_setregid(struct lwp *l, const struct sys_setregid_args *uap, register_t *retval)
531 1.35 mycroft {
532 1.105 dsl /* {
533 1.35 mycroft syscallarg(gid_t) rgid;
534 1.35 mycroft syscallarg(gid_t) egid;
535 1.105 dsl } */
536 1.92 ad kauth_cred_t cred = l->l_cred;
537 1.76 dsl gid_t rgid, egid, svgid;
538 1.35 mycroft
539 1.35 mycroft rgid = SCARG(uap, rgid);
540 1.35 mycroft egid = SCARG(uap, egid);
541 1.92 ad
542 1.76 dsl if (rgid == -1)
543 1.92 ad rgid = kauth_cred_getgid(cred);
544 1.76 dsl if (egid == -1)
545 1.92 ad egid = kauth_cred_getegid(cred);
546 1.92 ad
547 1.76 dsl /* Saved gid is set to the new egid if the rgid changed */
548 1.92 ad svgid = rgid == kauth_cred_getgid(cred) ? -1 : egid;
549 1.76 dsl
550 1.76 dsl return do_setresgid(l, rgid, egid, svgid,
551 1.76 dsl ID_R_EQ_R | ID_R_EQ_E |
552 1.76 dsl ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
553 1.76 dsl ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
554 1.57 minoura }
555 1.57 minoura
556 1.57 minoura int
557 1.105 dsl sys_issetugid(struct lwp *l, const void *v, register_t *retval)
558 1.57 minoura {
559 1.70 thorpej struct proc *p = l->l_proc;
560 1.75 enami
561 1.57 minoura /*
562 1.57 minoura * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
563 1.98 pavel * we use PK_SUGID because we consider changing the owners as
564 1.57 minoura * "tainting" as well.
565 1.57 minoura * This is significant for procs that start as root and "become"
566 1.57 minoura * a user without an exec - programs cannot know *everything*
567 1.57 minoura * that libc *might* have put in their data segment.
568 1.57 minoura */
569 1.98 pavel *retval = (p->p_flag & PK_SUGID) != 0;
570 1.60 christos return (0);
571 1.14 cgd }
572 1.14 cgd
573 1.15 cgd /* ARGSUSED */
574 1.32 christos int
575 1.105 dsl sys_setgroups(struct lwp *l, const struct sys_setgroups_args *uap, register_t *retval)
576 1.29 thorpej {
577 1.105 dsl /* {
578 1.41 thorpej syscallarg(int) gidsetsize;
579 1.38 cgd syscallarg(const gid_t *) gidset;
580 1.105 dsl } */
581 1.102 dsl kauth_cred_t ncred;
582 1.15 cgd int error;
583 1.15 cgd
584 1.102 dsl ncred = kauth_cred_alloc();
585 1.103 dsl error = kauth_cred_setgroups(ncred, SCARG(uap, gidset),
586 1.103 dsl SCARG(uap, gidsetsize), -1, UIO_USERSPACE);
587 1.102 dsl if (error != 0) {
588 1.102 dsl kauth_cred_free(ncred);
589 1.97 ad return error;
590 1.102 dsl }
591 1.97 ad
592 1.102 dsl return kauth_proc_setgroups(l, ncred);
593 1.14 cgd }
594 1.14 cgd
595 1.14 cgd /*
596 1.14 cgd * Get login name, if available.
597 1.14 cgd */
598 1.14 cgd /* ARGSUSED */
599 1.32 christos int
600 1.105 dsl sys___getlogin(struct lwp *l, const struct sys___getlogin_args *uap, register_t *retval)
601 1.29 thorpej {
602 1.105 dsl /* {
603 1.19 cgd syscallarg(char *) namebuf;
604 1.53 kleink syscallarg(size_t) namelen;
605 1.105 dsl } */
606 1.70 thorpej struct proc *p = l->l_proc;
607 1.97 ad char login[sizeof(p->p_session->s_login)];
608 1.118 maxv size_t namelen = SCARG(uap, namelen);
609 1.14 cgd
610 1.97 ad if (namelen > sizeof(login))
611 1.97 ad namelen = sizeof(login);
612 1.106 ad mutex_enter(proc_lock);
613 1.97 ad memcpy(login, p->p_session->s_login, namelen);
614 1.106 ad mutex_exit(proc_lock);
615 1.97 ad return (copyout(login, (void *)SCARG(uap, namebuf), namelen));
616 1.14 cgd }
617 1.14 cgd
618 1.14 cgd /*
619 1.14 cgd * Set login name.
620 1.14 cgd */
621 1.14 cgd /* ARGSUSED */
622 1.32 christos int
623 1.105 dsl sys___setlogin(struct lwp *l, const struct sys___setlogin_args *uap, register_t *retval)
624 1.29 thorpej {
625 1.105 dsl /* {
626 1.38 cgd syscallarg(const char *) namebuf;
627 1.105 dsl } */
628 1.70 thorpej struct proc *p = l->l_proc;
629 1.97 ad struct session *sp;
630 1.97 ad char newname[sizeof sp->s_login + 1];
631 1.14 cgd int error;
632 1.14 cgd
633 1.94 elad if ((error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_SETID,
634 1.94 elad p, NULL, NULL, NULL)) != 0)
635 1.14 cgd return (error);
636 1.114 matt error = copyinstr(SCARG(uap, namebuf), newname, sizeof newname, NULL);
637 1.72 dsl if (error != 0)
638 1.75 enami return (error == ENAMETOOLONG ? EINVAL : error);
639 1.72 dsl
640 1.106 ad mutex_enter(proc_lock);
641 1.97 ad sp = p->p_session;
642 1.97 ad if (sp->s_flags & S_LOGIN_SET && p->p_pid != sp->s_sid &&
643 1.97 ad strncmp(newname, sp->s_login, sizeof sp->s_login) != 0)
644 1.74 wiz log(LOG_WARNING, "%s (pid %d) changing logname from "
645 1.74 wiz "%.*s to %s\n", p->p_comm, p->p_pid,
646 1.97 ad (int)sizeof sp->s_login, sp->s_login, newname);
647 1.97 ad sp->s_flags |= S_LOGIN_SET;
648 1.97 ad strncpy(sp->s_login, newname, sizeof sp->s_login);
649 1.106 ad mutex_exit(proc_lock);
650 1.75 enami return (0);
651 1.14 cgd }
652