public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "William Hubbs" <williamh@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/
Date: Wed, 16 Jul 2014 18:14:40 +0000 (UTC)	[thread overview]
Message-ID: <1405534178.9c689542c3246e793310db938374bc97600435e6.williamh@OpenRC> (raw)

commit:     9c689542c3246e793310db938374bc97600435e6
Author:     Jason Zaman <jason <AT> perfinion <DOT> com>
AuthorDate: Tue Jul 15 18:27:34 2014 +0000
Commit:     William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Wed Jul 16 18:09:38 2014 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/openrc.git;a=commit;h=9c689542

checkpath: restore the SELinux context

X-Gentoo-Bug: 516956
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=516956

---
 src/rc/Makefile          |   6 ++-
 src/rc/checkpath.c       |  28 ++++++++---
 src/rc/rc-selinux-util.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++
 src/rc/rc-selinux-util.h |  33 +++++++++++++
 4 files changed, 186 insertions(+), 7 deletions(-)

diff --git a/src/rc/Makefile b/src/rc/Makefile
index 5f5aa63..fb5265c 100644
--- a/src/rc/Makefile
+++ b/src/rc/Makefile
@@ -4,7 +4,11 @@ SRCS=		checkpath.c fstabinfo.c mountinfo.c start-stop-daemon.c \
 		rc-misc.c rc-plugin.c rc-service.c rc-status.c rc-update.c \
 		runscript.c rc.c swclock.c
 
-CLEANFILES=	version.h
+ifeq (${MKSELINUX},yes)
+SRCS+=		rc-selinux-util.c
+endif
+
+CLEANFILES=	version.h rc-selinux-util.o
 
 BINDIR=		${PREFIX}/bin
 SBINDIR=	${PREFIX}/sbin

diff --git a/src/rc/checkpath.c b/src/rc/checkpath.c
index 6a0f893..6945b67 100644
--- a/src/rc/checkpath.c
+++ b/src/rc/checkpath.c
@@ -46,6 +46,10 @@
 #include "einfo.h"
 #include "rc-misc.h"
 
+#ifdef HAVE_SELINUX
+#include "rc-selinux-util.h"
+#endif
+
 typedef enum {
 	inode_unknown = 0,
 	inode_file = 1,
@@ -55,13 +59,9 @@ typedef enum {
 
 extern const char *applet;
 
-/* TODO: SELinux
- * This needs a LOT of SELinux loving
- * See systemd's src/label.c:label_mkdir
- */
 static int
 do_check(char *path, uid_t uid, gid_t gid, mode_t mode, inode_t type,
-		bool trunc, bool chowner)
+		bool trunc, bool chowner, bool selinux_on)
 {
 	struct stat st;
 	int fd, flags;
@@ -149,6 +149,11 @@ do_check(char *path, uid_t uid, gid_t gid, mode_t mode, inode_t type,
 		}
 	}
 
+#ifdef HAVE_SELINUX
+	if (selinux_on)
+		selinux_util_label(path);
+#endif
+
 	return 0;
 }
 
@@ -226,6 +231,7 @@ checkpath(int argc, char **argv)
 	bool trunc = false;
 	bool chowner = false;
 	bool writable = false;
+	bool selinux_on = false;
 
 	while ((opt = getopt_long(argc, argv, getoptstring,
 		    longopts, (int *) 0)) != -1)
@@ -276,13 +282,23 @@ checkpath(int argc, char **argv)
 	if (gr)
 		gid = gr->gr_gid;
 
+#ifdef HAVE_SELINUX
+	if (1 == selinux_util_open())
+		selinux_on = true;
+#endif
+
 	while (optind < argc) {
 		if (writable)
 			exit(!is_writable(argv[optind]));
-		if (do_check(argv[optind], uid, gid, mode, type, trunc, chowner))
+		if (do_check(argv[optind], uid, gid, mode, type, trunc, chowner, selinux_on))
 			retval = EXIT_FAILURE;
 		optind++;
 	}
 
+#ifdef HAVE_SELINUX
+	if (selinux_on)
+		selinux_util_close();
+#endif
+
 	return retval;
 }

