richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ac3a84
From c67164cf2c6aed29f70a98ef9050503e56aba952 Mon Sep 17 00:00:00 2001
ac3a84
From: Yu Watanabe <watanabe.yu+github@gmail.com>
ac3a84
Date: Sun, 13 Nov 2022 19:25:02 +0900
ac3a84
Subject: [PATCH] dissect-image: do not try to close invalid fd
ac3a84
ac3a84
Fixes a bug introduced by f7725647bb41c3398a867f139efe526efe8aa1b3.
ac3a84
ac3a84
Hopefully fixes #25348.
ac3a84
ac3a84
(cherry picked from commit 088377e0920a3785e7926f2ed382810836480ae6)
ac3a84
ac3a84
Related: #2138081
ac3a84
---
ac3a84
 src/shared/dissect-image.c | 58 +++++++++++++-------------------------
ac3a84
 src/shared/dissect-image.h |  6 ++++
ac3a84
 2 files changed, 26 insertions(+), 38 deletions(-)
ac3a84
ac3a84
diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c
ac3a84
index 2989d31d3c..6a991c877a 100644
ac3a84
--- a/src/shared/dissect-image.c
ac3a84
+++ b/src/shared/dissect-image.c
ac3a84
@@ -1010,19 +1010,13 @@ static int dissect_image(
ac3a84
                 log_debug("No root partition found of the native architecture, falling back to a root "
ac3a84
                           "partition of the secondary architecture.");
ac3a84
 
ac3a84
-                m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY];
ac3a84
-                zero(m->partitions[PARTITION_ROOT_SECONDARY]);
ac3a84
-                m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY];
ac3a84
-                zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]);
ac3a84
-                m->partitions[PARTITION_ROOT_VERITY_SIG] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG];
ac3a84
-                zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG]);
ac3a84
-
ac3a84
-                m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_SECONDARY];
ac3a84
-                zero(m->partitions[PARTITION_USR_SECONDARY]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_SECONDARY_VERITY];
ac3a84
-                zero(m->partitions[PARTITION_USR_SECONDARY_VERITY]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG];
ac3a84
-                zero(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]);
ac3a84
+                m->partitions[PARTITION_ROOT] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_SECONDARY]);
ac3a84
+                m->partitions[PARTITION_ROOT_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]);
ac3a84
+                m->partitions[PARTITION_ROOT_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG]);
ac3a84
+
ac3a84
+                m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]);
ac3a84
 
ac3a84
                 m->partitions[PARTITION_ROOT_OTHER].found = false;
ac3a84
                 m->partitions[PARTITION_ROOT_OTHER_VERITY].found = false;
ac3a84
@@ -1044,19 +1038,13 @@ static int dissect_image(
ac3a84
                           "falling back to a root partition of a non-native architecture (%s).",
ac3a84
                           architecture_to_string(m->partitions[PARTITION_ROOT_OTHER].architecture));
ac3a84
 
ac3a84
-                m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_OTHER];
ac3a84
-                zero(m->partitions[PARTITION_ROOT_OTHER]);
ac3a84
-                m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_OTHER_VERITY];
ac3a84
-                zero(m->partitions[PARTITION_ROOT_OTHER_VERITY]);
ac3a84
-                m->partitions[PARTITION_ROOT_VERITY_SIG] = m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG];
ac3a84
-                zero(m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG]);
ac3a84
-
ac3a84
-                m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_OTHER];
ac3a84
-                zero(m->partitions[PARTITION_USR_OTHER]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_OTHER_VERITY];
ac3a84
-                zero(m->partitions[PARTITION_USR_OTHER_VERITY]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_OTHER_VERITY_SIG];
ac3a84
-                zero(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]);
ac3a84
+                m->partitions[PARTITION_ROOT] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_OTHER]);
ac3a84
+                m->partitions[PARTITION_ROOT_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_OTHER_VERITY]);
ac3a84
+                m->partitions[PARTITION_ROOT_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG]);
ac3a84
+
ac3a84
+                m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]);
ac3a84
         }
ac3a84
 
ac3a84
         /* Hmm, we found a signature partition but no Verity data? Something is off. */
ac3a84
@@ -1083,12 +1071,9 @@ static int dissect_image(
ac3a84
                           "partition of the secondary architecture.");
ac3a84
 
ac3a84
                 /* Upgrade secondary arch to primary */
ac3a84
-                m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_SECONDARY];
ac3a84
-                zero(m->partitions[PARTITION_USR_SECONDARY]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_SECONDARY_VERITY];
ac3a84
-                zero(m->partitions[PARTITION_USR_SECONDARY_VERITY]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG];
ac3a84
-                zero(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]);
ac3a84
+                m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]);
ac3a84
 
ac3a84
                 m->partitions[PARTITION_USR_OTHER].found = false;
ac3a84
                 m->partitions[PARTITION_USR_OTHER_VERITY].found = false;
ac3a84
@@ -1105,12 +1090,9 @@ static int dissect_image(
ac3a84
                           architecture_to_string(m->partitions[PARTITION_ROOT_OTHER].architecture));
ac3a84
 
ac3a84
                 /* Upgrade other arch to primary */
ac3a84
-                m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_OTHER];
ac3a84
-                zero(m->partitions[PARTITION_USR_OTHER]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_OTHER_VERITY];
ac3a84
-                zero(m->partitions[PARTITION_USR_OTHER_VERITY]);
ac3a84
-                m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_OTHER_VERITY_SIG];
ac3a84
-                zero(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]);
ac3a84
+                m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY]);
ac3a84
+                m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]);
ac3a84
         }
ac3a84
 
ac3a84
         /* Hmm, we found a signature partition but no Verity data? Something is off. */
ac3a84
diff --git a/src/shared/dissect-image.h b/src/shared/dissect-image.h
ac3a84
index 8007b544e7..f2278c4dfa 100644
ac3a84
--- a/src/shared/dissect-image.h
ac3a84
+++ b/src/shared/dissect-image.h
ac3a84
@@ -40,6 +40,12 @@ struct DissectedPartition {
ac3a84
                 .architecture = _ARCHITECTURE_INVALID,                  \
ac3a84
                 .mount_node_fd = -1,                                    \
ac3a84
         })
ac3a84
+#define TAKE_PARTITION(p)                                       \
ac3a84
+        ({                                                      \
ac3a84
+                DissectedPartition *_pp = &(p), _p = *_pp;      \
ac3a84
+                *_pp = DISSECTED_PARTITION_NULL;                \
ac3a84
+                _p;                                             \
ac3a84
+        })
ac3a84
 
ac3a84
 typedef enum PartitionDesignator {
ac3a84
         PARTITION_ROOT,