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