Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : 3 : #include <stdio.h> 4 : #include <sys/prctl.h> 5 : 6 : #include "alloc-util.h" 7 : #include "cap-list.h" 8 : #include "capability-util.h" 9 : #include "parse-util.h" 10 : #include "string-util.h" 11 : #include "util.h" 12 : 13 : /* verify the capability parser */ 14 1 : static void test_cap_list(void) { 15 : int i; 16 : 17 1 : assert_se(!capability_to_name(-1)); 18 1 : assert_se(!capability_to_name(capability_list_length())); 19 : 20 39 : for (i = 0; i < capability_list_length(); i++) { 21 : const char *n; 22 : 23 38 : assert_se(n = capability_to_name(i)); 24 38 : assert_se(capability_from_name(n) == i); 25 38 : printf("%s = %i\n", n, i); 26 : } 27 : 28 1 : assert_se(capability_from_name("asdfbsd") == -EINVAL); 29 1 : assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ); 30 1 : assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ); 31 1 : assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ); 32 1 : assert_se(capability_from_name("0") == 0); 33 1 : assert_se(capability_from_name("15") == 15); 34 1 : assert_se(capability_from_name("-1") == -EINVAL); 35 : 36 39 : for (i = 0; i < capability_list_length(); i++) { 37 38 : _cleanup_cap_free_charp_ char *a = NULL; 38 : const char *b; 39 : unsigned u; 40 : 41 38 : assert_se(a = cap_to_name(i)); 42 : 43 : /* quit the loop as soon as libcap starts returning 44 : * numeric ids, formatted as strings */ 45 38 : if (safe_atou(a, &u) >= 0) 46 0 : break; 47 : 48 38 : assert_se(b = capability_to_name(i)); 49 : 50 38 : printf("%s vs. %s\n", a, b); 51 : 52 38 : assert_se(strcasecmp(a, b) == 0); 53 : } 54 1 : } 55 : 56 5 : static void test_capability_set_one(uint64_t c, const char *t) { 57 5 : _cleanup_free_ char *t1 = NULL; 58 5 : uint64_t c1, c_masked = c & ((UINT64_C(1) << capability_list_length()) - 1); 59 : 60 5 : assert_se(capability_set_to_string_alloc(c, &t1) == 0); 61 5 : assert_se(streq(t1, t)); 62 : 63 5 : assert_se(capability_set_from_string(t1, &c1) == 0); 64 5 : assert_se(c1 == c_masked); 65 : 66 5 : free(t1); 67 5 : assert_se(t1 = strjoin("'cap_chown cap_dac_override' \"cap_setgid cap_setuid\"", t, 68 : " hogehoge foobar 12345 3.14 -3 ", t)); 69 5 : assert_se(capability_set_from_string(t1, &c1) == 0); 70 5 : assert_se(c1 == c_masked); 71 5 : } 72 : 73 1 : static void test_capability_set(void) { 74 : uint64_t c; 75 : 76 1 : assert_se(capability_set_from_string(NULL, &c) == 0); 77 1 : assert_se(c == 0); 78 : 79 1 : assert_se(capability_set_from_string("", &c) == 0); 80 1 : assert_se(c == 0); 81 : 82 1 : assert_se(capability_set_from_string("0", &c) == 0); 83 1 : assert_se(c == UINT64_C(1)); 84 : 85 1 : assert_se(capability_set_from_string("1", &c) == 0); 86 1 : assert_se(c == UINT64_C(1) << 1); 87 : 88 1 : assert_se(capability_set_from_string("0 1 2 3", &c) == 0); 89 1 : assert_se(c == (UINT64_C(1) << 4) - 1); 90 : 91 1 : test_capability_set_one(0, ""); 92 1 : test_capability_set_one( 93 : UINT64_C(1) << CAP_DAC_OVERRIDE, 94 : "cap_dac_override"); 95 1 : test_capability_set_one( 96 : UINT64_C(1) << CAP_DAC_OVERRIDE | 97 1 : UINT64_C(1) << capability_list_length(), 98 : "cap_dac_override"); 99 1 : test_capability_set_one( 100 1 : UINT64_C(1) << capability_list_length(), ""); 101 1 : test_capability_set_one( 102 : UINT64_C(1) << CAP_CHOWN | 103 : UINT64_C(1) << CAP_DAC_OVERRIDE | 104 : UINT64_C(1) << CAP_DAC_READ_SEARCH | 105 : UINT64_C(1) << CAP_FOWNER | 106 : UINT64_C(1) << CAP_SETGID | 107 : UINT64_C(1) << CAP_SETUID | 108 : UINT64_C(1) << CAP_SYS_PTRACE | 109 : UINT64_C(1) << CAP_SYS_ADMIN | 110 : UINT64_C(1) << CAP_AUDIT_CONTROL | 111 : UINT64_C(1) << CAP_MAC_OVERRIDE | 112 : UINT64_C(1) << CAP_SYSLOG | 113 1 : UINT64_C(1) << (capability_list_length() + 1), 114 : "cap_chown cap_dac_override cap_dac_read_search cap_fowner " 115 : "cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin " 116 : "cap_audit_control cap_mac_override cap_syslog"); 117 1 : } 118 : 119 1 : int main(int argc, char *argv[]) { 120 1 : test_cap_list(); 121 1 : test_capability_set(); 122 : 123 1 : return 0; 124 : }