Skip to content

Commit 2f3238a

Browse files
committed
module: add flags arg to sys_finit_module()
Thanks to Michael Kerrisk for keeping us honest. These flags are actually useful for eliminating the only case where kmod has to mangle a module's internals: for overriding module versioning. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Acked-by: Lucas De Marchi <lucas.demarchi@profusion.mobi> Acked-by: Kees Cook <keescook@chromium.org>
1 parent 34e1169 commit 2f3238a

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎include/linux/syscalls.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,5 +868,5 @@ asmlinkage long sys_process_vm_writev(pid_t pid,
868868

869869
asmlinkage long sys_kcmp(pid_t pid1, pid_t pid2, int type,
870870
unsigned long idx1, unsigned long idx2);
871-
asmlinkage long sys_finit_module(int fd, const char __user *uargs);
871+
asmlinkage long sys_finit_module(int fd, const char __user *uargs, int flags);
872872
#endif

‎include/uapi/linux/module.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _UAPI_LINUX_MODULE_H
2+
#define _UAPI_LINUX_MODULE_H
3+
4+
/* Flags for sys_finit_module: */
5+
#define MODULE_INIT_IGNORE_MODVERSIONS 1
6+
#define MODULE_INIT_IGNORE_VERMAGIC 2
7+
8+
#endif /* _UAPI_LINUX_MODULE_H */

‎kernel/module.c‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <linux/pfn.h>
6161
#include <linux/bsearch.h>
6262
#include <linux/fips.h>
63+
#include <uapi/linux/module.h>
6364
#include "module-internal.h"
6465

6566
#define CREATE_TRACE_POINTS
@@ -2553,7 +2554,7 @@ static void free_copy(struct load_info *info)
25532554
vfree(info->hdr);
25542555
}
25552556

2556-
static int rewrite_section_headers(struct load_info *info)
2557+
static int rewrite_section_headers(struct load_info *info, int flags)
25572558
{
25582559
unsigned int i;
25592560

@@ -2581,7 +2582,10 @@ static int rewrite_section_headers(struct load_info *info)
25812582
}
25822583

25832584
/* Track but don't keep modinfo and version sections. */
2584-
info->index.vers = find_sec(info, "__versions");
2585+
if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
2586+
info->index.vers = 0; /* Pretend no __versions section! */
2587+
else
2588+
info->index.vers = find_sec(info, "__versions");
25852589
info->index.info = find_sec(info, ".modinfo");
25862590
info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
25872591
info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
@@ -2596,7 +2600,7 @@ static int rewrite_section_headers(struct load_info *info)
25962600
* Return the temporary module pointer (we'll replace it with the final
25972601
* one when we move the module sections around).
25982602
*/
2599-
static struct module *setup_load_info(struct load_info *info)
2603+
static struct module *setup_load_info(struct load_info *info, int flags)
26002604
{
26012605
unsigned int i;
26022606
int err;
@@ -2607,7 +2611,7 @@ static struct module *setup_load_info(struct load_info *info)
26072611
info->secstrings = (void *)info->hdr
26082612
+ info->sechdrs[info->hdr->e_shstrndx].sh_offset;
26092613

2610-
err = rewrite_section_headers(info);
2614+
err = rewrite_section_headers(info, flags);
26112615
if (err)
26122616
return ERR_PTR(err);
26132617

@@ -2645,11 +2649,14 @@ static struct module *setup_load_info(struct load_info *info)
26452649
return mod;
26462650
}
26472651

2648-
static int check_modinfo(struct module *mod, struct load_info *info)
2652+
static int check_modinfo(struct module *mod, struct load_info *info, int flags)
26492653
{
26502654
const char *modmagic = get_modinfo(info, "vermagic");
26512655
int err;
26522656

2657+
if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2658+
modmagic = NULL;
2659+
26532660
/* This is allowed: modprobe --force will invalidate it. */
26542661
if (!modmagic) {
26552662
err = try_to_force_load(mod, "bad vermagic");
@@ -2885,18 +2892,18 @@ int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
28852892
return 0;
28862893
}
28872894

2888-
static struct module *layout_and_allocate(struct load_info *info)
2895+
static struct module *layout_and_allocate(struct load_info *info, int flags)
28892896
{
28902897
/* Module within temporary copy. */
28912898
struct module *mod;
28922899
Elf_Shdr *pcpusec;
28932900
int err;
28942901

2895-
mod = setup_load_info(info);
2902+
mod = setup_load_info(info, flags);
28962903
if (IS_ERR(mod))
28972904
return mod;
28982905

2899-
err = check_modinfo(mod, info);
2906+
err = check_modinfo(mod, info, flags);
29002907
if (err)
29012908
return ERR_PTR(err);
29022909

@@ -3078,7 +3085,8 @@ static int may_init_module(void)
30783085

30793086
/* Allocate and load the module: note that size of section 0 is always
30803087
zero, and we rely on this for optional sections. */
3081-
static int load_module(struct load_info *info, const char __user *uargs)
3088+
static int load_module(struct load_info *info, const char __user *uargs,
3089+
int flags)
30823090
{
30833091
struct module *mod, *old;
30843092
long err;
@@ -3092,7 +3100,7 @@ static int load_module(struct load_info *info, const char __user *uargs)
30923100
goto free_copy;
30933101

30943102
/* Figure out module layout, and allocate all the memory. */
3095-
mod = layout_and_allocate(info);
3103+
mod = layout_and_allocate(info, flags);
30963104
if (IS_ERR(mod)) {
30973105
err = PTR_ERR(mod);
30983106
goto free_copy;
@@ -3241,10 +3249,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
32413249
if (err)
32423250
return err;
32433251

3244-
return load_module(&info, uargs);
3252+
return load_module(&info, uargs, 0);
32453253
}
32463254

3247-
SYSCALL_DEFINE2(finit_module, int, fd, const char __user *, uargs)
3255+
SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
32483256
{
32493257
int err;
32503258
struct load_info info = { };
@@ -3253,13 +3261,17 @@ SYSCALL_DEFINE2(finit_module, int, fd, const char __user *, uargs)
32533261
if (err)
32543262
return err;
32553263

3256-
pr_debug("finit_module: fd=%d, uargs=%p\n", fd, uargs);
3264+
pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3265+
3266+
if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3267+
|MODULE_INIT_IGNORE_VERMAGIC))
3268+
return -EINVAL;
32573269

32583270
err = copy_module_from_fd(fd, &info);
32593271
if (err)
32603272
return err;
32613273

3262-
return load_module(&info, uargs);
3274+
return load_module(&info, uargs, flags);
32633275
}
32643276

32653277
static inline int within(unsigned long addr, void *start, unsigned long size)

0 commit comments

Comments
 (0)