test_switch_id.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const char *testname;
  67. if (argc != 3) {
  68. fprintf(stderr, "I want 2 arguments: a username and a command.\n");
  69. return 1;
  70. }
  71. if (getuid() != 0) {
  72. fprintf(stderr, "This test only works when it's run as root.\n");
  73. return 1;
  74. }
  75. username = argv[1];
  76. testname = argv[2];
  77. int test_id = -1;
  78. int i;
  79. for (i = 0; which_test[i].name; ++i) {
  80. if (!strcmp(which_test[i].name, testname)) {
  81. test_id = which_test[i].test_id;
  82. break;
  83. }
  84. }
  85. if (test_id == -1) {
  86. fprintf(stderr, "Unrecognized test '%s'\n", testname);
  87. return 1;
  88. }
  89. #ifdef HAVE_LINUX_CAPABILITIES
  90. const int have_cap_support = 1;
  91. #else
  92. const int have_cap_support = 0;
  93. #endif
  94. int okay;
  95. init_logging(1);
  96. log_severity_list_t sev;
  97. memset(&sev, 0, sizeof(sev));
  98. set_log_severity_config(LOG_WARN, LOG_ERR, &sev);
  99. add_stream_log(&sev, "", fileno(stderr));
  100. switch (test_id)
  101. {
  102. case TEST_BUILT_WITH_CAPS:
  103. /* Succeed if we were built with capability support. */
  104. okay = have_cap_support;
  105. break;
  106. case TEST_HAVE_CAPS:
  107. /* Succeed if "capabilities work" == "we were built with capability
  108. * support." */
  109. okay = have_cap_support == have_capability_support();
  110. break;
  111. case TEST_ROOT_CAN_BIND_LOW:
  112. /* Succeed if root can bind low ports. */
  113. okay = check_can_bind_low_ports() == 1;
  114. break;
  115. case TEST_SETUID:
  116. /* Succeed if we can do a setuid with no capability retention, and doing
  117. * so makes us lose the ability to bind low ports */
  118. case TEST_SETUID_KEEPCAPS:
  119. /* Succeed if we can do a setuid with capability retention, and doing so
  120. * does not make us lose the ability to bind low ports */
  121. {
  122. int keepcaps = (test_id == TEST_SETUID_KEEPCAPS);
  123. okay = switch_id(username, keepcaps ? SWITCH_ID_KEEP_BINDLOW : 0) == 0;
  124. if (okay) {
  125. okay = check_can_bind_low_ports() == keepcaps;
  126. }
  127. break;
  128. }
  129. case TEST_SETUID_STRICT:
  130. /* Succeed if, after a setuid, we cannot setuid back, and we cannot
  131. * re-grab any capabilities. */
  132. okay = switch_id(username, SWITCH_ID_KEEP_BINDLOW) == 0;
  133. if (okay) {
  134. /* We'd better not be able to setuid back! */
  135. if (setuid(0) == 0 || errno != EPERM) {
  136. okay = 0;
  137. }
  138. }
  139. #ifdef HAVE_LINUX_CAPABILITIES
  140. if (okay) {
  141. cap_t caps = cap_get_proc();
  142. const cap_value_t caplist[] = {
  143. CAP_SETUID,
  144. };
  145. cap_set_flag(caps, CAP_PERMITTED, 1, caplist, CAP_SET);
  146. if (cap_set_proc(caps) == 0 || errno != EPERM) {
  147. okay = 0;
  148. }
  149. cap_free(caps);
  150. }
  151. #endif
  152. break;
  153. default:
  154. fprintf(stderr, "Unsupported test '%s'\n", testname);
  155. okay = 0;
  156. break;
  157. }
  158. if (!okay) {
  159. fprintf(stderr, "Test %s failed!\n", testname);
  160. }
  161. return (okay ? 0 : 1);
  162. }