From c1979cd881cce63902cf8f7e633bf7dd081162f7 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:28:36 +0100 Subject: [PATCH 01/12] repart: Use IN_SET() in one more place --- src/partition/repart.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 07efcd2857..b4ef7b8e34 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -1643,9 +1643,7 @@ static int partition_read_definition(Partition *p, const char *path, const char verity_mode_to_string(p->verity)); /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */ - if ((p->type.designator == PARTITION_ROOT_VERITY || - p->type.designator == PARTITION_USR_VERITY) && - p->read_only < 0) + if (IN_SET(p->type.designator, PARTITION_ROOT_VERITY, PARTITION_USR_VERITY) && p->read_only < 0) p->read_only = true; /* Default to "growfs" on, unless read-only */ From 5f59807d2dc0eb4653f2e9c0722b2f6bff751f7d Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:34:28 +0100 Subject: [PATCH 02/12] repart: Rework unused partition number algorithm --- src/partition/repart.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index b4ef7b8e34..20db7d7a1a 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -1056,16 +1056,13 @@ static uint64_t find_first_unused_partno(Context *context) { assert(context); - for (bool changed = true; changed;) { - changed = false; - - LIST_FOREACH(partitions, p, context->partitions) { - if (p->partno != UINT64_MAX && p->partno == partno) { - partno++; - changed = true; - break; - } - } + for (partno = 0;; partno++) { + bool found = false; + LIST_FOREACH(partitions, p, context->partitions) + if (p->partno != UINT64_MAX && p->partno == partno) + found = true; + if (!found) + break; } return partno; From 53538e33e9c680cde50aedb393b03a74eedd32df Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:35:18 +0100 Subject: [PATCH 03/12] repart: Fix FilterPartitionsType enum name --- src/partition/repart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 20db7d7a1a..71f120b359 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -109,7 +109,7 @@ static enum { EMPTY_CREATE, /* create disk as loopback file, create a partition table always */ } arg_empty = EMPTY_REFUSE; -typedef enum { +typedef enum FilterPartitionType { FILTER_PARTITIONS_NONE, FILTER_PARTITIONS_EXCLUDE, FILTER_PARTITIONS_INCLUDE, From 766f52f25c1dac09dca2e619e2f3ec9e826c77e1 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:36:37 +0100 Subject: [PATCH 04/12] repart: Fix parse_filter_partitions() log messages --- src/partition/repart.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 71f120b359..cec57ed81b 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -5345,11 +5345,11 @@ static int parse_filter_partitions(const char *p) { if (r == 0) break; if (r < 0) - return log_error_errno(r, "Failed to extract partition designator: %s", optarg); + return log_error_errno(r, "Failed to extract partition type identifier or GUID: %s", p); r = gpt_partition_type_from_string(name, &type); if (r < 0) - return log_error_errno(r, "'%s' is not a valid partition designator", name); + return log_error_errno(r, "'%s' is not a valid partition type identifier or GUID", name); if (!GREEDY_REALLOC(arg_filter_partitions, arg_filter_partitions_size + 1)) return log_oom(); From 983154f51831d0ee96ee08aed3367ea9990009e9 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:44:38 +0100 Subject: [PATCH 05/12] repart: Use ftruncate() instead of posix_allocate() --- src/partition/repart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index cec57ed81b..aa8a99fe1a 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -3199,7 +3199,7 @@ static int partition_encrypt(Context *context, Partition *p, const char *node) { return log_error_errno(r, "Failed to create temporary LUKS header file: %m"); /* Weird cryptsetup requirement which requires the header file to be the size of at least one sector. */ - r = posix_fallocate(fileno(h), 0, context->sector_size); + r = ftruncate(fileno(h), context->sector_size); if (r < 0) return log_error_errno(r, "Failed to grow temporary LUKS header file: %m"); From 922576e4525bb865efc1ae424140c6c0e547080a Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:45:26 +0100 Subject: [PATCH 06/12] repart: Use ASSERT_PTR() in partition_encrypt() --- src/partition/repart.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index aa8a99fe1a..8249a82043 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -3154,8 +3154,8 @@ static int partition_target_sync(Context *context, Partition *p, PartitionTarget static int partition_encrypt(Context *context, Partition *p, const char *node) { #if HAVE_LIBCRYPTSETUP && HAVE_CRYPT_SET_DATA_OFFSET && HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE && HAVE_CRYPT_REENCRYPT struct crypt_params_luks2 luks_params = { - .label = strempty(p->new_label), - .sector_size = context->sector_size, + .label = strempty(ASSERT_PTR(p)->new_label), + .sector_size = ASSERT_PTR(context)->sector_size, .data_device = node, }; struct crypt_params_reencrypt reencrypt_params = { From 29ec4bce0ee18c4347790ee938418f06e9c91b29 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:46:12 +0100 Subject: [PATCH 07/12] mkfs-util: Use GID_FMT --- src/shared/mkfs-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c index 0e135a9ac3..0f4d85b58c 100644 --- a/src/shared/mkfs-util.c +++ b/src/shared/mkfs-util.c @@ -118,10 +118,10 @@ static int setup_userns(uid_t uid, gid_t gid) { return log_error_errno(r, "Failed to write 'deny' to /proc/self/setgroups: %m"); r = write_string_filef("/proc/self/gid_map", WRITE_STRING_FILE_DISABLE_BUFFER, - UID_FMT " " UID_FMT " " UID_FMT, 0u, gid, 1u); + GID_FMT " " GID_FMT " " GID_FMT, 0u, gid, 1u); if (r < 0) return log_error_errno(r, - "Failed to write mapping for "UID_FMT" to /proc/self/gid_map: %m", + "Failed to write mapping for "GID_FMT" to /proc/self/gid_map: %m", gid); return 0; From 92b11378ff2e32626df830756d552d8e7e20e4a5 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:49:37 +0100 Subject: [PATCH 08/12] mkfs-util: Prefer fd based stat() where possible --- src/shared/mkfs-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c index 0f4d85b58c..b06ba32089 100644 --- a/src/shared/mkfs-util.c +++ b/src/shared/mkfs-util.c @@ -165,7 +165,7 @@ static int do_mcopy(const char *node, const char *root) { if (r < 0) return log_oom(); - if (stat(root, &st) < 0) + if (fstat(dirfd(rootdir), &st) < 0) return log_error_errno(errno, "Failed to stat '%s': %m", root); r = safe_fork("(mcopy)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|FORK_NEW_USERNS, NULL); From a9abef7f67c18d8aa88eeb429d2c3b053c8d2af2 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:51:15 +0100 Subject: [PATCH 09/12] mkfs-util: Use FORK_CLOSE_ALL_FDS --- src/shared/mkfs-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c index b06ba32089..5a56c5e473 100644 --- a/src/shared/mkfs-util.c +++ b/src/shared/mkfs-util.c @@ -168,7 +168,7 @@ static int do_mcopy(const char *node, const char *root) { if (fstat(dirfd(rootdir), &st) < 0) return log_error_errno(errno, "Failed to stat '%s': %m", root); - r = safe_fork("(mcopy)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|FORK_NEW_USERNS, NULL); + r = safe_fork("(mcopy)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|FORK_NEW_USERNS|FORK_CLOSE_ALL_FDS, NULL); if (r < 0) return r; if (r == 0) { @@ -492,7 +492,7 @@ int make_filesystem( if (root && stat(root, &st) < 0) return log_error_errno(errno, "Failed to stat %s: %m", root); - r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|(root ? FORK_NEW_USERNS : 0), NULL); + r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|FORK_CLOSE_ALL_FDS|(root ? FORK_NEW_USERNS : 0), NULL); if (r < 0) return r; if (r == 0) { From 48b1e18aaee487b7e3f5bf881c2848a7f4ce79b4 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 10:53:23 +0100 Subject: [PATCH 10/12] repart: Remove redundant parentheses in partition_populate_directory() --- src/partition/repart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 8249a82043..9a6bbdfb32 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -3786,7 +3786,7 @@ static int partition_populate_directory(Partition *p, const Set *denylist, char assert(ret); - if ((strv_isempty(p->copy_files) && strv_isempty(p->make_directories))) { + if (strv_isempty(p->copy_files) && strv_isempty(p->make_directories)) { *ret = NULL; return 0; } From 488bb758c074314b08293ae1501f6b53352f7d87 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 11:10:34 +0100 Subject: [PATCH 11/12] repart: Use fd based operations in partition_populate_directory() --- src/partition/repart.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 9a6bbdfb32..5bae554258 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -3782,6 +3782,7 @@ static int do_make_directories(Partition *p, const char *root) { static int partition_populate_directory(Partition *p, const Set *denylist, char **ret) { _cleanup_(rm_rf_physical_and_freep) char *root = NULL; + _cleanup_close_ int rfd = -1; int r; assert(ret); @@ -3791,11 +3792,11 @@ static int partition_populate_directory(Partition *p, const Set *denylist, char return 0; } - r = mkdtemp_malloc("/var/tmp/repart-XXXXXX", &root); - if (r < 0) - return log_error_errno(r, "Failed to create temporary directory: %m"); + rfd = mkdtemp_open("/var/tmp/repart-XXXXXX", 0, &root); + if (rfd < 0) + return log_error_errno(rfd, "Failed to create temporary directory: %m"); - if (chmod(root, 0755) < 0) + if (fchmod(rfd, 0755) < 0) return log_error_errno(errno, "Failed to change mode of temporary directory: %m"); /* Make sure everything is owned by the user running repart so that make_filesystem() can map the From 6b1ea55e0b84da0104afe058347b556dcef0e2ba Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 21 Nov 2022 11:23:51 +0100 Subject: [PATCH 12/12] repart: Rework PartitionTarget initialization --- src/partition/repart.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 5bae554258..5beba69f3f 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -3047,9 +3047,13 @@ static int partition_target_prepare( * the result into the image. */ - t = new0(PartitionTarget, 1); + t = new(PartitionTarget, 1); if (!t) return log_oom(); + *t = (PartitionTarget) { + .fd = -1, + .whole_fd = -1, + }; if (S_ISBLK(st.st_mode) || (p->format && !mkfs_supports_root_option(p->format))) { _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; @@ -3061,10 +3065,7 @@ static int partition_target_prepare( if (r < 0) return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno); - *t = (PartitionTarget) { - .loop = TAKE_PTR(d), - .fd = -1, - }; + t->loop = TAKE_PTR(d); } else if (need_path) { _cleanup_(unlink_and_freep) char *temp = NULL; _cleanup_close_ int fd = -1; @@ -3086,18 +3087,13 @@ static int partition_target_prepare( return log_error_errno(errno, "Failed to truncate temporary file to %s: %m", FORMAT_BYTES(size)); - *t = (PartitionTarget) { - .fd = TAKE_FD(fd), - .path = TAKE_PTR(temp), - }; + t->fd = TAKE_FD(fd); + t->path = TAKE_PTR(temp); } else { if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1) return log_error_errno(errno, "Failed to seek to partition offset: %m"); - *t = (PartitionTarget) { - .fd = -1, - .whole_fd = whole_fd, - }; + t->whole_fd = whole_fd; } *ret = TAKE_PTR(t);