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