From: Matthew Wilcox <willy-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
To: linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: select fails to verify all file descriptors are valid
Date: Tue, 14 Mar 2017 09:11:38 -0700 [thread overview]
Message-ID: <20170314161138.GC4033@bombadil.infradead.org> (raw)
Quoting the manpage:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
nfds is the highest-numbered file descriptor in any of the three sets,
plus 1.
EBADF An invalid file descriptor was given in one of the sets. (Per‐
haps a file descriptor that was already closed, or one on which
an error has occurred.)
That's not quite how Linux behaves. We only check the fd_set up to the
maximum number of fds allocated to this task:
rcu_read_lock();
fdt = files_fdtable(current->files);
max_fds = fdt->max_fds;
rcu_read_unlock();
if (n > max_fds)
n = max_fds;
(then we copy in up to 'n' bits worth of bitmaps).
It is pretty straightforward to demonstrate that Linux doesn't check:
int main(void)
{
int ret;
struct timeval tv = { };
fd_set fds;
FD_ZERO(&fds);
FD_SETFD(FD_SETSIZE - 1, &fds);
ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
assert(ret == -1 && errno == EBADF);
return 0;
}
Linux has behaved this way since 2.6.12, and I can't be bothered to get
out the historical git trees to find out what happened before 2005.
So ... if I change this behaviour by checking all the file descriptors, I
do stand a chance of breaking an application. On the other hand, that
application could already have been broken by the shell deciding to open
a really high file descriptor (I'm looking at you, bash), which the program
then inherits.
Worth fixing this bug? Worth documenting this bug, at least?
next reply other threads:[~2017-03-14 16:11 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-14 16:11 Matthew Wilcox [this message]
2017-03-15 5:34 ` select fails to verify all file descriptors are valid Carlos O'Donell
[not found] ` <4859e5e8-4cc1-626c-3294-e5f529fe41d3-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2017-03-24 14:41 ` Michael Kerrisk (man-pages)
[not found] ` <20170314161138.GC4033-PfSpb0PWhxZc2C7mugBRk2EX/6BAtgUQ@public.gmane.org>
2017-03-24 14:40 ` Michael Kerrisk (man-pages)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170314161138.GC4033@bombadil.infradead.org \
--to=willy-wegcikhe2lqwvfeawa7xhq@public.gmane.org \
--cc=linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).