Skip to content

Commit 90f175b

Browse files
jedomagalskiandrewboie
authored andcommitted
tests: userspace: test syscall buffer validation
Tests system call memory buffer read/write validation using the k_pipe_get() and k_pipe_put() calls from a userspace thread. Specifically, this tests _SYSCALL_MEMORY_READ/WRITE checks by the system call handler by attempting to read/write to a kernel object. write_kobject_user_pipe() attempts to write over a kernel object by using the kernel object's location as the buffer to place the data read from the pipe. read_kobject_user_pipe() attempts to read a kernel object by using the kernel object's location as the location of data to be placed into the pipe. Tested on qemu_x86 and frdm_k64, passes on both. Signed-off-by: Joshua Domagalski <[email protected]>
1 parent 3ad0207 commit 90f175b

File tree

1 file changed

+36
-2
lines changed
  • tests/kernel/mem_protect/userspace/src

1 file changed

+36
-2
lines changed

tests/kernel/mem_protect/userspace/src/main.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <stdlib.h>
1515

1616
#define INFO(fmt, ...) printk(fmt, ##__VA_ARGS__)
17+
#define PIPE_LEN 1
18+
#define BYTES_TO_READ_WRITE 1
1719

1820
K_SEM_DEFINE(uthread_start_sem, 0, 1);
1921
K_SEM_DEFINE(uthread_end_sem, 0, 1);
@@ -356,13 +358,43 @@ static void user_mode_enter(void)
356358
NULL, NULL, NULL);
357359
}
358360

361+
/* Define and initialize pipe. */
362+
K_PIPE_DEFINE(kpipe, PIPE_LEN, BYTES_TO_READ_WRITE);
363+
static size_t bytes_written_read;
364+
365+
static void write_kobject_user_pipe(void)
366+
{
367+
/*
368+
* Attempt to use system call from k_pipe_get to write over
369+
* a kernel object.
370+
*/
371+
k_pipe_get(&kpipe, &uthread_start_sem, BYTES_TO_READ_WRITE,
372+
&bytes_written_read, 1, K_NO_WAIT);
373+
374+
zassert_unreachable("System call memory write validation "
375+
"did not fault\n");
376+
}
377+
378+
static void read_kobject_user_pipe(void)
379+
{
380+
/*
381+
* Attempt to use system call from k_pipe_put to read a
382+
* kernel object.
383+
*/
384+
k_pipe_put(&kpipe, &uthread_start_sem, BYTES_TO_READ_WRITE,
385+
&bytes_written_read, 1, K_NO_WAIT);
386+
387+
zassert_unreachable("System call memory read validation "
388+
"did not fault\n");
389+
}
390+
359391
void test_main(void)
360392
{
361393
k_thread_access_grant(k_current_get(),
362394
&kthread_thread, &kthread_stack,
363395
&uthread_thread, &uthread_stack,
364396
&uthread_start_sem, &uthread_end_sem,
365-
&test_revoke_sem, NULL);
397+
&test_revoke_sem, &kpipe, NULL);
366398
ztest_test_suite(test_userspace,
367399
ztest_user_unit_test(is_usermode),
368400
ztest_user_unit_test(write_control),
@@ -383,7 +415,9 @@ void test_main(void)
383415
ztest_user_unit_test(revoke_noperms_object),
384416
ztest_user_unit_test(access_after_revoke),
385417
ztest_user_unit_test(revoke_other_thread),
386-
ztest_unit_test(user_mode_enter)
418+
ztest_unit_test(user_mode_enter),
419+
ztest_user_unit_test(write_kobject_user_pipe),
420+
ztest_user_unit_test(read_kobject_user_pipe)
387421
);
388422
ztest_run_test_suite(test_userspace);
389423
}

0 commit comments

Comments
 (0)