Skip to content

Commit 7cafe89

Browse files
committed
sanitizers: Improve FreeBSD ASLR detection
The kern.elf64.aslr.pie_enable and kern.elf32.aslr.pie_enable sysctls control the default setting for PIE binary address randomization, but it is possible to enable or disable ASLR on a per-process basis. Use procctl(2) to query whether ASLR is enabled. (Note that with ASLR enabled but sysctl kern.elf64.aslr.pie_enable=0 a PIE binary will in effect have randomization disabled, and be functional with msan. This is not intended as as a user-facing control though. The user can use proccontrol(1) to disable aslr for the process.) Approved by: dim Obtained from: LLVM 64de0064f315f57044294879d9ff4eacb454d45b MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D33933
1 parent 51fbd89 commit 7cafe89

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080

8181
#if SANITIZER_FREEBSD
8282
#include <sys/exec.h>
83+
#include <sys/procctl.h>
8384
#include <sys/sysctl.h>
8485
#include <machine/atomic.h>
8586
extern "C" {
@@ -2187,30 +2188,14 @@ void CheckASLR() {
21872188
ReExec();
21882189
}
21892190
#elif SANITIZER_FREEBSD
2190-
int aslr_pie;
2191-
uptr len = sizeof(aslr_pie);
2192-
#if SANITIZER_WORDSIZE == 64
2193-
if (UNLIKELY(internal_sysctlbyname("kern.elf64.aslr.pie_enable",
2194-
&aslr_pie, &len, NULL, 0) == -1)) {
2191+
int aslr_status;
2192+
if (UNLIKELY(procctl(P_PID, 0, PROC_ASLR_STATUS, &aslr_status) == -1)) {
21952193
// We're making things less 'dramatic' here since
2196-
// the OID is not necessarily guaranteed to be here
2194+
// the cmd is not necessarily guaranteed to be here
21972195
// just yet regarding FreeBSD release
21982196
return;
21992197
}
2200-
2201-
if (aslr_pie > 0) {
2202-
Printf("This sanitizer is not compatible with enabled ASLR "
2203-
"and binaries compiled with PIE\n");
2204-
Die();
2205-
}
2206-
#endif
2207-
// there might be 32 bits compat for 64 bits
2208-
if (UNLIKELY(internal_sysctlbyname("kern.elf32.aslr.pie_enable",
2209-
&aslr_pie, &len, NULL, 0) == -1)) {
2210-
return;
2211-
}
2212-
2213-
if (aslr_pie > 0) {
2198+
if ((aslr_status & PROC_ASLR_ACTIVE) != 0) {
22142199
Printf("This sanitizer is not compatible with enabled ASLR "
22152200
"and binaries compiled with PIE\n");
22162201
Die();

0 commit comments

Comments
 (0)