Skip to content

Commit

Permalink
Merge pull request #854 from sylvestre/kernel
Browse files Browse the repository at this point in the history
Add a check of the kernel version
  • Loading branch information
pvdrz authored Jul 15, 2024
2 parents b3b90d2 + 41aed0a commit 2ac8cbb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Error {
other_user: Option<SudoString>,
},
SelfCheck,
KernelCheck,
CommandNotFound(PathBuf),
InvalidCommand(PathBuf),
ChDirNotAllowed {
Expand Down Expand Up @@ -56,6 +57,7 @@ impl fmt::Display for Error {
Error::SelfCheck => {
f.write_str("sudo must be owned by uid 0 and have the setuid bit set")
}
Error::KernelCheck => f.write_str("sudo needs a Kernel >= 5.9"),
Error::CommandNotFound(p) => write!(f, "'{}': command not found", p.display()),
Error::InvalidCommand(p) => write!(f, "'{}': invalid command", p.display()),
Error::UserNotFound(u) => write!(f, "user '{u}' not found"),
Expand Down
2 changes: 2 additions & 0 deletions src/sudo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::common::resolve::CurrentUser;
use crate::common::{Context, Error};
use crate::log::dev_info;
use crate::system::kernel::kernel_check;
use crate::system::timestamp::RecordScope;
use crate::system::User;
use crate::system::{time::Duration, timestamp::SessionRecordFile, Process};
Expand Down Expand Up @@ -84,6 +85,7 @@ fn sudo_process() -> Result<(), Error> {
dev_info!("development logs are enabled");

self_check()?;
kernel_check(5, 9)?;

let pipeline = Pipeline {
policy: SudoersPolicy::default(),
Expand Down
37 changes: 37 additions & 0 deletions src/system/kernel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::ffi::CStr;

use std::mem::zeroed;

use crate::common::Error;

pub fn kernel_check(major: u32, minor: u32) -> Result<(), Error> {
let mut utsname: libc::utsname = unsafe { zeroed() };

if unsafe { libc::uname(&mut utsname) } != 0 {
// Could not get the kernel version. Try to run anyway
return Ok(());
}

let release = unsafe { CStr::from_ptr(utsname.release.as_ptr()) }
.to_string_lossy()
.into_owned();

let version_parts: Vec<&str> = release.split('.').collect();

if version_parts.len() < 2 {
// Could not get the kernel version. Try to run anyway
return Ok(());
}

// Parse the major and minor version numbers
if let (Ok(major_version), Ok(minor_version)) = (
version_parts[0].parse::<u32>(),
version_parts[1].parse::<u32>(),
) {
if major_version > major || (major_version == major && minor_version >= minor) {
return Ok(());
}
}

Err(Error::KernelCheck)
}
2 changes: 2 additions & 0 deletions src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ mod audit;
// generalized traits for when we want to hide implementations
pub mod interface;

pub mod kernel;

pub mod file;

pub mod time;
Expand Down

0 comments on commit 2ac8cbb

Please sign in to comment.