Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Wed, 5 Oct 2016 17:56:40 -0400
From: Assaf Gordon <>
Subject: possible bug in getdtablesize() ?


It seems that musl's getdtablesize() returns incorrect value,
possibly using the "rlim_max" instead of "rlim_cur" value (just a guess, not sure about the actual reason).

The following code demonstrates: trying to use dup2(2) on a file-descriptor value that "should work".

   #include <unistd.h>
   #include <stdio.h>
   #include <err.h>

   int main(void)
     int fd = STDIN_FILENO;
     int maxfd = getdtablesize();
     printf("maxfd = %d\n", maxfd);

     int newfd = maxfd-1;

     int i = dup2(fd,newfd);
     if (i == -1)
       err(1,"dup2(%d, %d) failed", fd, newfd);

     if (i != newfd)
       errx(1,"dup2(%d, %d) returned %d", fd, newfd, i);

     return 0;

With glibc, it works:

     $ gcc -o dup2-glibc dup2-test.c
     $ strace -e prlimit64,getrlimit,dup2 ./dup2-glibc
     getrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=4*1024}) = 0
     maxfd = 1024
     dup2(0, 1023)                           = 1023
     +++ exited with 0 +++

With musl-1.1.15, it failsdue to wrong 'max-fd' value:

     $ musl-gcc -o dup2-musl dup2-test.c
     $ strace -e prlimit64,getrlimit,dup2 ./dup2-musl
     prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024, rlim_max=4*1024}) = 0
     maxfd = 4096
     dup2(0, 4095)                           = -1 EBADF (Bad file descriptor)
     dup2-musl: dup2(0, 4095) failed: Bad file descriptor
     +++ exited with 1 +++


     $ uname -svr
     Linux 3.13.0-88-generic #135-Ubuntu SMP Wed Jun 8 21:10:42 UTC 2016

     $ gcc --version
     gcc (GCC) 5.2.0

     $ ulimit -a
     open files                      (-n) 1024

and glibc-2.19.

  - assaf

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.