selftests/mm: fix ptrace PEEKDATA check in memfd_secret test
Subsystems (3)
PATCH 1/1 selftests/mm: fix ptrace PEEKDATA check in memfd_secret test
From: Hongfu Li <[email protected]>
try_ptrace() treats PTRACE_PEEKDATA return value as a boolean check. A successful read returns non-zero data (memory filled with 0x55), causing the test to incorrectly report PASS when secret memory protection is broken.
Check the return value against -1 instead. The test should only pass when PTRACE_PEEKDATA fails, which means secret memory protection works.
--- tools/testing/selftests/mm/memfd_secret.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/memfd_secret.c +1 -1
@@ -145,7 +145,7 @@ static void try_ptrace(int fd, int pipefd[2])exit (KSFT_FAIL ); } -if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 )) +if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 ) ==-1 )exit (KSFT_PASS );exit (KSFT_FAIL );
Signature · 1 line
2.54.0
Quoted 7 lines · Hongfu Li
From: Hongfu Li <[email protected]>
try_ptrace()treatsPTRACE_PEEKDATAreturn value as a boolean check. A successful read returns non-zero data (memory filled with0x55), causing the test to incorrectly report PASS when secret memory protection is broken.
Quoted 5 lines
Check the return value against -1 instead. The test should only pass when
PTRACE_PEEKDATAfails, which means secret memory protection works.Fixes 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)")Signed-off-by Hongfu Li <[email protected]>
Quoted 21 lines
--- tools/testing/selftests/mm/memfd_secret.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/memfd_secret.c +1
-1@@ -145,7 +145,7 @@ static void try_ptrace(int fd, int pipefd[2])exit (KSFT_FAIL ); } -if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 )) +if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 ) ==-1 )exit (KSFT_PASS );exit (KSFT_FAIL );Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours, Mike.
Quoted 11 lines · Hongfu Li
From: Hongfu Li <[email protected]>
try_ptrace()treatsPTRACE_PEEKDATAreturn value as a boolean check. A successful read returns non-zero data (memory filled with0x55), causing the test to incorrectly report PASS when secret memory protection is broken.Check the return value against -1 instead. The test should only pass when
PTRACE_PEEKDATAfails, which means secret memory protection works.Fixes 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)")
Not sure if a fixes is warranted? But I also definitely don't think a backport is in any case in case :P
Quoted 1 line
Signed-off-by Hongfu Li <[email protected]>
The change LGTM afaict. Though I think a comment should be added. With that addressed:
Quoted 9 lines
--- tools/testing/selftests/mm/memfd_secret.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/memfd_secret.c +0
-0
tools/testing/selftests/mm/memfd_secret.c static void try_ptrace(int fd, int pipefd[2]) · line 149
Quoted 6 lines
tools/testing/selftests/mm/memfd_secret.c +1
-1@@ -145,7 +145,7 @@ static void try_ptrace(int fd, int pipefd[2])exit (KSFT_FAIL ); } -if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 )) +if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 ) ==-1 )exit (KSFT_PASS );
From https://man7.org/linux/man-pages/man2/ptrace.2.html#RETURN_VALUE :
On success, the PTRACE_PEEK* operations return the requested data (but see NOTES)...
On error, all operations return -1, ...
...
PTRACE_PEEKTEXT
PTRACE_PEEKDATA
Read a word at the address addr in the tracee's memory, returning the word as the result of the ptrace() call. Linux does not have separate text and data address spaces, so these two operations are currently equivalent. (data is ignored; but see NOTES.)
OK so we expect this to fail as otherwise that'd be a violation of secretmem.
Feels like maybe we should add a comment to that effect? :)
Quoted 5 lines
tools/testing/selftests/mm/memfd_secret.c +0
-1exit (KSFT_FAIL ); --2.54.0
--
Cheers, Lorenzo
Quoted 53 lines · Lorenzo Stoakes (ARM)
Quoted 11 lines · Hongfu Li
From: Hongfu Li <[email protected]>
try_ptrace()treatsPTRACE_PEEKDATAreturn value as a boolean check. A successful read returns non-zero data (memory filled with0x55), causing the test to incorrectly report PASS when secret memory protection is broken.Check the return value against -1 instead. The test should only pass when
PTRACE_PEEKDATAfails, which means secret memory protection works.Fixes 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)")Not sure if a fixes is warranted? But I also definitely don't think a backport is in any case in case :P
Quoted 1 line
Signed-off-by Hongfu Li <[email protected]>The change LGTM afaict. Though I think a comment should be added. With that addressed:
Quoted 9 lines
--- tools/testing/selftests/mm/memfd_secret.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/memfd_secret.c +0
-0tools/testing/selftests/mm/memfd_secret.c static void try_ptrace(int fd, int pipefd[2]) · line 149
Quoted 6 lines
tools/testing/selftests/mm/memfd_secret.c +1
-1@@ -145,7 +145,7 @@ static void try_ptrace(int fd, int pipefd[2])exit (KSFT_FAIL ); } -if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 )) +if (ptrace (PTRACE_PEEKDATA , ppid, mem,0 ) ==-1 )exit (KSFT_PASS );From https://man7.org/linux/man-pages/man2/ptrace.2.html#RETURN_VALUE :
On success, the PTRACE_PEEK* operations return the requested data (but see NOTES)...
On error, all operations return -1, ...
...
PTRACE_PEEKTEXT
PTRACE_PEEKDATA
Read a word at the address addr in the tracee's memory, returning the word as the result of theptrace()call. Linux does not have separate text and data address spaces, so these two operations are currently equivalent. (data is ignored; but see NOTES.)
Quoted 3 lines
OK so we expect this to fail as otherwise that'd be a violation of secretmem.
Feels like maybe we should add a comment to that effect? :)
Thanks for the review, and agreed on the comment: the test only passes when PEEKDATA fails, which is easy to misread, so I'll make that explicit in v2:
tools/testing/selftests/mm/memfd_secret.c +0 -0
@@ -145,7 +145,8 @@ static void try_ptrace(int fd, int pipefd[2])
exit(KSFT_FAIL);
}
- if (ptrace(PTRACE_PEEKDATA, ppid, mem, 0))
+ /* PEEKDATA on secret memory must fail, else protection is broken. */ + if (ptrace(PTRACE_PEEKDATA, ppid, mem, 0) == -1) exit(KSFT_PASS);
I'll also drop the Fixes: tag in v2 since a backport is not intended.
Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/mm/memfd_secret.c +0
-1exit (KSFT_FAIL ); --2.54.0
--
Cheers, Lorenzo
Signature · 2 lines
Best regards, Hongfu