From fb40c71a2156f91bc13049e35a7fcbcf793541ed Mon Sep 17 00:00:00 2001 From: Archana Shinde Date: Wed, 26 Apr 2023 11:43:58 -0700 Subject: [PATCH] env: Check for root privileges Check for root privileges early on. Signed-off-by: Archana Shinde --- src/tools/kata-ctl/src/ops/env_ops.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/tools/kata-ctl/src/ops/env_ops.rs b/src/tools/kata-ctl/src/ops/env_ops.rs index a0332af85a..de8970ad07 100644 --- a/src/tools/kata-ctl/src/ops/env_ops.rs +++ b/src/tools/kata-ctl/src/ops/env_ops.rs @@ -13,6 +13,7 @@ use crate::utils; use kata_types::config::TomlConfig; use anyhow::{anyhow, Context, Result}; +use nix::unistd::Uid; use serde::{Deserialize, Serialize}; use std::fs::File; use std::io::{self, Write}; @@ -441,6 +442,10 @@ pub fn get_env_info(toml_config: &TomlConfig) -> Result { } pub fn handle_env(env_args: EnvArgument) -> Result<()> { + if !Uid::effective().is_root() { + return Err(anyhow!("kata-ctl env command requires root privileges to get host information. Please run as root or use sudo")); + } + let mut file: Box = if let Some(path) = env_args.file { Box::new( File::create(path.as_str()).with_context(|| format!("Error creating file {}", path))?,