agent: valid envs for hooks

Envs contain null-byte will cause running hooks to panic,
this commit will filter envs and only pass valid envs to hooks.

Fixes: #3667

Signed-off-by: bin <bin@hyper.sh>
This commit is contained in:
bin 2022-02-15 11:51:23 +08:00 committed by Samuel Ortiz
parent a3b3274121
commit 80e8dbf1f5

View File

@ -1488,14 +1488,9 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> {
if args.len() > 1 { if args.len() > 1 {
args.remove(0); args.remove(0);
} }
let env: HashMap<String, String> = h
.env // all invalid envs will be ommit, only valid envs will be passed to hook.
.iter() let env: HashMap<&str, &str> = h.env.iter().filter_map(|e| valid_env(e)).collect();
.map(|e| {
let v: Vec<&str> = e.split('=').collect();
(v[0].to_string(), v[1].to_string())
})
.collect();
// Avoid the exit signal to be reaped by the global reaper. // Avoid the exit signal to be reaped by the global reaper.
let _wait_locker = WAIT_PID_LOCKER.lock().await; let _wait_locker = WAIT_PID_LOCKER.lock().await;
@ -1506,8 +1501,7 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> {
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.spawn() .spawn()?;
.unwrap();
// default timeout 10s // default timeout 10s
let mut timeout: u64 = 10; let mut timeout: u64 = 10;
@ -1647,13 +1641,16 @@ mod tests {
let touch = which("touch").await; let touch = which("touch").await;
defer!(fs::remove_file(temp_file).unwrap();); defer!(fs::remove_file(temp_file).unwrap(););
let invalid_str = vec![97, b'\0', 98];
let invalid_string = std::str::from_utf8(&invalid_str).unwrap();
let invalid_env = format!("{}=value", invalid_string);
execute_hook( execute_hook(
&slog_scope::logger(), &slog_scope::logger(),
&Hook { &Hook {
path: touch, path: touch,
args: vec!["touch".to_string(), temp_file.to_string()], args: vec!["touch".to_string(), temp_file.to_string()],
env: vec![], env: vec![invalid_env],
timeout: Some(10), timeout: Some(10),
}, },
&OCIState { &OCIState {