Add mseal() testing suite
v3 cover Add mseal() testing suite
Smoke test for mseal() syscall added in the kernel 6.10. All information can be found at the following link:
https://docs.kernel.org/userspace-api/mseal.html
---
Changes in v3:
- move memory allocation inside the child
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- don't use TST_ABI32 var anymore, but let tst_syscall handling ENOSYS
- call protection key syscalls via tst_syscall
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Andrea Cervesato (2):
Add mseal() syscall declaration
Add mseal01 test
include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/arc.in | 1 + include/lapi/syscalls/arm.in | 1 + include/lapi/syscalls/hppa.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/ia64.in | 1 + include/lapi/syscalls/loongarch.in | 1 + include/lapi/syscalls/mips_n32.in | 1 + include/lapi/syscalls/mips_n64.in | 1 + include/lapi/syscalls/mips_o32.in | 1 + include/lapi/syscalls/powerpc.in | 1 + include/lapi/syscalls/powerpc64.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/s390x.in | 1 + include/lapi/syscalls/sh.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/sparc64.in | 1 + include/lapi/syscalls/x86_64.in | 1 + testcases/kernel/syscalls/mseal/.gitignore | 1 + testcases/kernel/syscalls/mseal/Makefile | 7 ++ testcases/kernel/syscalls/mseal/mseal01.c | 156 +++++++++++++++++++++++++++++ 21 files changed, 182 insertions(+)
---
base-commit: 8422d4680b21e6576da63c677b5d49f46b477df0
change-id: 20240607-mseal-dfac12675096
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 1/2 Add mseal() syscall declaration
From: Andrea Cervesato <[email protected]>
--- include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/arc.in | 1 + include/lapi/syscalls/arm.in | 1 + include/lapi/syscalls/hppa.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/ia64.in | 1 + include/lapi/syscalls/loongarch.in | 1 + include/lapi/syscalls/mips_n32.in | 1 + include/lapi/syscalls/mips_n64.in | 1 + include/lapi/syscalls/mips_o32.in | 1 + include/lapi/syscalls/powerpc.in | 1 + include/lapi/syscalls/powerpc64.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/s390x.in | 1 + include/lapi/syscalls/sh.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/sparc64.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 18 files changed, 18 insertions(+)
include/lapi/syscalls/aarch64.in +1 -0
@@ -301,4 +301,5 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462 _sysctl 1078
include/lapi/syscalls/arc.in +1 -0
@@ -321,3 +321,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/arm.in +1 -0
@@ -400,3 +400,4 @@ landlock_restrict_self (__NR_SYSCALL_BASE+446) memfd_secret (__NR_SYSCALL_BASE+447) futex_waitv (__NR_SYSCALL_BASE+449) cachestat (__NR_SYSCALL_BASE+451) +mseal (__NR_SYSCALL_BASE+462)
include/lapi/syscalls/hppa.in +1 -0
@@ -48,3 +48,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/i386.in +1 -0
@@ -435,3 +435,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/ia64.in +1 -0
@@ -348,3 +348,4 @@ landlock_add_rule 1469 landlock_restrict_self 1470 futex_waitv 1473 cachestat 1475 +mseal 1486
include/lapi/syscalls/loongarch.in +1 -0
@@ -306,3 +306,4 @@ process_mrelease 448 futex_waitv 449 set_mempolicy_home_node 450 cachestat 451 +mseal 462
include/lapi/syscalls/mips_n32.in +1 -0
@@ -375,3 +375,4 @@ landlock_add_rule 6445 landlock_restrict_self 6446 futex_waitv 6449 cachestat 6451 +mseal 6462
include/lapi/syscalls/mips_n64.in +1 -0
@@ -351,3 +351,4 @@ landlock_add_rule 5445 landlock_restrict_self 5446 futex_waitv 5449 cachestat 5451 +mseal 5462
include/lapi/syscalls/mips_o32.in +1 -0
@@ -421,3 +421,4 @@ landlock_add_rule 4445 landlock_restrict_self 4446 futex_waitv 4449 cachestat 4451 +mseal 4462
include/lapi/syscalls/powerpc.in +1 -0
@@ -428,3 +428,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/powerpc64.in +1 -0
@@ -428,3 +428,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/s390.in +1 -0
@@ -415,3 +415,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/s390x.in +1 -0
@@ -363,3 +363,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/sh.in +1 -0
@@ -409,3 +409,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/sparc.in +1 -0
@@ -414,3 +414,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/sparc64.in +1 -0
@@ -379,3 +379,4 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462
include/lapi/syscalls/x86_64.in +1 -0
@@ -356,6 +356,7 @@ landlock_add_rule 445 landlock_restrict_self 446 futex_waitv 449 cachestat 451 +mseal 462 rt_sigaction 512 rt_sigreturn 513 ioctl 514
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 2/2 Add mseal01 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
This is a smoke test that verifies if mseal() protects specific VMA portions of a process. According to documentation, the syscall should protect memory from the following actions:
- unmapping, moving to another location, and shrinking the size, via munmap() and mremap()
- moving or expanding a different VMA into the current location, via mremap()
- modifying a VMA via mmap(MAP_FIXED)
- mprotect() and pkey_mprotect()
- destructive madvice() behaviors (e.g. MADV_DONTNEED) for anonymous memory, when users don’t have write permission to the memory
Any of the described actions is recognized via EPERM errno.
--- testcases/kernel/syscalls/mseal/.gitignore | 1 + testcases/kernel/syscalls/mseal/Makefile | 7 ++ testcases/kernel/syscalls/mseal/mseal01.c | 156 +++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+)
testcases/kernel/syscalls/mseal/.gitignore new file +1 -0
@@ -0,0 +1 @@ +mseal01
testcases/kernel/syscalls/mseal/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]> + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/mseal/mseal01.c new file +156 -0
156 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 2/2] Add mseal01 test
Hi!
This one looks good, but misses a runtest entry.
With runtest entry added (for the whole patchset):
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 2/2] Add mseal01 test
Quoted 7 lines · Cyril Hrubis
Hi!
This one looks good, but misses a runtest entry.With runtest entry added (for the whole patchset):
Reviewed-by Cyril Hrubis <[email protected]>
Pushed with runtest entry.
Thanks,
Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 2/2] Add mseal01 test
In reply to Andrea CervesatoHi Andrea,
Quoted 107 lines · Andrea Cervesato
From: Andrea Cervesato <[email protected]>
This is a smoke test that verifies if
mseal()protects specific VMA portions of a process. According to documentation, the syscall should protect memory from the following actions:- unmapping, moving to another location, and shrinking the size, via
munmap()andmremap()
- moving or expanding a different VMA into the current location, viamremap()
- modifying a VMA viammap(MAP_FIXED)
-mprotect()andpkey_mprotect()
- destructivemadvice()behaviors (e.g.MADV_DONTNEED) for anonymous memory, when users don’t have write permission to the memoryAny of the described actions is recognized via EPERM errno.
Signed-off-by Andrea Cervesato <[email protected]>--- testcases/kernel/syscalls/mseal/.gitignore | 1 + testcases/kernel/syscalls/mseal/Makefile | 7 ++ testcases/kernel/syscalls/mseal/mseal01.c | 156 +++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+)testcases/kernel/syscalls/mseal/.gitignore new file +1
-0@@ -0,0 +1 @@ +mseal01testcases/kernel/syscalls/mseal/Makefile new file +2
-0@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]Quoted 1 line
testcases/kernel/syscalls/mseal/Makefile new file +5
-0+ +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mktestcases/kernel/syscalls/mseal/mseal01.c new file +3
-0@@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2024 SUSE LLC Andrea Cervesato < [email protected]>
+ */testcases/kernel/syscalls/mseal/mseal01.c new file +5
-0+ +/*\ +* [Description] +* +* This is a smoke test that verifies if mseal() protects specific VMA portions
+ * of a process. According to documentation, the syscall should protect memory
+ * from the following actions:
+ *
+ * - unmapping, moving to another location, and shrinking the size, viamunmap()
+ * andmremap()
+ * - moving or expanding a different VMA into the current location, viamremap()
+ * - modifying a VMA viammap(MAP_FIXED)
+ * -mprotect()andpkey_mprotect()
+ * - destructivemadvice()behaviors (e.g.MADV_DONTNEED) for anonymous memory,
+ * when users don’t have write permission to the memory
+ *
+ * Any of the described actions is recognized via EPERM errno.
+ */testcases/kernel/syscalls/mseal/mseal01.c new file +23
-0+ +#define _GNU_SOURCE + +#include "tst_test.h" +#include "lapi/syscalls.h" + +#define MEMPAGES 8 +#define MEMSEAL 2 + +static void *mem_addr; +static int mem_size; +static int mem_offset; +static int mem_alignment; + +static inline int sys_mseal (void *start,size_t len) +{ +return tst_syscall (__NR_mseal, start, len,0 ); +} + +static void test_mprotect (void ) +{ +TST_EXP_FAIL (mprotect (mem_addr, mem_size,PROT_NONE ),EPERM ); +}
testcases/kernel/syscalls/mseal/mseal01.c line 55
Quoted 10 lines
testcases/kernel/syscalls/mseal/mseal01.c new file +10
-0+ +static void test_pkey_mprotect (void ) +{ +int ret; +int pkey; + + pkey =tst_syscall (__NR_pkey_alloc,0 ,0 ); +if (pkey ==-1 ) { +if (errno ==EINVAL ) +tst_brk (TCONF ,"pku is not supported on this CPU" );
Based on my limited knowledge of PKEY things, here might get three errnos:
ENOSYS, EINVAL and ENOSPC, so probably we can just do that by the exist pkey support checker and uses the ltp_pkey_* series capsulated wrappers?
https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/syscalls/pkeys/pkey.h
Quoted 97 lines
testcases/kernel/syscalls/mseal/mseal01.c new file +58
-0+ +tst_brk (TBROK |TERRNO ,"pkey_alloc() error" ); + } + +TST_EXP_FAIL (tst_syscall (__NR_pkey_mprotect, + mem_addr, mem_size, +PROT_NONE , + pkey), +EPERM ); + + ret =tst_syscall (__NR_pkey_free, pkey); +if (ret ==-1 ) +tst_brk (TBROK |TERRNO ,"pkey_free() error" ); +} + +static void test_madvise (void ) +{ +TST_EXP_FAIL (madvise (mem_addr, mem_size,MADV_DONTNEED ),EPERM ); +} + +static void test_munmap (void ) +{ +TST_EXP_FAIL (munmap (mem_addr, mem_size),EPERM ); +} + +static void test_mremap_resize (void ) +{ +void *new_addr; +size_t new_size =2 * mem_alignment; + + new_addr =SAFE_MMAP (NULL , mem_size, +PROT_READ , +MAP_ANONYMOUS |MAP_PRIVATE , +-1 ,0 ); + +TST_EXP_FAIL_PTR_VOID (mremap (mem_addr, mem_size, new_size, +MREMAP_MAYMOVE |MREMAP_FIXED , + new_addr), +EPERM ); + +SAFE_MUNMAP (new_addr, new_size); +} + +static void test_mmap_change_prot (void ) +{ +TST_EXP_FAIL_PTR_VOID (mmap (mem_addr, mem_size, +PROT_READ , +MAP_ANONYMOUS |MAP_PRIVATE |MAP_FIXED , +-1 ,0 ),EPERM ); +} + +static struct tcase { +void (*func_test)(void ); +int prot; +char *message; +} tcases[] = { + {test_mprotect,PROT_READ |PROT_WRITE ,"mprotect() availability" }, + {test_pkey_mprotect, PROT_READ | PROT_WRITE, "pkey_mprotect()availability"},
testcases/kernel/syscalls/mseal/mseal01.c new file +2
-0+ {test_madvise, PROT_READ, "madvise() availability"}, + {test_munmap, PROT_READ | PROT_WRITE, "munmap() availability fromchild"},
+ {test_mremap_resize,PROT_READ|PROT_WRITE, "mremap()address
move/resize"},
+ {test_mmap_change_prot,PROT_READ|PROT_WRITE, "mmap()protection
change"},testcases/kernel/syscalls/mseal/mseal01.c new file +18
-0+}; + +static void run (unsigned int n) +{ +/* the reason why we spawn a child is that mseal() will +* protect VMA until process will call _exit() +*/ +if (!SAFE_FORK ()) { +struct tcase *tc = &tcases[n]; + + mem_addr =SAFE_MMAP (NULL , mem_size, + tc->prot, +MAP_ANONYMOUS |MAP_PRIVATE , +-1 ,0 ); + +tst_res (TINFO ,"Testing %s" , tc->message); + + TST_EXP_PASS(sys_mseal(mem_addr + mem_offset,
mem_alignment));testcases/kernel/syscalls/mseal/mseal01.c new file +11
-0+ + tc->func_test (); +_exit (0 ); + } +} + +static void setup (void ) +{ + mem_alignment =getpagesize (); + mem_size = mem_alignment *MEMPAGES ; + mem_offset = mem_alignment *MEMSEAL ;
testcases/kernel/syscalls/mseal/mseal01.c line 153
Quoted 10 lines
testcases/kernel/syscalls/mseal/mseal01.c new file +7
-0+} + +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (tcases), + .setup = setup, + .min_kver ="6.10" ,
The minimal kernel version check is unnecessary.
Quoted 11 lines
testcases/kernel/syscalls/mseal/mseal01.c new file +3
-1+ .forks_child = 1, +}; + --2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/1 mseal01: handle more possible errnos when pkey_alloc gets fail
--- testcases/kernel/syscalls/mseal/mseal01.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
testcases/kernel/syscalls/mseal/mseal01.c +5 -11
@@ -25,6 +25,7 @@#include "tst_test.h" #include "lapi/syscalls.h" +#include "../pkeys/pkey.h" #define MEMPAGES 8#define MEMSEAL 2
@@ -46,25 +47,19 @@ static void test_mprotect(void)static void test_pkey_mprotect (void ) { -int ret;int pkey; - pkey =tst_syscall (__NR_pkey_alloc,0 ,0 ); -if (pkey ==-1 ) { -if (errno ==EINVAL ) -tst_brk (TCONF ,"pku is not supported on this CPU" ); +check_pkey_support (); -tst_brk (TBROK |TERRNO ,"pkey_alloc() error" ); - } + pkey =ltp_pkey_alloc (0 ,0 ); -TST_EXP_FAIL (tst_syscall (__NR_pkey_mprotect, +TST_EXP_FAIL (ltp_pkey_mprotect ( mem_addr, mem_size,PROT_NONE , pkey),EPERM ); - ret =tst_syscall (__NR_pkey_free, pkey); -if (ret ==-1 ) +if (ltp_pkey_free (pkey) ==-1 )tst_brk (TBROK |TERRNO ,"pkey_free() error" ); }
@@ -150,7 +145,6 @@ static struct tst_test test = { .test = run, .tcnt = ARRAY_SIZE(tcases), .setup = setup, - .min_kver = "6.10", .forks_child = 1, };
Signature · 5 lines
2.45.2 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH] mseal01: handle more possible errnos when pkey_alloc gets fail
Hi!
Thanks for helping on mseal01. I already submitted the patch, but we can apply a new patch on top of it without problems. Review below
Quoted 10 lines · Li Wang
Signed-off-by Li Wang <[email protected]>--- testcases/kernel/syscalls/mseal/mseal01.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)testcases/kernel/syscalls/mseal/mseal01.c +0
-0
testcases/kernel/syscalls/mseal/mseal01.c line 28
Quoted 4 lines
testcases/kernel/syscalls/mseal/mseal01.c +1
-0@@ -25,6 +25,7 @@#include "tst_test.h" #include "lapi/syscalls.h" +#include "../pkeys/pkey.h"
The problem with this approach is that testing suites won't be independent anymore.
We need to move all pkeys/pkey.h declarations inside the lapi/ folder, in order to use pkey syscalls from any testing suite.
Quoted 34 lines
testcases/kernel/syscalls/mseal/mseal01.c +4
-10#define MEMPAGES 8#define MEMSEAL 2@@ -46,25 +47,19 @@ static void test_mprotect(void)static void test_pkey_mprotect (void ) { -int ret;int pkey; - pkey =tst_syscall (__NR_pkey_alloc,0 ,0 ); -if (pkey ==-1 ) { -if (errno ==EINVAL ) -tst_brk (TCONF ,"pku is not supported on this CPU" ); +check_pkey_support (); -tst_brk (TBROK |TERRNO ,"pkey_alloc() error" ); - } + pkey =ltp_pkey_alloc (0 ,0 ); -TST_EXP_FAIL (tst_syscall (__NR_pkey_mprotect, +TST_EXP_FAIL (ltp_pkey_mprotect ( mem_addr, mem_size,PROT_NONE , pkey),EPERM ); - ret =tst_syscall (__NR_pkey_free, pkey); -if (ret ==-1 ) +if (ltp_pkey_free (pkey) ==-1 )tst_brk (TBROK |TERRNO ,"pkey_free() error" ); }
testcases/kernel/syscalls/mseal/mseal01.c static struct tst_test test = { · line 150
Quoted 7 lines
testcases/kernel/syscalls/mseal/mseal01.c +0
-1@@ -150,7 +145,6 @@ static struct tst_test test = { .test = run, .tcnt = ARRAY_SIZE(tcases), .setup = setup, - .min_kver = "6.10", .forks_child = 1, };
Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH] mseal01: handle more possible errnos when pkey_alloc gets fail
Quoted 19 lines · Andrea Cervesato via ltp
Hi!
Thanks for helping on mseal01. I already submitted the patch, but we can apply a new patch on top of it without problems.
Review belowReview
Quoted 6 lines · Li Wang
Signed-off-by Li Wang <[email protected]>--- testcases/kernel/syscalls/mseal/mseal01.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)(quoted diff) +0
-0
b/testcases/kernel/syscalls/mseal/mseal01.cQuoted 5 lines
index
eb2f4d588..2ff6ed119100644testcases/kernel/syscalls/mseal/mseal01.c +0
-0@@ -25,6 +25,7 @@
Quoted 8 lines
testcases/kernel/syscalls/mseal/mseal01.c line 28
Quoted 3 lines
testcases/kernel/syscalls/mseal/mseal01.c +1
-0#include "tst_test.h" #include "lapi/syscalls.h" +#include "../pkeys/pkey.h" The problem with this approach is that testing suites won't be independent anymore.
We need to move allpkeys/pkey.hdeclarations inside the lapi/ folder, in order to use pkey syscalls from any testing suite.
Sure, I will update my patch to include that. Thanks!
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp