Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAP_STACK fixes #2285

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions bin/cheribsdtest/cheribsdtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,25 @@
context, actual, actual_str, expected, expected_str);
}

#ifdef __CHERI_PURE_CAPABILITY__
#define __CHERIBSDTEST_PTR_FMT "%#p"
#else
#define __CHERIBSDTEST_PTR_FMT "%p"
#endif

/** Check that @p call fails and errno is set to @p expected_errno */
#define CHERIBSDTEST_CHECK_CALL_ERROR(call, expected_errno) \
do { \
errno = 0; \
int __ret = call; \
int call_errno = errno; \
CHERIBSDTEST_VERIFY2(__ret == -1, \
#call " unexpectedly returned %d", __ret); \
_cheribsdtest_check_errno(#call, call_errno, expected_errno); \
#define CHERIBSDTEST_CHECK_CALL_ERROR(call, expected_errno) \
do { \
errno = 0; \
__typeof(call) __ret = call; \
int call_errno = errno; \
CHERIBSDTEST_VERIFY2(__ret == (__typeof(__ret))-1, \

Check failure on line 322 in bin/cheribsdtest/cheribsdtest.h

View workflow job for this annotation

GitHub Actions / Style Checker

spaces required around that '-' (ctx:VxV)
bsdjhb marked this conversation as resolved.
Show resolved Hide resolved
_Generic((__ret), \
void *: #call " unexpectedly returned " __CHERIBSDTEST_PTR_FMT, \

Check warning on line 324 in bin/cheribsdtest/cheribsdtest.h

View workflow job for this annotation

GitHub Actions / Style Checker

line over 80 characters

Check failure on line 324 in bin/cheribsdtest/cheribsdtest.h

View workflow job for this annotation

GitHub Actions / Style Checker

spaces required around that '*' (ctx:WxO)

Check failure on line 324 in bin/cheribsdtest/cheribsdtest.h

View workflow job for this annotation

GitHub Actions / Style Checker

spaces required around that ':' (ctx:OxW)
default: #call " unexpectedly returned %d"), \

Check failure on line 325 in bin/cheribsdtest/cheribsdtest.h

View workflow job for this annotation

GitHub Actions / Style Checker

spaces required around that ':' (ctx:VxW)

Check failure on line 325 in bin/cheribsdtest/cheribsdtest.h

View workflow job for this annotation

GitHub Actions / Style Checker

trailing statements should be on next line
__ret); \
_cheribsdtest_check_errno(#call, call_errno, \
expected_errno); \
} while (0)

/* For libc_memcpy and libc_memset tests and the unaligned copy tests: */
Expand Down
37 changes: 36 additions & 1 deletion bin/cheribsdtest/cheribsdtest_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ CHERIBSDTEST(vm_notag_mprotect_no_cap,
static void
mmap_check_bad_protections(int prot, int expected_errno)
{
CHERIBSDTEST_CHECK_CALL_ERROR((int)(intptr_t)mmap(NULL, getpagesize(),
CHERIBSDTEST_CHECK_CALL_ERROR(mmap(NULL, getpagesize(),
prot, MAP_ANON, -1, 0), expected_errno);
}

Expand Down Expand Up @@ -2844,4 +2844,39 @@ CHERIBSDTEST(cheri_revoke_shm_anon_hoard_closed,

#endif /* CHERIBSDTEST_CHERI_REVOKE_TESTS */

/*
* This test is derived from a syskiller panic. Bugs in
* vm_map_stack_locked() when the stack was being inserted into an
* existing reservation (why would anyone do this in the real world?)
* caused a panic.
* https://github.com/CTSRD-CHERI/cheribsd/issues/2252
*/
CHERIBSDTEST(mmap_insert_stack,
"try to insert a stack mapping in a reservation")
{
void *p;

p = CHERIBSDTEST_CHECK_SYSCALL(mmap((void *)(intptr_t)0x20000000,
0x1000000, PROT_WRITE | PROT_READ,
MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));

/*
* Historically would fail, but leave the map in a broken state
* due to trying to insert a reservation inside an existing one.
* This is now rejected outright.
*/
CHERIBSDTEST_CHECK_CALL_ERROR(mmap(cheri_setaddress(p, 0x20ffc000),
0x2000, PROT_WRITE | PROT_READ, MAP_STACK | MAP_FIXED, -1, 0),
ENOMEM);

/*
* This would trigger a panic by trying to remove an unmapped
* entry left by the previous mmap.
*/
CHERIBSDTEST_CHECK_SYSCALL(munmap(cheri_setaddress(p, 0x20ffc000),
0x3000));

cheribsdtest_success();
}

#endif /* __CHERI_PURE_CAPABILITY__ */
8 changes: 8 additions & 0 deletions lib/libsys/mmap.2
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ and
.Dv PROT_WRITE .
The size of the guard, in pages, is specified by sysctl
.Dv security.bsd.stack_guard_page .
.Pp
Under CheriABI,
.Dv MAP_STACK
may not be combined with
.Dv MAP_FIXED
and an
.Fa addr
argument that is a valid pointer.
.El
.Pp
The
Expand Down
14 changes: 11 additions & 3 deletions sys/vm/vm_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -5437,6 +5437,7 @@ vm_map_stack_locked(vm_map_t map, vm_pointer_t addrbos, vm_size_t max_ssize,
vm_pointer_t bot, gap_bot, gap_top, top;
vm_size_t init_ssize, sgp;
int orient, rv;
vm_offset_t reservation;

/*
* The stack orientation is piggybacked with the cow argument.
Expand All @@ -5462,13 +5463,18 @@ vm_map_stack_locked(vm_map_t map, vm_pointer_t addrbos, vm_size_t max_ssize,
init_ssize = max_ssize - sgp;

if (map->flags & MAP_RESERVATIONS) {
/* Check reservation exists */
/*
* If this map enables reservations, a reservation must have
* already been created for us.
*/
if (vm_map_lookup_entry(map, addrbos, &prev_entry) == 0 ||
(prev_entry->eflags & MAP_ENTRY_UNMAPPED) == 0)
return (KERN_PROTECTION_FAILURE);
/* If reservation can't accommodate max_ssize, no go. */
if (prev_entry->end - (vm_offset_t)addrbos < max_ssize)
return (KERN_NO_SPACE);

reservation = prev_entry->reservation;
} else {
/* If addr is already mapped, no go */
if (vm_map_lookup_entry(map, addrbos, &prev_entry))
Expand All @@ -5478,6 +5484,8 @@ vm_map_stack_locked(vm_map_t map, vm_pointer_t addrbos, vm_size_t max_ssize,
*/
if (vm_map_entry_succ(prev_entry)->start < addrbos + max_ssize)
return (KERN_NO_SPACE);

reservation = addrbos;
}

/*
Expand All @@ -5501,7 +5509,7 @@ vm_map_stack_locked(vm_map_t map, vm_pointer_t addrbos, vm_size_t max_ssize,
gap_bot = top;
gap_top = addrbos + max_ssize;
}
rv = vm_map_insert1(map, NULL, 0, bot, top, prot, max, cow, addrbos,
rv = vm_map_insert1(map, NULL, 0, bot, top, prot, max, cow, reservation,
&new_entry);
if (rv != KERN_SUCCESS)
return (rv);
Expand All @@ -5517,7 +5525,7 @@ vm_map_stack_locked(vm_map_t map, vm_pointer_t addrbos, vm_size_t max_ssize,
return (KERN_SUCCESS);
rv = vm_map_insert1(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP), addrbos,
MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP), reservation,
&gap_entry);
if (rv == KERN_SUCCESS) {
KASSERT((gap_entry->eflags & MAP_ENTRY_GUARD) != 0,
Expand Down
2 changes: 2 additions & 0 deletions sys/vm/vm_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,11 @@
if (cheri_gettag(uap->addr)) {
if ((flags & MAP_FIXED) == 0)
return (EPROT);
else if ((flags & MAP_STACK) != 0)
return (ENOMEM);
else if ((cheri_getperm(uap->addr) & CHERI_PERM_SW_VMEM))
source_cap = uap->addr;
else {

Check warning on line 342 in sys/vm/vm_mmap.c

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line
SYSERRCAUSE("MAP_FIXED without CHERI_PERM_SW_VMEM");
return (EACCES);
}
Expand Down
Loading