Blame SOURCES/0002-adopt-inversion-of-SELinux-relabeling-in-virt-custom.patch

7b180b
From 5792f2e95bcddf476f2fe37e0bc4d97bd881d8fa Mon Sep 17 00:00:00 2001
7b180b
From: Laszlo Ersek <lersek@redhat.com>
7b180b
Date: Tue, 10 May 2022 12:50:46 +0200
7b180b
Subject: [PATCH] adopt inversion of SELinux relabeling in virt-customize
7b180b
7b180b
Remove "--selinux-relabel" options.
7b180b
7b180b
Do not add any "--no-selinux-relabel" options; rely on the internal check
7b180b
for SELinux support instead ("is_selinux_guest" in
7b180b
"common/mlcustomize/SELinux_relabel.ml").
7b180b
7b180b
"--no-selinux-relabel" becomes a real option for virt-sysprep now.
7b180b
(Again?)
7b180b
7b180b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1554735
7b180b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2075718
7b180b
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
7b180b
Message-Id: <20220510105046.15167-1-lersek@redhat.com>
7b180b
Acked-by: Richard W.M. Jones <rjones@redhat.com>
7b180b
[lersek@redhat.com: incorporate common submodule update]
7b180b
(cherry picked from commit 19de3d1c8d4efb53565dbffe532d41ee9d25a832)
7b180b
---
7b180b
 builder/templates/make-template.ml |  8 +-------
7b180b
 builder/virt-builder.pod           | 20 ++++----------------
7b180b
 common                             |  2 +-
7b180b
 customize/customize_run.ml         |  2 +-
7b180b
 customize/test-settings.sh         |  3 ---
7b180b
 sysprep/main.ml                    |  2 --
7b180b
 sysprep/test-virt-sysprep-docs.sh  |  2 +-
7b180b
 7 files changed, 8 insertions(+), 31 deletions(-)
7b180b
7b180b
diff --git a/builder/templates/make-template.ml b/builder/templates/make-template.ml
7b180b
index 6786fec19..b40789284 100755
7b180b
--- a/builder/templates/make-template.ml
7b180b
+++ b/builder/templates/make-template.ml
7b180b
@@ -256,8 +256,7 @@ let rec main () =
7b180b
     printf "Sysprepping ...\n%!";
7b180b
     let cmd =
7b180b
       sprintf "virt-sysprep --quiet -a %s%s"
7b180b
-              (quote tmpout)
7b180b
-              (if is_selinux_os os then " --selinux-relabel" else "") in
7b180b
+              (quote tmpout) in
7b180b
     if Sys.command cmd <> 0 then exit 1
7b180b
   );
7b180b
 
7b180b
@@ -480,11 +479,6 @@ and can_sysprep_os = function
7b180b
   | Debian _ | Ubuntu _ -> true
7b180b
   | FreeBSD _ | Windows _ -> false
7b180b
 
7b180b
-and is_selinux_os = function
7b180b
-  | RHEL _ | Alma _ | CentOS _ | CentOSStream _ | Fedora _ -> true
7b180b
-  | Debian _ | Ubuntu _
7b180b
-  | FreeBSD _ | Windows _ -> false
7b180b
-
7b180b
 and needs_uefi os arch =
7b180b
   match os, arch with
7b180b
   | Fedora _, Armv7
7b180b
diff --git a/builder/virt-builder.pod b/builder/virt-builder.pod
7b180b
index f7dd6cdad..aeb505296 100644
7b180b
--- a/builder/virt-builder.pod
7b180b
+++ b/builder/virt-builder.pod
7b180b
@@ -131,12 +131,6 @@ To update the installed packages to the latest version:
7b180b
 
7b180b
  virt-builder debian-7 --update
7b180b
 
7b180b
-For guests which use SELinux, like Fedora and Red Hat Enterprise
7b180b
-Linux, you may need to do SELinux relabelling after installing or
7b180b
-updating packages (see L</SELINUX> below):
7b180b
-
7b180b
- virt-builder fedora-27 --update --selinux-relabel
7b180b
-
7b180b
 =head2 Customizing the installation
7b180b
 
7b180b
 There are many options that let you customize the installation.  These
7b180b
@@ -972,7 +966,7 @@ command line.
7b180b
 
7b180b
 =item *
7b180b
 
