Skip to content

Commit 112bc89

Browse files
committed
jailer: Use O_NOFOLLOW for cgroup and netns file operations
Open cgroup files in writeln_special()/readln_special() and the network namespace file in join_netns() with O_NOFOLLOW, consistent with the existing pattern used for the exec file copy in env.rs. Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
1 parent 5303444 commit 112bc89

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/jailer/src/env.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,11 @@ impl Env {
519519

520520
fn join_netns(path: &str) -> Result<(), JailerError> {
521521
// The fd backing the file will be automatically dropped at the end of the scope
522-
let netns =
523-
File::open(path).map_err(|err| JailerError::FileOpen(PathBuf::from(path), err))?;
522+
let netns = OpenOptions::new()
523+
.read(true)
524+
.custom_flags(libc::O_NOFOLLOW)
525+
.open(path)
526+
.map_err(|err| JailerError::FileOpen(PathBuf::from(path), err))?;
524527

525528
// SAFETY: Safe because we are passing valid parameters.
526529
SyscallReturnCode(unsafe { libc::setns(netns.as_raw_fd(), libc::CLONE_NEWNET) })

src/jailer/src/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
use std::ffi::{CString, NulError, OsString};
55
use std::fmt::{Debug, Display};
6+
use std::fs::OpenOptions;
7+
use std::io::Read;
8+
use std::os::unix::fs::OpenOptionsExt;
69
use std::path::{Path, PathBuf};
710
use std::{env as p_env, fs, io};
811

@@ -240,12 +243,24 @@ where
240243
T: AsRef<Path> + Debug,
241244
V: Display + Debug,
242245
{
243-
fs::write(file_path, format!("{}\n", value))
246+
let mut file = OpenOptions::new()
247+
.write(true)
248+
.truncate(true)
249+
.custom_flags(libc::O_NOFOLLOW)
250+
.open(file_path.as_ref())
251+
.map_err(|err| JailerError::Write(PathBuf::from(file_path.as_ref()), err))?;
252+
io::Write::write_all(&mut file, format!("{}\n", value).as_bytes())
244253
.map_err(|err| JailerError::Write(PathBuf::from(file_path.as_ref()), err))
245254
}
246255

247256
pub fn readln_special<T: AsRef<Path> + Debug>(file_path: &T) -> Result<String, JailerError> {
248-
let mut line = fs::read_to_string(file_path)
257+
let mut file = OpenOptions::new()
258+
.read(true)
259+
.custom_flags(libc::O_NOFOLLOW)
260+
.open(file_path.as_ref())
261+
.map_err(|err| JailerError::ReadToString(PathBuf::from(file_path.as_ref()), err))?;
262+
let mut line = String::new();
263+
file.read_to_string(&mut line)
249264
.map_err(|err| JailerError::ReadToString(PathBuf::from(file_path.as_ref()), err))?;
250265

251266
// Remove the newline character at the end (if any).

0 commit comments

Comments
 (0)