All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Horman <horms@kernel.org>
To: Pingfan Liu <piliu@redhat.com>
Cc: Eric Biederman <ebiederm@xmission.com>,
	Baoquan He <bhe@redhat.com>, Dave Young <dyoung@redhat.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	Jan Hendrik Farr <kernel@jfarr.cc>,
	Philipp Rudo <prudo@redhat.com>,
	Lennart Poettering <mzxreary@0pointer.de>,
	kexec@lists.infradead.org
Subject: Re: [PATCHv2 2/3] kexec: Introduce UKI image parser
Date: Mon, 21 Oct 2024 11:16:46 +0100	[thread overview]
Message-ID: <20241021101646.GA402847@kernel.org> (raw)
In-Reply-To: <20241016113418.22568-3-piliu@redhat.com>

On Wed, Oct 16, 2024 at 07:34:14PM +0800, Pingfan Liu wrote:
> A UKI image is a PE file that consists of several sections, typically
> including: .text, .data, .linux, .initrd, .cmdline, and others.
> 
> The kernel image is stored in the .linux section, which is one of the
> formats currently recognized by kexec-tools. Therefore, the UKI parser
> can be used to strip away the UKI layer, allowing the other parser to
> continue the procession of the kernel image.
> 
> Signed-off-by: Pingfan Liu <piliu@redhat.com>

...

> diff --git a/kexec/kexec-uki.c b/kexec/kexec-uki.c

...

> +int uki_image_probe(const char *file_buf, off_t buf_sz)
> +{
> +	struct pe_hdr *pe_hdr;
> +	struct pe32plus_opt_hdr *opt_hdr;
> +	struct section_header *sect_hdr;
> +	int pe_hdr_offset, section_nr, linux_sz = -1;
> +	char *pe_part_buf, *linux_src;
> +	char *initrd_fname = NULL;
> +	int initrd_fd = -1;
> +
> +	pe_hdr_offset = get_pehdr_offset(file_buf);
> +	pe_part_buf = (char *)file_buf + pe_hdr_offset;
> +	pe_hdr = (struct pe_hdr *)pe_part_buf;
> +	if (pe_hdr->opt_hdr_size == 0) {
> +		printf("ERR: optional header is missing\n");
> +		return -1;
> +	}
> +	section_nr = pe_hdr->sections;
> +	opt_hdr = (struct pe32plus_opt_hdr *)(pe_part_buf + sizeof(struct pe_hdr));
> +	sect_hdr = (struct section_header *)((char *)opt_hdr + pe_hdr->opt_hdr_size);
> +
> +	for (int i = 0; i < section_nr; i++) {
> +		if (!strcmp(sect_hdr->name, UKI_LINUX_SECTION)) {
> +			/* data_addr is relative to the whole file */
> +			linux_src = (char *)file_buf + sect_hdr->data_addr;
> +			linux_sz = sect_hdr->raw_data_size;
> +
> +		} else if (!strcmp(sect_hdr->name, UKI_INITRD_SECTION)) {
> +			if (!(initrd_fname = strdup(FILENAME_UKI_INITRD))) {
> +				dbgprintf("%s: Can't duplicate strings\n", __func__);
> +				goto next;
> +			}
> +
> +			if ((initrd_fd = mkstemp(initrd_fname)) < 0) {
> +				dbgprintf("%s: Can't open file %s\n", __func__,	initrd_fname);
> +				goto next;
> +			}
> +
> +			if (write(initrd_fd, (char *)file_buf + sect_hdr->data_addr,
> +					sect_hdr->raw_data_size) != sect_hdr->raw_data_size) {
> +				dbgprintf("%s: Can't write the compressed file %s\n",
> +						__func__, initrd_fname);
> +				goto next;

I plan to apply this series, but initrd_fd appears to be leaked here.
Could you send a follow-up patch to address that? Perhaps like this:

			err = write(...);
			close(initrd_fd)
			if (err != sect_hdr->raw_data_size) {
				...
			} else {
				...
			}

Thanks!

> +			} else {
> +				implicit_initrd_fd = open(initrd_fname, O_RDONLY);
> +				close(initrd_fd);
> +			}
> +		}
> +next:
> +		sect_hdr++;
> +	}

...

_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

  reply	other threads:[~2024-10-21 10:17 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-16 11:34 [PATCHv2 0/3] kexec: Add support for UKI format kernel Pingfan Liu
2024-10-16 11:34 ` [PATCHv2 1/3] kexec: Introduce implicit_initrd_fd to pass internal initrd information Pingfan Liu
2024-10-16 11:34 ` [PATCHv2 2/3] kexec: Introduce UKI image parser Pingfan Liu
2024-10-21 10:16   ` Simon Horman [this message]
2024-10-22  6:33     ` Pingfan Liu
2024-10-22  6:27   ` [PATCHv3 " Pingfan Liu
2024-10-16 11:34 ` [PATCHv2 3/3] arm64: Support UKI image format Pingfan Liu
2024-10-21 10:20 ` [PATCHv2 0/3] kexec: Add support for UKI format kernel Simon Horman
2024-10-21 16:25 ` Philipp Rudo
2024-10-22  6:25   ` Pingfan Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241021101646.GA402847@kernel.org \
    --to=horms@kernel.org \
    --cc=ardb@kernel.org \
    --cc=bhe@redhat.com \
    --cc=dyoung@redhat.com \
    --cc=ebiederm@xmission.com \
    --cc=kernel@jfarr.cc \
    --cc=kexec@lists.infradead.org \
    --cc=mzxreary@0pointer.de \
    --cc=piliu@redhat.com \
    --cc=prudo@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.