Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'modules-for-v5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/jeyu/linux

Pull module updates from Jessica Yu:
"Summary of modules changes for the 5.3 merge window:

- Code fixes and cleanups

- Fix bug where set_memory_x() wasn't being called when rodata=n

- Fix bug where -EEXIST was being returned for going modules

- Allow arches to override module_exit_section()"

* tag 'modules-for-v5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/jeyu/linux:
modules: fix compile error if don't have strict module rwx
ARM: module: recognize unwind exit sections
module: allow arch overrides for .exit section names
modules: fix BUG when load module with rodata=n
kernel/module: Fix mem leak in module_add_modinfo_attrs
kernel: module: Use struct_size() helper
kernel/module.c: Only return -EEXIST for modules that have finished loading

+53 -19
+7
arch/arm/kernel/module.c
··· 55 55 } 56 56 #endif 57 57 58 + bool module_exit_section(const char *name) 59 + { 60 + return strstarts(name, ".exit") || 61 + strstarts(name, ".ARM.extab.exit") || 62 + strstarts(name, ".ARM.exidx.exit"); 63 + } 64 + 58 65 int 59 66 apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, 60 67 unsigned int relindex, struct module *module)
+5
include/linux/moduleloader.h
··· 29 29 /* Free memory returned from module_alloc. */ 30 30 void module_memfree(void *module_region); 31 31 32 + /* Determines if the section name is an exit section (that is only used during 33 + * module unloading) 34 + */ 35 + bool module_exit_section(const char *name); 36 + 32 37 /* 33 38 * Apply the given relocation to the (simplified) ELF. Return -error 34 39 * or 0.
+41 -19
kernel/module.c
··· 1492 1492 for (i = 0; i < info->hdr->e_shnum; i++) 1493 1493 if (!sect_empty(&info->sechdrs[i])) 1494 1494 nloaded++; 1495 - size[0] = ALIGN(sizeof(*sect_attrs) 1496 - + nloaded * sizeof(sect_attrs->attrs[0]), 1495 + size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded), 1497 1496 sizeof(sect_attrs->grp.attrs[0])); 1498 1497 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]); 1499 1498 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL); ··· 1696 1697 return ret; 1697 1698 } 1698 1699 1700 + static void module_remove_modinfo_attrs(struct module *mod, int end); 1701 + 1699 1702 static int module_add_modinfo_attrs(struct module *mod) 1700 1703 { 1701 1704 struct module_attribute *attr; ··· 1712 1711 return -ENOMEM; 1713 1712 1714 1713 temp_attr = mod->modinfo_attrs; 1715 - for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { 1714 + for (i = 0; (attr = modinfo_attrs[i]); i++) { 1716 1715 if (!attr->test || attr->test(mod)) { 1717 1716 memcpy(temp_attr, attr, sizeof(*temp_attr)); 1718 1717 sysfs_attr_init(&temp_attr->attr); 1719 1718 error = sysfs_create_file(&mod->mkobj.kobj, 1720 1719 &temp_attr->attr); 1720 + if (error) 1721 + goto error_out; 1721 1722 ++temp_attr; 1722 1723 } 1723 1724 } 1725 + 1726 + return 0; 1727 + 1728 + error_out: 1729 + if (i > 0) 1730 + module_remove_modinfo_attrs(mod, --i); 1724 1731 return error; 1725 1732 } 1726 1733 1727 - static void module_remove_modinfo_attrs(struct module *mod) 1734 + static void module_remove_modinfo_attrs(struct module *mod, int end) 1728 1735 { 1729 1736 struct module_attribute *attr; 1730 1737 int i; 1731 1738 1732 1739 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { 1740 + if (end >= 0 && i > end) 1741 + break; 1733 1742 /* pick a field to test for end of list */ 1734 1743 if (!attr->attr.name) 1735 1744 break; ··· 1827 1816 return 0; 1828 1817 1829 1818 out_unreg_modinfo_attrs: 1830 - module_remove_modinfo_attrs(mod); 1819 + module_remove_modinfo_attrs(mod, -1); 1831 1820 out_unreg_param: 1832 1821 module_param_sysfs_remove(mod); 1833 1822 out_unreg_holders: ··· 1863 1852 { 1864 1853 } 1865 1854 1866 - static void module_remove_modinfo_attrs(struct module *mod) 1855 + static void module_remove_modinfo_attrs(struct module *mod, int end) 1867 1856 { 1868 1857 } 1869 1858 ··· 1879 1868 static void mod_sysfs_teardown(struct module *mod) 1880 1869 { 1881 1870 del_usage_links(mod); 1882 - module_remove_modinfo_attrs(mod); 1871 + module_remove_modinfo_attrs(mod, -1); 1883 1872 module_param_sysfs_remove(mod); 1884 1873 kobject_put(mod->mkobj.drivers_dir); 1885 1874 kobject_put(mod->holders_dir); 1886 1875 mod_sysfs_fini(mod); 1887 1876 } 1888 1877 1889 - #ifdef CONFIG_STRICT_MODULE_RWX 1878 + #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX 1890 1879 /* 1891 1880 * LKM RO/NX protection: protect module's text/ro-data 1892 1881 * from modification and any data from execution. ··· 1909 1898 layout->text_size >> PAGE_SHIFT); 1910 1899 } 1911 1900 1901 + #ifdef CONFIG_STRICT_MODULE_RWX 1912 1902 static void frob_rodata(const struct module_layout *layout, 1913 1903 int (*set_memory)(unsigned long start, int num_pages)) 1914 1904 { ··· 1961 1949 set_vm_flush_reset_perms(mod->core_layout.base); 1962 1950 set_vm_flush_reset_perms(mod->init_layout.base); 1963 1951 frob_text(&mod->core_layout, set_memory_ro); 1964 - frob_text(&mod->core_layout, set_memory_x); 1965 1952 1966 1953 frob_rodata(&mod->core_layout, set_memory_ro); 1967 - 1968 1954 frob_text(&mod->init_layout, set_memory_ro); 1969 - frob_text(&mod->init_layout, set_memory_x); 1970 - 1971 1955 frob_rodata(&mod->init_layout, set_memory_ro); 1972 1956 1973 1957 if (after_init) ··· 2022 2014 } 2023 2015 mutex_unlock(&module_mutex); 2024 2016 } 2025 - #else 2017 + #else /* !CONFIG_STRICT_MODULE_RWX */ 2026 2018 static void module_enable_nx(const struct module *mod) { } 2027 - #endif 2019 + #endif /* CONFIG_STRICT_MODULE_RWX */ 2020 + static void module_enable_x(const struct module *mod) 2021 + { 2022 + frob_text(&mod->core_layout, set_memory_x); 2023 + frob_text(&mod->init_layout, set_memory_x); 2024 + } 2025 + #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ 2026 + static void module_enable_nx(const struct module *mod) { } 2027 + static void module_enable_x(const struct module *mod) { } 2028 + #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ 2029 + 2028 2030 2029 2031 #ifdef CONFIG_LIVEPATCH 2030 2032 /* ··· 2741 2723 return vmalloc_exec(size); 2742 2724 } 2743 2725 2726 + bool __weak module_exit_section(const char *name) 2727 + { 2728 + return strstarts(name, ".exit"); 2729 + } 2730 + 2744 2731 #ifdef CONFIG_DEBUG_KMEMLEAK 2745 2732 static void kmemleak_load_module(const struct module *mod, 2746 2733 const struct load_info *info) ··· 2935 2912 2936 2913 #ifndef CONFIG_MODULE_UNLOAD 2937 2914 /* Don't load .exit sections */ 2938 - if (strstarts(info->secstrings+shdr->sh_name, ".exit")) 2915 + if (module_exit_section(info->secstrings+shdr->sh_name)) 2939 2916 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC; 2940 2917 #endif 2941 2918 } ··· 3413 3390 sched_annotate_sleep(); 3414 3391 mutex_lock(&module_mutex); 3415 3392 mod = find_module_all(name, strlen(name), true); 3416 - ret = !mod || mod->state == MODULE_STATE_LIVE 3417 - || mod->state == MODULE_STATE_GOING; 3393 + ret = !mod || mod->state == MODULE_STATE_LIVE; 3418 3394 mutex_unlock(&module_mutex); 3419 3395 3420 3396 return ret; ··· 3603 3581 mutex_lock(&module_mutex); 3604 3582 old = find_module_all(mod->name, strlen(mod->name), true); 3605 3583 if (old != NULL) { 3606 - if (old->state == MODULE_STATE_COMING 3607 - || old->state == MODULE_STATE_UNFORMED) { 3584 + if (old->state != MODULE_STATE_LIVE) { 3608 3585 /* Wait in case it fails to load. */ 3609 3586 mutex_unlock(&module_mutex); 3610 3587 err = wait_event_interruptible(module_wq, ··· 3642 3621 3643 3622 module_enable_ro(mod, false); 3644 3623 module_enable_nx(mod); 3624 + module_enable_x(mod); 3645 3625 3646 3626 /* Mark state as coming so strong_try_module_get() ignores us, 3647 3627 * but kallsyms etc. can see us. */