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