3
0

chpst.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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"
  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"
  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"
  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"
  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"
  53. //config: default y
  54. //config: help
  55. //config: Sets soft resource limits as specified by options
  56. //applet:IF_CHPST(APPLET(chpst, BB_DIR_USR_BIN, BB_SUID_DROP))
  57. //applet:IF_ENVDIR(APPLET_ODDNAME(envdir, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, envdir))
  58. //applet:IF_ENVUIDGID(APPLET_ODDNAME(envuidgid, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, envuidgid))
  59. //applet:IF_SETUIDGID(APPLET_ODDNAME(setuidgid, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, setuidgid))
  60. //applet:IF_SOFTLIMIT(APPLET_ODDNAME(softlimit, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, softlimit))
  61. //kbuild:lib-$(CONFIG_CHPST) += chpst.o
  62. //kbuild:lib-$(CONFIG_ENVDIR) += chpst.o
  63. //kbuild:lib-$(CONFIG_ENVUIDGID) += chpst.o
  64. //kbuild:lib-$(CONFIG_SETUIDGID) += chpst.o
  65. //kbuild:lib-$(CONFIG_SOFTLIMIT) += chpst.o
  66. //usage:#define chpst_trivial_usage
  67. //usage: "[-vP012] [-u USER[:GRP]] [-U USER[:GRP]] [-e DIR]\n"
  68. //usage: " [-/ DIR] [-n NICE] [-m BYTES] [-d BYTES] [-o N]\n"
  69. //usage: " [-p N] [-f BYTES] [-c BYTES] PROG ARGS"
  70. //usage:#define chpst_full_usage "\n\n"
  71. //usage: "Change the process state, run PROG\n"
  72. //usage: "\n -u USER[:GRP] Set uid and gid"
  73. //usage: "\n -U USER[:GRP] Set $UID and $GID in environment"
  74. //usage: "\n -e DIR Set environment variables as specified by files"
  75. //usage: "\n in DIR: file=1st_line_of_file"
  76. //usage: "\n -/ DIR Chroot to DIR"
  77. //usage: "\n -n NICE Add NICE to nice value"
  78. //usage: "\n -m BYTES Same as -d BYTES -s BYTES -l BYTES"
  79. //usage: "\n -d BYTES Limit data segment"
  80. //usage: "\n -o N Limit number of open files per process"
  81. //usage: "\n -p N Limit number of processes per uid"
  82. //usage: "\n -f BYTES Limit output file sizes"
  83. //usage: "\n -c BYTES Limit core file size"
  84. //usage: "\n -v Verbose"
  85. //usage: "\n -P Create new process group"
  86. //usage: "\n -0 Close stdin"
  87. //usage: "\n -1 Close stdout"
  88. //usage: "\n -2 Close stderr"
  89. //usage:
  90. //usage:#define envdir_trivial_usage
  91. //usage: "DIR PROG ARGS"
  92. //usage:#define envdir_full_usage "\n\n"
  93. //usage: "Set various environment variables as specified by files\n"
  94. //usage: "in the directory DIR, run PROG"
  95. //usage:
  96. //usage:#define envuidgid_trivial_usage
  97. //usage: "USER PROG ARGS"
  98. //usage:#define envuidgid_full_usage "\n\n"
  99. //usage: "Set $UID to USER's uid and $GID to USER's gid, run PROG"
  100. //usage:
  101. //usage:#define setuidgid_trivial_usage
  102. //usage: "USER PROG ARGS"
  103. //usage:#define setuidgid_full_usage "\n\n"
  104. //usage: "Set uid and gid to USER's uid and gid, drop supplementary group ids,\n"
  105. //usage: "run PROG"
  106. //usage:
  107. //usage:#define softlimit_trivial_usage
  108. //usage: "[-a BYTES] [-m BYTES] [-d BYTES] [-s BYTES] [-l BYTES]\n"
  109. //usage: " [-f BYTES] [-c BYTES] [-r BYTES] [-o N] [-p N] [-t N]\n"
  110. //usage: " PROG ARGS"
  111. //usage:#define softlimit_full_usage "\n\n"
  112. //usage: "Set soft resource limits, then run PROG\n"
  113. //usage: "\n -a BYTES Limit total size of all segments"
  114. //usage: "\n -m BYTES Same as -d BYTES -s BYTES -l BYTES -a BYTES"
  115. //usage: "\n -d BYTES Limit data segment"
  116. //usage: "\n -s BYTES Limit stack segment"
  117. //usage: "\n -l BYTES Limit locked memory size"
  118. //usage: "\n -o N Limit number of open files per process"
  119. //usage: "\n -p N Limit number of processes per uid"
  120. //usage: "\nOptions controlling file sizes:"
  121. //usage: "\n -f BYTES Limit output file sizes"
  122. //usage: "\n -c BYTES Limit core file size"
  123. //usage: "\nEfficiency opts:"
  124. //usage: "\n -r BYTES Limit resident set size"
  125. //usage: "\n -t N Limit CPU time, process receives"
  126. //usage: "\n a SIGXCPU after N seconds"
  127. #include "libbb.h"
  128. #include <sys/resource.h> /* getrlimit */
  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_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_complementary = "-1:a+:c+:d+:f+:l+:m+:o+:p+:r+:s+:t+";
  282. opt = getopt32(argv, "+a:c:d:f:l:m:o:p:r:s:t:u:U:e:"
  283. IF_CHPST("/:n:vP012"),
  284. &limita, &limitc, &limitd, &limitf, &limitl,
  285. &limitm, &limito, &limitp, &limitr, &limits, &limitt,
  286. &set_user, &set_user, &env_dir
  287. IF_CHPST(, &root, &nicestr));
  288. argv += optind;
  289. if (opt & OPT_m) { // -m means -asld
  290. limita = limits = limitl = limitd = limitm;
  291. opt |= (OPT_s | OPT_l | OPT_a | OPT_d);
  292. }
  293. } else {
  294. option_mask32 = opt = 0;
  295. argv++;
  296. if (!*argv)
  297. bb_show_usage();
  298. }
  299. // envdir?
  300. if (ENABLE_ENVDIR && applet_name[3] == 'd') {
  301. env_dir = *argv++;
  302. opt |= OPT_e;
  303. }
  304. // setuidgid?
  305. if (ENABLE_SETUIDGID && applet_name[1] == 'e') {
  306. set_user = *argv++;
  307. opt |= OPT_u;
  308. }
  309. // envuidgid?
  310. if (ENABLE_ENVUIDGID && applet_name[0] == 'e' && applet_name[3] == 'u') {
  311. set_user = *argv++;
  312. opt |= OPT_U;
  313. }
  314. // we must have PROG [ARGS]
  315. if (!*argv)
  316. bb_show_usage();
  317. // set limits
  318. if (opt & OPT_d) {
  319. #ifdef RLIMIT_DATA
  320. limit(RLIMIT_DATA, limitd);
  321. #else
  322. if (opt & OPT_v)
  323. bb_error_msg("system does not support RLIMIT_%s",
  324. "DATA");
  325. #endif
  326. }
  327. if (opt & OPT_s) {
  328. #ifdef RLIMIT_STACK
  329. limit(RLIMIT_STACK, limits);
  330. #else
  331. if (opt & OPT_v)
  332. bb_error_msg("system does not support RLIMIT_%s",
  333. "STACK");
  334. #endif
  335. }
  336. if (opt & OPT_l) {
  337. #ifdef RLIMIT_MEMLOCK
  338. limit(RLIMIT_MEMLOCK, limitl);
  339. #else
  340. if (opt & OPT_v)
  341. bb_error_msg("system does not support RLIMIT_%s",
  342. "MEMLOCK");
  343. #endif
  344. }
  345. if (opt & OPT_a) {
  346. #ifdef RLIMIT_VMEM
  347. limit(RLIMIT_VMEM, limita);
  348. #else
  349. #ifdef RLIMIT_AS
  350. limit(RLIMIT_AS, limita);
  351. #else
  352. if (opt & OPT_v)
  353. bb_error_msg("system does not support RLIMIT_%s",
  354. "VMEM");
  355. #endif
  356. #endif
  357. }
  358. if (opt & OPT_o) {
  359. #ifdef RLIMIT_NOFILE
  360. limit(RLIMIT_NOFILE, limito);
  361. #else
  362. #ifdef RLIMIT_OFILE
  363. limit(RLIMIT_OFILE, limito);
  364. #else
  365. if (opt & OPT_v)
  366. bb_error_msg("system does not support RLIMIT_%s",
  367. "NOFILE");
  368. #endif
  369. #endif
  370. }
  371. if (opt & OPT_p) {
  372. #ifdef RLIMIT_NPROC
  373. limit(RLIMIT_NPROC, limitp);
  374. #else
  375. if (opt & OPT_v)
  376. bb_error_msg("system does not support RLIMIT_%s",
  377. "NPROC");
  378. #endif
  379. }
  380. if (opt & OPT_f) {
  381. #ifdef RLIMIT_FSIZE
  382. limit(RLIMIT_FSIZE, limitf);
  383. #else
  384. if (opt & OPT_v)
  385. bb_error_msg("system does not support RLIMIT_%s",
  386. "FSIZE");
  387. #endif
  388. }
  389. if (opt & OPT_c) {
  390. #ifdef RLIMIT_CORE
  391. limit(RLIMIT_CORE, limitc);
  392. #else
  393. if (opt & OPT_v)
  394. bb_error_msg("system does not support RLIMIT_%s",
  395. "CORE");
  396. #endif
  397. }
  398. if (opt & OPT_r) {
  399. #ifdef RLIMIT_RSS
  400. limit(RLIMIT_RSS, limitr);
  401. #else
  402. if (opt & OPT_v)
  403. bb_error_msg("system does not support RLIMIT_%s",
  404. "RSS");
  405. #endif
  406. }
  407. if (opt & OPT_t) {
  408. #ifdef RLIMIT_CPU
  409. limit(RLIMIT_CPU, limitt);
  410. #else
  411. if (opt & OPT_v)
  412. bb_error_msg("system does not support RLIMIT_%s",
  413. "CPU");
  414. #endif
  415. }
  416. if (opt & OPT_P)
  417. setsid();
  418. if (opt & OPT_e)
  419. edir(env_dir);
  420. if (opt & (OPT_u|OPT_U))
  421. xget_uidgid(&ugid, set_user);
  422. // chrooted jail must have /etc/passwd if we move this after chroot.
  423. // OTOH chroot fails for non-roots.
  424. // Solution: cache uid/gid before chroot, apply uid/gid after.
  425. if (opt & OPT_U) {
  426. xsetenv("GID", utoa(ugid.gid));
  427. xsetenv("UID", utoa(ugid.uid));
  428. }
  429. if (opt & OPT_root) {
  430. xchroot(root);
  431. }
  432. if (opt & OPT_u) {
  433. if (setgroups(1, &ugid.gid) == -1)
  434. bb_perror_msg_and_die("setgroups");
  435. xsetgid(ugid.gid);
  436. xsetuid(ugid.uid);
  437. }
  438. if (opt & OPT_n) {
  439. errno = 0;
  440. if (nice(xatoi(nicestr)) == -1)
  441. bb_perror_msg_and_die("nice");
  442. }
  443. if (opt & OPT_0)
  444. close(STDIN_FILENO);
  445. if (opt & OPT_1)
  446. close(STDOUT_FILENO);
  447. if (opt & OPT_2)
  448. close(STDERR_FILENO);
  449. BB_EXECVP_or_die(argv);
  450. }