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