Merge pull request #5436 from amshinde/kata-ctl-drop-privs

Kata ctl drop privs
This commit is contained in:
David Esparza
2022-10-26 11:37:27 -05:00
committed by GitHub
5 changed files with 1072 additions and 1 deletions

View File

@@ -1 +1 @@
/vendor/
src/ops/version.rs

1035
src/tools/kata-ctl/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -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"] }

View File

@@ -7,6 +7,7 @@ mod arch;
mod args;
mod check;
mod ops;
mod utils;
use anyhow::Result;
use clap::Parser;

View 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());
}
}