Skip to content

Commit 8fb02fa

Browse files
authored
[sanitizer-common][Darwin] Improve mach_vm_region_recurse error handling (#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
1 parent 2b0f25d commit 8fb02fa

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>
@@ -1327,17 +1328,35 @@ uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
13271328
kr = mach_vm_region_recurse(mach_task_self(), &address, &vmsize, &depth,
13281329
(vm_region_info_t)&vminfo, &count);
13291330

1330-
// There are cases where going beyond the processes' max vm does
1331-
// not return KERN_INVALID_ADDRESS so we check for going beyond that
1332-
// max address as well.
1333-
if (kr == KERN_INVALID_ADDRESS || address > max_vm_address) {
1331+
if (kr == KERN_SUCCESS) {
1332+
// There are cases where going beyond the processes' max vm does
1333+
// not return KERN_INVALID_ADDRESS so we check for going beyond that
1334+
// max address as well.
1335+
if (address > max_vm_address) {
1336+
address = max_vm_address;
1337+
kr = -1; // break after this iteration.
1338+
}
1339+
1340+
if (max_occupied_addr)
1341+
*max_occupied_addr = address + vmsize;
1342+
} else if (kr == KERN_INVALID_ADDRESS) {
13341343
// No more regions beyond "address", consider the gap at the end of VM.
13351344
address = max_vm_address;
1336-
vmsize = 0;
1337-
kr = -1; // break after this iteration.
1345+
1346+
// We will break after this iteration anyway since kr != KERN_SUCCESS
1347+
} else if (kr == KERN_DENIED) {
1348+
Report("ERROR: Unable to find a memory range for dynamic shadow.\n");
1349+
Report("HINT: Ensure mach_vm_region_recurse is allowed under sandbox.\n");
1350+
Die();
13381351
} else {
1339-
if (max_occupied_addr) *max_occupied_addr = address + vmsize;
1352+
Report(
1353+
"WARNING: mach_vm_region_recurse returned unexpected code %d (%s)\n",
1354+
kr, mach_error_string(kr));
1355+
DCHECK(false && "mach_vm_region_recurse returned unexpected code");
1356+
break; // address is not valid unless KERN_SUCCESS, therefore we must not
1357+
// use it.
13401358
}
1359+
13411360
if (free_begin != address) {
13421361
// We found a free region [free_begin..address-1].
13431362
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)