kern_prot.c revision 1.102 1 1.102 dsl /* $NetBSD: kern_prot.c,v 1.102 2007/06/23 09:08:37 dsl 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.102 dsl __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.102 2007/06/23 09:08:37 dsl Exp $");
45 1.45 thorpej
46 1.51 christos #include "opt_compat_43.h"
47 1.14 cgd
48 1.14 cgd #include <sys/param.h>
49 1.14 cgd #include <sys/acct.h>
50 1.14 cgd #include <sys/systm.h>
51 1.14 cgd #include <sys/ucred.h>
52 1.14 cgd #include <sys/proc.h>
53 1.14 cgd #include <sys/timeb.h>
54 1.14 cgd #include <sys/times.h>
55 1.82 simonb #include <sys/pool.h>
56 1.102 dsl #include <sys/prot.h>
57 1.72 dsl #include <sys/syslog.h>
58 1.81 christos #include <sys/resourcevar.h>
59 1.89 elad #include <sys/kauth.h>
60 1.14 cgd
61 1.19 cgd #include <sys/mount.h>
62 1.19 cgd #include <sys/syscallargs.h>
63 1.71 thorpej
64 1.89 elad #include <sys/malloc.h>
65 1.63 mycroft
66 1.70 thorpej int sys_getpid(struct lwp *, void *, register_t *);
67 1.70 thorpej int sys_getpid_with_ppid(struct lwp *, void *, register_t *);
68 1.70 thorpej int sys_getuid(struct lwp *, void *, register_t *);
69 1.70 thorpej int sys_getuid_with_euid(struct lwp *, void *, register_t *);
70 1.70 thorpej int sys_getgid(struct lwp *, void *, register_t *);
71 1.70 thorpej int sys_getgid_with_egid(struct lwp *, void *, register_t *);
72 1.32 christos
73 1.14 cgd /* ARGSUSED */
74 1.32 christos int
75 1.96 yamt sys_getpid(struct lwp *l, 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.62 mycroft /* ARGSUSED */
84 1.62 mycroft int
85 1.96 yamt sys_getpid_with_ppid(struct lwp *l, void *v, register_t *retval)
86 1.62 mycroft {
87 1.70 thorpej struct proc *p = l->l_proc;
88 1.62 mycroft
89 1.62 mycroft retval[0] = p->p_pid;
90 1.101 ad mutex_enter(&proclist_lock);
91 1.62 mycroft retval[1] = p->p_pptr->p_pid;
92 1.101 ad mutex_exit(&proclist_lock);
93 1.14 cgd return (0);
94 1.14 cgd }
95 1.14 cgd
96 1.14 cgd /* ARGSUSED */
97 1.32 christos int
98 1.96 yamt sys_getppid(struct lwp *l, void *v, register_t *retval)
99 1.14 cgd {
100 1.70 thorpej struct proc *p = l->l_proc;
101 1.14 cgd
102 1.101 ad mutex_enter(&proclist_lock);
103 1.14 cgd *retval = p->p_pptr->p_pid;
104 1.101 ad mutex_exit(&proclist_lock);
105 1.14 cgd return (0);
106 1.14 cgd }
107 1.14 cgd
108 1.14 cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
109 1.32 christos int
110 1.96 yamt sys_getpgrp(struct lwp *l, void *v, register_t *retval)
111 1.14 cgd {
112 1.70 thorpej struct proc *p = l->l_proc;
113 1.14 cgd
114 1.101 ad mutex_enter(&proclist_lock);
115 1.14 cgd *retval = p->p_pgrp->pg_id;
116 1.101 ad mutex_exit(&proclist_lock);
117 1.14 cgd return (0);
118 1.43 thorpej }
119 1.43 thorpej
120 1.43 thorpej /*
121 1.43 thorpej * Return the process group ID of the session leader (session ID)
122 1.43 thorpej * for the specified process.
123 1.43 thorpej */
124 1.43 thorpej int
125 1.73 dsl sys_getsid(struct lwp *l, void *v, register_t *retval)
126 1.43 thorpej {
127 1.43 thorpej struct sys_getsid_args /* {
128 1.43 thorpej syscalldarg(pid_t) pid;
129 1.43 thorpej } */ *uap = v;
130 1.97 ad pid_t pid = SCARG(uap, pid);
131 1.97 ad struct proc *p;
132 1.97 ad int error = 0;
133 1.97 ad
134 1.101 ad mutex_enter(&proclist_lock);
135 1.97 ad if (pid == 0)
136 1.97 ad *retval = l->l_proc->p_session->s_sid;
137 1.97 ad else if ((p = p_find(pid, PFIND_LOCKED)) != NULL)
138 1.97 ad *retval = p->p_session->s_sid;
139 1.97 ad else
140 1.97 ad error = ESRCH;
141 1.101 ad mutex_exit(&proclist_lock);
142 1.43 thorpej
143 1.97 ad return error;
144 1.36 mrg }
145 1.36 mrg
146 1.36 mrg int
147 1.73 dsl sys_getpgid(struct lwp *l, void *v, register_t *retval)
148 1.36 mrg {
149 1.56 augustss struct sys_getpgid_args /* {
150 1.36 mrg syscallarg(pid_t) pid;
151 1.36 mrg } */ *uap = v;
152 1.97 ad pid_t pid = SCARG(uap, pid);
153 1.97 ad struct proc *p;
154 1.97 ad int error = 0;
155 1.97 ad
156 1.101 ad mutex_enter(&proclist_lock);
157 1.97 ad if (pid == 0)
158 1.97 ad *retval = l->l_proc->p_pgid;
159 1.97 ad else if ((p = p_find(pid, PFIND_LOCKED)) != NULL)
160 1.97 ad *retval = p->p_pgid;
161 1.97 ad else
162 1.97 ad error = ESRCH;
163 1.101 ad mutex_exit(&proclist_lock);
164 1.36 mrg
165 1.97 ad return error;
166 1.14 cgd }
167 1.14 cgd
168 1.14 cgd /* ARGSUSED */
169 1.32 christos int
170 1.96 yamt sys_getuid(struct lwp *l, void *v, register_t *retval)
171 1.14 cgd {
172 1.14 cgd
173 1.92 ad *retval = kauth_cred_getuid(l->l_cred);
174 1.62 mycroft return (0);
175 1.62 mycroft }
176 1.62 mycroft
177 1.62 mycroft /* ARGSUSED */
178 1.62 mycroft int
179 1.96 yamt sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
180 1.62 mycroft {
181 1.62 mycroft
182 1.92 ad retval[0] = kauth_cred_getuid(l->l_cred);
183 1.92 ad retval[1] = kauth_cred_geteuid(l->l_cred);
184 1.14 cgd return (0);
185 1.14 cgd }
186 1.14 cgd
187 1.14 cgd /* ARGSUSED */
188 1.32 christos int
189 1.96 yamt sys_geteuid(struct lwp *l, void *v, register_t *retval)
190 1.14 cgd {
191 1.14 cgd
192 1.92 ad *retval = 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.96 yamt sys_getgid(struct lwp *l, void *v, register_t *retval)
199 1.14 cgd {
200 1.14 cgd
201 1.92 ad *retval = kauth_cred_getgid(l->l_cred);
202 1.62 mycroft return (0);
203 1.62 mycroft }
204 1.62 mycroft
205 1.62 mycroft /* ARGSUSED */
206 1.62 mycroft int
207 1.96 yamt sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
208 1.62 mycroft {
209 1.62 mycroft
210 1.92 ad retval[0] = kauth_cred_getgid(l->l_cred);
211 1.92 ad retval[1] = kauth_cred_getegid(l->l_cred);
212 1.14 cgd return (0);
213 1.14 cgd }
214 1.14 cgd
215 1.14 cgd /*
216 1.14 cgd * Get effective group ID. The "egid" is groups[0], and could be obtained
217 1.14 cgd * via getgroups. This syscall exists because it is somewhat painful to do
218 1.14 cgd * correctly in a library function.
219 1.14 cgd */
220 1.14 cgd /* ARGSUSED */
221 1.32 christos int
222 1.96 yamt sys_getegid(struct lwp *l, void *v, register_t *retval)
223 1.14 cgd {
224 1.75 enami
225 1.92 ad *retval = kauth_cred_getegid(l->l_cred);
226 1.14 cgd return (0);
227 1.14 cgd }
228 1.14 cgd
229 1.32 christos int
230 1.73 dsl sys_getgroups(struct lwp *l, void *v, register_t *retval)
231 1.29 thorpej {
232 1.56 augustss struct sys_getgroups_args /* {
233 1.41 thorpej syscallarg(int) gidsetsize;
234 1.19 cgd syscallarg(gid_t *) gidset;
235 1.29 thorpej } */ *uap = v;
236 1.102 dsl const gid_t *grbuf;
237 1.102 dsl
238 1.102 dsl *retval = kauth_cred_ngroups(l->l_cred);
239 1.102 dsl if (SCARG(uap, gidsetsize) == 0)
240 1.102 dsl return 0;
241 1.102 dsl
242 1.102 dsl grbuf = kauth_cred_getgrlist(l->l_cred, SCARG(uap, gidsetsize));
243 1.102 dsl if (grbuf == NULL)
244 1.102 dsl return EINVAL;
245 1.14 cgd
246 1.102 dsl return copyout(grbuf, SCARG(uap, gidset), *retval * sizeof(gid_t));
247 1.14 cgd }
248 1.14 cgd
249 1.14 cgd /* ARGSUSED */
250 1.32 christos int
251 1.96 yamt sys_setsid(struct lwp *l, void *v, register_t *retval)
252 1.14 cgd {
253 1.70 thorpej struct proc *p = l->l_proc;
254 1.97 ad int error;
255 1.14 cgd
256 1.97 ad error = enterpgrp(p, p->p_pid, p->p_pid, 1);
257 1.97 ad *retval = p->p_pid;
258 1.97 ad return (error);
259 1.14 cgd }
260 1.14 cgd
261 1.97 ad
262 1.14 cgd /*
263 1.14 cgd * set process group (setpgid/old setpgrp)
264 1.14 cgd *
265 1.14 cgd * caller does setpgid(targpid, targpgid)
266 1.14 cgd *
267 1.39 mikel * pgid must be in valid range (EINVAL)
268 1.14 cgd * pid must be caller or child of caller (ESRCH)
269 1.14 cgd * if a child
270 1.14 cgd * pid must be in same session (EPERM)
271 1.14 cgd * pid can't have done an exec (EACCES)
272 1.14 cgd * if pgid != pid
273 1.14 cgd * there must exist some pid in same session having pgid (EPERM)
274 1.14 cgd * pid must not be session leader (EPERM)
275 1.77 dsl *
276 1.77 dsl * Permission checks now in enterpgrp()
277 1.14 cgd */
278 1.14 cgd /* ARGSUSED */
279 1.32 christos int
280 1.96 yamt sys_setpgid(struct lwp *l, void *v, register_t *retval)
281 1.29 thorpej {
282 1.56 augustss struct sys_setpgid_args /* {
283 1.19 cgd syscallarg(int) pid;
284 1.19 cgd syscallarg(int) pgid;
285 1.29 thorpej } */ *uap = v;
286 1.97 ad struct proc *p = l->l_proc;
287 1.97 ad pid_t targp, pgid;
288 1.14 cgd
289 1.39 mikel if (SCARG(uap, pgid) < 0)
290 1.77 dsl return EINVAL;
291 1.97 ad if ((targp = SCARG(uap, pid)) == 0)
292 1.97 ad targp = p->p_pid;
293 1.97 ad if ((pgid = SCARG(uap, pgid)) == 0)
294 1.97 ad pgid = targp;
295 1.14 cgd
296 1.97 ad return enterpgrp(p, targp, pgid, 0);
297 1.14 cgd }
298 1.14 cgd
299 1.76 dsl /*
300 1.76 dsl * Set real, effective and saved uids to the requested values.
301 1.76 dsl * non-root callers can only ever change uids to values that match
302 1.76 dsl * one of the processes current uid values.
303 1.76 dsl * This is further restricted by the flags argument.
304 1.76 dsl */
305 1.76 dsl
306 1.76 dsl int
307 1.76 dsl do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
308 1.76 dsl {
309 1.93 ad struct proc *p = l->l_proc;
310 1.97 ad kauth_cred_t cred, ncred;
311 1.97 ad
312 1.97 ad ncred = kauth_cred_alloc();
313 1.93 ad
314 1.93 ad /* Get a write lock on the process credential. */
315 1.97 ad proc_crmod_enter();
316 1.93 ad cred = p->p_cred;
317 1.76 dsl
318 1.90 yamt /*
319 1.93 ad * Check that the new value is one of the allowed existing values,
320 1.93 ad * or that we have root privilege.
321 1.90 yamt */
322 1.90 yamt if ((r != -1
323 1.90 yamt && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
324 1.90 yamt && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
325 1.90 yamt && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred))) ||
326 1.90 yamt (e != -1
327 1.90 yamt && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
328 1.90 yamt && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
329 1.90 yamt && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred))) ||
330 1.90 yamt (sv != -1
331 1.90 yamt && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
332 1.90 yamt && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
333 1.90 yamt && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))) {
334 1.90 yamt int error;
335 1.90 yamt
336 1.94 elad error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
337 1.94 elad p, NULL, NULL, NULL);
338 1.90 yamt if (error != 0) {
339 1.99 thorpej proc_crmod_leave(cred, ncred, false);
340 1.76 dsl return error;
341 1.90 yamt }
342 1.76 dsl }
343 1.76 dsl
344 1.76 dsl /* If nothing has changed, short circuit the request */
345 1.89 elad if ((r == -1 || r == kauth_cred_getuid(cred))
346 1.89 elad && (e == -1 || e == kauth_cred_geteuid(cred))
347 1.90 yamt && (sv == -1 || sv == kauth_cred_getsvuid(cred))) {
348 1.99 thorpej proc_crmod_leave(cred, ncred, false);
349 1.76 dsl return 0;
350 1.90 yamt }
351 1.76 dsl
352 1.97 ad kauth_cred_clone(cred, ncred);
353 1.91 ad
354 1.97 ad if (r != -1 && r != kauth_cred_getuid(ncred)) {
355 1.76 dsl /* Update count of processes for this user */
356 1.97 ad (void)chgproccnt(kauth_cred_getuid(ncred), -1);
357 1.76 dsl (void)chgproccnt(r, 1);
358 1.97 ad kauth_cred_setuid(ncred, r);
359 1.76 dsl }
360 1.76 dsl if (sv != -1)
361 1.97 ad kauth_cred_setsvuid(ncred, sv);
362 1.91 ad if (e != -1)
363 1.97 ad kauth_cred_seteuid(ncred, e);
364 1.93 ad
365 1.92 ad /* Broadcast our credentials to the process and other LWPs. */
366 1.99 thorpej proc_crmod_leave(ncred, cred, true);
367 1.92 ad
368 1.76 dsl return 0;
369 1.76 dsl }
370 1.76 dsl
371 1.76 dsl /*
372 1.76 dsl * Set real, effective and saved gids to the requested values.
373 1.76 dsl * non-root callers can only ever change gids to values that match
374 1.76 dsl * one of the processes current gid values.
375 1.76 dsl * This is further restricted by the flags argument.
376 1.76 dsl */
377 1.76 dsl
378 1.76 dsl int
379 1.76 dsl do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
380 1.76 dsl {
381 1.93 ad struct proc *p = l->l_proc;
382 1.97 ad kauth_cred_t cred, ncred;
383 1.97 ad
384 1.97 ad ncred = kauth_cred_alloc();
385 1.93 ad
386 1.93 ad /* Get a write lock on the process credential. */
387 1.97 ad proc_crmod_enter();
388 1.93 ad cred = p->p_cred;
389 1.76 dsl
390 1.90 yamt /*
391 1.90 yamt * check new value is one of the allowed existing values.
392 1.90 yamt * otherwise, check if we have root privilege.
393 1.90 yamt */
394 1.90 yamt if ((r != -1
395 1.90 yamt && !((flags & ID_R_EQ_R) && r == kauth_cred_getgid(cred))
396 1.90 yamt && !((flags & ID_R_EQ_E) && r == kauth_cred_getegid(cred))
397 1.90 yamt && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvgid(cred))) ||
398 1.90 yamt (e != -1
399 1.90 yamt && !((flags & ID_E_EQ_R) && e == kauth_cred_getgid(cred))
400 1.90 yamt && !((flags & ID_E_EQ_E) && e == kauth_cred_getegid(cred))
401 1.90 yamt && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvgid(cred))) ||
402 1.90 yamt (sv != -1
403 1.90 yamt && !((flags & ID_S_EQ_R) && sv == kauth_cred_getgid(cred))
404 1.90 yamt && !((flags & ID_S_EQ_E) && sv == kauth_cred_getegid(cred))
405 1.90 yamt && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvgid(cred)))) {
406 1.90 yamt int error;
407 1.90 yamt
408 1.94 elad error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
409 1.94 elad p, NULL, NULL, NULL);
410 1.90 yamt if (error != 0) {
411 1.99 thorpej proc_crmod_leave(cred, ncred, false);
412 1.76 dsl return error;
413 1.90 yamt }
414 1.76 dsl }
415 1.76 dsl
416 1.76 dsl /* If nothing has changed, short circuit the request */
417 1.89 elad if ((r == -1 || r == kauth_cred_getgid(cred))
418 1.89 elad && (e == -1 || e == kauth_cred_getegid(cred))
419 1.90 yamt && (sv == -1 || sv == kauth_cred_getsvgid(cred))) {
420 1.99 thorpej proc_crmod_leave(cred, ncred, false);
421 1.76 dsl return 0;
422 1.90 yamt }
423 1.76 dsl
424 1.97 ad kauth_cred_clone(cred, ncred);
425 1.91 ad
426 1.76 dsl if (r != -1)
427 1.97 ad kauth_cred_setgid(ncred, r);
428 1.76 dsl if (sv != -1)
429 1.97 ad kauth_cred_setsvgid(ncred, sv);
430 1.91 ad if (e != -1)
431 1.97 ad kauth_cred_setegid(ncred, e);
432 1.93 ad
433 1.92 ad /* Broadcast our credentials to the process and other LWPs. */
434 1.99 thorpej proc_crmod_leave(ncred, cred, true);
435 1.92 ad
436 1.76 dsl return 0;
437 1.76 dsl }
438 1.76 dsl
439 1.14 cgd /* ARGSUSED */
440 1.32 christos int
441 1.96 yamt sys_setuid(struct lwp *l, void *v, register_t *retval)
442 1.29 thorpej {
443 1.30 mycroft struct sys_setuid_args /* {
444 1.19 cgd syscallarg(uid_t) uid;
445 1.29 thorpej } */ *uap = v;
446 1.76 dsl uid_t uid = SCARG(uap, uid);
447 1.14 cgd
448 1.76 dsl return do_setresuid(l, uid, uid, uid,
449 1.76 dsl ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
450 1.14 cgd }
451 1.14 cgd
452 1.14 cgd /* ARGSUSED */
453 1.32 christos int
454 1.96 yamt sys_seteuid(struct lwp *l, void *v, register_t *retval)
455 1.29 thorpej {
456 1.30 mycroft struct sys_seteuid_args /* {
457 1.19 cgd syscallarg(uid_t) euid;
458 1.29 thorpej } */ *uap = v;
459 1.59 christos
460 1.76 dsl return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
461 1.14 cgd }
462 1.14 cgd
463 1.35 mycroft int
464 1.96 yamt sys_setreuid(struct lwp *l, void *v, register_t *retval)
465 1.35 mycroft {
466 1.35 mycroft struct sys_setreuid_args /* {
467 1.35 mycroft syscallarg(uid_t) ruid;
468 1.35 mycroft syscallarg(uid_t) euid;
469 1.35 mycroft } */ *uap = v;
470 1.92 ad kauth_cred_t cred = l->l_cred;
471 1.76 dsl uid_t ruid, euid, svuid;
472 1.35 mycroft
473 1.35 mycroft ruid = SCARG(uap, ruid);
474 1.35 mycroft euid = SCARG(uap, euid);
475 1.92 ad
476 1.76 dsl if (ruid == -1)
477 1.92 ad ruid = kauth_cred_getuid(cred);
478 1.76 dsl if (euid == -1)
479 1.92 ad euid = kauth_cred_geteuid(cred);
480 1.92 ad
481 1.76 dsl /* Saved uid is set to the new euid if the ruid changed */
482 1.92 ad svuid = (ruid == kauth_cred_getuid(cred)) ? -1 : euid;
483 1.76 dsl
484 1.76 dsl return do_setresuid(l, ruid, euid, svuid,
485 1.76 dsl ID_R_EQ_R | ID_R_EQ_E |
486 1.76 dsl ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
487 1.76 dsl ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
488 1.35 mycroft }
489 1.35 mycroft
490 1.14 cgd /* ARGSUSED */
491 1.32 christos int
492 1.96 yamt sys_setgid(struct lwp *l, void *v, register_t *retval)
493 1.29 thorpej {
494 1.30 mycroft struct sys_setgid_args /* {
495 1.19 cgd syscallarg(gid_t) gid;
496 1.29 thorpej } */ *uap = v;
497 1.76 dsl gid_t gid = SCARG(uap, gid);
498 1.14 cgd
499 1.76 dsl return do_setresgid(l, gid, gid, gid,
500 1.76 dsl ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
501 1.14 cgd }
502 1.14 cgd
503 1.14 cgd /* ARGSUSED */
504 1.32 christos int
505 1.96 yamt sys_setegid(struct lwp *l, void *v, register_t *retval)
506 1.29 thorpej {
507 1.30 mycroft struct sys_setegid_args /* {
508 1.19 cgd syscallarg(gid_t) egid;
509 1.29 thorpej } */ *uap = v;
510 1.14 cgd
511 1.76 dsl return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
512 1.35 mycroft }
513 1.35 mycroft
514 1.35 mycroft int
515 1.96 yamt sys_setregid(struct lwp *l, void *v, register_t *retval)
516 1.35 mycroft {
517 1.35 mycroft struct sys_setregid_args /* {
518 1.35 mycroft syscallarg(gid_t) rgid;
519 1.35 mycroft syscallarg(gid_t) egid;
520 1.35 mycroft } */ *uap = v;
521 1.92 ad kauth_cred_t cred = l->l_cred;
522 1.76 dsl gid_t rgid, egid, svgid;
523 1.35 mycroft
524 1.35 mycroft rgid = SCARG(uap, rgid);
525 1.35 mycroft egid = SCARG(uap, egid);
526 1.92 ad
527 1.76 dsl if (rgid == -1)
528 1.92 ad rgid = kauth_cred_getgid(cred);
529 1.76 dsl if (egid == -1)
530 1.92 ad egid = kauth_cred_getegid(cred);
531 1.92 ad
532 1.76 dsl /* Saved gid is set to the new egid if the rgid changed */
533 1.92 ad svgid = rgid == kauth_cred_getgid(cred) ? -1 : egid;
534 1.76 dsl
535 1.76 dsl return do_setresgid(l, rgid, egid, svgid,
536 1.76 dsl ID_R_EQ_R | ID_R_EQ_E |
537 1.76 dsl ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
538 1.76 dsl ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
539 1.57 minoura }
540 1.57 minoura
541 1.57 minoura int
542 1.96 yamt sys_issetugid(struct lwp *l, void *v, register_t *retval)
543 1.57 minoura {
544 1.70 thorpej struct proc *p = l->l_proc;
545 1.75 enami
546 1.57 minoura /*
547 1.57 minoura * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
548 1.98 pavel * we use PK_SUGID because we consider changing the owners as
549 1.57 minoura * "tainting" as well.
550 1.57 minoura * This is significant for procs that start as root and "become"
551 1.57 minoura * a user without an exec - programs cannot know *everything*
552 1.57 minoura * that libc *might* have put in their data segment.
553 1.57 minoura */
554 1.98 pavel *retval = (p->p_flag & PK_SUGID) != 0;
555 1.60 christos return (0);
556 1.14 cgd }
557 1.14 cgd
558 1.15 cgd /* ARGSUSED */
559 1.32 christos int
560 1.96 yamt sys_setgroups(struct lwp *l, void *v, register_t *retval)
561 1.29 thorpej {
562 1.30 mycroft struct sys_setgroups_args /* {
563 1.41 thorpej syscallarg(int) gidsetsize;
564 1.38 cgd syscallarg(const gid_t *) gidset;
565 1.29 thorpej } */ *uap = v;
566 1.102 dsl kauth_cred_t ncred;
567 1.15 cgd int error;
568 1.102 dsl gid_t *grbuf;
569 1.15 cgd
570 1.102 dsl ncred = kauth_cred_alloc();
571 1.59 christos
572 1.102 dsl grbuf = kauth_cred_setngroups(ncred, SCARG(uap, gidsetsize));
573 1.102 dsl if (grbuf == NULL)
574 1.102 dsl error = EINVAL;
575 1.102 dsl else {
576 1.102 dsl error = copyin(SCARG(uap, gidset), grbuf,
577 1.102 dsl SCARG(uap, gidsetsize) * sizeof(gid_t));
578 1.102 dsl }
579 1.102 dsl if (error != 0) {
580 1.102 dsl kauth_cred_free(ncred);
581 1.97 ad return error;
582 1.102 dsl }
583 1.97 ad
584 1.102 dsl return kauth_proc_setgroups(l, ncred);
585 1.14 cgd }
586 1.14 cgd
587 1.14 cgd /*
588 1.14 cgd * Get login name, if available.
589 1.14 cgd */
590 1.14 cgd /* ARGSUSED */
591 1.32 christos int
592 1.96 yamt sys___getlogin(struct lwp *l, void *v, register_t *retval)
593 1.29 thorpej {
594 1.37 jtc struct sys___getlogin_args /* {
595 1.19 cgd syscallarg(char *) namebuf;
596 1.53 kleink syscallarg(size_t) namelen;
597 1.29 thorpej } */ *uap = v;
598 1.70 thorpej struct proc *p = l->l_proc;
599 1.97 ad char login[sizeof(p->p_session->s_login)];
600 1.97 ad int namelen = SCARG(uap, namelen);
601 1.14 cgd
602 1.97 ad if (namelen > sizeof(login))
603 1.97 ad namelen = sizeof(login);
604 1.101 ad mutex_enter(&proclist_lock);
605 1.97 ad memcpy(login, p->p_session->s_login, namelen);
606 1.101 ad mutex_exit(&proclist_lock);
607 1.97 ad return (copyout(login, (void *)SCARG(uap, namebuf), namelen));
608 1.14 cgd }
609 1.14 cgd
610 1.14 cgd /*
611 1.14 cgd * Set login name.
612 1.14 cgd */
613 1.14 cgd /* ARGSUSED */
614 1.32 christos int
615 1.96 yamt sys___setlogin(struct lwp *l, void *v, register_t *retval)
616 1.29 thorpej {
617 1.70 thorpej struct sys___setlogin_args /* {
618 1.38 cgd syscallarg(const char *) namebuf;
619 1.29 thorpej } */ *uap = v;
620 1.70 thorpej struct proc *p = l->l_proc;
621 1.97 ad struct session *sp;
622 1.97 ad char newname[sizeof sp->s_login + 1];
623 1.14 cgd int error;
624 1.14 cgd
625 1.94 elad if ((error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_SETID,
626 1.94 elad p, NULL, NULL, NULL)) != 0)
627 1.14 cgd return (error);
628 1.72 dsl error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
629 1.72 dsl if (error != 0)
630 1.75 enami return (error == ENAMETOOLONG ? EINVAL : error);
631 1.72 dsl
632 1.101 ad mutex_enter(&proclist_lock);
633 1.97 ad sp = p->p_session;
634 1.97 ad if (sp->s_flags & S_LOGIN_SET && p->p_pid != sp->s_sid &&
635 1.97 ad strncmp(newname, sp->s_login, sizeof sp->s_login) != 0)
636 1.74 wiz log(LOG_WARNING, "%s (pid %d) changing logname from "
637 1.74 wiz "%.*s to %s\n", p->p_comm, p->p_pid,
638 1.97 ad (int)sizeof sp->s_login, sp->s_login, newname);
639 1.97 ad sp->s_flags |= S_LOGIN_SET;
640 1.97 ad strncpy(sp->s_login, newname, sizeof sp->s_login);
641 1.101 ad mutex_exit(&proclist_lock);
642 1.75 enami return (0);
643 1.14 cgd }
644 1.97 ad
645