Skip to content

Commit b537439

Browse files
committed
Merge tag 'modules-5.19-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux
Pull module fixes from Luis Chamberlain: "Although most of the move of code in in v5.19-rc1 should have not introduced a regression patch review on one of the file changes captured a checkpatch warning which advised to use strscpy() and it caused a buffer overflow when an incorrect length is passed. Another change which checkpatch complained about was an odd RCU usage, but that was properly addressed in a separate patch to the move by Aaron. That caused a regression with PREEMPT_RT=y due to an unbounded latency. This series fixes both and adjusts documentation which we forgot to do for the move" * tag 'modules-5.19-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux: module: kallsyms: Ensure preemption in add_kallsyms() with PREEMPT_RT doc: module: update file references module: Fix "warning: variable 'exit' set but not used" module: Fix selfAssignment cppcheck warning modules: Fix corruption of /proc/kallsyms
2 parents 816e51d + e69a661 commit b537439

File tree

9 files changed

+48
-35
lines changed

9 files changed

+48
-35
lines changed

Documentation/core-api/kernel-api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ Module Loading
223223
Inter Module support
224224
--------------------
225225

226-
Refer to the file kernel/module.c for more information.
226+
Refer to the files in kernel/module/ for more information.
227227

228228
Hardware Interfaces
229229
===================

Documentation/core-api/symbol-namespaces.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ namespace ``USB_STORAGE``, use::
5151
The corresponding ksymtab entry struct ``kernel_symbol`` will have the member
5252
``namespace`` set accordingly. A symbol that is exported without a namespace will
5353
refer to ``NULL``. There is no default namespace if none is defined. ``modpost``
54-
and kernel/module.c make use the namespace at build time or module load time,
55-
respectively.
54+
and kernel/module/main.c make use the namespace at build time or module load
55+
time, respectively.
5656

5757
2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
5858
=============================================

Documentation/livepatch/module-elf-format.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ module->symtab.
210210
=====================================
211211
Normally, a stripped down copy of a module's symbol table (containing only
212212
"core" symbols) is made available through module->symtab (See layout_symtab()
213-
in kernel/module.c). For livepatch modules, the symbol table copied into memory
214-
on module load must be exactly the same as the symbol table produced when the
215-
patch module was compiled. This is because the relocations in each livepatch
216-
relocation section refer to their respective symbols with their symbol indices,
217-
and the original symbol indices (and thus the symtab ordering) must be
213+
in kernel/module/kallsyms.c). For livepatch modules, the symbol table copied
214+
into memory on module load must be exactly the same as the symbol table produced
215+
when the patch module was compiled. This is because the relocations in each
216+
livepatch relocation section refer to their respective symbols with their symbol
217+
indices, and the original symbol indices (and thus the symtab ordering) must be
218218
preserved in order for apply_relocate_add() to find the right symbol.
219219

220220
For example, take this particular rela from a livepatch module:::

Documentation/translations/it_IT/core-api/symbol-namespaces.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ Di conseguenza, nella tabella dei simboli del kernel ci sarà una voce
5050
rappresentata dalla struttura ``kernel_symbol`` che avrà il campo
5151
``namespace`` (spazio dei nomi) impostato. Un simbolo esportato senza uno spazio
5252
dei nomi avrà questo campo impostato a ``NULL``. Non esiste uno spazio dei nomi
53-
di base. Il programma ``modpost`` e il codice in kernel/module.c usano lo spazio
54-
dei nomi, rispettivamente, durante la compilazione e durante il caricamento
55-
di un modulo.
53+
di base. Il programma ``modpost`` e il codice in kernel/module/main.c usano lo
54+
spazio dei nomi, rispettivamente, durante la compilazione e durante il
55+
caricamento di un modulo.
5656

5757
2.2 Usare il simbolo di preprocessore DEFAULT_SYMBOL_NAMESPACE
5858
==============================================================

Documentation/translations/zh_CN/core-api/kernel-api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ kernel/kmod.c
224224
模块接口支持
225225
------------
226226

227-
更多信息请参考文件kernel/module.c
227+
更多信息请参阅kernel/module/目录下的文件
228228

229229
硬件接口
230230
========

Documentation/translations/zh_CN/core-api/symbol-namespaces.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
相应的 ksymtab 条目结构体 ``kernel_symbol`` 将有相应的成员 ``命名空间`` 集。
5454
导出时未指明命名空间的符号将指向 ``NULL`` 。如果没有定义命名空间,则默认没有。
55-
``modpost`` 和kernel/module.c分别在构建时或模块加载时使用名称空间。
55+
``modpost`` 和kernel/module/main.c分别在构建时或模块加载时使用名称空间。
5656

5757
2.2 使用DEFAULT_SYMBOL_NAMESPACE定义
5858
====================================

kernel/module/internal.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/mutex.h>
1212
#include <linux/rculist.h>
1313
#include <linux/rcupdate.h>
14+
#include <linux/mm.h>
1415

1516
#ifndef ARCH_SHF_SMALL
1617
#define ARCH_SHF_SMALL 0
@@ -30,11 +31,13 @@
3031
* to ensure complete separation of code and data, but
3132
* only when CONFIG_STRICT_MODULE_RWX=y
3233
*/
33-
#ifdef CONFIG_STRICT_MODULE_RWX
34-
# define strict_align(X) PAGE_ALIGN(X)
35-
#else
36-
# define strict_align(X) (X)
37-
#endif
34+
static inline unsigned int strict_align(unsigned int size)
35+
{
36+
if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
37+
return PAGE_ALIGN(size);
38+
else
39+
return size;
40+
}
3841

