|
3 | 3 |
|
4 | 4 | use std::ffi::{CString, NulError, OsString}; |
5 | 5 | use std::fmt::{Debug, Display}; |
6 | | -use std::os::unix::prelude::AsRawFd; |
7 | 6 | use std::path::{Path, PathBuf}; |
8 | 7 | use std::{env as p_env, fs, io}; |
9 | 8 |
|
@@ -261,44 +260,10 @@ fn close_fds_by_close_range() -> Result<(), JailerError> { |
261 | 260 | .map_err(JailerError::CloseRange) |
262 | 261 | } |
263 | 262 |
|
264 | | -fn close_fds_by_reading_proc() -> Result<(), JailerError> { |
265 | | - // Calling this method means that close_range failed (we might be on kernel < 5.9). |
266 | | - // We can't use std::fs::ReadDir here as under the hood we need access to the dirfd in order to |
267 | | - // not close it twice |
268 | | - let path = "/proc/self/fd"; |
269 | | - let mut dir = nix::dir::Dir::open( |
270 | | - path, |
271 | | - nix::fcntl::OFlag::O_DIRECTORY | nix::fcntl::OFlag::O_NOATIME, |
272 | | - nix::sys::stat::Mode::empty(), |
273 | | - ) |
274 | | - .map_err(|e| JailerError::DirOpen(path.to_string(), e.to_string()))?; |
275 | | - |
276 | | - let dirfd = dir.as_raw_fd(); |
277 | | - let mut c = dir.iter(); |
278 | | - |
279 | | - while let Some(Ok(path)) = c.next() { |
280 | | - let file_name = path.file_name(); |
281 | | - let fd_str = file_name.to_str().map_err(JailerError::UTF8Parsing)?; |
282 | | - |
283 | | - // If the entry is an INT entry, we go ahead and we treat it as an FD identifier. |
284 | | - if let Ok(fd) = fd_str.parse::<i32>() { |
285 | | - if fd > 2 && fd != dirfd { |
286 | | - // SAFETY: Safe because close() cannot fail when passed a valid parameter. |
287 | | - unsafe { libc::close(fd) }; |
288 | | - } |
289 | | - } |
290 | | - } |
291 | | - Ok(()) |
292 | | -} |
293 | | - |
294 | 263 | // Closes all FDs other than 0 (STDIN), 1 (STDOUT) and 2 (STDERR) |
295 | 264 | fn close_inherited_fds() -> Result<(), JailerError> { |
296 | | - // The approach we take here is to firstly try to use the close_range syscall |
297 | | - // which is available on kernels > 5.9. |
298 | | - // We then fallback to using /proc/sef/fd to close open fds. |
299 | | - if close_fds_by_close_range().is_err() { |
300 | | - close_fds_by_reading_proc()?; |
301 | | - } |
| 265 | + // We use the close_range syscall which is available on kernels > 5.9. |
| 266 | + close_fds_by_close_range()?; |
302 | 267 | Ok(()) |
303 | 268 | } |
304 | 269 |
|
@@ -439,11 +404,6 @@ mod tests { |
439 | 404 | } |
440 | 405 | } |
441 | 406 |
|
442 | | - #[test] |
443 | | - fn test_fds_proc() { |
444 | | - run_close_fds_test(close_fds_by_reading_proc); |
445 | | - } |
446 | | - |
447 | 407 | #[test] |
448 | 408 | fn test_sanitize_process() { |
449 | 409 | run_close_fds_test(sanitize_process); |
|
0 commit comments