module: add in-kernel support for decompressing
PATCH v4 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,
---
v4:
- use late_initcall() instead of module_init() for setting up 'compression' sysfs attribute per Luis' request
v3:
- fix missing Kconfig dependency
v2:
- changed flag MODULE_INIT_COMPRESSED_DATA -> MODULE_INIT_COMPRESSED_FLAG per Kees' request
include/uapi/linux/module.h | 1 + init/Kconfig | 13 ++ kernel/Makefile | 1 + kernel/module-internal.h | 18 +++ kernel/module.c | 35 +++-- kernel/module_decompress.c | 271 ++++++++++++++++++++++++++++++++++++ 6 files changed, 328 insertions(+), 11 deletions(-)
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_FILE 4#endif /* _UAPI_LINUX_MODULE_H */
init/Kconfig +13 -0
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD endchoice +config MODULE_DECOMPRESS + bool "Support in-kernel module decompression" + depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ + 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
@@ -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
@@ -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
@@ -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_FILE ) +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_FILE ))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_FILE ) { + 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.1.448.ga2b2bfdf31-goog -- Dmitry
kernel test robot
build botI love your patch! Perhaps something to improve:
Hi Dmitry,
I love your patch! Perhaps something to improve:
[auto build test WARNING on kees/for-next/pstore]
[also build test WARNING on linus/master jeyu/modules-next v5.16-rc8 next-20211224] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Dmitry-Torokhov/module-add-in-kernel-support-for-decompressing/20220104-133017 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: um-i386_defconfig (https://download.01.org/0day-ci/archive/20220104/202201041739.pqEpumnU-lkp(a)intel.com/config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/dee0e9ca58491575033bc7911dd4fe8f7cea40e4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Dmitry-Torokhov/module-add-in-kernel-support-for-decompressing/20220104-133017 git checkout dee0e9ca58491575033bc7911dd4fe8f7cea40e4
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=um SUBARCH=i386 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
In file included from kernel/module.c:61:
>> kernel/module-internal.h:42:5: warning: no previous prototype for 'module_decompress' [-Wmissing-prototypes]
42 | int module_decompress(struct load_info *info, const void *buf, size_t size)
| ^~~~~~~~~~~~~~~~~
>> kernel/module-internal.h:46:6: warning: no previous prototype for 'module_decompress_cleanup' [-Wmissing-prototypes] 46 | void module_decompress_cleanup(struct load_info *info)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
vim +/module_decompress +42 kernel/module-internal.h
37
38 #ifdef CONFIG_MODULE_DECOMPRESS
39 int module_decompress(struct load_info *info, const void *buf, size_t size);
40 void module_decompress_cleanup(struct load_info *info);
41 #else
> 42 int module_decompress(struct load_info *info, const void *buf, size_t size)
43 {
44 return -EOPNOTSUPP;
45 }
> 46 void module_decompress_cleanup(struct load_info *info)---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botI love your patch! Yet something to improve:
Hi Dmitry,
I love your patch! Yet something to improve:
[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on linus/master jeyu/modules-next v5.16-rc8 next-20211224] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Dmitry-Torokhov/module-add-in-kernel-support-for-decompressing/20220104-133017 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20220104/202201041811.tEtjVHGi-lkp(a)intel.com/config) compiler: m68k-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/dee0e9ca58491575033bc7911dd4fe8f7cea40e4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Dmitry-Torokhov/module-add-in-kernel-support-for-decompressing/20220104-133017 git checkout dee0e9ca58491575033bc7911dd4fe8f7cea40e4
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=m68k SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
m68k-linux-ld: kernel/module_signing.o: in function `module_decompress':
>> module_signing.c:(.text+0x0): multiple definition of `module_decompress'; kernel/module.o:(.text+0x3280): first defined here m68k-linux-ld: kernel/module_signing.o: in function `module_decompress_cleanup': >> module_signing.c:(.text+0x40): multiple definition of `module_decompress_cleanup'; kernel/module.o:(.text+0x32c0): first defined here
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org