Blame SOURCES/coreutils-8.22-cp-selinux.patch

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