chpst.c 14 KB

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