chpst.c 9.5 KB

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