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