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