diff options
Diffstat (limited to 'lib/kobject_uevent.c')
| -rw-r--r-- | lib/kobject_uevent.c | 93 | 
1 files changed, 70 insertions, 23 deletions
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c index 70af0a7f97c..9ebf9e20de5 100644 --- a/lib/kobject_uevent.c +++ b/lib/kobject_uevent.c @@ -17,7 +17,8 @@  #include <linux/spinlock.h>  #include <linux/string.h>  #include <linux/kobject.h> -#include <linux/module.h> +#include <linux/export.h> +#include <linux/kmod.h>  #include <linux/slab.h>  #include <linux/user_namespace.h>  #include <linux/socket.h> @@ -28,17 +29,20 @@  u64 uevent_seqnum; +#ifdef CONFIG_UEVENT_HELPER  char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH; -static DEFINE_SPINLOCK(sequence_lock); +#endif  #ifdef CONFIG_NET  struct uevent_sock {  	struct list_head list;  	struct sock *sk;  };  static LIST_HEAD(uevent_sock_list); -static DEFINE_MUTEX(uevent_sock_mutex);  #endif +/* This lock protects uevent_seqnum and uevent_sock_list */ +static DEFINE_MUTEX(uevent_sock_mutex); +  /* the strings here must match the enum in include/linux/kobject.h */  static const char *kobject_actions[] = {  	[KOBJ_ADD] =		"add", @@ -86,11 +90,17 @@ out:  #ifdef CONFIG_NET  static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)  { -	struct kobject *kobj = data; +	struct kobject *kobj = data, *ksobj;  	const struct kobj_ns_type_operations *ops;  	ops = kobj_ns_ops(kobj); -	if (ops) { +	if (!ops && kobj->kset) { +		ksobj = &kobj->kset->kobj; +		if (ksobj->parent != NULL) +			ops = kobj_ns_ops(ksobj->parent); +	} + +	if (ops && ops->netlink_ns && kobj->ktype->namespace) {  		const void *sock_ns, *ns;  		ns = kobj->ktype->namespace(kobj);  		sock_ns = ops->netlink_ns(dsk); @@ -101,6 +111,7 @@ static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)  }  #endif +#ifdef CONFIG_UEVENT_HELPER  static int kobj_usermode_filter(struct kobject *kobj)  {  	const struct kobj_ns_type_operations *ops; @@ -116,6 +127,31 @@ static int kobj_usermode_filter(struct kobject *kobj)  	return 0;  } +static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem) +{ +	int len; + +	len = strlcpy(&env->buf[env->buflen], subsystem, +		      sizeof(env->buf) - env->buflen); +	if (len >= (sizeof(env->buf) - env->buflen)) { +		WARN(1, KERN_ERR "init_uevent_argv: buffer size too small\n"); +		return -ENOMEM; +	} + +	env->argv[0] = uevent_helper; +	env->argv[1] = &env->buf[env->buflen]; +	env->argv[2] = NULL; + +	env->buflen += len + 1; +	return 0; +} + +static void cleanup_uevent_env(struct subprocess_info *info) +{ +	kfree(info->data); +} +#endif +  /**   * kobject_uevent_env - send an uevent with environmental data   * @@ -136,7 +172,6 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,  	struct kobject *top_kobj;  	struct kset *kset;  	const struct kset_uevent_ops *uevent_ops; -	u64 seq;  	int i = 0;  	int retval = 0;  #ifdef CONFIG_NET @@ -243,22 +278,24 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,  	else if (action == KOBJ_REMOVE)  		kobj->state_remove_uevent_sent = 1; +	mutex_lock(&uevent_sock_mutex);  	/* we will send an event, so request a new sequence number */ -	spin_lock(&sequence_lock); -	seq = ++uevent_seqnum; -	spin_unlock(&sequence_lock); -	retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq); -	if (retval) +	retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum); +	if (retval) { +		mutex_unlock(&uevent_sock_mutex);  		goto exit; +	}  #if defined(CONFIG_NET)  	/* send netlink message */ -	mutex_lock(&uevent_sock_mutex);  	list_for_each_entry(ue_sk, &uevent_sock_list, list) {  		struct sock *uevent_sock = ue_sk->sk;  		struct sk_buff *skb;  		size_t len; +		if (!netlink_has_listeners(uevent_sock, 1)) +			continue; +  		/* allocate message with the maximum possible size */  		len = strlen(action_string) + strlen(devpath) + 2;  		skb = alloc_skb(len + env->buflen, GFP_KERNEL); @@ -282,21 +319,19 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,  							    kobj_bcast_filter,  							    kobj);  			/* ENOBUFS should be handled in userspace */ -			if (retval == -ENOBUFS) +			if (retval == -ENOBUFS || retval == -ESRCH)  				retval = 0;  		} else  			retval = -ENOMEM;  	} -	mutex_unlock(&uevent_sock_mutex);  #endif +	mutex_unlock(&uevent_sock_mutex); +#ifdef CONFIG_UEVENT_HELPER  	/* call uevent_helper, usually only enabled during early boot */  	if (uevent_helper[0] && !kobj_usermode_filter(kobj)) { -		char *argv [3]; +		struct subprocess_info *info; -		argv [0] = uevent_helper; -		argv [1] = (char *)subsystem; -		argv [2] = NULL;  		retval = add_uevent_var(env, "HOME=/");  		if (retval)  			goto exit; @@ -304,10 +339,20 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,  					"PATH=/sbin:/bin:/usr/sbin:/usr/bin");  		if (retval)  			goto exit; +		retval = init_uevent_argv(env, subsystem); +		if (retval) +			goto exit; -		retval = call_usermodehelper(argv[0], argv, -					     env->envp, UMH_WAIT_EXEC); +		retval = -ENOMEM; +		info = call_usermodehelper_setup(env->argv[0], env->argv, +						 env->envp, GFP_KERNEL, +						 NULL, cleanup_uevent_env, env); +		if (info) { +			retval = call_usermodehelper_exec(info, UMH_NO_WAIT); +			env = NULL;	/* freed by cleanup_uevent_env */ +		}  	} +#endif  exit:  	kfree(devpath); @@ -370,13 +415,16 @@ EXPORT_SYMBOL_GPL(add_uevent_var);  static int uevent_net_init(struct net *net)  {  	struct uevent_sock *ue_sk; +	struct netlink_kernel_cfg cfg = { +		.groups	= 1, +		.flags	= NL_CFG_F_NONROOT_RECV, +	};  	ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);  	if (!ue_sk)  		return -ENOMEM; -	ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, -					  1, NULL, NULL, THIS_MODULE); +	ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);  	if (!ue_sk->sk) {  		printk(KERN_ERR  		       "kobject_uevent: unable to create netlink socket!\n"); @@ -416,7 +464,6 @@ static struct pernet_operations uevent_net_ops = {  static int __init kobject_uevent_init(void)  { -	netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);  	return register_pernet_subsys(&uevent_net_ops);  }  | 
