Skip to contentSkip to conversation

module: add in-kernel support for decompressing

PATCH Superseded Dmitry Torokhov 3 participants 6 messages Nov 28, 2021 – Dec 3, 2021

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 module: add in-kernel support for decompressing

Current scheme of having userspace decompress kernel modules before loading them into the kernel runs afoul of LoadPin security policy, as it loses link between the source of kernel module on the disk and binary blob that is being loaded into the kernel. To solve this issue let's implement decompression in kernel, so that we can pass a file descriptor of compressed module file into finit_module() which will keep LoadPin happy.

To let userspace know what compression/decompression scheme kernel supports it will create /sys/module/compression attribute. kmod can read this attribute and decide if it can pass compressed file to finit_module(). New MODULE_INIT_COMPRESSED_DATA flag indicates that the kernel should attempt to decompress the data read from file descriptor prior to trying load the module.

To simplify things kernel will only implement single decompression method matching compression method selected when generating modules. This patch implements gzip and xz; more can be added later,

---

I am also attaching a patch to kmod to make use of this new facility.

Thanks!

 include/uapi/linux/module.h |   1 +
 init/Kconfig                |  12 ++
 kernel/Makefile             |   1 +
 kernel/module-internal.h    |  18 +++
 kernel/module.c             |  35 +++--
 kernel/module_decompress.c  | 271 ++++++++++++++++++++++++++++++++++++
 6 files changed, 327 insertions(+), 11 deletions(-)
include/uapi/linux/module.h +1 -0
index 50d98ec5e866..becab4a1c5db 100644
@@ -5,5 +5,6 @@
 /* Flags for sys_finit_module: */
 #define MODULE_INIT_IGNORE_MODVERSIONS	1
 #define MODULE_INIT_IGNORE_VERMAGIC	2
+#define MODULE_INIT_COMPRESSED_DATA	4
 
 #endif /* _UAPI_LINUX_MODULE_H */
init/Kconfig +12 -0
index 03d3c21e28a3..a3f37ae7436d 100644
@@ -2309,6 +2309,18 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.
+
+	  If unsure, say N.
+
 config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
 	bool "Allow loading of modules with missing namespace imports"
 	help
kernel/Makefile +1 -0
index 186c49582f45..56f4ee97f328 100644
@@ -67,6 +67,7 @@ obj-y += up.o
 endif
 obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULES) += module.o
+obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o
 obj-$(CONFIG_MODULE_SIG) += module_signing.o
 obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
kernel/module-internal.h +18 -0
index 33783abc377b..3c1143d2c8c7 100644
@@ -22,6 +22,11 @@ struct load_info {
 	bool sig_ok;
 #ifdef CONFIG_KALLSYMS
 	unsigned long mod_kallsyms_init_off;
+#endif
+#ifdef CONFIG_MODULE_DECOMPRESS
+	struct page **pages;
+	unsigned int max_pages;
+	unsigned int used_pages;
 #endif
 	struct {
 		unsigned int sym, str, mod, vers, info, pcpu;
@@ -29,3 +34,16 @@ struct load_info {
 };
 
 extern int mod_verify_sig(const void *mod, struct load_info *info);
+
+#ifdef CONFIG_MODULE_DECOMPRESS
+int module_decompress(struct load_info *info, const void *buf, size_t size);
+void module_decompress_cleanup(struct load_info *info);
+#else
+int module_decompress(struct load_info *info, const void *buf, size_t size)
+{
+	return -EOPNOTSUPP;
+}
+void module_decompress_cleanup(struct load_info *info)
+{
+}
+#endif
kernel/module.c +24 -11
index 84a9141a5e15..eeab85ea1627 100644
@@ -3174,9 +3174,12 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,
 	return err;
 }
 
-static void free_copy(struct load_info *info)
+static void free_copy(struct load_info *info, int flags)
 {
-	vfree(info->hdr);
+	if (flags & MODULE_INIT_COMPRESSED_DATA)
+		module_decompress_cleanup(info);
+	else
+		vfree(info->hdr);
 }
 
 static int rewrite_section_headers(struct load_info *info, int flags)
@@ -4125,7 +4128,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	}
 
 	/* Get rid of temporary copy. */
-	free_copy(info);
+	free_copy(info, flags);
 
 	/* Done! */
 	trace_module_load(mod);
@@ -4174,7 +4177,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 
 	module_deallocate(mod, info);
  free_copy:
-	free_copy(info);
+	free_copy(info, flags);
 	return err;
 }
 
