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

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
1647}1647}
1648 1648
1649/*1649/*
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.
1651 *1652 *
1652 * Also do basic validity checks against section offsets and sizes, the1653 * This does basic validity checks against section offsets and sizes, the
1653 * section name string table, and the indices used for it (sh_name).1654 * 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.
1654 */1668 */
1655static int elf_validity_check(struct load_info *info)1669static int elf_validity_cache_copy(struct load_info *info, int flags)
1656{1670{
1657 unsigned int i;1671 unsigned int i;
1658 Elf_Shdr *shdr, *strhdr;1672 Elf_Shdr *shdr, *strhdr;
@@ -1872,6 +1886,13 @@ static int elf_validity_check(struct load_info *info) · 213 unmodified lines
1872 if (!info->name)1886 if (!info->name)
1873 info->name = info->mod->name;1887 info->name = info->mod->name;
1874 1888
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
1875 return 0;1896 return 0;
1876 1897
1877no_exec:1898no_exec:
@@ -1984,26 +2005,6 @@ static int rewrite_section_headers(struct load_info *info, int flags) · 106 unmodified lines
1984 return 0;2005 return 0;
1985}2006}
1986 2007
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 */
1995static 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
2007/*2008/*
2008 * These calls taint the kernel depending certain module circumstances */2009 * These calls taint the kernel depending certain module circumstances */
2009static void module_augment_kernel_taints(struct module *mod, struct load_info *info)2010static 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
2809 2810
2810 /*2811 /*
2811 * Do basic sanity checks against the ELF header and2812 * Do basic sanity checks against the ELF header and
2812 * sections.2813 * sections. Cache useful sections and set the
2813 */2814 * info->mod to the userspace passed struct module.
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.
2821 */2815 */
2822 err = setup_load_info(info, flags);2816 err = elf_validity_cache_copy(info, flags);
2823 if (err)2817 if (err)
2824 goto free_copy;2818 goto free_copy;
2825 2819