setuid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/process/setuid.h"
  7. #if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_CAP_SET_PROC)
  8. #define HAVE_LINUX_CAPABILITIES
  9. #endif
  10. #include "lib/container/smartlist.h"
  11. #include "lib/fs/userdb.h"
  12. #include "lib/log/torlog.h"
  13. #include "lib/log/util_bug.h"
  14. #include "lib/malloc/util_malloc.h"
  15. #ifdef HAVE_SYS_TYPES_H
  16. #include <sys/types.h>
  17. #endif
  18. #ifdef HAVE_UNISTD_H
  19. #include <unistd.h>
  20. #endif
  21. #ifdef HAVE_GRP_H
  22. #include <grp.h>
  23. #endif
  24. #ifdef HAVE_PWD_H
  25. #include <pwd.h>
  26. #endif
  27. #ifdef HAVE_SYS_CAPABILITY_H
  28. #include <sys/capability.h>
  29. #endif
  30. #ifdef HAVE_SYS_PRCTL_H
  31. #include <sys/prctl.h>
  32. #endif
  33. #include <errno.h>
  34. #include <string.h>
  35. #ifndef _WIN32
  36. /** Log details of current user and group credentials. Return 0 on
  37. * success. Logs and return -1 on failure.
  38. */
  39. static int
  40. log_credential_status(void)
  41. {
  42. /** Log level to use when describing non-error UID/GID status. */
  43. #define CREDENTIAL_LOG_LEVEL LOG_INFO
  44. /* Real, effective and saved UIDs */
  45. uid_t ruid, euid, suid;
  46. /* Read, effective and saved GIDs */
  47. gid_t rgid, egid, sgid;
  48. /* Supplementary groups */
  49. gid_t *sup_gids = NULL;
  50. int sup_gids_size;
  51. /* Number of supplementary groups */
  52. int ngids;
  53. /* log UIDs */
  54. #ifdef HAVE_GETRESUID
  55. if (getresuid(&ruid, &euid, &suid) != 0 ) {
  56. log_warn(LD_GENERAL, "Error getting changed UIDs: %s", strerror(errno));
  57. return -1;
  58. } else {
  59. log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
  60. "UID is %u (real), %u (effective), %u (saved)",
  61. (unsigned)ruid, (unsigned)euid, (unsigned)suid);
  62. }
  63. #else /* !(defined(HAVE_GETRESUID)) */
  64. /* getresuid is not present on MacOS X, so we can't get the saved (E)UID */
  65. ruid = getuid();
  66. euid = geteuid();
  67. (void)suid;
  68. log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
  69. "UID is %u (real), %u (effective), unknown (saved)",
  70. (unsigned)ruid, (unsigned)euid);
  71. #endif /* defined(HAVE_GETRESUID) */
  72. /* log GIDs */
  73. #ifdef HAVE_GETRESGID
  74. if (getresgid(&rgid, &egid, &sgid) != 0 ) {
  75. log_warn(LD_GENERAL, "Error getting changed GIDs: %s", strerror(errno));
  76. return -1;
  77. } else {
  78. log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
  79. "GID is %u (real), %u (effective), %u (saved)",
  80. (unsigned)rgid, (unsigned)egid, (unsigned)sgid);
  81. }
  82. #else /* !(defined(HAVE_GETRESGID)) */
  83. /* getresgid is not present on MacOS X, so we can't get the saved (E)GID */
  84. rgid = getgid();
  85. egid = getegid();
  86. (void)sgid;
  87. log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
  88. "GID is %u (real), %u (effective), unknown (saved)",
  89. (unsigned)rgid, (unsigned)egid);
  90. #endif /* defined(HAVE_GETRESGID) */
  91. /* log supplementary groups */
  92. sup_gids_size = 64;
  93. sup_gids = tor_calloc(64, sizeof(gid_t));
  94. while ((ngids = getgroups(sup_gids_size, sup_gids)) < 0 &&
  95. errno == EINVAL &&
  96. sup_gids_size < NGROUPS_MAX) {
  97. sup_gids_size *= 2;
  98. sup_gids = tor_reallocarray(sup_gids, sizeof(gid_t), sup_gids_size);
  99. }
  100. if (ngids < 0) {
  101. log_warn(LD_GENERAL, "Error getting supplementary GIDs: %s",
  102. strerror(errno));
  103. tor_free(sup_gids);
  104. return -1;
  105. } else {
  106. int i, retval = 0;
  107. char *s = NULL;
  108. smartlist_t *elts = smartlist_new();
  109. for (i = 0; i<ngids; i++) {
  110. smartlist_add_asprintf(elts, "%u", (unsigned)sup_gids[i]);
  111. }
  112. s = smartlist_join_strings(elts, " ", 0, NULL);
  113. log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL, "Supplementary groups are: %s",s);
  114. tor_free(s);
  115. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  116. smartlist_free(elts);
  117. tor_free(sup_gids);
  118. return retval;
  119. }
  120. return 0;
  121. }
  122. #endif /* !defined(_WIN32) */
  123. /** Return true iff we were compiled with capability support, and capabilities
  124. * seem to work. **/
  125. int
  126. have_capability_support(void)
  127. {
  128. #ifdef HAVE_LINUX_CAPABILITIES
  129. cap_t caps = cap_get_proc();
  130. if (caps == NULL)
  131. return 0;
  132. cap_free(caps);
  133. return 1;
  134. #else /* !(defined(HAVE_LINUX_CAPABILITIES)) */
  135. return 0;
  136. #endif /* defined(HAVE_LINUX_CAPABILITIES) */
  137. }
  138. #ifdef HAVE_LINUX_CAPABILITIES
  139. /** Helper. Drop all capabilities but a small set, and set PR_KEEPCAPS as
  140. * appropriate.
  141. *
  142. * If pre_setuid, retain only CAP_NET_BIND_SERVICE, CAP_SETUID, and
  143. * CAP_SETGID, and use PR_KEEPCAPS to ensure that capabilities persist across
  144. * setuid().
  145. *
  146. * If not pre_setuid, retain only CAP_NET_BIND_SERVICE, and disable
  147. * PR_KEEPCAPS.
  148. *
  149. * Return 0 on success, and -1 on failure.
  150. */
  151. static int
  152. drop_capabilities(int pre_setuid)
  153. {
  154. /* We keep these three capabilities, and these only, as we setuid.
  155. * After we setuid, we drop all but the first. */
  156. const cap_value_t caplist[] = {
  157. CAP_NET_BIND_SERVICE, CAP_SETUID, CAP_SETGID
  158. };
  159. const char *where = pre_setuid ? "pre-setuid" : "post-setuid";
  160. const int n_effective = pre_setuid ? 3 : 1;
  161. const int n_permitted = pre_setuid ? 3 : 1;
  162. const int n_inheritable = 1;
  163. const int keepcaps = pre_setuid ? 1 : 0;
  164. /* Sets whether we keep capabilities across a setuid. */
  165. if (prctl(PR_SET_KEEPCAPS, keepcaps) < 0) {
  166. log_warn(LD_CONFIG, "Unable to call prctl() %s: %s",
  167. where, strerror(errno));
  168. return -1;
  169. }
  170. cap_t caps = cap_get_proc();
  171. if (!caps) {
  172. log_warn(LD_CONFIG, "Unable to call cap_get_proc() %s: %s",
  173. where, strerror(errno));
  174. return -1;
  175. }
  176. cap_clear(caps);
  177. cap_set_flag(caps, CAP_EFFECTIVE, n_effective, caplist, CAP_SET);
  178. cap_set_flag(caps, CAP_PERMITTED, n_permitted, caplist, CAP_SET);
  179. cap_set_flag(caps, CAP_INHERITABLE, n_inheritable, caplist, CAP_SET);
  180. int r = cap_set_proc(caps);
  181. cap_free(caps);
  182. if (r < 0) {
  183. log_warn(LD_CONFIG, "No permission to set capabilities %s: %s",
  184. where, strerror(errno));
  185. return -1;
  186. }
  187. return 0;
  188. }
  189. #endif /* defined(HAVE_LINUX_CAPABILITIES) */
  190. /** Call setuid and setgid to run as <b>user</b> and switch to their
  191. * primary group. Return 0 on success. On failure, log and return -1.
  192. *
  193. * If SWITCH_ID_KEEP_BINDLOW is set in 'flags', try to use the capability
  194. * system to retain the abilitity to bind low ports.
  195. *
  196. * If SWITCH_ID_WARN_IF_NO_CAPS is set in flags, also warn if we have
  197. * don't have capability support.
  198. */
  199. int
  200. switch_id(const char *user, const unsigned flags)
  201. {
  202. #ifndef _WIN32
  203. const struct passwd *pw = NULL;
  204. uid_t old_uid;
  205. gid_t old_gid;
  206. static int have_already_switched_id = 0;
  207. const int keep_bindlow = !!(flags & SWITCH_ID_KEEP_BINDLOW);
  208. const int warn_if_no_caps = !!(flags & SWITCH_ID_WARN_IF_NO_CAPS);
  209. tor_assert(user);
  210. if (have_already_switched_id)
  211. return 0;
  212. /* Log the initial credential state */
  213. if (log_credential_status())
  214. return -1;
  215. log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL, "Changing user and groups");
  216. /* Get old UID/GID to check if we changed correctly */
  217. old_uid = getuid();
  218. old_gid = getgid();
  219. /* Lookup the user and group information, if we have a problem, bail out. */
  220. pw = tor_getpwnam(user);
  221. if (pw == NULL) {
  222. log_warn(LD_CONFIG, "Error setting configured user: %s not found", user);
  223. return -1;
  224. }
  225. #ifdef HAVE_LINUX_CAPABILITIES
  226. (void) warn_if_no_caps;
  227. if (keep_bindlow) {
  228. if (drop_capabilities(1))
  229. return -1;
  230. }
  231. #else /* !(defined(HAVE_LINUX_CAPABILITIES)) */
  232. (void) keep_bindlow;
  233. if (warn_if_no_caps) {
  234. log_warn(LD_CONFIG, "KeepBindCapabilities set, but no capability support "
  235. "on this system.");
  236. }
  237. #endif /* defined(HAVE_LINUX_CAPABILITIES) */
  238. /* Properly switch egid,gid,euid,uid here or bail out */
  239. if (setgroups(1, &pw->pw_gid)) {
  240. log_warn(LD_GENERAL, "Error setting groups to gid %d: \"%s\".",
  241. (int)pw->pw_gid, strerror(errno));
  242. if (old_uid == pw->pw_uid) {
  243. log_warn(LD_GENERAL, "Tor is already running as %s. You do not need "
  244. "the \"User\" option if you are already running as the user "
  245. "you want to be. (If you did not set the User option in your "
  246. "torrc, check whether it was specified on the command line "
  247. "by a startup script.)", user);
  248. } else {
  249. log_warn(LD_GENERAL, "If you set the \"User\" option, you must start Tor"
  250. " as root.");
  251. }
  252. return -1;
  253. }
  254. if (setegid(pw->pw_gid)) {
  255. log_warn(LD_GENERAL, "Error setting egid to %d: %s",
  256. (int)pw->pw_gid, strerror(errno));
  257. return -1;
  258. }
  259. if (setgid(pw->pw_gid)) {
  260. log_warn(LD_GENERAL, "Error setting gid to %d: %s",
  261. (int)pw->pw_gid, strerror(errno));
  262. return -1;
  263. }
  264. if (setuid(pw->pw_uid)) {
  265. log_warn(LD_GENERAL, "Error setting configured uid to %s (%d): %s",
  266. user, (int)pw->pw_uid, strerror(errno));
  267. return -1;
  268. }
  269. if (seteuid(pw->pw_uid)) {
  270. log_warn(LD_GENERAL, "Error setting configured euid to %s (%d): %s",
  271. user, (int)pw->pw_uid, strerror(errno));
  272. return -1;
  273. }
  274. /* This is how OpenBSD rolls:
  275. if (setgroups(1, &pw->pw_gid) || setegid(pw->pw_gid) ||
  276. setgid(pw->pw_gid) || setuid(pw->pw_uid) || seteuid(pw->pw_uid)) {
  277. setgid(pw->pw_gid) || seteuid(pw->pw_uid) || setuid(pw->pw_uid)) {
  278. log_warn(LD_GENERAL, "Error setting configured UID/GID: %s",
  279. strerror(errno));
  280. return -1;
  281. }
  282. */
  283. /* We've properly switched egid, gid, euid, uid, and supplementary groups if
  284. * we're here. */
  285. #ifdef HAVE_LINUX_CAPABILITIES
  286. if (keep_bindlow) {
  287. if (drop_capabilities(0))
  288. return -1;
  289. }
  290. #endif /* defined(HAVE_LINUX_CAPABILITIES) */
  291. #if !defined(CYGWIN) && !defined(__CYGWIN__)
  292. /* If we tried to drop privilege to a group/user other than root, attempt to
  293. * restore root (E)(U|G)ID, and abort if the operation succeeds */
  294. /* Only check for privilege dropping if we were asked to be non-root */
  295. if (pw->pw_uid) {
  296. /* Try changing GID/EGID */
  297. if (pw->pw_gid != old_gid &&
  298. (setgid(old_gid) != -1 || setegid(old_gid) != -1)) {
  299. log_warn(LD_GENERAL, "Was able to restore group credentials even after "
  300. "switching GID: this means that the setgid code didn't work.");
  301. return -1;
  302. }
  303. /* Try changing UID/EUID */
  304. if (pw->pw_uid != old_uid &&
  305. (setuid(old_uid) != -1 || seteuid(old_uid) != -1)) {
  306. log_warn(LD_GENERAL, "Was able to restore user credentials even after "
  307. "switching UID: this means that the setuid code didn't work.");
  308. return -1;
  309. }
  310. }
  311. #endif /* !defined(CYGWIN) && !defined(__CYGWIN__) */
  312. /* Check what really happened */
  313. if (log_credential_status()) {
  314. return -1;
  315. }
  316. have_already_switched_id = 1; /* mark success so we never try again */
  317. #if defined(__linux__) && defined(HAVE_SYS_PRCTL_H) && \
  318. defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
  319. if (pw->pw_uid) {
  320. /* Re-enable core dumps if we're not running as root. */
  321. log_info(LD_CONFIG, "Re-enabling coredumps");
  322. if (prctl(PR_SET_DUMPABLE, 1)) {
  323. log_warn(LD_CONFIG, "Unable to re-enable coredumps: %s",strerror(errno));
  324. }
  325. }
  326. #endif /* defined(__linux__) && defined(HAVE_SYS_PRCTL_H) && ... */
  327. return 0;
  328. #else /* !(!defined(_WIN32)) */
  329. (void)user;
  330. (void)flags;
  331. log_warn(LD_CONFIG, "Switching users is unsupported on your OS.");
  332. return -1;
  333. #endif /* !defined(_WIN32) */
  334. }