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

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