kernel-install: Avoid reopening file descriptor via /proc

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.
This commit is contained in:
Daan De Meyer 2023-07-14 10:51:18 +02:00
parent 7f92564b38
commit a061312324

View 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);