Skip to content

Commit ce47f7c

Browse files
aleegitmcgrof
authored andcommitted
module: abort module loading when sysfs setup suffer errors
When insmod a kernel module, if fails in add_notes_attrs or add_sysfs_attrs such as memory allocation fail, mod_sysfs_setup will still return success, but we can't access user interface on android device. Patch for make mod_sysfs_setup can check the error of add_notes_attrs and add_sysfs_attrs [mcgrof: the section stuff comes from linux history.git [0]] Fixes: 3f7b067 ("Module section offsets in /sys/module") [0] Fixes: 6d76013 ("Add /sys/module/name/notes") Acked-by: Luis Chamberlain <mcgrof@kernel.org> Reviewed-by: Petr Pavlu <petr.pavlu@suse.com> Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202409010016.3XIFSmRA-lkp@intel.com/ Closes: https://lore.kernel.org/oe-kbuild-all/202409072018.qfEzZbO7-lkp@intel.com/ Link: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=3f7b0672086b97b2d7f322bdc289cbfa203f10ef [0] Signed-off-by: Xion Wang <xion.wang@mediatek.com> Signed-off-by: Chunhui Li <chunhui.li@mediatek.com> Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
1 parent 907fa79 commit ce47f7c

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

kernel/module/sysfs.c

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
6969
kfree(sect_attrs);
7070
}
7171

72-
static void add_sect_attrs(struct module *mod, const struct load_info *info)
72+
static int add_sect_attrs(struct module *mod, const struct load_info *info)
7373
{
7474
unsigned int nloaded = 0, i, size[2];
7575
struct module_sect_attrs *sect_attrs;
7676
struct module_sect_attr *sattr;
7777
struct bin_attribute **gattr;
78+
int ret;
7879

7980
/* Count loaded sections and allocate structures */
8081
for (i = 0; i < info->hdr->e_shnum; i++)
@@ -85,7 +86,7 @@ static void add_sect_attrs(struct module *mod, const struct load_info *info)
8586
size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
8687
sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
8788
if (!sect_attrs)
88-
return;
89+
return -ENOMEM;
8990

9091
/* Setup section attributes. */
9192
sect_attrs->grp.name = "sections";
@@ -103,8 +104,10 @@ static void add_sect_attrs(struct module *mod, const struct load_info *info)
103104
sattr->address = sec->sh_addr;
104105
sattr->battr.attr.name =
105106
kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
106-
if (!sattr->battr.attr.name)
107+
if (!sattr->battr.attr.name) {
108+
ret = -ENOMEM;
107109
goto out;
110+
}
108111
sect_attrs->nsections++;
109112
sattr->battr.read = module_sect_read;
110113
sattr->battr.size = MODULE_SECT_READ_SIZE;
@@ -113,13 +116,15 @@ static void add_sect_attrs(struct module *mod, const struct load_info *info)
113116
}
114117
*gattr = NULL;
115118

116-
if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
119+
ret = sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp);
120+
if (ret)
117121
goto out;
118122

119123
mod->sect_attrs = sect_attrs;
120-
return;
124+
return 0;
121125
out:
122126
free_sect_attrs(sect_attrs);
127+
return ret;
123128
}
124129

125130
static void remove_sect_attrs(struct module *mod)
@@ -158,15 +163,12 @@ static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
158163
kfree(notes_attrs);
159164
}
160165

161-
static void add_notes_attrs(struct module *mod, const struct load_info *info)
166+
static int add_notes_attrs(struct module *mod, const struct load_info *info)
162167
{
163168
unsigned int notes, loaded, i;
164169
struct module_notes_attrs *notes_attrs;
165170
struct bin_attribute *nattr;
166-
167-
/* failed to create section attributes, so can't create notes */
168-
if (!mod->sect_attrs)
169-
return;
171+
int ret;
170172

171173
/* Count notes sections and allocate structures. */
172174
notes = 0;
@@ -176,12 +178,12 @@ static void add_notes_attrs(struct module *mod, const struct load_info *info)
176178
++notes;
177179

178180
if (notes == 0)
179-
return;
181+
return 0;
180182

181183
notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
182184
GFP_KERNEL);
183185
if (!notes_attrs)
184-
return;
186+
return -ENOMEM;
185187

186188
notes_attrs->notes = notes;
187189
nattr = &notes_attrs->attrs[0];
@@ -201,19 +203,23 @@ static void add_notes_attrs(struct module *mod, const struct load_info *info)
201203
}
202204

203205
notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
204-
if (!notes_attrs->dir)
206+
if (!notes_attrs->dir) {
207+
ret = -ENOMEM;
205208
goto out;
209+
}
206210

207-
for (i = 0; i < notes; ++i)
208-
if (sysfs_create_bin_file(notes_attrs->dir,
209-
&notes_attrs->attrs[i]))
211+
for (i = 0; i < notes; ++i) {
212+
ret = sysfs_create_bin_file(notes_attrs->dir, &notes_attrs->attrs[i]);
213+
if (ret)
210214
goto out;
215+
}
211216

212217
mod->notes_attrs = notes_attrs;
213-
return;
218+
return 0;
214219

215220
out:
216221
free_notes_attrs(notes_attrs, i);
222+
return ret;
217223
}
218224

219225
static void remove_notes_attrs(struct module *mod)
@@ -223,9 +229,15 @@ static void remove_notes_attrs(struct module *mod)
223229
}
224230

225231
#else /* !CONFIG_KALLSYMS */
226-
static inline void add_sect_attrs(struct module *mod, const struct load_info *info) { }
232+
static inline int add_sect_attrs(struct module *mod, const struct load_info *info)
233+
{
234+
return 0;
235+
}
227236
static inline void remove_sect_attrs(struct module *mod) { }
228-
static inline void add_notes_attrs(struct module *mod, const struct load_info *info) { }
237+
static inline int add_notes_attrs(struct module *mod, const struct load_info *info)
238+
{
239+
return 0;
240+
}
229241
static inline void remove_notes_attrs(struct module *mod) { }
230242
#endif /* CONFIG_KALLSYMS */
231243

@@ -385,11 +397,20 @@ int mod_sysfs_setup(struct module *mod,
385397
if (err)
386398
goto out_unreg_modinfo_attrs;
387399

388-
add_sect_attrs(mod, info);
389-
add_notes_attrs(mod, info);
400+
err = add_sect_attrs(mod, info);
401+
if (err)
402+
goto out_del_usage_links;
403+
404+
err = add_notes_attrs(mod, info);
405+
if (err)
406+
goto out_unreg_sect_attrs;
390407

391408
return 0;
392409

410+
out_unreg_sect_attrs:
411+
remove_sect_attrs(mod);
412+
out_del_usage_links:
413+
del_usage_links(mod);
393414
out_unreg_modinfo_attrs:
394415
module_remove_modinfo_attrs(mod, -1);
395416
out_unreg_param:

0 commit comments

Comments
 (0)