]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
kernel-install: Avoid reopening file descriptor via /proc
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 14 Jul 2023 08:51:18 +0000 (10:51 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 14 Jul 2023 10:28:26 +0000 (12:28 +0200)
kernel-install used to work without /proc mounted before the rewrite
in C. Let's restore that property by making sure we don't reopen
file descriptors via /proc. In this case, parse_env_file_fdv() calls
fdopen_independent() to get a FILE * for the given file descriptor
(which itself calls fd_reopen()). Let's avoid the call to
fdopen_independent() by using chase_and_fopenat_unlocked() which
gives us a FILE * immediately without having to reopen any file
descriptors.

src/kernel-install/kernel-install.c

index 64833e53d9fc73f3dfd972f28ba2003cf32b26a2..0acd8ebaa815cf958fde8e09f4f7a57c9fcb56f2 100644 (file)
@@ -340,7 +340,7 @@ static int context_ensure_conf_root(Context *c) {
 }
 
 static int context_load_install_conf_one(Context *c, const char *path) {
-        _cleanup_close_ int fd = -EBADF;
+        _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char
                 *conf = NULL, *machine_id = NULL, *boot_root = NULL, *layout = NULL,
                 *initrd_generator = NULL, *uki_generator = NULL;
@@ -353,7 +353,7 @@ static int context_load_install_conf_one(Context *c, const char *path) {
         if (!conf)
                 return log_oom();
 
-        r = chaseat(c->rfd, conf, CHASE_AT_RESOLVE_IN_ROOT, NULL, &fd);
+        r = chase_and_fopenat_unlocked(c->rfd, conf, CHASE_AT_RESOLVE_IN_ROOT, "re", NULL, &f);
         if (r == -ENOENT)
                 return 0;
         if (r < 0)
@@ -361,12 +361,12 @@ static int context_load_install_conf_one(Context *c, const char *path) {
 
         log_debug("Loading %s…", conf);
 
-        r = parse_env_file_fd(fd, conf,
-                              "MACHINE_ID",       &machine_id,
-                              "BOOT_ROOT",        &boot_root,
-                              "layout",           &layout,
-                              "initrd_generator", &initrd_generator,
-                              "uki_generator",    &uki_generator);
+        r = parse_env_file(f, conf,
+                           "MACHINE_ID",       &machine_id,
+                           "BOOT_ROOT",        &boot_root,
+                           "layout",           &layout,
+                           "initrd_generator", &initrd_generator,
+                           "uki_generator",    &uki_generator);
         if (r < 0)
                 return log_error_errno(r, "Failed to parse '%s': %m", conf);
 
@@ -401,7 +401,7 @@ static int context_load_install_conf(Context *c) {
 }
 
 static int context_load_machine_info(Context *c) {
-        _cleanup_close_ int fd = -EBADF;
+        _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *machine_id = NULL, *layout = NULL;
         static const char *path = "/etc/machine-info";
         int r;
@@ -423,7 +423,7 @@ static int context_load_machine_info(Context *c) {
                 return 0;
         }
 
-        r = chaseat(c->rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, &fd);
+        r = chase_and_fopenat_unlocked(c->rfd, path, CHASE_AT_RESOLVE_IN_ROOT, "re", NULL, &f);
         if (r == -ENOENT)
                 return 0;
         if (r < 0)
@@ -431,9 +431,9 @@ static int context_load_machine_info(Context *c) {
 
         log_debug("Loading %s…", path);
 
-        r = parse_env_file_fd(fd, path,
-                              "KERNEL_INSTALL_MACHINE_ID", &machine_id,
-                              "KERNEL_INSTALL_LAYOUT", &layout);
+        r = parse_env_file(f, path,
+                           "KERNEL_INSTALL_MACHINE_ID", &machine_id,
+                           "KERNEL_INSTALL_LAYOUT", &layout);
         if (r < 0)
                 return log_error_errno(r, "Failed to parse '%s': %m", path);