History

module: merge remnants of setup_load_info() to elf validation

The setup_load_info() was actually had ELF validation checks of its own. To later cache useful variables as an secondary step just means looping again over the ELF sections we just validated. We can simply keep tabs of the key sections of interest as we validate the module ELF section in one swoop, so do that and merge the two routines together. Expand a bit on the documentation / intent / goals.

Luis Chamberlain committed 3 years ago

Commit 3d40bb91 parent 1bb49db1 file · +27 −33Browse files

Showing the changes to kernel/module (1 of 1 file). Show all

Unified · Split

kernel/module/main.c+27−33
@@ -1647,12 +1647,26 @@ static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) · 1646 unmodified lines
16471647 }
16481648
16491649 /*
1650− * Sanity checks against invalid binaries, wrong arch, weird elf version.
1650+ * Check userspace passed ELF module against our expectations, and cache
1651+ * useful variables for further processing as we go.
16511652 *
1652− * Also do basic validity checks against section offsets and sizes, the
1653+ * This does basic validity checks against section offsets and sizes, the
16531654 * section name string table, and the indices used for it (sh_name).
1655+ *
1656+ * As a last step, since we're already checking the ELF sections we cache
1657+ * useful variables which will be used later for our convenience:
1658+ *
1659+ * o pointers to section headers
1660+ * o cache the modinfo symbol section
1661+ * o cache the string symbol section
1662+ * o cache the module section
1663+ *
1664+ * As a last step we set info->mod to the temporary copy of the module in
1665+ * info->hdr. The final one will be allocated in move_module(). Any
1666+ * modifications we make to our copy of the module will be carried over
1667+ * to the final minted module.
16541668 */
1655−static int elf_validity_check(struct load_info *info)
1669+static int elf_validity_cache_copy(struct load_info *info, int flags)
16561670 {
16571671 unsigned int i;
16581672 Elf_Shdr *shdr, *strhdr;
@@ -1872,6 +1886,13 @@ static int elf_validity_check(struct load_info *info) · 213 unmodified lines
18721886 if (!info->name)
18731887 info->name = info->mod->name;
18741888
1889+ if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
1890+ info->index.vers = 0; /* Pretend no __versions section! */
1891+ else
1892+ info->index.vers = find_sec(info, "__versions");
1893+
1894+ info->index.pcpu = find_pcpusec(info);
1895+
18751896 return 0;
18761897
18771898 no_exec:
@@ -1984,26 +2005,6 @@ static int rewrite_section_headers(struct load_info *info, int flags) · 106 unmodified lines
19842005 return 0;
19852006 }
19862007
1987−/*
1988− * Set up our basic convenience variables (pointers to section headers,
1989− * search for module section index etc), and do some basic section
1990− * verification.
1991− *
1992− * Set info->mod to the temporary copy of the module in info->hdr. The final one
1993− * will be allocated in move_module().
1994− */
1995−static int setup_load_info(struct load_info *info, int flags)
1996−{
1997− if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
1998− info->index.vers = 0; /* Pretend no __versions section! */
1999− else
2000− info->index.vers = find_sec(info, "__versions");
2001−
2002− info->index.pcpu = find_pcpusec(info);
2003−
2004− return 0;
2005−}
2006−
20072008 /*
20082009 * These calls taint the kernel depending certain module circumstances */
20092010 static void module_augment_kernel_taints(struct module *mod, struct load_info *info)
@@ -2809,17 +2810,10 @@ static int load_module(struct load_info *info, const char __user *uargs, · 799 unmodified lines
28092810
28102811 /*
28112812 * Do basic sanity checks against the ELF header and
2812− * sections.
2813− */
2814− err = elf_validity_check(info);
2815− if (err)
2816− goto free_copy;
2817−
2818− /*
2819− * Everything checks out, so set up the section info
2820− * in the info structure.
2813+ * sections. Cache useful sections and set the
2814+ * info->mod to the userspace passed struct module.
28212815 */
2822− err = setup_load_info(info, flags);
2816+ err = elf_validity_cache_copy(info, flags);
28232817 if (err)
28242818 goto free_copy;
28252819