3942
extern struct mutex module_mutex;
4043
extern struct list_head modules;

kernel/module/kallsyms.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ void layout_symtab(struct module *mod, struct load_info *info)
137137
info->symoffs = ALIGN(mod->data_layout.size, symsect->sh_addralign ?: 1);
138138
info->stroffs = mod->data_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
139139
mod->data_layout.size += strtab_size;
140+
/* Note add_kallsyms() computes strtab_size as core_typeoffs - stroffs */
140141
info->core_typeoffs = mod->data_layout.size;
141142
mod->data_layout.size += ndst * sizeof(char);
142143
mod->data_layout.size = strict_align(mod->data_layout.size);
@@ -169,19 +170,20 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
169170
Elf_Sym *dst;
170171
char *s;
171172
Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
173+
unsigned long strtab_size;
172174

173175
/* Set up to point into init section. */
174176
mod->kallsyms = (void __rcu *)mod->init_layout.base +
175177
info->mod_kallsyms_init_off;
176178

177-
preempt_disable();
179+
rcu_read_lock();
178180
/* The following is safe since this pointer cannot change */
179-
rcu_dereference_sched(mod->kallsyms)->symtab = (void *)symsec->sh_addr;
180-
rcu_dereference_sched(mod->kallsyms)->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
181+
rcu_dereference(mod->kallsyms)->symtab = (void *)symsec->sh_addr;
182+
rcu_dereference(mod->kallsyms)->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
181183
/* Make sure we get permanent strtab: don't use info->strtab. */
182-
rcu_dereference_sched(mod->kallsyms)->strtab =
184+
rcu_dereference(mod->kallsyms)->strtab =
183185
(void *)info->sechdrs[info->index.str].sh_addr;
184-
rcu_dereference_sched(mod->kallsyms)->typetab = mod->init_layout.base + info->init_typeoffs;
186+
rcu_dereference(mod->kallsyms)->typetab = mod->init_layout.base + info->init_typeoffs;
185187

186188
/*
187189
* Now populate the cut down core kallsyms for after init
@@ -190,22 +192,29 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
190192
mod->core_kallsyms.symtab = dst = mod->data_layout.base + info->symoffs;
191193
mod->core_kallsyms.strtab = s = mod->data_layout.base + info->stroffs;
192194
mod->core_kallsyms.typetab = mod->data_layout.base + info->core_typeoffs;
193-
src = rcu_dereference_sched(mod->kallsyms)->symtab;
194-
for (ndst = i = 0; i < rcu_dereference_sched(mod->kallsyms)->num_symtab; i++) {
195-
rcu_dereference_sched(mod->kallsyms)->typetab[i] = elf_type(src + i, info);
195+
strtab_size = info->core_typeoffs - info->stroffs;
196+
src = rcu_dereference(mod->kallsyms)->symtab;
197+
for (ndst = i = 0; i < rcu_dereference(mod->kallsyms)->num_symtab; i++) {
198+
rcu_dereference(mod->kallsyms)->typetab[i] = elf_type(src + i, info);
196199
if (i == 0 || is_livepatch_module(mod) ||
197200
is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
198201
info->index.pcpu)) {
202+
ssize_t ret;
203+
199204
mod->core_kallsyms.typetab[ndst] =
200-
rcu_dereference_sched(mod->kallsyms)->typetab[i];
205+
rcu_dereference(mod->kallsyms)->typetab[i];
201206
dst[ndst] = src[i];
202207
dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
203-
s += strscpy(s,
204-
&rcu_dereference_sched(mod->kallsyms)->strtab[src[i].st_name],
205-
KSYM_NAME_LEN) + 1;
208+
ret = strscpy(s,
209+
&rcu_dereference(mod->kallsyms)->strtab[src[i].st_name],
210+
strtab_size);
211+
if (ret < 0)
212+
break;
213+
s += ret + 1;
214+
strtab_size -= ret + 1;
206215
}
207216
}
208-
preempt_enable();
217+
rcu_read_unlock();
209218
mod->core_kallsyms.num_symtab = ndst;
210219
}
211220

kernel/module/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,24 +2939,25 @@ static void cfi_init(struct module *mod)
29392939
{
29402940
#ifdef CONFIG_CFI_CLANG
29412941
initcall_t *init;
2942+
#ifdef CONFIG_MODULE_UNLOAD
29422943
exitcall_t *exit;
2944+
#endif
29432945

29442946
rcu_read_lock_sched();
29452947
mod->cfi_check = (cfi_check_fn)
29462948
find_kallsyms_symbol_value(mod, "__cfi_check");
29472949
init = (initcall_t *)
29482950
find_kallsyms_symbol_value(mod, "__cfi_jt_init_module");
2949-
exit = (exitcall_t *)
2950-
find_kallsyms_symbol_value(mod, "__cfi_jt_cleanup_module");
2951-
rcu_read_unlock_sched();
2952-
29532951
/* Fix init/exit functions to point to the CFI jump table */
29542952
if (init)
29552953
mod->init = *init;
29562954
#ifdef CONFIG_MODULE_UNLOAD
2955+
exit = (exitcall_t *)
2956+
find_kallsyms_symbol_value(mod, "__cfi_jt_cleanup_module");
29572957
if (exit)
29582958
mod->exit = *exit;
29592959
#endif
2960+
rcu_read_unlock_sched();
29602961

29612962
cfi_module_add(mod, mod_tree.addr_min);
29622963
#endif

0 commit comments

Comments
 (0)