@@ -4201,7 +4204,8 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 {
 	struct load_info info = { };
-	void *hdr = NULL;
+	void *buf = NULL;
+	int len;
 	int err;
 
 	err = may_init_module();
@@ -4211,15 +4215,24 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 	pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
 
 	if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
-		      |MODULE_INIT_IGNORE_VERMAGIC))
+		      |MODULE_INIT_IGNORE_VERMAGIC
+		      |MODULE_INIT_COMPRESSED_DATA))
 		return -EINVAL;
 
-	err = kernel_read_file_from_fd(fd, 0, &hdr, INT_MAX, NULL,
+	len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL,
 				       READING_MODULE);
-	if (err < 0)
-		return err;
-	info.hdr = hdr;
-	info.len = err;
+	if (len < 0)
+		return len;
+
+	if (flags & MODULE_INIT_COMPRESSED_DATA) {
+		err = module_decompress(&info, buf, len);
+		vfree(buf); /* compressed data is no longer needed */
+		if (err)
+			return err;
+	} else {
+		info.hdr = buf;
+		info.len = len;
+	}
 
 	return load_module(&info, uargs, flags);
 }
kernel/module_decompress.c new file +271 -0

271 lines not on this page · raw message

Signature · 5 lines
2.34.0.rc2.393.gf8c9666880-goog


-- 
Dmitry

From 45b430985585164fe2d600795805f6d02b04ac96 Mon Sep 17 00:00:00 2001
From: Dmitry Torokhov <[email protected]>
Date: Sun, 14 Nov 2021 14:25:27 -0800
Subject: [PATCH] kmod: add support for passing compressed modules directly
into kernel

Kernel's load pinning facilities interfere with loading compressed modules, because if decompression happens in userspace and module is instantiated via init_module() call there is no way for kernel to ascertain that module data is coming from a trusted source. To solve this issue module decompression support was added to the kernel.

Let's use the new kernel facility, and if kernel indicates (via /sys/module/compression) that that it can handle certain compression scheme, then use finit_module() and pass a file descriptor to the compressed module instead of init_module() with decompressed data.

---
 libkmod/libkmod-file.c     | 41 +++++++++++++++++++-------------------
 libkmod/libkmod-internal.h |  9 ++++++++-
 libkmod/libkmod-module.c   | 41 +++++++++++++++++++++++++++++++++++++-
 shared/missing.h           |  4 ++++
 4 files changed, 73 insertions(+), 22 deletions(-)
libkmod/libkmod-file.c +21 -20

126 lines not on this page · raw message

libkmod/libkmod-internal.h +8 -1
index c22644a..a8405b3 100644
@@ -48,6 +48,13 @@ static _always_inline_ _printf_format_(2, 3) void
 #  endif
 #endif
 
+enum kmod_compression {
+	KMOD_COMPRESSION_NONE,
+	KMOD_COMPRESSION_GZIP,
+	KMOD_COMPRESSION_XZ,
+	KMOD_COMPRESSION_ZSTD,
+};
+
 void kmod_log(const struct kmod_ctx *ctx,
 		int priority, const char *file, int line, const char *fn,
 		const char *format, ...) __attribute__((format(printf, 6, 7))) __attribute__((nonnull(1, 3, 5)));
