summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2007-01-05 18:23:54 +0100
committerJim Meyering <jim@meyering.net>2007-03-29 21:37:05 +0200
commit3ea540c7570a76bf72ae697c4040e77dad5bea6d (patch)
tree6d22f047af49512ccaf7551b1834c0d2ab6fa682
parent85d6c618b08d8e8e5c8d9d51d65b8c526ec22164 (diff)
downloadcoreutils-3ea540c7570a76bf72ae697c4040e77dad5bea6d.tar.xz
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.
-rw-r--r--ChangeLog19
-rw-r--r--src/Makefile.am6
-rw-r--r--src/ls.c90
-rw-r--r--tests/Makefile.am4
-rwxr-xr-xtests/chmod/equals4
-rwxr-xr-xtests/cp/cp-parents6
-rwxr-xr-xtests/cp/fail-perm5
-rwxr-xr-xtests/cp/link-preserve2
-rwxr-xr-xtests/help-version6
-rwxr-xr-xtests/install/basic-15
-rw-r--r--tests/misc/Makefile.am5
-rwxr-xr-xtests/misc/chcon73
-rwxr-xr-xtests/misc/chcon-fail41
-rwxr-xr-xtests/misc/mknod6
-rwxr-xr-xtests/misc/selinux62
-rwxr-xr-xtests/mkdir/parents8
-rwxr-xr-xtests/mkdir/special-18
-rwxr-xr-xtests/mv/partition-perm2
18 files changed, 278 insertions, 74 deletions
diff --git a/ChangeLog b/ChangeLog
index 9c74ecaad..1396ed803 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -917,25 +917,6 @@
2007-01-04 Jim Meyering <jim@meyering.net>
- * man/chcon.x: New file.
- * man/Makefile.am: Build chcon.1.
-
- New program: chcon
- * gl/modules/selinux-at: New module. Check for libselinux and set
- LIB_SELINUX here, unconditionally, rather than depending on
- the configure-time --enable-selinux option.
- * gl/modules/selinux-h: New module.
- * bootstrap.conf (gnulib_modules): Add selinux-at.
- * gl/lib/selinux-at.c, gl/lib/selinux-at.h: New files.
- * gl/lib/se-selinux_.h: New file.
- * gl/lib/se-context_.h: New file.
- * gl/m4/selinux-selinux-h.m4: New file.
- * gl/m4/selinux-context-h.m4: New file.
- * src/Makefile.am (bin_PROGRAMS): Add chcon.
- (chcon_LDADD): Define.
- * README: Add chcon to the list of programs.
- * src/chcon.c: Rewrite the original (Red Hat) chcon to use fts.
-
* Makefile.cfg (local-checks-to-skip): Skip strftime-check, in
case you don't have convenient access to glibc info documentation.
diff --git a/src/Makefile.am b/src/Makefile.am
index 6a740b65b..8d2580631 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -72,12 +72,12 @@ __LDADD = $(LDADD) $(LIB_EACCESS)
# for clock_gettime and fdatasync
dd_LDADD = $(LDADD) $(LIB_GETHRXTIME) $(LIB_FDATASYNC)
-dir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
-ls_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
+dir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX)
+ls_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX)
pr_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
shred_LDADD = $(LDADD) $(LIB_GETHRXTIME) $(LIB_FDATASYNC)
shuf_LDADD = $(LDADD) $(LIB_GETHRXTIME)
-vdir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
+vdir_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) $(LIB_SELINUX)
## If necessary, add -lm to resolve use of pow in lib/strtod.c.
sort_LDADD = $(LDADD) $(POW_LIB) $(LIB_GETHRXTIME)
diff --git a/src/ls.c b/src/ls.c
index 77abf67be..f8727a246 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -61,6 +61,7 @@
#include <pwd.h>
#include <getopt.h>
#include <signal.h>
+#include <selinux/selinux.h>
/* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
present. */
@@ -167,24 +168,20 @@ struct fileinfo
zero. */
mode_t linkmode;
+ /* SELinux security context. */
+ security_context_t scontext;
+
bool stat_ok;
/* For symbolic link and color printing, true if linked-to file
exists, otherwise false. */
bool linkok;
-#if USE_ACL
- /* For long listings, true if the file has an access control list. */
+ /* For long listings, true if the file has an access control list,
+ or an SELinux security context. */
bool have_acl;
-#endif
};
-#if USE_ACL
-# define FILE_HAS_ACL(F) ((F)->have_acl)
-#else
-# define FILE_HAS_ACL(F) 0
-#endif
-
#define LEN_STR_PAIR(s) sizeof (s) - 1, s
/* Null is a valid character in a color indicator (think about Epson
@@ -320,14 +317,12 @@ static struct pending *pending_dirs;
static time_t current_time = TYPE_MINIMUM (time_t);
static int current_time_ns = -1;
+static bool print_scontext;
+
/* Whether any of the files has an ACL. This affects the width of the
mode column. */
-#if USE_ACL
static bool any_has_acl;
-#else
-enum { any_has_acl = false };
-#endif
/* The number of columns to use for columns containing inode numbers,
block sizes, link counts, owners, groups, authors, major device
@@ -336,6 +331,7 @@ enum { any_has_acl = false };
static int inode_number_width;
static int block_size_width;
static int nlink_width;
+static int scontext_width;
static int owner_width;
static int group_width;
static int author_width;
@@ -787,6 +783,7 @@ static struct option const long_options[] =
{"time-style", required_argument, NULL, TIME_STYLE_OPTION},
{"color", optional_argument, NULL, COLOR_OPTION},
{"block-size", required_argument, NULL, BLOCK_SIZE_OPTION},
+ {"context", no_argument, 0, 'Z'},
{"author", no_argument, NULL, AUTHOR_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
@@ -1246,6 +1243,7 @@ main (int argc, char **argv)
format_needs_stat = sort_type == sort_time || sort_type == sort_size
|| format == long_format
+ || print_scontext
|| print_block_size;
format_needs_type = (! format_needs_stat
&& (recursive
@@ -1439,6 +1437,7 @@ decode_switches (int argc, char **argv)
ignore_mode = IGNORE_DEFAULT;
ignore_patterns = NULL;
hide_patterns = NULL;
+ print_scontext = false;
/* FIXME: put this in a function. */
{
@@ -1514,7 +1513,7 @@ decode_switches (int argc, char **argv)
}
while ((c = getopt_long (argc, argv,
- "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UX1",
+ "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UXZ1",
long_options, NULL)) != -1)
{
switch (c)
@@ -1813,6 +1812,10 @@ decode_switches (int argc, char **argv)
file_output_block_size = output_block_size = 1;
break;
+ case 'Z':
+ print_scontext = true;
+ break;
+
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
@@ -2516,18 +2519,22 @@ clear_files (void)
struct fileinfo *f = sorted_file[i];
free (f->name);
free (f->linkname);
+ if (f->scontext)
+ {
+ freecon (f->scontext);
+ f->scontext = NULL;
+ }
}
cwd_n_used = 0;
-#if USE_ACL
any_has_acl = false;
-#endif
inode_number_width = 0;
block_size_width = 0;
nlink_width = 0;
owner_width = 0;
group_width = 0;
author_width = 0;
+ scontext_width = 0;
major_device_number_width = 0;
minor_device_number_width = 0;
file_size_width = 0;
@@ -2591,7 +2598,7 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
{
/* Absolute name of this file. */
char *absolute_name;
-
+ bool do_deref;
int err;
if (name[0] == '/' || dirname[0] == 0)
@@ -2606,6 +2613,7 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
{
case DEREF_ALWAYS:
err = stat (absolute_name, &f->stat);
+ do_deref = true;
break;
case DEREF_COMMAND_LINE_ARGUMENTS:
@@ -2614,6 +2622,7 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
{
bool need_lstat;
err = stat (absolute_name, &f->stat);
+ do_deref = true;
if (dereference == DEREF_COMMAND_LINE_ARGUMENTS)
break;
@@ -2632,9 +2641,27 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
default: /* DEREF_NEVER */
err = lstat (absolute_name, &f->stat);
+ do_deref = false;
break;
}
+ bool file_has_security_context = false;
+ if (err == 0 && (format == long_format || print_scontext))
+ {
+ int attr_len = (do_deref
+ ? getfilecon (absolute_name, &f->scontext)
+ : lgetfilecon (absolute_name, &f->scontext));
+ err = (attr_len < 0);
+ file_has_security_context = (err == 0);
+
+ /* When requesting security context information, don't make
+ ls fail just because the file (even a command line argument)
+ isn't on the right type of file system. I.e., a getfilecon
+ failure isn't in the same class as a stat failure. */
+ if (err && errno == ENOTSUP)
+ err = 0;
+ }
+
if (err != 0)
{
/* Failure to stat a command line argument leads to
@@ -2653,16 +2680,14 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
f->stat_ok = true;
-#if USE_ACL
if (format == long_format)
{
int n = file_has_acl (absolute_name, &f->stat);
- f->have_acl = (0 < n);
+ f->have_acl = (0 < n || file_has_security_context);
any_has_acl |= f->have_acl;
if (n < 0)
error (0, errno, "%s", quotearg_colon (absolute_name));
}
-#endif
if (S_ISLNK (f->stat.st_mode)
&& (format == long_format || check_symlink_color))
@@ -2738,6 +2763,13 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
author_width = len;
}
+ if (print_scontext)
+ {
+ int len = f->scontext ? strlen (f->scontext) : 0;
+ if (scontext_width < len)
+ scontext_width = len;
+ }
+
{
char buf[INT_BUFSIZE_BOUND (uintmax_t)];
int len = strlen (umaxtostr (f->stat.st_nlink, buf));
@@ -3387,7 +3419,7 @@ print_long_format (const struct fileinfo *f)
struct tm *when_local;
/* Compute the mode string, except remove the trailing space if no
- files in this directory have ACLs. */
+ file in this directory has an ACL or SELinux security context. */
if (f->stat_ok)
filemodestring (&f->stat, modebuf);
else
@@ -3398,7 +3430,7 @@ print_long_format (const struct fileinfo *f)
}
if (! any_has_acl)
modebuf[10] = '\0';
- else if (FILE_HAS_ACL (f))
+ else if (f->have_acl)
modebuf[10] = '+';
switch (time_type)
@@ -3463,7 +3495,7 @@ print_long_format (const struct fileinfo *f)
DIRED_INDENT ();
- if (print_owner | print_group | print_author)
+ if (print_owner | print_group | print_author | print_scontext)
{
DIRED_FPUTS (buf, stdout, p - buf);
@@ -3476,6 +3508,10 @@ print_long_format (const struct fileinfo *f)
if (print_author)
format_user (f->stat.st_author, author_width, f->stat_ok);
+ if (print_scontext)
+ format_user_or_group ((f->scontext ? f->scontext : "?"),
+ 0, scontext_width);
+
p = buf;
}
@@ -3812,6 +3848,10 @@ print_file_name_and_frills (const struct fileinfo *f)
human_readable (ST_NBLOCKS (f->stat), buf, human_output_opts,
ST_NBLOCKSIZE, output_block_size));
+ if (print_scontext)
+ printf ("%*s ", format == with_commas ? 0 : scontext_width,
+ (f->scontext ? f->scontext : "?"));
+
print_name_with_quoting (f->name, FILE_OR_LINK_MODE (f), f->linkok,
f->stat_ok, f->filetype, NULL);
@@ -3975,6 +4015,9 @@ length_of_file_name_and_frills (const struct fileinfo *f)
output_block_size))
: block_size_width);
+ if (print_scontext)
+ len += 1 + (format == with_commas ? strlen (f->scontext) : scontext_width);
+
quote_name (NULL, f->name, filename_quoting_options, &name_width);
len += name_width;
@@ -4403,6 +4446,7 @@ Mandatory arguments to long options are mandatory for short options too.\n\
-w, --width=COLS assume screen width instead of current value\n\
-x list entries by lines instead of by columns\n\
-X sort alphabetically by entry extension\n\
+ -Z, --context print any SELinux security context of each file\n\
-1 list one file per line\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
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 <<EOF > 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