Skip to content

Commit f5db3c9

Browse files
committed
Address review comments: refactor SMACK helpers and rename SELinux flags
1 parent ad5d569 commit f5db3c9

7 files changed

Lines changed: 67 additions & 57 deletions

File tree

src/uu/id/locales/en-US.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ id-error-names-real-ids-require-flags = printing only names or real IDs requires
1818
id-error-zero-not-permitted-default = option --zero not permitted in default format
1919
id-error-cannot-print-context-with-user = cannot print security context when user specified
2020
id-error-cannot-get-context = can't get process context
21-
id-error-context-selinux-only = --context (-Z) works only on an SELinux-enabled kernel
21+
id-error-context-security-only = --context (-Z) works only on an SELinux/SMACK-enabled kernel
2222
id-error-no-such-user = { $user }: no such user
2323
id-error-cannot-find-group-name = cannot find name for group ID { $gid }
2424
id-error-cannot-find-user-name = cannot find name for user ID { $uid }

src/uu/id/locales/fr-FR.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ id-error-names-real-ids-require-flags = l'affichage des noms uniquement ou des I
1818
id-error-zero-not-permitted-default = l'option --zero n'est pas autorisée dans le format par défaut
1919
id-error-cannot-print-context-with-user = impossible d'afficher le contexte de sécurité quand un utilisateur est spécifié
2020
id-error-cannot-get-context = impossible d'obtenir le contexte du processus
21-
id-error-context-selinux-only = --context (-Z) ne fonctionne que sur un noyau avec SELinux activé
21+
id-error-context-security-only = --context (-Z) ne fonctionne que sur un noyau avec SELinux/SMACK activé
2222
id-error-no-such-user = { $user } : utilisateur inexistant
2323
id-error-cannot-find-group-name = impossible de trouver le nom pour l'ID de groupe { $gid }
2424
id-error-cannot-find-user-name = impossible de trouver le nom pour l'ID utilisateur { $uid }

src/uu/id/src/id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
210210
// Neither SELinux nor SMACK supported
211211
return Err(USimpleError::new(
212212
1,
213-
translate!("id-error-context-selinux-only"),
213+
translate!("id-error-context-security-only"),
214214
));
215215
}
216216

src/uu/mkdir/src/mkdir.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod options {
2727
pub const PARENTS: &str = "parents";
2828
pub const VERBOSE: &str = "verbose";
2929
pub const DIRS: &str = "dirs";
30-
pub const SELINUX: &str = "z";
30+
pub const SECURITY_CONTEXT: &str = "z";
3131
pub const CONTEXT: &str = "context";
3232
}
3333

@@ -42,8 +42,8 @@ pub struct Config<'a> {
4242
/// Print message for each created directory.
4343
pub verbose: bool,
4444

45-
/// Set `SELinux` security context.
46-
pub set_selinux_context: bool,
45+
/// Set security context (SELinux/SMACK).
46+
pub set_security_context: bool,
4747

4848
/// Specific `SELinux` context.
4949
pub context: Option<&'a String>,
@@ -79,7 +79,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
7979
let recursive = matches.get_flag(options::PARENTS);
8080

8181
// Extract the SELinux related flags and options
82-
let set_selinux_context = matches.get_flag(options::SELINUX);
82+
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
8383
let context = matches.get_one::<String>(options::CONTEXT);
8484

8585
match get_mode(&matches) {
@@ -88,7 +88,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8888
recursive,
8989
mode,
9090
verbose,
91-
set_selinux_context: set_selinux_context || context.is_some(),
91+
set_security_context: set_security_context || context.is_some(),
9292
context,
9393
};
9494
exec(dirs, &config)
@@ -129,7 +129,7 @@ pub fn uu_app() -> Command {
129129
.action(ArgAction::SetTrue),
130130
)
131131
.arg(
132-
Arg::new(options::SELINUX)
132+
Arg::new(options::SECURITY_CONTEXT)
133133
.short('Z')
134134
.help(translate!("mkdir-help-selinux"))
135135
.action(ArgAction::SetTrue),
@@ -292,7 +292,7 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
292292

293293
// Apply SELinux context if requested
294294
#[cfg(feature = "selinux")]
295-
if config.set_selinux_context && uucore::selinux::is_selinux_enabled() {
295+
if config.set_security_context && uucore::selinux::is_selinux_enabled() {
296296
if let Err(e) = uucore::selinux::set_selinux_security_context(path, config.context)
297297
{
298298
let _ = std::fs::remove_dir(path);
@@ -302,13 +302,8 @@ fn create_single_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<(
302302

303303
// Apply SMACK context if requested
304304
#[cfg(feature = "smack")]
305-
if config.set_selinux_context && uucore::smack::is_smack_enabled() {
306-
if let Some(ctx) = config.context {
307-
if let Err(e) = uucore::smack::set_smack_label_for_path(path, ctx) {
308-
let _ = std::fs::remove_dir(path);
309-
return Err(USimpleError::new(1, e.to_string()));
310-
}
311-
}
305+
if config.set_security_context {
306+
uucore::smack::set_smack_label_for_new_dir(path, config.context)?;
312307
}
313308
Ok(())
314309
}

src/uu/mkfifo/src/mkfifo.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use uucore::{format_usage, show};
1616

1717
mod options {
1818
pub static MODE: &str = "mode";
19-
pub static SELINUX: &str = "Z";
19+
pub static SECURITY_CONTEXT: &str = "Z";
2020
pub static CONTEXT: &str = "context";
2121
pub static FIFO: &str = "fifo";
2222
}
@@ -62,10 +62,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6262
#[cfg(feature = "selinux")]
6363
{
6464
// Extract the SELinux related flags and options
65-
let set_selinux_context = matches.get_flag(options::SELINUX);
65+
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
6666
let context = matches.get_one::<String>(options::CONTEXT);
6767

68-
if set_selinux_context || context.is_some() {
68+
if set_security_context || context.is_some() {
6969
use std::path::Path;
7070
if let Err(e) =
7171
uucore::selinux::set_selinux_security_context(Path::new(&f), context)
@@ -79,17 +79,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
7979
// Apply SMACK context if requested
8080
#[cfg(feature = "smack")]
8181
{
82-
let set_smack_context = matches.get_flag(options::SELINUX);
82+
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
8383
let context = matches.get_one::<String>(options::CONTEXT);
84-
85-
if (set_smack_context || context.is_some()) && uucore::smack::is_smack_enabled() {
86-
if let Some(ctx) = context {
87-
use std::path::Path;
88-
if let Err(e) = uucore::smack::set_smack_label_for_path(Path::new(&f), ctx) {
89-
let _ = fs::remove_file(&f);
90-
return Err(USimpleError::new(1, e.to_string()));
91-
}
92-
}
84+
if set_security_context || context.is_some() {
85+
uucore::smack::set_smack_label_for_new_file(&f, context)?;
9386
}
9487
}
9588
}
@@ -112,7 +105,7 @@ pub fn uu_app() -> Command {
112105
.value_name("MODE"),
113106
)
114107
.arg(
115-
Arg::new(options::SELINUX)
108+
Arg::new(options::SECURITY_CONTEXT)
116109
.short('Z')
117110
.help(translate!("mkfifo-help-selinux"))
118111
.action(ArgAction::SetTrue),

src/uu/mknod/src/mknod.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod options {
2323
pub const TYPE: &str = "type";
2424
pub const MAJOR: &str = "major";
2525
pub const MINOR: &str = "minor";
26-
pub const SELINUX: &str = "z";
26+
pub const SECURITY_CONTEXT: &str = "z";
2727
pub const CONTEXT: &str = "context";
2828
}
2929

@@ -54,10 +54,10 @@ pub struct Config<'a> {
5454

5555
pub dev: dev_t,
5656

57-
/// Set `SELinux` security context.
58-
pub set_selinux_context: bool,
57+
/// Set security context (SELinux/SMACK).
58+
pub set_security_context: bool,
5959

60-
/// Specific `SELinux` context.
60+
/// Specific security context (SELinux/SMACK).
6161
pub context: Option<&'a String>,
6262
}
6363

@@ -88,7 +88,7 @@ fn mknod(file_name: &str, config: Config) -> i32 {
8888

8989
// Apply SELinux context if requested
9090
#[cfg(feature = "selinux")]
91-
if config.set_selinux_context {
91+
if config.set_security_context {
9292
if let Err(e) = uucore::selinux::set_selinux_security_context(
9393
std::path::Path::new(file_name),
9494
config.context,
@@ -102,16 +102,10 @@ fn mknod(file_name: &str, config: Config) -> i32 {
102102

103103
// Apply SMACK context if requested
104104
#[cfg(feature = "smack")]
105-
if config.set_selinux_context && uucore::smack::is_smack_enabled() {
106-
if let Some(ctx) = config.context {
107-
if let Err(e) =
108-
uucore::smack::set_smack_label_for_path(std::path::Path::new(file_name), ctx)
109-
{
110-
// if it fails, delete the file
111-
let _ = std::fs::remove_file(file_name);
112-
eprintln!("{}: {}", uucore::util_name(), e);
113-
return 1;
114-
}
105+
if config.set_security_context {
106+
if let Err(e) = uucore::smack::set_smack_label_for_new_file(file_name, config.context) {
107+
eprintln!("{}: {}", uucore::util_name(), e);
108+
return 1;
115109
}
116110
}
117111

@@ -139,8 +133,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
139133
.get_one::<String>("name")
140134
.expect("Missing argument 'NAME'");
141135

142-
// Extract the SELinux related flags and options
143-
let set_selinux_context = matches.get_flag(options::SELINUX);
136+
// Extract the security context related flags and options
137+
let set_security_context = matches.get_flag(options::SECURITY_CONTEXT);
144138
let context = matches.get_one::<String>(options::CONTEXT);
145139

146140
let dev = match (
@@ -168,7 +162,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
168162
mode,
169163
use_umask,
170164
dev,
171-
set_selinux_context: set_selinux_context || context.is_some(),
165+
set_security_context: set_security_context || context.is_some(),
172166
context,
173167
};
174168

@@ -219,7 +213,7 @@ pub fn uu_app() -> Command {
219213
.value_parser(value_parser!(u64)),
220214
)
221215
.arg(
222-
Arg::new(options::SELINUX)
216+
Arg::new(options::SECURITY_CONTEXT)
223217
.short('Z')
224218
.help(translate!("mknod-help-selinux"))
225219
.action(ArgAction::SetTrue),

src/uucore/src/lib/features/smack.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::sync::OnceLock;
1313

1414
use thiserror::Error;
1515

16-
use crate::error::{UError, strip_errno};
16+
use crate::error::{UError, USimpleError, strip_errno};
1717
use crate::translate;
1818

1919
#[derive(Debug, Error)]
@@ -72,13 +72,9 @@ pub fn set_smack_label_for_self(label: &str) -> Result<(), SmackError> {
7272
return Err(SmackError::SmackNotEnabled);
7373
}
7474

75-
let label_owned = label.to_string();
7675
fs::File::create("/proc/self/attr/current")
77-
.map_err(|e| SmackError::LabelSetFailure(label_owned.clone(), e))?
78-
.write_all(label.as_bytes())
79-
.map_err(|e| SmackError::LabelSetFailure(label_owned, e))?;
80-
81-
Ok(())
76+
.and_then(|mut f| f.write_all(label.as_bytes()))
77+
.map_err(|e| SmackError::LabelSetFailure(label.to_string(), e))
8278
}
8379

8480
/// Gets the SMACK label for a filesystem path via xattr.
@@ -106,3 +102,35 @@ pub fn set_smack_label_for_path(path: &Path, label: &str) -> Result<(), SmackErr
106102
xattr::set(path, "security.SMACK64", label.as_bytes())
107103
.map_err(|e| SmackError::LabelSetFailure(label.to_string(), e))
108104
}
105+
106+
/// Sets SMACK label for a file, removing it on failure.
107+
pub fn set_smack_label_for_new_file(
108+
path: impl AsRef<Path>,
109+
context: Option<&String>,
110+
) -> Result<(), Box<dyn UError>> {
111+
let Some(ctx) = context else { return Ok(()) };
112+
if !is_smack_enabled() {
113+
return Ok(());
114+
}
115+
let path = path.as_ref();
116+
set_smack_label_for_path(path, ctx).map_err(|e| {
117+
let _ = fs::remove_file(path);
118+
USimpleError::new(1, e.to_string())
119+
})
120+
}
121+
122+
/// Sets SMACK label for a directory, removing it on failure.
123+
pub fn set_smack_label_for_new_dir(
124+
path: impl AsRef<Path>,
125+
context: Option<&String>,
126+
) -> Result<(), Box<dyn UError>> {
127+
let Some(ctx) = context else { return Ok(()) };
128+
if !is_smack_enabled() {
129+
return Ok(());
130+
}
131+
let path = path.as_ref();
132+
set_smack_label_for_path(path, ctx).map_err(|e| {
133+
let _ = fs::remove_dir(path);
134+
USimpleError::new(1, e.to_string())
135+
})
136+
}

0 commit comments

Comments
 (0)