mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0153-New-API-selinux_relabel-SELinux-relabel-parts-of-the.patch

e76f14
From dfb5c5ef41f696da5224ed28cc60bb47c8bed0fa Mon Sep 17 00:00:00 2001
e76f14
From: "Richard W.M. Jones" <rjones@redhat.com>
e76f14
Date: Wed, 13 Jul 2016 18:33:55 +0100
e76f14
Subject: [PATCH] New API: selinux_relabel - SELinux relabel parts of the
e76f14
 filesystem.
e76f14
e76f14
(cherry picked from commit 9d205f1c284a69390907120ca44f5c723fecc244)
e76f14
---
e76f14
 TODO                     |   6 ---
e76f14
 appliance/packagelist.in |   1 +
e76f14
 daemon/Makefile.am       |   1 +
e76f14
 daemon/selinux-relabel.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++
e76f14
 generator/actions.ml     |  23 +++++++++++
e76f14
 gobject/Makefile.inc     |   2 +
e76f14
 po/POTFILES              |   2 +
e76f14
 src/MAX_PROC_NR          |   2 +-
e76f14
 8 files changed, 130 insertions(+), 7 deletions(-)
e76f14
 create mode 100644 daemon/selinux-relabel.c
e76f14
e76f14
diff --git a/TODO b/TODO
e76f14
index 90f6f68..fc20b2a 100644
e76f14
--- a/TODO
e76f14
+++ b/TODO
e76f14
@@ -59,12 +59,6 @@ Ideas for extra commands
e76f14
 
e76f14
   SELinux:
e76f14
     chcat
e76f14
-    restorecon
e76f14
-      [Wanlong Gao submitted patches for restorecon, but
e76f14
-       there are problems with using the restorecon binary
e76f14
-       from the host on the guest.  Most of the time it
e76f14
-       would do more harm than good.]
e76f14
-    setfiles
e76f14
 
e76f14
   Oddball:
e76f14
     pivot_root
