Skip to content

Commit 43e8b0a

Browse files
committed
Fix mlock/mlock2/munlock in case of zero length.
1 parent f1e9adf commit 43e8b0a

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

filc/tests/mlocknull/manifest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
return:
2+
failure
3+
output-includes:
4+
- "cannot read pointer with null object"

filc/tests/mlocknull/mlocknull.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <errno.h>
2+
#include <stdio.h>
3+
#include <sys/mman.h>
4+
int main(void) {
5+
if (mlock(NULL, 4096) == -1)
6+
perror("mlock");
7+
return 0;
8+
}

filc/tests/mlockzerobytes/manifest

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
return:
2+
success
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <errno.h>
2+
#include <stdio.h>
3+
#include <sys/mman.h>
4+
#include <stdfil.h>
5+
#include <string.h>
6+
int main(void) {
7+
ZASSERT(!mlock(NULL, 0));
8+
ZASSERT(!mlock(main, 0));
9+
int res = mlock((void*)666, 0);
10+
int my_errno = errno;
11+
zprintf("res = %d, error = %s\n", res, strerror(my_errno));
12+
ZASSERT(res == -1);
13+
ZASSERT(my_errno == ENOMEM);
14+
ZASSERT(!mlock(zweak_new(NULL), 0));
15+
ZASSERT(!munlock(NULL, 0));
16+
ZASSERT(!munlock(main, 0));
17+
res = munlock((void*)666, 0);
18+
my_errno = errno;
19+
zprintf("res = %d, error = %s\n", res, strerror(my_errno));
20+
ZASSERT(res == -1);
21+
ZASSERT(my_errno == ENOMEM);
22+
ZASSERT(!munlock(zweak_new(NULL), 0));
23+
return 0;
24+
}

libpas/src/libpas/filc_runtime.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8950,8 +8950,12 @@ int filc_native_zsys_getsid(filc_thread* my_thread, int pid)
89508950
static int mlock_impl(filc_thread* my_thread, filc_ptr addr_ptr, size_t len,
89518951
int (*actual_mlock)(const void*, size_t))
89528952
{
8953-
filc_check_access(addr_ptr, len, filc_read_access);
8954-
check_mmap(addr_ptr);
8953+
/* Only check the pointer if len is not zero. If len is zero, still make the syscall, because I am
8954+
guessing someone might do this to check capabilities. */
8955+
if (len) {
8956+
filc_check_access(addr_ptr, len, filc_read_access);
8957+
check_mmap(addr_ptr);
8958+
}
89558959
return FILC_SYSCALL(my_thread, actual_mlock(filc_ptr_ptr(addr_ptr), len));
89568960
}
89578961

0 commit comments

Comments
 (0)