setpriv.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * setpriv implementation for busybox based on linux-utils-ng 2.29
  4. *
  5. * Copyright (C) 2017 by <assafgordon@gmail.com>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config SETPRIV
  10. //config: bool "setpriv (6.6 kb)"
  11. //config: default y
  12. //config: select PLATFORM_LINUX
  13. //config: select LONG_OPTS
  14. //config: help
  15. //config: Run a program with different Linux privilege settings.
  16. //config: Requires kernel >= 3.5
  17. //config:
  18. //config:config FEATURE_SETPRIV_DUMP
  19. //config: bool "Support dumping current privilege state"
  20. //config: default y
  21. //config: depends on SETPRIV
  22. //config: help
  23. //config: Enables the "--dump" switch to print out the current privilege
  24. //config: state. This is helpful for diagnosing problems.
  25. //config:
  26. //config:config FEATURE_SETPRIV_CAPABILITIES
  27. //config: bool "Support capabilities"
  28. //config: default y
  29. //config: depends on SETPRIV
  30. //config: help
  31. //config: Capabilities can be used to grant processes additional rights
  32. //config: without the necessity to always execute as the root user.
  33. //config: Enabling this option enables "--dump" to show information on
  34. //config: capabilities.
  35. //config:
  36. //config:config FEATURE_SETPRIV_CAPABILITY_NAMES
  37. //config: bool "Support capability names"
  38. //config: default y
  39. //config: depends on SETPRIV && FEATURE_SETPRIV_CAPABILITIES
  40. //config: help
  41. //config: Capabilities can be either referenced via a human-readble name,
  42. //config: e.g. "net_admin", or using their index, e.g. "cap_12". Enabling
  43. //config: this option allows using the human-readable names in addition to
  44. //config: the index-based names.
  45. //applet:IF_SETPRIV(APPLET(setpriv, BB_DIR_BIN, BB_SUID_DROP))
  46. //kbuild:lib-$(CONFIG_SETPRIV) += setpriv.o
  47. //usage:#define setpriv_trivial_usage
  48. //usage: "[OPTIONS] PROG [ARGS]"
  49. //usage:#define setpriv_full_usage "\n\n"
  50. //usage: "Run PROG with different privilege settings\n"
  51. //usage: IF_FEATURE_SETPRIV_DUMP(
  52. //usage: "\n-d,--dump Show current capabilities"
  53. //usage: )
  54. //usage: "\n--nnp,--no-new-privs Ignore setuid/setgid bits and file capabilities"
  55. //usage: IF_FEATURE_SETPRIV_CAPABILITIES(
  56. //usage: "\n--inh-caps CAP,CAP Set inheritable capabilities"
  57. //usage: "\n--ambient-caps CAP,CAP Set ambient capabilities"
  58. //usage: )
  59. //setpriv from util-linux 2.28:
  60. // -d, --dump show current state (and do not exec anything)
  61. // --nnp, --no-new-privs disallow granting new privileges
  62. // --inh-caps <caps,...> set inheritable capabilities
  63. // --bounding-set <caps> set capability bounding set
  64. // --ruid <uid> set real uid
  65. // --euid <uid> set effective uid
  66. // --rgid <gid> set real gid
  67. // --egid <gid> set effective gid
  68. // --reuid <uid> set real and effective uid
  69. // --regid <gid> set real and effective gid
  70. // --clear-groups clear supplementary groups
  71. // --keep-groups keep supplementary groups
  72. // --groups <group,...> set supplementary groups
  73. // --securebits <bits> set securebits
  74. // --selinux-label <label> set SELinux label
  75. // --apparmor-profile <pr> set AppArmor profile
  76. #if ENABLE_FEATURE_SETPRIV_CAPABILITIES
  77. #include <linux/capability.h>
  78. // #include <sys/capability.h>
  79. // This header is in libcap, but the functions are in libc.
  80. // Comment in the header says this above capset/capget:
  81. /* system calls - look to libc for function to system call mapping */
  82. extern int capset(cap_user_header_t header, cap_user_data_t data);
  83. extern int capget(cap_user_header_t header, const cap_user_data_t data);
  84. // so for bbox, let's just repeat the declarations.
  85. // This way, libcap needs not be installed in build environment.
  86. #endif
  87. #include <sys/prctl.h>
  88. #include "libbb.h"
  89. #ifndef PR_CAPBSET_READ
  90. #define PR_CAPBSET_READ 23
  91. #endif
  92. #ifndef PR_SET_NO_NEW_PRIVS
  93. #define PR_SET_NO_NEW_PRIVS 38
  94. #endif
  95. #ifndef PR_GET_NO_NEW_PRIVS
  96. #define PR_GET_NO_NEW_PRIVS 39
  97. #endif
  98. #ifndef PR_CAP_AMBIENT
  99. #define PR_CAP_AMBIENT 47
  100. #define PR_CAP_AMBIENT_IS_SET 1
  101. #define PR_CAP_AMBIENT_RAISE 2
  102. #define PR_CAP_AMBIENT_LOWER 3
  103. #endif
  104. enum {
  105. IF_FEATURE_SETPRIV_DUMP(OPTBIT_DUMP,)
  106. IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_INH,)
  107. IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_AMB,)
  108. OPTBIT_NNP,
  109. IF_FEATURE_SETPRIV_DUMP(OPT_DUMP = (1 << OPTBIT_DUMP),)
  110. IF_FEATURE_SETPRIV_CAPABILITIES(OPT_INH = (1 << OPTBIT_INH),)
  111. IF_FEATURE_SETPRIV_CAPABILITIES(OPT_AMB = (1 << OPTBIT_AMB),)
  112. OPT_NNP = (1 << OPTBIT_NNP),
  113. };
  114. #if ENABLE_FEATURE_SETPRIV_CAPABILITIES
  115. DEFINE_STRUCT_CAPS;
  116. static unsigned parse_cap(const char *cap)
  117. {
  118. switch (cap[0]) {
  119. case '-':
  120. break;
  121. case '+':
  122. break;
  123. default:
  124. bb_error_msg_and_die("invalid capability '%s'", cap);
  125. break;
  126. }
  127. cap++;
  128. return cap_name_to_number(cap);
  129. }
  130. static void set_inh_caps(char *capstring)
  131. {
  132. struct caps caps;
  133. getcaps(&caps);
  134. capstring = strtok(capstring, ",");
  135. while (capstring) {
  136. unsigned cap;
  137. cap = parse_cap(capstring);
  138. if (CAP_TO_INDEX(cap) >= caps.u32s)
  139. bb_error_msg_and_die("invalid capability '%s'", capstring);
  140. if (capstring[0] == '+')
  141. caps.data[CAP_TO_INDEX(cap)].inheritable |= CAP_TO_MASK(cap);
  142. else
  143. caps.data[CAP_TO_INDEX(cap)].inheritable &= ~CAP_TO_MASK(cap);
  144. capstring = strtok(NULL, ",");
  145. }
  146. if (capset(&caps.header, caps.data) != 0)
  147. bb_simple_perror_msg_and_die("capset");
  148. }
  149. static void set_ambient_caps(char *string)
  150. {
  151. char *cap;
  152. cap = strtok(string, ",");
  153. while (cap) {
  154. unsigned idx;
  155. idx = parse_cap(cap);
  156. if (cap[0] == '+') {
  157. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, idx, 0, 0) < 0)
  158. bb_simple_perror_msg("cap_ambient_raise");
  159. } else {
  160. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, idx, 0, 0) < 0)
  161. bb_simple_perror_msg("cap_ambient_lower");
  162. }
  163. cap = strtok(NULL, ",");
  164. }
  165. }
  166. #endif /* FEATURE_SETPRIV_CAPABILITIES */
  167. #if ENABLE_FEATURE_SETPRIV_DUMP
  168. # if !ENABLE_FEATURE_SETPRIV_CAPABILITY_NAMES
  169. # define printf_cap(pfx, cap_no) printf("%scap_%u", (pfx), (cap_no))
  170. # endif
  171. static int dump(void)
  172. {
  173. IF_FEATURE_SETPRIV_CAPABILITIES(struct caps caps;)
  174. const char *fmt;
  175. uid_t ruid, euid, suid;
  176. gid_t rgid, egid, sgid;
  177. gid_t *gids;
  178. int i, ngids, nnp;
  179. getresuid(&ruid, &euid, &suid); /* never fails in Linux */
  180. getresgid(&rgid, &egid, &sgid); /* never fails in Linux */
  181. ngids = 0;
  182. gids = bb_getgroups(&ngids, NULL); /* never fails in Linux */
  183. nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
  184. if (nnp < 0)
  185. bb_perror_msg_and_die("prctl: %s", "GET_NO_NEW_PRIVS");
  186. printf("uid: %u\n", (unsigned)ruid);
  187. printf("euid: %u\n", (unsigned)euid);
  188. printf("gid: %u\n", (unsigned)rgid);
  189. printf("egid: %u\n", (unsigned)egid);
  190. printf("Supplementary groups: ");
  191. if (ngids == 0) {
  192. printf("[none]");
  193. } else {
  194. fmt = ",%u" + 1;
  195. for (i = 0; i < ngids; i++) {
  196. printf(fmt, (unsigned)gids[i]);
  197. fmt = ",%u";
  198. }
  199. }
  200. printf("\nno_new_privs: %d\n", nnp);
  201. # if ENABLE_FEATURE_SETPRIV_CAPABILITIES
  202. getcaps(&caps);
  203. printf("Inheritable capabilities: ");
  204. fmt = "";
  205. for (i = 0; cap_valid(i); i++) {
  206. unsigned idx = CAP_TO_INDEX(i);
  207. if (idx >= caps.u32s) {
  208. printf("\nindex: %u u32s: %u capability: %u\n", idx, caps.u32s, i);
  209. bb_simple_error_msg_and_die("unsupported capability");
  210. }
  211. if (caps.data[idx].inheritable & CAP_TO_MASK(i)) {
  212. printf_cap(fmt, i);
  213. fmt = ",";
  214. }
  215. }
  216. if (!fmt[0])
  217. printf("[none]");
  218. printf("\nAmbient capabilities: ");
  219. fmt = "";
  220. for (i = 0; cap_valid(i); i++) {
  221. int ret = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, (unsigned long) i, 0UL, 0UL);
  222. if (ret < 0)
  223. bb_perror_msg_and_die("prctl: %s", "CAP_AMBIENT_IS_SET");
  224. if (ret) {
  225. printf_cap(fmt, i);
  226. fmt = ",";
  227. }
  228. }
  229. if (i == 0)
  230. printf("[unsupported]");
  231. else if (!fmt[0])
  232. printf("[none]");
  233. printf("\nCapability bounding set: ");
  234. fmt = "";
  235. for (i = 0; cap_valid(i); i++) {
  236. int ret = prctl(PR_CAPBSET_READ, (unsigned long) i, 0UL, 0UL, 0UL);
  237. if (ret < 0)
  238. bb_perror_msg_and_die("prctl: %s", "CAPBSET_READ");
  239. if (ret) {
  240. printf_cap(fmt, i);
  241. fmt = ",";
  242. }
  243. }
  244. if (!fmt[0])
  245. printf("[none]");
  246. bb_putchar('\n');
  247. # endif
  248. if (ENABLE_FEATURE_CLEAN_UP)
  249. free(gids);
  250. return EXIT_SUCCESS;
  251. }
  252. #endif /* FEATURE_SETPRIV_DUMP */
  253. int setpriv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  254. int setpriv_main(int argc UNUSED_PARAM, char **argv)
  255. {
  256. static const char setpriv_longopts[] ALIGN1 =
  257. IF_FEATURE_SETPRIV_DUMP(
  258. "dump\0" No_argument "d"
  259. )
  260. "nnp\0" No_argument "\xff"
  261. "no-new-privs\0" No_argument "\xff"
  262. IF_FEATURE_SETPRIV_CAPABILITIES(
  263. "inh-caps\0" Required_argument "\xfe"
  264. "ambient-caps\0" Required_argument "\xfd"
  265. )
  266. ;
  267. int opts;
  268. IF_FEATURE_SETPRIV_CAPABILITIES(char *inh_caps, *ambient_caps;)
  269. opts = getopt32long(argv, "+"
  270. IF_FEATURE_SETPRIV_DUMP("d")
  271. IF_FEATURE_SETPRIV_CAPABILITIES("\xfe:\xfd:"),
  272. setpriv_longopts
  273. IF_FEATURE_SETPRIV_CAPABILITIES(, &inh_caps, &ambient_caps)
  274. );
  275. argv += optind;
  276. #if ENABLE_FEATURE_SETPRIV_DUMP
  277. if (opts & OPT_DUMP) {
  278. if (argv[0] || (opts - OPT_DUMP) != 0)
  279. bb_show_usage();
  280. return dump();
  281. }
  282. #endif
  283. if (opts & OPT_NNP) {
  284. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
  285. bb_perror_msg_and_die("prctl: %s", "SET_NO_NEW_PRIVS");
  286. }
  287. #if ENABLE_FEATURE_SETPRIV_CAPABILITIES
  288. if (opts & OPT_INH)
  289. set_inh_caps(inh_caps);
  290. if (opts & OPT_AMB)
  291. set_ambient_caps(ambient_caps);
  292. #endif
  293. if (!argv[0])
  294. bb_show_usage();
  295. BB_EXECVP_or_die(argv);
  296. }