chpst.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. static 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 = opendir(".");
  92. if (!dir)
  93. bb_perror_msg_and_die("opendir %s", directory_name);
  94. for (;;) {
  95. char buf[256];
  96. char *tail;
  97. int size;
  98. errno = 0;
  99. d = readdir(dir);
  100. if (!d) {
  101. if (errno)
  102. bb_perror_msg_and_die("readdir %s",
  103. directory_name);
  104. break;
  105. }
  106. if (d->d_name[0] == '.')
  107. continue;
  108. fd = open(d->d_name, O_RDONLY | O_NDELAY);
  109. if (fd < 0) {
  110. if ((errno == EISDIR) && directory_name) {
  111. if (option_mask32 & OPT_v)
  112. bb_perror_msg("warning: %s/%s is a directory",
  113. directory_name, d->d_name);
  114. continue;
  115. } else
  116. bb_perror_msg_and_die("open %s/%s",
  117. directory_name, d->d_name);
  118. }
  119. size = full_read(fd, buf, sizeof(buf)-1);
  120. close(fd);
  121. if (size < 0)
  122. bb_perror_msg_and_die("read %s/%s",
  123. directory_name, d->d_name);
  124. if (size == 0) {
  125. unsetenv(d->d_name);
  126. continue;
  127. }
  128. buf[size] = '\n';
  129. tail = strchr(buf, '\n');
  130. /* skip trailing whitespace */
  131. while (1) {
  132. *tail = '\0';
  133. tail--;
  134. if (tail < buf || !isspace(*tail))
  135. break;
  136. }
  137. xsetenv(d->d_name, buf);
  138. }
  139. closedir(dir);
  140. if (fchdir(wdir) == -1)
  141. bb_perror_msg_and_die("fchdir");
  142. close(wdir);
  143. }
  144. static void limit(int what, long l)
  145. {
  146. struct rlimit r;
  147. /* Never fails under Linux (except if you pass it bad arguments) */
  148. getrlimit(what, &r);
  149. if ((l < 0) || (l > r.rlim_max))
  150. r.rlim_cur = r.rlim_max;
  151. else
  152. r.rlim_cur = l;
  153. if (setrlimit(what, &r) == -1)
  154. bb_perror_msg_and_die("setrlimit");
  155. }
  156. int chpst_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  157. int chpst_main(int argc UNUSED_PARAM, char **argv)
  158. {
  159. struct bb_uidgid_t ugid;
  160. char *set_user = set_user; /* for compiler */
  161. char *env_user = env_user;
  162. char *env_dir = env_dir;
  163. char *root;
  164. char *nicestr;
  165. unsigned limita;
  166. unsigned limitc;
  167. unsigned limitd;
  168. unsigned limitf;
  169. unsigned limitl;
  170. unsigned limitm;
  171. unsigned limito;
  172. unsigned limitp;
  173. unsigned limitr;
  174. unsigned limits;
  175. unsigned limitt;
  176. unsigned opt;
  177. if ((ENABLE_CHPST && applet_name[0] == 'c')
  178. || (ENABLE_SOFTLIMIT && applet_name[1] == 'o')
  179. ) {
  180. // FIXME: can we live with int-sized limits?
  181. // can we live with 40000 days?
  182. // if yes -> getopt converts strings to numbers for us
  183. opt_complementary = "-1:a+:c+:d+:f+:l+:m+:o+:p+:r+:s+:t+";
  184. opt = getopt32(argv, "+a:c:d:f:l:m:o:p:r:s:t:u:U:e:"
  185. USE_CHPST("/:n:vP012"),
  186. &limita, &limitc, &limitd, &limitf, &limitl,
  187. &limitm, &limito, &limitp, &limitr, &limits, &limitt,
  188. &set_user, &env_user, &env_dir
  189. USE_CHPST(, &root, &nicestr));
  190. argv += optind;
  191. if (opt & OPT_m) { // -m means -asld
  192. limita = limits = limitl = limitd = limitm;
  193. opt |= (OPT_s | OPT_l | OPT_a | OPT_d);
  194. }
  195. } else {
  196. option_mask32 = opt = 0;
  197. argv++;
  198. if (!*argv)
  199. bb_show_usage();
  200. }
  201. // envdir?
  202. if (ENABLE_ENVDIR && applet_name[3] == 'd') {
  203. env_dir = *argv++;
  204. opt |= OPT_e;
  205. }
  206. // setuidgid?
  207. if (ENABLE_SETUIDGID && applet_name[1] == 'e') {
  208. set_user = *argv++;
  209. opt |= OPT_u;
  210. }
  211. // envuidgid?
  212. if (ENABLE_ENVUIDGID && applet_name[0] == 'e' && applet_name[3] == 'u') {
  213. env_user = *argv++;
  214. opt |= OPT_U;
  215. }
  216. // we must have PROG [ARGS]
  217. if (!*argv)
  218. bb_show_usage();
  219. // set limits
  220. if (opt & OPT_d) {
  221. #ifdef RLIMIT_DATA
  222. limit(RLIMIT_DATA, limitd);
  223. #else
  224. if (opt & OPT_v)
  225. bb_error_msg("system does not support RLIMIT_%s",
  226. "DATA");
  227. #endif
  228. }
  229. if (opt & OPT_s) {
  230. #ifdef RLIMIT_STACK
  231. limit(RLIMIT_STACK, limits);
  232. #else
  233. if (opt & OPT_v)
  234. bb_error_msg("system does not support RLIMIT_%s",
  235. "STACK");
  236. #endif
  237. }
  238. if (opt & OPT_l) {
  239. #ifdef RLIMIT_MEMLOCK
  240. limit(RLIMIT_MEMLOCK, limitl);
  241. #else
  242. if (opt & OPT_v)
  243. bb_error_msg("system does not support RLIMIT_%s",
  244. "MEMLOCK");
  245. #endif
  246. }
  247. if (opt & OPT_a) {
  248. #ifdef RLIMIT_VMEM
  249. limit(RLIMIT_VMEM, limita);
  250. #else
  251. #ifdef RLIMIT_AS
  252. limit(RLIMIT_AS, limita);
  253. #else
  254. if (opt & OPT_v)
  255. bb_error_msg("system does not support RLIMIT_%s",
  256. "VMEM");
  257. #endif
  258. #endif
  259. }
  260. if (opt & OPT_o) {
  261. #ifdef RLIMIT_NOFILE
  262. limit(RLIMIT_NOFILE, limito);
  263. #else
  264. #ifdef RLIMIT_OFILE
  265. limit(RLIMIT_OFILE, limito);
  266. #else
  267. if (opt & OPT_v)
  268. bb_error_msg("system does not support RLIMIT_%s",
  269. "NOFILE");
  270. #endif
  271. #endif
  272. }
  273. if (opt & OPT_p) {
  274. #ifdef RLIMIT_NPROC
  275. limit(RLIMIT_NPROC, limitp);
  276. #else
  277. if (opt & OPT_v)
  278. bb_error_msg("system does not support RLIMIT_%s",
  279. "NPROC");
  280. #endif
  281. }
  282. if (opt & OPT_f) {
  283. #ifdef RLIMIT_FSIZE
  284. limit(RLIMIT_FSIZE, limitf);
  285. #else
  286. if (opt & OPT_v)
  287. bb_error_msg("system does not support RLIMIT_%s",
  288. "FSIZE");
  289. #endif
  290. }
  291. if (opt & OPT_c) {
  292. #ifdef RLIMIT_CORE
  293. limit(RLIMIT_CORE, limitc);
  294. #else
  295. if (opt & OPT_v)
  296. bb_error_msg("system does not support RLIMIT_%s",
  297. "CORE");
  298. #endif
  299. }
  300. if (opt & OPT_r) {
  301. #ifdef RLIMIT_RSS
  302. limit(RLIMIT_RSS, limitr);
  303. #else
  304. if (opt & OPT_v)
  305. bb_error_msg("system does not support RLIMIT_%s",
  306. "RSS");
  307. #endif
  308. }
  309. if (opt & OPT_t) {
  310. #ifdef RLIMIT_CPU
  311. limit(RLIMIT_CPU, limitt);
  312. #else
  313. if (opt & OPT_v)
  314. bb_error_msg("system does not support RLIMIT_%s",
  315. "CPU");
  316. #endif
  317. }
  318. if (opt & OPT_P)
  319. setsid();
  320. if (opt & OPT_e)
  321. edir(env_dir);
  322. // FIXME: chrooted jail must have /etc/passwd if we move this after chroot!
  323. // OTOH chroot fails for non-roots!
  324. // SOLUTION: cache uid/gid before chroot, apply uid/gid after
  325. if (opt & OPT_U) {
  326. xget_uidgid(&ugid, env_user);
  327. xsetenv("GID", utoa(ugid.gid));
  328. xsetenv("UID", utoa(ugid.uid));
  329. }
  330. if (opt & OPT_u) {
  331. xget_uidgid(&ugid, set_user);
  332. }
  333. if (opt & OPT_root) {
  334. xchdir(root);
  335. xchroot(".");
  336. }
  337. if (opt & OPT_u) {
  338. if (setgroups(1, &ugid.gid) == -1)
  339. bb_perror_msg_and_die("setgroups");
  340. xsetgid(ugid.gid);
  341. xsetuid(ugid.uid);
  342. }
  343. if (opt & OPT_n) {
  344. errno = 0;
  345. if (nice(xatoi(nicestr)) == -1)
  346. bb_perror_msg_and_die("nice");
  347. }
  348. if (opt & OPT_0)
  349. close(STDIN_FILENO);
  350. if (opt & OPT_1)
  351. close(STDOUT_FILENO);
  352. if (opt & OPT_2)
  353. close(STDERR_FILENO);
  354. BB_EXECVP(argv[0], argv);
  355. bb_perror_msg_and_die("exec %s", argv[0]);
  356. }