chpst.c 9.6 KB

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