Skip to content

Commit 01d565c

Browse files
committed
[sanitizer-common] Improve mach_vm_region_recurse error handling
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://145860383
1 parent a6585b0 commit 01d565c

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,17 +1265,34 @@ uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
12651265
kr = mach_vm_region_recurse(mach_task_self(), &address, &vmsize, &depth,
12661266
(vm_region_info_t)&vminfo, &count);
12671267

1268-
// There are cases where going beyond the processes' max vm does
1269-
// not return KERN_INVALID_ADDRESS so we check for going beyond that
1270-
// max address as well.
1271-
if (kr == KERN_INVALID_ADDRESS || address > max_vm_address) {
1268+
if (kr == KERN_SUCCESS) {
1269+
// There are cases where going beyond the processes' max vm does
1270+
// not return KERN_INVALID_ADDRESS so we check for going beyond that
1271+
// max address as well.
1272+
if (address > max_vm_address) {
1273+
address = max_vm_address;
1274+
kr = -1; // break after this iteration.
1275+
}
1276+
1277+
if (max_occupied_addr)
1278+
*max_occupied_addr = address + vmsize;
1279+
} else if (kr == KERN_INVALID_ADDRESS) {
12721280
// No more regions beyond "address", consider the gap at the end of VM.
12731281
address = max_vm_address;
1274-
vmsize = 0;
1275-
kr = -1; // break after this iteration.
1282+
1283+
// We will break after this iteration anyway since kr != KERN_SUCCESS
1284+
} else if (kr == KERN_DENIED) {
1285+
Report("ERROR: Unable to find a memory range for dynamic shadow.\n");
1286+
Report("HINT: Ensure mach_vm_region_recurse is allowed under sandbox.\n");
1287+
Die();
12761288
} else {
1277-
if (max_occupied_addr) *max_occupied_addr = address + vmsize;
1289+
Report("WARNING: mach_vm_region_recurse returned unexpected code %d\n",
1290+
kr);
1291+
DCHECK(false && "mach_vm_region_recurse returned unexpected code");
1292+
break; // address is not valid unless KERN_SUCCESS, therefore we must not
1293+
// use it.
12781294
}
1295+
12791296
if (free_begin != address) {
12801297
// We found a free region [free_begin..address-1].
12811298
uptr gap_start = RoundUpTo((uptr)free_begin + left_padding, alignment);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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
15+
// UNSUPPORTED: x86_64h-darwin
16+
17+
#include <stdlib.h>
18+
19+
int main() {
20+
char *x = (char *)malloc(10 * sizeof(char));
21+
free(x);
22+
return x[5];
23+
// CHECK-ALLOW: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
24+
// CHECK-DENY-NOT: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
25+
// CHECK-ALLOW: {{READ of size 1 at 0x.* thread T0}}
26+
// CHECK-ALLOW: {{ #0 0x.* in main}}
27+
// CHECK-ALLOW: {{freed by thread T0 here:}}
28+
// CHECK-ALLOW: {{ #0 0x.* in free}}
29+
// CHECK-ALLOW: {{ #1 0x.* in main}}
30+
// CHECK-ALLOW: {{previously allocated by thread T0 here:}}
31+
// CHECK-ALLOW: {{ #0 0x.* in malloc}}
32+
// CHECK-ALLOW: {{ #1 0x.* in main}}
33+
// CHECK-DENY: {{.*HINT: Ensure mach_vm_region_recurse is allowed under sandbox}}
34+
}

0 commit comments

Comments
 (0)