4590 lines · 117.2 KB · C
RawDownload
	module_arch_freeing_init(mod);	module_memfree(mod->init_layout.base);	module_memfree(mod->core_layout.base);} int __weak module_finalize(const Elf_Ehdr *hdr,			   const Elf_Shdr *sechdrs,			   struct module *me){	return 0;} static int post_relocation(struct module *mod, const struct load_info *info){	/* Sort exception table now relocations are done. */	sort_extable(mod->extable, mod->extable + mod->num_exentries); 	/* Copy relocated percpu area over. */	percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,		       info->sechdrs[info->index.pcpu].sh_size); 	/* Setup kallsyms-specific fields. */	add_kallsyms(mod, info); 	/* Arch-specific module finalizing. */	return module_finalize(info->hdr, info->sechdrs, mod);} /* Is this module of this name done loading?  No locks held. */static bool finished_loading(const char *name){	struct module *mod;	bool ret; 	/*	 * The module_mutex should not be a heavily contended lock;	 * if we get the occasional sleep here, we'll go an extra iteration	 * in the wait_event_interruptible(), which is harmless.	 */	sched_annotate_sleep();	mutex_lock(&module_mutex);	mod = find_module_all(name, strlen(name), true);	ret = !mod || mod->state == MODULE_STATE_LIVE;	mutex_unlock(&module_mutex); 	return ret;} /* Call module constructors. */static void do_mod_ctors(struct module *mod){#ifdef CONFIG_CONSTRUCTORS	unsigned long i; 	for (i = 0; i < mod->num_ctors; i++)		mod->ctors[i]();#endif} /* For freeing module_init on success, in case kallsyms traversing */struct mod_initfree {	struct llist_node node;	void *module_init;}; static void do_free_init(struct work_struct *w){	struct llist_node *pos, *n, *list;	struct mod_initfree *initfree; 	list = llist_del_all(&init_free_list); 	synchronize_rcu(); 	llist_for_each_safe(pos, n, list) {		initfree = container_of(pos, struct mod_initfree, node);		module_memfree(initfree->module_init);		kfree(initfree);	}} static int __init modules_wq_init(void){	INIT_WORK(&init_free_wq, do_free_init);	init_llist_head(&init_free_list);	return 0;}module_init(modules_wq_init); /* * This is where the real work happens. * * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb * helper command 'lx-symbols'. */static noinline int do_init_module(struct module *mod){	int ret = 0;	struct mod_initfree *freeinit; 	freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);	if (!freeinit) {		ret = -ENOMEM;		goto fail;	}	freeinit->module_init = mod->init_layout.base; 	/*	 * We want to find out whether @mod uses async during init.  Clear	 * PF_USED_ASYNC.  async_schedule*() will set it.	 */	current->flags &= ~PF_USED_ASYNC; 	do_mod_ctors(mod);	/* Start the module */	if (mod->init != NULL)		ret = do_one_initcall(mod->init);	if (ret < 0) {		goto fail_free_freeinit;	}	if (ret > 0) {		pr_warn("%s: '%s'->init suspiciously returned %d, it should "			"follow 0/-E convention\n"			"%s: loading module anyway...\n",			__func__, mod->name, ret, __func__);		dump_stack();	} 	/* Now it's a first class citizen! */	mod->state = MODULE_STATE_LIVE;	blocking_notifier_call_chain(&module_notify_list,				     MODULE_STATE_LIVE, mod); 	/*	 * We need to finish all async code before the module init sequence	 * is done.  This has potential to deadlock.  For example, a newly	 * detected block device can trigger request_module() of the	 * default iosched from async probing task.  Once userland helper	 * reaches here, async_synchronize_full() will wait on the async	 * task waiting on request_module() and deadlock.	 *	 * This deadlock is avoided by perfomring async_synchronize_full()	 * iff module init queued any async jobs.  This isn't a full	 * solution as it will deadlock the same if module loading from	 * async jobs nests more than once; however, due to the various	 * constraints, this hack seems to be the best option for now.	 * Please refer to the following thread for details.	 *	 * http://thread.gmane.org/gmane.linux.kernel/1420814	 */	if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))		async_synchronize_full(); 	ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +			mod->init_layout.size);	mutex_lock(&module_mutex);	/* Drop initial reference. */	module_put(mod);	trim_init_extable(mod);#ifdef CONFIG_KALLSYMS	/* Switch to core kallsyms now init is done: kallsyms may be walking! */	rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);#endif	module_enable_ro(mod, true);	mod_tree_remove_init(mod);	module_arch_freeing_init(mod);	mod->init_layout.base = NULL;	mod->init_layout.size = 0;	mod->init_layout.ro_size = 0;	mod->init_layout.ro_after_init_size = 0;	mod->init_layout.text_size = 0;	/*	 * We want to free module_init, but be aware that kallsyms may be	 * walking this with preempt disabled.  In all the failure paths, we	 * call synchronize_rcu(), but we don't want to slow down the success	 * path. module_memfree() cannot be called in an interrupt, so do the	 * work and call synchronize_rcu() in a work queue.	 *	 * Note that module_alloc() on most architectures creates W+X page	 * mappings which won't be cleaned up until do_free_init() runs.  Any	 * code such as mark_rodata_ro() which depends on those mappings to	 * be cleaned up needs to sync with the queued work - ie	 * rcu_barrier()	 */	if (llist_add(&freeinit->node, &init_free_list))		schedule_work(&init_free_wq); 	mutex_unlock(&module_mutex);	wake_up_all(&module_wq); 	return 0; fail_free_freeinit:	kfree(freeinit);fail:	/* Try to protect us from buggy refcounters. */	mod->state = MODULE_STATE_GOING;	synchronize_rcu();	module_put(mod);	blocking_notifier_call_chain(&module_notify_list,				     MODULE_STATE_GOING, mod);	klp_module_going(mod);	ftrace_release_mod(mod);	free_module(mod);	wake_up_all(&module_wq);	return ret;} static int may_init_module(void){	if (!capable(CAP_SYS_MODULE) || modules_disabled)		return -EPERM; 	return 0;} /* * We try to place it in the list now to make sure it's unique before * we dedicate too many resources.  In particular, temporary percpu * memory exhaustion. */static int add_unformed_module(struct module *mod){	int err;	struct module *old; 	mod->state = MODULE_STATE_UNFORMED; again:	mutex_lock(&module_mutex);	old = find_module_all(mod->name, strlen(mod->name), true);	if (old != NULL) {		if (old->state != MODULE_STATE_LIVE) {			/* Wait in case it fails to load. */			mutex_unlock(&module_mutex);			err = wait_event_interruptible(module_wq,					       finished_loading(mod->name));			if (err)				goto out_unlocked;			goto again;		}		err = -EEXIST;		goto out;	}	mod_update_bounds(mod);	list_add_rcu(&mod->list, &modules);	mod_tree_insert(mod);	err = 0; out:	mutex_unlock(&module_mutex);out_unlocked:	return err;} static int complete_formation(struct module *mod, struct load_info *info){	int err; 	mutex_lock(&module_mutex); 	/* Find duplicate symbols (must be called under lock). */	err = verify_exported_symbols(mod);	if (err < 0)		goto out; 	/* This relies on module_mutex for list integrity. */	module_bug_finalize(info->hdr, info->sechdrs, mod); 	module_enable_ro(mod, false);	module_enable_nx(mod);	module_enable_x(mod); 	/* Mark state as coming so strong_try_module_get() ignores us,	 * but kallsyms etc. can see us. */	mod->state = MODULE_STATE_COMING;	mutex_unlock(&module_mutex); 	return 0; out:	mutex_unlock(&module_mutex);	return err;} static int prepare_coming_module(struct module *mod){	int err; 	ftrace_module_enable(mod);	err = klp_module_coming(mod);	if (err)		return err; 	blocking_notifier_call_chain(&module_notify_list,				     MODULE_STATE_COMING, mod);	return 0;} static int unknown_module_param_cb(char *param, char *val, const char *modname,				   void *arg){	struct module *mod = arg;	int ret; 	if (strcmp(param, "async_probe") == 0) {		mod->async_probe_requested = true;		return 0;	} 	/* Check for magic 'dyndbg' arg */	ret = ddebug_dyndbg_module_param_cb(param, val, modname);	if (ret != 0)		pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);	return 0;} /* Allocate and load the module: note that size of section 0 is always   zero, and we rely on this for optional sections. */static int load_module(struct load_info *info, const char __user *uargs,		       int flags){	struct module *mod;	long err = 0;	char *after_dashes; 	err = elf_header_check(info);	if (err)		goto free_copy; 	err = setup_load_info(info, flags);	if (err)		goto free_copy; 	if (blacklisted(info->name)) {		err = -EPERM;		goto free_copy;	} 	err = module_sig_check(info, flags);	if (err)		goto free_copy; 	err = rewrite_section_headers(info, flags);	if (err)		goto free_copy; 	/* Check module struct version now, before we try to use module. */	if (!check_modstruct_version(info, info->mod)) {		err = -ENOEXEC;		goto free_copy;	} 	/* Figure out module layout, and allocate all the memory. */	mod = layout_and_allocate(info, flags);	if (IS_ERR(mod)) {		err = PTR_ERR(mod);		goto free_copy;	} 	audit_log_kern_module(mod->name); 	/* Reserve our place in the list. */	err = add_unformed_module(mod);	if (err)		goto free_module; #ifdef CONFIG_MODULE_SIG	mod->sig_ok = info->sig_ok;	if (!mod->sig_ok) {		pr_notice_once("%s: module verification failed: signature "			       "and/or required key missing - tainting "			       "kernel\n", mod->name);		add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);	}#endif 	/* To avoid stressing percpu allocator, do this once we're unique. */	err = percpu_modalloc(mod, info);	if (err)		goto unlink_mod; 	/* Now module is in final location, initialize linked lists, etc. */	err = module_unload_init(mod);	if (err)		goto unlink_mod; 	init_param_lock(mod); 	/* Now we've got everything in the final locations, we can	 * find optional sections. */	err = find_module_sections(mod, info);	if (err)		goto free_unload; 	err = check_module_license_and_versions(mod);	if (err)		goto free_unload; 	/* Set up MODINFO_ATTR fields */	setup_modinfo(mod, info); 	/* Fix up syms, so that st_value is a pointer to location. */	err = simplify_symbols(mod, info);	if (err < 0)		goto free_modinfo; 	err = apply_relocations(mod, info);	if (err < 0)		goto free_modinfo; 	err = post_relocation(mod, info);	if (err < 0)		goto free_modinfo; 	flush_module_icache(mod); 	/* Now copy in args */	mod->args = strndup_user(uargs, ~0UL >> 1);	if (IS_ERR(mod->args)) {		err = PTR_ERR(mod->args);		goto free_arch_cleanup;	} 	dynamic_debug_setup(mod, info->debug, info->num_debug); 	/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */	ftrace_module_init(mod); 	/* Finally it's fully formed, ready to start executing. */	err = complete_formation(mod, info);	if (err)		goto ddebug_cleanup; 	err = prepare_coming_module(mod);	if (err)		goto bug_cleanup; 	/* Module is ready to execute: parsing args may do that. */	after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,				  -32768, 32767, mod,				  unknown_module_param_cb);	if (IS_ERR(after_dashes)) {		err = PTR_ERR(after_dashes);		goto coming_cleanup;	} else if (after_dashes) {		pr_warn("%s: parameters '%s' after `--' ignored\n",		       mod->name, after_dashes);	} 	/* Link in to sysfs. */	err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);	if (err < 0)		goto coming_cleanup; 	if (is_livepatch_module(mod)) {		err = copy_module_elf(mod, info);		if (err < 0)			goto sysfs_cleanup;	} 	/* Get rid of temporary copy. */	free_copy(info); 	/* Done! */	trace_module_load(mod); 	return do_init_module(mod);  sysfs_cleanup:	mod_sysfs_teardown(mod); coming_cleanup:	mod->state = MODULE_STATE_GOING;	destroy_params(mod->kp, mod->num_kp);	blocking_notifier_call_chain(&module_notify_list,				     MODULE_STATE_GOING, mod);	klp_module_going(mod); bug_cleanup:	/* module_bug_cleanup needs module_mutex protection */	mutex_lock(&module_mutex);	module_bug_cleanup(mod);	mutex_unlock(&module_mutex);  ddebug_cleanup:	ftrace_release_mod(mod);	dynamic_debug_remove(mod, info->debug);	synchronize_rcu();	kfree(mod->args); free_arch_cleanup:	module_arch_cleanup(mod); free_modinfo:	free_modinfo(mod); free_unload:	module_unload_free(mod); unlink_mod:	mutex_lock(&module_mutex);	/* Unlink carefully: kallsyms could be walking list. */	list_del_rcu(&mod->list);	mod_tree_remove(mod);	wake_up_all(&module_wq);

Lines 3501–4000 of 4590. Previous lines