at commit fcbc48dc
| 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 <corbet@lwn.net> |
| 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 | const 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 | const struct bin_attribute *const *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->grp.bin_attrs); |
| 62 | kfree(sect_attrs); |
| 63 | } |
| 64 | |
| 65 | static int add_sect_attrs(struct module *mod, const struct load_info *info) |
| 66 | { |
| 67 | struct module_sect_attrs *sect_attrs; |
| 68 | const struct bin_attribute **gattr; |
| 69 | struct bin_attribute *sattr; |
| 70 | unsigned int nloaded = 0, i; |
| 71 | int ret; |
| 72 | |
| 73 | /* Count loaded sections and allocate structures */ |
| 74 | for (i = 0; i < info->hdr->e_shnum; i++) |
| 75 | if (!sect_empty(&info->sechdrs[i])) |
| 76 | nloaded++; |
| 77 | sect_attrs = kzalloc_flex(*sect_attrs, attrs, nloaded); |
| 78 | if (!sect_attrs) |
| 79 | return -ENOMEM; |
| 80 | |
| 81 | gattr = kzalloc_objs(*gattr, nloaded + 1); |
| 82 | if (!gattr) { |
| 83 | kfree(sect_attrs); |
| 84 | return -ENOMEM; |
| 85 | } |
| 86 | |
| 87 | /* Setup section attributes. */ |
| 88 | sect_attrs->grp.name = "sections"; |
| 89 | sect_attrs->grp.bin_attrs = gattr; |
| 90 | |
| 91 | sattr = §_attrs->attrs[0]; |
| 92 | for (i = 0; i < info->hdr->e_shnum; i++) { |
| 93 | Elf_Shdr *sec = &info->sechdrs[i]; |
| 94 | |
| 95 | if (sect_empty(sec)) |
| 96 | continue; |
| 97 | sysfs_bin_attr_init(sattr); |
| 98 | sattr->attr.name = |
| 99 | kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL); |
| 100 | if (!sattr->attr.name) { |
| 101 | ret = -ENOMEM; |
| 102 | goto out; |
| 103 | } |
| 104 | sattr->read = module_sect_read; |
| 105 | sattr->private = (void *)sec->sh_addr; |
| 106 | sattr->size = MODULE_SECT_READ_SIZE; |
| 107 | sattr->attr.mode = 0400; |
| 108 | *(gattr++) = sattr++; |
| 109 | } |
| 110 | |
| 111 | ret = sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp); |
| 112 | if (ret) |
| 113 | goto out; |
| 114 | |
| 115 | mod->sect_attrs = sect_attrs; |
| 116 | return 0; |
| 117 | out: |
| 118 | free_sect_attrs(sect_attrs); |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static void remove_sect_attrs(struct module *mod) |
| 123 | { |
| 124 | if (mod->sect_attrs) { |
| 125 | sysfs_remove_group(&mod->mkobj.kobj, |
| 126 | &mod->sect_attrs->grp); |
| 127 | /* |
| 128 | * We are positive that no one is using any sect attrs |
| 129 | * at this point. Deallocate immediately. |
| 130 | */ |
| 131 | free_sect_attrs(mod->sect_attrs); |
| 132 | mod->sect_attrs = NULL; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. |
| 138 | */ |
| 139 | |
| 140 | struct module_notes_attrs { |
| 141 | struct attribute_group grp; |
| 142 | struct bin_attribute attrs[]; |
| 143 | }; |
| 144 | |
| 145 | static void free_notes_attrs(struct module_notes_attrs *notes_attrs) |
| 146 | { |
| 147 | kfree(notes_attrs->grp.bin_attrs); |
| 148 | kfree(notes_attrs); |
| 149 | } |
| 150 | |
| 151 | static int add_notes_attrs(struct module *mod, const struct load_info *info) |
| 152 | { |
| 153 | unsigned int notes, loaded, i; |
| 154 | struct module_notes_attrs *notes_attrs; |
| 155 | const struct bin_attribute **gattr; |
| 156 | struct bin_attribute *nattr; |
| 157 | int ret; |
| 158 | |
| 159 | /* Count notes sections and allocate structures. */ |
| 160 | notes = 0; |
| 161 | for (i = 0; i < info->hdr->e_shnum; i++) |
| 162 | if (!sect_empty(&info->sechdrs[i]) && |
| 163 | info->sechdrs[i].sh_type == SHT_NOTE) |
| 164 | ++notes; |
| 165 | |
| 166 | if (notes == 0) |
| 167 | return 0; |
| 168 | |
| 169 | notes_attrs = kzalloc_flex(*notes_attrs, attrs, notes); |
| 170 | if (!notes_attrs) |
| 171 | return -ENOMEM; |
| 172 | |
| 173 | gattr = kzalloc_objs(*gattr, notes + 1); |
| 174 | if (!gattr) { |
| 175 | kfree(notes_attrs); |
| 176 | return -ENOMEM; |
| 177 | } |
| 178 | |
| 179 | notes_attrs->grp.name = "notes"; |
| 180 | notes_attrs->grp.bin_attrs = gattr; |
| 181 | |
| 182 | nattr = ¬es_attrs->attrs[0]; |
| 183 | for (loaded = i = 0; i < info->hdr->e_shnum; ++i) { |
| 184 | if (sect_empty(&info->sechdrs[i])) |
| 185 | continue; |
| 186 | if (info->sechdrs[i].sh_type == SHT_NOTE) { |
| 187 | sysfs_bin_attr_init(nattr); |
| 188 | nattr->attr.name = mod->sect_attrs->attrs[loaded].attr.name; |
| 189 | nattr->attr.mode = 0444; |
| 190 | nattr->size = info->sechdrs[i].sh_size; |
| 191 | nattr->private = (void *)info->sechdrs[i].sh_addr; |
| 192 | nattr->read = sysfs_bin_attr_simple_read; |
| 193 | *(gattr++) = nattr++; |
| 194 | } |
| 195 | ++loaded; |
| 196 | } |
| 197 | |
| 198 | ret = sysfs_create_group(&mod->mkobj.kobj, ¬es_attrs->grp); |
| 199 | if (ret) |
| 200 | goto out; |
| 201 | |
| 202 | mod->notes_attrs = notes_attrs; |
| 203 | return 0; |
| 204 | |
| 205 | out: |
| 206 | free_notes_attrs(notes_attrs); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static void remove_notes_attrs(struct module *mod) |
| 211 | { |
| 212 | if (mod->notes_attrs) { |
| 213 | sysfs_remove_group(&mod->mkobj.kobj, |
| 214 | &mod->notes_attrs->grp); |
| 215 | /* |
| 216 | * We are positive that no one is using any notes attrs |
| 217 | * at this point. Deallocate immediately. |
| 218 | */ |
| 219 | free_notes_attrs(mod->notes_attrs); |
| 220 | mod->notes_attrs = NULL; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #else /* !CONFIG_KALLSYMS */ |
| 225 | static inline int add_sect_attrs(struct module *mod, const struct load_info *info) |
| 226 | { |
| 227 | return 0; |
| 228 | } |
| 229 | static inline void remove_sect_attrs(struct module *mod) { } |
| 230 | static inline int add_notes_attrs(struct module *mod, const struct load_info *info) |
| 231 | { |
| 232 | return 0; |
| 233 | } |
| 234 | static inline void remove_notes_attrs(struct module *mod) { } |
| 235 | #endif /* CONFIG_KALLSYMS */ |
| 236 | |
| 237 | static void del_usage_links(struct module *mod) |
| 238 | { |
| 239 | #ifdef CONFIG_MODULE_UNLOAD |
| 240 | struct module_use *use; |
| 241 | |
| 242 | mutex_lock(&module_mutex); |
| 243 | list_for_each_entry(use, &mod->target_list, target_list) |
| 244 | sysfs_remove_link(use->target->holders_dir, mod->name); |
| 245 | mutex_unlock(&module_mutex); |
| 246 | #endif |
| 247 | } |
| 248 | |
| 249 | static int add_usage_links(struct module *mod) |
| 250 | { |
| 251 | int ret = 0; |
| 252 | #ifdef CONFIG_MODULE_UNLOAD |
| 253 | struct module_use *use; |
| 254 | |
| 255 | mutex_lock(&module_mutex); |
| 256 | list_for_each_entry(use, &mod->target_list, target_list) { |
| 257 | ret = sysfs_create_link(use->target->holders_dir, |
| 258 | &mod->mkobj.kobj, mod->name); |
| 259 | if (ret) |
| 260 | break; |
| 261 | } |
| 262 | mutex_unlock(&module_mutex); |
| 263 | if (ret) |
| 264 | del_usage_links(mod); |
| 265 | #endif |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static void module_remove_modinfo_attrs(struct module *mod, int end) |
| 270 | { |
| 271 | const struct module_attribute *attr; |
| 272 | int i; |
| 273 | |
| 274 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { |
| 275 | if (end >= 0 && i > end) |
| 276 | break; |
| 277 | /* pick a field to test for end of list */ |
| 278 | if (!attr->attr.name) |
| 279 | break; |
| 280 | sysfs_remove_file(&mod->mkobj.kobj, &attr->attr); |
| 281 | if (attr->free) |
| 282 | attr->free(mod); |
| 283 | } |
| 284 | kfree(mod->modinfo_attrs); |
| 285 | } |
| 286 | |
| 287 | static int module_add_modinfo_attrs(struct module *mod) |
| 288 | { |
| 289 | const struct module_attribute *attr; |
| 290 | struct module_attribute *temp_attr; |
| 291 | int error = 0; |
| 292 | int i; |
| 293 | |
| 294 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * |
| 295 | (modinfo_attrs_count + 1)), |
| 296 | GFP_KERNEL); |
| 297 | if (!mod->modinfo_attrs) |
| 298 | return -ENOMEM; |
| 299 | |
| 300 | temp_attr = mod->modinfo_attrs; |
| 301 | for (i = 0; (attr = modinfo_attrs[i]); i++) { |
| 302 | if (!attr->test || attr->test(mod)) { |
| 303 | memcpy(temp_attr, attr, sizeof(*temp_attr)); |
| 304 | sysfs_attr_init(&temp_attr->attr); |
| 305 | error = sysfs_create_file(&mod->mkobj.kobj, |
| 306 | &temp_attr->attr); |
| 307 | if (error) |
| 308 | goto error_out; |
| 309 | ++temp_attr; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | |
| 315 | error_out: |
| 316 | if (i > 0) |
| 317 | module_remove_modinfo_attrs(mod, --i); |
| 318 | else |
| 319 | kfree(mod->modinfo_attrs); |
| 320 | return error; |
| 321 | } |
| 322 | |
| 323 | static void mod_kobject_put(struct module *mod) |
| 324 | { |
| 325 | DECLARE_COMPLETION_ONSTACK(c); |
| 326 | |
| 327 | mod->mkobj.kobj_completion = &c; |
| 328 | kobject_put(&mod->mkobj.kobj); |
| 329 | wait_for_completion(&c); |
| 330 | } |
| 331 | |
| 332 | static int mod_sysfs_init(struct module *mod) |
| 333 | { |
| 334 | int err; |
| 335 | struct kobject *kobj; |
| 336 | |
| 337 | if (!module_kset) { |
| 338 | pr_err("%s: module sysfs not initialized\n", mod->name); |
| 339 | err = -EINVAL; |
| 340 | goto out; |
| 341 | } |
| 342 | |
| 343 | kobj = kset_find_obj(module_kset, mod->name); |
| 344 | if (kobj) { |
| 345 | pr_err("%s: module is already loaded\n", mod->name); |
| 346 | kobject_put(kobj); |
| 347 | err = -EINVAL; |
| 348 | goto out; |
| 349 | } |
| 350 | |
| 351 | mod->mkobj.mod = mod; |
| 352 | |
| 353 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); |
| 354 | mod->mkobj.kobj.kset = module_kset; |
| 355 | err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, |
| 356 | "%s", mod->name); |
| 357 | if (err) |
| 358 | mod_kobject_put(mod); |
| 359 | |
| 360 | out: |
| 361 | return err; |
| 362 | } |
| 363 | |
| 364 | int mod_sysfs_setup(struct module *mod, |
| 365 | const struct load_info *info, |
| 366 | struct kernel_param *kparam, |
| 367 | unsigned int num_params) |
| 368 | { |
| 369 | int err; |
| 370 | |
| 371 | err = mod_sysfs_init(mod); |
| 372 | if (err) |
| 373 | goto out; |
| 374 | |
| 375 | mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); |
| 376 | if (!mod->holders_dir) { |
| 377 | err = -ENOMEM; |
| 378 | goto out_unreg; |
| 379 | } |
| 380 | |
| 381 | err = module_param_sysfs_setup(mod, kparam, num_params); |
| 382 | if (err) |
| 383 | goto out_unreg_holders; |
| 384 | |
| 385 | err = module_add_modinfo_attrs(mod); |
| 386 | if (err) |
| 387 | goto out_unreg_param; |
| 388 | |
| 389 | err = add_usage_links(mod); |
| 390 | if (err) |
| 391 | goto out_unreg_modinfo_attrs; |
| 392 | |
| 393 | err = add_sect_attrs(mod, info); |
| 394 | if (err) |
| 395 | goto out_del_usage_links; |
| 396 | |
| 397 | err = add_notes_attrs(mod, info); |
| 398 | if (err) |
| 399 | goto out_unreg_sect_attrs; |
| 400 | |
| 401 | return 0; |
| 402 | |
| 403 | out_unreg_sect_attrs: |
| 404 | remove_sect_attrs(mod); |
| 405 | out_del_usage_links: |
| 406 | del_usage_links(mod); |
| 407 | out_unreg_modinfo_attrs: |
| 408 | module_remove_modinfo_attrs(mod, -1); |
| 409 | out_unreg_param: |
| 410 | module_param_sysfs_remove(mod); |
| 411 | out_unreg_holders: |
| 412 | kobject_put(mod->holders_dir); |
| 413 | out_unreg: |
| 414 | mod_kobject_put(mod); |
| 415 | out: |
| 416 | return err; |
| 417 | } |
| 418 | |
| 419 | static void mod_sysfs_fini(struct module *mod) |
| 420 | { |
| 421 | remove_notes_attrs(mod); |
| 422 | remove_sect_attrs(mod); |
| 423 | mod_kobject_put(mod); |
| 424 | } |
| 425 | |
| 426 | void mod_sysfs_teardown(struct module *mod) |
| 427 | { |
| 428 | del_usage_links(mod); |
| 429 | module_remove_modinfo_attrs(mod, -1); |
| 430 | module_param_sysfs_remove(mod); |
| 431 | kobject_put(mod->mkobj.drivers_dir); |
| 432 | kobject_put(mod->holders_dir); |
| 433 | mod_sysfs_fini(mod); |
| 434 | } |
| 435 | |
| 436 | void init_param_lock(struct module *mod) |
| 437 | { |
| 438 | mutex_init(&mod->param_lock); |
| 439 | } |