1 /*- 2 * Copyright (c) 1996 by 3 * Sean Eric Fagan <sef@kithrup.com> 4 * David Nugent <davidn@blaze.net.au> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, is permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice immediately at the beginning of the file, without modification, 12 * this list of conditions, and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. This work was done expressly for inclusion into FreeBSD. Other use 17 * is permitted provided this notation is included. 18 * 4. Absolutely no warranty of function or purpose is made by the authors. 19 * 5. Modifications may be freely made to this file providing the above 20 * conditions are met. 21 * 22 * High-level routines relating to use of the user capabilities database 23 */ 24 25 #include <sys/param.h> 26 #include <sys/cpuset.h> 27 #include <sys/mac.h> 28 #include <sys/resource.h> 29 #include <sys/rtprio.h> 30 #include <sys/stat.h> 31 #include <sys/time.h> 32 33 #include <ctype.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <login_cap.h> 38 #include <paths.h> 39 #include <pwd.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <syslog.h> 44 #include <unistd.h> 45 46 47 static struct login_res { 48 const char *what; 49 rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t); 50 int why; 51 } resources[] = { 52 { "cputime", login_getcaptime, RLIMIT_CPU }, 53 { "filesize", login_getcapsize, RLIMIT_FSIZE }, 54 { "datasize", login_getcapsize, RLIMIT_DATA }, 55 { "stacksize", login_getcapsize, RLIMIT_STACK }, 56 { "memoryuse", login_getcapsize, RLIMIT_RSS }, 57 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK }, 58 { "maxproc", login_getcapnum, RLIMIT_NPROC }, 59 { "openfiles", login_getcapnum, RLIMIT_NOFILE }, 60 { "coredumpsize", login_getcapsize, RLIMIT_CORE }, 61 { "sbsize", login_getcapsize, RLIMIT_SBSIZE }, 62 { "vmemoryuse", login_getcapsize, RLIMIT_VMEM }, 63 { "pseudoterminals", login_getcapnum, RLIMIT_NPTS }, 64 { "swapuse", login_getcapsize, RLIMIT_SWAP }, 65 { "kqueues", login_getcapsize, RLIMIT_KQUEUES }, 66 { "umtxp", login_getcapnum, RLIMIT_UMTXP }, 67 { NULL, 0, 0 } 68 }; 69 70 71 void 72 setclassresources(login_cap_t *lc) 73 { 74 struct login_res *lr; 75 76 if (lc == NULL) 77 return; 78 79 for (lr = resources; lr->what != NULL; ++lr) { 80 struct rlimit rlim; 81 82 /* 83 * The login.conf file can have <limit>, <limit>-max, and 84 * <limit>-cur entries. 85 * What we do is get the current current- and maximum- limits. 86 * Then, we try to get an entry for <limit> from the capability, 87 * using the current and max limits we just got as the 88 * default/error values. 89 * *Then*, we try looking for <limit>-cur and <limit>-max, 90 * again using the appropriate values as the default/error 91 * conditions. 92 */ 93 94 if (getrlimit(lr->why, &rlim) != 0) 95 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 96 else { 97 char name_cur[40]; 98 char name_max[40]; 99 rlim_t rcur = rlim.rlim_cur; 100 rlim_t rmax = rlim.rlim_max; 101 102 sprintf(name_cur, "%s-cur", lr->what); 103 sprintf(name_max, "%s-max", lr->what); 104 105 rcur = (*lr->who)(lc, lr->what, rcur, rcur); 106 rmax = (*lr->who)(lc, lr->what, rmax, rmax); 107 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 108 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 109 110 if (setrlimit(lr->why, &rlim) == -1) 111 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 112 } 113 } 114 } 115 116 117 118 static struct login_vars { 119 const char *tag; 120 const char *var; 121 const char *def; 122 int overwrite; 123 } pathvars[] = { 124 { "path", "PATH", NULL, 1}, 125 { "cdpath", "CDPATH", NULL, 1}, 126 { "manpath", "MANPATH", NULL, 1}, 127 { NULL, NULL, NULL, 0} 128 }, envars[] = { 129 { "lang", "LANG", NULL, 1}, 130 { "charset", "MM_CHARSET", NULL, 1}, 131 { "mail", "MAIL", NULL, 1}, 132 { "timezone", "TZ", NULL, 1}, 133 { "term", "TERM", NULL, 0}, 134 { NULL, NULL, NULL, 0} 135 }; 136 137 static char * 138 substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 139 { 140 char *np = NULL; 141 142 if (var != NULL) { 143 int tildes = 0; 144 int dollas = 0; 145 char *p; 146 const char *q; 147 148 if (pwd != NULL) { 149 for (q = var; *q != '\0'; ++q) { 150 tildes += (*q == '~'); 151 dollas += (*q == '$'); 152 } 153 } 154 155 np = malloc(strlen(var) + (dollas * nlen) 156 - dollas + (tildes * (pch+hlen)) 157 - tildes + 1); 158 159 if (np != NULL) { 160 p = strcpy(np, var); 161 162 if (pwd != NULL) { 163 /* 164 * This loop does user username and homedir substitutions 165 * for unescaped $ (username) and ~ (homedir) 166 */ 167 while (*(p += strcspn(p, "~$")) != '\0') { 168 int l = strlen(p); 169 170 if (p > np && *(p-1) == '\\') /* Escaped: */ 171 memmove(p - 1, p, l + 1); /* Slide-out the backslash */ 172 else if (*p == '~') { 173 int v = pch && *(p+1) != '/'; /* Avoid double // */ 174 memmove(p + hlen + v, p + 1, l); /* Subst homedir */ 175 memmove(p, pwd->pw_dir, hlen); 176 if (v) 177 p[hlen] = '/'; 178 p += hlen + v; 179 } 180 else /* if (*p == '$') */ { 181 memmove(p + nlen, p + 1, l); /* Subst username */ 182 memmove(p, pwd->pw_name, nlen); 183 p += nlen; 184 } 185 } 186 } 187 } 188 } 189 190 return (np); 191 } 192 193 194 void 195 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 196 { 197 struct login_vars *vars = paths ? pathvars : envars; 198 int hlen = pwd ? strlen(pwd->pw_dir) : 0; 199 int nlen = pwd ? strlen(pwd->pw_name) : 0; 200 char pch = 0; 201 202 if (hlen && pwd->pw_dir[hlen-1] != '/') 203 ++pch; 204 205 while (vars->tag != NULL) { 206 const char * var = paths ? login_getpath(lc, vars->tag, NULL) 207 : login_getcapstr(lc, vars->tag, NULL, NULL); 208 209 char * np = substvar(var, pwd, hlen, pch, nlen); 210 211 if (np != NULL) { 212 setenv(vars->var, np, vars->overwrite); 213 free(np); 214 } else if (vars->def != NULL) { 215 setenv(vars->var, vars->def, 0); 216 } 217 ++vars; 218 } 219 220 /* 221 * If we're not processing paths, then see if there is a setenv list by 222 * which the admin and/or user may set an arbitrary set of env vars. 223 */ 224 if (!paths) { 225 const char **set_env = login_getcaplist(lc, "setenv", ","); 226 227 if (set_env != NULL) { 228 while (*set_env != NULL) { 229 char *p = strchr(*set_env, '='); 230 231 if (p != NULL && p != *set_env) { /* Discard invalid entries */ 232 const char *ep; 233 char *np; 234 235 *p++ = '\0'; 236 /* Strip leading spaces from variable name */ 237 ep = *set_env; 238 while (*ep == ' ' || *ep == '\t') 239 ep++; 240 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 241 setenv(ep, np, 1); 242 free(np); 243 } 244 } 245 ++set_env; 246 } 247 } 248 } 249 } 250 251 252 static int 253 list2cpuset(const char *list, cpuset_t *mask) 254 { 255 enum { NONE, NUM, DASH } state; 256 int lastnum; 257 int curnum; 258 const char *l; 259 260 state = NONE; 261 curnum = lastnum = 0; 262 for (l = list; *l != '\0';) { 263 if (isdigit(*l)) { 264 curnum = atoi(l); 265 if (curnum > CPU_SETSIZE) 266 errx(EXIT_FAILURE, 267 "Only %d cpus supported", CPU_SETSIZE); 268 while (isdigit(*l)) 269 l++; 270 switch (state) { 271 case NONE: 272 lastnum = curnum; 273 state = NUM; 274 break; 275 case DASH: 276 for (; lastnum <= curnum; lastnum++) 277 CPU_SET(lastnum, mask); 278 state = NONE; 279 break; 280 case NUM: 281 default: 282 return (0); 283 } 284 continue; 285 } 286 switch (*l) { 287 case ',': 288 switch (state) { 289 case NONE: 290 break; 291 case NUM: 292 CPU_SET(curnum, mask); 293 state = NONE; 294 break; 295 case DASH: 296 return (0); 297 break; 298 } 299 break; 300 case '-': 301 if (state != NUM) 302 return (0); 303 state = DASH; 304 break; 305 default: 306 return (0); 307 } 308 l++; 309 } 310 switch (state) { 311 case NONE: 312 break; 313 case NUM: 314 CPU_SET(curnum, mask); 315 break; 316 case DASH: 317 return (0); 318 } 319 return (1); 320 } 321 322 323 void 324 setclasscpumask(login_cap_t *lc) 325 { 326 const char *maskstr; 327 cpuset_t maskset; 328 cpusetid_t setid; 329 330 maskstr = login_getcapstr(lc, "cpumask", NULL, NULL); 331 CPU_ZERO(&maskset); 332 if (maskstr == NULL) 333 return; 334 if (strcasecmp("default", maskstr) == 0) 335 return; 336 if (!list2cpuset(maskstr, &maskset)) { 337 syslog(LOG_WARNING, 338 "list2cpuset(%s) invalid mask specification", maskstr); 339 return; 340 } 341 342 if (cpuset(&setid) != 0) { 343 syslog(LOG_ERR, "cpuset(): %s", strerror(errno)); 344 return; 345 } 346 347 if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, 348 sizeof(maskset), &maskset) != 0) 349 syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr, 350 strerror(errno)); 351 } 352 353 354 /* 355 * setclasscontext() 356 * 357 * For the login class <class>, set various class context values 358 * (limits, mainly) to the values for that class. Which values are 359 * set are controlled by <flags> -- see <login_class.h> for the 360 * possible values. 361 * 362 * setclasscontext() can only set resources, priority, and umask. 363 */ 364 365 int 366 setclasscontext(const char *classname, unsigned int flags) 367 { 368 int rc; 369 login_cap_t *lc; 370 371 lc = login_getclassbyname(classname, NULL); 372 373 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 374 LOGIN_SETUMASK | LOGIN_SETPATH; 375 376 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 377 login_close(lc); 378 return (rc); 379 } 380 381 382 383 /* 384 * Private function which takes care of processing 385 */ 386 387 static mode_t 388 setlogincontext(login_cap_t *lc, const struct passwd *pwd, 389 mode_t mymask, unsigned long flags) 390 { 391 if (lc) { 392 /* Set resources */ 393 if (flags & LOGIN_SETRESOURCES) 394 setclassresources(lc); 395 /* See if there's a umask override */ 396 if (flags & LOGIN_SETUMASK) 397 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask); 398 /* Set paths */ 399 if (flags & LOGIN_SETPATH) 400 setclassenvironment(lc, pwd, 1); 401 /* Set environment */ 402 if (flags & LOGIN_SETENV) 403 setclassenvironment(lc, pwd, 0); 404 /* Set cpu affinity */ 405 if (flags & LOGIN_SETCPUMASK) 406 setclasscpumask(lc); 407 } 408 return (mymask); 409 } 410 411 412 413 /* 414 * setusercontext() 415 * 416 * Given a login class <lc> and a user in <pwd>, with a uid <uid>, 417 * set the context as in setclasscontext(). <flags> controls which 418 * values are set. 419 * 420 * The difference between setclasscontext() and setusercontext() is 421 * that the former sets things up for an already-existing process, 422 * while the latter sets things up from a root context. Such as might 423 * be called from login(1). 424 * 425 */ 426 427 int 428 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 429 { 430 rlim_t p; 431 mode_t mymask; 432 login_cap_t *llc = NULL; 433 struct rtprio rtp; 434 int error; 435 436 if (lc == NULL) { 437 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 438 llc = lc; /* free this when we're done */ 439 } 440 441 if (flags & LOGIN_SETPATH) 442 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 443 444 /* we need a passwd entry to set these */ 445 if (pwd == NULL) 446 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); 447 448 /* Set the process priority */ 449 if (flags & LOGIN_SETPRIORITY) { 450 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 451 452 if (p > PRIO_MAX) { 453 rtp.type = RTP_PRIO_IDLE; 454 p += RTP_PRIO_MIN - (PRIO_MAX + 1); 455 rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p; 456 if (rtprio(RTP_SET, 0, &rtp)) 457 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 458 pwd ? pwd->pw_name : "-", 459 lc ? lc->lc_class : LOGIN_DEFCLASS); 460 } else if (p < PRIO_MIN) { 461 rtp.type = RTP_PRIO_REALTIME; 462 p += RTP_PRIO_MAX - (PRIO_MIN - 1); 463 rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p; 464 if (rtprio(RTP_SET, 0, &rtp)) 465 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 466 pwd ? pwd->pw_name : "-", 467 lc ? lc->lc_class : LOGIN_DEFCLASS); 468 } else { 469 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 470 syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 471 pwd ? pwd->pw_name : "-", 472 lc ? lc->lc_class : LOGIN_DEFCLASS); 473 } 474 } 475 476 /* Setup the user's group permissions */ 477 if (flags & LOGIN_SETGROUP) { 478 if (setgid(pwd->pw_gid) != 0) { 479 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 480 login_close(llc); 481 return (-1); 482 } 483 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 484 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 485 (u_long)pwd->pw_gid); 486 login_close(llc); 487 return (-1); 488 } 489 } 490 491 /* Set up the user's MAC label. */ 492 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) { 493 const char *label_string; 494 mac_t label; 495 496 label_string = login_getcapstr(lc, "label", NULL, NULL); 497 if (label_string != NULL) { 498 if (mac_from_text(&label, label_string) == -1) { 499 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m", 500 pwd->pw_name, label_string); 501 return (-1); 502 } 503 if (mac_set_proc(label) == -1) 504 error = errno; 505 else 506 error = 0; 507 mac_free(label); 508 if (error != 0) { 509 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s", 510 label_string, pwd->pw_name, strerror(error)); 511 return (-1); 512 } 513 } 514 } 515 516 /* Set the sessions login */ 517 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 518 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 519 login_close(llc); 520 return (-1); 521 } 522 523 /* Inform the kernel about current login class */ 524 if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) { 525 error = setloginclass(lc->lc_class); 526 if (error != 0) { 527 syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class); 528 #ifdef notyet 529 login_close(llc); 530 return (-1); 531 #endif 532 } 533 } 534 535 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 536 mymask = setlogincontext(lc, pwd, mymask, flags); 537 login_close(llc); 538 539 /* This needs to be done after anything that needs root privs */ 540 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 541 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 542 return (-1); /* Paranoia again */ 543 } 544 545 /* 546 * Now, we repeat some of the above for the user's private entries 547 */ 548 if (geteuid() == uid && (lc = login_getuserclass(pwd)) != NULL) { 549 mymask = setlogincontext(lc, pwd, mymask, flags); 550 login_close(lc); 551 } 552 553 /* Finally, set any umask we've found */ 554 if (flags & LOGIN_SETUMASK) 555 umask(mymask); 556 557 return (0); 558 } 559