7b180b
-SELinux relabelling is done (I<--selinux-relabel>).
7b180b
+SELinux relabelling is done unless disabled with I<--no-selinux-relabel>.
7b180b
 
7b180b
 =back
7b180b
 
7b180b
@@ -1072,8 +1066,7 @@ A typical virt-builder command would be:
7b180b
    --install puppet \
7b180b
    --append-line '/etc/puppet/puppet.conf:[agent]' \
7b180b
    --append-line '/etc/puppet/puppet.conf:server = puppetmaster.example.com/' \
7b180b
-   --run-command 'systemctl enable puppet' \
7b180b
-   --selinux-relabel
7b180b
+   --run-command 'systemctl enable puppet'
7b180b
 
7b180b
 The precise instructions vary according to the Linux distro.  For
7b180b
 further information see:
7b180b
@@ -1753,14 +1746,14 @@ two possible strategies it can use to ensure correct labelling:
7b180b
 
7b180b
 =over 4
7b180b
 
7b180b
-=item Using I<--selinux-relabel>
7b180b
+=item Automatic relabeling
7b180b
 
7b180b
 This runs L<setfiles(8)> just before finalizing the guest, which sets
7b180b
 SELinux labels correctly in the disk image.
7b180b
 
7b180b
 This is the recommended method.
7b180b
 
7b180b
-=item I<--touch> F</.autorelabel>
7b180b
+=item Using I<--no-selinux-relabel> I<--touch> F</.autorelabel>
7b180b
 
7b180b
 Guest templates may already contain a file called F</.autorelabel> or
7b180b
 you may touch it.
7b180b
@@ -1771,11 +1764,6 @@ them, which is normal and harmless.
7b180b
 
7b180b
 =back
7b180b
 
7b180b
-Please note that if your guest uses SELinux, and you are doing operations
7b180b
-on it which might create new files or change existing ones, you are
7b180b
-recommended to use I<--selinux-relabel>.  This will help in making sure
7b180b
-that files have the right SELinux labels.
7b180b
-
7b180b
 =head1 MACHINE READABLE OUTPUT
7b180b
 
7b180b
 The I<--machine-readable> option can be used to make the output more
7b180b
Submodule common 0a231b3e6..48527b876:
7b180b
diff --git a/common/mlcustomize/customize-options.pod b/common/mlcustomize/customize-options.pod
7b180b
index 71b545d..a83c80a 100644
7b180b
--- a/common/mlcustomize/customize-options.pod
7b180b
+++ b/common/mlcustomize/customize-options.pod
7b180b
@@ -206,6 +206,19 @@ the image was built, use this option.
7b180b
 
7b180b
 See also: L</LOG FILE>.
7b180b
 
7b180b
+=item B<--no-selinux-relabel>
7b180b
+
7b180b
+Do not attempt to correct the SELinux labels of files in the guest.
7b180b
+
7b180b
+In such guests that support SELinux, customization automatically
7b180b
+relabels files so that they have the correct SELinux label.  (The
7b180b
+relabeling is performed immediately, but if the operation fails,
7b180b
+customization will instead touch F</.autorelabel> on the image to
7b180b
+schedule a relabel operation for the next time the image boots.)  This
7b180b
+option disables the automatic relabeling.
7b180b
+
7b180b
+The option is a no-op for guests that do not support SELinux.
7b180b
+
7b180b
 =item B<--password> USER:SELECTOR
7b180b
 
7b180b
 Set the password for C<USER>.  (Note this option does I<not>
7b180b
@@ -297,16 +310,6 @@ It cannot delete directories, only regular files.
7b180b
 
7b180b
 =back
7b180b
 
7b180b
-=item B<--selinux-relabel>
7b180b
-
7b180b
-Relabel files in the guest so that they have the correct SELinux label.
7b180b
-
7b180b
-This will attempt to relabel files immediately, but if the operation fails
7b180b
-this will instead touch F</.autorelabel> on the image to schedule a
7b180b
-relabel operation for the next time the image boots.
7b180b
-
7b180b
-You should only use this option for guests which support SELinux.
7b180b
-
7b180b
 =item B<--sm-attach> SELECTOR
7b180b
 
7b180b
 Attach to a pool using C<subscription-manager>.
7b180b
diff --git a/common/mlcustomize/customize-synopsis.pod b/common/mlcustomize/customize-synopsis.pod
7b180b
index 5f18540..2520853 100644
7b180b
--- a/common/mlcustomize/customize-synopsis.pod
7b180b
+++ b/common/mlcustomize/customize-synopsis.pod
7b180b
@@ -12,5 +12,5 @@
7b180b
     [--truncate-recursive PATH] [--timezone TIMEZONE] [--touch FILE]
7b180b
     [--uninstall PKG,PKG..] [--update] [--upload FILE:DEST]
7b180b
     [--write FILE:CONTENT] [--no-logfile]
7b180b
-    [--password-crypto md5|sha256|sha512] [--selinux-relabel]
7b180b
+    [--password-crypto md5|sha256|sha512] [--no-selinux-relabel]
7b180b
     [--sm-credentials SELECTOR]
7b180b
diff --git a/common/mlcustomize/customize_cmdline.ml b/common/mlcustomize/customize_cmdline.ml
7b180b
index 9326baa..5d404e8 100644
7b180b
--- a/common/mlcustomize/customize_cmdline.ml
7b180b
+++ b/common/mlcustomize/customize_cmdline.ml
7b180b
@@ -109,8 +109,8 @@ and flags = {
7b180b
       (* --no-logfile *)
7b180b
   password_crypto : Password.password_crypto option;
7b180b
       (* --password-crypto md5|sha256|sha512 *)
7b180b
-  selinux_relabel : bool;
7b180b
-      (* --selinux-relabel *)
7b180b
+  no_selinux_relabel : bool;
7b180b
+      (* --no-selinux-relabel *)
7b180b
   sm_credentials : Subscription_manager.sm_credentials option;
7b180b
       (* --sm-credentials SELECTOR *)
7b180b
 }
7b180b
@@ -121,7 +121,7 @@ let rec argspec () =
7b180b
   let ops = ref [] in
7b180b
   let scrub_logfile = ref false in
7b180b
   let password_crypto = ref None in
7b180b
-  let selinux_relabel = ref false in
7b180b
+  let no_selinux_relabel = ref false in
7b180b
   let sm_credentials = ref None in
7b180b
 
7b180b
   let rec get_ops () = {
7b180b
@@ -131,7 +131,7 @@ let rec argspec () =
7b180b
   and get_flags () = {
7b180b
     scrub_logfile = !scrub_logfile;
7b180b
     password_crypto = !password_crypto;
7b180b
-    selinux_relabel = !selinux_relabel;
7b180b
+    no_selinux_relabel = !no_selinux_relabel;
7b180b
     sm_credentials = !sm_credentials;
7b180b
   }
7b180b
   in
7b180b
@@ -459,11 +459,11 @@ let rec argspec () =
7b180b
     ),
7b180b
     Some "md5|sha256|sha512", "When the virt tools change or set a password in the guest, this\noption sets the password encryption of that password to\nC<md5>, C<sha256> or C<sha512>.\n\nC<sha256> and C<sha512> require glibc E<ge> 2.7 (check crypt(3) inside\nthe guest).\n\nC<md5> will work with relatively old Linux guests (eg. RHEL 3), but\nis not secure against modern attacks.\n\nThe default is C<sha512> unless libguestfs detects an old guest that\ndidn't have support for SHA-512, in which case it will use C<md5>.\nYou can override libguestfs by specifying this option.\n\nNote this does not change the default password encryption used\nby the guest when you create new user accounts inside the guest.\nIf you want to do that, then you should use the I<--edit> option\nto modify C</etc/sysconfig/authconfig> (Fedora, RHEL) or\nC</etc/pam.d/common-password> (Debian, Ubuntu).";
7b180b
     (
7b180b
-      [ L"selinux-relabel" ],
7b180b
-      Getopt.Set selinux_relabel,
7b180b
-      s_"Relabel files with correct SELinux labels"
7b180b
+      [ L"no-selinux-relabel" ],
7b180b
+      Getopt.Set no_selinux_relabel,
7b180b
+      s_"Do not relabel files with correct SELinux labels"
7b180b
     ),
7b180b
-    None, "Relabel files in the guest so that they have the correct SELinux label.\n\nThis will attempt to relabel files immediately, but if the operation fails\nthis will instead touch F</.autorelabel> on the image to schedule a\nrelabel operation for the next time the image boots.\n\nYou should only use this option for guests which support SELinux.";
7b180b
+    None, "Do not attempt to correct the SELinux labels of files in the guest.\n\nIn such guests that support SELinux, customization automatically\nrelabels files so that they have the correct SELinux label.  (The\nrelabeling is performed immediately, but if the operation fails,\ncustomization will instead touch F</.autorelabel> on the image to\nschedule a relabel operation for the next time the image boots.)  This\noption disables the automatic relabeling.\n\nThe option is a no-op for guests that do not support SELinux.";
7b180b
     (
7b180b
       [ L"sm-credentials" ],
7b180b
       Getopt.String (
7b180b
diff --git a/common/mlcustomize/customize_cmdline.mli b/common/mlcustomize/customize_cmdline.mli
7b180b
index 14eda49..7ee882a 100644
7b180b
--- a/common/mlcustomize/customize_cmdline.mli
7b180b
+++ b/common/mlcustomize/customize_cmdline.mli
7b180b
@@ -101,8 +101,8 @@ and flags = {
7b180b
       (* --no-logfile *)
7b180b
   password_crypto : Password.password_crypto option;
7b180b
       (* --password-crypto md5|sha256|sha512 *)
7b180b
-  selinux_relabel : bool;
7b180b
-      (* --selinux-relabel *)
7b180b
+  no_selinux_relabel : bool;
7b180b
+      (* --no-selinux-relabel *)
7b180b
   sm_credentials : Subscription_manager.sm_credentials option;
7b180b
       (* --sm-credentials SELECTOR *)
7b180b
 }
7b180b
diff --git a/common/mlcustomize/test-firstboot.sh b/common/mlcustomize/test-firstboot.sh
7b180b
index b906997..24c67f3 100755
7b180b
--- a/common/mlcustomize/test-firstboot.sh
7b180b
+++ b/common/mlcustomize/test-firstboot.sh
7b180b
@@ -61,9 +61,6 @@ case "$guestname" in
7b180b
         extra[${#extra[*]}]='/etc/inittab:
7b180b
                                 s,^#([1-9].*respawn.*/sbin/getty.*),$1,'
7b180b
         ;;
7b180b
-    fedora*|rhel*|centos*)
7b180b
-        extra[${#extra[*]}]='--selinux-relabel'
7b180b
-        ;;
7b180b
     *)
7b180b
         ;;
7b180b
 esac
7b180b
diff --git a/common/mlcustomize/test-selinuxrelabel.sh b/common/mlcustomize/test-selinuxrelabel.sh
7b180b
index 86278c6..caf7521 100755
7b180b
--- a/common/mlcustomize/test-selinuxrelabel.sh
7b180b
+++ b/common/mlcustomize/test-selinuxrelabel.sh
7b180b
@@ -41,13 +41,12 @@ virt-builder "$guestname" --quiet -o "$disk"
7b180b
 # Test #1: relabel with the default configuration works.
7b180b
 rm -f  "$disk_overlay"
7b180b
 guestfish -- disk-create "$disk_overlay" qcow2 -1 backingfile:"$disk"
7b180b
-virt-customize -a "$disk" --selinux-relabel
7b180b
+virt-customize -a "$disk"
7b180b
 
7b180b
 # Test #2: relabel with no SELINUXTYPE in the configuration.
7b180b
 rm -f  "$disk_overlay"
7b180b
 guestfish -- disk-create "$disk_overlay" qcow2 -1 backingfile:"$disk"
7b180b
 virt-customize -a "$disk" \
7b180b
-  --edit /etc/selinux/config:"s,^SELINUXTYPE=,#&,g" \
7b180b
-  --selinux-relabel
7b180b
+  --edit /etc/selinux/config:"s,^SELINUXTYPE=,#&,g"
7b180b
 
7b180b
 rm "$disk" "$disk_overlay"
7b180b
diff --git a/common/options/uri.c b/common/options/uri.c
7b180b
index 6b696fc..84d393c 100644
7b180b
--- a/common/options/uri.c
7b180b
+++ b/common/options/uri.c
7b180b
@@ -135,7 +135,7 @@ parse (const char *arg, char **path_ret, char **protocol_ret,
7b180b
   socket = query_get (uri, "socket");
7b180b
 
7b180b
   if (uri->server && STRNEQ (uri->server, "") && socket) {
7b180b
-    fprintf (stderr, _("%s: %s: cannot both a server name and a socket query parameter\n"),
7b180b
+    fprintf (stderr, _("%s: %s: cannot have both a server name and a socket query parameter\n"),
7b180b
              getprogname (), arg);
7b180b
     return -1;
7b180b
   }
7b180b
@@ -347,6 +347,7 @@ make_server (xmlURIPtr uri, const char *socket, char ***ret)
7b180b
   *ret = malloc (sizeof (char *) * 2);
7b180b
   if (*ret == NULL) {
7b180b
     perror ("malloc");
7b180b
+    free (server);
7b180b
     return -1;
7b180b
   }
7b180b
   (*ret)[0] = server;
7b180b
diff --git a/customize/customize_run.ml b/customize/customize_run.ml
7b180b
index f2ee20413..99b5fe14d 100644
7b180b
--- a/customize/customize_run.ml
7b180b
+++ b/customize/customize_run.ml
7b180b
@@ -415,7 +415,7 @@ let run (g : G.guestfs) root (ops : ops) =
7b180b
       warning (f_"passwords could not be set for this type of guest")
7b180b
   );
7b180b
 
7b180b
-  if ops.flags.selinux_relabel then (
7b180b
+  if not ops.flags.no_selinux_relabel then (
7b180b
     message (f_"SELinux relabelling");
7b180b
     SELinux_relabel.relabel g
7b180b
   );
7b180b
diff --git a/customize/test-settings.sh b/customize/test-settings.sh
7b180b
index ed4c90f2e..e8b492dd1 100755
7b180b
--- a/customize/test-settings.sh
7b180b
+++ b/customize/test-settings.sh
7b180b
@@ -61,9 +61,6 @@ case "$guestname" in
7b180b
         extra[${#extra[*]}]='/etc/inittab:
7b180b
                                 s,^#([1-9].*respawn.*/sbin/getty.*),$1,'
7b180b
         ;;
7b180b
-    fedora*|rhel*|centos*)
7b180b
-        extra[${#extra[*]}]='--selinux-relabel'
7b180b
-        ;;
7b180b
     *)
7b180b
         ;;
7b180b
 esac
7b180b
diff --git a/sysprep/main.ml b/sysprep/main.ml
7b180b
index 087d1a17f..b760618ad 100644
7b180b
--- a/sysprep/main.ml
7b180b
+++ b/sysprep/main.ml
7b180b
@@ -132,8 +132,6 @@ let main () =
7b180b
       [ L"mount-options" ], Getopt.Set_string (s_"opts", mount_opts),  s_"Set mount options (eg /:noatime;/var:rw,noatime)";
7b180b
       [ L"network" ], Getopt.Set network,           s_"Enable appliance network";
7b180b
       [ L"no-network" ], Getopt.Clear network,      s_"Disable appliance network (default)";
7b180b
-      [ L"no-selinux-relabel" ], Getopt.Unit (fun () -> ()),
7b180b
-                                              s_"Compatibility option, does nothing";
7b180b
       [ L"operation"; L"operations" ],  Getopt.String (s_"operations", set_operations), s_"Enable/disable specific operations";
7b180b
     ] in
7b180b
     let args = basic_args @ Sysprep_operation.extra_args () in
7b180b
diff --git a/sysprep/test-virt-sysprep-docs.sh b/sysprep/test-virt-sysprep-docs.sh
7b180b
index 51500b5e9..9d0298d68 100755
7b180b
--- a/sysprep/test-virt-sysprep-docs.sh
7b180b
+++ b/sysprep/test-virt-sysprep-docs.sh
7b180b
@@ -25,4 +25,4 @@ $top_srcdir/podcheck.pl "$srcdir/virt-sysprep.pod" virt-sysprep \
7b180b
     --path $top_srcdir/common/options \
7b180b
     --insert sysprep-extra-options.pod:__EXTRA_OPTIONS__ \
7b180b
     --insert sysprep-operations.pod:__OPERATIONS__ \
7b180b
-    --ignore=--dryrun,--dump-pod,--dump-pod-options,--no-selinux-relabel
7b180b
+    --ignore=--dryrun,--dump-pod,--dump-pod-options
7b180b
-- 
7b180b
2.31.1
7b180b