test_switch_id.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, &one, sizeof(one))) {
  43. perror("setsockopt");
  44. tor_close_socket_simple(fd);
  45. return -1;
  46. }
  47. int res = bind(fd, (struct sockaddr *)&sin, sizeof(sin));
  48. tor_close_socket_simple(fd);
  49. if (res == 0) {
  50. /* bind was successful */
  51. return 1;
  52. } else if (errno == EACCES || errno == EPERM) {
  53. /* Got a permission-denied error. */
  54. return 0;
  55. } else if (errno == EADDRINUSE) {
  56. /* Huh; somebody is using that port. */
  57. } else {
  58. perror("bind");
  59. }
  60. }
  61. return -1;
  62. }
  63. int
  64. main(int argc, char **argv)
  65. {
  66. #if defined(_WIN32)
  67. (void) argc;
  68. (void) argv;
  69. fprintf(stderr, "This test is not supported on your OS.\n");
  70. return 1;
  71. #else
  72. const char *testname;
  73. if (argc != 3) {
  74. fprintf(stderr, "I want 2 arguments: a username and a command.\n");
  75. return 1;
  76. }
  77. if (getuid() != 0) {
  78. fprintf(stderr, "This test only works when it's run as root.\n");
  79. return 1;
  80. }
  81. username = argv[1];
  82. testname = argv[2];
  83. int test_id = -1;
  84. int i;
  85. for (i = 0; which_test[i].name; ++i) {
  86. if (!strcmp(which_test[i].name, testname)) {
  87. test_id = which_test[i].test_id;
  88. break;
  89. }
  90. }
  91. if (test_id == -1) {
  92. fprintf(stderr, "Unrecognized test '%s'\n", testname);
  93. return 1;
  94. }
  95. #ifdef HAVE_LINUX_CAPABILITIES
  96. const int have_cap_support = 1;
  97. #else
  98. const int have_cap_support = 0;
  99. #endif
  100. int okay;
  101. init_logging(1);
  102. log_severity_list_t sev;
  103. memset(&sev, 0, sizeof(sev));
  104. set_log_severity_config(LOG_WARN, LOG_ERR, &sev);
  105. add_stream_log(&sev, "", fileno(stderr));
  106. switch (test_id)
  107. {
  108. case TEST_BUILT_WITH_CAPS:
  109. /* Succeed if we were built with capability support. */
  110. okay = have_cap_support;
  111. break;
  112. case TEST_HAVE_CAPS:
  113. /* Succeed if "capabilities work" == "we were built with capability
  114. * support." */
  115. okay = have_cap_support == have_capability_support();
  116. break;
  117. case TEST_ROOT_CAN_BIND_LOW:
  118. /* Succeed if root can bind low ports. */
  119. okay = check_can_bind_low_ports() == 1;
  120. break;
  121. case TEST_SETUID:
  122. /* Succeed if we can do a setuid with no capability retention, and doing
  123. * so makes us lose the ability to bind low ports */
  124. case TEST_SETUID_KEEPCAPS:
  125. /* Succeed if we can do a setuid with capability retention, and doing so
  126. * does not make us lose the ability to bind low ports */
  127. {
  128. int keepcaps = (test_id == TEST_SETUID_KEEPCAPS);
  129. okay = switch_id(username, keepcaps ? SWITCH_ID_KEEP_BINDLOW : 0) == 0;
  130. if (okay) {
  131. okay = check_can_bind_low_ports() == keepcaps;
  132. }
  133. break;
  134. }
  135. case TEST_SETUID_STRICT:
  136. /* Succeed if, after a setuid, we cannot setuid back, and we cannot
  137. * re-grab any capabilities. */
  138. okay = switch_id(username, SWITCH_ID_KEEP_BINDLOW) == 0;
  139. if (okay) {
  140. /* We'd better not be able to setuid back! */
  141. if (setuid(0) == 0 || errno != EPERM) {
  142. okay = 0;
  143. }
  144. }
  145. #ifdef HAVE_LINUX_CAPABILITIES
  146. if (okay) {
  147. cap_t caps = cap_get_proc();
  148. const cap_value_t caplist[] = {
  149. CAP_SETUID,
  150. };
  151. cap_set_flag(caps, CAP_PERMITTED, 1, caplist, CAP_SET);
  152. if (cap_set_proc(caps) == 0 || errno != EPERM) {
  153. okay = 0;
  154. }
  155. cap_free(caps);
  156. }
  157. #endif
  158. break;
  159. default:
  160. fprintf(stderr, "Unsupported test '%s'\n", testname);
  161. okay = 0;
  162. break;
  163. }
  164. if (!okay) {
  165. fprintf(stderr, "Test %s failed!\n", testname);
  166. }
  167. return (okay ? 0 : 1);
  168. #endif
  169. }