Skip to contentSkip to conversation

kexec-tools: Determine if the image is lzma commpressed

PATCH Superseded Lichen Liu 2 participants 3 messages Mar 30, 2022

Subsystems (2)
lore
Share & tools
lore

Lichen Liu

author
#0 lore raw

PATCH v2 1/1 kexec-tools: Determine if the image is lzma commpressed

Currently there are 2 functions for decompressing compressed image. The zlib_decompress_file() will determine if the image is compressed by gzip before read, but lzma_decompress_file() will not. This can cause misleading information to be printed when the image is not compressed by lzma and debug option is used:

]# kexec -d -s -l /boot/vmlinuz-5.14.10-300.fc35.x86_64 \
--initrd /boot/initramfs-5.14.10-300.fc35.x86_64.img \
--reuse-cmdline
Try gzip decompression.
Try LZMA decompression.
lzma_decompress_file: read on /boot/vmlinuz-5.14.10-300.fc35.x86_64 of 65536 bytes failed

Add a helper function is_lzma_file() to help behave consistently.

---
Changes in v2:
- add a fclose() when file is too small.

---
 kexec/lzma.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
kexec/lzma.c +39 -0
index 5bfccb7..2fc07e6 100644
@@ -155,6 +155,42 @@ ssize_t lzread(LZFILE *lzfile, void *buf, size_t len)
 	}
 }
 
+int is_lzma_file(const char *filename)
+{
+	FILE *fp;
+	int ret = 0;
+	uint8_t buf[13];
+
+	if (!filename)
+		return 0;
+
+	fp = fopen(filename, "r");
+	if (fp == NULL)
+		return 0;
+
+	const size_t size = fread(buf, 1, sizeof(buf), fp);
+
+	if (size != 13) {
+		/* file is too small to be a lzma file. */
+		fclose(fp);
+		return 0;
+	}
+
+	lzma_filter filter = { .id = LZMA_FILTER_LZMA1 };
+
+	switch (lzma_properties_decode(&filter, NULL, buf, 5)) {
+	case LZMA_OK:
+		ret = 1;
+		break;
+	default:
+		/* It's not a lzma file */
+		ret = 0;
+	}
+
+	fclose(fp);
+	return ret;
+}
+
 char *lzma_decompress_file(const char *filename, off_t *r_size)
 {
 	LZFILE *fp;
@@ -168,6 +204,9 @@ char *lzma_decompress_file(const char *filename, off_t *r_size)
 	if (!filename)
 		return NULL;
 
+	if (!is_lzma_file(filename))
+		return NULL;
+
 	fp = lzopen(filename, "rb");
 	if (fp == 0) {
 		dbgprintf("Cannot open `%s'\n", filename);
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/1 kexec-tools: Determine if the image is lzma commpressed

Quoted 14 lines · Lichen Liu

Currently there are 2 functions for decompressing compressed image. The zlib_decompress_file() will determine if the image is compressed by gzip before read, but lzma_decompress_file() will not. This can cause misleading information to be printed when the image is not compressed by lzma and debug option is used:

]# kexec -d -s -l /boot/vmlinuz-5.14.10-300.fc35.x86_64 \
--initrd /boot/initramfs-5.14.10-300.fc35.x86_64.img \
--reuse-cmdline
Try gzip decompression.
Try LZMA decompression.
lzma_decompress_file: read on /boot/vmlinuz-5.14.10-300.fc35.x86_64 of 65536 bytes failed

Quoted 6 lines

Add a helper function is_lzma_file() to help behave consistently.

---
Changes in v2:
- add a fclose() when file is too small.

Hi Lichen Liu,

I have already applied v1 of this patch,
so could you consider providing a follow-up patch
which contains the delta between v1 and v2?

Thanks!

Ref: https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/1b03cf7adc3c

author
#2 lore raw

PATCH v2 1/1 kexec-tools: Determine if the image is lzma commpressed

Hi Simon,

I will send a new patch to fix it.

Thanks!

Quoted 38 lines · Simon Horman
Quoted 14 lines · Lichen Liu

Currently there are 2 functions for decompressing compressed image. The zlib_decompress_file() will determine if the image is compressed by gzip before read, but lzma_decompress_file() will not. This can cause misleading information to be printed when the image is not compressed by lzma and debug option is used:

]# kexec -d -s -l /boot/vmlinuz-5.14.10-300.fc35.x86_64 \
--initrd /boot/initramfs-5.14.10-300.fc35.x86_64.img \
--reuse-cmdline
Try gzip decompression.
Try LZMA decompression.
lzma_decompress_file: read on /boot/vmlinuz-5.14.10-300.fc35.x86_64 of 65536 bytes failed

Quoted 6 lines

Add a helper function is_lzma_file() to help behave consistently.

---
Changes in v2:
- add a fclose() when file is too small.

Hi Lichen Liu,

I have already applied v1 of this patch,
so could you consider providing a follow-up patch
which contains the delta between v1 and v2?

Thanks!

Ref: https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/1b03cf7adc3c

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