kern_prot.c revision 1.63.2.5 1 1.63.2.5 nathanw /* $NetBSD: kern_prot.c,v 1.63.2.5 2002/02/28 19:59:35 nathanw 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.14 cgd * 3. All advertising materials mentioning features or use of this software
21 1.14 cgd * must display the following acknowledgement:
22 1.14 cgd * This product includes software developed by the University of
23 1.14 cgd * California, Berkeley and its contributors.
24 1.14 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.14 cgd * may be used to endorse or promote products derived from this software
26 1.14 cgd * without specific prior written permission.
27 1.14 cgd *
28 1.14 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.14 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.14 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.14 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.14 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.14 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.14 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.14 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.14 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.14 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.14 cgd * SUCH DAMAGE.
39 1.14 cgd *
40 1.44 fvdl * @(#)kern_prot.c 8.9 (Berkeley) 2/14/95
41 1.14 cgd */
42 1.14 cgd
43 1.14 cgd /*
44 1.14 cgd * System calls related to processes and protection
45 1.14 cgd */
46 1.63.2.3 nathanw
47 1.63.2.3 nathanw #include <sys/cdefs.h>
48 1.63.2.5 nathanw __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.63.2.5 2002/02/28 19:59:35 nathanw Exp $");
49 1.45 thorpej
50 1.51 christos #include "opt_compat_43.h"
51 1.14 cgd
52 1.14 cgd #include <sys/param.h>
53 1.14 cgd #include <sys/acct.h>
54 1.14 cgd #include <sys/systm.h>
55 1.14 cgd #include <sys/ucred.h>
56 1.63.2.1 nathanw #include <sys/lwp.h>
57 1.14 cgd #include <sys/proc.h>
58 1.14 cgd #include <sys/timeb.h>
59 1.14 cgd #include <sys/times.h>
60 1.14 cgd #include <sys/malloc.h>
61 1.14 cgd
62 1.19 cgd #include <sys/mount.h>
63 1.19 cgd #include <sys/syscallargs.h>
64 1.63 mycroft
65 1.63.2.1 nathanw int sys_getpid(struct lwp *, void *, register_t *);
66 1.63.2.1 nathanw int sys_getpid_with_ppid(struct lwp *, void *, register_t *);
67 1.63.2.1 nathanw int sys_getuid(struct lwp *, void *, register_t *);
68 1.63.2.1 nathanw int sys_getuid_with_euid(struct lwp *, void *, register_t *);
69 1.63.2.1 nathanw int sys_getgid(struct lwp *, void *, register_t *);
70 1.63.2.1 nathanw int sys_getgid_with_egid(struct lwp *, void *, register_t *);
71 1.32 christos
72 1.14 cgd /* ARGSUSED */
73 1.32 christos int
74 1.63.2.1 nathanw sys_getpid(l, v, retval)
75 1.63.2.1 nathanw struct lwp *l;
76 1.30 mycroft void *v;
77 1.19 cgd register_t *retval;
78 1.14 cgd {
79 1.63.2.1 nathanw 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.63.2.1 nathanw sys_getpid_with_ppid(l, v, retval)
88 1.63.2.1 nathanw struct lwp *l;
89 1.62 mycroft void *v;
90 1.62 mycroft register_t *retval;
91 1.62 mycroft {
92 1.63.2.1 nathanw struct proc *p = l->l_proc;
93 1.62 mycroft
94 1.62 mycroft retval[0] = p->p_pid;
95 1.62 mycroft retval[1] = p->p_pptr->p_pid;
96 1.14 cgd return (0);
97 1.14 cgd }
98 1.14 cgd
99 1.14 cgd /* ARGSUSED */
100 1.32 christos int
101 1.63.2.1 nathanw sys_getppid(l, v, retval)
102 1.63.2.1 nathanw struct lwp *l;
103 1.30 mycroft void *v;
104 1.19 cgd register_t *retval;
105 1.14 cgd {
106 1.63.2.1 nathanw struct proc *p = l->l_proc;
107 1.14 cgd
108 1.14 cgd *retval = p->p_pptr->p_pid;
109 1.14 cgd return (0);
110 1.14 cgd }
111 1.14 cgd
112 1.14 cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
113 1.32 christos int
114 1.63.2.1 nathanw sys_getpgrp(l, v, retval)
115 1.63.2.1 nathanw struct lwp *l;
116 1.30 mycroft void *v;
117 1.19 cgd register_t *retval;
118 1.14 cgd {
119 1.63.2.1 nathanw struct proc *p = l->l_proc;
120 1.14 cgd
121 1.14 cgd *retval = p->p_pgrp->pg_id;
122 1.14 cgd return (0);
123 1.43 thorpej }
124 1.43 thorpej
125 1.43 thorpej /*
126 1.43 thorpej * Return the process group ID of the session leader (session ID)
127 1.43 thorpej * for the specified process.
128 1.43 thorpej */
129 1.43 thorpej int
130 1.63.2.1 nathanw sys_getsid(l, v, retval)
131 1.63.2.1 nathanw struct lwp *l;
132 1.43 thorpej void *v;
133 1.43 thorpej register_t *retval;
134 1.43 thorpej {
135 1.43 thorpej struct sys_getsid_args /* {
136 1.43 thorpej syscalldarg(pid_t) pid;
137 1.43 thorpej } */ *uap = v;
138 1.63.2.1 nathanw struct proc *p = l->l_proc;
139 1.43 thorpej
140 1.43 thorpej if (SCARG(uap, pid) == 0)
141 1.43 thorpej goto found;
142 1.43 thorpej if ((p = pfind(SCARG(uap, pid))) == 0)
143 1.43 thorpej return (ESRCH);
144 1.43 thorpej found:
145 1.43 thorpej *retval = p->p_session->s_sid;
146 1.60 christos return (0);
147 1.36 mrg }
148 1.36 mrg
149 1.36 mrg int
150 1.63.2.1 nathanw sys_getpgid(l, v, retval)
151 1.63.2.1 nathanw struct lwp *l;
152 1.36 mrg void *v;
153 1.36 mrg register_t *retval;
154 1.36 mrg {
155 1.56 augustss struct sys_getpgid_args /* {
156 1.36 mrg syscallarg(pid_t) pid;
157 1.36 mrg } */ *uap = v;
158 1.63.2.1 nathanw struct proc *p = l->l_proc;
159 1.36 mrg
160 1.36 mrg if (SCARG(uap, pid) == 0)
161 1.36 mrg goto found;
162 1.36 mrg if ((p = pfind(SCARG(uap, pid))) == 0)
163 1.36 mrg return (ESRCH);
164 1.36 mrg found:
165 1.36 mrg *retval = p->p_pgid;
166 1.60 christos return (0);
167 1.14 cgd }
168 1.14 cgd
169 1.14 cgd /* ARGSUSED */
170 1.32 christos int
171 1.63.2.1 nathanw sys_getuid(l, v, retval)
172 1.63.2.1 nathanw struct lwp *l;
173 1.30 mycroft void *v;
174 1.19 cgd register_t *retval;
175 1.14 cgd {
176 1.63.2.1 nathanw struct proc *p = l->l_proc;
177 1.14 cgd
178 1.14 cgd *retval = p->p_cred->p_ruid;
179 1.62 mycroft return (0);
180 1.62 mycroft }
181 1.62 mycroft
182 1.62 mycroft /* ARGSUSED */
183 1.62 mycroft int
184 1.63.2.1 nathanw sys_getuid_with_euid(l, v, retval)
185 1.63.2.1 nathanw struct lwp *l;
186 1.62 mycroft void *v;
187 1.62 mycroft register_t *retval;
188 1.62 mycroft {
189 1.63.2.1 nathanw struct proc *p = l->l_proc;
190 1.62 mycroft
191 1.62 mycroft retval[0] = p->p_cred->p_ruid;
192 1.62 mycroft retval[1] = p->p_ucred->cr_uid;
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.63.2.1 nathanw sys_geteuid(l, v, retval)
199 1.63.2.1 nathanw struct lwp *l;
200 1.30 mycroft void *v;
201 1.19 cgd register_t *retval;
202 1.14 cgd {
203 1.63.2.1 nathanw struct proc *p = l->l_proc;
204 1.14 cgd
205 1.14 cgd *retval = p->p_ucred->cr_uid;
206 1.14 cgd return (0);
207 1.14 cgd }
208 1.14 cgd
209 1.14 cgd /* ARGSUSED */
210 1.32 christos int
211 1.63.2.1 nathanw sys_getgid(l, v, retval)
212 1.63.2.1 nathanw struct lwp *l;
213 1.30 mycroft void *v;
214 1.19 cgd register_t *retval;
215 1.14 cgd {
216 1.63.2.1 nathanw struct proc *p = l->l_proc;
217 1.14 cgd
218 1.14 cgd *retval = p->p_cred->p_rgid;
219 1.62 mycroft return (0);
220 1.62 mycroft }
221 1.62 mycroft
222 1.62 mycroft /* ARGSUSED */
223 1.62 mycroft int
224 1.63.2.1 nathanw sys_getgid_with_egid(l, v, retval)
225 1.63.2.1 nathanw struct lwp *l;
226 1.62 mycroft void *v;
227 1.62 mycroft register_t *retval;
228 1.62 mycroft {
229 1.63.2.1 nathanw struct proc *p = l->l_proc;
230 1.62 mycroft
231 1.62 mycroft retval[0] = p->p_cred->p_rgid;
232 1.62 mycroft retval[1] = p->p_ucred->cr_gid;
233 1.14 cgd return (0);
234 1.14 cgd }
235 1.14 cgd
236 1.14 cgd /*
237 1.14 cgd * Get effective group ID. The "egid" is groups[0], and could be obtained
238 1.14 cgd * via getgroups. This syscall exists because it is somewhat painful to do
239 1.14 cgd * correctly in a library function.
240 1.14 cgd */
241 1.14 cgd /* ARGSUSED */
242 1.32 christos int
243 1.63.2.1 nathanw sys_getegid(l, v, retval)
244 1.63.2.1 nathanw struct lwp *l;
245 1.30 mycroft void *v;
246 1.19 cgd register_t *retval;
247 1.14 cgd {
248 1.63.2.1 nathanw struct proc *p = l->l_proc;
249 1.27 jtc *retval = p->p_ucred->cr_gid;
250 1.14 cgd return (0);
251 1.14 cgd }
252 1.14 cgd
253 1.32 christos int
254 1.63.2.1 nathanw sys_getgroups(l, v, retval)
255 1.63.2.1 nathanw struct lwp *l;
256 1.29 thorpej void *v;
257 1.29 thorpej register_t *retval;
258 1.29 thorpej {
259 1.56 augustss struct sys_getgroups_args /* {
260 1.41 thorpej syscallarg(int) gidsetsize;
261 1.19 cgd syscallarg(gid_t *) gidset;
262 1.29 thorpej } */ *uap = v;
263 1.63.2.1 nathanw struct proc *p = l->l_proc;
264 1.56 augustss struct pcred *pc = p->p_cred;
265 1.56 augustss int ngrp;
266 1.14 cgd int error;
267 1.14 cgd
268 1.42 mycroft ngrp = SCARG(uap, gidsetsize);
269 1.42 mycroft if (ngrp == 0) {
270 1.14 cgd *retval = pc->pc_ucred->cr_ngroups;
271 1.14 cgd return (0);
272 1.14 cgd }
273 1.14 cgd if (ngrp < pc->pc_ucred->cr_ngroups)
274 1.14 cgd return (EINVAL);
275 1.14 cgd ngrp = pc->pc_ucred->cr_ngroups;
276 1.32 christos error = copyout((caddr_t)pc->pc_ucred->cr_groups,
277 1.32 christos (caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
278 1.32 christos if (error)
279 1.14 cgd return (error);
280 1.14 cgd *retval = ngrp;
281 1.14 cgd return (0);
282 1.14 cgd }
283 1.14 cgd
284 1.14 cgd /* ARGSUSED */
285 1.32 christos int
286 1.63.2.1 nathanw sys_setsid(l, v, retval)
287 1.63.2.1 nathanw struct lwp *l;
288 1.30 mycroft void *v;
289 1.19 cgd register_t *retval;
290 1.14 cgd {
291 1.63.2.1 nathanw struct proc *p = l->l_proc;
292 1.14 cgd
293 1.14 cgd if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
294 1.14 cgd return (EPERM);
295 1.14 cgd } else {
296 1.15 cgd (void)enterpgrp(p, p->p_pid, 1);
297 1.14 cgd *retval = p->p_pid;
298 1.14 cgd return (0);
299 1.14 cgd }
300 1.14 cgd }
301 1.14 cgd
302 1.14 cgd /*
303 1.14 cgd * set process group (setpgid/old setpgrp)
304 1.14 cgd *
305 1.14 cgd * caller does setpgid(targpid, targpgid)
306 1.14 cgd *
307 1.39 mikel * pgid must be in valid range (EINVAL)
308 1.14 cgd * pid must be caller or child of caller (ESRCH)
309 1.14 cgd * if a child
310 1.14 cgd * pid must be in same session (EPERM)
311 1.14 cgd * pid can't have done an exec (EACCES)
312 1.14 cgd * if pgid != pid
313 1.14 cgd * there must exist some pid in same session having pgid (EPERM)
314 1.14 cgd * pid must not be session leader (EPERM)
315 1.14 cgd */
316 1.14 cgd /* ARGSUSED */
317 1.32 christos int
318 1.63.2.1 nathanw sys_setpgid(l, v, retval)
319 1.63.2.1 nathanw struct lwp *l;
320 1.29 thorpej void *v;
321 1.29 thorpej register_t *retval;
322 1.29 thorpej {
323 1.56 augustss struct sys_setpgid_args /* {
324 1.19 cgd syscallarg(int) pid;
325 1.19 cgd syscallarg(int) pgid;
326 1.29 thorpej } */ *uap = v;
327 1.63.2.1 nathanw struct proc *curp = l->l_proc;
328 1.56 augustss struct proc *targp; /* target process */
329 1.56 augustss struct pgrp *pgrp; /* target pgrp */
330 1.14 cgd
331 1.39 mikel if (SCARG(uap, pgid) < 0)
332 1.39 mikel return (EINVAL);
333 1.14 cgd
334 1.19 cgd if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
335 1.58 sommerfe if ((targp = pfind(SCARG(uap, pid))) == 0
336 1.58 sommerfe || !inferior(targp, curp))
337 1.14 cgd return (ESRCH);
338 1.14 cgd if (targp->p_session != curp->p_session)
339 1.14 cgd return (EPERM);
340 1.15 cgd if (targp->p_flag & P_EXEC)
341 1.14 cgd return (EACCES);
342 1.14 cgd } else
343 1.14 cgd targp = curp;
344 1.14 cgd if (SESS_LEADER(targp))
345 1.14 cgd return (EPERM);
346 1.19 cgd if (SCARG(uap, pgid) == 0)
347 1.19 cgd SCARG(uap, pgid) = targp->p_pid;
348 1.19 cgd else if (SCARG(uap, pgid) != targp->p_pid)
349 1.19 cgd if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
350 1.14 cgd pgrp->pg_session != curp->p_session)
351 1.14 cgd return (EPERM);
352 1.19 cgd return (enterpgrp(targp, SCARG(uap, pgid), 0));
353 1.14 cgd }
354 1.14 cgd
355 1.14 cgd /* ARGSUSED */
356 1.32 christos int
357 1.63.2.1 nathanw sys_setuid(l, v, retval)
358 1.63.2.1 nathanw struct lwp *l;
359 1.29 thorpej void *v;
360 1.29 thorpej register_t *retval;
361 1.29 thorpej {
362 1.30 mycroft struct sys_setuid_args /* {
363 1.19 cgd syscallarg(uid_t) uid;
364 1.29 thorpej } */ *uap = v;
365 1.63.2.1 nathanw struct proc *p = l->l_proc;
366 1.56 augustss struct pcred *pc = p->p_cred;
367 1.56 augustss uid_t uid;
368 1.14 cgd int error;
369 1.14 cgd
370 1.19 cgd uid = SCARG(uap, uid);
371 1.14 cgd if (uid != pc->p_ruid &&
372 1.14 cgd (error = suser(pc->pc_ucred, &p->p_acflag)))
373 1.14 cgd return (error);
374 1.14 cgd /*
375 1.60 christos * Check if we are all set, and this is a no-op.
376 1.59 christos */
377 1.59 christos if (pc->p_ruid == uid && pc->p_svuid == uid &&
378 1.59 christos pc->pc_ucred->cr_uid == uid)
379 1.60 christos return (0);
380 1.59 christos /*
381 1.15 cgd * Everything's okay, do it.
382 1.15 cgd * Transfer proc count to new user.
383 1.15 cgd * Copy credentials so other references do not see our changes.
384 1.14 cgd */
385 1.15 cgd (void)chgproccnt(pc->p_ruid, -1);
386 1.15 cgd (void)chgproccnt(uid, 1);
387 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
388 1.14 cgd pc->pc_ucred->cr_uid = uid;
389 1.14 cgd pc->p_ruid = uid;
390 1.14 cgd pc->p_svuid = uid;
391 1.55 bouyer p_sugid(p);
392 1.14 cgd return (0);
393 1.14 cgd }
394 1.14 cgd
395 1.14 cgd /* ARGSUSED */
396 1.32 christos int
397 1.63.2.1 nathanw sys_seteuid(l, v, retval)
398 1.63.2.1 nathanw struct lwp *l;
399 1.29 thorpej void *v;
400 1.29 thorpej register_t *retval;
401 1.29 thorpej {
402 1.30 mycroft struct sys_seteuid_args /* {
403 1.19 cgd syscallarg(uid_t) euid;
404 1.29 thorpej } */ *uap = v;
405 1.63.2.1 nathanw struct proc *p = l->l_proc;
406 1.56 augustss struct pcred *pc = p->p_cred;
407 1.56 augustss uid_t euid;
408 1.14 cgd int error;
409 1.14 cgd
410 1.19 cgd euid = SCARG(uap, euid);
411 1.14 cgd if (euid != pc->p_ruid && euid != pc->p_svuid &&
412 1.14 cgd (error = suser(pc->pc_ucred, &p->p_acflag)))
413 1.14 cgd return (error);
414 1.14 cgd /*
415 1.60 christos * Check if we are all set, and this is a no-op.
416 1.59 christos */
417 1.59 christos if (pc->pc_ucred->cr_uid == euid)
418 1.60 christos return (0);
419 1.59 christos
420 1.59 christos /*
421 1.14 cgd * Everything's okay, do it. Copy credentials so other references do
422 1.14 cgd * not see our changes.
423 1.14 cgd */
424 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
425 1.14 cgd pc->pc_ucred->cr_uid = euid;
426 1.55 bouyer p_sugid(p);
427 1.14 cgd return (0);
428 1.14 cgd }
429 1.14 cgd
430 1.35 mycroft int
431 1.63.2.1 nathanw sys_setreuid(l, v, retval)
432 1.63.2.1 nathanw struct lwp *l;
433 1.35 mycroft void *v;
434 1.35 mycroft register_t *retval;
435 1.35 mycroft {
436 1.35 mycroft struct sys_setreuid_args /* {
437 1.35 mycroft syscallarg(uid_t) ruid;
438 1.35 mycroft syscallarg(uid_t) euid;
439 1.35 mycroft } */ *uap = v;
440 1.63.2.1 nathanw struct proc *p = l->l_proc;
441 1.56 augustss struct pcred *pc = p->p_cred;
442 1.56 augustss uid_t ruid, euid;
443 1.59 christos int error, changed = 0;
444 1.35 mycroft
445 1.35 mycroft ruid = SCARG(uap, ruid);
446 1.35 mycroft euid = SCARG(uap, euid);
447 1.35 mycroft
448 1.35 mycroft if (ruid != (uid_t)-1 &&
449 1.35 mycroft ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
450 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
451 1.35 mycroft return (error);
452 1.35 mycroft
453 1.35 mycroft if (euid != (uid_t)-1 &&
454 1.35 mycroft euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
455 1.35 mycroft euid != pc->p_svuid &&
456 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
457 1.35 mycroft return (error);
458 1.35 mycroft
459 1.59 christos if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
460 1.35 mycroft pc->pc_ucred = crcopy(pc->pc_ucred);
461 1.35 mycroft pc->pc_ucred->cr_uid = euid;
462 1.59 christos changed++;
463 1.35 mycroft }
464 1.35 mycroft
465 1.59 christos if (ruid != (uid_t)-1 &&
466 1.59 christos (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
467 1.35 mycroft (void)chgproccnt(pc->p_ruid, -1);
468 1.35 mycroft (void)chgproccnt(ruid, 1);
469 1.35 mycroft pc->p_ruid = ruid;
470 1.35 mycroft pc->p_svuid = pc->pc_ucred->cr_uid;
471 1.59 christos changed++;
472 1.35 mycroft }
473 1.35 mycroft
474 1.59 christos if (changed)
475 1.55 bouyer p_sugid(p);
476 1.35 mycroft return (0);
477 1.35 mycroft }
478 1.35 mycroft
479 1.14 cgd /* ARGSUSED */
480 1.32 christos int
481 1.63.2.1 nathanw sys_setgid(l, v, retval)
482 1.63.2.1 nathanw struct lwp *l;
483 1.29 thorpej void *v;
484 1.29 thorpej register_t *retval;
485 1.29 thorpej {
486 1.30 mycroft struct sys_setgid_args /* {
487 1.19 cgd syscallarg(gid_t) gid;
488 1.29 thorpej } */ *uap = v;
489 1.63.2.1 nathanw struct proc *p = l->l_proc;
490 1.56 augustss struct pcred *pc = p->p_cred;
491 1.56 augustss gid_t gid;
492 1.14 cgd int error;
493 1.14 cgd
494 1.19 cgd gid = SCARG(uap, gid);
495 1.34 mycroft if (gid != pc->p_rgid &&
496 1.34 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
497 1.14 cgd return (error);
498 1.59 christos /*
499 1.60 christos * Check if we are all set, and this is a no-op.
500 1.59 christos */
501 1.59 christos if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
502 1.59 christos pc->p_svgid == gid)
503 1.60 christos return (0);
504 1.59 christos
505 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
506 1.27 jtc pc->pc_ucred->cr_gid = gid;
507 1.14 cgd pc->p_rgid = gid;
508 1.34 mycroft pc->p_svgid = gid;
509 1.55 bouyer p_sugid(p);
510 1.14 cgd return (0);
511 1.14 cgd }
512 1.14 cgd
513 1.14 cgd /* ARGSUSED */
514 1.32 christos int
515 1.63.2.1 nathanw sys_setegid(l, v, retval)
516 1.63.2.1 nathanw struct lwp *l;
517 1.29 thorpej void *v;
518 1.29 thorpej register_t *retval;
519 1.29 thorpej {
520 1.30 mycroft struct sys_setegid_args /* {
521 1.19 cgd syscallarg(gid_t) egid;
522 1.29 thorpej } */ *uap = v;
523 1.63.2.1 nathanw struct proc *p = l->l_proc;
524 1.56 augustss struct pcred *pc = p->p_cred;
525 1.56 augustss gid_t egid;
526 1.14 cgd int error;
527 1.14 cgd
528 1.19 cgd egid = SCARG(uap, egid);
529 1.14 cgd if (egid != pc->p_rgid && egid != pc->p_svgid &&
530 1.14 cgd (error = suser(pc->pc_ucred, &p->p_acflag)))
531 1.14 cgd return (error);
532 1.59 christos /*
533 1.60 christos * Check if we are all set, and this is a no-op.
534 1.59 christos */
535 1.59 christos if (pc->pc_ucred->cr_gid == egid)
536 1.60 christos return (0);
537 1.59 christos
538 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
539 1.27 jtc pc->pc_ucred->cr_gid = egid;
540 1.55 bouyer p_sugid(p);
541 1.35 mycroft return (0);
542 1.35 mycroft }
543 1.35 mycroft
544 1.35 mycroft int
545 1.63.2.1 nathanw sys_setregid(l, v, retval)
546 1.63.2.1 nathanw struct lwp *l;
547 1.35 mycroft void *v;
548 1.35 mycroft register_t *retval;
549 1.35 mycroft {
550 1.35 mycroft struct sys_setregid_args /* {
551 1.35 mycroft syscallarg(gid_t) rgid;
552 1.35 mycroft syscallarg(gid_t) egid;
553 1.35 mycroft } */ *uap = v;
554 1.63.2.1 nathanw struct proc *p = l->l_proc;
555 1.56 augustss struct pcred *pc = p->p_cred;
556 1.56 augustss gid_t rgid, egid;
557 1.59 christos int error, changed = 0;
558 1.35 mycroft
559 1.35 mycroft rgid = SCARG(uap, rgid);
560 1.35 mycroft egid = SCARG(uap, egid);
561 1.35 mycroft
562 1.35 mycroft if (rgid != (gid_t)-1 &&
563 1.35 mycroft rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
564 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
565 1.35 mycroft return (error);
566 1.35 mycroft
567 1.35 mycroft if (egid != (gid_t)-1 &&
568 1.35 mycroft egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
569 1.35 mycroft egid != pc->p_svgid &&
570 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
571 1.35 mycroft return (error);
572 1.35 mycroft
573 1.59 christos if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
574 1.35 mycroft pc->pc_ucred = crcopy(pc->pc_ucred);
575 1.35 mycroft pc->pc_ucred->cr_gid = egid;
576 1.59 christos changed++;
577 1.35 mycroft }
578 1.35 mycroft
579 1.59 christos if (rgid != (gid_t)-1 &&
580 1.59 christos (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
581 1.35 mycroft pc->p_rgid = rgid;
582 1.35 mycroft pc->p_svgid = pc->pc_ucred->cr_gid;
583 1.59 christos changed++;
584 1.35 mycroft }
585 1.35 mycroft
586 1.59 christos if (changed)
587 1.55 bouyer p_sugid(p);
588 1.14 cgd return (0);
589 1.57 minoura }
590 1.57 minoura
591 1.57 minoura int
592 1.63.2.1 nathanw sys_issetugid(l, v, retval)
593 1.63.2.1 nathanw struct lwp *l;
594 1.57 minoura void *v;
595 1.57 minoura register_t *retval;
596 1.57 minoura {
597 1.63.2.1 nathanw struct proc *p = l->l_proc;
598 1.57 minoura /*
599 1.57 minoura * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
600 1.57 minoura * we use P_SUGID because we consider changing the owners as
601 1.57 minoura * "tainting" as well.
602 1.57 minoura * This is significant for procs that start as root and "become"
603 1.57 minoura * a user without an exec - programs cannot know *everything*
604 1.57 minoura * that libc *might* have put in their data segment.
605 1.57 minoura */
606 1.57 minoura *retval = (p->p_flag & P_SUGID) != 0;
607 1.60 christos return (0);
608 1.14 cgd }
609 1.14 cgd
610 1.15 cgd /* ARGSUSED */
611 1.32 christos int
612 1.63.2.1 nathanw sys_setgroups(l, v, retval)
613 1.63.2.1 nathanw struct lwp *l;
614 1.29 thorpej void *v;
615 1.29 thorpej register_t *retval;
616 1.29 thorpej {
617 1.30 mycroft struct sys_setgroups_args /* {
618 1.41 thorpej syscallarg(int) gidsetsize;
619 1.38 cgd syscallarg(const gid_t *) gidset;
620 1.29 thorpej } */ *uap = v;
621 1.63.2.1 nathanw struct proc *p = l->l_proc;
622 1.56 augustss struct pcred *pc = p->p_cred;
623 1.56 augustss int ngrp;
624 1.15 cgd int error;
625 1.59 christos gid_t grp[NGROUPS];
626 1.59 christos size_t grsize;
627 1.15 cgd
628 1.32 christos if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
629 1.15 cgd return (error);
630 1.59 christos
631 1.19 cgd ngrp = SCARG(uap, gidsetsize);
632 1.42 mycroft if ((u_int)ngrp > NGROUPS)
633 1.15 cgd return (EINVAL);
634 1.59 christos
635 1.59 christos grsize = ngrp * sizeof(gid_t);
636 1.59 christos error = copyin(SCARG(uap, gidset), grp, grsize);
637 1.32 christos if (error)
638 1.15 cgd return (error);
639 1.59 christos /*
640 1.60 christos * Check if this is a no-op.
641 1.59 christos */
642 1.59 christos if (pc->pc_ucred->cr_ngroups == ngrp &&
643 1.59 christos memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
644 1.60 christos return (0);
645 1.59 christos
646 1.59 christos pc->pc_ucred = crcopy(pc->pc_ucred);
647 1.59 christos (void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
648 1.15 cgd pc->pc_ucred->cr_ngroups = ngrp;
649 1.55 bouyer p_sugid(p);
650 1.15 cgd return (0);
651 1.15 cgd }
652 1.14 cgd
653 1.14 cgd /*
654 1.14 cgd * Check if gid is a member of the group set.
655 1.14 cgd */
656 1.32 christos int
657 1.14 cgd groupmember(gid, cred)
658 1.14 cgd gid_t gid;
659 1.56 augustss struct ucred *cred;
660 1.14 cgd {
661 1.56 augustss gid_t *gp;
662 1.14 cgd gid_t *egp;
663 1.14 cgd
664 1.14 cgd egp = &(cred->cr_groups[cred->cr_ngroups]);
665 1.14 cgd for (gp = cred->cr_groups; gp < egp; gp++)
666 1.14 cgd if (*gp == gid)
667 1.14 cgd return (1);
668 1.14 cgd return (0);
669 1.14 cgd }
670 1.14 cgd
671 1.14 cgd /*
672 1.14 cgd * Test whether the specified credentials imply "super-user"
673 1.14 cgd * privilege; if so, and we have accounting info, set the flag
674 1.14 cgd * indicating use of super-powers.
675 1.14 cgd * Returns 0 or error.
676 1.14 cgd */
677 1.32 christos int
678 1.14 cgd suser(cred, acflag)
679 1.14 cgd struct ucred *cred;
680 1.14 cgd u_short *acflag;
681 1.14 cgd {
682 1.14 cgd if (cred->cr_uid == 0) {
683 1.14 cgd if (acflag)
684 1.14 cgd *acflag |= ASU;
685 1.14 cgd return (0);
686 1.14 cgd }
687 1.14 cgd return (EPERM);
688 1.14 cgd }
689 1.14 cgd
690 1.14 cgd /*
691 1.14 cgd * Allocate a zeroed cred structure.
692 1.14 cgd */
693 1.14 cgd struct ucred *
694 1.14 cgd crget()
695 1.14 cgd {
696 1.56 augustss struct ucred *cr;
697 1.14 cgd
698 1.14 cgd MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
699 1.49 perry memset((caddr_t)cr, 0, sizeof(*cr));
700 1.14 cgd cr->cr_ref = 1;
701 1.14 cgd return (cr);
702 1.14 cgd }
703 1.14 cgd
704 1.14 cgd /*
705 1.14 cgd * Free a cred structure.
706 1.14 cgd * Throws away space when ref count gets to 0.
707 1.14 cgd */
708 1.22 cgd void
709 1.14 cgd crfree(cr)
710 1.14 cgd struct ucred *cr;
711 1.14 cgd {
712 1.14 cgd
713 1.15 cgd if (--cr->cr_ref == 0)
714 1.15 cgd FREE((caddr_t)cr, M_CRED);
715 1.14 cgd }
716 1.14 cgd
717 1.14 cgd /*
718 1.14 cgd * Copy cred structure to a new one and free the old one.
719 1.14 cgd */
720 1.14 cgd struct ucred *
721 1.14 cgd crcopy(cr)
722 1.14 cgd struct ucred *cr;
723 1.14 cgd {
724 1.14 cgd struct ucred *newcr;
725 1.14 cgd
726 1.14 cgd if (cr->cr_ref == 1)
727 1.14 cgd return (cr);
728 1.14 cgd newcr = crget();
729 1.14 cgd *newcr = *cr;
730 1.14 cgd crfree(cr);
731 1.14 cgd newcr->cr_ref = 1;
732 1.14 cgd return (newcr);
733 1.14 cgd }
734 1.14 cgd
735 1.14 cgd /*
736 1.14 cgd * Dup cred struct to a new held one.
737 1.14 cgd */
738 1.14 cgd struct ucred *
739 1.14 cgd crdup(cr)
740 1.14 cgd struct ucred *cr;
741 1.14 cgd {
742 1.14 cgd struct ucred *newcr;
743 1.14 cgd
744 1.14 cgd newcr = crget();
745 1.14 cgd *newcr = *cr;
746 1.14 cgd newcr->cr_ref = 1;
747 1.14 cgd return (newcr);
748 1.63.2.4 nathanw }
749 1.63.2.4 nathanw
750 1.63.2.4 nathanw /*
751 1.63.2.4 nathanw * convert from userland credentials to kernel one
752 1.63.2.4 nathanw */
753 1.63.2.4 nathanw void
754 1.63.2.4 nathanw crcvt(uc, uuc)
755 1.63.2.4 nathanw struct ucred *uc;
756 1.63.2.4 nathanw const struct uucred *uuc;
757 1.63.2.4 nathanw {
758 1.63.2.4 nathanw uc->cr_ref = 0;
759 1.63.2.4 nathanw uc->cr_uid = uuc->cr_uid;
760 1.63.2.4 nathanw uc->cr_gid = uuc->cr_gid;
761 1.63.2.4 nathanw uc->cr_ngroups = uuc->cr_ngroups;
762 1.63.2.4 nathanw (void)memcpy(uc->cr_groups, uuc->cr_groups, sizeof(uuc->cr_groups));
763 1.14 cgd }
764 1.14 cgd
765 1.14 cgd /*
766 1.14 cgd * Get login name, if available.
767 1.14 cgd */
768 1.14 cgd /* ARGSUSED */
769 1.32 christos int
770 1.63.2.1 nathanw sys___getlogin(l, v, retval)
771 1.63.2.1 nathanw struct lwp *l;
772 1.29 thorpej void *v;
773 1.29 thorpej register_t *retval;
774 1.29 thorpej {
775 1.37 jtc struct sys___getlogin_args /* {
776 1.19 cgd syscallarg(char *) namebuf;
777 1.53 kleink syscallarg(size_t) namelen;
778 1.29 thorpej } */ *uap = v;
779 1.63.2.1 nathanw struct proc *p = l->l_proc;
780 1.14 cgd
781 1.48 perry if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
782 1.48 perry SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
783 1.14 cgd return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
784 1.19 cgd (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
785 1.14 cgd }
786 1.14 cgd
787 1.14 cgd /*
788 1.14 cgd * Set login name.
789 1.14 cgd */
790 1.14 cgd /* ARGSUSED */
791 1.32 christos int
792 1.63.2.5 nathanw sys___setlogin(l, v, retval)
793 1.63.2.1 nathanw struct lwp *l;
794 1.29 thorpej void *v;
795 1.29 thorpej register_t *retval;
796 1.29 thorpej {
797 1.63.2.5 nathanw struct sys___setlogin_args /* {
798 1.38 cgd syscallarg(const char *) namebuf;
799 1.29 thorpej } */ *uap = v;
800 1.63.2.1 nathanw struct proc *p = l->l_proc;
801 1.14 cgd int error;
802 1.14 cgd
803 1.32 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
804 1.14 cgd return (error);
805 1.38 cgd error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
806 1.48 perry sizeof(p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
807 1.14 cgd if (error == ENAMETOOLONG)
808 1.14 cgd error = EINVAL;
809 1.14 cgd return (error);
810 1.14 cgd }
811