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