validate_cap.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <cap-ng.h>
  2. #include <err.h>
  3. #include <linux/capability.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <sys/prctl.h>
  8. #include <sys/auxv.h>
  9. #ifndef PR_CAP_AMBIENT
  10. #define PR_CAP_AMBIENT 47
  11. # define PR_CAP_AMBIENT_IS_SET 1
  12. # define PR_CAP_AMBIENT_RAISE 2
  13. # define PR_CAP_AMBIENT_LOWER 3
  14. # define PR_CAP_AMBIENT_CLEAR_ALL 4
  15. #endif
  16. #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19)
  17. # define HAVE_GETAUXVAL
  18. #endif
  19. static bool bool_arg(char **argv, int i)
  20. {
  21. if (!strcmp(argv[i], "0"))
  22. return false;
  23. else if (!strcmp(argv[i], "1"))
  24. return true;
  25. else
  26. errx(1, "wrong argv[%d]", i);
  27. }
  28. int main(int argc, char **argv)
  29. {
  30. const char *atsec = "";
  31. /*
  32. * Be careful just in case a setgid or setcapped copy of this
  33. * helper gets out.
  34. */
  35. if (argc != 5)
  36. errx(1, "wrong argc");
  37. #ifdef HAVE_GETAUXVAL
  38. if (getauxval(AT_SECURE))
  39. atsec = " (AT_SECURE is set)";
  40. else
  41. atsec = " (AT_SECURE is not set)";
  42. #endif
  43. capng_get_caps_process();
  44. if (capng_have_capability(CAPNG_EFFECTIVE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 1)) {
  45. printf("[FAIL]\tWrong effective state%s\n", atsec);
  46. return 1;
  47. }
  48. if (capng_have_capability(CAPNG_PERMITTED, CAP_NET_BIND_SERVICE) != bool_arg(argv, 2)) {
  49. printf("[FAIL]\tWrong permitted state%s\n", atsec);
  50. return 1;
  51. }
  52. if (capng_have_capability(CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE) != bool_arg(argv, 3)) {
  53. printf("[FAIL]\tWrong inheritable state%s\n", atsec);
  54. return 1;
  55. }
  56. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != bool_arg(argv, 4)) {
  57. printf("[FAIL]\tWrong ambient state%s\n", atsec);
  58. return 1;
  59. }
  60. printf("[OK]\tCapabilities after execve were correct\n");
  61. return 0;
  62. }