3
0

chpst.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. /* Dependencies on runit_lib.c removed */
  26. //usage:#define chpst_trivial_usage
  27. //usage: "[-vP012] [-u USER[:GRP]] [-U USER[:GRP]] [-e DIR]\n"
  28. //usage: " [-/ DIR] [-n NICE] [-m BYTES] [-d BYTES] [-o N]\n"
  29. //usage: " [-p N] [-f BYTES] [-c BYTES] PROG ARGS"
  30. //usage:#define chpst_full_usage "\n\n"
  31. //usage: "Change the process state, run PROG\n"
  32. //usage: "\n -u USER[:GRP] Set uid and gid"
  33. //usage: "\n -U USER[:GRP] Set $UID and $GID in environment"
  34. //usage: "\n -e DIR Set environment variables as specified by files"
  35. //usage: "\n in DIR: file=1st_line_of_file"
  36. //usage: "\n -/ DIR Chroot to DIR"
  37. //usage: "\n -n NICE Add NICE to nice value"
  38. //usage: "\n -m BYTES Same as -d BYTES -s BYTES -l BYTES"
  39. //usage: "\n -d BYTES Limit data segment"
  40. //usage: "\n -o N Limit number of open files per process"
  41. //usage: "\n -p N Limit number of processes per uid"
  42. //usage: "\n -f BYTES Limit output file sizes"
  43. //usage: "\n -c BYTES Limit core file size"
  44. //usage: "\n -v Verbose"
  45. //usage: "\n -P Create new process group"
  46. //usage: "\n -0 Close stdin"
  47. //usage: "\n -1 Close stdout"
  48. //usage: "\n -2 Close stderr"
  49. //usage:
  50. //usage:#define envdir_trivial_usage
  51. //usage: "DIR PROG ARGS"
  52. //usage:#define envdir_full_usage "\n\n"
  53. //usage: "Set various environment variables as specified by files\n"
  54. //usage: "in the directory DIR, run PROG"
  55. //usage:
  56. //usage:#define envuidgid_trivial_usage
  57. //usage: "USER PROG ARGS"
  58. //usage:#define envuidgid_full_usage "\n\n"
  59. //usage: "Set $UID to USER's uid and $GID to USER's gid, run PROG"
  60. //usage:
  61. //usage:#define setuidgid_trivial_usage
  62. //usage: "USER PROG ARGS"
  63. //usage:#define setuidgid_full_usage "\n\n"
  64. //usage: "Set uid and gid to USER's uid and gid, drop supplementary group ids,\n"
  65. //usage: "run PROG"
  66. //usage:
  67. //usage:#define softlimit_trivial_usage
  68. //usage: "[-a BYTES] [-m BYTES] [-d BYTES] [-s BYTES] [-l BYTES]\n"
  69. //usage: " [-f BYTES] [-c BYTES] [-r BYTES] [-o N] [-p N] [-t N]\n"
  70. //usage: " PROG ARGS"
  71. //usage:#define softlimit_full_usage "\n\n"
  72. //usage: "Set soft resource limits, then run PROG\n"
  73. //usage: "\n -a BYTES Limit total size of all segments"
  74. //usage: "\n -m BYTES Same as -d BYTES -s BYTES -l BYTES -a BYTES"
  75. //usage: "\n -d BYTES Limit data segment"
  76. //usage: "\n -s BYTES Limit stack segment"
  77. //usage: "\n -l BYTES Limit locked memory size"
  78. //usage: "\n -o N Limit number of open files per process"
  79. //usage: "\n -p N Limit number of processes per uid"
  80. //usage: "\nOptions controlling file sizes:"
  81. //usage: "\n -f BYTES Limit output file sizes"
  82. //usage: "\n -c BYTES Limit core file size"
  83. //usage: "\nEfficiency opts:"
  84. //usage: "\n -r BYTES Limit resident set size"
  85. //usage: "\n -t N Limit CPU time, process receives"
  86. //usage: "\n a SIGXCPU after N seconds"
  87. #include "libbb.h"
  88. #include <sys/resource.h> /* getrlimit */
  89. /*
  90. Five applets here: chpst, envdir, envuidgid, setuidgid, softlimit.
  91. Only softlimit and chpst are taking options:
  92. # common
  93. -o N Limit number of open files per process
  94. -p N Limit number of processes per uid
  95. -m BYTES Same as -d BYTES -s BYTES -l BYTES [-a BYTES]
  96. -d BYTES Limit data segment
  97. -f BYTES Limit output file sizes
  98. -c BYTES Limit core file size
  99. # softlimit
  100. -a BYTES Limit total size of all segments
  101. -s BYTES Limit stack segment
  102. -l BYTES Limit locked memory size
  103. -r BYTES Limit resident set size
  104. -t N Limit CPU time
  105. # chpst
  106. -u USER[:GRP] Set uid and gid
  107. -U USER[:GRP] Set $UID and $GID in environment
  108. -e DIR Set environment variables as specified by files in DIR
  109. -/ DIR Chroot to DIR
  110. -n NICE Add NICE to nice value
  111. -v Verbose
  112. -P Create new process group
  113. -0 -1 -2 Close fd 0,1,2
  114. Even though we accept all these options for both softlimit and chpst,
  115. they are not to be advertised on their help texts.
  116. We have enough problems with feature creep in other people's
  117. software, don't want to add our own.
  118. envdir, envuidgid, setuidgid take no options, but they reuse code which
  119. handles -e, -U and -u.
  120. */
  121. enum {
  122. OPT_a = (1 << 0) * ENABLE_SOFTLIMIT,
  123. OPT_c = (1 << 1) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  124. OPT_d = (1 << 2) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  125. OPT_f = (1 << 3) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  126. OPT_l = (1 << 4) * ENABLE_SOFTLIMIT,
  127. OPT_m = (1 << 5) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  128. OPT_o = (1 << 6) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  129. OPT_p = (1 << 7) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
  130. OPT_r = (1 << 8) * ENABLE_SOFTLIMIT,
  131. OPT_s = (1 << 9) * ENABLE_SOFTLIMIT,
  132. OPT_t = (1 << 10) * ENABLE_SOFTLIMIT,
  133. OPT_u = (1 << 11) * (ENABLE_CHPST || ENABLE_SETUIDGID),
  134. OPT_U = (1 << 12) * (ENABLE_CHPST || ENABLE_ENVUIDGID),
  135. OPT_e = (1 << 13) * (ENABLE_CHPST || ENABLE_ENVDIR),
  136. OPT_root = (1 << 14) * ENABLE_CHPST,
  137. OPT_n = (1 << 15) * ENABLE_CHPST,
  138. OPT_v = (1 << 16) * ENABLE_CHPST,
  139. OPT_P = (1 << 17) * ENABLE_CHPST,
  140. OPT_0 = (1 << 18) * ENABLE_CHPST,
  141. OPT_1 = (1 << 19) * ENABLE_CHPST,
  142. OPT_2 = (1 << 20) * ENABLE_CHPST,
  143. };
  144. /* TODO: use recursive_action? */
  145. static NOINLINE void edir(const char *directory_name)
  146. {
  147. int wdir;
  148. DIR *dir;
  149. struct dirent *d;
  150. int fd;
  151. wdir = xopen(".", O_RDONLY | O_NDELAY);
  152. xchdir(directory_name);
  153. dir = xopendir(".");
  154. for (;;) {
  155. char buf[256];
  156. char *tail;
  157. int size;
  158. errno = 0;
  159. d = readdir(dir);
  160. if (!d) {
  161. if (errno)
  162. bb_perror_msg_and_die("readdir %s",
  163. directory_name);
  164. break;
  165. }
  166. if (d->d_name[0] == '.')
  167. continue;
  168. fd = open(d->d_name, O_RDONLY | O_NDELAY);
  169. if (fd < 0) {
  170. if ((errno == EISDIR) && directory_name) {
  171. if (option_mask32 & OPT_v)
  172. bb_perror_msg("warning: %s/%s is a directory",
  173. directory_name, d->d_name);
  174. continue;
  175. }
  176. bb_perror_msg_and_die("open %s/%s",
  177. directory_name, d->d_name);
  178. }
  179. size = full_read(fd, buf, sizeof(buf)-1);
  180. close(fd);
  181. if (size < 0)
  182. bb_perror_msg_and_die("read %s/%s",
  183. directory_name, d->d_name);
  184. if (size == 0) {
  185. unsetenv(d->d_name);
  186. continue;
  187. }
  188. buf[size] = '\n';
  189. tail = strchr(buf, '\n');
  190. /* skip trailing whitespace */
  191. while (1) {
  192. *tail = '\0';
  193. tail--;
  194. if (tail < buf || !isspace(*tail))
  195. break;
  196. }
  197. xsetenv(d->d_name, buf);
  198. }
  199. closedir(dir);
  200. if (fchdir(wdir) == -1)
  201. bb_perror_msg_and_die("fchdir");
  202. close(wdir);
  203. }
  204. static void limit(int what, long l)
  205. {
  206. struct rlimit r;
  207. /* Never fails under Linux (except if you pass it bad arguments) */
  208. getrlimit(what, &r);
  209. if ((l < 0) || (l > r.rlim_max))
  210. r.rlim_cur = r.rlim_max;
  211. else
  212. r.rlim_cur = l;
  213. if (setrlimit(what, &r) == -1)
  214. bb_perror_msg_and_die("setrlimit");
  215. }
  216. int chpst_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  217. int chpst_main(int argc UNUSED_PARAM, char **argv)
  218. {
  219. struct bb_uidgid_t ugid;
  220. char *set_user = set_user; /* for compiler */
  221. char *env_user = env_user;
  222. char *env_dir = env_dir;
  223. char *root;
  224. char *nicestr;
  225. unsigned limita;
  226. unsigned limitc;
  227. unsigned limitd;
  228. unsigned limitf;
  229. unsigned limitl;
  230. unsigned limitm;
  231. unsigned limito;
  232. unsigned limitp;
  233. unsigned limitr;
  234. unsigned limits;
  235. unsigned limitt;
  236. unsigned opt;
  237. if ((ENABLE_CHPST && applet_name[0] == 'c')
  238. || (ENABLE_SOFTLIMIT && applet_name[1] == 'o')
  239. ) {
  240. // FIXME: can we live with int-sized limits?
  241. // can we live with 40000 days?
  242. // if yes -> getopt converts strings to numbers for us
  243. opt_complementary = "-1:a+:c+:d+:f+:l+:m+:o+:p+:r+:s+:t+";
  244. opt = getopt32(argv, "+a:c:d:f:l:m:o:p:r:s:t:u:U:e:"
  245. IF_CHPST("/:n:vP012"),
  246. &limita, &limitc, &limitd, &limitf, &limitl,
  247. &limitm, &limito, &limitp, &limitr, &limits, &limitt,
  248. &set_user, &env_user, &env_dir
  249. IF_CHPST(, &root, &nicestr));
  250. argv += optind;
  251. if (opt & OPT_m) { // -m means -asld
  252. limita = limits = limitl = limitd = limitm;
  253. opt |= (OPT_s | OPT_l | OPT_a | OPT_d);
  254. }
  255. } else {
  256. option_mask32 = opt = 0;
  257. argv++;
  258. if (!*argv)
  259. bb_show_usage();
  260. }
  261. // envdir?
  262. if (ENABLE_ENVDIR && applet_name[3] == 'd') {
  263. env_dir = *argv++;
  264. opt |= OPT_e;
  265. }
  266. // setuidgid?
  267. if (ENABLE_SETUIDGID && applet_name[1] == 'e') {
  268. set_user = *argv++;
  269. opt |= OPT_u;
  270. }
  271. // envuidgid?
  272. if (ENABLE_ENVUIDGID && applet_name[0] == 'e' && applet_name[3] == 'u') {
  273. env_user = *argv++;
  274. opt |= OPT_U;
  275. }
  276. // we must have PROG [ARGS]
  277. if (!*argv)
  278. bb_show_usage();
  279. // set limits
  280. if (opt & OPT_d) {
  281. #ifdef RLIMIT_DATA
  282. limit(RLIMIT_DATA, limitd);
  283. #else
  284. if (opt & OPT_v)
  285. bb_error_msg("system does not support RLIMIT_%s",
  286. "DATA");
  287. #endif
  288. }
  289. if (opt & OPT_s) {
  290. #ifdef RLIMIT_STACK
  291. limit(RLIMIT_STACK, limits);
  292. #else
  293. if (opt & OPT_v)
  294. bb_error_msg("system does not support RLIMIT_%s",
  295. "STACK");
  296. #endif
  297. }
  298. if (opt & OPT_l) {
  299. #ifdef RLIMIT_MEMLOCK
  300. limit(RLIMIT_MEMLOCK, limitl);
  301. #else
  302. if (opt & OPT_v)
  303. bb_error_msg("system does not support RLIMIT_%s",
  304. "MEMLOCK");
  305. #endif
  306. }
  307. if (opt & OPT_a) {
  308. #ifdef RLIMIT_VMEM
  309. limit(RLIMIT_VMEM, limita);
  310. #else
  311. #ifdef RLIMIT_AS
  312. limit(RLIMIT_AS, limita);
  313. #else
  314. if (opt & OPT_v)
  315. bb_error_msg("system does not support RLIMIT_%s",
  316. "VMEM");
  317. #endif
  318. #endif
  319. }
  320. if (opt & OPT_o) {
  321. #ifdef RLIMIT_NOFILE
  322. limit(RLIMIT_NOFILE, limito);
  323. #else
  324. #ifdef RLIMIT_OFILE
  325. limit(RLIMIT_OFILE, limito);
  326. #else
  327. if (opt & OPT_v)
  328. bb_error_msg("system does not support RLIMIT_%s",
  329. "NOFILE");
  330. #endif
  331. #endif
  332. }
  333. if (opt & OPT_p) {
  334. #ifdef RLIMIT_NPROC
  335. limit(RLIMIT_NPROC, limitp);
  336. #else
  337. if (opt & OPT_v)
  338. bb_error_msg("system does not support RLIMIT_%s",
  339. "NPROC");
  340. #endif
  341. }
  342. if (opt & OPT_f) {
  343. #ifdef RLIMIT_FSIZE
  344. limit(RLIMIT_FSIZE, limitf);
  345. #else
  346. if (opt & OPT_v)
  347. bb_error_msg("system does not support RLIMIT_%s",
  348. "FSIZE");
  349. #endif
  350. }
  351. if (opt & OPT_c) {
  352. #ifdef RLIMIT_CORE
  353. limit(RLIMIT_CORE, limitc);
  354. #else
  355. if (opt & OPT_v)
  356. bb_error_msg("system does not support RLIMIT_%s",
  357. "CORE");
  358. #endif
  359. }
  360. if (opt & OPT_r) {
  361. #ifdef RLIMIT_RSS
  362. limit(RLIMIT_RSS, limitr);
  363. #else
  364. if (opt & OPT_v)
  365. bb_error_msg("system does not support RLIMIT_%s",
  366. "RSS");
  367. #endif
  368. }
  369. if (opt & OPT_t) {
  370. #ifdef RLIMIT_CPU
  371. limit(RLIMIT_CPU, limitt);
  372. #else
  373. if (opt & OPT_v)
  374. bb_error_msg("system does not support RLIMIT_%s",
  375. "CPU");
  376. #endif
  377. }
  378. if (opt & OPT_P)
  379. setsid();
  380. if (opt & OPT_e)
  381. edir(env_dir);
  382. if (opt & (OPT_u|OPT_U))
  383. xget_uidgid(&ugid, set_user);
  384. // chrooted jail must have /etc/passwd if we move this after chroot.
  385. // OTOH chroot fails for non-roots.
  386. // Solution: cache uid/gid before chroot, apply uid/gid after.
  387. if (opt & OPT_U) {
  388. xsetenv("GID", utoa(ugid.gid));
  389. xsetenv("UID", utoa(ugid.uid));
  390. }
  391. if (opt & OPT_root) {
  392. xchroot(root);
  393. }
  394. if (opt & OPT_u) {
  395. if (setgroups(1, &ugid.gid) == -1)
  396. bb_perror_msg_and_die("setgroups");
  397. xsetgid(ugid.gid);
  398. xsetuid(ugid.uid);
  399. }
  400. if (opt & OPT_n) {
  401. errno = 0;
  402. if (nice(xatoi(nicestr)) == -1)
  403. bb_perror_msg_and_die("nice");
  404. }
  405. if (opt & OPT_0)
  406. close(STDIN_FILENO);
  407. if (opt & OPT_1)
  408. close(STDOUT_FILENO);
  409. if (opt & OPT_2)
  410. close(STDERR_FILENO);
  411. BB_EXECVP_or_die(argv);
  412. }