Skip to content

Commit 75895bd

Browse files
committed
[sanitizer] Fix running sanitizer_bad_report_path_test on Linux as root
Running tests as root is not the greatest idea, however, there is one valid use case - running them in a container in order to verify LLVM on different distros. There is no reason to configure unprivileged users in a container, so one works as root. sanitizer_bad_report_path_test assumes that creating a file in a non-writable directory would fail, which is not the case if CAP_DAC_OVERRIDE, which root has, is in effect. So drop it.
1 parent 0035c2e commit 75895bd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_bad_report_path_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,32 @@
1313
#include <stdio.h>
1414
#include <string.h>
1515

16+
#if defined(__linux__)
17+
# include <linux/capability.h>
18+
19+
/* Use capget() and capset() from glibc. */
20+
extern "C" int capget(cap_user_header_t header, cap_user_data_t data);
21+
extern "C" int capset(cap_user_header_t header, const cap_user_data_t data);
22+
23+
static void try_drop_cap_dac_override(void) {
24+
struct __user_cap_header_struct hdr = {
25+
.version = _LINUX_CAPABILITY_VERSION_1,
26+
.pid = 0,
27+
};
28+
struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_1];
29+
if (!capget(&hdr, data)) {
30+
data[CAP_DAC_OVERRIDE >> 5].effective &= ~(1 << (CAP_DAC_OVERRIDE & 31));
31+
capset(&hdr, data);
32+
}
33+
}
34+
#else
35+
static void try_drop_cap_dac_override(void) {}
36+
#endif
37+
1638
volatile int *null = 0;
1739

1840
int main(int argc, char **argv) {
41+
try_drop_cap_dac_override();
1942
char buff[1000];
2043
sprintf(buff, "%s.report_path/report", argv[0]);
2144
__sanitizer_set_report_path(buff);

0 commit comments

Comments
 (0)