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