diff --git a/src/rc/rc-selinux-util.c b/src/rc/rc-selinux-util.c
new file mode 100644
index 0000000..6cbb5db
--- /dev/null
+++ b/src/rc/rc-selinux-util.c
@@ -0,0 +1,126 @@
+/*
+  rc-selinux.c
+  SELinux helpers to get and set contexts.
+*/
+
+/*
+ * Copyright (c) 2014 Jason Zaman <jason@perfinion.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stddef.h>
+#include <errno.h>
+
+#include <sys/stat.h>
+
+#include <selinux/selinux.h>
+#include <selinux/label.h>
+
+#include "rc-selinux-util.h"
+
+static struct selabel_handle *hnd = NULL;
+
+int
+selinux_util_label(const char *path)
+{
+	int retval = 0;
+	int enforce;
+	struct stat st;
+	security_context_t con;
+
+	enforce = security_getenforce();
+	if (retval < 0)
+		return retval;
+
+	if (NULL == hnd)
+		return (enforce) ? -1 : 0;
+
+	retval = lstat(path, &st);
+	if (retval < 0) {
+		if (ENOENT == errno)
+			return 0;
+		return (enforce) ? -1 : 0;
+	}
+
+	/* lookup the context */
+	retval = selabel_lookup_raw(hnd, &con, path, st.st_mode);
+	if (retval < 0) {
+		if (ENOENT == errno)
+			return 0;
+		return (enforce) ? -1 : 0;
+	}
+
+	/* apply the context */
+	retval = lsetfilecon(path, con);
+	freecon(con);
+	if (retval < 0) {
+		if (ENOENT == errno)
+			return 0;
+		if (ENOTSUP == errno)
+			return 0;
+		return (enforce) ? -1 : 0;
+	}
+
+	return 0;
+}
+
+/*
+ * Open the label handle
+ * returns 1 on success, 0 if no selinux, negative on error
+ */
+int
+selinux_util_open(void)
+{
+	int retval = 0;
+
+	retval = is_selinux_enabled();
+	if (retval <= 0)
+		return retval;
+
+	hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
+	if (NULL == hnd)
+		return -2;
+
+	return 1;
+}
+
+/*
+ * Close the label handle
+ * returns 1 on success, 0 if no selinux, negative on error
+ */
+int
+selinux_util_close(void)
+{
+	int retval = 0;
+
+	retval = is_selinux_enabled();
+	if (retval <= 0)
+		return retval;
+
+	if (hnd) {
+		selabel_close(hnd);
+		hnd = NULL;
+	}
+
+	return 0;
+}

diff --git a/src/rc/rc-selinux-util.h b/src/rc/rc-selinux-util.h
new file mode 100644
index 0000000..69624b3
--- /dev/null
+++ b/src/rc/rc-selinux-util.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014 Jason Zaman <jason@perfinion.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef RC_SELINUX_UTIL_H
+#define RC_SELINUX_UTIL_H
+
+int selinux_util_open(void);
+int selinux_util_label(const char *path);
+int selinux_util_close(void);
+
+#endif


             reply	other threads:[~2014-07-16 18:14 UTC|newest]

Thread overview: 257+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-16 18:14 William Hubbs [this message]
  -- strict thread matches above, loose matches on Subject: below --