@@ -155,7 +162,7 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filenam
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file) __attribute__((nonnull(1)));
 void *kmod_file_get_contents(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
 off_t kmod_file_get_size(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
-bool kmod_file_get_direct(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
+enum kmod_compression kmod_file_get_compression(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
 int kmod_file_get_fd(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
 void kmod_file_unref(struct kmod_file *file) __attribute__((nonnull(1)));
 
libkmod/libkmod-module.c +40 -1

65 lines not on this page · raw message

shared/missing.h +4 -0
index 4c0d136..f1e68b6 100644
@@ -15,6 +15,10 @@
 # define MODULE_INIT_IGNORE_VERMAGIC 2
 #endif
 
+#ifndef MODULE_INIT_COMPRESSED_DATA
+# define MODULE_INIT_COMPRESSED_DATA 4
+#endif
+
 #ifndef __NR_finit_module
 # define __NR_finit_module -1
 #endif
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
#1 lore raw
Quoted 7 lines · Dmitry Torokhov

Current scheme of having userspace decompress kernel modules before loading them into the kernel runs afoul of LoadPin security policy, as it loses link between the source of kernel module on the disk and binary blob that is being loaded into the kernel. To solve this issue let's implement decompression in kernel, so that we can pass a file descriptor of compressed module file into finit_module() which will keep LoadPin happy.

Yeah, adding module signing for this case seems like needless overhead when there is an existing fd association back down to a dm-verity device, etc.

Quoted 6 lines

To let userspace know what compression/decompression scheme kernel supports it will create /sys/module/compression attribute. kmod can read this attribute and decide if it can pass compressed file to finit_module(). New MODULE_INIT_COMPRESSED_DATA flag indicates that the kernel should attempt to decompress the data read from file descriptor prior to trying load the module.

Cool; this seems reasonable.

Quoted 24 lines

To simplify things kernel will only implement single decompression method matching compression method selected when generating modules. This patch implements gzip and xz; more can be added later,

---

I am also attaching a patch to kmod to make use of this new facility.

Thanks!

 include/uapi/linux/module.h |   1 +
 init/Kconfig                |  12 ++
 kernel/Makefile             |   1 +
 kernel/module-internal.h    |  18 +++
 kernel/module.c             |  35 +++--
 kernel/module_decompress.c  | 271 ++++++++++++++++++++++++++++++++++++
 6 files changed, 327 insertions(+), 11 deletions(-)
include/uapi/linux/module.h +0 -0
index 50d98ec5e866..becab4a1c5db 100644

include/uapi/linux/module.h line 8

Quoted 4 lines
include/uapi/linux/module.h +1 -0
@@ -5,5 +5,6 @@
 /* Flags for sys_finit_module: */
 #define MODULE_INIT_IGNORE_MODVERSIONS	1
 #define MODULE_INIT_IGNORE_VERMAGIC	2
+#define MODULE_INIT_COMPRESSED_DATA	4

bikeshedding: adding "_DATA" seems redundant/misleading? The entire module is compressed, so maybe call it just MODULE_INIT_COMPRESSED ?

Quoted 75 lines
include/uapi/linux/module.h +0 -0
 #endif /* _UAPI_LINUX_MODULE_H */
init/Kconfig +12 -0
index 03d3c21e28a3..a3f37ae7436d 100644
@@ -2309,6 +2309,18 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.
+
+	  If unsure, say N.
+
 config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
 	bool "Allow loading of modules with missing namespace imports"
 	help
kernel/Makefile +1 -0
index 186c49582f45..56f4ee97f328 100644
@@ -67,6 +67,7 @@ obj-y += up.o
 endif
 obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULES) += module.o
+obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o
 obj-$(CONFIG_MODULE_SIG) += module_signing.o
 obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
kernel/module-internal.h +18 -0
index 33783abc377b..3c1143d2c8c7 100644
@@ -22,6 +22,11 @@ struct load_info {
 	bool sig_ok;
 #ifdef CONFIG_KALLSYMS
 	unsigned long mod_kallsyms_init_off;
+#endif
+#ifdef CONFIG_MODULE_DECOMPRESS
+	struct page **pages;
+	unsigned int max_pages;
+	unsigned int used_pages;
 #endif
 	struct {
 		unsigned int sym, str, mod, vers, info, pcpu;
@@ -29,3 +34,16 @@ struct load_info {
 };
 
 extern int mod_verify_sig(const void *mod, struct load_info *info);
+
+#ifdef CONFIG_MODULE_DECOMPRESS
+int module_decompress(struct load_info *info, const void *buf, size_t size);
+void module_decompress_cleanup(struct load_info *info);
+#else
+int module_decompress(struct load_info *info, const void *buf, size_t size)
+{
+	return -EOPNOTSUPP;
+}
+void module_decompress_cleanup(struct load_info *info)
+{
+}
+#endif
kernel/module.c +0 -0
index 84a9141a5e15..eeab85ea1627 100644

kernel/module.c static int copy_module_from_user(const void __user *umod, unsigned long len, · line 3177

Quoted 5 lines
kernel/module.c +1 -1
@@ -3174,9 +3174,12 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,
 	return err;
 }
 
-static void free_copy(struct load_info *info)
+static void free_copy(struct load_info *info, int flags)

Since struct load_info is already being modified, how about adding flags there instead, then it doesn't need to be plumbed down into each of these functions?

kernel/module_decompress.c

Quoted 73 lines
kernel/module.c +23 -10
 {
-	vfree(info->hdr);
+	if (flags & MODULE_INIT_COMPRESSED_DATA)
+		module_decompress_cleanup(info);
+	else
+		vfree(info->hdr);
 }
 
 static int rewrite_section_headers(struct load_info *info, int flags)
@@ -4125,7 +4128,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	}
 
 	/* Get rid of temporary copy. */
-	free_copy(info);
+	free_copy(info, flags);
 
 	/* Done! */
 	trace_module_load(mod);
@@ -4174,7 +4177,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 
 	module_deallocate(mod, info);
  free_copy:
-	free_copy(info);
+	free_copy(info, flags);
 	return err;
 }
 
@@ -4201,7 +4204,8 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 {
 	struct load_info info = { };
-	void *hdr = NULL;
+	void *buf = NULL;
+	int len;
 	int err;
 
 	err = may_init_module();
@@ -4211,15 +4215,24 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 	pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
 
 	if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
-		      |MODULE_INIT_IGNORE_VERMAGIC))
+		      |MODULE_INIT_IGNORE_VERMAGIC
+		      |MODULE_INIT_COMPRESSED_DATA))
 		return -EINVAL;
 
-	err = kernel_read_file_from_fd(fd, 0, &hdr, INT_MAX, NULL,
+	len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL,
 				       READING_MODULE);
-	if (err < 0)
-		return err;
-	info.hdr = hdr;
-	info.len = err;
+	if (len < 0)
+		return len;
+
+	if (flags & MODULE_INIT_COMPRESSED_DATA) {
+		err = module_decompress(&info, buf, len);
+		vfree(buf); /* compressed data is no longer needed */
+		if (err)
+			return err;
+	} else {
+		info.hdr = buf;
+		info.len = len;
+	}
 
 	return load_module(&info, uargs, flags);
 }
