3
0

chpst.c 12 KB

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