From 3ea540c7570a76bf72ae697c4040e77dad5bea6d Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Fri, 5 Jan 2007 18:23:54 +0100 Subject: ls: Add support for SELinux and a slightly modified -Z option. I started with the patches from Red Hat. The entries below tell how the code evolved. * src/ls.c (print_long_format, print_file_name_and_frills): When there is no security context (due to getfilecon/lgetfilecon failing with e.g. ENOTSUP), print it as "?", not "". * src/ls.c (print_file_name_and_frills): Make -Z work without -l. (length_of_file_name_and_frills): Likewise. * src/ls.c: Remove the --lcontext and --scontext options. Change the way -Z, --context work so that it no longer implies -l. Thus, -Z -l will work like -lcontext and -Z without -l will work like --scontext. Adjust tests to reflect new 'ls -l' syntax -- affects only systems with SELinux when operating on a file with no ACL. These tests assumed that everything before the first space on each line is the 10-byte mode string. But there may also be a "+" in the 11th column, just before the space. However, note that this is not new. The same thing would have happened even without the change below, when listing a file with an ACL. * tests/chmod/equals, tests/cp/cp-parents, tests/cp/fail-perm: * tests/cp/link-preserve, tests/install/basic-1, tests/misc/mknod: * tests/mkdir/parents, tests/mkdir/special-1, tests/mv/partition-perm: Don't make compilation depend on USE_ACL. An SELinux security context counts as an "alternate access control method", so ls must output a "+" for each file with a security context. * src/ls.c [struct fileinfo] (have_acl): Declare unconditionally. (FILE_HAS_ACL): Remove macro definition. Use f->have_acl directly. (gobble_file): Record whether a file has a security context, and update the condition used to determine whether to print the "+". (gobble_file): Call getfilecon/lgetfilecon also when format == long_format, so that we get the "+". * src/ls.c (gobble_file): Add a comment explaining why (with a security context option) ls doesn't exit nonzero due to e.g., getfilecon failing with errno == ENOTSUP. * src/ls.c (gobble_file): Ignore failure of getfilecon if it's due to ENOTSUP. * src/ls.c (gobble_file): Factor out three small blocks using getfilecon and lgetfilecon. Don't ignore return value from getfilecon and lgetfilecon. * src/ls.c (print_long_format): Don't use ?: (empty 2nd arg with C ternary operator). (print_scontext_format): Likewise. (print_scontext): Declare to be "bool", not int. Adjust uses. * src/Makefile.am (dir_LDADD, ls_LDADD, vdir_LDADD): Add $(LIB_SELINUX). * tests/misc/chcon: New file. * tests/misc/chcon-fail: New file. * tests/Makefile.am (check-root): Run new, root-only misc/chcon test. * tests/misc/Makefile.am (TESTS): Add chcon and chcon-fail. * tests/misc/Makefile.am (TESTS): Add selinux. * tests/misc/selinux: New file. * tests/help-version: Skip chcon. --- tests/Makefile.am | 4 ++- tests/chmod/equals | 4 +-- tests/cp/cp-parents | 6 ++-- tests/cp/fail-perm | 5 ++-- tests/cp/link-preserve | 2 +- tests/help-version | 6 ++-- tests/install/basic-1 | 5 ++-- tests/misc/Makefile.am | 5 +++- tests/misc/chcon | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/misc/chcon-fail | 41 +++++++++++++++++++++++++++ tests/misc/mknod | 6 ++-- tests/misc/selinux | 62 +++++++++++++++++++++++++++++++++++++++++ tests/mkdir/parents | 8 +++--- tests/mkdir/special-1 | 8 +++--- tests/mv/partition-perm | 2 +- 15 files changed, 208 insertions(+), 29 deletions(-) create mode 100755 tests/misc/chcon create mode 100755 tests/misc/chcon-fail create mode 100755 tests/misc/selinux (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index aaf0a2908..b7e30cc66 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -30,7 +30,7 @@ SUBDIRS = \ tsort unexpand uniq wc ## N O T E :: Please do not add new directories. -all_t = t1 t2 t3 t4 t5 t6 t7 +all_t = t1 t2 t3 t4 t5 t6 t7 t8 .PHONY: check-root $(all_t) check-root: $(all_t) @@ -48,6 +48,8 @@ t6: cd rm && $(MAKE) check TESTS=one-file-system t7: cd ls && $(MAKE) check TESTS=nameless-uid +t8: + cd misc && $(MAKE) check TESTS=chcon check-recursive: root-hint diff --git a/tests/chmod/equals b/tests/chmod/equals index dfaad17e3..bed16fb3e 100755 --- a/tests/chmod/equals +++ b/tests/chmod/equals @@ -51,7 +51,7 @@ for src in u g o; do for dest in u g o; do test $dest = $src && continue chmod a=,$src=rwx,$dest=$src,$src= f || fail=1 - set _ `ls -l f`; shift; actual_perms=$1 + actual_perms=`ls -l f|cut -b-10` expected_perms=`eval 'echo \$expected_'$dest` test "$actual_perms" = "$expected_perms" || fail=1 done @@ -59,7 +59,7 @@ done umask 027 chmod a=,u=rwx,=u f || fail=1 -set _ `ls -l f`; shift; actual_perms=$1 +actual_perms=`ls -l f|cut -b-10` test "$actual_perms" = "-rwxr-x---" || fail=1 (exit $fail); exit $fail diff --git a/tests/cp/cp-parents b/tests/cp/cp-parents index 6c123d225..384f6c926 100755 --- a/tests/cp/cp-parents +++ b/tests/cp/cp-parents @@ -75,8 +75,8 @@ test -d d/f && fail=1 # Check that re_protect works. chmod go=w d/a cp -a --parents d/a/b/c e || fail=1 -p=`ls -ld e/d|sed 's/ .*//'`; case $p in drwxr-xr-x);; *) fail=1;; esac -p=`ls -ld e/d/a|sed 's/ .*//'`; case $p in drwx-w--w-);; *) fail=1;; esac -p=`ls -ld e/d/a/b/c|sed 's/ .*//'`; case $p in drwxr-xr-x);; *) fail=1;; esac +p=`ls -ld e/d|cut -b-10`; case $p in drwxr-xr-x);; *) fail=1;; esac +p=`ls -ld e/d/a|cut -b-10`; case $p in drwx-w--w-);; *) fail=1;; esac +p=`ls -ld e/d/a/b/c|cut -b-10`; case $p in drwxr-xr-x);; *) fail=1;; esac (exit $fail); exit $fail diff --git a/tests/cp/fail-perm b/tests/cp/fail-perm index 5fff090cd..a7486b6af 100755 --- a/tests/cp/fail-perm +++ b/tests/cp/fail-perm @@ -52,9 +52,8 @@ cp -pR D DD > /dev/null 2>&1 && fail=1 # Permissions on DD must be `dr-x------' -set X `ls -ld DD` -shift -test "$1" = dr-x------ || fail=1 +mode=`ls -ld DD|cut -b-10` +test "$mode" = dr-x------ || fail=1 chmod 0 D ln -s D/D symlink diff --git a/tests/cp/link-preserve b/tests/cp/link-preserve index 8fec65dea..e0138e4b6 100755 --- a/tests/cp/link-preserve +++ b/tests/cp/link-preserve @@ -98,7 +98,7 @@ rm -rf a b c d touch a; chmod 731 a umask 077 cp -a --no-preserve=mode a b -set _ `ls -l b`; shift; mode=$1 +mode=`ls -l b|cut -b-10` test "$mode" = "-rwx------" || fail=1 umask 022 # -------------------------------------- diff --git a/tests/help-version b/tests/help-version index c17cc493b..d3c9bfb18 100755 --- a/tests/help-version +++ b/tests/help-version @@ -2,8 +2,7 @@ # Make sure all these programs work properly # when invoked with --help or --version. -# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software -# Foundation, Inc. +# Copyright (C) 2000-2007 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -72,6 +71,7 @@ for lang in C fr da; do # Skip `test'; it doesn't accept --help or --version. test $i = test && continue; + test $i = chcon && continue; # false fails even when invoked with --help or --version. if test $i = false; then @@ -197,7 +197,7 @@ lbracket_args=": ]" for i in $all_programs; do # Skip these. - case $i in chroot|stty|tty|false) continue;; esac + case $i in chroot|stty|tty|false|chcon) continue;; esac rm -rf $tmp_in $tmp_in2 $tmp_dir $tmp_out echo > $tmp_in diff --git a/tests/install/basic-1 b/tests/install/basic-1 index 327060404..360cd9c3d 100755 --- a/tests/install/basic-1 +++ b/tests/install/basic-1 @@ -98,9 +98,8 @@ ginstall -s -c -m 555 $dd $dir || fail=1 test -f $dd || fail=1 # Make sure that the destination file has the requested permissions. -set X `ls -l $dir/$dd` -shift -test "$1" = -r-xr-xr-x || fail=1 +mode=`ls -l $dir/$dd|cut -b-10` +test "$mode" = -r-xr-xr-x || fail=1 # These failed in coreutils CVS from 2004-06-25 to 2004-08-11. ginstall -d . || fail=1 diff --git a/tests/misc/Makefile.am b/tests/misc/Makefile.am index 4d6a754a8..e4a15cf9e 100644 --- a/tests/misc/Makefile.am +++ b/tests/misc/Makefile.am @@ -1,6 +1,6 @@ # Make miscellaneous coreutils tests. -*-Makefile-*- -# Copyright (C) 200-2007 Free Software Foundation, Inc. +# Copyright (C) 2001-2007 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -43,6 +43,9 @@ TESTS = \ pr \ df-P \ pwd-unreadable-parent \ + chcon \ + chcon-fail \ + selinux \ cut \ wc-files0-from \ wc-files0 \ diff --git a/tests/misc/chcon b/tests/misc/chcon new file mode 100755 index 000000000..88c009baf --- /dev/null +++ b/tests/misc/chcon @@ -0,0 +1,73 @@ +#!/bin/sh +# exercise chcon + +if test "$VERBOSE" = yes; then + set -x + chcon --version +fi + +. $srcdir/../lang-default +PRIV_CHECK_ARG=require-root . $srcdir/../priv-check + +pwd=`pwd` +t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$ +trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0 +trap '(exit $?); exit $?' 1 2 13 15 + +framework_failure=0 +mkdir -p $tmp || framework_failure=1 +cd $tmp || framework_failure=1 +mkdir -p d/sub/s2 || framework_failure=1 +touch f g d/sub/1 d/sub/2 || framework_failure=1 + +if test $framework_failure = 1; then + echo "$0: failure in testing framework" 1>&2 + (exit 1); exit 1 +fi + +fail=0 + +# Set to a specified context. +u1=root +r1=object_r +t1=tmp_t +ctx=$u1:$r1:$t1 +chcon $ctx f || fail=1 +stat --printf='f|%C\n' f > out || fail=1 + +# Use --reference. +chcon --ref=f g || fail=1 +stat --printf='g|%C\n' g >> out || fail=1 + +# Change the individual parts of the context, one by one. +u2=user_u +r2=object_r +t2=file_t +l2=SystemLow-SystemHigh +for i in --user=$u2 --role=$r2 --type=$t2 --range=$l2; do + chcon $i f || fail=1 + stat --printf="f|$i|"'%C\n' f >> out || fail=1 +done + +# Same, but change back using the short-named options. +for i in -u$u1 -r$r1 -t$t1; do + chcon $i f || fail=1 + stat --printf="f|$i|"'%C\n' f >> out || fail=1 +done + +cat < exp || fail=1 +f|$ctx +g|$ctx +f|--user=$u2|$u2:$r1:$t1 +f|--role=$r2|$u2:$r2:$t1 +f|--type=$t2|$u2:$r2:$t2 +f|--range=$l2|$u2:$r2:$t2:$l2 +f|-uroot|root:object_r:file_t:SystemLow-SystemHigh +f|-robject_r|root:object_r:file_t:SystemLow-SystemHigh +f|-ttmp_t|root:object_r:tmp_t:SystemLow-SystemHigh +EOF + +cmp out exp || fail=1 +test $fail = 1 && diff out exp 2> /dev/null + +(exit $fail); exit $fail diff --git a/tests/misc/chcon-fail b/tests/misc/chcon-fail new file mode 100755 index 000000000..d639e86b5 --- /dev/null +++ b/tests/misc/chcon-fail @@ -0,0 +1,41 @@ +#!/bin/sh +# Ensure that chcon fails when it should. +# These tests don't use any actual SE Linux syscalls. + +if test "$VERBOSE" = yes; then + set -x + chcon --version +fi + +. $srcdir/../lang-default + +pwd=`pwd` +t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$ +trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0 +trap '(exit $?); exit $?' 1 2 13 15 + +framework_failure=0 +mkdir -p $tmp || framework_failure=1 +cd $tmp || framework_failure=1 + +if test $framework_failure = 1; then + echo "$0: failure in testing framework" 1>&2 + (exit 1); exit 1 +fi + +fail=0 + +# neither context nor file +chcon 2> /dev/null && fail=1 + +# No file +chcon CON 2> /dev/null && fail=1 + +# No file +touch f +chcon --reference=f 2> /dev/null && fail=1 + +# No file +chcon -u anyone 2> /dev/null && fail=1 + +(exit $fail); exit $fail diff --git a/tests/misc/mknod b/tests/misc/mknod index 12cb0a623..d7fb22eb0 100755 --- a/tests/misc/mknod +++ b/tests/misc/mknod @@ -42,15 +42,15 @@ fail=0 umask 777 mknod -m 734 f1 p || fail=1 -set _ `ls -dgo f1`; shift; mode=$1 +mode=`ls -dgo f1|cut -b-10` test $mode = prwx-wxr-- || fail=1 mkfifo -m 734 f2 || fail=1 -set _ `ls -dgo f2`; shift; mode=$1 +mode=`ls -dgo f2|cut -b-10` test $mode = prwx-wxr-- || fail=1 mkdir -m 734 f3 || fail=1 -set _ `ls -dgo f3`; shift; mode=$1 +mode=`ls -dgo f3|cut -b-10` test $mode = drwx-wxr-- || test $mode = drwx-wsr-- || fail=1 (exit $fail); exit $fail diff --git a/tests/misc/selinux b/tests/misc/selinux new file mode 100755 index 000000000..1207161fd --- /dev/null +++ b/tests/misc/selinux @@ -0,0 +1,62 @@ +#!/bin/sh +# Test SELinux-related options. + +if test "$VERBOSE" = yes; then + set -x + ls --version +fi + +. $srcdir/../envvar-check +. $srcdir/../lang-default +PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check + +test "`ls -Zd .`" = '? .' && + { + echo "$0: skipping this test; this system lacks SELinux support" 1>&2 + exit 77 + } + +pwd=`pwd` +t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$ +trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0 +trap '(exit $?); exit $?' 1 2 13 15 + +framework_failure=0 +mkdir -p $tmp || framework_failure=1 +cd $tmp || framework_failure=1 + +# Create a regular file, dir, fifo. +touch f || framework_failure=1 +mkdir d s1 s2 || framework_failure=1 +mkfifo p || framework_failure=1 + +if test $framework_failure = 1; then + echo "$0: failure in testing framework" 1>&2 + (exit 1); exit 1 +fi + +fail=0 + +ctx=root:object_r:tmp_t +# FIXME, what if $ctx is no different from the default. Not likely. +# give each a different context, via chcon +chcon $ctx f d p || fail=1 +# inspect that context with both ls -Z and stat. +for i in d f p; do + c=`ls -dogZ $i|cut -d' ' -f3`; test x$c = x$ctx || fail=1 + c=`stat --printf %C $i`; test x$c = x$ctx || fail=1 +done + +# Copy each to a new directory and ensure that context is preserved. +cp -r --preserve=all d f p s1 || fail=1 +for i in d f p; do + c=`stat --printf %C s1/$i`; test x$c = x$ctx || fail=1 +done + +# Now, move each to a new directory and ensure that context is preserved. +mv d f p s2 || fail=1 +for i in d f p; do + c=`stat --printf %C s2/$i`; test x$c = x$ctx || fail=1 +done + +(exit $fail); exit $fail diff --git a/tests/mkdir/parents b/tests/mkdir/parents index f0ead6ee8..d956e39d7 100755 --- a/tests/mkdir/parents +++ b/tests/mkdir/parents @@ -65,12 +65,12 @@ d_mode_arg=`"$abs_srcdir/../rwx-to-mode" $d_mode_str` mkdir -p -m $d_mode_arg a/b/c/d # Make sure the permissions of `a' haven't been changed. -p=`ls -ld a|sed 's/ .*//'`; case $p in $mode_str);; *) fail=1;; esac +p=`ls -ld a|cut -b-10`; case $p in $mode_str);; *) fail=1;; esac # `b's and `c's should reflect the umask -p=`ls -ld a/b|sed 's/ .*//'`; case $p in drwx------);; *) fail=1;; esac -p=`ls -ld a/b/c|sed 's/ .*//'`; case $p in drwx------);; *) fail=1;; esac +p=`ls -ld a/b|cut -b-10`; case $p in drwx------);; *) fail=1;; esac +p=`ls -ld a/b/c|cut -b-10`; case $p in drwx------);; *) fail=1;; esac # `d's perms are determined by the -m argument. -p=`ls -ld a/b/c/d|sed 's/ .*//'`; case $p in $d_mode_str);; *) fail=1;; esac +p=`ls -ld a/b/c/d|cut -b-10`; case $p in $d_mode_str);; *) fail=1;; esac (exit $fail); exit $fail diff --git a/tests/mkdir/special-1 b/tests/mkdir/special-1 index a5b65043b..7f8dc7405 100755 --- a/tests/mkdir/special-1 +++ b/tests/mkdir/special-1 @@ -33,8 +33,8 @@ output_mode_string=drwxr-x-wT mkdir -m$set_mode_string $tmp || fail=1 test -d $tmp || fail=1 -set -- `ls -ld $tmp` -case "$1" in +mode=`ls -ld $tmp|cut -b-10` +case "$mode" in $output_mode_string) ;; *) fail=1 ;; esac @@ -49,8 +49,8 @@ mkdir -m$set_mode_string $tmp2 2> /dev/null && fail=1 mkdir --parents -m$set_mode_string $tmp2 || fail=1 test -d $tmp2 || fail=1 -set -- `ls -ld $tmp2` -case "$1" in +mode=`ls -ld $tmp2|cut -b-10` +case "$mode" in $output_mode_string) ;; *) fail=1 ;; esac diff --git a/tests/mv/partition-perm b/tests/mv/partition-perm index 969dc74cd..129146a80 100755 --- a/tests/mv/partition-perm +++ b/tests/mv/partition-perm @@ -49,7 +49,7 @@ test -f file && fail=1 test -f $other_partition_tmpdir/file || fail=1 # This would have failed with the mv from fileutils-4.0i. -set _ `ls -l $other_partition_tmpdir/file`; shift; mode=$1 +mode=`ls -l $other_partition_tmpdir/file|cut -b-10` test "$mode" = "-rwxrwxrwx" || fail=1 exit $fail -- cgit v1.2.3-54-g00ecf