sigrelse01: Fix out-of-bounds read when invoking write()
PATCH 1/1 sigrelse01: Fix out-of-bounds read when invoking write()
The sigrelse01 test would invoke write(fd, msg, MAXMESG), where MAXMESG=512. However, msg is often as short as "ready", i.e., 6 bytes (5 bytes + \0).
This mismatch causes write() to read additional bytes outside the *msg buffer and write everything to the file descriptor. For example, the strace output of sigrelese01 contains the following:
write(6, "ready\0Unable to tell child to go"..., 512)
Fix the out-of-bounds read in sigrelse01 by invoking write() with the correct number of bytes to (read and) write by using strlen(). There is one case where sigrelese01 invoked write_pipe() not passing a
string: when the child sends sig_array to its parent process. We convert this case from write_pipe() to write() using the proper arguments. After doing so, the memcpy() of sig_array is no longer required.
We identified this issue on a CHERI [1] system, which provides fine-grained memory protection through architectural capabilities. Unlike traditional MMU-based protection, which would not detect this specific out-of-bounds access, CHERI precisely bounds memory regions. In sigrelse01's case, CHERI correctly identified that the 6-byte buffer containing "ready" was being overread. Consequently, this out-of-bounds read during the write() syscall would cause the Linux kernel to return -EFAULT, revealing this hidden bug.
1: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
--- testcases/kernel/syscalls/sigrelse/sigrelse01.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/sigrelse/sigrelse01.c +8 -4
@@ -486,12 +486,14 @@ static void child(void)* then PASS, otherwise FAIL. */ - if (exit_val == EXIT_OK) { - (void )memcpy (note, (char *)sig_array,sizeof (sig_array)); - } -/* send note to parent and exit */ -if (write_pipe (pipe_fd[1 ], note) <0 ) { +if (exit_val ==EXIT_OK ) { +if (write (pipe_fd[1 ], sig_array,sizeof (sig_array)) <0 ) { +tst_resm (TBROK ,"write() pipe failed. error:%d %s.\n" , errno,strerror (errno)); +exit (WRITE_BROK ); + } + } +else if (write_pipe (pipe_fd[1 ], note) <0 ) {/* * write_pipe() failed. Set exit value to WRITE_BROK to let * parent know what happened
@@ -622,7 +624,7 @@ static int write_pipe(int fd, char *msg)printf ("write_pipe: pid=%d, sending %s.\n" ,getpid (), msg);#endif - if (write(fd, msg, MAXMESG) < 0) { +if (write (fd, msg,strlen (msg) +1 ) <0 ) { (void )sprintf (mesg,"write() pipe failed. error:%d %s." , errno,strerror (errno)); -- 2.49.1
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
can you please check why this patch is not applying first?
https://github.com/linux-test-project/ltp/actions/runs/16225377946
- Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 3 lines · Andrea Cervesato
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Hi!
Hi Andrea,
Quoted 1 line
can you please check why this patch is not applying first?
Something added an additional line break along the way to you. The "diff --git a/testcases/…" should be one line instead of two. See my previous reply to Wei at
https://lists.linux.it/pipermail/ltp/2025-July/044345.html
- Florian
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 8 lines · Florian Schmaus
Quoted 3 lines · Andrea Cervesato
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Hi!
Hi Andrea,
Quoted 8 lines
Quoted 1 line
can you please check why this patch is not applying first?
Something added an additional line break along the way to you. The "diff --git a/testcases/…" should be one line instead of two. See my previous reply to Wei at
https://lists.linux.it/pipermail/ltp/2025-July/044345.html
- Florian
I see, I guess it will need to be taken into account, otherwise we can't pass the patch due to CI failure. Can you please send a new patch with the proper format?
Thanks,
Andrea
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/1 sigrelse01: Fix out-of-bounds read when invoking write()
The sigrelse01 test would invoke write(fd, msg, MAXMESG), where MAXMESG=512. However, msg is often as short as "ready", i.e., 6 bytes (5 bytes + \0).
This mismatch causes write() to read additional bytes outside the *msg buffer and write everything to the file descriptor. For example, the strace output of sigrelese01 contains the following:
write(6, "ready\0Unable to tell child to go"..., 512)
Fix the out-of-bounds read in sigrelse01 by invoking write() with the correct number of bytes to (read and) write by using strlen(). There is one case where sigrelese01 invoked write_pipe() not passing a
string: when the child sends sig_array to its parent process. We convert this case from write_pipe() to write() using the proper arguments. After doing so, the memcpy() of sig_array is no longer required.
We identified this issue on a CHERI [1] system, which provides fine-grained memory protection through architectural capabilities. Unlike traditional MMU-based protection, which would not detect this specific out-of-bounds access, CHERI precisely bounds memory regions. In sigrelse01's case, CHERI correctly identified that the 6-byte buffer containing "ready" was being overread. Consequently, this out-of-bounds read during the write() syscall would cause the Linux kernel to return -EFAULT, revealing this hidden bug.
1: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
--- testcases/kernel/syscalls/sigrelse/sigrelse01.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/sigrelse/sigrelse01.c +8 -6
@@ -486,12 +486,14 @@ static void child(void)* then PASS, otherwise FAIL. */ -if (exit_val ==EXIT_OK ) { - (void )memcpy (note, (char *)sig_array,sizeof (sig_array)); - } -/* send note to parent and exit */ -if (write_pipe (pipe_fd[1 ], note) <0 ) { +if (exit_val ==EXIT_OK ) { +if (write (pipe_fd[1 ], sig_array,sizeof (sig_array)) <0 ) { +tst_resm (TBROK ,"write() pipe failed. error:%d %s.\n" , errno,strerror (errno)); +exit (WRITE_BROK ); + } + } +else if (write_pipe (pipe_fd[1 ], note) <0 ) {/* * write_pipe() failed. Set exit value to WRITE_BROK to let * parent know what happened
@@ -622,7 +624,7 @@ static int write_pipe(int fd, char *msg)printf ("write_pipe: pid=%d, sending %s.\n" ,getpid (), msg);#endif -if (write (fd, msg,MAXMESG ) <0 ) { +if (write (fd, msg,strlen (msg) +1 ) <0 ) { (void )sprintf (mesg,"write() pipe failed. error:%d %s." , errno,strerror (errno));
Signature · 5 lines
2.49.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 41 lines · Florian Schmaus via ltp
The sigrelse01 test would invoke
write(fd, msg, MAXMESG), where MAXMESG=512. However, msg is often as short as "ready", i.e., 6 bytes (5 bytes + \0).This mismatch causes
write()to read additional bytes outside the *msg buffer and write everything to the file descriptor. For example, the strace output of sigrelese01 contains the following:write(6, "ready\0Unable to tell child to go"..., 512)Fix the out-of-bounds read in sigrelse01 by invoking
write()with the correct number of bytes to (read and) write by usingstrlen(). There is one case where sigrelese01 invokedwrite_pipe()not passing a
string: when the child sendssig_arrayto its parent process. We convert this case fromwrite_pipe()towrite()using the proper arguments. After doing so, thememcpy()ofsig_arrayis no longer required.We identified this issue on a CHERI [1] system, which provides fine-grained memory protection through architectural capabilities. Unlike traditional MMU-based protection, which would not detect this specific out-of-bounds access, CHERI precisely bounds memory regions. In sigrelse01's case, CHERI correctly identified that the 6-byte buffer containing "ready" was being overread. Consequently, this out-of-bounds read during the
write()syscall would cause the Linux kernel to return-EFAULT, revealing this hidden bug.1: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
Signed-off-by Florian Schmaus <[email protected]>--- testcases/kernel/syscalls/sigrelse/sigrelse01.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)testcases/kernel/syscalls/sigrelse/sigrelse01.c +0
-0@@ -486,12 +486,14 @@ static void child(void)* then PASS, otherwise FAIL. */
testcases/kernel/syscalls/sigrelse/sigrelse01.c static void child(void) · line 492
Quoted 10 lines
testcases/kernel/syscalls/sigrelse/sigrelse01.c +3
-5-if (exit_val ==EXIT_OK ) { - (void )memcpy (note, (char *)sig_array,sizeof (sig_array)); - } -/* send note to parent and exit */ -if (write_pipe (pipe_fd[1 ], note) <0 ) { +if (exit_val ==EXIT_OK ) { +if (write (pipe_fd[1 ], sig_array,sizeof (sig_array)) <0 ) { +tst_resm (TBROK ,"write() pipe failed. error:%d %s.\n" , errno,strerror (errno));
tst_resm will add "\n" at the end of string so you do not need add it.
Quoted 22 lines
testcases/kernel/syscalls/sigrelse/sigrelse01.c +5
-1+exit (WRITE_BROK ); + } + } +else if (write_pipe (pipe_fd[1 ], note) <0 ) {/* * write_pipe() failed. Set exit value to WRITE_BROK to let * parent know what happened @@ -622,7 +624,7 @@ static int write_pipe(int fd, char *msg)printf ("write_pipe: pid=%d, sending %s.\n" ,getpid (), msg);#endif -if (write (fd, msg,MAXMESG ) <0 ) { +if (write (fd, msg,strlen (msg) +1 ) <0 ) { (void )sprintf (mesg,"write() pipe failed. error:%d %s." , errno,strerror (errno));Signature · 5 lines
2.49.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp