mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0155-customize-Add-module-for-doing-SELinux-relabel-of-fi.patch

e76f14
From 6ea415aa049adaa8339e36a90e7ca6592e7090a4 Mon Sep 17 00:00:00 2001
e76f14
From: "Richard W.M. Jones" <rjones@redhat.com>
e76f14
Date: Wed, 13 Jul 2016 18:34:21 +0100
e76f14
Subject: [PATCH] customize: Add module for doing SELinux relabel of filesystem
e76f14
 (RHBZ#554829, RHBZ#983969, RHBZ#1089100).
e76f14
e76f14
This implements the --selinux-relabel option for virt-customize,
e76f14
virt-builder and virt-sysprep.  There is no need to autorelabel
e76f14
functionality now.
e76f14
e76f14
Thanks: Stephen Smalley
e76f14
(cherry picked from commit f3c69fe60bc29ebfcef0ea9d86d407e1a88686b0)
e76f14
---
e76f14
 builder/Makefile.am           |  1 +
e76f14
 builder/virt-builder.pod      | 18 ++++++--------
e76f14
 customize/Makefile.am         |  2 ++
e76f14
 customize/SELinux_relabel.ml  | 57 +++++++++++++++++++++++++++++++++++++++++++
e76f14
 customize/SELinux_relabel.mli | 29 ++++++++++++++++++++++
e76f14
 customize/customize_run.ml    | 14 +----------
e76f14
 po/POTFILES-ml                |  1 +
e76f14
 sysprep/Makefile.am           |  1 +
e76f14
 8 files changed, 100 insertions(+), 23 deletions(-)
e76f14
 create mode 100644 customize/SELinux_relabel.ml
e76f14
 create mode 100644 customize/SELinux_relabel.mli
e76f14
e76f14
diff --git a/builder/Makefile.am b/builder/Makefile.am
e76f14
index d69e7ec..d306293 100644
e76f14
--- a/builder/Makefile.am
e76f14
+++ b/builder/Makefile.am
e76f14
@@ -154,6 +154,7 @@ BOBJECTS = \
e76f14
 	$(top_builddir)/customize/perl_edit.cmo \
e76f14
 	$(top_builddir)/customize/crypt.cmo \
e76f14
 	$(top_builddir)/customize/password.cmo \
e76f14
+	$(top_builddir)/customize/SELinux_relabel.cmo \
e76f14
 	$(top_builddir)/customize/ssh_key.cmo \
e76f14
 	$(top_builddir)/customize/subscription_manager.cmo \
e76f14
 	$(top_builddir)/customize/customize_cmdline.cmo \
e76f14
diff --git a/builder/virt-builder.pod b/builder/virt-builder.pod
e76f14
index 5e344de..4209245 100644
e76f14
--- a/builder/virt-builder.pod
e76f14
+++ b/builder/virt-builder.pod
e76f14
@@ -1754,20 +1754,19 @@ two possible strategies it can use to ensure correct labelling:
e76f14
 
e76f14
 =item Using I<--selinux-relabel>
e76f14
 
e76f14
-This runs L<fixfiles(8)> just before finalizing the guest, which sets
e76f14
+This runs L<setfiles(8)> just before finalizing the guest, which sets
e76f14
 SELinux labels correctly in the disk image.
e76f14
 
e76f14
-Sometimes fixfiles is not possible during installation, in which case
e76f14
-this option falls back on:
e76f14
+This is the recommended method.
e76f14
 
e76f14
-=item Touching F</.autorelabel>
e76f14
+=item I<--touch> F</.autorelabel>
e76f14
 
e76f14
-Guest templates may already contain a file called F</.autorelabel>, or
e76f14
-it is touched if I<--selinux-relabel> cannot run fixfiles.
e76f14
+Guest templates may already contain a file called F</.autorelabel> or
e76f14
+you may touch it.
e76f14
 
e76f14
-For guests that use SELinux, this causes fixfiles to run at first
e76f14
-boot.  Guests will reboot themselves once the first time you use them,
e76f14
-which is normal and harmless.
e76f14
+For guests that use SELinux, this causes L<restorecon(8)> to run at
e76f14
+first boot.  Guests will reboot themselves once the first time you use
e76f14
+them, which is normal and harmless.
e76f14
 
e76f14
 =back
e76f14
 
e76f14
@@ -1876,7 +1875,6 @@ L<gpg(1)>,
e76f14
 L<curl(1)>,
e76f14
 L<virt-make-fs(1)>,
e76f14
 L<genisoimage(1)>,
e76f14
-L<fixfiles(8)>,
e76f14
 L<http://libguestfs.org/>.
e76f14
 
e76f14
 =head1 AUTHOR
e76f14
diff --git a/customize/Makefile.am b/customize/Makefile.am
e76f14
index 05f144f..9b48e8f 100644
e76f14
--- a/customize/Makefile.am
e76f14
+++ b/customize/Makefile.am
e76f14
@@ -43,6 +43,7 @@ SOURCES_MLI = \
e76f14
 	password.mli \
e76f14
 	perl_edit.mli \
e76f14
 	random_seed.mli \
e76f14
+	SELinux_relabel.mli \
e76f14
 	ssh_key.mli \
e76f14
 	subscription_manager.mli \
e76f14
 	timezone.mli \
e76f14
@@ -58,6 +59,7 @@ SOURCES_ML = \
e76f14
 	password.ml \
e76f14
 	perl_edit.ml \
e76f14
 	random_seed.ml \
e76f14
+	SELinux_relabel.ml \
e76f14
 	ssh_key.ml \
e76f14
 	subscription_manager.ml \
e76f14
 	timezone.ml \
e76f14
diff --git a/customize/SELinux_relabel.ml b/customize/SELinux_relabel.ml
e76f14
new file mode 100644
e76f14
index 0000000..fa9603c
e76f14
--- /dev/null
e76f14
+++ b/customize/SELinux_relabel.ml
e76f14
@@ -0,0 +1,57 @@
e76f14
+(* virt-customize
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 along
e76f14
+ * with this program; if not, write to the Free Software Foundation, Inc.,
e76f14
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e76f14
+ *)
e76f14
+
e76f14
+open Common_gettext.Gettext
e76f14
+open Common_utils
e76f14
+
e76f14
+open Printf
e76f14
+
e76f14
+module G = Guestfs
e76f14
+
e76f14
+let relabel (g : G.guestfs) =
e76f14
+  (* Is the guest using SELinux? *)
e76f14
+  if g#is_file ~followsymlinks:true "/usr/sbin/load_policy" &&
e76f14
+     g#is_file ~followsymlinks:true "/etc/selinux/config" then (
e76f14
+    (* Is setfiles / SELinux relabelling functionality available? *)
e76f14
+    if g#feature_available [| "selinuxrelabel" |] then (
e76f14
+      (* Use Augeas to parse /etc/selinux/config. *)
e76f14
+      g#aug_init "/" (16+32) (* AUG_SAVE_NOOP | AUG_NO_LOAD *);
e76f14
+      (* See: https://bugzilla.redhat.com/show_bug.cgi?id=975412#c0 *)
e76f14
+      ignore (g#aug_rm "/augeas/load/*[\"/etc/selinux/config/\" !~ regexp('^') + glob(incl) + regexp('/.*')]");
e76f14
+      g#aug_load ();
e76f14
+      debug_augeas_errors g;
e76f14
+
e76f14
+      (* Get the SELinux policy name, eg. "targeted", "minimum". *)
e76f14
+      let policy = g#aug_get "/files/etc/selinux/config/SELINUXTYPE" in
e76f14
+      g#aug_close ();
e76f14
+
e76f14
+      (* Get the spec file name. *)
e76f14
+      let specfile =
e76f14
+        sprintf "/etc/selinux/%s/contexts/files/file_contexts" policy in
e76f14
+
e76f14
+      (* Relabel everything. *)
e76f14
+      g#selinux_relabel ~force:true specfile "/";
e76f14
+
e76f14
+      (* If that worked, we don't need to autorelabel. *)
e76f14
+      g#rm_f "/.autorelabel"
e76f14
+    )
e76f14
+    else (
e76f14
+      (* SELinux guest, but not SELinux host.  Fallback to this. *)
e76f14
+      g#touch "/.autorelabel"
e76f14
+    )
e76f14
+  )
e76f14
diff --git a/customize/SELinux_relabel.mli b/customize/SELinux_relabel.mli
e76f14
new file mode 100644
e76f14
index 0000000..7b4f7ff
e76f14
--- /dev/null
e76f14
+++ b/customize/SELinux_relabel.mli
e76f14
@@ -0,0 +1,29 @@
e76f14
+(* virt-customize
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 along
e76f14
+ * with this program; if not, write to the Free Software Foundation, Inc.,
e76f14
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e76f14
+ *)
e76f14
+
e76f14
+(** SELinux-relabel the filesystem. *)
e76f14
+
e76f14
+val relabel : Guestfs.guestfs -> unit
e76f14
+(** Relabel the mounted guestfs filesystem using the current SELinux
e76f14
+    policy that applies to the guest.
e76f14
+
e76f14
+    If the guest does not look like it uses SELinux, this does nothing.
e76f14
+
e76f14
+    In case relabelling is not possible (since it is an optional
e76f14
+    feature which requires the setfiles(8) program), instead we
e76f14
+    fall back to touching [/.autorelabel]. *)
e76f14
diff --git a/customize/customize_run.ml b/customize/customize_run.ml
e76f14
index 10647fd..7d1c806 100644
e76f14
--- a/customize/customize_run.ml
e76f14
+++ b/customize/customize_run.ml
e76f14
@@ -412,19 +412,7 @@ exec >>%s 2>&1
e76f14
 
e76f14
   if ops.flags.selinux_relabel then (
e76f14
     message (f_"SELinux relabelling");
e76f14
-    if guest_arch_compatible then (
e76f14
-      let cmd = sprintf "
e76f14
-        if load_policy && fixfiles restore; then
e76f14
-          rm -f /.autorelabel
e76f14
-        else
e76f14
-          touch /.autorelabel
e76f14
-          echo '%s: SELinux relabelling failed, will relabel at boot instead.'
e76f14
-        fi
e76f14
-      " prog in
e76f14
-      do_run ~display:"load_policy && fixfiles restore" cmd
e76f14
-    ) else (
e76f14
-      g#touch "/.autorelabel"
e76f14
-    )
e76f14
+    SELinux_relabel.relabel g
e76f14
   );
e76f14
 
e76f14
   (* Clean up the log file:
e76f14
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
e76f14
index 4ea49a5..f5e8eba 100644
e76f14
--- a/po/POTFILES-ml
e76f14
+++ b/po/POTFILES-ml
e76f14
@@ -17,6 +17,7 @@ builder/sources.ml
e76f14
 builder/utils.ml
e76f14
 builder/yajl.ml
e76f14
 builder/yajl_tests.ml
e76f14
+customize/SELinux_relabel.ml
e76f14
 customize/crypt.ml
e76f14
 customize/customize_cmdline.ml
e76f14
 customize/customize_main.ml
e76f14
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
e76f14
index 46cc324..b689aef 100644
e76f14
--- a/sysprep/Makefile.am
e76f14
+++ b/sysprep/Makefile.am
e76f14
@@ -122,6 +122,7 @@ BOBJECTS = \
e76f14
 	$(top_builddir)/customize/timezone.cmo \
e76f14
 	$(top_builddir)/customize/firstboot.cmo \
e76f14
 	$(top_builddir)/customize/perl_edit.cmo \
e76f14
+	$(top_builddir)/customize/SELinux_relabel.cmo \
e76f14
 	$(top_builddir)/customize/ssh_key.cmo \
e76f14
 	$(top_builddir)/customize/subscription_manager.cmo \
e76f14
 	$(top_builddir)/customize/customize_cmdline.cmo \
e76f14
-- 
e76f14
1.8.3.1
e76f14