Skip to content

Commit 6031a07

Browse files
committed
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: #62314 Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 481068a commit 6031a07

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/libstd/sys/unix/fd.rs

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ impl FileDesc {
175175
target_os = "emscripten",
176176
target_os = "fuchsia",
177177
target_os = "l4re",
178+
target_os = "linux",
178179
target_os = "haiku")))]
179180
pub fn set_cloexec(&self) -> io::Result<()> {
180181
unsafe {
@@ -187,6 +188,7 @@ impl FileDesc {
187188
target_os = "emscripten",
188189
target_os = "fuchsia",
189190
target_os = "l4re",
191+
target_os = "linux",
190192
target_os = "haiku"))]
191193
pub fn set_cloexec(&self) -> io::Result<()> {
192194
unsafe {

0 commit comments

Comments
 (0)