f180de
From 2b3b5bfcd5f4161d17c0bc3d43f6edcfc4a2b294 Mon Sep 17 00:00:00 2001
f180de
From: Nicolas Looss <nicolas.iooss@m4x.org>
f180de
Date: Sat, 4 Jan 2014 03:03:51 +0000
f180de
Subject: [PATCH] copy: fix a segfault in SELinux context copying code
f180de
f180de
* src/selinux.c (restorecon_private): On ArchLinux the
f180de
`fakeroot cp -a file1 file2` command segfaulted due
f180de
to getfscreatecon() returning a NULL context.
f180de
So map this to the sometimes ignored ENODATA error,
f180de
rather than crashing.
f180de
* tests/cp/no-ctx.sh: Add a new test case.
f180de
* tests/local.mk: Reference the new test.
f180de
---
f180de
 src/selinux.c      |    5 ++++
f180de
 tests/cp/no-ctx.sh |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
f180de
 tests/local.mk     |    1 +
f180de
 3 files changed, 59 insertions(+), 0 deletions(-)
f180de
 create mode 100755 tests/cp/no-ctx.sh
f180de
f180de
diff --git a/src/selinux.c b/src/selinux.c
f180de
index cd38a81..016db16 100644
f180de
--- a/src/selinux.c
f180de
+++ b/src/selinux.c
f180de
@@ -192,6 +192,11 @@ restorecon_private (char const *path, bool local)
f180de
     {
f180de
       if (getfscreatecon (&tcon) < 0)
f180de
         return rc;
f180de
+      if (!tcon)
f180de
+        {
f180de
+          errno = ENODATA;
f180de
+          return rc;
f180de
+        }
f180de
       rc = lsetfilecon (path, tcon);
f180de
       freecon (tcon);
f180de
       return rc;
f180de
diff --git a/tests/cp/no-ctx.sh b/tests/cp/no-ctx.sh
f180de
new file mode 100755
f180de
index 0000000..59d30de
f180de
--- /dev/null
f180de
+++ b/tests/cp/no-ctx.sh
f180de
@@ -0,0 +1,53 @@
f180de
+#!/bin/sh
f180de
+# Ensure we handle file systems returning no SELinux context,
f180de
+# which triggered a segmentation fault in coreutils-8.22.
f180de
+# This test is skipped on systems that lack LD_PRELOAD support; that's fine.
f180de
+# Similarly, on a system that lacks lgetfilecon altogether, skipping it is fine.
f180de
+
f180de
+# Copyright (C) 2014 Free Software Foundation, Inc.
f180de
+
f180de
+# This program is free software: you can redistribute it and/or modify
f180de
+# it under the terms of the GNU General Public License as published by
f180de
+# the Free Software Foundation, either version 3 of the License, or
f180de
+# (at your option) any later version.
f180de
+
f180de
+# This program is distributed in the hope that it will be useful,
f180de
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
f180de
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f180de
+# GNU General Public License for more details.
f180de
+
f180de
+# You should have received a copy of the GNU General Public License
f180de
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
f180de
+
f180de
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
f180de
+print_ver_ cp
f180de
+require_gcc_shared_
f180de
+
f180de
+# Replace each getfilecon and lgetfilecon call with a call to these stubs.
f180de
+cat > k.c <<'EOF' || skip_
f180de
+#include <selinux/selinux.h>
f180de
+#include <errno.h>
f180de
+
f180de
+int getfilecon (const char *path, security_context_t *con)
f180de
+{ errno=ENODATA; return -1; }
f180de
+int lgetfilecon (const char *path, security_context_t *con)
f180de
+{ errno=ENODATA; return -1; }
f180de
+EOF
f180de
+
f180de
+# Then compile/link it:
f180de
+$CC -shared -fPIC -O2 k.c -o k.so \
f180de
+  || skip_ 'failed to build SELinux shared library'
f180de
+
f180de
+touch file_src
f180de
+
f180de
+# New file with SELinux context optionally included
f180de
+LD_PRELOAD=./k.so cp -a file_src file_dst || fail=1
f180de
+
f180de
+# Existing file with SELinux context optionally included
f180de
+LD_PRELOAD=./k.so cp -a file_src file_dst || fail=1
f180de
+
f180de
+# ENODATA should give an immediate error when required to preserve ctx
f180de
+# This is debatable, and maybe we should not fail when no context available?
f180de
+LD_PRELOAD=./k.so cp --preserve=context file_src file_dst && fail=1
f180de
+
f180de
+Exit $fail
f180de
diff --git a/tests/local.mk b/tests/local.mk
f180de
index dc7341c..9d556f6 100644
f180de
--- a/tests/local.mk
f180de
+++ b/tests/local.mk
f180de
@@ -161,6 +161,7 @@ all_tests =					\
f180de
   tests/rm/ext3-perf.sh				\
f180de
   tests/rm/cycle.sh				\
f180de
   tests/cp/link-heap.sh				\
f180de
+  tests/cp/no-ctx.sh				\
f180de
   tests/misc/tty-eof.pl				\
f180de
   tests/tail-2/inotify-hash-abuse.sh		\
f180de
   tests/tail-2/inotify-hash-abuse2.sh		\
f180de
-- 
f180de
1.7.7.6
f180de