1 // RUN: %clang %s -o %t && %run %t
2 // capget() and capset() are not intercepted on Android.
3 // UNSUPPORTED: android
4
5 #include <assert.h>
6 #include <errno.h>
7 #include <linux/capability.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "sanitizer_common/sanitizer_specific.h"
12
13 /* Use capget() and capset() from glibc. */
14 int capget(cap_user_header_t header, cap_user_data_t data);
15 int capset(cap_user_header_t header, const cap_user_data_t data);
16
test(int version,int u32s)17 static void test(int version, int u32s) {
18 struct __user_cap_header_struct hdr = {
19 .version = version,
20 .pid = 0,
21 };
22 struct __user_cap_data_struct data[u32s];
23 if (capget(&hdr, data)) {
24 assert(errno == EINVAL);
25 /* Check that memory is not touched. */
26 #if __has_feature(memory_sanitizer)
27 assert(__msan_test_shadow(data, sizeof(data)) == 0);
28 #endif
29 hdr.version = version;
30 int err = capset(&hdr, data);
31 assert(errno == EINVAL);
32 } else {
33 for (int i = 0; i < u32s; i++)
34 printf("%x %x %x\n", data[i].effective, data[i].permitted,
35 data[i].inheritable);
36 int err = capset(&hdr, data);
37 assert(!err);
38 }
39 }
40
main()41 int main() {
42 test(0, 1); /* Test an incorrect version. */
43 test(_LINUX_CAPABILITY_VERSION_1, _LINUX_CAPABILITY_U32S_1);
44 test(_LINUX_CAPABILITY_VERSION_2, _LINUX_CAPABILITY_U32S_2);
45 test(_LINUX_CAPABILITY_VERSION_3, _LINUX_CAPABILITY_U32S_3);
46
47 return EXIT_SUCCESS;
48 }
49