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