filedesc: don't use ioctl(FIOCLEX) on Linux

All ioctl(2)s will fail on O_PATH file descriptors on Linux (because
they use &empty_fops as a security measure against O_PATH descriptors
affecting the backing file).

As a result, File::try_clone() and various other methods would always
fail with -EBADF on O_PATH file descriptors. The solution is to simply
use F_SETFD (as is used on other unices) which works on O_PATH
descriptors because it operates through the fnctl(2) layer and not
through ioctl(2)s.

Since this code is usually only used in strange error paths (a broken or
ancient kernel), the extra overhead of one syscall shouldn't cause any
dramas. Most other systems programming languages also use the fnctl(2)
so this brings us in line with them.

Fixes: rust-lang/rust#62314
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai 2019-07-06 00:51:19 +10:00
parent 481068a707
commit 6031a07a46
No known key found for this signature in database
GPG key ID: 9D94B96321B9D012

View file

@ -175,6 +175,7 @@ impl FileDesc {
target_os = "emscripten", target_os = "emscripten",
target_os = "fuchsia", target_os = "fuchsia",
target_os = "l4re", target_os = "l4re",
target_os = "linux",
target_os = "haiku")))] target_os = "haiku")))]
pub fn set_cloexec(&self) -> io::Result<()> { pub fn set_cloexec(&self) -> io::Result<()> {
unsafe { unsafe {
@ -187,6 +188,7 @@ impl FileDesc {
target_os = "emscripten", target_os = "emscripten",
target_os = "fuchsia", target_os = "fuchsia",
target_os = "l4re", target_os = "l4re",
target_os = "linux",
target_os = "haiku"))] target_os = "haiku"))]
pub fn set_cloexec(&self) -> io::Result<()> { pub fn set_cloexec(&self) -> io::Result<()> {
unsafe { unsafe {