Skip to content

Commit

Permalink
refactor: use std::io::Error when setting seccomp
Browse files Browse the repository at this point in the history
Replace __errno_location() with std::io::Error::last_os_error() as a
more standard of getting errno value.

Signed-off-by: Egor Lazarchuk <[email protected]>
  • Loading branch information
ShadowCurse committed Dec 11, 2024
1 parent f9f24fe commit 141ffe8
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/vmm/src/seccomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ pub fn deserialize_binary<R: Read>(
}

/// Filter installation errors.
#[derive(Debug, PartialEq, Eq, thiserror::Error, displaydoc::Display)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]

Check warning on line 63 in src/vmm/src/seccomp.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/seccomp.rs#L63

Added line #L63 was not covered by tests
pub enum InstallationError {
/// Filter length exceeds the maximum size of {BPF_MAX_LEN:} instructions
FilterTooLarge,
/// prctl` syscall failed with error code: {0}
Prctl(i32),
Prctl(std::io::Error),
}

/// The maximum seccomp-BPF program length allowed by the linux kernel.
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn apply_filter(bpf_filter: BpfProgramRef) -> Result<(), InstallationError>
{
let rc = libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
if rc != 0 {
return Err(InstallationError::Prctl(*libc::__errno_location()));
return Err(InstallationError::Prctl(std::io::Error::last_os_error()));

Check warning on line 104 in src/vmm/src/seccomp.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/seccomp.rs#L104

Added line #L104 was not covered by tests
}
}

Expand All @@ -118,7 +118,7 @@ pub fn apply_filter(bpf_filter: BpfProgramRef) -> Result<(), InstallationError>
bpf_prog_ptr,
);
if rc != 0 {
return Err(InstallationError::Prctl(*libc::__errno_location()));
return Err(InstallationError::Prctl(std::io::Error::last_os_error()));
}
}
}
Expand Down Expand Up @@ -191,10 +191,10 @@ mod tests {
let filter: BpfProgram = vec![0; 5000];

// Apply seccomp filter.
assert_eq!(
assert!(matches!(
apply_filter(&filter).unwrap_err(),
InstallationError::FilterTooLarge
);
));
})
.join()
.unwrap();
Expand Down Expand Up @@ -224,10 +224,10 @@ mod tests {
let seccomp_level = unsafe { libc::prctl(libc::PR_GET_SECCOMP) };
assert_eq!(seccomp_level, 0);

assert_eq!(
assert!(matches!(
apply_filter(&filter).unwrap_err(),
InstallationError::Prctl(22)
);
InstallationError::Prctl(_)
));

// test that seccomp level remains 0 on failure.
let seccomp_level = unsafe { libc::prctl(libc::PR_GET_SECCOMP) };
Expand Down

0 comments on commit 141ffe8

Please sign in to comment.