Skip to content

Commit d1ff0e2

Browse files
committed
tools/nolibc: avoid error in dup2() if old fd equals new fd
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: 30ca205 ("tools headers: Move the nolibc header from rcutorture to tools/include/nolibc/") Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Acked-by: Willy Tarreau <w@1wt.eu> Link: https://lore.kernel.org/r/20250820-nolibc-dup2-einval-v2-1-807185a45c56@linutronix.de Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
1 parent 850047b commit d1ff0e2

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

  • tools/include/nolibc

tools/include/nolibc/sys.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,19 @@ static __attribute__((unused))
238238
int sys_dup2(int old, int new)
239239
{
240240
#if defined(__NR_dup3)
241+
int ret, nr_fcntl;
242+
243+
#ifdef __NR_fcntl64
244+
nr_fcntl = __NR_fcntl64;
245+
#else
246+
nr_fcntl = __NR_fcntl;
247+
#endif
248+
249+
if (old == new) {
250+
ret = my_syscall2(nr_fcntl, old, F_GETFD);
251+
return ret < 0 ? ret : old;
252+
}
253+
241254
return my_syscall3(__NR_dup3, old, new, 0);
242255
#elif defined(__NR_dup2)
243256
return my_syscall2(__NR_dup2, old, new);

0 commit comments

Comments
 (0)