xref: /original-bsd/sys/kern/kern_prot.c (revision e0c0d005)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_prot.c	7.18 (Berkeley) 02/06/91
8  */
9 
10 /*
11  * System calls related to processes and protection
12  */
13 
14 #include "param.h"
15 #include "acct.h"
16 #include "systm.h"
17 #include "user.h"
18 #include "proc.h"
19 #include "timeb.h"
20 #include "times.h"
21 #include "malloc.h"
22 
23 /* ARGSUSED */
24 getpid(p, uap, retval)
25 	struct proc *p;
26 	void *uap;
27 	int *retval;
28 {
29 
30 	*retval = p->p_pid;
31 #ifdef COMPAT_43
32 	retval[1] = p->p_ppid;
33 #endif
34 	return (0);
35 }
36 
37 /* ARGSUSED */
38 getppid(p, uap, retval)
39 	struct proc *p;
40 	void *uap;
41 	int *retval;
42 {
43 
44 	*retval = p->p_ppid;
45 	return (0);
46 }
47 
48 getpgrp(p, uap, retval)
49 	struct proc *p;
50 	struct args {
51 		int	pid;
52 	} *uap;
53 	int *retval;
54 {
55 
56 	if (uap->pid != 0 && (p = pfind(uap->pid)) == 0)
57 		return (ESRCH);
58 	*retval = p->p_pgrp->pg_id;
59 	return (0);
60 }
61 
62 /* ARGSUSED */
63 getuid(p, uap, retval)
64 	struct proc *p;
65 	void *uap;
66 	int *retval;
67 {
68 
69 	*retval = p->p_ruid;
70 #ifdef COMPAT_43
71 	retval[1] = u.u_cred->cr_uid;
72 #endif
73 	return (0);
74 }
75 
76 /* ARGSUSED */
77 geteuid(p, uap, retval)
78 	struct proc *p;
79 	void *uap;
80 	int *retval;
81 {
82 
83 	*retval = u.u_cred->cr_uid;
84 	return (0);
85 }
86 
87 /* ARGSUSED */
88 getgid(p, uap, retval)
89 	struct proc *p;
90 	void *uap;
91 	int *retval;
92 {
93 
94 	*retval = p->p_rgid;
95 #ifdef COMPAT_43
96 	retval[1] = u.u_cred->cr_groups[0];
97 #endif
98 	return (0);
99 }
100 
101 /*
102  * Get effective group ID.  The "egid" is groups[0], and could be obtained
103  * via getgroups.  This syscall exists because it is somewhat painful to do
104  * correctly in a library function.
105  */
106 /* ARGSUSED */
107 getegid(p, uap, retval)
108 	struct proc *p;
109 	void *uap;
110 	int *retval;
111 {
112 	*retval = u.u_cred->cr_groups[0];
113 	return (0);
114 }
115 
116 getgroups(p, uap, retval)
117 	struct proc *p;
118 	register struct	arg {
119 		u_int	gidsetsize;
120 		int	*gidset;		/* XXX not yet POSIX */
121 	} *uap;
122 	int *retval;
123 {
124 	register gid_t *gp;
125 	register int *lp;
126 	register u_int ngrp;
127 	int groups[NGROUPS];
128 	int error;
129 
130 	if ((ngrp = uap->gidsetsize) == 0) {
131 		*retval = u.u_cred->cr_ngroups;
132 		return (0);
133 	}
134 	if (ngrp < u.u_cred->cr_ngroups)
135 		return (EINVAL);
136 	ngrp = u.u_cred->cr_ngroups;
137 	for (gp = u.u_cred->cr_groups, lp = groups; lp < &groups[ngrp]; )
138 		*lp++ = *gp++;
139 	if (error = copyout((caddr_t)groups, (caddr_t)uap->gidset,
140 	    ngrp * sizeof (groups[0])))
141 		return (error);
142 	*retval = ngrp;
143 	return (0);
144 }
145 
146 /* ARGSUSED */
147 setsid(p, uap, retval)
148 	struct proc *p;
149 	void *uap;
150 	int *retval;
151 {
152 
153 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
154 		return (EPERM);
155 	} else {
156 		pgmv(p, p->p_pid, 1);
157 		*retval = p->p_pid;
158 		return (0);
159 	}
160 }
161 
162 /*
163  * set process group (setpgrp/setpgid)
164  *
165  * caller does setpgrp(pid, pgid)
166  *
167  * pid must be caller or child of caller (ESRCH)
168  * if a child
169  *	pid must be in same session (EPERM)
170  *	pid can't have done an exec (EACCES)
171  * if pgid != pid
172  * 	there must exist some pid in same session having pgid (EPERM)
173  * pid must not be session leader (EPERM)
174  */
175 /* ARGSUSED */
176 setpgrp(cp, uap, retval)
177 	struct proc *cp;
178 	register struct args {
179 		int	pid;
180 		int	pgid;
181 	} *uap;
182 	int *retval;
183 {
184 	register struct proc *p;
185 	register struct pgrp *pgrp;
186 
187 	if (uap->pid != 0) {
188 		if ((p = pfind(uap->pid)) == 0 || !inferior(p))
189 			return (ESRCH);
190 		if (p->p_session != cp->p_session)
191 			return (EPERM);
192 		if (p->p_flag&SEXEC)
193 			return (EACCES);
194 	} else
195 		p = cp;
196 	if (SESS_LEADER(p))
197 		return (EPERM);
198 	if (uap->pgid == 0)
199 		uap->pgid = p->p_pid;
200 	else if (uap->pgid != p->p_pid)
201 		if ((pgrp = pgfind(uap->pgid)) == 0 ||
202 		    pgrp->pg_mem == NULL ||
203 	            pgrp->pg_session != cp->p_session)
204 			return (EPERM);
205 	pgmv(p, uap->pgid, 0);
206 	return (0);
207 }
208 
209 /* ARGSUSED */
210 setuid(p, uap, retval)
211 	register struct proc *p;
212 	struct args {
213 		int	uid;
214 	} *uap;
215 	int *retval;
216 {
217 	register uid_t uid;
218 	int error;
219 
220 	uid = uap->uid;
221 	if (uid != p->p_ruid && (error = suser(u.u_cred, &u.u_acflag)))
222 		return (error);
223 	/*
224 	 * Everything's okay, do it.  Copy credentials so other references do
225 	 * not see our changes.
226 	 */
227 	u.u_cred = crcopy(u.u_cred);
228 	u.u_cred->cr_uid = uid;
229 	p->p_uid = uid;
230 	p->p_ruid = uid;
231 	p->p_svuid = uid;
232 	return (0);
233 }
234 
235 /* ARGSUSED */
236 seteuid(p, uap, retval)
237 	register struct proc *p;
238 	struct args {
239 		int	euid;
240 	} *uap;
241 	int *retval;
242 {
243 	register uid_t euid;
244 	int error;
245 
246 	euid = uap->euid;
247 	if (euid != p->p_ruid && euid != p->p_svuid &&
248 	    (error = suser(u.u_cred, &u.u_acflag)))
249 		return (error);
250 	/*
251 	 * Everything's okay, do it.  Copy credentials so other references do
252 	 * not see our changes.
253 	 */
254 	u.u_cred = crcopy(u.u_cred);
255 	u.u_cred->cr_uid = euid;
256 	p->p_uid = euid;
257 	return (0);
258 }
259 
260 /* ARGSUSED */
261 setgid(p, uap, retval)
262 	struct proc *p;
263 	struct args {
264 		int	gid;
265 	} *uap;
266 	int *retval;
267 {
268 	register gid_t gid;
269 	int error;
270 
271 	gid = uap->gid;
272 	if (gid != p->p_rgid && (error = suser(u.u_cred, &u.u_acflag)))
273 		return (error);
274 	u.u_cred = crcopy(u.u_cred);
275 	p->p_rgid = gid;
276 	u.u_cred->cr_groups[0] = gid;
277 	p->p_svgid = gid;		/* ??? */
278 	return (0);
279 }
280 
281 /* ARGSUSED */
282 setegid(p, uap, retval)
283 	struct proc *p;
284 	struct args {
285 		int	egid;
286 	} *uap;
287 	int *retval;
288 {
289 	register gid_t egid;
290 	int error;
291 
292 	egid = uap->egid;
293 	if (egid != p->p_rgid && egid != p->p_svgid &&
294 	    (error = suser(u.u_cred, &u.u_acflag)))
295 		return (error);
296 	u.u_cred = crcopy(u.u_cred);
297 	u.u_cred->cr_groups[0] = egid;
298 	return (0);
299 }
300 
301 #ifdef COMPAT_43
302 /* ARGSUSED */
303 osetreuid(p, uap, retval)
304 	register struct proc *p;
305 	struct args {
306 		int	ruid;
307 		int	euid;
308 	} *uap;
309 	int *retval;
310 {
311 	register uid_t ruid, euid;
312 	int error;
313 
314 	if (uap->ruid == -1)
315 		ruid = p->p_ruid;
316 	else
317 		ruid = uap->ruid;
318 	/*
319 	 * Allow setting real uid to previous effective, for swapping real and
320 	 * effective.  This should be:
321 	 *
322 	 * if (ruid != p->p_ruid && (error = suser(u.u_cred, &u.u_acflag)))
323 	 */
324 	if (ruid != p->p_ruid && ruid != u.u_cred->cr_uid /* XXX */ &&
325 	    (error = suser(u.u_cred, &u.u_acflag)))
326 		return (error);
327 	if (uap->euid == -1)
328 		euid = u.u_cred->cr_uid;
329 	else
330 		euid = uap->euid;
331 	if (euid != u.u_cred->cr_uid && euid != p->p_ruid &&
332 	    euid != p->p_svuid && (error = suser(u.u_cred, &u.u_acflag)))
333 		return (error);
334 	/*
335 	 * Everything's okay, do it.  Copy credentials so other references do
336 	 * not see our changes.
337 	 */
338 	u.u_cred = crcopy(u.u_cred);
339 	u.u_cred->cr_uid = euid;
340 	p->p_uid = euid;
341 	p->p_ruid = ruid;
342 	return (0);
343 }
344 
345 /* ARGSUSED */
346 osetregid(p, uap, retval)
347 	struct proc *p;
348 	struct args {
349 		int	rgid;
350 		int	egid;
351 	} *uap;
352 	int *retval;
353 {
354 	register gid_t rgid, egid;
355 	int error;
356 
357 	if (uap->rgid == -1)
358 		rgid = p->p_rgid;
359 	else
360 		rgid = uap->rgid;
361 	/*
362 	 * Allow setting real gid to previous effective, for swapping real and
363 	 * effective.  This didn't really work correctly in 4.[23], but is
364 	 * preserved so old stuff doesn't fail.  This should be:
365 	 *
366 	 * if (rgid != p->p_rgid && (error = suser(u.u_cred, &u.u_acflag)))
367 	 */
368 	if (rgid != p->p_rgid && rgid != u.u_cred->cr_groups[0] /* XXX */ &&
369 	    (error = suser(u.u_cred, &u.u_acflag)))
370 		return (error);
371 	if (uap->egid == -1)
372 		egid = u.u_cred->cr_groups[0];
373 	else
374 		egid = uap->egid;
375 	if (egid != u.u_cred->cr_groups[0] && egid != p->p_rgid &&
376 	    egid != p->p_svgid && (error = suser(u.u_cred, &u.u_acflag)))
377 		return (error);
378 	u.u_cred = crcopy(u.u_cred);
379 	p->p_rgid = rgid;
380 	u.u_cred->cr_groups[0] = egid;
381 	return (0);
382 }
383 #endif
384 
385 /* ARGSUSED */
386 setgroups(p, uap, retval)
387 	struct proc *p;
388 	struct args {
389 		u_int	gidsetsize;
390 		int	*gidset;
391 	} *uap;
392 	int *retval;
393 {
394 	register gid_t *gp;
395 	register u_int ngrp;
396 	register int *lp;
397 	int error, groups[NGROUPS];
398 
399 	if (error = suser(u.u_cred, &u.u_acflag))
400 		return (error);
401 	if ((ngrp = uap->gidsetsize) > NGROUPS)
402 		return (EINVAL);
403 	if (error = copyin((caddr_t)uap->gidset, (caddr_t)groups,
404 	    ngrp * sizeof (groups[0])))
405 		return (error);
406 	u.u_cred = crcopy(u.u_cred);
407 	u.u_cred->cr_ngroups = ngrp;
408 	/* convert from int's to gid_t's */
409 	for (gp = u.u_cred->cr_groups, lp = groups; ngrp--; )
410 		*gp++ = *lp++;
411 	return (0);
412 }
413 
414 /*
415  * Check if gid is a member of the group set.
416  */
417 groupmember(gid, cred)
418 	gid_t gid;
419 	register struct ucred *cred;
420 {
421 	register gid_t *gp;
422 	gid_t *egp;
423 
424 	egp = &(cred->cr_groups[cred->cr_ngroups]);
425 	for (gp = cred->cr_groups; gp < egp; gp++)
426 		if (*gp == gid)
427 			return (1);
428 	return (0);
429 }
430 
431 /*
432  * Test whether the specified credentials imply "super-user"
433  * privilege; if so, and we have accounting info, set the flag
434  * indicating use of super-powers.
435  * Returns 0 or error.
436  */
437 suser(cred, acflag)
438 	struct ucred *cred;
439 	short *acflag;
440 {
441 	if (cred->cr_uid == 0) {
442 		if (acflag)
443 			*acflag |= ASU;
444 		return (0);
445 	}
446 	return (EPERM);
447 }
448 
449 /*
450  * Allocate a zeroed cred structure.
451  */
452 struct ucred *
453 crget()
454 {
455 	register struct ucred *cr;
456 
457 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
458 	bzero((caddr_t)cr, sizeof(*cr));
459 	cr->cr_ref = 1;
460 	return (cr);
461 }
462 
463 /*
464  * Free a cred structure.
465  * Throws away space when ref count gets to 0.
466  */
467 crfree(cr)
468 	struct ucred *cr;
469 {
470 	int s = splimp();
471 
472 	if (--cr->cr_ref != 0) {
473 		(void) splx(s);
474 		return;
475 	}
476 	FREE((caddr_t)cr, M_CRED);
477 	(void) splx(s);
478 }
479 
480 /*
481  * Copy cred structure to a new one and free the old one.
482  */
483 struct ucred *
484 crcopy(cr)
485 	struct ucred *cr;
486 {
487 	struct ucred *newcr;
488 
489 	if (cr->cr_ref == 1)
490 		return (cr);
491 	newcr = crget();
492 	*newcr = *cr;
493 	crfree(cr);
494 	newcr->cr_ref = 1;
495 	return (newcr);
496 }
497 
498 /*
499  * Dup cred struct to a new held one.
500  */
501 struct ucred *
502 crdup(cr)
503 	struct ucred *cr;
504 {
505 	struct ucred *newcr;
506 
507 	newcr = crget();
508 	*newcr = *cr;
509 	newcr->cr_ref = 1;
510 	return (newcr);
511 }
512 
513 /*
514  * Get login name, if available.
515  */
516 /* ARGSUSED */
517 getlogin(p, uap, retval)
518 	struct proc *p;
519 	struct args {
520 		char	*namebuf;
521 		u_int	namelen;
522 	} *uap;
523 	int *retval;
524 {
525 
526 	if (uap->namelen > sizeof (p->p_logname))
527 		uap->namelen = sizeof (p->p_logname);
528 	return (copyout((caddr_t)p->p_logname, (caddr_t)uap->namebuf,
529 	    uap->namelen));
530 }
531 
532 /*
533  * Set login name.
534  */
535 /* ARGSUSED */
536 setlogin(p, uap, retval)
537 	struct proc *p;
538 	struct args {
539 		char	*namebuf;
540 	} *uap;
541 	int *retval;
542 {
543 	int error;
544 
545 	if (error = suser(u.u_cred, &u.u_acflag))
546 		return (error);
547 	error = copyinstr((caddr_t)uap->namebuf, (caddr_t)p->p_logname,
548 	    sizeof (p->p_logname) - 1, (u_int *)0);
549 	if (error == ENAMETOOLONG)		/* name too long */
550 		error = EINVAL;
551 	return (error);
552 }
553