security/dirtyc0w_shmem: Add new test for CVE-2022-2590
PATCH v2 1/1 security/dirtyc0w_shmem: Add new test for CVE-2022-2590
This test is based on the original reproducer [1] written by me. The LTP adaption is implemented similar to the original dirtyc0w test.
Try handling absence of userfaultfd minor fault mode support for shmem gracefully.
[1] https://seclists.org/oss-sec/2022/q3/128
---
v1 -> v2:
* Use proper [Description] comment
* Make "child_early_exit" variable volatile as it's modified from a signal handler
* Use SAFE_FILE_PRINTF()+SAFE_CHMOD()
* Add ".needs_tmpdir" flag
--- runtest/cve | 1 + runtest/syscalls | 1 + .../kernel/security/dirtyc0w_shmem/.gitignore | 2 + .../kernel/security/dirtyc0w_shmem/Makefile | 8 + .../security/dirtyc0w_shmem/dirtyc0w_shmem.c | 121 +++++++++ .../dirtyc0w_shmem/dirtyc0w_shmem_child.c | 241 ++++++++++++++++++ 6 files changed, 374 insertions(+) create mode 100644 testcases/kernel/security/dirtyc0w_shmem/.gitignore create mode 100644 testcases/kernel/security/dirtyc0w_shmem/Makefile create mode 100644 testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c create mode 100644 testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c
runtest/cve +1 -0
@@ -73,5 +73,6 @@ cve-2021-22555 setsockopt08 -i 100 cve-2021-26708 vsock01 cve-2021-22600 setsockopt09 cve-2022-0847 dirtypipe +cve-2022-2590 dirtyc0w_shmem # Tests below may cause kernel memory leak cve-2020-25704 perf_event_open03
runtest/syscalls +1 -0
@@ -1036,6 +1036,7 @@ process_vm_writev02 process_vm_writev02 prot_hsymlinks prot_hsymlinks dirtyc0w dirtyc0w +dirtyc0w_shmem dirtyc0w_shmem dirtypipe dirtypipe pselect01 pselect01
testcases/kernel/security/dirtyc0w_shmem/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +dirtyc0w_shmem +dirtyc0w_shmem_child
testcases/kernel/security/dirtyc0w_shmem/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2016 Linux Test Project + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +dirtyc0w_shmem_child: CFLAGS+=-pthread +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c new file +121 -0
121 lines not on this page · raw message
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c new file +241 -0
241 lines not on this page · raw message
Signature · 5 lines
2.38.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi David,
Quoted 6 lines
This test is based on the original reproducer [1] written by me. The LTP adaption is implemented similar to the original dirtyc0w test.
Try handling absence of userfaultfd minor fault mode support for shmem gracefully.
Thanks a lot, merged.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi,
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c line 72
Quoted 5 lines · David Hildenbrand
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c new file +5
-0+ pid =SAFE_FORK (); +if (!pid) { +SAFE_SETGID (nobody_gid); +SAFE_SETUID (nobody_uid); +SAFE_EXECLP ("dirtyc0w_shmem_child" ,"dirtyc0w_shmem_child" ,NULL );
Manpage says that the last argument of execlp() must be (char*)NULL, including the explicit typecast.
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c line 241
Quoted 4 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c new file +4
-0+#else /* UFFD_FEATURE_MINOR_SHMEM */ +#include "tst_test.h" +TST_TEST_TCONF ("System does not have userfaultfd minor fault support for shmem" ); +#endif /* UFFD_FEATURE_MINOR_SHMEM */
When the child exits through this TST_TEST_TCONF(), the TST_CHECKPOINT_WAIT() in parent will fail. The parent process should not even fork() when UFFD_FEATURE_MINOR_SHMEM is not defined in config.h.
Signature · 11 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Martin,
Quoted 11 lines
Hi,
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c line 72
Quoted 5 lines · David Hildenbrand
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c new file +5
-0+ pid =SAFE_FORK (); +if (!pid) { +SAFE_SETGID (nobody_gid); +SAFE_SETUID (nobody_uid); +SAFE_EXECLP ("dirtyc0w_shmem_child" ,"dirtyc0w_shmem_child" ,NULL );Manpage says that the last argument of
execlp()must be (char*)NULL, including the explicit typecast.
I was too fast here (already merged).
You're right, although we use execlp() or SAFE_EXECLP with just NULL on many places, including testing execlp() itself in execlp01.c. I guess we should fix that.
Quoted 8 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c line 241
Quoted 4 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c new file +4
-0+#else /* UFFD_FEATURE_MINOR_SHMEM */ +#include "tst_test.h" +TST_TEST_TCONF ("System does not have userfaultfd minor fault support for shmem" ); +#endif /* UFFD_FEATURE_MINOR_SHMEM */ When the child exits through this
TST_TEST_TCONF(), theTST_CHECKPOINT_WAIT()in parent will fail. The parent process should not evenfork()whenUFFD_FEATURE_MINOR_SHMEMis not defined inconfig.h.
+1, this should be fixed. Please let us know if you don't have time to send fix yourself.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 13 lines · Petr Vorel
Hi Martin,
Quoted 11 lines
Hi,
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c line 72
Quoted 5 lines · David Hildenbrand
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c new file +5
-0+ pid =SAFE_FORK (); +if (!pid) { +SAFE_SETGID (nobody_gid); +SAFE_SETUID (nobody_uid); +SAFE_EXECLP ("dirtyc0w_shmem_child" ,"dirtyc0w_shmem_child" ,NULL );Manpage says that the last argument of
execlp()must be (char*)NULL, including the explicit typecast.
Quoted 5 lines
I was too fast here (already merged).
You're right, although we use
execlp()orSAFE_EXECLPwith just NULL on many places, including testingexeclp()itself inexeclp01.c. I guess we should fix that.
See my other mail, it's the case on all instances that pass NULL (and I don't really see the need to do this when working with NULL.
Quoted 11 lines
Quoted 9 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c line 241
Quoted 5 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c new file +4
-0+#else /* UFFD_FEATURE_MINOR_SHMEM */ +#include "tst_test.h" +TST_TEST_TCONF ("System does not have userfaultfd minor fault support for shmem" ); +#endif /* UFFD_FEATURE_MINOR_SHMEM */ When the child exits through this
TST_TEST_TCONF(), theTST_CHECKPOINT_WAIT()in parent will fail. The parent process should not evenfork()whenUFFD_FEATURE_MINOR_SHMEMis not defined inconfig.h.+1, this should be fixed. Please let us know if you don't have time to send fix yourself.
Let me know if I should send the fixup as an official, separate patch.
Thanks all!
Signature · 7 lines
Thanks, David / dhildenb -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 1 line · David Hildenbrand
See my other mail, it's the case on all instances that pass NULL (and I don't really see the need to do this when working with NULL.
NULL may be defined as simple integer 0. When int is 32bit and pointers 64bit, this will cause trouble in variadic functions such as execlp(). You do not need to remind us that LTP tests have lots of bugs, we know.
Signature · 11 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 7 lines · Martin Doucha
Quoted 2 lines · David Hildenbrand
See my other mail, it's the case on all instances that pass NULL (and I don't really see the need to do this when working with NULL.
NULL may be defined as simple integer 0. When int is 32bit and pointers 64bit, this will cause trouble in variadic functions such as
execlp(). You do not need to remind us that LTP tests have lots of bugs, we know.
I can send a fixup patch for all these instances.
Signature · 7 lines
Thanks, David / dhildenb -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 10 lines · Martin Doucha
Quoted 7 lines · David Hildenbrand
Quoted 2 lines
See my other mail, it's the case on all instances that pass NULL (and I don't really see the need to do this when working with NULL.
NULL may be defined as simple integer 0. When int is 32bit and pointers 64bit, this will cause trouble in variadic functions such as
execlp(). You do not need to remind us that LTP tests have lots of bugs, we know.I can send a fixup patch for all these instances.
Thank you!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 5 lines · Petr Vorel
Quoted 4 lines · Martin Doucha
Quoted 3 lines · David Hildenbrand
Quoted 2 lines
See my other mail, it's the case on all instances that pass NULL (and I don't really see the need to do this when working with NULL.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
NULL may be defined as simple integer 0. When int is 32bit and pointers 64bit, this will cause trouble in variadic functions such as
execlp(). You do not need to remind us that LTP tests have lots of bugs, we know.I can send a fixup patch for all these instances.
Thank you!
The list of SAFE_EXEC* was fairly short, now I discovered manual execl* usage... let me get a patch ready to fix all these :)
Signature · 7 lines
Thanks, David / dhildenb -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 2 lines · Martin Doucha
Hi,
Hi Martin,
Quoted 9 lines · David Hildenbrand
Review
Quoted 5 lines
(quoted diff) +5
-0+ pid = SAFE_FORK(); + if (!pid) { + SAFE_SETGID(nobody_gid); + SAFE_SETUID(nobody_uid); + SAFE_EXECLP("dirtyc0w_shmem_child", "dirtyc0w_shmem_child", NULL);Manpage says that the last argument of
execlp()must be (char*)NULL, including the explicit typecast.
$ git grep SAFE_EXECLP | grep NULL
testcases/kernel/connectors/pec/event_generator.c: SAFE_EXECLP(prog_name, prog_name, "-e", "exec", "-n", buf, NULL);
testcases/kernel/security/dirtyc0w/dirtyc0w.c: SAFE_EXECLP("dirtyc0w_child", "dirtyc0w_child",NULL);
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c: SAFE_EXECLP("dirtyc0w_shmem_child", "dirtyc0w_shmem_child", NULL);
testcases/kernel/syscalls/pipe2/pipe2_02.c: SAFE_EXECLP(TESTBIN, TESTBIN, buf, NULL);
testcases/kernel/syscalls/setpgid/setpgid03.c: SAFE_EXECLP(TEST_APP, TEST_APP, NULL);
testcases/kernel/syscalls/setrlimit/setrlimit04.c: SAFE_EXECLP("/bin/true", "/bin/true", NULL);Quoted 9 lines
Review
Quoted 5 lines
(quoted diff) +4
-0+#else /* UFFD_FEATURE_MINOR_SHMEM */ +#include "tst_test.h" +TST_TEST_TCONF("System does not have userfaultfd minor fault support for shmem"); +#endif /* UFFD_FEATURE_MINOR_SHMEM */When the child exits through this
TST_TEST_TCONF(), theTST_CHECKPOINT_WAIT()in parent will fail. The parent process should not evenfork()whenUFFD_FEATURE_MINOR_SHMEMis not defined inconfig.h.
Thanks, you're right, that's the remaining case that doesn't make the checkpoint happy.
I tried handling TCONF in the parent and it got all very ugly. The following should do the trick:
From fb13df0ea9e477b8e903d3ef4df317e548200a86 Mon Sep 17 00:00:00 2001
From: David Hildenbrand <[email protected]>
Date: Fri, 25 Nov 2022 05:12:26 -0500
Subject: [PATCH v1] security/dirtyc0w_shmem: Fix test result when
UFFD_FEATURE_MINOR_SHMEM is missing
We have make the checkpoint happy, otherwise our parent process will run into a timeout.
--- .../security/dirtyc0w_shmem/dirtyc0w_shmem_child.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c +8 -4
@@ -24,12 +24,12 @@#include <linux/userfaultfd.h> #endif -#ifdef UFFD_FEATURE_MINOR_SHMEM -#define TST_NO_DEFAULT_MAIN #include "tst_test.h" #include "tst_safe_macros.h" #include "tst_safe_pthread.h" + +#ifdef UFFD_FEATURE_MINOR_SHMEM #include "lapi/syscalls.h" #define TMP_DIR "tmp_dirtyc0w_shmem"
@@ -236,6 +236,10 @@ int main(void) return 0; }#else /* UFFD_FEATURE_MINOR_SHMEM */ -#include "tst_test.h" -TST_TEST_TCONF ("System does not have userfaultfd minor fault support for shmem" ); +int main (void ) +{ +tst_reinit (); +TST_CHECKPOINT_WAKE (0 ); +tst_brk (TCONF ,"System does not have userfaultfd minor fault support for shmem" ); +}#endif /* UFFD_FEATURE_MINOR_SHMEM */
Signature · 27 lines
2.38.1 Which gives me: # ./dirtyc0w_shmem tst_test.c:1552: TINFO: Timeout per run is 0h 02m 30s dirtyc0w_shmem_child.c:243: TCONF: System does not have userfaultfd minor fault support for shmem dirtyc0w_shmem.c:95: TINFO: Early child process exit Summary: passed 0 failed 0 broken 0 skipped 1 warnings 0 -- Thanks, David / dhildenb -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 8 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c line 72
Quoted 5 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem.c new file +5
-0+ pid =SAFE_FORK (); +if (!pid) { +SAFE_SETGID (nobody_gid); +SAFE_SETUID (nobody_uid); +SAFE_EXECLP ("dirtyc0w_shmem_child" ,"dirtyc0w_shmem_child" ,NULL );Manpage says that the last argument of
execlp()must be (char*)NULL, including the explicit typecast.
I wonder if this is actually valid. Do you know in which way is different ((char*)0) from ((void*)0) ?
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 5 lines · Cyril Hrubis
Quoted 2 lines
Manpage says that the last argument of
execlp()must be (char*)NULL, including the explicit typecast.I wonder if this is actually valid. Do you know in which way is different ((char*)0) from ((void*)0) ?
I think the point is that there's no guarantee that NULL will actually be defined as (void*)0. It can be defined as a plain integer instead. And it *IS* defined as plain integer when the header is #included in C++.
Signature · 11 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 3 lines
I think the point is that there's no guarantee that NULL will actually be defined as (void*)0. It can be defined as a plain integer instead. And it *IS* defined as plain integer when the header is #included in C++.
NULL is required to be 0 cast to void* in POSIX.
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stddef.h.html
So at least on POSIX-like systems in C NULL must be ((void*)0)
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c line 156
Quoted 4 lines · David Hildenbrand
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c new file +4
-0+ uffdio_api.api =UFFD_API ; + uffdio_api.features =UFFD_FEATURE_MINOR_SHMEM ; +TEST (ioctl (uffd,UFFDIO_API , &uffdio_api)); +if (TST_RET <0 ) {
One more thing, checking just the ioctl() return value here is not enough. You need to check that uffdio_api.features still includes the UFFD_FEATURE_MINOR_SHMEM flag after the call. PPC64 does not seem to support it on kernel 5.14 and the ioctl() still succeeds there.
Signature · 11 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Martin, David,
Quoted 10 lines · David Hildenbrand
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c line 156
Quoted 4 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c new file +4
-0+ uffdio_api.api =UFFD_API ; + uffdio_api.features =UFFD_FEATURE_MINOR_SHMEM ; +TEST (ioctl (uffd,UFFDIO_API , &uffdio_api)); +if (TST_RET <0 ) {One more thing, checking just the
ioctl()return value here is not enough. You need to check that uffdio_api.features still includes theUFFD_FEATURE_MINOR_SHMEMflag after the call. PPC64 does not seem to support it on kernel 5.14 and theioctl()still succeeds there.
Very good catch, thanks!
David, please send official patch (speedups merging). Thanks a lot!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 12 lines · Petr Vorel
Hi Martin, David,
Quoted 10 lines · David Hildenbrand
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c line 156
Quoted 4 lines
testcases/kernel/security/dirtyc0w_shmem/dirtyc0w_shmem_child.c new file +4
-0+ uffdio_api.api =UFFD_API ; + uffdio_api.features =UFFD_FEATURE_MINOR_SHMEM ; +TEST (ioctl (uffd,UFFDIO_API , &uffdio_api)); +if (TST_RET <0 ) {One more thing, checking just the
ioctl()return value here is not enough. You need to check that uffdio_api.features still includes theUFFD_FEATURE_MINOR_SHMEMflag after the call. PPC64 does not seem to support it on kernel 5.14 and theioctl()still succeeds there.
Quoted 3 lines
Very good catch, thanks!
David, please send official patch (speedups merging). Thanks a lot!
Interesting, thanks for testing! Will fix that as well and send a combined patch.
Signature · 7 lines
Thanks, David / dhildenb -- Mailing list info: https://lists.linux.it/listinfo/ltp