<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "http://sourceware.org/bugzilla/bugzilla.dtd">

<bugzilla version="4.0.10"
          urlbase="http://sourceware.org/bugzilla/"
          
          maintainer="overseers@sourceware.org"
>

    <bug>
          <bug_id>10352</bug_id>
          
          <creation_ts>2009-06-30 20:06:00 +0000</creation_ts>
          <short_desc>no protection against using fd_set with fd&gt;1024</short_desc>
          <delta_ts>2011-05-03 20:05:03 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>glibc</product>
          <component>libc</component>
          <version>unspecified</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>WONTFIX</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Kees Cook">kees</reporter>
          <assigned_to name="Ulrich Drepper">drepper.fsp</assigned_to>
          <cc>bressers</cc>
    
    <cc>bugdal</cc>
    
    <cc>glibc-bugs</cc>
          <cf_gcchost></cf_gcchost>
          <cf_gcctarget></cf_gcctarget>
          <cf_gccbuild></cf_gccbuild>
          

      

      

      

          <long_desc isprivate="0">
            <commentid>36962</commentid>
            <who name="Kees Cook">kees</who>
            <bug_when>2009-06-30 20:06:20 +0000</bug_when>
            <thetext>When a program using select() starts tracking file descriptors above 1024,
the fd_set vector (128 bytes) will overflow, writing to whatever is
beyond the vector, leading to stack/heap corruption.

This is a known, old, issue.  Examples:
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2002-1500
http://marc.info/?l=bugtraq&amp;m=110660879328901

It is perfectly valid to use select() on a user-allocated vector that IS large
enough to handle the the fds being tracked, but it seems that glibc should take
some proactive measures to help applications that are not checking FD_SETSIZE.

It was pointed out that SSH, e.g. uses this to work around the issue:
fdset = (fd_set *)xcalloc(howmany(maxfd + 1, NFDBITS)

Some ideas could be to flag FD_ZERO as dangerous?  Or to check sizeof(...) on
select() inputs?

I would love to see a reasonable approach to protecting applications that aren&apos;t
prepared for RLIMIT_NOFILE to be &gt;1024.  :)

Also being tracked here: https://bugs.launchpad.net/bugs/386558
Thanks!</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>39379</commentid>
            <who name="Ulrich Drepper">drepper.fsp</who>
            <bug_when>2009-10-30 06:30:54 +0000</bug_when>
            <thetext>select is what it is.  Every program using it must be considered buggy.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>48483</commentid>
            <who name="Josh Bressers">bressers</who>
            <bug_when>2011-05-02 15:19:21 +0000</bug_when>
            <thetext>Ulrich,

Could I convince you to revisit this bug? This issue is currently being hit by some enterprise sized daemons (lots of open fds). The biggest issue is that almost every use of select is wrong, so fixing them all in a timely manner is rather impractical. Some projects like Samba have already moved to poll(), but they&apos;re now hitting fd issues in various libraries.

I do agree that this is a library bug, but I think given the situation, it could make sense to add a fix for this to glibc to prevent buggy select use from overwriting arbitrary bits in memory. It&apos;s obvious that most projects don&apos;t use select() properly, even though its correct use is documented in the man pages.

Thanks.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>48488</commentid>
            <who name="Ulrich Drepper">drepper.fsp</who>
            <bug_when>2011-05-03 00:31:51 +0000</bug_when>
            <thetext>(In reply to comment #2)
&gt; Could I convince you to revisit this bug?

No.  Any such change breaks existing code since there are programs which redefine the set size and do other stupid things.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>48509</commentid>
            <who name="Rich Felker">bugdal</who>
            <bug_when>2011-05-03 20:05:03 +0000</bug_when>
            <thetext>Wouldn&apos;t it be reasonable to range-check the file descriptor when security-related feature test macros (perhaps FORTIFY_SOURCE) are enabled?

By the way, POSIX specifies that passing fd values greater than or equal to FD_SETSIZE to the FD_* macros/functions results in undefined behavior, so programs which want to *try* using select with higher fds should do it by allocating an *array of fd_set objects* with (maxfd+FD_SETSIZE)/FD_SETSIZE elements, then performing operations like FD_SET(fd%FD_SETSIZE, &amp;fds[fd/FD_SETSIZE]); -- this also avoids dependency on nonstandard and nonportable macros like NFDBITS.</thetext>
          </long_desc>
      
      

    </bug>

</bugzilla>