setpriv.c 9.2 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 LONG_OPTS
  13. //config: help
  14. //config: Run a program with different Linux privilege settings.
  15. //config: Requires kernel >= 3.5
  16. //config:
  17. //config:config FEATURE_SETPRIV_DUMP
  18. //config: bool "Support dumping current privilege state"
  19. //config: default y
  20. //config: depends on SETPRIV
  21. //config: help
  22. //config: Enables the "--dump" switch to print out the current privilege
  23. //config: state. This is helpful for diagnosing problems.
  24. //config:
  25. //config:config FEATURE_SETPRIV_CAPABILITIES
  26. //config: bool "Support capabilities"
  27. //config: default y
  28. //config: depends on SETPRIV
  29. //config: help
  30. //config: Capabilities can be used to grant processes additional rights
  31. //config: without the necessity to always execute as the root user.
  32. //config: Enabling this option enables "--dump" to show information on
  33. //config: capabilities.
  34. //config:
  35. //config:config FEATURE_SETPRIV_CAPABILITY_NAMES
  36. //config: bool "Support capability names"
  37. //config: default y
  38. //config: depends on SETPRIV && FEATURE_SETPRIV_CAPABILITIES
  39. //config: help
  40. //config: Capabilities can be either referenced via a human-readble name,
  41. //config: e.g. "net_admin", or using their index, e.g. "cap_12". Enabling
  42. //config: this option allows using the human-readable names in addition to
  43. //config: the index-based names.
  44. //applet:IF_SETPRIV(APPLET(setpriv, BB_DIR_BIN, BB_SUID_DROP))
  45. //kbuild:lib-$(CONFIG_SETPRIV) += setpriv.o
  46. //usage:#define setpriv_trivial_usage
  47. //usage: "[OPTIONS] PROG [ARGS]"
  48. //usage:#define setpriv_full_usage "\n\n"
  49. //usage: "Run PROG with different privilege settings\n"
  50. //usage: IF_FEATURE_SETPRIV_DUMP(
  51. //usage: "\n-d,--dump Show current capabilities"
  52. //usage: )
  53. //usage: "\n--nnp,--no-new-privs Ignore setuid/setgid bits and file capabilities"
  54. //usage: IF_FEATURE_SETPRIV_CAPABILITIES(
  55. //usage: "\n--inh-caps CAP,CAP Set inheritable capabilities"
  56. //usage: "\n--ambient-caps CAP,CAP Set ambient capabilities"
  57. //usage: )
  58. //setpriv from util-linux 2.28:
  59. // -d, --dump show current state (and do not exec anything)
  60. // --nnp, --no-new-privs disallow granting new privileges
  61. // --inh-caps <caps,...> set inheritable capabilities
  62. // --bounding-set <caps> set capability bounding set
  63. // --ruid <uid> set real uid
  64. // --euid <uid> set effective uid
  65. // --rgid <gid> set real gid
  66. // --egid <gid> set effective gid
  67. // --reuid <uid> set real and effective uid
  68. // --regid <gid> set real and effective gid
  69. // --clear-groups clear supplementary groups
  70. // --keep-groups keep supplementary groups
  71. // --groups <group,...> set supplementary groups
  72. // --securebits <bits> set securebits
  73. // --selinux-label <label> set SELinux label
  74. // --apparmor-profile <pr> set AppArmor profile
  75. #if ENABLE_FEATURE_SETPRIV_CAPABILITIES
  76. #include <linux/capability.h>
  77. // #include <sys/capability.h>
  78. // This header is in libcap, but the functions are in libc.
  79. // Comment in the header says this above capset/capget:
  80. /* system calls - look to libc for function to system call mapping */
  81. extern int capset(cap_user_header_t header, cap_user_data_t data);
  82. extern int capget(cap_user_header_t header, const cap_user_data_t data);
  83. // so for bbox, let's just repeat the declarations.
  84. // This way, libcap needs not be installed in build environment.
  85. #endif
  86. #include <sys/prctl.h>
  87. #include "libbb.h"
  88. #ifndef PR_CAPBSET_READ
  89. #define PR_CAPBSET_READ 23
  90. #endif
  91. #ifndef PR_SET_NO_NEW_PRIVS
  92. #define PR_SET_NO_NEW_PRIVS 38
  93. #endif
  94. #ifndef PR_GET_NO_NEW_PRIVS
  95. #define PR_GET_NO_NEW_PRIVS 39
  96. #endif
  97. #ifndef PR_CAP_AMBIENT
  98. #define PR_CAP_AMBIENT 47
  99. #define PR_CAP_AMBIENT_IS_SET 1
  100. #define PR_CAP_AMBIENT_RAISE 2
  101. #define PR_CAP_AMBIENT_LOWER 3
  102. #endif
  103. enum {
  104. IF_FEATURE_SETPRIV_DUMP(OPTBIT_DUMP,)
  105. IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_INH,)
  106. IF_FEATURE_SETPRIV_CAPABILITIES(OPTBIT_AMB,)
  107. OPTBIT_NNP,
  108. IF_FEATURE_SETPRIV_DUMP(OPT_DUMP = (1 << OPTBIT_DUMP),)
  109. IF_FEATURE_SETPRIV_CAPABILITIES(OPT_INH = (1 << OPTBIT_INH),)
  110. IF_FEATURE_SETPRIV_CAPABILITIES(OPT_AMB = (1 << OPTBIT_AMB),)
  111. OPT_NNP = (1 << OPTBIT_NNP),
  112. };
  113. #if ENABLE_FEATURE_SETPRIV_CAPABILITIES
  114. DEFINE_STRUCT_CAPS;
  115. static unsigned parse_cap(const char *cap)
  116. {
  117. switch (cap[0]) {
  118. case '-':
  119. break;
  120. case '+':
  121. break;
  122. default:
  123. bb_error_msg_and_die("invalid capability '%s'", cap);
  124. break;
  125. }
  126. cap++;
  127. return cap_name_to_number(cap);
  128. }
  129. static void set_inh_caps(char *capstring)
  130. {
  131. struct caps caps;
  132. char *string;
  133. getcaps(&caps);
  134. capstring = strtok_r(capstring, ",", &string);
  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_r(NULL, ",", &string);
  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_r(string, ",", &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_r(NULL, ",", &string);
  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. }