test_switch_id.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Copyright (c) 2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #ifdef HAVE_SYS_CAPABILITY_H
  5. #include <sys/capability.h>
  6. #endif
  7. #define TEST_BUILT_WITH_CAPS 0
  8. #define TEST_HAVE_CAPS 1
  9. #define TEST_ROOT_CAN_BIND_LOW 2
  10. #define TEST_SETUID 3
  11. #define TEST_SETUID_KEEPCAPS 4
  12. #define TEST_SETUID_STRICT 5
  13. static const char *username;
  14. static const struct {
  15. const char *name;
  16. int test_id;
  17. } which_test[] = {
  18. { "built-with-caps", TEST_BUILT_WITH_CAPS },
  19. { "have-caps", TEST_HAVE_CAPS },
  20. { "root-bind-low", TEST_ROOT_CAN_BIND_LOW },
  21. { "setuid", TEST_SETUID },
  22. { "setuid-keepcaps", TEST_SETUID_KEEPCAPS },
  23. { "setuid-strict", TEST_SETUID_STRICT },
  24. { NULL, 0 }
  25. };
  26. /* 0 on no, 1 on yes, -1 on failure. */
  27. static int
  28. check_can_bind_low_ports(void)
  29. {
  30. int port;
  31. struct sockaddr_in sin;
  32. memset(&sin, 0, sizeof(sin));
  33. sin.sin_family = AF_INET;
  34. for (port = 600; port < 1024; ++port) {
  35. sin.sin_port = htons(port);
  36. tor_socket_t fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  37. if (! SOCKET_OK(fd)) {
  38. perror("socket");
  39. return -1;
  40. }
  41. int one = 1;
  42. if (setsockopt(fd, SOL_SOCKET,SO_REUSEADDR, (void*)&one,
  43. (socklen_t)sizeof(one))) {
  44. perror("setsockopt");
  45. tor_close_socket_simple(fd);
  46. return -1;
  47. }
  48. int res = bind(fd, (struct sockaddr *)&sin, sizeof(sin));
  49. tor_close_socket_simple(fd);
  50. if (res == 0) {
  51. /* bind was successful */
  52. return 1;
  53. } else if (errno == EACCES || errno == EPERM) {
  54. /* Got a permission-denied error. */
  55. return 0;
  56. #if defined(_WIN32)
  57. } else if (errno == WSAEADDRINUSE) {
  58. #else
  59. } else if (errno == EADDRINUSE) {
  60. #endif
  61. /* Huh; somebody is using that port. */
  62. } else {
  63. perror("bind");
  64. }
  65. }
  66. return -1;
  67. }
  68. int
  69. main(int argc, char **argv)
  70. {
  71. #if defined(_WIN32)
  72. (void) argc;
  73. (void) argv;
  74. fprintf(stderr, "This test is not supported on your OS.\n");
  75. return 1;
  76. #else
  77. const char *testname;
  78. if (argc != 3) {
  79. fprintf(stderr, "I want 2 arguments: a username and a command.\n");
  80. return 1;
  81. }
  82. if (getuid() != 0) {
  83. fprintf(stderr, "This test only works when it's run as root.\n");
  84. return 1;
  85. }
  86. username = argv[1];
  87. testname = argv[2];
  88. int test_id = -1;
  89. int i;
  90. for (i = 0; which_test[i].name; ++i) {
  91. if (!strcmp(which_test[i].name, testname)) {
  92. test_id = which_test[i].test_id;
  93. break;
  94. }
  95. }
  96. if (test_id == -1) {
  97. fprintf(stderr, "Unrecognized test '%s'\n", testname);
  98. return 1;
  99. }
  100. #ifdef HAVE_LINUX_CAPABILITIES
  101. const int have_cap_support = 1;
  102. #else
  103. const int have_cap_support = 0;
  104. #endif
  105. int okay;
  106. init_logging(1);
  107. log_severity_list_t sev;
  108. memset(&sev, 0, sizeof(sev));
  109. set_log_severity_config(LOG_WARN, LOG_ERR, &sev);
  110. add_stream_log(&sev, "", fileno(stderr));
  111. switch (test_id)
  112. {
  113. case TEST_BUILT_WITH_CAPS:
  114. /* Succeed if we were built with capability support. */
  115. okay = have_cap_support;
  116. break;
  117. case TEST_HAVE_CAPS:
  118. /* Succeed if "capabilities work" == "we were built with capability
  119. * support." */
  120. okay = have_cap_support == have_capability_support();
  121. break;
  122. case TEST_ROOT_CAN_BIND_LOW:
  123. /* Succeed if root can bind low ports. */
  124. okay = check_can_bind_low_ports() == 1;
  125. break;
  126. case TEST_SETUID:
  127. /* Succeed if we can do a setuid with no capability retention, and doing
  128. * so makes us lose the ability to bind low ports */
  129. case TEST_SETUID_KEEPCAPS:
  130. /* Succeed if we can do a setuid with capability retention, and doing so
  131. * does not make us lose the ability to bind low ports */
  132. {
  133. int keepcaps = (test_id == TEST_SETUID_KEEPCAPS);
  134. okay = switch_id(username, keepcaps ? SWITCH_ID_KEEP_BINDLOW : 0) == 0;
  135. if (okay) {
  136. okay = check_can_bind_low_ports() == keepcaps;
  137. }
  138. break;
  139. }
  140. case TEST_SETUID_STRICT:
  141. /* Succeed if, after a setuid, we cannot setuid back, and we cannot
  142. * re-grab any capabilities. */
  143. okay = switch_id(username, SWITCH_ID_KEEP_BINDLOW) == 0;
  144. if (okay) {
  145. /* We'd better not be able to setuid back! */
  146. if (setuid(0) == 0 || errno != EPERM) {
  147. okay = 0;
  148. }
  149. }
  150. #ifdef HAVE_LINUX_CAPABILITIES
  151. if (okay) {
  152. cap_t caps = cap_get_proc();
  153. const cap_value_t caplist[] = {
  154. CAP_SETUID,
  155. };
  156. cap_set_flag(caps, CAP_PERMITTED, 1, caplist, CAP_SET);
  157. if (cap_set_proc(caps) == 0 || errno != EPERM) {
  158. okay = 0;
  159. }
  160. cap_free(caps);
  161. }
  162. #endif
  163. break;
  164. default:
  165. fprintf(stderr, "Unsupported test '%s'\n", testname);
  166. okay = 0;
  167. break;
  168. }
  169. if (!okay) {
  170. fprintf(stderr, "Test %s failed!\n", testname);
  171. }
  172. return (okay ? 0 : 1);
  173. #endif
  174. }