arm64/fpsimd: ptrace: Fix inactive SVE and SSVE regsets
Subsystems (3)
PATCH 1/2 arm64/fpsimd: ptrace: Fix inactive SVE and SSVE regsets
sve_init_header_from_task() takes header as a pointer, so for the inactive mode
header->size = sizeof(header);
stores 8 rather than sizeof(struct user_sve_header), which is 16. Userspace sees an impossible size smaller than the header it describes.
The inactive-mode check in sve_get_common() compares header.size against sizeof(header) as well, but there header is a struct, so the check can never fire. Reads of NT_ARM_SVE and NT_ARM_SSVE for the inactive mode therefore still return the other mode's FPSIMD data, exactly the situation the check was added to prevent.
Fix the size, and make the check return the remaining membuf space instead of 0, which regset_get() would interpret as the entire (zero-filled) buffer having been populated.
---
Found by inspection while reviewing arch/arm64.
Confirmed in the generated code: building arch/arm64/kernel/ptrace.o with arm64 defconfig and gcc 15.2.0 and disassembling sve_get_common shows the inactive branch storing a literal 8 into header.size, and no compare against 16 anywhere - the compiler constant-folds the check away entirely and falls straight through to __fpr_get(). That is a machine-checked demonstration that the early return can never fire.
Runtime tested under QEMU TCG with -cpu max,sme=on. Before this change, PTRACE_GETREGSET(NT_ARM_SSVE) on a tracee outside streaming mode returned header.size == 8 and copied the 528-byte NT_PRFPREG payload. Afterwards, header.size is 16 and bytes in the userspace buffer after the header are left untouched.
arch/arm64/kernel/ptrace.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kernel/ptrace.c +2 -2
@@ -801,7 +801,7 @@ static void sve_init_header_from_task(struct user_sve_header *header,if (active) header->size =SVE_PT_SIZE (vq, header->flags);else - header->size =sizeof (header); + header->size =sizeof (*header); header->max_size =SVE_PT_SIZE (sve_vq_from_vl (header->max_vl),SVE_PT_REGS_SVE ); }
@@ -837,7 +837,7 @@ static int sve_get_common(struct task_struct *target,* from the other mode to userspace. */ if (header.size ==sizeof (header)) -return 0 ; +return to.left;switch ((header.flags &SVE_PT_REGS_MASK )) {case SVE_PT_REGS_FPSIMD :
Signature · 1 line
2.51.0
PATCH 2/2 kselftest/arm64: fp-ptrace: Fix checks for inactive SVE and SSVE regsets
The checks on the header size reported for the inactive regset of the NT_ARM_SVE/NT_ARM_SSVE pair compare it against sizeof(sve), but sve is a struct user_sve_header *, so this is 8 rather than the intended 16. The kernel carried the identical typo when filling in the header, so kernel and test agreed on the wrong value and the test passed.
Compare against sizeof(*sve), stop after the header checks for an inactive regset since it has no payload to compare, and prefill the buffer with a sentinel to verify that reading an inactive regset leaves everything after the header untouched. This also covers the getter's return value, which determines how many bytes ptrace copies back to userspace.
---
With the size comparison fixed, fp-ptrace fails against kernels that do not have the preceding regset fix.
tools/testing/selftests/arm64/fp/fp-ptrace.c | 47 +++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-)
tools/testing/selftests/arm64/fp/fp-ptrace.c +41 -6
@@ -65,6 +65,9 @@/* VL 128..2048 in powers of 2 */ #define MAX_NUM_VLS 5 +/* Sentinel for detecting buffer bytes the kernel did not write */ +#define REGSET_SENTINEL 0xa5 +/* * FPMR bits we can set without doing feature checks to see if values * are valid.
@@ -181,6 +184,20 @@ static bool compare_buffer(const char *name, void *out,return false; } +static bool buffer_is_filled (const void *buffer,size_t size, +unsigned char value) +{ +const unsigned char *bytes = buffer; +size_t i; + +for (i =0 ; i < size; i++) { +if (bytes[i] != value) +return false; + } + +return true; +} +struct test_config {int sve_vl_in;int sve_vl_expected;
@@ -401,6 +418,7 @@ static bool check_ptrace_values_sve(pid_t child, struct test_config *config)struct user_sve_header *sve;struct user_fpsimd_state *fpsimd;struct iovec iov; +size_t buf_size;int ret, vq;bool pass = true;
@@ -409,14 +427,16 @@ static bool check_ptrace_values_sve(pid_t child, struct test_config *config) vq =__sve_vq_from_vl (config->sve_vl_in); - iov.iov_len =SVE_PT_SVE_OFFSET +SVE_PT_SVE_SIZE (vq,SVE_PT_REGS_SVE ); - iov.iov_base =malloc (iov.iov_len); + buf_size =SVE_PT_SVE_OFFSET +SVE_PT_SVE_SIZE (vq,SVE_PT_REGS_SVE ); + iov.iov_len = buf_size; + iov.iov_base =malloc (buf_size);if (!iov.iov_base) {ksft_print_msg ("OOM allocating %lu byte SVE buffer\n" , iov.iov_len);return false; } +memset (iov.iov_base,REGSET_SENTINEL , buf_size); ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_SVE , &iov);if (ret !=0 ) { ksft_print_msg("Failed to read initial SVE: %s (%d)\n",
@@ -440,10 +460,16 @@ static bool check_ptrace_values_sve(pid_t child, struct test_config *config) }if (svcr_in &SVCR_SM ) { -if (sve->size !=sizeof (sve)) { +if (sve->size !=sizeof (*sve)) {ksft_print_msg ("NT_ARM_SVE reports data with PSTATE.SM\n" ); pass = false; } +if (!buffer_is_filled (iov.iov_base +sizeof (*sve), + buf_size -sizeof (*sve),REGSET_SENTINEL )) { +ksft_print_msg ("NT_ARM_SVE wrote beyond its header with PSTATE.SM\n" ); + pass = false; + } + goto out; }else {if (sve->size !=SVE_PT_SIZE (vq, sve->flags)) { ksft_print_msg("Mismatch in SVE header size: %d != %lu\n",
@@ -485,6 +511,7 @@ static bool check_ptrace_values_ssve(pid_t child, struct test_config *config)struct user_sve_header *sve;struct user_fpsimd_state *fpsimd;struct iovec iov; +size_t buf_size;int ret, vq;bool pass = true;
@@ -493,14 +520,16 @@ static bool check_ptrace_values_ssve(pid_t child, struct test_config *config) vq =__sve_vq_from_vl (config->sme_vl_in); - iov.iov_len =SVE_PT_SVE_OFFSET +SVE_PT_SVE_SIZE (vq,SVE_PT_REGS_SVE ); - iov.iov_base =malloc (iov.iov_len); + buf_size =SVE_PT_SVE_OFFSET +SVE_PT_SVE_SIZE (vq,SVE_PT_REGS_SVE ); + iov.iov_len = buf_size; + iov.iov_base =malloc (buf_size);if (!iov.iov_base) {ksft_print_msg ("OOM allocating %lu byte SSVE buffer\n" , iov.iov_len);return false; } +memset (iov.iov_base,REGSET_SENTINEL , buf_size); ret =ptrace (PTRACE_GETREGSET , child,NT_ARM_SSVE , &iov);if (ret !=0 ) { ksft_print_msg("Failed to read initial SSVE: %s (%d)\n",
@@ -523,10 +552,16 @@ static bool check_ptrace_values_ssve(pid_t child, struct test_config *config) }if (!(svcr_in &SVCR_SM )) { -if (sve->size !=sizeof (sve)) { +if (sve->size !=sizeof (*sve)) {ksft_print_msg ("NT_ARM_SSVE reports data without PSTATE.SM\n" ); pass = false; } +if (!buffer_is_filled (iov.iov_base +sizeof (*sve), + buf_size -sizeof (*sve),REGSET_SENTINEL )) { +ksft_print_msg ("NT_ARM_SSVE wrote beyond its header without PSTATE.SM\n" ); + pass = false; + } + goto out; }else {if (sve->size !=SVE_PT_SIZE (vq, sve->flags)) { ksft_print_msg("Mismatch in SSVE header size: %d != %lu\n",
Signature · 1 line
2.51.0
Will Deacon
maintainerQuoted 10 lines · Karl Mehltretter
sve_init_header_from_task()takes header as a pointer, so for the inactive modeheader->size = sizeof(header);stores 8 rather than
sizeof(struct user_sve_header), which is 16. Userspace sees an impossible size smaller than the header it describes.[...]
Applied selftest update to arm64 (for-next/selftests), thanks!
[2/2] kselftest/arm64: fp-ptrace: Fix checks for inactive SVE and SSVE regsets
https://git.kernel.org/arm64/c/bd290e7fc245
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev
Quoted 8 lines · Will Deacon
Quoted 2 lines · Karl Mehltretter
sve_init_header_from_task()takes header as a pointer, so for the inactive modeApplied selftest update to arm64 (for-next/selftests), thanks!
[2/2] kselftest/arm64: fp-ptrace: Fix checks for inactive SVE and SSVE regsets
https://git.kernel.org/arm64/c/bd290e7fc245
As noted in the changelog applying patch 1 as a fix and this only for -next blows up the fp-ptrace test rather impressively in any configuration which can generate headers only reports:
https://lava.sirena.org.uk/scheduler/job/3075737#L3231
for the release. Both patches really do need to go together.
Will Deacon
maintainerQuoted 10 lines · Mark Brown
Quoted 8 lines · Will Deacon
Quoted 2 lines · Karl Mehltretter
sve_init_header_from_task()takes header as a pointer, so for the inactive modeApplied selftest update to arm64 (for-next/selftests), thanks!
[2/2] kselftest/arm64: fp-ptrace: Fix checks for inactive SVE and SSVE regsets
https://git.kernel.org/arm64/c/bd290e7fc245
Quoted 7 lines
As noted in the changelog applying patch 1 as a fix and this only for -next blows up the fp-ptrace test rather impressively in any configuration which can generate headers only reports:
https://lava.sirena.org.uk/scheduler/job/3075737#L3231for the release. Both patches really do need to go together.
By "blows up" you mean the test fails, right? That's the same behaviour if anybody runs the test on a kernel without the fix. Both patches are merged in for-next/core.
Will
Quoted 6 lines · Will Deacon
Quoted 3 lines · Mark Brown
As noted in the changelog applying patch 1 as a fix and this only for -next blows up the fp-ptrace test rather impressively in any configuration which can generate headers only reports:
Quoted 7 lines
Quoted 3 lines
https://lava.sirena.org.uk/scheduler/job/3075737#L3231for the release. Both patches really do need to go together.
By "blows up" you mean the test fails, right? That's the same behaviour if anybody runs the test on a kernel without the fix. Both patches are merged in for-next/core.
Right, the test starts reporting 96 fails which doesn't look great in a dashboard. I'd misread the original branch as being merged as a fix but I see it's not been merged into the fixes branch, sorry for the confusion.
Will Deacon
maintainerQuoted 11 lines · Mark Brown
Quoted 9 lines · Will Deacon
Quoted 7 lines · Mark Brown
As noted in the changelog applying patch 1 as a fix and this only for -next blows up the fp-ptrace test rather impressively in any configuration which can generate headers only reports:
https://lava.sirena.org.uk/scheduler/job/3075737#L3231for the release. Both patches really do need to go together.
Quoted 8 lines
Quoted 3 lines
By "blows up" you mean the test fails, right? That's the same behaviour if anybody runs the test on a kernel without the fix. Both patches are merged in for-next/core.
Right, the test starts reporting 96 fails which doesn't look great in a dashboard. I'd misread the original branch as being merged as a fix but I see it's not been merged into the fixes branch, sorry for the confusion.
No problem, thank you for running the tests :)
Will
Will Deacon
maintainerQuoted 10 lines · Karl Mehltretter
sve_init_header_from_task()takes header as a pointer, so for the inactive modeheader->size = sizeof(header);stores 8 rather than
sizeof(struct user_sve_header), which is 16. Userspace sees an impossible size smaller than the header it describes.[...]
Applied fix to arm64 (for-next/ptrace), thanks!
[1/2] arm64/fpsimd: ptrace: Fix inactive SVE and SSVE regsets
https://git.kernel.org/arm64/c/c3f83d021162
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev