Skip to content

Commit b1ae6dc

Browse files
dtormcgrof
authored andcommitted
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, Signed-off-by: Dmitry Torokhov <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]>
1 parent ef307fc commit b1ae6dc

File tree

6 files changed

+329
-11
lines changed

6 files changed

+329
-11
lines changed

include/uapi/linux/module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
/* Flags for sys_finit_module: */
66
#define MODULE_INIT_IGNORE_MODVERSIONS 1
77
#define MODULE_INIT_IGNORE_VERMAGIC 2
8+
#define MODULE_INIT_COMPRESSED_FILE 4
89

910
#endif /* _UAPI_LINUX_MODULE_H */

init/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,19 @@ config MODULE_COMPRESS_ZSTD
22742274

22752275
endchoice
22762276

2277+
config MODULE_DECOMPRESS
2278+
bool "Support in-kernel module decompression"
2279+
depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
2280+
select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
2281+
select XZ_DEC if MODULE_COMPRESS_XZ
2282+
help
2283+
2284+
Support for decompressing kernel modules by the kernel itself
2285+
instead of relying on userspace to perform this task. Useful when
2286+
load pinning security policy is enabled.
2287+
2288+
If unsure, say N.
2289+
22772290
config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
22782291
bool "Allow loading of modules with missing namespace imports"
22792292
help

kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ obj-y += up.o
6767
endif
6868
obj-$(CONFIG_UID16) += uid16.o
6969
obj-$(CONFIG_MODULES) += module.o
70+
obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o
7071
obj-$(CONFIG_MODULE_SIG) += module_signing.o
7172
obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
7273
obj-$(CONFIG_KALLSYMS) += kallsyms.o

kernel/module-internal.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,29 @@ struct load_info {
2222
bool sig_ok;
2323
#ifdef CONFIG_KALLSYMS
2424
unsigned long mod_kallsyms_init_off;
25+
#endif
26+
#ifdef CONFIG_MODULE_DECOMPRESS
27+
struct page **pages;
28+
unsigned int max_pages;
29+
unsigned int used_pages;
2530
#endif
2631
struct {
2732
unsigned int sym, str, mod, vers, info, pcpu;
2833
} index;
2934
};
3035

3136
extern int mod_verify_sig(const void *mod, struct load_info *info);
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+
static inline int module_decompress(struct load_info *info,
43+
const void *buf, size_t size)
44+
{
45+
return -EOPNOTSUPP;
46+
}
47+
static inline void module_decompress_cleanup(struct load_info *info)
48+
{
49+
}
50+
#endif

kernel/module.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,9 +3173,12 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,
31733173
return err;
31743174
}
31753175

3176-
static void free_copy(struct load_info *info)
3176+
static void free_copy(struct load_info *info, int flags)
31773177
{
3178-
vfree(info->hdr);
3178+
if (flags & MODULE_INIT_COMPRESSED_FILE)
3179+
module_decompress_cleanup(info);
3180+
else
3181+
vfree(info->hdr);
31793182
}
31803183

31813184
static int rewrite_section_headers(struct load_info *info, int flags)
@@ -4124,7 +4127,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
41244127
}
41254128

41264129
/* Get rid of temporary copy. */
4127-
free_copy(info);
4130+
free_copy(info, flags);
41284131

41294132
/* Done! */
41304133
trace_module_load(mod);
@@ -4173,7 +4176,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
41734176

41744177
module_deallocate(mod, info);
41754178
free_copy:
4176-
free_copy(info);
4179+
free_copy(info, flags);
41774180
return err;
41784181
}
41794182

@@ -4200,7 +4203,8 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
42004203
SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
42014204
{
42024205
struct load_info info = { };
4203-
void *hdr = NULL;
4206+
void *buf = NULL;
4207+
int len;
42044208
int err;
42054209

42064210
err = may_init_module();
@@ -4210,15 +4214,24 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
42104214
pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
42114215

42124216
if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
4213-
|MODULE_INIT_IGNORE_VERMAGIC))
4217+
|MODULE_INIT_IGNORE_VERMAGIC
4218+
|MODULE_INIT_COMPRESSED_FILE))
42144219
return -EINVAL;
42154220

4216-
err = kernel_read_file_from_fd(fd, 0, &hdr, INT_MAX, NULL,
4221+
len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL,
42174222
READING_MODULE);
4218-
if (err < 0)
4219-
return err;
4220-
info.hdr = hdr;
4221-
info.len = err;
4223+
if (len < 0)
4224+
return len;
4225+
4226+
if (flags & MODULE_INIT_COMPRESSED_FILE) {
4227+
err = module_decompress(&info, buf, len);
4228+
vfree(buf); /* compressed data is no longer needed */
4229+
if (err)
4230+
return err;
4231+
} else {
4232+
info.hdr = buf;
4233+
info.len = len;
4234+
}
42224235

42234236
return load_module(&info, uargs, flags);
42244237
}

0 commit comments

Comments
 (0)