chpst.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. Copyright (c) 2001-2006, Gerrit Pape
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. The name of the author may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
  14. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  16. EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  21. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
  25. //config:config CHPST
  26. //config: bool "chpst (9.2 kb)"
  27. //config: default y
  28. //config: help
  29. //config: chpst changes the process state according to the given options, and
  30. //config: execs specified program.
  31. //config:
  32. //config:config SETUIDGID
  33. //config: bool "setuidgid (4.2 kb)"
  34. //config: default y
  35. //config: help
  36. //config: Sets soft resource limits as specified by options
  37. //config:
  38. //config:config ENVUIDGID
  39. //config: bool "envuidgid (4.1 kb)"
  40. //config: default y
  41. //config: help
  42. //config: Sets $UID to account's uid and $GID to account's gid
  43. //config:
  44. //config:config ENVDIR
  45. //config: bool "envdir (2.9 kb)"
  46. //config: default y
  47. //config: help
  48. //config: Sets various environment variables as specified by files
  49. //config: in the given directory
  50. //config:
  51. //config:config SOFTLIMIT
  52. //config: bool "softlimit (4.7 kb)"
  53. //config: default y
  54. //config: help
  55. //config: Sets soft resource limits as specified by options
  56. //applet:IF_CHPST( APPLET_NOEXEC(chpst, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, chpst))
  57. // APPLET_NOEXEC:name main location suid_type help
  58. //applet:IF_ENVDIR( APPLET_NOEXEC(envdir, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, envdir))
  59. //applet:IF_ENVUIDGID(APPLET_NOEXEC(envuidgid, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, envuidgid))
  60. //applet:IF_SETUIDGID(APPLET_NOEXEC(setuidgid, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, setuidgid))
  61. //applet:IF_SOFTLIMIT(APPLET_NOEXEC(softlimit, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, softlimit))
  62. //kbuild:lib-$(CONFIG_CHPST) += chpst.o
  63. //kbuild:lib-$(CONFIG_ENVDIR) += chpst.o
  64. //kbuild:lib-$(CONFIG_ENVUIDGID) += chpst.o
  65. //kbuild:lib-$(CONFIG_SETUIDGID) += chpst.o
  66. //kbuild:lib-$(CONFIG_SOFTLIMIT) += chpst.o
  67. //usage:#define chpst_trivial_usage
  68. //usage: "[-vP012] [-u USER[:GRP]] [-U USER[:GRP]] [-e DIR]\n"
  69. //usage: " [-/ DIR] [-n NICE] [-m BYTES] [-d BYTES] [-o N]\n"
  70. //usage: " [-p N] [-f BYTES] [-c BYTES] PROG ARGS"
  71. //usage:#define chpst_full_usage "\n\n"
  72. //usage: "Change the process state, run PROG\n"
  73. //usage: "\n -u USER[:GRP] Set uid and gid"
  74. //usage: "\n -U USER[:GRP] Set $UID and $GID in environment"
  75. //usage: "\n -e DIR Set environment variables as specified by files"
  76. //usage: "\n in DIR: file=1st_line_of_file"
  77. //usage: "\n -/ DIR Chroot to DIR"
  78. //usage: "\n -n NICE Add NICE to nice value"
  79. //usage: "\n -m BYTES Same as -d BYTES -s BYTES -l BYTES"
  80. //usage: "\n -d BYTES Limit data segment"
  81. //usage: "\n -o N Limit number of open files per process"
  82. //usage: "\n -p N Limit number of processes per uid"
  83. //usage: "\n -f BYTES Limit output file sizes"
  84. //usage: "\n -c BYTES Limit core file size"
  85. //usage: "\n -v Verbose"
  86. //usage: "\n -P Create new process group"
  87. //usage: "\n -0 Close stdin"
  88. //usage: "\n -1 Close stdout"
  89. //usage: "\n -2 Close stderr"
  90. //usage:
  91. //usage:#define envdir_trivial_usage
  92. //usage: "DIR PROG ARGS"
  93. //usage:#define envdir_full_usage "\n\n"
  94. //usage: "Set various environment variables as specified by files\n"
  95. //usage: "in the directory DIR, run PROG"
  96. //usage:
  97. //usage:#define envuidgid_trivial_usage
  98. //usage: "USER PROG ARGS"
  99. //usage:#define envuidgid_full_usage "\n\n"
  100. //usage: "Set $UID to USER's uid and $GID to USER's gid, run PROG"
  101. //usage:
  102. //usage:#define setuidgid_trivial_usage
  103. //usage: "USER PROG ARGS"
  104. //usage:#define setuidgid_full_usage "\n\n"
  105. //usage: "Set uid and gid to USER's uid and gid, drop supplementary group ids,\n"
  106. //usage: "run PROG"
  107. //usage:
  108. //usage:#define softlimit_trivial_usage
  109. //usage: "[-a BYTES] [-m BYTES] [-d BYTES] [-s BYTES] [-l BYTES]\n"
  110. //usage: " [-f BYTES] [-c BYTES] [-r BYTES] [-o N] [-p N] [-t N]\n"
  111. //usage: " PROG ARGS"
  112. //usage:#define softlimit_full_usage "\n\n"
  113. //usage: "Set soft resource limits, then run PROG\n"
  114. //usage: "\n -a BYTES Limit total size of all segments"
  115. //usage: "\n -m BYTES Same as -d BYTES -s BYTES -l BYTES -a BYTES"
  116. //usage: "\n -d BYTES Limit data segment"
  117. //usage: "\n -s BYTES Limit stack segment"
  118. //usage: "\n -l BYTES Limit locked memory size"
  119. //usage: "\n -o N Limit number of open files per process"
  120. //usage: "\n -p N Limit number of processes per uid"
  121. //usage: "\nOptions controlling file sizes:"
  122. //usage: "\n -f BYTES Limit output file sizes"
  123. //usage: "\n -c BYTES Limit core file size"
  124. //usage: "\nEfficiency opts:"
  125. //usage: "\n -r BYTES Limit resident set size"
  126. //usage: "\n -t N Limit CPU time, process receives"
  127. //usage: "\n a SIGXCPU after N seconds"
  128. #include "libbb.h"
  129. /*
  130. Five applets here: chpst, envdir, envuidgid, setuidgid, softlimit.
  131. Only softlimit and chpst are taking options:
  132. # common
  133. -o N Limit number of open files per process
  134. -p N Limit number of processes per uid
  135. -m BYTES Same as -d BYTES -s BYTES -l BYTES [-a BYTES]
  136. -d BYTES Limit data segment
  137. -f BYTES Limit output file sizes
  138. -c BYTES Limit core file size
  139. # softlimit
  140. -a BYTES Limit total size of all segments
  141. -s BYTES Limit stack segment
  142. -l BYTES Limit locked memory size
  143. -r BYTES Limit resident set size
  144. -t N Limit CPU time
  145. # chpst
  146. -u USER[:GRP] Set uid and gid
  147. -U USER[:GRP] Set $UID and $GID in environment
  148. -e DIR Set environment variables as specified by files in DIR
  149. -/ DIR Chroot to DIR
  150. -n NICE Add NICE to nice value
  151. -v Verbose
  152. -P Create new process group
  153. -0 -1 -2 Close fd 0,1,2
  154. Even though we accept all these options for both softlimit and chpst,
  155. they are not to be advertised on their help texts.
  156. We have enough problems with feature creep in other people's
  157. software, don't want to add our own.
  158. envdir, envuidgid, setuidgid take no options, but they reuse code which
  159. handles -e, -U and -u.
  160. */
  161. enum {
  162. OPT_a = (1 << 0) * ENABLE_SOFTLIMIT,
  163. OPT_c = (1 << 1) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  164. OPT_d = (1 << 2) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  165. OPT_f = (1 << 3) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  166. OPT_l = (1 << 4) * ENABLE_SOFTLIMIT,
  167. OPT_m = (1 << 5) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  168. OPT_o = (1 << 6) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  169. OPT_p = (1 << 7) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  170. OPT_r = (1 << 8) * ENABLE_SOFTLIMIT,
  171. OPT_s = (1 << 9) * ENABLE_SOFTLIMIT,
  172. OPT_t = (1 << 10) * ENABLE_SOFTLIMIT,
  173. OPT_u = (1 << 11) * (ENABLE_CHPST || ENABLE_SETUIDGID),
  174. OPT_U = (1 << 12) * (ENABLE_CHPST || ENABLE_ENVUIDGID),
  175. OPT_e = (1 << 13) * (ENABLE_CHPST || ENABLE_ENVDIR),
  176. OPT_root = (1 << 14) * ENABLE_CHPST,
  177. OPT_n = (1 << 15) * ENABLE_CHPST,
  178. OPT_v = (1 << 16) * ENABLE_CHPST,
  179. OPT_P = (1 << 17) * ENABLE_CHPST,
  180. OPT_0 = (1 << 18) * ENABLE_CHPST,
  181. OPT_1 = (1 << 19) * ENABLE_CHPST,
  182. OPT_2 = (1 << 20) * ENABLE_CHPST,
  183. };
  184. /* TODO: use recursive_action? */
  185. static NOINLINE void edir(const char *directory_name)
  186. {
  187. int wdir;
  188. DIR *dir;
  189. struct dirent *d;
  190. int fd;
  191. wdir = xopen(".", O_RDONLY | O_NDELAY);
  192. xchdir(directory_name);
  193. dir = xopendir(".");
  194. for (;;) {
  195. char buf[256];
  196. char *tail;
  197. int size;
  198. errno = 0;
  199. d = readdir(dir);
  200. if (!d) {
  201. if (errno)
  202. bb_perror_msg_and_die("readdir %s",
  203. directory_name);
  204. break;
  205. }
  206. if (d->d_name[0] == '.')
  207. continue;
  208. fd = open(d->d_name, O_RDONLY | O_NDELAY);
  209. if (fd < 0) {
  210. if ((errno == EISDIR) && directory_name) {
  211. if (option_mask32 & OPT_v)
  212. bb_perror_msg("warning: %s/%s is a directory",
  213. directory_name, d->d_name);
  214. continue;
  215. }
  216. bb_perror_msg_and_die("open %s/%s",
  217. directory_name, d->d_name);
  218. }
  219. size = full_read(fd, buf, sizeof(buf)-1);
  220. close(fd);
  221. if (size < 0)
  222. bb_perror_msg_and_die("read %s/%s",
  223. directory_name, d->d_name);
  224. if (size == 0) {
  225. unsetenv(d->d_name);
  226. continue;
  227. }
  228. buf[size] = '\n';
  229. tail = strchr(buf, '\n');
  230. /* skip trailing whitespace */
  231. while (1) {
  232. *tail = '\0';
  233. tail--;
  234. if (tail < buf || !isspace(*tail))
  235. break;
  236. }
  237. xsetenv(d->d_name, buf);
  238. }
  239. closedir(dir);
  240. xfchdir(wdir);
  241. close(wdir);
  242. }
  243. static void limit(int what, long l)
  244. {
  245. struct rlimit r;
  246. /* Never fails under Linux (except if you pass it bad arguments) */
  247. getrlimit(what, &r);
  248. if ((l < 0) || (l > r.rlim_max))
  249. r.rlim_cur = r.rlim_max;
  250. else
  251. r.rlim_cur = l;
  252. if (setrlimit(what, &r) == -1)
  253. bb_simple_perror_msg_and_die("setrlimit");
  254. }
  255. int chpst_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  256. int chpst_main(int argc UNUSED_PARAM, char **argv)
  257. {
  258. struct bb_uidgid_t ugid;
  259. char *set_user = set_user; /* for compiler */
  260. char *env_dir = env_dir;
  261. char *root;
  262. char *nicestr;
  263. unsigned limita;
  264. unsigned limitc;
  265. unsigned limitd;
  266. unsigned limitf;
  267. unsigned limitl;
  268. unsigned limitm;
  269. unsigned limito;
  270. unsigned limitp;
  271. unsigned limitr;
  272. unsigned limits;
  273. unsigned limitt;
  274. unsigned opt;
  275. if ((ENABLE_CHPST && applet_name[0] == 'c')
  276. || (ENABLE_SOFTLIMIT && applet_name[1] == 'o')
  277. ) {
  278. // FIXME: can we live with int-sized limits?
  279. // can we live with 40000 days?
  280. // if yes -> getopt converts strings to numbers for us
  281. opt = getopt32(argv, "^+"
  282. "a:+c:+d:+f:+l:+m:+o:+p:+r:+s:+t:+u:U:e:"
  283. IF_CHPST("/:n:vP012")
  284. "\0" "-1",
  285. &limita, &limitc, &limitd, &limitf, &limitl,
  286. &limitm, &limito, &limitp, &limitr, &limits, &limitt,
  287. &set_user, &set_user, &env_dir
  288. IF_CHPST(, &root, &nicestr));
  289. argv += optind;
  290. if (opt & OPT_m) { // -m means -asld
  291. limita = limits = limitl = limitd = limitm;
  292. opt |= (OPT_s | OPT_l | OPT_a | OPT_d);
  293. }
  294. } else {
  295. option_mask32 = opt = 0;
  296. argv++;
  297. if (!*argv)
  298. bb_show_usage();
  299. }
  300. // envdir?
  301. if (ENABLE_ENVDIR && applet_name[3] == 'd') {
  302. env_dir = *argv++;
  303. opt |= OPT_e;
  304. }
  305. // setuidgid?
  306. if (ENABLE_SETUIDGID && applet_name[1] == 'e') {
  307. set_user = *argv++;
  308. opt |= OPT_u;
  309. }
  310. // envuidgid?
  311. if (ENABLE_ENVUIDGID && applet_name[0] == 'e' && applet_name[3] == 'u') {
  312. set_user = *argv++;
  313. opt |= OPT_U;
  314. }
  315. // we must have PROG [ARGS]
  316. if (!*argv)
  317. bb_show_usage();
  318. // set limits
  319. if (opt & OPT_d) {
  320. #ifdef RLIMIT_DATA
  321. limit(RLIMIT_DATA, limitd);
  322. #else
  323. if (opt & OPT_v)
  324. bb_error_msg("system does not support RLIMIT_%s",
  325. "DATA");
  326. #endif
  327. }
  328. if (opt & OPT_s) {
  329. #ifdef RLIMIT_STACK
  330. limit(RLIMIT_STACK, limits);
  331. #else
  332. if (opt & OPT_v)
  333. bb_error_msg("system does not support RLIMIT_%s",
  334. "STACK");
  335. #endif
  336. }
  337. if (opt & OPT_l) {
  338. #ifdef RLIMIT_MEMLOCK
  339. limit(RLIMIT_MEMLOCK, limitl);
  340. #else
  341. if (opt & OPT_v)
  342. bb_error_msg("system does not support RLIMIT_%s",
  343. "MEMLOCK");
  344. #endif
  345. }
  346. if (opt & OPT_a) {
  347. #ifdef RLIMIT_VMEM
  348. limit(RLIMIT_VMEM, limita);
  349. #else
  350. #ifdef RLIMIT_AS
  351. limit(RLIMIT_AS, limita);
  352. #else
  353. if (opt & OPT_v)
  354. bb_error_msg("system does not support RLIMIT_%s",
  355. "VMEM");
  356. #endif
  357. #endif
  358. }
  359. if (opt & OPT_o) {
  360. #ifdef RLIMIT_NOFILE
  361. limit(RLIMIT_NOFILE, limito);
  362. #else
  363. #ifdef RLIMIT_OFILE
  364. limit(RLIMIT_OFILE, limito);
  365. #else
  366. if (opt & OPT_v)
  367. bb_error_msg("system does not support RLIMIT_%s",
  368. "NOFILE");
  369. #endif
  370. #endif
  371. }
  372. if (opt & OPT_p) {
  373. #ifdef RLIMIT_NPROC
  374. limit(RLIMIT_NPROC, limitp);
  375. #else
  376. if (opt & OPT_v)
  377. bb_error_msg("system does not support RLIMIT_%s",
  378. "NPROC");
  379. #endif
  380. }
  381. if (opt & OPT_f) {
  382. #ifdef RLIMIT_FSIZE
  383. limit(RLIMIT_FSIZE, limitf);
  384. #else
  385. if (opt & OPT_v)
  386. bb_error_msg("system does not support RLIMIT_%s",
  387. "FSIZE");
  388. #endif
  389. }
  390. if (opt & OPT_c) {
  391. #ifdef RLIMIT_CORE
  392. limit(RLIMIT_CORE, limitc);
  393. #else
  394. if (opt & OPT_v)
  395. bb_error_msg("system does not support RLIMIT_%s",
  396. "CORE");
  397. #endif
  398. }
  399. if (opt & OPT_r) {
  400. #ifdef RLIMIT_RSS
  401. limit(RLIMIT_RSS, limitr);
  402. #else
  403. if (opt & OPT_v)
  404. bb_error_msg("system does not support RLIMIT_%s",
  405. "RSS");
  406. #endif
  407. }
  408. if (opt & OPT_t) {
  409. #ifdef RLIMIT_CPU
  410. limit(RLIMIT_CPU, limitt);
  411. #else
  412. if (opt & OPT_v)
  413. bb_error_msg("system does not support RLIMIT_%s",
  414. "CPU");
  415. #endif
  416. }
  417. if (opt & OPT_P)
  418. setsid();
  419. if (opt & OPT_e)
  420. edir(env_dir);
  421. if (opt & (OPT_u|OPT_U))
  422. xget_uidgid(&ugid, set_user);
  423. // chrooted jail must have /etc/passwd if we move this after chroot.
  424. // OTOH chroot fails for non-roots.
  425. // Solution: cache uid/gid before chroot, apply uid/gid after.
  426. if (opt & OPT_U) {
  427. xsetenv("GID", utoa(ugid.gid));
  428. xsetenv("UID", utoa(ugid.uid));
  429. }
  430. if (opt & OPT_root) {
  431. xchroot(root);
  432. }
  433. /* nice should be done before xsetuid */
  434. if (opt & OPT_n) {
  435. errno = 0;
  436. if (nice(xatoi(nicestr)) == -1)
  437. bb_simple_perror_msg_and_die("nice");
  438. }
  439. if (opt & OPT_u) {
  440. if (setgroups(1, &ugid.gid) == -1)
  441. bb_simple_perror_msg_and_die("setgroups");
  442. xsetgid(ugid.gid);
  443. xsetuid(ugid.uid);
  444. }
  445. if (opt & OPT_0)
  446. close(STDIN_FILENO);
  447. if (opt & OPT_1)
  448. close(STDOUT_FILENO);
  449. if (opt & OPT_2)
  450. close(STDERR_FILENO);
  451. BB_EXECVP_or_die(argv);
  452. }