From 87b473af799262ef7cbe71214045789c16d9ad9a Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Mon, 4 Dec 2023 18:21:23 +0100
Subject: [PATCH] dissect-image: fix fd leak in
 dissected_image_acquire_metadata()

We have to go through the "finish" label to properly close all pipes in
the error path, so that we don't leak them.

(cherry picked from commit 8d5e61db432932faa5b2d8531ab804bb4da4791d)
(cherry picked from commit f6cf899f1ed55f9ed140f1a4b57d6e27b973854b)
(cherry picked from commit 92479eb37614c0b7d9b83c686d8940ee2ea32a90)
(cherry picked from commit 053c90d59bc83592da3b74257c91e162c8cbd01a)

Conflict:NA
Reference:https://github.com/systemd/systemd/commit/8d5e61db432932faa5b2d8531ab804bb4da4791d
---
 src/shared/dissect-image.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c
index 37dbf64d96..d2446deb0c 100644
--- a/src/shared/dissect-image.c
+++ b/src/shared/dissect-image.c
@@ -3042,18 +3042,25 @@ int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_
         r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
         child = 0;
         if (r < 0)
-                return r;
+                goto finish;
 
         n = read(error_pipe[0], &v, sizeof(v));
-        if (n < 0)
-                return -errno;
-        if (n == sizeof(v))
-                return v; /* propagate error sent to us from child */
-        if (n != 0)
-                return -EIO;
-
-        if (r != EXIT_SUCCESS)
-                return -EPROTO;
+        if (n < 0) {
+                r = -errno;
+                goto finish;
+        }
+        if (n == sizeof(v)) {
+                r = v; /* propagate error sent to us from child */
+                goto finish;
+        }
+        if (n != 0) {
+                r = -EIO;
+                goto finish;
+        }
+        if (r != EXIT_SUCCESS) {
+                r = -EPROTO;
+                goto finish;
+        }
 
         free_and_replace(m->hostname, hostname);
         m->machine_id = machine_id;
-- 
2.33.0