kernel/module_decompress.c new file +0 -0
new file mode 100644
index 000000000000..590ca00aa098

I think most of this can be dropped. I think using crypto_comp_decompress() would make much more sense.

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 8 lines · Kees Cook
Quoted 4 lines · Dmitry Torokhov

/* Flags for sys_finit_module: */
#define MODULE_INIT_IGNORE_MODVERSIONS 1
#define MODULE_INIT_IGNORE_VERMAGIC 2
+#define MODULE_INIT_COMPRESSED_DATA 4

bikeshedding: adding "_DATA" seems redundant/misleading? The entire module is compressed, so maybe call it just MODULE_INIT_COMPRESSED ?

OK, or maybe MODULE_INIT_COMPRESSED_FILE since we are indeed dealing with a file?

Thanks.

Signature · 1 line
Dmitry
#3 lore raw
Quoted 7 lines · Dmitry Torokhov
Quoted 6 lines · Kees Cook
Quoted 4 lines · Dmitry Torokhov

/* Flags for sys_finit_module: */
#define MODULE_INIT_IGNORE_MODVERSIONS 1
#define MODULE_INIT_IGNORE_VERMAGIC 2
+#define MODULE_INIT_COMPRESSED_DATA 4

Quoted 5 lines
Quoted 2 lines

bikeshedding: adding "_DATA" seems redundant/misleading? The entire module is compressed, so maybe call it just MODULE_INIT_COMPRESSED ?

OK, or maybe MODULE_INIT_COMPRESSED_FILE since we are indeed dealing with a file?

Sounds good to me! :)

As far as my tangent on using the crypto subsystem, I think that looks like a long path, so your existing routines are likely the right place to start. I still wonder if it might be able to use of the "acomp" API instead of calling directly into the specific decompressor.

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 2 lines · Kees Cook

I still wonder if it might be able to use of the "acomp" API instead of calling directly into the specific decompressor.

drivers/base/firmware_loader/main.c also deals with decompression of files, so I wonder if something can be shared with that code too?

Luis

#5 lore raw
In reply to Kees Cook
Quoted 14 lines · Kees Cook
Quoted 7 lines · Dmitry Torokhov
Quoted 6 lines · Kees Cook
Quoted 4 lines · Dmitry Torokhov

/* Flags for sys_finit_module: */
#define MODULE_INIT_IGNORE_MODVERSIONS 1
#define MODULE_INIT_IGNORE_VERMAGIC 2
+#define MODULE_INIT_COMPRESSED_DATA 4

Quoted 5 lines
Quoted 2 lines

bikeshedding: adding "_DATA" seems redundant/misleading? The entire module is compressed, so maybe call it just MODULE_INIT_COMPRESSED ?

OK, or maybe MODULE_INIT_COMPRESSED_FILE since we are indeed dealing with a file?

Quoted 6 lines

Sounds good to me! :)

As far as my tangent on using the crypto subsystem, I think that looks like a long path, so your existing routines are likely the right place to start. I still wonder if it might be able to use of the "acomp" API instead of calling directly into the specific decompressor.

I looked at the acomp and it still needs preallocation of the output buffer, so it has to wait for your "get worst size" API additions before it can be used. But ideally I'd have some streaming option where I could feed it chunks of data and consume output...

Also, regarding your comment about not changing free_copy() but move this logic to load_module(): free copy is called twice there, both in success and error paths, and can't really be reduced to one call site even with "jumping backwards goto", so I'd be forced to duplicate this logic in 2 places, which is not great. Or did I misunderstand your idea?

Thanks.

Signature · 1 line
Dmitry