2018-11-06  3:38 [gentoo-commits] proj/openrc:master commit in: src/rc/ William Hubbs
2018-11-06  3:38 William Hubbs
2018-11-02 23:24 William Hubbs
2018-11-02 23:24 William Hubbs
2018-10-15 16:52 William Hubbs
2018-10-15 16:52 William Hubbs
2018-10-09 16:35 William Hubbs
2018-10-06 18:03 William Hubbs
2018-10-06 18:03 William Hubbs
2018-06-29 20:32 William Hubbs
2018-06-28 18:03 William Hubbs
2018-06-20 14:38 William Hubbs
2018-06-19 23:00 William Hubbs
2018-06-19 23:00 William Hubbs
2018-06-19 22:07 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-19 21:34 William Hubbs
2018-06-14 19:56 William Hubbs
2018-06-13 21:28 William Hubbs
2018-05-22 22:12 William Hubbs
2018-05-22 22:12 William Hubbs
2018-05-22 22:12 William Hubbs
2018-05-15 22:11 William Hubbs
2018-05-15 22:11 William Hubbs
2018-05-15  0:11 William Hubbs
2018-05-15  0:11 William Hubbs
2018-05-11 18:19 William Hubbs
2018-05-09 22:35 William Hubbs
2018-05-07 23:21 William Hubbs
2018-05-07 23:21 William Hubbs
2018-03-15  1:43 William Hubbs
2018-03-12  2:43 William Hubbs
2018-02-28 18:45 William Hubbs
2018-02-26 20:34 William Hubbs
2018-02-26 19:15 William Hubbs
2018-02-26 19:15 William Hubbs
2018-02-26 18:23 William Hubbs
2018-02-24 23:03 William Hubbs
2018-02-24 23:03 William Hubbs
2018-02-22 22:17 William Hubbs
2018-02-22 18:55 William Hubbs
2018-02-22  0:17 William Hubbs
2018-02-22  0:17 William Hubbs
2018-02-22  0:17 William Hubbs
2018-02-21 19:50 William Hubbs
2018-02-20 22:36 William Hubbs
2018-02-20 22:36 William Hubbs
2018-02-20 22:36 William Hubbs
2018-02-16 20:07 William Hubbs
2018-02-16 20:07 William Hubbs
2018-02-16 20:07 William Hubbs
2018-02-14 23:37 William Hubbs
2018-02-14 23:37 William Hubbs
2018-01-24 23:44 William Hubbs
2018-01-23 23:08 William Hubbs
2018-01-16 19:36 William Hubbs
2018-01-16 19:14 William Hubbs
2018-01-09 23:35 William Hubbs
2017-11-29 21:12 William Hubbs
2017-11-28 23:17 William Hubbs
2017-11-28 23:17 William Hubbs
2017-11-07 21:33 William Hubbs
2017-11-07 21:33 William Hubbs
2017-11-07 21:33 William Hubbs
2017-11-07 21:33 William Hubbs
2017-10-26 22:01 William Hubbs
2017-10-26 18:58 William Hubbs
2017-10-26 18:58 William Hubbs
2017-10-26 18:58 William Hubbs
2017-10-25 20:10 William Hubbs
2017-10-18 23:09 William Hubbs
2017-10-05 23:31 William Hubbs
2017-09-22 22:25 William Hubbs
2017-09-18 18:31 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-18 18:07 William Hubbs
2017-09-15 18:31 William Hubbs
2017-09-06 22:33 William Hubbs
2017-08-25 16:48 William Hubbs
2017-08-24 16:45 William Hubbs
2017-07-24 23:27 William Hubbs
2017-06-12 15:41 William Hubbs
2017-06-12 15:41 William Hubbs
2017-06-07 16:34 William Hubbs
2017-05-30 21:25 William Hubbs
2017-05-30 21:25 William Hubbs
2017-05-22 16:30 William Hubbs
2017-05-19 23:30 William Hubbs
2017-05-16  0:00 William Hubbs
2017-05-16  0:00 William Hubbs
2017-05-12  2:42 William Hubbs
2017-05-12  2:42 William Hubbs
2017-05-12  2:42 William Hubbs
2017-05-11 16:38 William Hubbs
2017-04-29 22:56 William Hubbs
2017-04-29 22:56 William Hubbs
2017-04-29 22:56 William Hubbs
2017-04-29 14:44 William Hubbs
2017-04-17 17:24 William Hubbs
2017-04-13 17:56 William Hubbs
2017-04-07 12:43 William Hubbs
2017-04-03 15:45 William Hubbs
2017-01-25 23:58 William Hubbs
2017-01-05  0:24 William Hubbs
2017-01-04 23:23 William Hubbs
2016-11-30 22:49 William Hubbs
2016-11-03 16:13 William Hubbs
2016-10-24 17:48 William Hubbs
2016-10-24 17:43 William Hubbs
2016-10-11 15:31 William Hubbs
2016-09-30 22:10 William Hubbs
2016-09-19 17:10 William Hubbs
2016-09-16 13:58 William Hubbs
2016-09-16 13:58 William Hubbs
2016-08-25 16:17 William Hubbs
2016-07-25 18:54 William Hubbs
2016-07-16 20:17 William Hubbs
2016-07-14 17:25 William Hubbs
2016-06-10 22:45 William Hubbs
2016-05-24 16:43 William Hubbs
2016-05-24 16:43 William Hubbs
2016-05-13 18:00 William Hubbs
2016-05-13 17:12 William Hubbs
2016-05-04 23:24 William Hubbs
2016-04-11 16:18 William Hubbs
2016-02-12 18:58 William Hubbs
2016-01-22 18:53 William Hubbs
2016-01-20 17:29 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-14 17:33 William Hubbs
2016-01-12 20:12 William Hubbs
2015-12-06  0:14 William Hubbs
2015-12-02 21:46 William Hubbs
2015-10-13 13:36 William Hubbs
2015-10-05 15:27 William Hubbs
2015-10-04 20:37 William Hubbs
2015-09-18 17:45 William Hubbs
2015-08-02 14:12 William Hubbs
2015-07-23 17:48 William Hubbs
2015-04-24 18:13 William Hubbs
2015-03-25 13:36 William Hubbs
2015-03-25  6:10 William Hubbs
2015-03-25  4:38 William Hubbs
2015-03-24 20:53 William Hubbs
2015-03-24 20:40 William Hubbs
2015-02-19 21:16 William Hubbs
2015-02-15 22:15 William Hubbs
2015-02-15 22:10 William Hubbs
2015-01-22 18:54 William Hubbs
2014-09-20 21:52 William Hubbs
2014-08-22 19:10 William Hubbs
2014-07-25 16:06 ` William Hubbs
2014-08-22 19:10 William Hubbs
2014-08-11 18:18 ` William Hubbs
2014-07-28 15:51 William Hubbs
2014-08-22 19:10 ` William Hubbs
2014-07-19 18:06 William Hubbs
2014-07-18  4:47 William Hubbs
2014-07-16 23:01 William Hubbs
2014-07-16 23:01 William Hubbs
2014-07-16 19:48 William Hubbs
2014-07-16 19:17 William Hubbs
2014-07-13 19:16 William Hubbs
2014-07-11 20:24 William Hubbs
2014-07-11 20:19 William Hubbs
2014-01-18 20:02 William Hubbs
2013-12-13 18:20 William Hubbs
2013-12-13 18:05 William Hubbs
2013-12-01 17:25 William Hubbs
2013-10-12 14:50 William Hubbs
2013-10-06 17:36 William Hubbs
2013-10-04 16:23 William Hubbs
2013-09-24  6:52 William Hubbs
2013-09-23 22:59 William Hubbs
2013-09-23 18:38 William Hubbs
2013-09-23 18:38 William Hubbs
2013-09-04  0:46 William Hubbs
2013-08-26 22:26 William Hubbs
2013-08-26 20:59 William Hubbs
2013-07-23 23:01 William Hubbs
2013-07-16 18:56 William Hubbs
2013-05-01 23:24 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-25 20:50 William Hubbs
2013-02-15 19:35 William Hubbs
2012-10-27 18:12 William Hubbs
2012-10-24 17:38 William Hubbs
2012-10-17 23:48 William Hubbs
2012-10-09 23:56 William Hubbs
2012-10-09 21:50 William Hubbs
2012-09-14 21:00 Christian Ruppert
2012-09-12 19:36 Christian Ruppert
2012-09-12 19:00 Christian Ruppert
2012-08-18 22:26 Christian Ruppert
2012-08-18 21:26 Christian Ruppert
2012-05-23 23:31 Mike Frysinger
2012-05-16 22:16 Mike Frysinger
2012-05-06  5:16 Mike Frysinger
2012-04-24  3:32 Christian Ruppert
2012-02-24  2:38 William Hubbs
2012-02-21  2:09 William Hubbs
2012-02-21  1:08 Jory Pratt
2012-02-12  1:23 Christian Ruppert
2012-02-10 23:25 Christian Ruppert
2012-01-31 22:59 William Hubbs
2012-01-28 18:05 Christian Ruppert
2012-01-28 15:45 Christian Ruppert
2012-01-24 18:41 Christian Ruppert
2012-01-23 10:27 Robin H. Johnson
2012-01-23 10:27 Robin H. Johnson
2012-01-23  5:27 Robin H. Johnson
2012-01-22 20:46 William Hubbs
2011-12-31  2:36 Christian Ruppert
2011-12-30 15:03 Christian Ruppert
2011-12-30 15:03 Christian Ruppert
2011-12-29 12:59 Christian Ruppert
2011-12-29  2:18 Christian Ruppert
2011-12-29  2:10 Christian Ruppert
2011-12-29  1:50 Christian Ruppert
2011-12-11 20:43 William Hubbs
2011-11-19  8:11 Mike Frysinger
2011-11-19  8:11 Mike Frysinger
2011-11-19  8:11 Mike Frysinger
2011-11-17 22:10 William Hubbs
2011-11-15 21:26 William Hubbs
2011-11-09  5:10 Mike Frysinger
2011-09-08 17:22 Christian Ruppert
2011-09-08 17:22 Christian Ruppert
2011-09-02 16:47 William Hubbs
2011-09-01 22:14 William Hubbs
2011-07-26 21:59 William Hubbs
2011-07-20 19:40 William Hubbs
2011-07-05 22:52 Christian Ruppert
2011-07-04 22:54 Christian Ruppert
2011-06-30 18:21 Christian Ruppert
2011-06-27 21:21 Christian Ruppert
2011-06-05 14:52 Christian Ruppert
2011-05-28 16:12 Mike Frysinger
2011-05-28 15:42 Mike Frysinger
2011-05-23 19:25 William Hubbs
2011-02-16 15:02 William Hubbs
2011-02-15  0:50 William Hubbs
2011-02-07  8:30 William Hubbs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1405534178.9c689542c3246e793310db938374bc97600435e6.williamh@OpenRC \
    --to=williamh@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox