teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0075-dissect-image-do-not-try-to-close-invalid-fd.patch

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