xref: /linux-6.15/kernel/module/sysfs.c (revision 34f5ec0f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Module sysfs support
4  *
5  * Copyright (C) 2008 Rusty Russell
6  */
7 
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/fs.h>
11 #include <linux/sysfs.h>
12 #include <linux/slab.h>
13 #include <linux/kallsyms.h>
14 #include <linux/mutex.h>
15 #include "internal.h"
16 
17 /*
18  * /sys/module/foo/sections stuff
19  * J. Corbet <[email protected]>
20  */
21 #ifdef CONFIG_KALLSYMS
22 struct module_sect_attrs {
23 	struct attribute_group grp;
24 	struct bin_attribute attrs[];
25 };
26 
27 #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
28 static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
29 				struct bin_attribute *battr,
30 				char *buf, loff_t pos, size_t count)
31 {
32 	char bounce[MODULE_SECT_READ_SIZE + 1];
33 	size_t wrote;
34 
35 	if (pos != 0)
36 		return -EINVAL;
37 
38 	/*
39 	 * Since we're a binary read handler, we must account for the
40 	 * trailing NUL byte that sprintf will write: if "buf" is
41 	 * too small to hold the NUL, or the NUL is exactly the last
42 	 * byte, the read will look like it got truncated by one byte.
43 	 * Since there is no way to ask sprintf nicely to not write
44 	 * the NUL, we have to use a bounce buffer.
45 	 */
46 	wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
47 			  kallsyms_show_value(file->f_cred)
48 				? battr->private : NULL);
49 	count = min(count, wrote);
50 	memcpy(buf, bounce, count);
51 
52 	return count;
53 }
54 
55 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
56 {
57 	struct bin_attribute **bin_attr;
58 
59 	for (bin_attr = sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++)
60 		kfree((*bin_attr)->attr.name);
61 	kfree(sect_attrs);
62 }
63 
64 static int add_sect_attrs(struct module *mod, const struct load_info *info)
65 {
66 	unsigned int nloaded = 0, i, size[2];
67 	struct module_sect_attrs *sect_attrs;
68 	struct bin_attribute **gattr;
69 	struct bin_attribute *sattr;
70 	int ret;
71 
72 	/* Count loaded sections and allocate structures */
73 	for (i = 0; i < info->hdr->e_shnum; i++)
74 		if (!sect_empty(&info->sechdrs[i]))
75 			nloaded++;
76 	size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
77 			sizeof(sect_attrs->grp.bin_attrs[0]));
78 	size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
79 	sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
80 	if (!sect_attrs)
81 		return -ENOMEM;
82 
83 	/* Setup section attributes. */
84 	sect_attrs->grp.name = "sections";
85 	sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
86 
87 	sattr = &sect_attrs->attrs[0];
88 	gattr = &sect_attrs->grp.bin_attrs[0];
89 	for (i = 0; i < info->hdr->e_shnum; i++) {
90 		Elf_Shdr *sec = &info->sechdrs[i];
91 
92 		if (sect_empty(sec))
93 			continue;
94 		sysfs_bin_attr_init(sattr);
95 		sattr->attr.name =
96 			kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
97 		if (!sattr->attr.name) {
98 			ret = -ENOMEM;
99 			goto out;
100 		}
101 		sattr->read = module_sect_read;
102 		sattr->private = (void *)sec->sh_addr;
103 		sattr->size = MODULE_SECT_READ_SIZE;
104 		sattr->attr.mode = 0400;
105 		*(gattr++) = sattr++;
106 	}
107 	*gattr = NULL;
108 
109 	ret = sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp);
110 	if (ret)
111 		goto out;
112 
113 	mod->sect_attrs = sect_attrs;
114 	return 0;
115 out:
116 	free_sect_attrs(sect_attrs);
117 	return ret;
118 }
119 
120 static void remove_sect_attrs(struct module *mod)
121 {
122 	if (mod->sect_attrs) {
123 		sysfs_remove_group(&mod->mkobj.kobj,
124 				   &mod->sect_attrs->grp);
125 		/*
126 		 * We are positive that no one is using any sect attrs
127 		 * at this point.  Deallocate immediately.
128 		 */
129 		free_sect_attrs(mod->sect_attrs);
130 		mod->sect_attrs = NULL;
131 	}
132 }
133 
134 /*
135  * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
136  */
137 
138 struct module_notes_attrs {
139 	struct kobject *dir;
140 	unsigned int notes;
141 	struct bin_attribute attrs[] __counted_by(notes);
142 };
143 
144 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
145 			     unsigned int i)
146 {
147 	if (notes_attrs->dir) {
148 		while (i-- > 0)
149 			sysfs_remove_bin_file(notes_attrs->dir,
150 					      &notes_attrs->attrs[i]);
151 		kobject_put(notes_attrs->dir);
152 	}
153 	kfree(notes_attrs);
154 }
155 
156 static int add_notes_attrs(struct module *mod, const struct load_info *info)
157 {
158 	unsigned int notes, loaded, i;
159 	struct module_notes_attrs *notes_attrs;
160 	struct bin_attribute *nattr;
161 	int ret;
162 
163 	/* Count notes sections and allocate structures.  */
164 	notes = 0;
165 	for (i = 0; i < info->hdr->e_shnum; i++)
166 		if (!sect_empty(&info->sechdrs[i]) &&
167 		    info->sechdrs[i].sh_type == SHT_NOTE)
168 			++notes;
169 
170 	if (notes == 0)
171 		return 0;
172 
173 	notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
174 			      GFP_KERNEL);
175 	if (!notes_attrs)
176 		return -ENOMEM;
177 
178 	notes_attrs->notes = notes;
179 	nattr = &notes_attrs->attrs[0];
180 	for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
181 		if (sect_empty(&info->sechdrs[i]))
182 			continue;
183 		if (info->sechdrs[i].sh_type == SHT_NOTE) {
184 			sysfs_bin_attr_init(nattr);
185 			nattr->attr.name = mod->sect_attrs->attrs[loaded].attr.name;
186 			nattr->attr.mode = 0444;
187 			nattr->size = info->sechdrs[i].sh_size;
188 			nattr->private = (void *)info->sechdrs[i].sh_addr;
189 			nattr->read = sysfs_bin_attr_simple_read;
190 			++nattr;
191 		}
192 		++loaded;
193 	}
194 
195 	notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
196 	if (!notes_attrs->dir) {
197 		ret = -ENOMEM;
198 		goto out;
199 	}
200 
201 	for (i = 0; i < notes; ++i) {
202 		ret = sysfs_create_bin_file(notes_attrs->dir, &notes_attrs->attrs[i]);
203 		if (ret)
204 			goto out;
205 	}
206 
207 	mod->notes_attrs = notes_attrs;
208 	return 0;
209 
210 out:
211 	free_notes_attrs(notes_attrs, i);
212 	return ret;
213 }
214 
215 static void remove_notes_attrs(struct module *mod)
216 {
217 	if (mod->notes_attrs)
218 		free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
219 }
220 
221 #else /* !CONFIG_KALLSYMS */
222 static inline int add_sect_attrs(struct module *mod, const struct load_info *info)
223 {
224 	return 0;
225 }
226 static inline void remove_sect_attrs(struct module *mod) { }
227 static inline int add_notes_attrs(struct module *mod, const struct load_info *info)
228 {
229 	return 0;
230 }
231 static inline void remove_notes_attrs(struct module *mod) { }
232 #endif /* CONFIG_KALLSYMS */
233 
234 static void del_usage_links(struct module *mod)
235 {
236 #ifdef CONFIG_MODULE_UNLOAD
237 	struct module_use *use;
238 
239 	mutex_lock(&module_mutex);
240 	list_for_each_entry(use, &mod->target_list, target_list)
241 		sysfs_remove_link(use->target->holders_dir, mod->name);
242 	mutex_unlock(&module_mutex);
243 #endif
244 }
245 
246 static int add_usage_links(struct module *mod)
247 {
248 	int ret = 0;
249 #ifdef CONFIG_MODULE_UNLOAD
250 	struct module_use *use;
251 
252 	mutex_lock(&module_mutex);
253 	list_for_each_entry(use, &mod->target_list, target_list) {
254 		ret = sysfs_create_link(use->target->holders_dir,
255 					&mod->mkobj.kobj, mod->name);
256 		if (ret)
257 			break;
258 	}
259 	mutex_unlock(&module_mutex);
260 	if (ret)
261 		del_usage_links(mod);
262 #endif
263 	return ret;
264 }
265 
266 static void module_remove_modinfo_attrs(struct module *mod, int end)
267 {
268 	const struct module_attribute *attr;
269 	int i;
270 
271 	for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
272 		if (end >= 0 && i > end)
273 			break;
274 		/* pick a field to test for end of list */
275 		if (!attr->attr.name)
276 			break;
277 		sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
278 		if (attr->free)
279 			attr->free(mod);
280 	}
281 	kfree(mod->modinfo_attrs);
282 }
283 
284 static int module_add_modinfo_attrs(struct module *mod)
285 {
286 	const struct module_attribute *attr;
287 	struct module_attribute *temp_attr;
288 	int error = 0;
289 	int i;
290 
291 	mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
292 					(modinfo_attrs_count + 1)),
293 					GFP_KERNEL);
294 	if (!mod->modinfo_attrs)
295 		return -ENOMEM;
296 
297 	temp_attr = mod->modinfo_attrs;
298 	for (i = 0; (attr = modinfo_attrs[i]); i++) {
299 		if (!attr->test || attr->test(mod)) {
300 			memcpy(temp_attr, attr, sizeof(*temp_attr));
301 			sysfs_attr_init(&temp_attr->attr);
302 			error = sysfs_create_file(&mod->mkobj.kobj,
303 						  &temp_attr->attr);
304 			if (error)
305 				goto error_out;
306 			++temp_attr;
307 		}
308 	}
309 
310 	return 0;
311 
312 error_out:
313 	if (i > 0)
314 		module_remove_modinfo_attrs(mod, --i);
315 	else
316 		kfree(mod->modinfo_attrs);
317 	return error;
318 }
319 
320 static void mod_kobject_put(struct module *mod)
321 {
322 	DECLARE_COMPLETION_ONSTACK(c);
323 
324 	mod->mkobj.kobj_completion = &c;
325 	kobject_put(&mod->mkobj.kobj);
326 	wait_for_completion(&c);
327 }
328 
329 static int mod_sysfs_init(struct module *mod)
330 {
331 	int err;
332 	struct kobject *kobj;
333 
334 	if (!module_kset) {
335 		pr_err("%s: module sysfs not initialized\n", mod->name);
336 		err = -EINVAL;
337 		goto out;
338 	}
339 
340 	kobj = kset_find_obj(module_kset, mod->name);
341 	if (kobj) {
342 		pr_err("%s: module is already loaded\n", mod->name);
343 		kobject_put(kobj);
344 		err = -EINVAL;
345 		goto out;
346 	}
347 
348 	mod->mkobj.mod = mod;
349 
350 	memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
351 	mod->mkobj.kobj.kset = module_kset;
352 	err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
353 				   "%s", mod->name);
354 	if (err)
355 		mod_kobject_put(mod);
356 
357 out:
358 	return err;
359 }
360 
361 int mod_sysfs_setup(struct module *mod,
362 		    const struct load_info *info,
363 			   struct kernel_param *kparam,
364 			   unsigned int num_params)
365 {
366 	int err;
367 
368 	err = mod_sysfs_init(mod);
369 	if (err)
370 		goto out;
371 
372 	mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
373 	if (!mod->holders_dir) {
374 		err = -ENOMEM;
375 		goto out_unreg;
376 	}
377 
378 	err = module_param_sysfs_setup(mod, kparam, num_params);
379 	if (err)
380 		goto out_unreg_holders;
381 
382 	err = module_add_modinfo_attrs(mod);
383 	if (err)
384 		goto out_unreg_param;
385 
386 	err = add_usage_links(mod);
387 	if (err)
388 		goto out_unreg_modinfo_attrs;
389 
390 	err = add_sect_attrs(mod, info);
391 	if (err)
392 		goto out_del_usage_links;
393 
394 	err = add_notes_attrs(mod, info);
395 	if (err)
396 		goto out_unreg_sect_attrs;
397 
398 	return 0;
399 
400 out_unreg_sect_attrs:
401 	remove_sect_attrs(mod);
402 out_del_usage_links:
403 	del_usage_links(mod);
404 out_unreg_modinfo_attrs:
405 	module_remove_modinfo_attrs(mod, -1);
406 out_unreg_param:
407 	module_param_sysfs_remove(mod);
408 out_unreg_holders:
409 	kobject_put(mod->holders_dir);
410 out_unreg:
411 	mod_kobject_put(mod);
412 out:
413 	return err;
414 }
415 
416 static void mod_sysfs_fini(struct module *mod)
417 {
418 	remove_notes_attrs(mod);
419 	remove_sect_attrs(mod);
420 	mod_kobject_put(mod);
421 }
422 
423 void mod_sysfs_teardown(struct module *mod)
424 {
425 	del_usage_links(mod);
426 	module_remove_modinfo_attrs(mod, -1);
427 	module_param_sysfs_remove(mod);
428 	kobject_put(mod->mkobj.drivers_dir);
429 	kobject_put(mod->holders_dir);
430 	mod_sysfs_fini(mod);
431 }
432 
433 void init_param_lock(struct module *mod)
434 {
435 	mutex_init(&mod->param_lock);
436 }
437