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