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