about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-07-11 04:33:16 +0200
committerGitHub <noreply@github.com>2019-07-11 04:33:16 +0200
commite07df9c9df149b48ef7e035ddaa5ba5a22cb719c (patch)
treee8337056581fac31db4eecd91d442f5c3d36ac8e /src/libstd/sys
parent8fe7ed02d5a88839ec832d36899822d431725942 (diff)
parent6031a07a464eae202cc43fbb15fada094171488d (diff)
downloadrust-e07df9c9df149b48ef7e035ddaa5ba5a22cb719c.tar.gz
rust-e07df9c9df149b48ef7e035ddaa5ba5a22cb719c.zip
Rollup merge of #62425 - cyphar:linux-cloexec-use-fcntl, r=alexcrichton
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>
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/fd.rs2
1 files changed, 2 insertions, 0 deletions
diff --git a/src/libstd/sys/unix/fd.rs b/src/libstd/sys/unix/fd.rs
index 6d23963e141..0cecdd7ffa0 100644
--- a/src/libstd/sys/unix/fd.rs
+++ b/src/libstd/sys/unix/fd.rs
@@ -175,6 +175,7 @@ impl FileDesc {
                   target_os = "emscripten",
                   target_os = "fuchsia",
                   target_os = "l4re",
+                  target_os = "linux",
                   target_os = "haiku")))]
     pub fn set_cloexec(&self) -> io::Result<()> {
         unsafe {
@@ -187,6 +188,7 @@ impl FileDesc {
               target_os = "emscripten",
               target_os = "fuchsia",
               target_os = "l4re",
+              target_os = "linux",
               target_os = "haiku"))]
     pub fn set_cloexec(&self) -> io::Result<()> {
         unsafe {