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