* [PATCH v2] tools/nolibc: avoid error in dup2() if old fd equals new fd
@ 2025-08-20 8:29 Thomas Weißschuh
2025-08-23 10:18 ` Willy Tarreau
0 siblings, 1 reply; 2+ messages in thread
From: Thomas Weißschuh @ 2025-08-20 8:29 UTC (permalink / raw)
To: Willy Tarreau, Thomas Weißschuh, Paul E. McKenney
Cc: linux-kernel, Thomas Weißschuh
dup2() allows both 'old' and 'new' to have the same value, which dup3()
does not. If libc dup2() is implemented through the dup3() system call,
then it would incorrectly fail in this case.
Avoid the error by handling old == new explicitly.
Fixes: 30ca20517ac1 ("tools headers: Move the nolibc header from rcutorture to tools/include/nolibc/")
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
Changes in v2:
- Correctly handle the case where 'old' is invalid
- Link to v1: https://lore.kernel.org/r/20250819-nolibc-dup2-einval-v1-1-d6c8308cdc50@linutronix.de
---
tools/include/nolibc/sys.h | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 295e71d34abadb7f9c7ca995012b4395b6830975..8aed6c5f00a9186b943e388f9d5f702412c77dd2 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -238,6 +238,13 @@ static __attribute__((unused))
int sys_dup2(int old, int new)
{
#if defined(__NR_dup3)
+ int ret;
+
+ if (old == new) {
+ ret = my_syscall2(__NR_fcntl, old, F_GETFD);
+ return ret < 0 ? ret : old;
+ }
+
return my_syscall3(__NR_dup3, old, new, 0);
#elif defined(__NR_dup2)
return my_syscall2(__NR_dup2, old, new);
---
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250819-nolibc-dup2-einval-825bfd77de7a
Best regards,
--
Thomas Weißschuh <thomas.weissschuh@linutronix.de>
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] tools/nolibc: avoid error in dup2() if old fd equals new fd
2025-08-20 8:29 [PATCH v2] tools/nolibc: avoid error in dup2() if old fd equals new fd Thomas Weißschuh
@ 2025-08-23 10:18 ` Willy Tarreau
0 siblings, 0 replies; 2+ messages in thread
From: Willy Tarreau @ 2025-08-23 10:18 UTC (permalink / raw)
To: Thomas Weißschuh
Cc: Thomas Weißschuh, Paul E. McKenney, linux-kernel
On Wed, Aug 20, 2025 at 10:29:27AM +0200, Thomas Weißschuh wrote:
> dup2() allows both 'old' and 'new' to have the same value, which dup3()
> does not. If libc dup2() is implemented through the dup3() system call,
> then it would incorrectly fail in this case.
>
> Avoid the error by handling old == new explicitly.
>
> Fixes: 30ca20517ac1 ("tools headers: Move the nolibc header from rcutorture to tools/include/nolibc/")
> Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Good, LGTM!
Acked-by: Willy Tarreau <w@1wt.eu>
Willy
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-08-23 10:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-20 8:29 [PATCH v2] tools/nolibc: avoid error in dup2() if old fd equals new fd Thomas Weißschuh
2025-08-23 10:18 ` Willy Tarreau
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®