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