e76f14
diff --git a/appliance/packagelist.in b/appliance/packagelist.in
e76f14
index 6349922..38d39f8 100644
e76f14
--- a/appliance/packagelist.in
e76f14
+++ b/appliance/packagelist.in
e76f14
@@ -42,6 +42,7 @@ ifelse(REDHAT,1,
e76f14
   ntfs-3g
e76f14
   openssh-clients
e76f14
   pcre
e76f14
+  policycoreutils
e76f14
   reiserfs-utils
e76f14
   libselinux
e76f14
   syslinux-extlinux
e76f14
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
e76f14
index 20a6289..ac75439 100644
e76f14
--- a/daemon/Makefile.am
e76f14
+++ b/daemon/Makefile.am
e76f14
@@ -167,6 +167,7 @@ guestfsd_SOURCES = \
e76f14
 	rsync.c \
e76f14
 	scrub.c \
e76f14
 	selinux.c \
e76f14
+	selinux-relabel.c \
e76f14
 	sfdisk.c \
e76f14
 	sh.c \
e76f14
 	sleep.c \
e76f14
diff --git a/daemon/selinux-relabel.c b/daemon/selinux-relabel.c
e76f14
new file mode 100644
e76f14
index 0000000..daafe9e
e76f14
--- /dev/null
e76f14
+++ b/daemon/selinux-relabel.c
e76f14
@@ -0,0 +1,100 @@
e76f14
+/* libguestfs - the guestfsd daemon
e76f14
+ * Copyright (C) 2016 Red Hat Inc.
e76f14
+ *
e76f14
+ * This program is free software; you can redistribute it and/or modify
e76f14
+ * it under the terms of the GNU General Public License as published by
e76f14
+ * the Free Software Foundation; either version 2 of the License, or
e76f14
+ * (at your option) any later version.
e76f14
+ *
e76f14
+ * This program is distributed in the hope that it will be useful,
e76f14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
e76f14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e76f14
+ * GNU General Public License for more details.
e76f14
+ *
e76f14
+ * You should have received a copy of the GNU General Public License
e76f14
+ * along with this program; if not, write to the Free Software
e76f14
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e76f14
+ */
e76f14
+
e76f14
+#include <config.h>
e76f14
+
e76f14
+#include <stdio.h>
e76f14
+#include <stdlib.h>
e76f14
+#include <string.h>
e76f14
+
e76f14
+#include "guestfs_protocol.h"
e76f14
+#include "daemon.h"
e76f14
+#include "actions.h"
e76f14
+#include "optgroups.h"
e76f14
+
e76f14
+GUESTFSD_EXT_CMD(str_setfiles, setfiles);
e76f14
+
e76f14
+#define MAX_ARGS 64
e76f14
+
e76f14
+int
e76f14
+optgroup_selinuxrelabel_available (void)
e76f14
+{
e76f14
+  return prog_exists (str_setfiles);
e76f14
+}
e76f14
+
e76f14
+/* Takes optional arguments, consult optargs_bitmask. */
e76f14
+int
e76f14
+do_selinux_relabel (const char *specfile, const char *path,
e76f14
+                    int force)
e76f14
+{
e76f14
+  const char *argv[MAX_ARGS];
e76f14
+  CLEANUP_FREE char *s_dev = NULL, *s_proc = NULL, *s_selinux = NULL,
e76f14
+    *s_sys = NULL, *s_specfile = NULL, *s_path = NULL;
e76f14
+  CLEANUP_FREE char *err = NULL;
e76f14
+  size_t i = 0;
e76f14
+
e76f14
+  s_dev = sysroot_path ("/dev");
e76f14
+  if (!s_dev) {
e76f14
+  malloc_error:
e76f14
+    reply_with_perror ("malloc");
e76f14
+    return -1;
e76f14
+  }
e76f14
+  s_proc = sysroot_path ("/proc");       if (!s_proc) goto malloc_error;
e76f14
+  s_selinux = sysroot_path ("/selinux"); if (!s_selinux) goto malloc_error;
e76f14
+  s_sys = sysroot_path ("/sys");         if (!s_sys) goto malloc_error;
e76f14
+  s_specfile = sysroot_path (specfile);  if (!s_specfile) goto malloc_error;
e76f14
+  s_path = sysroot_path (path);          if (!s_path) goto malloc_error;
e76f14
+
e76f14
+  /* Default settings if not selected. */
e76f14
+  if (!(optargs_bitmask & GUESTFS_SELINUX_RELABEL_FORCE_BITMASK))
e76f14
+    force = 0;
e76f14
+
e76f14
+  ADD_ARG (argv, i, str_setfiles);
e76f14
+  if (force)
e76f14
+    ADD_ARG (argv, i, "-F");
e76f14
+
e76f14
+  /* Exclude some directories that should never be relabelled in
e76f14
+   * ordinary Linux guests.  These won't be mounted anyway.  We have
e76f14
+   * to prefix all these with the sysroot path.
e76f14
+   */
e76f14
+  ADD_ARG (argv, i, "-e"); ADD_ARG (argv, i, s_dev);
e76f14
+  ADD_ARG (argv, i, "-e"); ADD_ARG (argv, i, s_proc);
e76f14
+  ADD_ARG (argv, i, "-e"); ADD_ARG (argv, i, s_selinux);
e76f14
+  ADD_ARG (argv, i, "-e"); ADD_ARG (argv, i, s_sys);
e76f14
+
e76f14
+  /* Relabelling in a chroot. */
e76f14
+  if (STRNEQ (sysroot, "/")) {
e76f14
+    ADD_ARG (argv, i, "-r");
e76f14
+    ADD_ARG (argv, i, sysroot);
e76f14
+  }
e76f14
+
e76f14
+  /* Suppress non-error output. */
e76f14
+  ADD_ARG (argv, i, "-q");
e76f14
+
e76f14
+  /* Add parameters. */
e76f14
+  ADD_ARG (argv, i, s_specfile);
e76f14
+  ADD_ARG (argv, i, s_path);
e76f14
+  ADD_ARG (argv, i, NULL);
e76f14
+
e76f14
+  if (commandv (NULL, &err, argv) == -1) {
e76f14
+    reply_with_perror ("%s", err);
e76f14
+    return -1;
e76f14
+  }
e76f14
+
e76f14
+  return 0;
e76f14
+}
e76f14
diff --git a/generator/actions.ml b/generator/actions.ml
e76f14
index 998caa5..964a42b 100644
e76f14
--- a/generator/actions.ml
e76f14
+++ b/generator/actions.ml
e76f14
@@ -12753,6 +12753,29 @@ See also L<ntfsresize(8)>, L<resize2fs(8)>, L<btrfs(8)>, L<xfs_info(8)>." };
e76f14
     longdesc = "\
e76f14
 This is the internal call which implements C<guestfs_feature_available>." };
e76f14
 
e76f14
+  { defaults with
e76f14
+    name = "selinux_relabel"; added = (1, 33, 43);
e76f14
+    style = RErr, [String "specfile"; Pathname "path"], [OBool "force"];
e76f14
+    proc_nr = Some 467;
e76f14
+    optional = Some "selinuxrelabel";
e76f14
+    test_excuse = "tests are in the tests/relabel directory";
e76f14
+    shortdesc = "relabel parts of the filesystem";
e76f14
+    longdesc = "\
e76f14
+SELinux relabel parts of the filesystem.
e76f14
+
e76f14
+The C<specfile> parameter controls the policy spec file used.
e76f14
+You have to parse C</etc/selinux/config> to find the correct
e76f14
+SELinux policy and then pass the spec file, usually:
e76f14
+C</etc/selinux/> + I<selinuxtype> + C</contexts/files/file_contexts>.
e76f14
+
e76f14
+The required C<path> parameter is the top level directory where
e76f14
+relabelling starts.  Normally you should pass C<path> as C
e76f14
+to relabel the whole guest filesystem.
e76f14
+
e76f14
+The optional C<force> boolean controls whether the context
e76f14
+is reset for customizable files, and also whether the
e76f14
+user, role and range parts of the file context is changed." };
e76f14
+
e76f14
 ]
e76f14
 
e76f14
 (* Non-API meta-commands available only in guestfish.
e76f14
diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc
e76f14
index 4b99a78..349f650 100644
e76f14
--- a/gobject/Makefile.inc
e76f14
+++ b/gobject/Makefile.inc
e76f14
@@ -96,6 +96,7 @@ guestfs_gobject_headers= \
e76f14
   include/guestfs-gobject/optargs-rsync.h \
e76f14
   include/guestfs-gobject/optargs-rsync_in.h \
e76f14
   include/guestfs-gobject/optargs-rsync_out.h \
e76f14
+  include/guestfs-gobject/optargs-selinux_relabel.h \
e76f14
   include/guestfs-gobject/optargs-set_e2attrs.h \
e76f14
   include/guestfs-gobject/optargs-syslinux.h \
e76f14
   include/guestfs-gobject/optargs-tar_in.h \
e76f14
@@ -182,6 +183,7 @@ guestfs_gobject_sources= \
e76f14
   src/optargs-rsync.c \
e76f14
   src/optargs-rsync_in.c \
e76f14
   src/optargs-rsync_out.c \
e76f14
+  src/optargs-selinux_relabel.c \
e76f14
   src/optargs-set_e2attrs.c \
e76f14
   src/optargs-syslinux.c \
e76f14
   src/optargs-tar_in.c \
e76f14
diff --git a/po/POTFILES b/po/POTFILES
e76f14
index bef6540..98d4623 100644
e76f14
--- a/po/POTFILES
e76f14
+++ b/po/POTFILES
e76f14
@@ -94,6 +94,7 @@ daemon/realpath.c
e76f14
 daemon/rename.c
e76f14
 daemon/rsync.c
e76f14
 daemon/scrub.c
e76f14
+daemon/selinux-relabel.c
e76f14
 daemon/selinux.c
e76f14
 daemon/sfdisk.c
e76f14
 daemon/sh.c
e76f14
@@ -221,6 +222,7 @@ gobject/src/optargs-remount.c
e76f14
 gobject/src/optargs-rsync.c
e76f14
 gobject/src/optargs-rsync_in.c
e76f14
 gobject/src/optargs-rsync_out.c
e76f14
+gobject/src/optargs-selinux_relabel.c
e76f14
 gobject/src/optargs-set_e2attrs.c
e76f14
 gobject/src/optargs-syslinux.c
e76f14
 gobject/src/optargs-tar_in.c
e76f14
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
e76f14
index c92ddb6..5873851 100644
e76f14
--- a/src/MAX_PROC_NR
e76f14
+++ b/src/MAX_PROC_NR
e76f14
@@ -1 +1 @@
e76f14
-458
e76f14
+467
e76f14
-- 
aa0300
2.7.4
e76f14