mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-31 08:28:34 +00:00
Merge pull request #5436 from amshinde/kata-ctl-drop-privs
Kata ctl drop privs
This commit is contained in:
2
src/tools/kata-ctl/.gitignore
vendored
2
src/tools/kata-ctl/.gitignore
vendored
@@ -1 +1 @@
|
||||
/vendor/
|
||||
src/ops/version.rs
|
||||
|
1035
src/tools/kata-ctl/Cargo.lock
generated
Normal file
1035
src/tools/kata-ctl/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -17,6 +17,8 @@ anyhow = "1.0.31"
|
||||
clap = { version = "3.2.20", features = ["derive", "cargo"] }
|
||||
serde_json = "1.0.85"
|
||||
thiserror = "1.0.35"
|
||||
privdrop = "0.5.2"
|
||||
nix = "0.25.0"
|
||||
|
||||
[target.'cfg(target_arch = "s390x")'.dependencies]
|
||||
reqwest = { version = "0.11", default-features = false, features = ["json", "blocking", "native-tls"] }
|
||||
|
@@ -7,6 +7,7 @@ mod arch;
|
||||
mod args;
|
||||
mod check;
|
||||
mod ops;
|
||||
mod utils;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
|
33
src/tools/kata-ctl/src/utils.rs
Normal file
33
src/tools/kata-ctl/src/utils.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2022 Intel Corporation
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
const NON_PRIV_USER: &str = "nobody";
|
||||
|
||||
pub fn drop_privs() -> Result<()> {
|
||||
if nix::unistd::Uid::effective().is_root() {
|
||||
privdrop::PrivDrop::default()
|
||||
.chroot("/")
|
||||
.user(NON_PRIV_USER)
|
||||
.apply()
|
||||
.map_err(|e| anyhow!("Failed to drop privileges to user {}: {}", NON_PRIV_USER, e))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_drop_privs() {
|
||||
let res = drop_privs();
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user