Skip to content

Commit ead6ec0

Browse files
committed
[sanitizer-common][Darwin] Improve mach_vm_region_recurse error handling (llvm#158670)
Some sanitizers use mach_vm_region_recurse on macOS to find a sufficiently large gap to allocate shadow memory. Some sandboxes do not allow this. When we get KERN_DENIED, we suggest to the user that it may have been blocked by the sandbox. For error codes other than KERN_INVALID_ADDRESS and KERN_DENIED, we make sure to log a message and not use the address. rdar://160625998 (cherry picked from commit 8fb02fa)
1 parent 28455a9 commit ead6ec0

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extern char ***_NSGetArgv(void);
6767
# include <libkern/OSAtomic.h>
6868
# include <mach-o/dyld.h>
6969
# include <mach/mach.h>
70+
# include <mach/mach_error.h>
7071
# include <mach/mach_time.h>
7172
# include <mach/vm_statistics.h>
7273
# include <malloc/malloc.h>
@@ -1321,17 +1322,35 @@ uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
13211322
kr = mach_vm_region_recurse(mach_task_self(), &address, &vmsize, &depth,
13221323
(vm_region_info_t)&vminfo, &count);
13231324

1324-
// There are cases where going beyond the processes' max vm does
1325-
// not return KERN_INVALID_ADDRESS so we check for going beyond that
1326-
// max address as well.
1327-
if (kr == KERN_INVALID_ADDRESS || address > max_vm_address) {
1325+
if (kr == KERN_SUCCESS) {
1326+
// There are cases where going beyond the processes' max vm does
1327+
// not return KERN_INVALID_ADDRESS so we check for going beyond that
1328+
// max address as well.
1329+
if (address > max_vm_address) {
1330+
address = max_vm_address;
1331+
kr = -1; // break after this iteration.
1332+
}
1333+
1334+
if (max_occupied_addr)
1335+
*max_occupied_addr = address + vmsize;
1336+
} else if (kr == KERN_INVALID_ADDRESS) {
13281337
// No more regions beyond "address", consider the gap at the end of VM.
13291338
address = max_vm_address;
1330-
vmsize = 0;
1331-
kr = -1; // break after this iteration.
1339+
1340+
// We will break after this iteration anyway since kr != KERN_SUCCESS
1341+
} else if (kr == KERN_DENIED) {
1342+
Report("ERROR: Unable to find a memory range for dynamic shadow.\n");
1343+
Report("HINT: Ensure mach_vm_region_recurse is allowed under sandbox.\n");
1344+
Die();
13321345
} else {
1333-
if (max_occupied_addr) *max_occupied_addr = address + vmsize;
1346+
Report(
1347+
"WARNING: mach_vm_region_recurse returned unexpected code %d (%s)\n",
1348+
kr, mach_error_string(kr));
1349+
DCHECK(false && "mach_vm_region_recurse returned unexpected code");
1350+
break; // address is not valid unless KERN_SUCCESS, therefore we must not
1351+
// use it.
13341352
}
1353+
13351354
if (free_begin != address) {
13361355
// We found a free region [free_begin..address-1].
13371356
uptr gap_start = RoundUpTo((uptr)free_begin + left_padding, alignment);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Check that if mach_vm_region_recurse is disallowed by sandbox, we report a message saying so.
2+
3+
// RUN: %clangxx_asan -O0 %s -o %t
4+
// RUN: not %run sandbox-exec -p '(version 1)(allow default)(deny syscall-mig (kernel-mig-routine mach_vm_region_recurse))' %t 2>&1 | FileCheck --check-prefix=CHECK-DENY %s
5+
// RUN: not %run %t 2>&1 | FileCheck --check-prefix=CHECK-ALLOW %s
6+
// RUN: %clangxx_asan -O3 %s -o %t
7+
// RUN: not %run sandbox-exec -p '(version 1)(allow default)(deny syscall-mig (kernel-mig-routine mach_vm_region_recurse))' %t 2>&1 | FileCheck --check-prefix=CHECK-DENY %s
8+
// RUN: not %run %t 2>&1 | FileCheck --check-prefix=CHECK-ALLOW %s
9+
10+
// sandbox-exec isn't available on iOS
11+
// UNSUPPORTED: ios
12+
13+
// x86_64 does not use ASAN_SHADOW_OFFSET_DYNAMIC
14+
// UNSUPPORTED: x86_64-darwin || x86_64h-darwin
15+
16+
#include <stdlib.h>
17+
18+
int main() {
19+
char *x = (char *)malloc(10 * sizeof(char));
20+
free(x);
21+
return x[5];
22+
// CHECK-ALLOW: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
23+
// CHECK-DENY-NOT: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
24+
// CHECK-ALLOW: {{READ of size 1 at 0x.* thread T0}}
25+
// CHECK-ALLOW: {{ #0 0x.* in main}}
26+
// CHECK-ALLOW: {{freed by thread T0 here:}}
27+
// CHECK-ALLOW: {{ #0 0x.* in free}}
28+
// CHECK-ALLOW: {{ #1 0x.* in main}}
29+
// CHECK-ALLOW: {{previously allocated by thread T0 here:}}
30+
// CHECK-ALLOW: {{ #0 0x.* in malloc}}
31+
// CHECK-ALLOW: {{ #1 0x.* in main}}
32+
// CHECK-DENY: {{.*HINT: Ensure mach_vm_region_recurse is allowed under sandbox}}
33+
}

0 commit comments

Comments
 (0)