kern_prot.c revision 1.86 1 1.86 christos /* $NetBSD: kern_prot.c,v 1.86 2005/08/23 07:58:58 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.86 christos __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.86 2005/08/23 07:58:58 christos 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.82 simonb POOL_INIT(cred_pool, sizeof(struct ucred), 0, 0, 0, "credpl",
64 1.82 simonb &pool_allocator_nointr);
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.86 christos static int grsortu(gid_t *, int);
74 1.86 christos
75 1.14 cgd /* ARGSUSED */
76 1.32 christos int
77 1.73 dsl sys_getpid(struct lwp *l, void *v, register_t *retval)
78 1.14 cgd {
79 1.70 thorpej struct proc *p = l->l_proc;
80 1.14 cgd
81 1.14 cgd *retval = p->p_pid;
82 1.62 mycroft return (0);
83 1.62 mycroft }
84 1.62 mycroft
85 1.62 mycroft /* ARGSUSED */
86 1.62 mycroft int
87 1.73 dsl sys_getpid_with_ppid(struct lwp *l, void *v, register_t *retval)
88 1.62 mycroft {
89 1.70 thorpej struct proc *p = l->l_proc;
90 1.62 mycroft
91 1.62 mycroft retval[0] = p->p_pid;
92 1.62 mycroft retval[1] = p->p_pptr->p_pid;
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.73 dsl 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.14 cgd *retval = p->p_pptr->p_pid;
103 1.14 cgd return (0);
104 1.14 cgd }
105 1.14 cgd
106 1.14 cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
107 1.32 christos int
108 1.73 dsl sys_getpgrp(struct lwp *l, void *v, register_t *retval)
109 1.14 cgd {
110 1.70 thorpej struct proc *p = l->l_proc;
111 1.14 cgd
112 1.14 cgd *retval = p->p_pgrp->pg_id;
113 1.14 cgd return (0);
114 1.43 thorpej }
115 1.43 thorpej
116 1.43 thorpej /*
117 1.43 thorpej * Return the process group ID of the session leader (session ID)
118 1.43 thorpej * for the specified process.
119 1.43 thorpej */
120 1.43 thorpej int
121 1.73 dsl sys_getsid(struct lwp *l, void *v, register_t *retval)
122 1.43 thorpej {
123 1.43 thorpej struct sys_getsid_args /* {
124 1.43 thorpej syscalldarg(pid_t) pid;
125 1.43 thorpej } */ *uap = v;
126 1.70 thorpej struct proc *p = l->l_proc;
127 1.43 thorpej
128 1.43 thorpej if (SCARG(uap, pid) == 0)
129 1.43 thorpej goto found;
130 1.43 thorpej if ((p = pfind(SCARG(uap, pid))) == 0)
131 1.43 thorpej return (ESRCH);
132 1.43 thorpej found:
133 1.43 thorpej *retval = p->p_session->s_sid;
134 1.60 christos return (0);
135 1.36 mrg }
136 1.36 mrg
137 1.36 mrg int
138 1.73 dsl sys_getpgid(struct lwp *l, void *v, register_t *retval)
139 1.36 mrg {
140 1.56 augustss struct sys_getpgid_args /* {
141 1.36 mrg syscallarg(pid_t) pid;
142 1.36 mrg } */ *uap = v;
143 1.70 thorpej struct proc *p = l->l_proc;
144 1.36 mrg
145 1.36 mrg if (SCARG(uap, pid) == 0)
146 1.36 mrg goto found;
147 1.36 mrg if ((p = pfind(SCARG(uap, pid))) == 0)
148 1.36 mrg return (ESRCH);
149 1.36 mrg found:
150 1.36 mrg *retval = p->p_pgid;
151 1.60 christos return (0);
152 1.14 cgd }
153 1.14 cgd
154 1.14 cgd /* ARGSUSED */
155 1.32 christos int
156 1.73 dsl sys_getuid(struct lwp *l, void *v, register_t *retval)
157 1.14 cgd {
158 1.70 thorpej struct proc *p = l->l_proc;
159 1.14 cgd
160 1.14 cgd *retval = p->p_cred->p_ruid;
161 1.62 mycroft return (0);
162 1.62 mycroft }
163 1.62 mycroft
164 1.62 mycroft /* ARGSUSED */
165 1.62 mycroft int
166 1.73 dsl sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
167 1.62 mycroft {
168 1.70 thorpej struct proc *p = l->l_proc;
169 1.62 mycroft
170 1.62 mycroft retval[0] = p->p_cred->p_ruid;
171 1.62 mycroft retval[1] = p->p_ucred->cr_uid;
172 1.14 cgd return (0);
173 1.14 cgd }
174 1.14 cgd
175 1.14 cgd /* ARGSUSED */
176 1.32 christos int
177 1.73 dsl sys_geteuid(struct lwp *l, void *v, register_t *retval)
178 1.14 cgd {
179 1.70 thorpej struct proc *p = l->l_proc;
180 1.14 cgd
181 1.14 cgd *retval = p->p_ucred->cr_uid;
182 1.14 cgd return (0);
183 1.14 cgd }
184 1.14 cgd
185 1.14 cgd /* ARGSUSED */
186 1.32 christos int
187 1.73 dsl sys_getgid(struct lwp *l, void *v, register_t *retval)
188 1.14 cgd {
189 1.70 thorpej struct proc *p = l->l_proc;
190 1.14 cgd
191 1.14 cgd *retval = p->p_cred->p_rgid;
192 1.62 mycroft return (0);
193 1.62 mycroft }
194 1.62 mycroft
195 1.62 mycroft /* ARGSUSED */
196 1.62 mycroft int
197 1.73 dsl sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
198 1.62 mycroft {
199 1.70 thorpej struct proc *p = l->l_proc;
200 1.62 mycroft
201 1.62 mycroft retval[0] = p->p_cred->p_rgid;
202 1.62 mycroft retval[1] = p->p_ucred->cr_gid;
203 1.14 cgd return (0);
204 1.14 cgd }
205 1.14 cgd
206 1.14 cgd /*
207 1.14 cgd * Get effective group ID. The "egid" is groups[0], and could be obtained
208 1.14 cgd * via getgroups. This syscall exists because it is somewhat painful to do
209 1.14 cgd * correctly in a library function.
210 1.14 cgd */
211 1.14 cgd /* ARGSUSED */
212 1.32 christos int
213 1.73 dsl sys_getegid(struct lwp *l, void *v, register_t *retval)
214 1.14 cgd {
215 1.70 thorpej struct proc *p = l->l_proc;
216 1.75 enami
217 1.27 jtc *retval = p->p_ucred->cr_gid;
218 1.14 cgd return (0);
219 1.14 cgd }
220 1.14 cgd
221 1.32 christos int
222 1.73 dsl sys_getgroups(struct lwp *l, void *v, register_t *retval)
223 1.29 thorpej {
224 1.56 augustss struct sys_getgroups_args /* {
225 1.41 thorpej syscallarg(int) gidsetsize;
226 1.19 cgd syscallarg(gid_t *) gidset;
227 1.29 thorpej } */ *uap = v;
228 1.70 thorpej struct proc *p = l->l_proc;
229 1.56 augustss struct pcred *pc = p->p_cred;
230 1.69 thorpej u_int ngrp;
231 1.14 cgd int error;
232 1.14 cgd
233 1.69 thorpej if (SCARG(uap, gidsetsize) == 0) {
234 1.14 cgd *retval = pc->pc_ucred->cr_ngroups;
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.14 cgd if (ngrp < pc->pc_ucred->cr_ngroups)
240 1.14 cgd return (EINVAL);
241 1.14 cgd ngrp = pc->pc_ucred->cr_ngroups;
242 1.32 christos error = copyout((caddr_t)pc->pc_ucred->cr_groups,
243 1.75 enami (caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
244 1.32 christos if (error)
245 1.14 cgd return (error);
246 1.14 cgd *retval = ngrp;
247 1.14 cgd return (0);
248 1.14 cgd }
249 1.14 cgd
250 1.14 cgd /* ARGSUSED */
251 1.32 christos int
252 1.73 dsl sys_setsid(struct lwp *l, void *v, register_t *retval)
253 1.14 cgd {
254 1.70 thorpej struct proc *p = l->l_proc;
255 1.14 cgd
256 1.14 cgd if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
257 1.14 cgd return (EPERM);
258 1.14 cgd } else {
259 1.15 cgd (void)enterpgrp(p, p->p_pid, 1);
260 1.14 cgd *retval = p->p_pid;
261 1.14 cgd return (0);
262 1.14 cgd }
263 1.14 cgd }
264 1.14 cgd
265 1.14 cgd /*
266 1.14 cgd * set process group (setpgid/old setpgrp)
267 1.14 cgd *
268 1.14 cgd * caller does setpgid(targpid, targpgid)
269 1.14 cgd *
270 1.39 mikel * pgid must be in valid range (EINVAL)
271 1.14 cgd * pid must be caller or child of caller (ESRCH)
272 1.14 cgd * if a child
273 1.14 cgd * pid must be in same session (EPERM)
274 1.14 cgd * pid can't have done an exec (EACCES)
275 1.14 cgd * if pgid != pid
276 1.14 cgd * there must exist some pid in same session having pgid (EPERM)
277 1.14 cgd * pid must not be session leader (EPERM)
278 1.77 dsl *
279 1.77 dsl * Permission checks now in enterpgrp()
280 1.14 cgd */
281 1.14 cgd /* ARGSUSED */
282 1.32 christos int
283 1.73 dsl sys_setpgid(struct lwp *l, void *v, register_t *retval)
284 1.29 thorpej {
285 1.56 augustss struct sys_setpgid_args /* {
286 1.19 cgd syscallarg(int) pid;
287 1.19 cgd syscallarg(int) pgid;
288 1.29 thorpej } */ *uap = v;
289 1.70 thorpej struct proc *curp = l->l_proc;
290 1.56 augustss struct proc *targp; /* target process */
291 1.14 cgd
292 1.39 mikel if (SCARG(uap, pgid) < 0)
293 1.77 dsl return EINVAL;
294 1.14 cgd
295 1.77 dsl /* XXX MP - there is a horrid race here with targp exiting! */
296 1.19 cgd if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
297 1.77 dsl targp = pfind(SCARG(uap, pid));
298 1.77 dsl if (targp == NULL)
299 1.77 dsl return ESRCH;
300 1.14 cgd } else
301 1.14 cgd targp = curp;
302 1.77 dsl
303 1.19 cgd if (SCARG(uap, pgid) == 0)
304 1.19 cgd SCARG(uap, pgid) = targp->p_pid;
305 1.77 dsl return enterpgrp(targp, SCARG(uap, pgid), 0);
306 1.14 cgd }
307 1.14 cgd
308 1.76 dsl /*
309 1.76 dsl * Set real, effective and saved uids to the requested values.
310 1.76 dsl * non-root callers can only ever change uids to values that match
311 1.76 dsl * one of the processes current uid values.
312 1.76 dsl * This is further restricted by the flags argument.
313 1.76 dsl */
314 1.76 dsl
315 1.76 dsl int
316 1.76 dsl do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
317 1.76 dsl {
318 1.76 dsl int error;
319 1.76 dsl struct proc *p = l->l_proc;
320 1.76 dsl struct pcred *pcred = p->p_cred;
321 1.76 dsl struct ucred *cred = pcred->pc_ucred;
322 1.76 dsl
323 1.76 dsl /* Superuser can do anything it wants to.... */
324 1.76 dsl error = suser(cred, &p->p_acflag);
325 1.76 dsl if (error) {
326 1.76 dsl /* Otherwise check new value is one of the allowed
327 1.76 dsl existing values. */
328 1.76 dsl if (r != -1 && !((flags & ID_R_EQ_R) && r == pcred->p_ruid)
329 1.76 dsl && !((flags & ID_R_EQ_E) && r == cred->cr_uid)
330 1.76 dsl && !((flags & ID_R_EQ_S) && r == pcred->p_svuid))
331 1.76 dsl return error;
332 1.76 dsl if (e != -1 && !((flags & ID_E_EQ_R) && e == pcred->p_ruid)
333 1.76 dsl && !((flags & ID_E_EQ_E) && e == cred->cr_uid)
334 1.76 dsl && !((flags & ID_E_EQ_S) && e == pcred->p_svuid))
335 1.76 dsl return error;
336 1.76 dsl if (sv != -1 && !((flags & ID_S_EQ_R) && sv == pcred->p_ruid)
337 1.76 dsl && !((flags & ID_S_EQ_E) && sv == cred->cr_uid)
338 1.76 dsl && !((flags & ID_S_EQ_S) && sv == pcred->p_svuid))
339 1.76 dsl return error;
340 1.76 dsl }
341 1.76 dsl
342 1.76 dsl /* If nothing has changed, short circuit the request */
343 1.76 dsl if ((r == -1 || r == pcred->p_ruid)
344 1.76 dsl && (e == -1 || e == cred->cr_uid)
345 1.76 dsl && (sv == -1 || sv == pcred->p_svuid))
346 1.76 dsl /* nothing to do */
347 1.76 dsl return 0;
348 1.76 dsl
349 1.76 dsl /* The pcred structure is not actually shared... */
350 1.76 dsl if (r != -1 && r != pcred->p_ruid) {
351 1.76 dsl /* Update count of processes for this user */
352 1.76 dsl (void)chgproccnt(pcred->p_ruid, -1);
353 1.76 dsl (void)chgproccnt(r, 1);
354 1.76 dsl pcred->p_ruid = r;
355 1.76 dsl }
356 1.76 dsl if (sv != -1)
357 1.76 dsl pcred->p_svuid = sv;
358 1.76 dsl if (e != -1 && e != cred->cr_uid) {
359 1.76 dsl /* Update a clone of the current credentials */
360 1.76 dsl pcred->pc_ucred = cred = crcopy(cred);
361 1.76 dsl cred->cr_uid = e;
362 1.76 dsl }
363 1.76 dsl
364 1.76 dsl /* Mark process as having changed credentials, stops tracing etc */
365 1.76 dsl p_sugid(p);
366 1.76 dsl return 0;
367 1.76 dsl }
368 1.76 dsl
369 1.76 dsl /*
370 1.76 dsl * Set real, effective and saved gids to the requested values.
371 1.76 dsl * non-root callers can only ever change gids to values that match
372 1.76 dsl * one of the processes current gid values.
373 1.76 dsl * This is further restricted by the flags argument.
374 1.76 dsl */
375 1.76 dsl
376 1.76 dsl int
377 1.76 dsl do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
378 1.76 dsl {
379 1.76 dsl int error;
380 1.76 dsl struct proc *p = l->l_proc;
381 1.76 dsl struct pcred *pcred = p->p_cred;
382 1.76 dsl struct ucred *cred = pcred->pc_ucred;
383 1.76 dsl
384 1.76 dsl /* Superuser can do anything it wants to.... */
385 1.76 dsl error = suser(cred, &p->p_acflag);
386 1.76 dsl if (error) {
387 1.76 dsl /* Otherwise check new value is one of the allowed
388 1.76 dsl existing values. */
389 1.76 dsl if (r != -1 && !((flags & ID_R_EQ_R) && r == pcred->p_rgid)
390 1.76 dsl && !((flags & ID_R_EQ_E) && r == cred->cr_gid)
391 1.76 dsl && !((flags & ID_R_EQ_S) && r == pcred->p_svgid))
392 1.76 dsl return error;
393 1.76 dsl if (e != -1 && !((flags & ID_E_EQ_R) && e == pcred->p_rgid)
394 1.76 dsl && !((flags & ID_E_EQ_E) && e == cred->cr_gid)
395 1.76 dsl && !((flags & ID_E_EQ_S) && e == pcred->p_svgid))
396 1.76 dsl return error;
397 1.76 dsl if (sv != -1 && !((flags & ID_S_EQ_R) && sv == pcred->p_rgid)
398 1.76 dsl && !((flags & ID_S_EQ_E) && sv == cred->cr_gid)
399 1.76 dsl && !((flags & ID_S_EQ_S) && sv == pcred->p_svgid))
400 1.76 dsl return error;
401 1.76 dsl }
402 1.76 dsl
403 1.76 dsl /* If nothing has changed, short circuit the request */
404 1.76 dsl if ((r == -1 || r == pcred->p_rgid)
405 1.76 dsl && (e == -1 || e == cred->cr_gid)
406 1.76 dsl && (sv == -1 || sv == pcred->p_svgid))
407 1.76 dsl /* nothing to do */
408 1.76 dsl return 0;
409 1.76 dsl
410 1.76 dsl /* The pcred structure is not actually shared... */
411 1.76 dsl if (r != -1)
412 1.76 dsl pcred->p_rgid = r;
413 1.76 dsl if (sv != -1)
414 1.76 dsl pcred->p_svgid = sv;
415 1.76 dsl if (e != -1 && e != cred->cr_gid) {
416 1.76 dsl /* Update a clone of the current credentials */
417 1.76 dsl pcred->pc_ucred = cred = crcopy(cred);
418 1.76 dsl cred->cr_gid = e;
419 1.76 dsl }
420 1.76 dsl
421 1.76 dsl /* Mark process as having changed credentials, stops tracing etc */
422 1.76 dsl p_sugid(p);
423 1.76 dsl return 0;
424 1.76 dsl }
425 1.76 dsl
426 1.14 cgd /* ARGSUSED */
427 1.32 christos int
428 1.73 dsl sys_setuid(struct lwp *l, void *v, register_t *retval)
429 1.29 thorpej {
430 1.30 mycroft struct sys_setuid_args /* {
431 1.19 cgd syscallarg(uid_t) uid;
432 1.29 thorpej } */ *uap = v;
433 1.76 dsl uid_t uid = SCARG(uap, uid);
434 1.14 cgd
435 1.76 dsl return do_setresuid(l, uid, uid, uid,
436 1.76 dsl ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
437 1.14 cgd }
438 1.14 cgd
439 1.14 cgd /* ARGSUSED */
440 1.32 christos int
441 1.73 dsl sys_seteuid(struct lwp *l, void *v, register_t *retval)
442 1.29 thorpej {
443 1.30 mycroft struct sys_seteuid_args /* {
444 1.19 cgd syscallarg(uid_t) euid;
445 1.29 thorpej } */ *uap = v;
446 1.59 christos
447 1.76 dsl return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
448 1.14 cgd }
449 1.14 cgd
450 1.35 mycroft int
451 1.73 dsl sys_setreuid(struct lwp *l, void *v, register_t *retval)
452 1.35 mycroft {
453 1.35 mycroft struct sys_setreuid_args /* {
454 1.35 mycroft syscallarg(uid_t) ruid;
455 1.35 mycroft syscallarg(uid_t) euid;
456 1.35 mycroft } */ *uap = v;
457 1.70 thorpej struct proc *p = l->l_proc;
458 1.76 dsl uid_t ruid, euid, svuid;
459 1.35 mycroft
460 1.35 mycroft ruid = SCARG(uap, ruid);
461 1.35 mycroft euid = SCARG(uap, euid);
462 1.76 dsl if (ruid == -1)
463 1.76 dsl ruid = p->p_cred->p_ruid;
464 1.76 dsl if (euid == -1)
465 1.76 dsl euid = p->p_ucred->cr_uid;
466 1.76 dsl /* Saved uid is set to the new euid if the ruid changed */
467 1.78 jdolecek svuid = (ruid == p->p_cred->p_ruid) ? -1 : euid;
468 1.76 dsl
469 1.76 dsl return do_setresuid(l, ruid, euid, svuid,
470 1.76 dsl ID_R_EQ_R | ID_R_EQ_E |
471 1.76 dsl ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
472 1.76 dsl ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
473 1.35 mycroft }
474 1.35 mycroft
475 1.14 cgd /* ARGSUSED */
476 1.32 christos int
477 1.73 dsl sys_setgid(struct lwp *l, void *v, register_t *retval)
478 1.29 thorpej {
479 1.30 mycroft struct sys_setgid_args /* {
480 1.19 cgd syscallarg(gid_t) gid;
481 1.29 thorpej } */ *uap = v;
482 1.76 dsl gid_t gid = SCARG(uap, gid);
483 1.14 cgd
484 1.76 dsl return do_setresgid(l, gid, gid, gid,
485 1.76 dsl ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
486 1.14 cgd }
487 1.14 cgd
488 1.14 cgd /* ARGSUSED */
489 1.32 christos int
490 1.73 dsl sys_setegid(struct lwp *l, void *v, register_t *retval)
491 1.29 thorpej {
492 1.30 mycroft struct sys_setegid_args /* {
493 1.19 cgd syscallarg(gid_t) egid;
494 1.29 thorpej } */ *uap = v;
495 1.14 cgd
496 1.76 dsl return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
497 1.35 mycroft }
498 1.35 mycroft
499 1.35 mycroft int
500 1.73 dsl sys_setregid(struct lwp *l, void *v, register_t *retval)
501 1.35 mycroft {
502 1.35 mycroft struct sys_setregid_args /* {
503 1.35 mycroft syscallarg(gid_t) rgid;
504 1.35 mycroft syscallarg(gid_t) egid;
505 1.35 mycroft } */ *uap = v;
506 1.70 thorpej struct proc *p = l->l_proc;
507 1.76 dsl gid_t rgid, egid, svgid;
508 1.35 mycroft
509 1.35 mycroft rgid = SCARG(uap, rgid);
510 1.35 mycroft egid = SCARG(uap, egid);
511 1.76 dsl if (rgid == -1)
512 1.76 dsl rgid = p->p_cred->p_rgid;
513 1.76 dsl if (egid == -1)
514 1.76 dsl egid = p->p_ucred->cr_gid;
515 1.76 dsl /* Saved gid is set to the new egid if the rgid changed */
516 1.76 dsl svgid = rgid == p->p_cred->p_rgid ? -1 : egid;
517 1.76 dsl
518 1.76 dsl return do_setresgid(l, rgid, egid, svgid,
519 1.76 dsl ID_R_EQ_R | ID_R_EQ_E |
520 1.76 dsl ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
521 1.76 dsl ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
522 1.57 minoura }
523 1.57 minoura
524 1.57 minoura int
525 1.73 dsl sys_issetugid(struct lwp *l, void *v, register_t *retval)
526 1.57 minoura {
527 1.70 thorpej struct proc *p = l->l_proc;
528 1.75 enami
529 1.57 minoura /*
530 1.57 minoura * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
531 1.57 minoura * we use P_SUGID because we consider changing the owners as
532 1.57 minoura * "tainting" as well.
533 1.57 minoura * This is significant for procs that start as root and "become"
534 1.57 minoura * a user without an exec - programs cannot know *everything*
535 1.57 minoura * that libc *might* have put in their data segment.
536 1.57 minoura */
537 1.57 minoura *retval = (p->p_flag & P_SUGID) != 0;
538 1.60 christos return (0);
539 1.14 cgd }
540 1.14 cgd
541 1.86 christos /*
542 1.86 christos * sort -u for groups.
543 1.86 christos */
544 1.86 christos static int
545 1.86 christos grsortu(gid_t *grp, int ngrp)
546 1.86 christos {
547 1.86 christos int i, j, k;
548 1.86 christos static const size_t gs = sizeof(grp[0]);
549 1.86 christos
550 1.86 christos /* bubble sort */
551 1.86 christos for (i = 0; i < ngrp; i++)
552 1.86 christos for (j = i + 1; j < ngrp; j++)
553 1.86 christos if (grp[i] > grp[j]) {
554 1.86 christos gid_t tmp = grp[i];
555 1.86 christos grp[i] = grp[j];
556 1.86 christos grp[j] = tmp;
557 1.86 christos }
558 1.86 christos /* uniq */
559 1.86 christos for (i = 0; i < ngrp; i++) {
560 1.86 christos for (j = i + 1; j < ngrp && grp[i] == grp[j]; j++)
561 1.86 christos continue;
562 1.86 christos k = j - (i + 1);
563 1.86 christos if (k == 0)
564 1.86 christos continue;
565 1.86 christos (void)memcpy(&grp[i + 1], &grp[j], gs * (ngrp - j));
566 1.86 christos ngrp -= k;
567 1.86 christos #ifdef DIAGNOSTIC
568 1.86 christos /* zero out the rest of the array */
569 1.86 christos (void)memset(&grp[ngrp], 0, gs * k);
570 1.86 christos #endif
571 1.86 christos }
572 1.86 christos return ngrp;
573 1.86 christos }
574 1.86 christos
575 1.15 cgd /* ARGSUSED */
576 1.32 christos int
577 1.73 dsl sys_setgroups(struct lwp *l, void *v, register_t *retval)
578 1.29 thorpej {
579 1.30 mycroft struct sys_setgroups_args /* {
580 1.41 thorpej syscallarg(int) gidsetsize;
581 1.38 cgd syscallarg(const gid_t *) gidset;
582 1.29 thorpej } */ *uap = v;
583 1.70 thorpej struct proc *p = l->l_proc;
584 1.56 augustss struct pcred *pc = p->p_cred;
585 1.56 augustss int ngrp;
586 1.15 cgd int error;
587 1.59 christos gid_t grp[NGROUPS];
588 1.59 christos size_t grsize;
589 1.15 cgd
590 1.32 christos if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
591 1.15 cgd return (error);
592 1.59 christos
593 1.19 cgd ngrp = SCARG(uap, gidsetsize);
594 1.42 mycroft if ((u_int)ngrp > NGROUPS)
595 1.15 cgd return (EINVAL);
596 1.59 christos
597 1.59 christos grsize = ngrp * sizeof(gid_t);
598 1.59 christos error = copyin(SCARG(uap, gidset), grp, grsize);
599 1.32 christos if (error)
600 1.15 cgd return (error);
601 1.86 christos
602 1.86 christos ngrp = grsortu(grp, ngrp);
603 1.59 christos /*
604 1.60 christos * Check if this is a no-op.
605 1.59 christos */
606 1.69 thorpej if (pc->pc_ucred->cr_ngroups == (u_int) ngrp &&
607 1.59 christos memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
608 1.60 christos return (0);
609 1.59 christos
610 1.59 christos pc->pc_ucred = crcopy(pc->pc_ucred);
611 1.59 christos (void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
612 1.15 cgd pc->pc_ucred->cr_ngroups = ngrp;
613 1.55 bouyer p_sugid(p);
614 1.15 cgd return (0);
615 1.15 cgd }
616 1.14 cgd
617 1.14 cgd /*
618 1.14 cgd * Check if gid is a member of the group set.
619 1.14 cgd */
620 1.32 christos int
621 1.73 dsl groupmember(gid_t gid, const struct ucred *cred)
622 1.14 cgd {
623 1.73 dsl const gid_t *gp;
624 1.73 dsl const gid_t *egp;
625 1.14 cgd
626 1.14 cgd egp = &(cred->cr_groups[cred->cr_ngroups]);
627 1.14 cgd for (gp = cred->cr_groups; gp < egp; gp++)
628 1.14 cgd if (*gp == gid)
629 1.14 cgd return (1);
630 1.14 cgd return (0);
631 1.14 cgd }
632 1.14 cgd
633 1.14 cgd /*
634 1.14 cgd * Test whether the specified credentials imply "super-user"
635 1.14 cgd * privilege; if so, and we have accounting info, set the flag
636 1.14 cgd * indicating use of super-powers.
637 1.14 cgd * Returns 0 or error.
638 1.14 cgd */
639 1.32 christos int
640 1.73 dsl suser(const struct ucred *cred, u_short *acflag)
641 1.14 cgd {
642 1.75 enami
643 1.14 cgd if (cred->cr_uid == 0) {
644 1.14 cgd if (acflag)
645 1.14 cgd *acflag |= ASU;
646 1.14 cgd return (0);
647 1.14 cgd }
648 1.14 cgd return (EPERM);
649 1.14 cgd }
650 1.14 cgd
651 1.14 cgd /*
652 1.14 cgd * Allocate a zeroed cred structure.
653 1.14 cgd */
654 1.14 cgd struct ucred *
655 1.73 dsl crget(void)
656 1.14 cgd {
657 1.56 augustss struct ucred *cr;
658 1.14 cgd
659 1.82 simonb cr = pool_get(&cred_pool, PR_WAITOK);
660 1.82 simonb memset(cr, 0, sizeof(*cr));
661 1.83 pk simple_lock_init(&cr->cr_lock);
662 1.14 cgd cr->cr_ref = 1;
663 1.14 cgd return (cr);
664 1.14 cgd }
665 1.14 cgd
666 1.14 cgd /*
667 1.14 cgd * Free a cred structure.
668 1.14 cgd * Throws away space when ref count gets to 0.
669 1.14 cgd */
670 1.22 cgd void
671 1.73 dsl crfree(struct ucred *cr)
672 1.14 cgd {
673 1.83 pk int n;
674 1.14 cgd
675 1.83 pk simple_lock(&cr->cr_lock);
676 1.83 pk n = --cr->cr_ref;
677 1.83 pk simple_unlock(&cr->cr_lock);
678 1.83 pk if (n == 0)
679 1.82 simonb pool_put(&cred_pool, cr);
680 1.79 christos }
681 1.79 christos
682 1.79 christos /*
683 1.79 christos * Compare cred structures and return 0 if they match
684 1.79 christos */
685 1.79 christos int
686 1.79 christos crcmp(const struct ucred *cr1, const struct uucred *cr2)
687 1.79 christos {
688 1.85 rillig /* FIXME: The group lists should be compared element by element,
689 1.85 rillig * as the order of groups may be different in the two lists.
690 1.85 rillig * Currently this function can return a non-zero value for
691 1.85 rillig * equivalent group lists. */
692 1.79 christos return cr1->cr_uid != cr2->cr_uid ||
693 1.79 christos cr1->cr_gid != cr2->cr_gid ||
694 1.79 christos cr1->cr_ngroups != (uint32_t)cr2->cr_ngroups ||
695 1.85 rillig memcmp(cr1->cr_groups, cr2->cr_groups,
696 1.85 rillig sizeof(cr1->cr_groups[0]) * cr1->cr_ngroups);
697 1.14 cgd }
698 1.14 cgd
699 1.14 cgd /*
700 1.14 cgd * Copy cred structure to a new one and free the old one.
701 1.14 cgd */
702 1.14 cgd struct ucred *
703 1.73 dsl crcopy(struct ucred *cr)
704 1.14 cgd {
705 1.14 cgd struct ucred *newcr;
706 1.14 cgd
707 1.84 pk if (cr->cr_ref == 1)
708 1.14 cgd return (cr);
709 1.83 pk
710 1.14 cgd newcr = crget();
711 1.83 pk memcpy(&newcr->cr_startcopy, &cr->cr_startcopy,
712 1.83 pk sizeof(struct ucred) - offsetof(struct ucred, cr_startcopy));
713 1.14 cgd crfree(cr);
714 1.14 cgd return (newcr);
715 1.14 cgd }
716 1.14 cgd
717 1.14 cgd /*
718 1.14 cgd * Dup cred struct to a new held one.
719 1.14 cgd */
720 1.14 cgd struct ucred *
721 1.73 dsl crdup(const struct ucred *cr)
722 1.14 cgd {
723 1.14 cgd struct ucred *newcr;
724 1.14 cgd
725 1.14 cgd newcr = crget();
726 1.83 pk memcpy(&newcr->cr_startcopy, &cr->cr_startcopy,
727 1.83 pk sizeof(struct ucred) - offsetof(struct ucred, cr_startcopy));
728 1.14 cgd return (newcr);
729 1.67 christos }
730 1.67 christos
731 1.67 christos /*
732 1.67 christos * convert from userland credentials to kernel one
733 1.67 christos */
734 1.67 christos void
735 1.73 dsl crcvt(struct ucred *uc, const struct uucred *uuc)
736 1.67 christos {
737 1.75 enami
738 1.67 christos uc->cr_ref = 0;
739 1.67 christos uc->cr_uid = uuc->cr_uid;
740 1.67 christos uc->cr_gid = uuc->cr_gid;
741 1.67 christos uc->cr_ngroups = uuc->cr_ngroups;
742 1.67 christos (void)memcpy(uc->cr_groups, uuc->cr_groups, sizeof(uuc->cr_groups));
743 1.14 cgd }
744 1.14 cgd
745 1.14 cgd /*
746 1.14 cgd * Get login name, if available.
747 1.14 cgd */
748 1.14 cgd /* ARGSUSED */
749 1.32 christos int
750 1.73 dsl sys___getlogin(struct lwp *l, void *v, register_t *retval)
751 1.29 thorpej {
752 1.37 jtc struct sys___getlogin_args /* {
753 1.19 cgd syscallarg(char *) namebuf;
754 1.53 kleink syscallarg(size_t) namelen;
755 1.29 thorpej } */ *uap = v;
756 1.70 thorpej struct proc *p = l->l_proc;
757 1.14 cgd
758 1.48 perry if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
759 1.48 perry SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
760 1.14 cgd return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
761 1.19 cgd (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
762 1.14 cgd }
763 1.14 cgd
764 1.14 cgd /*
765 1.14 cgd * Set login name.
766 1.14 cgd */
767 1.14 cgd /* ARGSUSED */
768 1.32 christos int
769 1.73 dsl sys___setlogin(struct lwp *l, void *v, register_t *retval)
770 1.29 thorpej {
771 1.70 thorpej struct sys___setlogin_args /* {
772 1.38 cgd syscallarg(const char *) namebuf;
773 1.29 thorpej } */ *uap = v;
774 1.70 thorpej struct proc *p = l->l_proc;
775 1.72 dsl struct session *s = p->p_pgrp->pg_session;
776 1.72 dsl char newname[sizeof s->s_login + 1];
777 1.14 cgd int error;
778 1.14 cgd
779 1.32 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
780 1.14 cgd return (error);
781 1.72 dsl error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
782 1.72 dsl if (error != 0)
783 1.75 enami return (error == ENAMETOOLONG ? EINVAL : error);
784 1.72 dsl
785 1.72 dsl if (s->s_flags & S_LOGIN_SET && p->p_pid != s->s_sid &&
786 1.72 dsl strncmp(newname, s->s_login, sizeof s->s_login) != 0)
787 1.74 wiz log(LOG_WARNING, "%s (pid %d) changing logname from "
788 1.74 wiz "%.*s to %s\n", p->p_comm, p->p_pid,
789 1.74 wiz (int)sizeof s->s_login, s->s_login, newname);
790 1.72 dsl s->s_flags |= S_LOGIN_SET;
791 1.72 dsl strncpy(s->s_login, newname, sizeof s->s_login);
792 1.75 enami return (0);
793 1.14 cgd }
794