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->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 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(struct_size(sect_attrs, attrs, nloaded), GFP_KERNEL); 78 if (!sect_attrs) 79 return -ENOMEM; 80 81 gattr = kcalloc(nloaded + 1, sizeof(*gattr), GFP_KERNEL); 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 kobject *dir; 142 unsigned int notes; 143 struct bin_attribute attrs[] __counted_by(notes); 144 }; 145 146 static void free_notes_attrs(struct module_notes_attrs *notes_attrs, 147 unsigned int i) 148 { 149 if (notes_attrs->dir) { 150 while (i-- > 0) 151 sysfs_remove_bin_file(notes_attrs->dir, 152 ¬es_attrs->attrs[i]); 153 kobject_put(notes_attrs->dir); 154 } 155 kfree(notes_attrs); 156 } 157 158 static int add_notes_attrs(struct module *mod, const struct load_info *info) 159 { 160 unsigned int notes, loaded, i; 161 struct module_notes_attrs *notes_attrs; 162 struct bin_attribute *nattr; 163 int ret; 164 165 /* Count notes sections and allocate structures. */ 166 notes = 0; 167 for (i = 0; i < info->hdr->e_shnum; i++) 168 if (!sect_empty(&info->sechdrs[i]) && 169 info->sechdrs[i].sh_type == SHT_NOTE) 170 ++notes; 171 172 if (notes == 0) 173 return 0; 174 175 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes), 176 GFP_KERNEL); 177 if (!notes_attrs) 178 return -ENOMEM; 179 180 notes_attrs->notes = notes; 181 nattr = ¬es_attrs->attrs[0]; 182 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) { 183 if (sect_empty(&info->sechdrs[i])) 184 continue; 185 if (info->sechdrs[i].sh_type == SHT_NOTE) { 186 sysfs_bin_attr_init(nattr); 187 nattr->attr.name = mod->sect_attrs->attrs[loaded].attr.name; 188 nattr->attr.mode = 0444; 189 nattr->size = info->sechdrs[i].sh_size; 190 nattr->private = (void *)info->sechdrs[i].sh_addr; 191 nattr->read = sysfs_bin_attr_simple_read; 192 ++nattr; 193 } 194 ++loaded; 195 } 196 197 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj); 198 if (!notes_attrs->dir) { 199 ret = -ENOMEM; 200 goto out; 201 } 202 203 for (i = 0; i < notes; ++i) { 204 ret = sysfs_create_bin_file(notes_attrs->dir, ¬es_attrs->attrs[i]); 205 if (ret) 206 goto out; 207 } 208 209 mod->notes_attrs = notes_attrs; 210 return 0; 211 212 out: 213 free_notes_attrs(notes_attrs, i); 214 return ret; 215 } 216 217 static void remove_notes_attrs(struct module *mod) 218 { 219 if (mod->notes_attrs) 220 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes); 221 } 222 223 #else /* !CONFIG_KALLSYMS */ 224 static inline int add_sect_attrs(struct module *mod, const struct load_info *info) 225 { 226 return 0; 227 } 228 static inline void remove_sect_attrs(struct module *mod) { } 229 static inline int add_notes_attrs(struct module *mod, const struct load_info *info) 230 { 231 return 0; 232 } 233 static inline void remove_notes_attrs(struct module *mod) { } 234 #endif /* CONFIG_KALLSYMS */ 235 236 static void del_usage_links(struct module *mod) 237 { 238 #ifdef CONFIG_MODULE_UNLOAD 239 struct module_use *use; 240 241 mutex_lock(&module_mutex); 242 list_for_each_entry(use, &mod->target_list, target_list) 243 sysfs_remove_link(use->target->holders_dir, mod->name); 244 mutex_unlock(&module_mutex); 245 #endif 246 } 247 248 static int add_usage_links(struct module *mod) 249 { 250 int ret = 0; 251 #ifdef CONFIG_MODULE_UNLOAD 252 struct module_use *use; 253 254 mutex_lock(&module_mutex); 255 list_for_each_entry(use, &mod->target_list, target_list) { 256 ret = sysfs_create_link(use->target->holders_dir, 257 &mod->mkobj.kobj, mod->name); 258 if (ret) 259 break; 260 } 261 mutex_unlock(&module_mutex); 262 if (ret) 263 del_usage_links(mod); 264 #endif 265 return ret; 266 } 267 268 static void module_remove_modinfo_attrs(struct module *mod, int end) 269 { 270 const struct module_attribute *attr; 271 int i; 272 273 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { 274 if (end >= 0 && i > end) 275 break; 276 /* pick a field to test for end of list */ 277 if (!attr->attr.name) 278 break; 279 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr); 280 if (attr->free) 281 attr->free(mod); 282 } 283 kfree(mod->modinfo_attrs); 284 } 285 286 static int module_add_modinfo_attrs(struct module *mod) 287 { 288 const struct module_attribute *attr; 289 struct module_attribute *temp_attr; 290 int error = 0; 291 int i; 292 293 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * 294 (modinfo_attrs_count + 1)), 295 GFP_KERNEL); 296 if (!mod->modinfo_attrs) 297 return -ENOMEM; 298 299 temp_attr = mod->modinfo_attrs; 300 for (i = 0; (attr = modinfo_attrs[i]); i++) { 301 if (!attr->test || attr->test(mod)) { 302 memcpy(temp_attr, attr, sizeof(*temp_attr)); 303 sysfs_attr_init(&temp_attr->attr); 304 error = sysfs_create_file(&mod->mkobj.kobj, 305 &temp_attr->attr); 306 if (error) 307 goto error_out; 308 ++temp_attr; 309 } 310 } 311 312 return 0; 313 314 error_out: 315 if (i > 0) 316 module_remove_modinfo_attrs(mod, --i); 317 else 318 kfree(mod->modinfo_attrs); 319 return error; 320 } 321 322 static void mod_kobject_put(struct module *mod) 323 { 324 DECLARE_COMPLETION_ONSTACK(c); 325 326 mod->mkobj.kobj_completion = &c; 327 kobject_put(&mod->mkobj.kobj); 328 wait_for_completion(&c); 329 } 330 331 static int mod_sysfs_init(struct module *mod) 332 { 333 int err; 334 struct kobject *kobj; 335 336 if (!module_kset) { 337 pr_err("%s: module sysfs not initialized\n", mod->name); 338 err = -EINVAL; 339 goto out; 340 } 341 342 kobj = kset_find_obj(module_kset, mod->name); 343 if (kobj) { 344 pr_err("%s: module is already loaded\n", mod->name); 345 kobject_put(kobj); 346 err = -EINVAL; 347 goto out; 348 } 349 350 mod->mkobj.mod = mod; 351 352 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); 353 mod->mkobj.kobj.kset = module_kset; 354 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, 355 "%s", mod->name); 356 if (err) 357 mod_kobject_put(mod); 358 359 out: 360 return err; 361 } 362 363 int mod_sysfs_setup(struct module *mod, 364 const struct load_info *info, 365 struct kernel_param *kparam, 366 unsigned int num_params) 367 { 368 int err; 369 370 err = mod_sysfs_init(mod); 371 if (err) 372 goto out; 373 374 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); 375 if (!mod->holders_dir) { 376 err = -ENOMEM; 377 goto out_unreg; 378 } 379 380 err = module_param_sysfs_setup(mod, kparam, num_params); 381 if (err) 382 goto out_unreg_holders; 383 384 err = module_add_modinfo_attrs(mod); 385 if (err) 386 goto out_unreg_param; 387 388 err = add_usage_links(mod); 389 if (err) 390 goto out_unreg_modinfo_attrs; 391 392 err = add_sect_attrs(mod, info); 393 if (err) 394 goto out_del_usage_links; 395 396 err = add_notes_attrs(mod, info); 397 if (err) 398 goto out_unreg_sect_attrs; 399 400 return 0; 401 402 out_unreg_sect_attrs: 403 remove_sect_attrs(mod); 404 out_del_usage_links: 405 del_usage_links(mod); 406 out_unreg_modinfo_attrs: 407 module_remove_modinfo_attrs(mod, -1); 408 out_unreg_param: 409 module_param_sysfs_remove(mod); 410 out_unreg_holders: 411 kobject_put(mod->holders_dir); 412 out_unreg: 413 mod_kobject_put(mod); 414 out: 415 return err; 416 } 417 418 static void mod_sysfs_fini(struct module *mod) 419 { 420 remove_notes_attrs(mod); 421 remove_sect_attrs(mod); 422 mod_kobject_put(mod); 423 } 424 425 void mod_sysfs_teardown(struct module *mod) 426 { 427 del_usage_links(mod); 428 module_remove_modinfo_attrs(mod, -1); 429 module_param_sysfs_remove(mod); 430 kobject_put(mod->mkobj.drivers_dir); 431 kobject_put(mod->holders_dir); 432 mod_sysfs_fini(mod); 433 } 434 435 void init_param_lock(struct module *mod) 436 { 437 mutex_init(&mod->param_lock); 438 } 439