ioctl_pidfd02.c: fix clone3 EFAULT in 32-bit compat mode due to sign extension
PATCH 1/1 ioctl_pidfd02.c: fix clone3 EFAULT in 32-bit compat mode due to sign extension
When running 32-bit binaries on a 64-bit kernel (compat mode), the user stack is often mapped in the upper range of the 32-bit address space (e.g., 0xffxxxxxx).
Directly casting a 32-bit pointer to uint64_t for the args->pidfd field in struct clone_args can trigger sign extension if the pointer's MSB (Most Significant Bit) is 1. For example, a 32-bit user address 0xff80e0bc is incorrectly sign-extended to 0xfffffffffff80e0bc.
When the 64-bit kernel executes put_user(), it identifies this address as being in the 64-bit kernel canonical range rather than user space, leading to a failed access_ok() check and returning -EFAULT.
This patch fixes the issue by double-casting through uintptr_t to ensure zero-extension, keeping the address within the valid 32-bit user-space range from the kernel's perspective.
--- testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c +1 -1
@@ -27,7 +27,7 @@ static void run(unsigned int isolate)if (isolate) { args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c +1 -1
@@ -24,7 +24,7 @@ static void run(void)memset (args,0 ,sizeof (struct tst_clone_args )); args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c +1 -1
@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c +1 -1
@@ -36,7 +36,7 @@ static void run(void) info_invalid->dummy =1 ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c +1 -1
@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
Signature · 5 lines
2.52.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 9 lines · Wei Gao via ltp
When running 32-bit binaries on a 64-bit kernel (compat mode), the user stack is often mapped in the upper range of the 32-bit address space (e.g., 0xffxxxxxx).
Directly casting a 32-bit pointer to
uint64_tfor theargs->pidfdfield in structclone_argscan trigger sign extension if the pointer's MSB (Most Significant Bit) is 1. For example, a 32-bit user address0xff80e0bcis incorrectly sign-extended to0xfffffffffff80e0bc.
Quoted 7 lines
When the 64-bit kernel executes
put_user(), it identifies this address as being in the 64-bit kernel canonical range rather than user space, leading to a failedaccess_ok()check and returning-EFAULT.This patch fixes the issue by double-casting through
uintptr_tto ensure zero-extension, keeping the address within the valid 32-bit user-space range from the kernel's perspective.
The git commit message is unnecesarily complex. We can say:
Correct the 32-bit pointer u64 conversion for args->pidfd. Direct casting from a 32-bit pointer to a 64-bit integer was causing incorrect sign-extension. Using (uint64_t)(uintptr_t) ensures a valid zero-padded 64-bit address.
Quoted 67 lines
Signed-off-by Wei Gao <[email protected]>--- testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-)testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c +1
-1@@ -27,7 +27,7 @@ static void run(unsigned int isolate)if (isolate) { args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c +1
-1@@ -24,7 +24,7 @@ static void run(void)memset (args,0 ,sizeof (struct tst_clone_args )); args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c +1
-1@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c +1
-1@@ -36,7 +36,7 @@ static void run(void) info_invalid->dummy =1 ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c +0
-0
testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c static void run(void) · line 32
Quoted 8 lines
testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c +1
-1@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd = (uint64_t )(uintptr_t )&pidfd; args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
At this point I would define a macro as following and use it around the tests when it's needed:
#define TST_PTR_TO_UINT(x) ( (uint64_t)(uintptr_t)(x) )
Signature · 7 lines
Andrea Cervesato SUSE QE Automation Engineer Linux [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/1 ioctl_pidfd02.c: fix clone3 EFAULT in 32-bit compat mode due to sign extension
In reply to Wei Gao via ltpCorrect the 32-bit pointer u64 conversion for args->pidfd. Direct casting from a 32-bit pointer to a 64-bit integer was causing incorrect sign-extension. Using (uint64_t)(uintptr_t) ensures a valid zero-padded 64-bit address.
--- include/tst_common.h | 5 +++++ testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c | 2 +- 6 files changed, 10 insertions(+), 5 deletions(-)
include/tst_common.h +5 -0
@@ -86,4 +86,9 @@#define TST_TO_STR_ (s) #s#define TST_TO_STR (s) TST_TO_STR_(s) +/* +* TST_PTR_TO_UINT - Casts a pointer to a 64-bit unsigned integer. +*/ +#define TST_PTR_TO_UINT (x) ((uint64_t)(uintptr_t)(x)) +#endif /* TST_COMMON_H__ */
testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c +1 -1
@@ -27,7 +27,7 @@ static void run(unsigned int isolate)if (isolate) { args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c +1 -1
@@ -24,7 +24,7 @@ static void run(void)memset (args,0 ,sizeof (struct tst_clone_args )); args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c +1 -1
@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c +1 -1
@@ -36,7 +36,7 @@ static void run(void) info_invalid->dummy =1 ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c +1 -1
@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
Signature · 5 lines
2.52.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 15 lines
Signed-off-by Wei Gao <[email protected]>--- include/tst_common.h | 5 +++++ testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c | 2 +- 6 files changed, 10 insertions(+), 5 deletions(-)include/tst_common.h +0
-0
include/tst_common.h line 92
Quoted 7 lines
include/tst_common.h +4
-0@@ -86,4 +86,9 @@#define TST_TO_STR_ (s) #s#define TST_TO_STR (s) TST_TO_STR_(s) +/* +* TST_PTR_TO_UINT - Casts a pointer to a 64-bit unsigned integer. +*/ +#define TST_PTR_TO_UINT (x) ((uint64_t)(uintptr_t)(x))
There is no need to have the (uint64_t) cast here, the uintptr_t cast is enough to fix the problem.
Other than that:
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi,
Quoted 2 lines
There is no need to have the (
uint64_t) cast here, theuintptr_tcast is enough to fix the problem.
aggressive static analysis and future changes might require this cast, I wouldn't remove it just because compiler automatically cast to it. Also it makes the conversion explicit and more readable.
Signature · 7 lines
Andrea Cervesato SUSE QE Automation Engineer Linux [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 1/1 ioctl_pidfd02.c: fix clone3 EFAULT in 32-bit compat mode due to sign extension
In reply to Wei Gao via ltpCorrect the 32-bit pointer u64 conversion for args->pidfd. Direct casting from a 32-bit pointer to a 64-bit integer was causing incorrect sign-extension. Using (uint64_t)(uintptr_t) ensures a valid zero-padded 64-bit address.
--- include/tst_common.h | 5 +++++ testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c | 2 +- testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c | 2 +- 6 files changed, 10 insertions(+), 5 deletions(-)
include/tst_common.h +5 -0
@@ -86,4 +86,9 @@#define TST_TO_STR_ (s) #s#define TST_TO_STR (s) TST_TO_STR_(s) +/* +* TST_PTR_TO_UINT - Casts a pointer to a 64-bit unsigned integer. +*/ +#define TST_PTR_TO_UINT (x) ((uintptr_t)(x)) +#endif /* TST_COMMON_H__ */
testcases/kernel/syscalls/ioctl/ioctl_pidfd02.c +1 -1
@@ -27,7 +27,7 @@ static void run(unsigned int isolate)if (isolate) { args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd03.c +1 -1
@@ -24,7 +24,7 @@ static void run(void)memset (args,0 ,sizeof (struct tst_clone_args )); args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd04.c +1 -1
@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd05.c +1 -1
@@ -36,7 +36,7 @@ static void run(void) info_invalid->dummy =1 ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
testcases/kernel/syscalls/ioctl/ioctl_pidfd06.c +1 -1
@@ -26,7 +26,7 @@ static void run(void) info->mask =PIDFD_INFO_EXIT ; args->flags =CLONE_PIDFD |CLONE_NEWUSER |CLONE_NEWPID ; - args->pidfd = (uint64_t )&pidfd; + args->pidfd =TST_PTR_TO_UINT (&pidfd); args->exit_signal =SIGCHLD ; pid_child =SAFE_CLONE (args);
Signature · 5 lines
2.52.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Merged!
Signature · 7 lines
Andrea Cervesato SUSE QE Automation Engineer Linux [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp