Debian Codegraph @

tests/by-util/test_chcon.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore (jargon) xattributes
7 #![allow(clippy::missing_errors_doc, clippy::similar_names)]
8 #![cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
9
10 use std::ffi::CString;
11 use std::path::Path;
12 use std::{io, iter, str};
13
14 use uutests::at_and_ucmd;
15 use uutests::new_ucmd;
16
17 #[test]
18 fn version() {
19 new_ucmd!().arg("--version").succeeds();
20 new_ucmd!().arg("-V").succeeds();
21 }
22
23 #[test]
24 fn help() {
25 new_ucmd!().fails();
26 new_ucmd!().arg("--help").succeeds();
27 new_ucmd!().arg("-h").fails(); // -h is NOT --help, it is actually --no-dereference.
28 }
29
30 #[test]
31 fn reference_errors() {
32 for args in [
33 &["--verbose", "--reference"] as &[&str],
34 &["--verbose", "--reference=/dev/null"],
35 &["--verbose", "--reference=/inexistent", "/dev/null"],
36 ] {
37 new_ucmd!().args(args).fails();
38 }
39 }
40
41 #[test]
42 fn recursive_errors() {
43 for args in [
44 &["--verbose", "-P"] as &[&str],
45 &["--verbose", "-H"],
46 &["--verbose", "-L"],
47 &["--verbose", "--recursive", "-P", "--dereference"],
48 &["--verbose", "--recursive", "-H", "--no-dereference"],
49 &["--verbose", "--recursive", "-L", "--no-dereference"],
50 ] {
51 new_ucmd!().args(args).fails();
52 }
53 }
54
55 #[test]
56 fn valid_context() {
57 let (dir, mut cmd) = at_and_ucmd!();
58 dir.touch("a.tmp");
59 dir.symlink_file("a.tmp", "la.tmp");
60
61 let la_context = get_file_context(dir.plus("a.tmp")).unwrap();
62 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
63
64 cmd.args(&["--verbose", new_la_context])
65 .arg(dir.plus("la.tmp"))
66 .succeeds();
67 assert_eq!(get_file_context(dir.plus("la.tmp")).unwrap(), la_context);
68 assert_eq!(
69 get_file_context(dir.plus("a.tmp")).unwrap().as_deref(),
70 Some(new_la_context)
71 );
72 }
73
74 #[test]
75 fn valid_context_on_valid_symlink() {
76 let (dir, mut cmd) = at_and_ucmd!();
77 dir.touch("a.tmp");
78 dir.symlink_file("a.tmp", "la.tmp");
79
80 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
81 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
82
83 cmd.args(&["--verbose", "--no-dereference", new_la_context])
84 .arg(dir.plus("la.tmp"))
85 .succeeds();
86 assert_eq!(
87 get_file_context(dir.plus("la.tmp")).unwrap().as_deref(),
88 Some(new_la_context)
89 );
90 assert_eq!(get_file_context(dir.plus("a.tmp")).unwrap(), a_context);
91 }
92
93 #[test]
94 fn valid_context_on_valid_symlink_override_dereference() {
95 let (dir, mut cmd) = at_and_ucmd!();
96 dir.touch("a.tmp");
97 dir.symlink_file("a.tmp", "la.tmp");
98
99 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
100 let la_context = get_file_context(dir.plus("la.tmp")).unwrap();
101 let new_a_context = "guest_u:object_r:etc_t:s0:c42";
102 assert_ne!(a_context.as_deref(), Some(new_a_context));
103 assert_ne!(la_context.as_deref(), Some(new_a_context));
104
105 cmd.args(&[
106 "--verbose",
107 "--no-dereference",
108 "--dereference",
109 new_a_context,
110 ])
111 .arg(dir.plus("la.tmp"))
112 .succeeds();
113 assert_eq!(
114 get_file_context(dir.plus("a.tmp")).unwrap().as_deref(),
115 Some(new_a_context)
116 );
117 assert_eq!(get_file_context(dir.plus("la.tmp")).unwrap(), la_context);
118 }
119
120 #[test]
121 fn valid_context_on_broken_symlink() {
122 let (dir, mut cmd) = at_and_ucmd!();
123 dir.symlink_file("a.tmp", "la.tmp");
124
125 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
126
127 cmd.args(&["--verbose", "--no-dereference", new_la_context])
128 .arg(dir.plus("la.tmp"))
129 .succeeds();
130 assert_eq!(
131 get_file_context(dir.plus("la.tmp")).unwrap().as_deref(),
132 Some(new_la_context)
133 );
134 }
135
136 #[test]
137 fn valid_context_on_broken_symlink_after_deref() {
138 let (dir, mut cmd) = at_and_ucmd!();
139 dir.symlink_file("a.tmp", "la.tmp");
140
141 let la_context = get_file_context(dir.plus("la.tmp")).unwrap();
142 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
143 assert_ne!(la_context.as_deref(), Some(new_la_context));
144
145 cmd.args(&[
146 "--verbose",
147 "--dereference",
148 "--no-dereference",
149 new_la_context,
150 ])
151 .arg(dir.plus("la.tmp"))
152 .succeeds();
153 assert_eq!(
154 get_file_context(dir.plus("la.tmp")).unwrap().as_deref(),
155 Some(new_la_context)
156 );
157 }
158
159 #[test]
160 fn valid_context_with_prior_xattributes() {
161 let (dir, mut cmd) = at_and_ucmd!();
162 dir.touch("a.tmp");
163
164 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
165 if a_context.is_none() {
166 set_file_context(dir.plus("a.tmp"), "unconfined_u:object_r:user_tmp_t:s0").unwrap();
167 }
168 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
169
170 cmd.args(&["--verbose", new_la_context])
171 .arg(dir.plus("a.tmp"))
172 .succeeds();
173 assert_eq!(
174 get_file_context(dir.plus("a.tmp")).unwrap().as_deref(),
175 Some(new_la_context)
176 );
177 }
178
179 #[test]
180 fn valid_context_directory() {
181 let (dir, mut cmd) = at_and_ucmd!();
182 dir.mkdir("a");
183 dir.symlink_dir("a", "la");
184
185 let b_path = Path::new("a").join("b.txt");
186 dir.touch(b_path.to_str().unwrap());
187
188 let la_context = get_file_context(dir.plus("la")).unwrap();
189 let b_context = get_file_context(dir.plus(b_path.to_str().unwrap())).unwrap();
190
191 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
192
193 cmd.args(&["--verbose", new_la_context])
194 .arg(dir.plus("la"))
195 .succeeds();
196 assert_eq!(get_file_context(dir.plus("la")).unwrap(), la_context);
197 assert_eq!(
198 get_file_context(dir.plus("a")).unwrap().as_deref(),
199 Some(new_la_context)
200 );
201 assert_eq!(
202 get_file_context(dir.plus(b_path.to_str().unwrap())).unwrap(),
203 b_context
204 );
205 }
206
207 #[test]
208 fn valid_context_directory_recursive() {
209 let (dir, mut cmd) = at_and_ucmd!();
210 dir.mkdir("a");
211 dir.symlink_dir("a", "la");
212
213 let b_path = Path::new("a").join("b.txt");
214 dir.touch(b_path.to_str().unwrap());
215
216 let a_context = get_file_context(dir.plus("a")).unwrap();
217 let b_context = get_file_context(dir.plus(b_path.to_str().unwrap())).unwrap();
218
219 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
220
221 // -P (default): do not traverse any symbolic links.
222 cmd.args(&["--verbose", "--recursive", new_la_context])
223 .arg(dir.plus("la"))
224 .succeeds();
225 assert_eq!(
226 get_file_context(dir.plus("la")).unwrap().as_deref(),
227 Some(new_la_context)
228 );
229 assert_eq!(get_file_context(dir.plus("a")).unwrap(), a_context);
230 assert_eq!(
231 get_file_context(dir.plus(b_path.to_str().unwrap())).unwrap(),
232 b_context
233 );
234 }
235
236 #[test]
237 fn valid_context_directory_recursive_follow_args_dir_symlinks() {
238 let (dir, mut cmd) = at_and_ucmd!();
239 dir.mkdir("a");
240 dir.symlink_dir("a", "la");
241
242 let b_path = Path::new("a").join("b.txt");
243 dir.touch(b_path.to_str().unwrap());
244
245 let la_context = get_file_context(dir.plus("la")).unwrap();
246 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
247
248 /*
249 let lc_path = Path::new("a").join("lc");
250 dir.symlink_dir("c", lc_path.to_str().unwrap());
251 assert_eq!(
252 get_file_context(dir.plus(lc_path.to_str().unwrap())).unwrap(),
253 None
254 );
255 */
256
257 // -H: if a command line argument is a symbolic link to a directory, traverse it.
258 cmd.args(&["--verbose", "--recursive", "-H", new_la_context])
259 .arg(dir.plus("la"))
260 .succeeds();
261 assert_eq!(
262 get_file_context(dir.plus("a")).unwrap().as_deref(),
263 Some(new_la_context)
264 );
265 assert_eq!(
266 get_file_context(dir.plus(b_path.to_str().unwrap()))
267 .unwrap()
268 .as_deref(),
269 Some(new_la_context)
270 );
271 assert_eq!(get_file_context(dir.plus("la")).unwrap(), la_context);
272 /*
273 assert_eq!(
274 get_file_context(dir.plus(lc_path.to_str().unwrap()))
275 .unwrap()
276 .as_deref(),
277 Some(new_la_context)
278 );
279 */
280 }
281
282 #[test]
283 fn valid_context_directory_recursive_follow_all_symlinks() {
284 let (dir, mut cmd) = at_and_ucmd!();
285 dir.mkdir("a");
286 dir.symlink_dir("a", "la");
287
288 let b_path = Path::new("a").join("b.txt");
289 dir.touch(b_path.to_str().unwrap());
290
291 let c_path = Path::new("a").join("c");
292 dir.touch(c_path.to_str().unwrap());
293
294 let lc_path = Path::new("a").join("lc");
295 dir.symlink_dir(c_path.to_str().unwrap(), lc_path.to_str().unwrap());
296
297 let la_context = get_file_context(dir.plus("la")).unwrap();
298 let lc_context = get_file_context(dir.plus(lc_path.to_str().unwrap())).unwrap();
299
300 let new_la_context = "guest_u:object_r:etc_t:s0:c42";
301
302 // -L: traverse every symbolic link to a directory encountered.
303 cmd.args(&["--verbose", "--recursive", "-L", new_la_context])
304 .arg(dir.plus("la"))
305 .succeeds();
306 assert_eq!(get_file_context(dir.plus("la")).unwrap(), la_context);
307 assert_eq!(
308 get_file_context(dir.plus("a")).unwrap().as_deref(),
309 Some(new_la_context)
310 );
311 assert_eq!(
312 get_file_context(dir.plus(b_path.to_str().unwrap()))
313 .unwrap()
314 .as_deref(),
315 Some(new_la_context)
316 );
317 assert_eq!(
318 get_file_context(dir.plus(lc_path.to_str().unwrap())).unwrap(),
319 lc_context
320 );
321 assert_eq!(
322 get_file_context(dir.plus(c_path.to_str().unwrap()))
323 .unwrap()
324 .as_deref(),
325 Some(new_la_context)
326 );
327 }
328
329 #[test]
330 fn user_role_range_type() {
331 let (dir, mut cmd) = at_and_ucmd!();
332
333 dir.touch("a.tmp");
334 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
335 if a_context.is_none() {
336 set_file_context(dir.plus("a.tmp"), "unconfined_u:object_r:user_tmp_t:s0").unwrap();
337 }
338
339 cmd.args(&[
340 "--verbose",
341 "--user=guest_u",
342 "--role=object_r",
343 "--type=etc_t",
344 "--range=s0:c42",
345 ])
346 .arg(dir.plus("a.tmp"))
347 .succeeds();
348
349 assert_eq!(
350 get_file_context(dir.plus("a.tmp")).unwrap().as_deref(),
351 Some("guest_u:object_r:etc_t:s0:c42")
352 );
353 }
354
355 #[test]
356 fn user_change() {
357 let (dir, mut cmd) = at_and_ucmd!();
358
359 dir.touch("a.tmp");
360 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
361 let new_a_context = if let Some(a_context) = a_context {
362 let mut components: Vec<_> = a_context.split(':').collect();
363 components[0] = "guest_u";
364 components.join(":")
365 } else {
366 set_file_context(dir.plus("a.tmp"), "unconfined_u:object_r:user_tmp_t:s0").unwrap();
367 String::from("guest_u:object_r:user_tmp_t:s0")
368 };
369
370 cmd.args(&["--verbose", "--user=guest_u"])
371 .arg(dir.plus("a.tmp"))
372 .succeeds();
373 assert_eq!(
374 get_file_context(dir.plus("a.tmp")).unwrap(),
375 Some(new_a_context)
376 );
377 }
378
379 #[test]
380 fn user_change_repeated() {
381 let (dir, mut cmd) = at_and_ucmd!();
382
383 dir.touch("a.tmp");
384 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
385 let new_a_context = if let Some(a_context) = a_context {
386 let mut components: Vec<_> = a_context.split(':').collect();
387 components[0] = "guest_u";
388 components.join(":")
389 } else {
390 set_file_context(dir.plus("a.tmp"), "unconfined_u:object_r:user_tmp_t:s0").unwrap();
391 String::from("guest_u:object_r:user_tmp_t:s0")
392 };
393
394 cmd.args(&["--verbose", "--user=wrong", "--user=guest_u"])
395 .arg(dir.plus("a.tmp"))
396 .succeeds();
397 assert_eq!(
398 get_file_context(dir.plus("a.tmp")).unwrap(),
399 Some(new_a_context)
400 );
401 }
402
403 #[test]
404 fn role_change() {
405 let (dir, mut cmd) = at_and_ucmd!();
406
407 dir.touch("a.tmp");
408 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
409 let new_a_context = if let Some(a_context) = a_context {
410 let mut components: Vec<_> = a_context.split(':').collect();
411 components[1] = "system_r";
412 components.join(":")
413 } else {
414 set_file_context(dir.plus("a.tmp"), "unconfined_u:object_r:user_tmp_t:s0").unwrap();
415 String::from("unconfined_u:system_r:user_tmp_t:s0")
416 };
417
418 cmd.args(&["--verbose", "--role=system_r"])
419 .arg(dir.plus("a.tmp"))
420 .succeeds();
421 assert_eq!(
422 get_file_context(dir.plus("a.tmp")).unwrap(),
423 Some(new_a_context)
424 );
425 }
426
427 #[test]
428 fn type_change() {
429 let (dir, mut cmd) = at_and_ucmd!();
430
431 dir.touch("a.tmp");
432 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
433 let new_a_context = if let Some(a_context) = a_context {
434 let mut components: Vec<_> = a_context.split(':').collect();
435 components[2] = "etc_t";
436 components.join(":")
437 } else {
438 set_file_context(dir.plus("a.tmp"), "unconfined_u:object_r:user_tmp_t:s0").unwrap();
439 String::from("unconfined_u:object_r:etc_t:s0")
440 };
441
442 cmd.args(&["--verbose", "--type=etc_t"])
443 .arg(dir.plus("a.tmp"))
444 .succeeds();
445 assert_eq!(
446 get_file_context(dir.plus("a.tmp")).unwrap(),
447 Some(new_a_context)
448 );
449 }
450
451 #[test]
452 fn range_change() {
453 let (dir, mut cmd) = at_and_ucmd!();
454
455 dir.touch("a.tmp");
456 let a_context = get_file_context(dir.plus("a.tmp")).unwrap();
457 let new_a_context = if let Some(a_context) = a_context {
458 a_context
459 .split(':')
460 .take(3)
461 .chain(iter::once("s0:c42"))
462 .collect::<Vec<_>>()
463 .join(":")
464 } else {
465 set_file_context(dir.plus("a.tmp"), "unconfined_u:object_r:user_tmp_t:s0").unwrap();
466 String::from("unconfined_u:object_r:user_tmp_t:s0:c42")
467 };
468
469 cmd.args(&["--verbose", "--range=s0:c42"])
470 .arg(dir.plus("a.tmp"))
471 .succeeds();
472 assert_eq!(
473 get_file_context(dir.plus("a.tmp")).unwrap(),
474 Some(new_a_context)
475 );
476 }
477
478 #[test]
479 fn valid_reference() {
480 let (dir, mut cmd) = at_and_ucmd!();
481
482 dir.touch("a.tmp");
483 let new_a_context = "guest_u:object_r:etc_t:s0:c42";
484 set_file_context(dir.plus("a.tmp"), new_a_context).unwrap();
485
486 dir.touch("b.tmp");
487 let b_context = get_file_context(dir.plus("b.tmp")).unwrap();
488 assert_ne!(b_context.as_deref(), Some(new_a_context));
489
490 cmd.arg("--verbose")
491 .arg(format!("--reference={}", dir.plus_as_string("a.tmp")))
492 .arg(dir.plus("b.tmp"))
493 .succeeds();
494 assert_eq!(
495 get_file_context(dir.plus("b.tmp")).unwrap().as_deref(),
496 Some(new_a_context)
497 );
498 }
499
500 #[test]
501 fn valid_reference_repeat_flags() {
502 let (dir, mut cmd) = at_and_ucmd!();
503
504 dir.touch("a.tmp");
505 let new_a_context = "guest_u:object_r:etc_t:s0:c42";
506 set_file_context(dir.plus("a.tmp"), new_a_context).unwrap();
507
508 dir.touch("b.tmp");
509 let b_context = get_file_context(dir.plus("b.tmp")).unwrap();
510 assert_ne!(b_context.as_deref(), Some(new_a_context));
511
512 cmd.arg("--verbose")
513 .arg("-vvRRHHLLPP") // spell-checker:disable-line
514 .arg("--no-preserve-root")
515 .arg("--no-preserve-root")
516 .arg("--preserve-root")
517 .arg("--preserve-root")
518 .arg("--dereference")
519 .arg("--dereference")
520 .arg("--no-dereference")
521 .arg("--no-dereference")
522 .arg(format!("--reference={}", dir.plus_as_string("a.tmp")))
523 .arg(dir.plus("b.tmp"))
524 .succeeds();
525 assert_eq!(
526 get_file_context(dir.plus("b.tmp")).unwrap().as_deref(),
527 Some(new_a_context)
528 );
529 }
530
531 #[test]
532 #[ignore = "issue #7443"]
533 fn valid_reference_repeated_reference() {
534 let (dir, mut cmd) = at_and_ucmd!();
535
536 dir.touch("a.tmp");
537 let new_a_context = "guest_u:object_r:etc_t:s0:c42";
538 set_file_context(dir.plus("a.tmp"), new_a_context).unwrap();
539
540 dir.touch("wrong.tmp");
541 let new_wrong_context = "guest_u:object_r:etc_t:s42:c0";
542 set_file_context(dir.plus("wrong.tmp"), new_wrong_context).unwrap();
543
544 dir.touch("b.tmp");
545 let b_context = get_file_context(dir.plus("b.tmp")).unwrap();
546 assert_ne!(b_context.as_deref(), Some(new_a_context));
547
548 cmd.arg("--verbose")
549 .arg(format!("--reference={}", dir.plus_as_string("wrong.tmp")))
550 .arg(format!("--reference={}", dir.plus_as_string("a.tmp")))
551 .arg(dir.plus("b.tmp"))
552 .succeeds();
553 assert_eq!(
554 get_file_context(dir.plus("b.tmp")).unwrap().as_deref(),
555 Some(new_a_context)
556 );
557 assert_eq!(
558 get_file_context(dir.plus("wrong.tmp")).unwrap().as_deref(),
559 Some(new_wrong_context)
560 );
561 }
562
563 #[test]
564 fn valid_reference_multi() {
565 let (dir, mut cmd) = at_and_ucmd!();
566
567 dir.touch("a.tmp");
568 let new_a_context = "guest_u:object_r:etc_t:s0:c42";
569 set_file_context(dir.plus("a.tmp"), new_a_context).unwrap();
570
571 dir.touch("b1.tmp");
572 let b1_context = get_file_context(dir.plus("b1.tmp")).unwrap();
573 assert_ne!(b1_context.as_deref(), Some(new_a_context));
574
575 dir.touch("b2.tmp");
576 let b2_context = get_file_context(dir.plus("b2.tmp")).unwrap();
577 assert_ne!(b2_context.as_deref(), Some(new_a_context));
578
579 cmd.arg("--verbose")
580 .arg(format!("--reference={}", dir.plus_as_string("a.tmp")))
581 .arg(dir.plus("b1.tmp"))
582 .arg(dir.plus("b2.tmp"))
583 .succeeds();
584 assert_eq!(
585 get_file_context(dir.plus("b1.tmp")).unwrap().as_deref(),
586 Some(new_a_context)
587 );
588 assert_eq!(
589 get_file_context(dir.plus("b2.tmp")).unwrap().as_deref(),
590 Some(new_a_context)
591 );
592 }
593
594 fn get_file_context(path: impl AsRef<Path>) -> Result<Option<String>, selinux::errors::Error> {
595 let path = path.as_ref();
596 match selinux::SecurityContext::of_path(path, false, false) {
597 Err(r) => {
598 println!("get_file_context failed: '{}': {r}.", path.display());
599 Err(r)
600 }
601
602 Ok(None) => {
603 println!(
604 "get_file_context: '{}': No SELinux context defined.",
605 path.display()
606 );
607 Ok(None)
608 }
609
610 Ok(Some(context)) => {
611 let bytes = context
612 .as_bytes()
613 .splitn(2, |&b| b == 0_u8)
614 .next()
615 .unwrap_or_default();
616 let context = String::from_utf8(bytes.into()).unwrap_or_default();
617 println!("get_file_context: '{context}' => '{}'.", path.display());
618 Ok(Some(context))
619 }
620 }
621 }
622
623 fn set_file_context(path: impl AsRef<Path>, context: &str) -> Result<(), selinux::errors::Error> {
624 let c_context = CString::new(context.as_bytes()).map_err(|_r| selinux::errors::Error::IO {
625 source: io::Error::from(io::ErrorKind::InvalidInput),
626 operation: "CString::new",
627 })?;
628
629 let path = path.as_ref();
630 let r =
631 selinux::SecurityContext::from_c_str(&c_context, false).set_for_path(path, false, false);
632 if let Err(r) = &r {
633 println!(
634 "set_file_context failed: '{context}' => '{}': {r}.",
635 path.display(),
636 );
637 } else {
638 println!("set_file_context: '{context}' => '{}'.", path.display());
639 }
640 r
641 }