From: Yu Watanabe Date: Sun, 13 Nov 2022 10:25:02 +0000 (+0900) Subject: dissect-image: do not try to close invalid fd X-Git-Tag: v253-rc1~536 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=088377e0920a3785e7926f2ed382810836480ae6;p=thirdparty%2Fsystemd.git dissect-image: do not try to close invalid fd Fixes a bug introduced by f7725647bb41c3398a867f139efe526efe8aa1b3. Hopefully fixes #25348. --- diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index a027973e044..8418ed6dc37 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -1030,19 +1030,13 @@ static int dissect_image( log_debug("No root partition found of the native architecture, falling back to a root " "partition of the secondary architecture."); - m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY]; - zero(m->partitions[PARTITION_ROOT_SECONDARY]); - m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY]; - zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]); - m->partitions[PARTITION_ROOT_VERITY_SIG] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG]; - zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG]); - - m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_SECONDARY]; - zero(m->partitions[PARTITION_USR_SECONDARY]); - m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_SECONDARY_VERITY]; - zero(m->partitions[PARTITION_USR_SECONDARY_VERITY]); - m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]; - zero(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]); + m->partitions[PARTITION_ROOT] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_SECONDARY]); + m->partitions[PARTITION_ROOT_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]); + m->partitions[PARTITION_ROOT_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG]); + + m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY]); + m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY]); + m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]); m->partitions[PARTITION_ROOT_OTHER].found = false; m->partitions[PARTITION_ROOT_OTHER_VERITY].found = false; @@ -1064,19 +1058,13 @@ static int dissect_image( "falling back to a root partition of a non-native architecture (%s).", architecture_to_string(m->partitions[PARTITION_ROOT_OTHER].architecture)); - m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_OTHER]; - zero(m->partitions[PARTITION_ROOT_OTHER]); - m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_OTHER_VERITY]; - zero(m->partitions[PARTITION_ROOT_OTHER_VERITY]); - m->partitions[PARTITION_ROOT_VERITY_SIG] = m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG]; - zero(m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG]); - - m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_OTHER]; - zero(m->partitions[PARTITION_USR_OTHER]); - m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_OTHER_VERITY]; - zero(m->partitions[PARTITION_USR_OTHER_VERITY]); - m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_OTHER_VERITY_SIG]; - zero(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]); + m->partitions[PARTITION_ROOT] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_OTHER]); + m->partitions[PARTITION_ROOT_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_OTHER_VERITY]); + m->partitions[PARTITION_ROOT_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG]); + + m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER]); + m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY]); + m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]); } /* Hmm, we found a signature partition but no Verity data? Something is off. */ @@ -1103,12 +1091,9 @@ static int dissect_image( "partition of the secondary architecture."); /* Upgrade secondary arch to primary */ - m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_SECONDARY]; - zero(m->partitions[PARTITION_USR_SECONDARY]); - m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_SECONDARY_VERITY]; - zero(m->partitions[PARTITION_USR_SECONDARY_VERITY]); - m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]; - zero(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]); + m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY]); + m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY]); + m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]); m->partitions[PARTITION_USR_OTHER].found = false; m->partitions[PARTITION_USR_OTHER_VERITY].found = false; @@ -1125,12 +1110,9 @@ static int dissect_image( architecture_to_string(m->partitions[PARTITION_ROOT_OTHER].architecture)); /* Upgrade other arch to primary */ - m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_OTHER]; - zero(m->partitions[PARTITION_USR_OTHER]); - m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_OTHER_VERITY]; - zero(m->partitions[PARTITION_USR_OTHER_VERITY]); - m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_OTHER_VERITY_SIG]; - zero(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]); + m->partitions[PARTITION_USR] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER]); + m->partitions[PARTITION_USR_VERITY] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY]); + m->partitions[PARTITION_USR_VERITY_SIG] = TAKE_PARTITION(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]); } /* Hmm, we found a signature partition but no Verity data? Something is off. */ diff --git a/src/shared/dissect-image.h b/src/shared/dissect-image.h index af043297b9e..1a398010b53 100644 --- a/src/shared/dissect-image.h +++ b/src/shared/dissect-image.h @@ -41,6 +41,12 @@ struct DissectedPartition { .architecture = _ARCHITECTURE_INVALID, \ .mount_node_fd = -1, \ }) +#define TAKE_PARTITION(p) \ + ({ \ + DissectedPartition *_pp = &(p), _p = *_pp; \ + *_pp = DISSECTED_PARTITION_NULL; \ + _p; \ + }) typedef enum DissectImageFlags { DISSECT_IMAGE_DEVICE_READ_ONLY = 1 << 0, /* Make device read-only */