diff mbox series

[2/4] syscalls/mprotect01: Invoke the syscall directly instead of the libc wrapper

Message ID 20220817133946.234985-3-tudor.cretu@arm.com
State Superseded
Headers show
Series syscalls: Fix various syscalls tests when compiled with Musl | expand

Commit Message

Tudor Cretu Aug. 17, 2022, 1:39 p.m. UTC
per POSIX: The mprotect() function shall change the access protections
to be that specified by prot for those whole pages containing any part
of the address space of the process starting at address addr and
continuing for len bytes.

Issue 6 of POSIX introduces: The implementation may require that addr
be a multiple of the page size as returned by sysconf().

Therefore it's not strictly required that addr is a multiple of the page
size. Some libcs (e.g. Musl) indeed don't have this requirement, so calling
the C standard library function doesn't fail in their case. As the
testsuite focuses on mprotect(2), the testcases should call the syscall
directly instead of the libc function.

Signed-off-by: Tudor Cretu <tudor.cretu@arm.com>
---
 testcases/kernel/syscalls/mprotect/mprotect01.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Cyril Hrubis Aug. 17, 2022, 2:29 p.m. UTC | #1
Hi!
> per POSIX: The mprotect() function shall change the access protections
> to be that specified by prot for those whole pages containing any part
> of the address space of the process starting at address addr and
> continuing for len bytes.
> 
> Issue 6 of POSIX introduces: The implementation may require that addr
> be a multiple of the page size as returned by sysconf().
> 
> Therefore it's not strictly required that addr is a multiple of the page
> size. Some libcs (e.g. Musl) indeed don't have this requirement, so calling
> the C standard library function doesn't fail in their case. As the
> testsuite focuses on mprotect(2), the testcases should call the syscall
> directly instead of the libc function.

So Musl mproctect() does clear a few bits of the start and rounds the
len. That sounds like a dangerous thing to do to be honest, since you
end up with a different region than you requested.

I guess that this patch is reasonable solution since the glibc
mprotect() seems to be just wrapper for the syscall.

> Signed-off-by: Tudor Cretu <tudor.cretu@arm.com>
> ---
>  testcases/kernel/syscalls/mprotect/mprotect01.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/testcases/kernel/syscalls/mprotect/mprotect01.c b/testcases/kernel/syscalls/mprotect/mprotect01.c
> index be4d982ea..aa4685258 100644
> --- a/testcases/kernel/syscalls/mprotect/mprotect01.c
> +++ b/testcases/kernel/syscalls/mprotect/mprotect01.c
> @@ -43,6 +43,7 @@
>  #include <stdlib.h>
>  #include <unistd.h>
>  #include "test.h"
> +#include "lapi/syscalls.h"
>  #include "safe_macros.h"
>  
>  char *TCID = "mprotect01";
> @@ -97,7 +98,7 @@ int main(int ac, char **av)
>  			if (TC[i].setupfunc != NULL)
>  				TC[i].setupfunc(&TC[i]);
>  
> -			TEST(mprotect(TC[i].addr, TC[i].len, TC[i].prot));
> +			TEST(tst_syscall(__NR_mprotect, TC[i].addr, TC[i].len, TC[i].prot));
>  
>  			if (TEST_RETURN != -1) {
>  				tst_resm(TFAIL, "call succeeded unexpectedly");
> -- 
> 2.25.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/mprotect/mprotect01.c b/testcases/kernel/syscalls/mprotect/mprotect01.c
index be4d982ea..aa4685258 100644
--- a/testcases/kernel/syscalls/mprotect/mprotect01.c
+++ b/testcases/kernel/syscalls/mprotect/mprotect01.c
@@ -43,6 +43,7 @@ 
 #include <stdlib.h>
 #include <unistd.h>
 #include "test.h"
+#include "lapi/syscalls.h"
 #include "safe_macros.h"
 
 char *TCID = "mprotect01";
@@ -97,7 +98,7 @@  int main(int ac, char **av)
 			if (TC[i].setupfunc != NULL)
 				TC[i].setupfunc(&TC[i]);
 
-			TEST(mprotect(TC[i].addr, TC[i].len, TC[i].prot));
+			TEST(tst_syscall(__NR_mprotect, TC[i].addr, TC[i].len, TC[i].prot));
 
 			if (TEST_RETURN != -1) {
 				tst_resm(TFAIL, "call succeeded unexpectedly");