setuid.c 11 KB

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