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