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