1d9d16e16SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * kernel userspace event delivery
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
61da177e4SLinus Torvalds * Copyright (C) 2004 Novell, Inc. All rights reserved.
71da177e4SLinus Torvalds * Copyright (C) 2004 IBM, Inc. All rights reserved.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Authors:
101da177e4SLinus Torvalds * Robert Love <rml@novell.com>
111da177e4SLinus Torvalds * Kay Sievers <kay.sievers@vrfy.org>
121da177e4SLinus Torvalds * Arjan van de Ven <arjanv@redhat.com>
131da177e4SLinus Torvalds * Greg Kroah-Hartman <greg@kroah.com>
141da177e4SLinus Torvalds */
151da177e4SLinus Torvalds
161da177e4SLinus Torvalds #include <linux/spinlock.h>
172d38f9a4SDenis V. Lunev #include <linux/string.h>
182d38f9a4SDenis V. Lunev #include <linux/kobject.h>
198bc3bcc9SPaul Gortmaker #include <linux/export.h>
208bc3bcc9SPaul Gortmaker #include <linux/kmod.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/socket.h>
231da177e4SLinus Torvalds #include <linux/skbuff.h>
241da177e4SLinus Torvalds #include <linux/netlink.h>
2526045a7bSChristian Brauner #include <linux/uidgid.h>
26f36776faSPeter Rajnoha #include <linux/uuid.h>
27f36776faSPeter Rajnoha #include <linux/ctype.h>
281da177e4SLinus Torvalds #include <net/sock.h>
29692ec06dSChristian Brauner #include <net/netlink.h>
3007e98962SEric W. Biederman #include <net/net_namespace.h>
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds
332444a80cSEric Dumazet atomic64_t uevent_seqnum;
3486d56134SMichael Marineau #ifdef CONFIG_UEVENT_HELPER
356a8d8abbSKay Sievers char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
3686d56134SMichael Marineau #endif
3794e5e308SChristian Brauner
3807e98962SEric W. Biederman struct uevent_sock {
3907e98962SEric W. Biederman struct list_head list;
4007e98962SEric W. Biederman struct sock *sk;
4107e98962SEric W. Biederman };
4294e5e308SChristian Brauner
4394e5e308SChristian Brauner #ifdef CONFIG_NET
4407e98962SEric W. Biederman static LIST_HEAD(uevent_sock_list);
452444a80cSEric Dumazet /* This lock protects uevent_sock_list */
467b60a18dSAndrew Vagin static DEFINE_MUTEX(uevent_sock_mutex);
475c0941c5SEric Dumazet #endif
487b60a18dSAndrew Vagin
495c5daf65SKay Sievers /* the strings here must match the enum in include/linux/kobject.h */
505c5daf65SKay Sievers static const char *kobject_actions[] = {
515c5daf65SKay Sievers [KOBJ_ADD] = "add",
525c5daf65SKay Sievers [KOBJ_REMOVE] = "remove",
535c5daf65SKay Sievers [KOBJ_CHANGE] = "change",
545c5daf65SKay Sievers [KOBJ_MOVE] = "move",
555c5daf65SKay Sievers [KOBJ_ONLINE] = "online",
565c5daf65SKay Sievers [KOBJ_OFFLINE] = "offline",
571455cf8dSDmitry Torokhov [KOBJ_BIND] = "bind",
581455cf8dSDmitry Torokhov [KOBJ_UNBIND] = "unbind",
595c5daf65SKay Sievers };
605c5daf65SKay Sievers
kobject_action_type(const char * buf,size_t count,enum kobject_action * type,const char ** args)61f36776faSPeter Rajnoha static int kobject_action_type(const char *buf, size_t count,
62f36776faSPeter Rajnoha enum kobject_action *type,
63f36776faSPeter Rajnoha const char **args)
645c5daf65SKay Sievers {
655c5daf65SKay Sievers enum kobject_action action;
66f36776faSPeter Rajnoha size_t count_first;
67f36776faSPeter Rajnoha const char *args_start;
685c5daf65SKay Sievers int ret = -EINVAL;
695c5daf65SKay Sievers
70a9edadbfSMark Lord if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
715c5daf65SKay Sievers count--;
725c5daf65SKay Sievers
735c5daf65SKay Sievers if (!count)
745c5daf65SKay Sievers goto out;
755c5daf65SKay Sievers
76f36776faSPeter Rajnoha args_start = strnchr(buf, count, ' ');
77f36776faSPeter Rajnoha if (args_start) {
78f36776faSPeter Rajnoha count_first = args_start - buf;
79f36776faSPeter Rajnoha args_start = args_start + 1;
80f36776faSPeter Rajnoha } else
81f36776faSPeter Rajnoha count_first = count;
82f36776faSPeter Rajnoha
835c5daf65SKay Sievers for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
84f36776faSPeter Rajnoha if (strncmp(kobject_actions[action], buf, count_first) != 0)
855c5daf65SKay Sievers continue;
86f36776faSPeter Rajnoha if (kobject_actions[action][count_first] != '\0')
875c5daf65SKay Sievers continue;
88f36776faSPeter Rajnoha if (args)
89f36776faSPeter Rajnoha *args = args_start;
905c5daf65SKay Sievers *type = action;
915c5daf65SKay Sievers ret = 0;
925c5daf65SKay Sievers break;
935c5daf65SKay Sievers }
945c5daf65SKay Sievers out:
955c5daf65SKay Sievers return ret;
965c5daf65SKay Sievers }
975c5daf65SKay Sievers
action_arg_word_end(const char * buf,const char * buf_end,char delim)98f36776faSPeter Rajnoha static const char *action_arg_word_end(const char *buf, const char *buf_end,
99f36776faSPeter Rajnoha char delim)
100f36776faSPeter Rajnoha {
101f36776faSPeter Rajnoha const char *next = buf;
102f36776faSPeter Rajnoha
103f36776faSPeter Rajnoha while (next <= buf_end && *next != delim)
104f36776faSPeter Rajnoha if (!isalnum(*next++))
105f36776faSPeter Rajnoha return NULL;
106f36776faSPeter Rajnoha
107f36776faSPeter Rajnoha if (next == buf)
108f36776faSPeter Rajnoha return NULL;
109f36776faSPeter Rajnoha
110f36776faSPeter Rajnoha return next;
111f36776faSPeter Rajnoha }
112f36776faSPeter Rajnoha
kobject_action_args(const char * buf,size_t count,struct kobj_uevent_env ** ret_env)113f36776faSPeter Rajnoha static int kobject_action_args(const char *buf, size_t count,
114f36776faSPeter Rajnoha struct kobj_uevent_env **ret_env)
115f36776faSPeter Rajnoha {
116f36776faSPeter Rajnoha struct kobj_uevent_env *env = NULL;
117f36776faSPeter Rajnoha const char *next, *buf_end, *key;
118f36776faSPeter Rajnoha int key_len;
119f36776faSPeter Rajnoha int r = -EINVAL;
120f36776faSPeter Rajnoha
121f36776faSPeter Rajnoha if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
122f36776faSPeter Rajnoha count--;
123f36776faSPeter Rajnoha
124f36776faSPeter Rajnoha if (!count)
125f36776faSPeter Rajnoha return -EINVAL;
126f36776faSPeter Rajnoha
127f36776faSPeter Rajnoha env = kzalloc(sizeof(*env), GFP_KERNEL);
128f36776faSPeter Rajnoha if (!env)
129f36776faSPeter Rajnoha return -ENOMEM;
130f36776faSPeter Rajnoha
131f36776faSPeter Rajnoha /* first arg is UUID */
132f36776faSPeter Rajnoha if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
133f36776faSPeter Rajnoha add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
134f36776faSPeter Rajnoha goto out;
135f36776faSPeter Rajnoha
136f36776faSPeter Rajnoha /*
137f36776faSPeter Rajnoha * the rest are custom environment variables in KEY=VALUE
138f36776faSPeter Rajnoha * format with ' ' delimiter between each KEY=VALUE pair
139f36776faSPeter Rajnoha */
140f36776faSPeter Rajnoha next = buf + UUID_STRING_LEN;
141f36776faSPeter Rajnoha buf_end = buf + count - 1;
142f36776faSPeter Rajnoha
143f36776faSPeter Rajnoha while (next <= buf_end) {
144f36776faSPeter Rajnoha if (*next != ' ')
145f36776faSPeter Rajnoha goto out;
146f36776faSPeter Rajnoha
147f36776faSPeter Rajnoha /* skip the ' ', key must follow */
148f36776faSPeter Rajnoha key = ++next;
149f36776faSPeter Rajnoha if (key > buf_end)
150f36776faSPeter Rajnoha goto out;
151f36776faSPeter Rajnoha
152f36776faSPeter Rajnoha buf = next;
153f36776faSPeter Rajnoha next = action_arg_word_end(buf, buf_end, '=');
154f36776faSPeter Rajnoha if (!next || next > buf_end || *next != '=')
155f36776faSPeter Rajnoha goto out;
156f36776faSPeter Rajnoha key_len = next - buf;
157f36776faSPeter Rajnoha
158f36776faSPeter Rajnoha /* skip the '=', value must follow */
159f36776faSPeter Rajnoha if (++next > buf_end)
160f36776faSPeter Rajnoha goto out;
161f36776faSPeter Rajnoha
162f36776faSPeter Rajnoha buf = next;
163f36776faSPeter Rajnoha next = action_arg_word_end(buf, buf_end, ' ');
164f36776faSPeter Rajnoha if (!next)
165f36776faSPeter Rajnoha goto out;
166f36776faSPeter Rajnoha
167f36776faSPeter Rajnoha if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
168f36776faSPeter Rajnoha key_len, key, (int) (next - buf), buf))
169f36776faSPeter Rajnoha goto out;
170f36776faSPeter Rajnoha }
171f36776faSPeter Rajnoha
172f36776faSPeter Rajnoha r = 0;
173f36776faSPeter Rajnoha out:
174f36776faSPeter Rajnoha if (r)
175f36776faSPeter Rajnoha kfree(env);
176f36776faSPeter Rajnoha else
177f36776faSPeter Rajnoha *ret_env = env;
178f36776faSPeter Rajnoha return r;
179f36776faSPeter Rajnoha }
180f36776faSPeter Rajnoha
181f36776faSPeter Rajnoha /**
182f36776faSPeter Rajnoha * kobject_synth_uevent - send synthetic uevent with arguments
183f36776faSPeter Rajnoha *
184f36776faSPeter Rajnoha * @kobj: struct kobject for which synthetic uevent is to be generated
185f36776faSPeter Rajnoha * @buf: buffer containing action type and action args, newline is ignored
186f36776faSPeter Rajnoha * @count: length of buffer
187f36776faSPeter Rajnoha *
188f36776faSPeter Rajnoha * Returns 0 if kobject_synthetic_uevent() is completed with success or the
189f36776faSPeter Rajnoha * corresponding error when it fails.
190f36776faSPeter Rajnoha */
kobject_synth_uevent(struct kobject * kobj,const char * buf,size_t count)191f36776faSPeter Rajnoha int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
192f36776faSPeter Rajnoha {
193f36776faSPeter Rajnoha char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
194f36776faSPeter Rajnoha enum kobject_action action;
195f36776faSPeter Rajnoha const char *action_args;
196f36776faSPeter Rajnoha struct kobj_uevent_env *env;
197f36776faSPeter Rajnoha const char *msg = NULL, *devpath;
198f36776faSPeter Rajnoha int r;
199f36776faSPeter Rajnoha
200f36776faSPeter Rajnoha r = kobject_action_type(buf, count, &action, &action_args);
201f36776faSPeter Rajnoha if (r) {
202549ad243SBo YU msg = "unknown uevent action string";
203f36776faSPeter Rajnoha goto out;
204f36776faSPeter Rajnoha }
205f36776faSPeter Rajnoha
206f36776faSPeter Rajnoha if (!action_args) {
207f36776faSPeter Rajnoha r = kobject_uevent_env(kobj, action, no_uuid_envp);
208f36776faSPeter Rajnoha goto out;
209f36776faSPeter Rajnoha }
210f36776faSPeter Rajnoha
211f36776faSPeter Rajnoha r = kobject_action_args(action_args,
212f36776faSPeter Rajnoha count - (action_args - buf), &env);
213f36776faSPeter Rajnoha if (r == -EINVAL) {
214549ad243SBo YU msg = "incorrect uevent action arguments";
215f36776faSPeter Rajnoha goto out;
216f36776faSPeter Rajnoha }
217f36776faSPeter Rajnoha
218f36776faSPeter Rajnoha if (r)
219f36776faSPeter Rajnoha goto out;
220f36776faSPeter Rajnoha
221f36776faSPeter Rajnoha r = kobject_uevent_env(kobj, action, env->envp);
222f36776faSPeter Rajnoha kfree(env);
223f36776faSPeter Rajnoha out:
224f36776faSPeter Rajnoha if (r) {
225f36776faSPeter Rajnoha devpath = kobject_get_path(kobj, GFP_KERNEL);
226549ad243SBo YU pr_warn("synth uevent: %s: %s\n",
227f36776faSPeter Rajnoha devpath ?: "unknown device",
228f36776faSPeter Rajnoha msg ?: "failed to send uevent");
229f36776faSPeter Rajnoha kfree(devpath);
230f36776faSPeter Rajnoha }
231f36776faSPeter Rajnoha return r;
232f36776faSPeter Rajnoha }
233f36776faSPeter Rajnoha
23486d56134SMichael Marineau #ifdef CONFIG_UEVENT_HELPER
kobj_usermode_filter(struct kobject * kobj)235417daa1eSEric W. Biederman static int kobj_usermode_filter(struct kobject *kobj)
236417daa1eSEric W. Biederman {
237417daa1eSEric W. Biederman const struct kobj_ns_type_operations *ops;
238417daa1eSEric W. Biederman
239417daa1eSEric W. Biederman ops = kobj_ns_ops(kobj);
240417daa1eSEric W. Biederman if (ops) {
241417daa1eSEric W. Biederman const void *init_ns, *ns;
2426be244dcSBo YU
243417daa1eSEric W. Biederman ns = kobj->ktype->namespace(kobj);
244417daa1eSEric W. Biederman init_ns = ops->initial_ns();
245417daa1eSEric W. Biederman return ns != init_ns;
246417daa1eSEric W. Biederman }
247417daa1eSEric W. Biederman
248417daa1eSEric W. Biederman return 0;
249417daa1eSEric W. Biederman }
250417daa1eSEric W. Biederman
init_uevent_argv(struct kobj_uevent_env * env,const char * subsystem)251bcccff93SVladimir Davydov static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
252bcccff93SVladimir Davydov {
253b4104180SGreg Kroah-Hartman int buffer_size = sizeof(env->buf) - env->buflen;
254bcccff93SVladimir Davydov int len;
255bcccff93SVladimir Davydov
2566cd59324SAzeem Shaikh len = strscpy(&env->buf[env->buflen], subsystem, buffer_size);
2576cd59324SAzeem Shaikh if (len < 0) {
2586cd59324SAzeem Shaikh pr_warn("%s: insufficient buffer space (%u left) for %s\n",
2596cd59324SAzeem Shaikh __func__, buffer_size, subsystem);
260bcccff93SVladimir Davydov return -ENOMEM;
261bcccff93SVladimir Davydov }
262bcccff93SVladimir Davydov
263bcccff93SVladimir Davydov env->argv[0] = uevent_helper;
264bcccff93SVladimir Davydov env->argv[1] = &env->buf[env->buflen];
265bcccff93SVladimir Davydov env->argv[2] = NULL;
266bcccff93SVladimir Davydov
267bcccff93SVladimir Davydov env->buflen += len + 1;
268bcccff93SVladimir Davydov return 0;
269bcccff93SVladimir Davydov }
270bcccff93SVladimir Davydov
cleanup_uevent_env(struct subprocess_info * info)271bcccff93SVladimir Davydov static void cleanup_uevent_env(struct subprocess_info *info)
272bcccff93SVladimir Davydov {
273bcccff93SVladimir Davydov kfree(info->data);
274bcccff93SVladimir Davydov }
27586d56134SMichael Marineau #endif
276bcccff93SVladimir Davydov
27726045a7bSChristian Brauner #ifdef CONFIG_NET
alloc_uevent_skb(struct kobj_uevent_env * env,const char * action_string,const char * devpath)27826045a7bSChristian Brauner static struct sk_buff *alloc_uevent_skb(struct kobj_uevent_env *env,
27926045a7bSChristian Brauner const char *action_string,
28026045a7bSChristian Brauner const char *devpath)
28126045a7bSChristian Brauner {
28226045a7bSChristian Brauner struct netlink_skb_parms *parms;
28326045a7bSChristian Brauner struct sk_buff *skb = NULL;
28426045a7bSChristian Brauner char *scratch;
28526045a7bSChristian Brauner size_t len;
28626045a7bSChristian Brauner
28726045a7bSChristian Brauner /* allocate message with maximum possible size */
28826045a7bSChristian Brauner len = strlen(action_string) + strlen(devpath) + 2;
28926045a7bSChristian Brauner skb = alloc_skb(len + env->buflen, GFP_KERNEL);
29026045a7bSChristian Brauner if (!skb)
29126045a7bSChristian Brauner return NULL;
29226045a7bSChristian Brauner
29326045a7bSChristian Brauner /* add header */
29426045a7bSChristian Brauner scratch = skb_put(skb, len);
29526045a7bSChristian Brauner sprintf(scratch, "%s@%s", action_string, devpath);
29626045a7bSChristian Brauner
29726045a7bSChristian Brauner skb_put_data(skb, env->buf, env->buflen);
29826045a7bSChristian Brauner
29926045a7bSChristian Brauner parms = &NETLINK_CB(skb);
30026045a7bSChristian Brauner parms->creds.uid = GLOBAL_ROOT_UID;
30126045a7bSChristian Brauner parms->creds.gid = GLOBAL_ROOT_GID;
30226045a7bSChristian Brauner parms->dst_group = 1;
30326045a7bSChristian Brauner parms->portid = 0;
30426045a7bSChristian Brauner
30526045a7bSChristian Brauner return skb;
30626045a7bSChristian Brauner }
30726045a7bSChristian Brauner
uevent_net_broadcast_untagged(struct kobj_uevent_env * env,const char * action_string,const char * devpath)308a3498436SChristian Brauner static int uevent_net_broadcast_untagged(struct kobj_uevent_env *env,
30916dff336SEric Dumazet const char *action_string,
31016dff336SEric Dumazet const char *devpath)
31116dff336SEric Dumazet {
312d464e84eSEric Dumazet struct sk_buff *skb = NULL;
31316dff336SEric Dumazet struct uevent_sock *ue_sk;
314a3498436SChristian Brauner int retval = 0;
31516dff336SEric Dumazet
31616dff336SEric Dumazet /* send netlink message */
3175c0941c5SEric Dumazet mutex_lock(&uevent_sock_mutex);
31816dff336SEric Dumazet list_for_each_entry(ue_sk, &uevent_sock_list, list) {
31916dff336SEric Dumazet struct sock *uevent_sock = ue_sk->sk;
32016dff336SEric Dumazet
32116dff336SEric Dumazet if (!netlink_has_listeners(uevent_sock, 1))
32216dff336SEric Dumazet continue;
32316dff336SEric Dumazet
324d464e84eSEric Dumazet if (!skb) {
325d464e84eSEric Dumazet retval = -ENOMEM;
32626045a7bSChristian Brauner skb = alloc_uevent_skb(env, action_string, devpath);
327d464e84eSEric Dumazet if (!skb)
328d464e84eSEric Dumazet continue;
329d464e84eSEric Dumazet }
330d464e84eSEric Dumazet
331a3498436SChristian Brauner retval = netlink_broadcast(uevent_sock, skb_get(skb), 0, 1,
332a3498436SChristian Brauner GFP_KERNEL);
33316dff336SEric Dumazet /* ENOBUFS should be handled in userspace */
33416dff336SEric Dumazet if (retval == -ENOBUFS || retval == -ESRCH)
33516dff336SEric Dumazet retval = 0;
33616dff336SEric Dumazet }
3375c0941c5SEric Dumazet mutex_unlock(&uevent_sock_mutex);
338d464e84eSEric Dumazet consume_skb(skb);
339a3498436SChristian Brauner
34016dff336SEric Dumazet return retval;
34116dff336SEric Dumazet }
34216dff336SEric Dumazet
uevent_net_broadcast_tagged(struct sock * usk,struct kobj_uevent_env * env,const char * action_string,const char * devpath)343a3498436SChristian Brauner static int uevent_net_broadcast_tagged(struct sock *usk,
344a3498436SChristian Brauner struct kobj_uevent_env *env,
345a3498436SChristian Brauner const char *action_string,
346a3498436SChristian Brauner const char *devpath)
347a3498436SChristian Brauner {
348a3498436SChristian Brauner struct user_namespace *owning_user_ns = sock_net(usk)->user_ns;
349a3498436SChristian Brauner struct sk_buff *skb = NULL;
350a3498436SChristian Brauner int ret = 0;
351a3498436SChristian Brauner
352a3498436SChristian Brauner skb = alloc_uevent_skb(env, action_string, devpath);
353a3498436SChristian Brauner if (!skb)
354a3498436SChristian Brauner return -ENOMEM;
355a3498436SChristian Brauner
356a3498436SChristian Brauner /* fix credentials */
357a3498436SChristian Brauner if (owning_user_ns != &init_user_ns) {
358a3498436SChristian Brauner struct netlink_skb_parms *parms = &NETLINK_CB(skb);
359a3498436SChristian Brauner kuid_t root_uid;
360a3498436SChristian Brauner kgid_t root_gid;
361a3498436SChristian Brauner
362a3498436SChristian Brauner /* fix uid */
363a3498436SChristian Brauner root_uid = make_kuid(owning_user_ns, 0);
364a3498436SChristian Brauner if (uid_valid(root_uid))
365a3498436SChristian Brauner parms->creds.uid = root_uid;
366a3498436SChristian Brauner
367a3498436SChristian Brauner /* fix gid */
368a3498436SChristian Brauner root_gid = make_kgid(owning_user_ns, 0);
369a3498436SChristian Brauner if (gid_valid(root_gid))
370a3498436SChristian Brauner parms->creds.gid = root_gid;
371a3498436SChristian Brauner }
372a3498436SChristian Brauner
373a3498436SChristian Brauner ret = netlink_broadcast(usk, skb, 0, 1, GFP_KERNEL);
374a3498436SChristian Brauner /* ENOBUFS should be handled in userspace */
375a3498436SChristian Brauner if (ret == -ENOBUFS || ret == -ESRCH)
376a3498436SChristian Brauner ret = 0;
377a3498436SChristian Brauner
378a3498436SChristian Brauner return ret;
379a3498436SChristian Brauner }
380a3498436SChristian Brauner #endif
381a3498436SChristian Brauner
kobject_uevent_net_broadcast(struct kobject * kobj,struct kobj_uevent_env * env,const char * action_string,const char * devpath)382a3498436SChristian Brauner static int kobject_uevent_net_broadcast(struct kobject *kobj,
383a3498436SChristian Brauner struct kobj_uevent_env *env,
384a3498436SChristian Brauner const char *action_string,
385a3498436SChristian Brauner const char *devpath)
386a3498436SChristian Brauner {
387a3498436SChristian Brauner int ret = 0;
388a3498436SChristian Brauner
389a3498436SChristian Brauner #ifdef CONFIG_NET
390a3498436SChristian Brauner const struct kobj_ns_type_operations *ops;
391a3498436SChristian Brauner const struct net *net = NULL;
392a3498436SChristian Brauner
393a3498436SChristian Brauner ops = kobj_ns_ops(kobj);
394a3498436SChristian Brauner if (!ops && kobj->kset) {
395a3498436SChristian Brauner struct kobject *ksobj = &kobj->kset->kobj;
3966be244dcSBo YU
397a3498436SChristian Brauner if (ksobj->parent != NULL)
398a3498436SChristian Brauner ops = kobj_ns_ops(ksobj->parent);
399a3498436SChristian Brauner }
400a3498436SChristian Brauner
401a3498436SChristian Brauner /* kobjects currently only carry network namespace tags and they
402a3498436SChristian Brauner * are the only tag relevant here since we want to decide which
403a3498436SChristian Brauner * network namespaces to broadcast the uevent into.
404a3498436SChristian Brauner */
405a3498436SChristian Brauner if (ops && ops->netlink_ns && kobj->ktype->namespace)
406a3498436SChristian Brauner if (ops->type == KOBJ_NS_TYPE_NET)
407a3498436SChristian Brauner net = kobj->ktype->namespace(kobj);
408a3498436SChristian Brauner
409a3498436SChristian Brauner if (!net)
410a3498436SChristian Brauner ret = uevent_net_broadcast_untagged(env, action_string,
411a3498436SChristian Brauner devpath);
412a3498436SChristian Brauner else
413a3498436SChristian Brauner ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
414a3498436SChristian Brauner action_string, devpath);
415a3498436SChristian Brauner #endif
416a3498436SChristian Brauner
417a3498436SChristian Brauner return ret;
418a3498436SChristian Brauner }
419a3498436SChristian Brauner
zap_modalias_env(struct kobj_uevent_env * env)4206878e7deSDmitry Torokhov static void zap_modalias_env(struct kobj_uevent_env *env)
4216878e7deSDmitry Torokhov {
4226878e7deSDmitry Torokhov static const char modalias_prefix[] = "MODALIAS=";
4239b3fa47dSDmitry Torokhov size_t len;
4249b3fa47dSDmitry Torokhov int i, j;
4256878e7deSDmitry Torokhov
4266878e7deSDmitry Torokhov for (i = 0; i < env->envp_idx;) {
4276878e7deSDmitry Torokhov if (strncmp(env->envp[i], modalias_prefix,
4286878e7deSDmitry Torokhov sizeof(modalias_prefix) - 1)) {
4296878e7deSDmitry Torokhov i++;
4306878e7deSDmitry Torokhov continue;
4316878e7deSDmitry Torokhov }
4326878e7deSDmitry Torokhov
4339b3fa47dSDmitry Torokhov len = strlen(env->envp[i]) + 1;
4349b3fa47dSDmitry Torokhov
4359b3fa47dSDmitry Torokhov if (i != env->envp_idx - 1) {
436*dd6e9894SZijun Hu /* @env->envp[] contains pointers to @env->buf[]
437*dd6e9894SZijun Hu * with @env->buflen chars, and we are removing
438*dd6e9894SZijun Hu * variable MODALIAS here pointed by @env->envp[i]
439*dd6e9894SZijun Hu * with length @len as shown below:
440*dd6e9894SZijun Hu *
441*dd6e9894SZijun Hu * 0 @env->buf[] @env->buflen
442*dd6e9894SZijun Hu * ---------------------------------------------
443*dd6e9894SZijun Hu * ^ ^ ^ ^
444*dd6e9894SZijun Hu * | |-> @len <-| target block |
445*dd6e9894SZijun Hu * @env->envp[0] @env->envp[i] @env->envp[i + 1]
446*dd6e9894SZijun Hu *
447*dd6e9894SZijun Hu * so the "target block" indicated above is moved
448*dd6e9894SZijun Hu * backward by @len, and its right size is
449*dd6e9894SZijun Hu * @env->buflen - (@env->envp[i + 1] - @env->envp[0]).
450*dd6e9894SZijun Hu */
4519b3fa47dSDmitry Torokhov memmove(env->envp[i], env->envp[i + 1],
452*dd6e9894SZijun Hu env->buflen - (env->envp[i + 1] - env->envp[0]));
4539b3fa47dSDmitry Torokhov
4549b3fa47dSDmitry Torokhov for (j = i; j < env->envp_idx - 1; j++)
4559b3fa47dSDmitry Torokhov env->envp[j] = env->envp[j + 1] - len;
4569b3fa47dSDmitry Torokhov }
4576878e7deSDmitry Torokhov
4586878e7deSDmitry Torokhov env->envp_idx--;
4599b3fa47dSDmitry Torokhov env->buflen -= len;
4606878e7deSDmitry Torokhov }
4616878e7deSDmitry Torokhov }
4626878e7deSDmitry Torokhov
4631da177e4SLinus Torvalds /**
4648a82472fSCornelia Huck * kobject_uevent_env - send an uevent with environmental data
4651da177e4SLinus Torvalds *
4661da177e4SLinus Torvalds * @kobj: struct kobject that the action is happening to
467edbbf994SJulia Lawall * @action: action that is happening
4688a82472fSCornelia Huck * @envp_ext: pointer to environmental data
469542cfce6SAneesh Kumar K.V *
470f6e6e779SXiaotian Feng * Returns 0 if kobject_uevent_env() is completed with success or the
471542cfce6SAneesh Kumar K.V * corresponding error when it fails.
4721da177e4SLinus Torvalds */
kobject_uevent_env(struct kobject * kobj,enum kobject_action action,char * envp_ext[])473542cfce6SAneesh Kumar K.V int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
4748a82472fSCornelia Huck char *envp_ext[])
4751da177e4SLinus Torvalds {
4767eff2e7aSKay Sievers struct kobj_uevent_env *env;
4777eff2e7aSKay Sievers const char *action_string = kobject_actions[action];
4785f123fbdSKay Sievers const char *devpath = NULL;
4795f123fbdSKay Sievers const char *subsystem;
4805f123fbdSKay Sievers struct kobject *top_kobj;
4815f123fbdSKay Sievers struct kset *kset;
4829cd43611SEmese Revfy const struct kset_uevent_ops *uevent_ops;
4831da177e4SLinus Torvalds int i = 0;
484542cfce6SAneesh Kumar K.V int retval = 0;
4851da177e4SLinus Torvalds
486c03a0fd0STetsuo Handa /*
487c03a0fd0STetsuo Handa * Mark "remove" event done regardless of result, for some subsystems
488c03a0fd0STetsuo Handa * do not want to re-trigger "remove" event via automatic cleanup.
489c03a0fd0STetsuo Handa */
490c03a0fd0STetsuo Handa if (action == KOBJ_REMOVE)
491c03a0fd0STetsuo Handa kobj->state_remove_uevent_sent = 1;
492c03a0fd0STetsuo Handa
4939f66fa2aSGreg Kroah-Hartman pr_debug("kobject: '%s' (%p): %s\n",
494810304dbSHarvey Harrison kobject_name(kobj), kobj, __func__);
4951da177e4SLinus Torvalds
4965f123fbdSKay Sievers /* search the kset we belong to */
4975f123fbdSKay Sievers top_kobj = kobj;
498ccd490a3SKay Sievers while (!top_kobj->kset && top_kobj->parent)
4995f123fbdSKay Sievers top_kobj = top_kobj->parent;
500ccd490a3SKay Sievers
501542cfce6SAneesh Kumar K.V if (!top_kobj->kset) {
5029f66fa2aSGreg Kroah-Hartman pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
5039f66fa2aSGreg Kroah-Hartman "without kset!\n", kobject_name(kobj), kobj,
504810304dbSHarvey Harrison __func__);
505542cfce6SAneesh Kumar K.V return -EINVAL;
506542cfce6SAneesh Kumar K.V }
5075f123fbdSKay Sievers
5085f123fbdSKay Sievers kset = top_kobj->kset;
509312c004dSKay Sievers uevent_ops = kset->uevent_ops;
5105f123fbdSKay Sievers
511f67f129eSMing Lei /* skip the event, if uevent_suppress is set*/
512f67f129eSMing Lei if (kobj->uevent_suppress) {
513f67f129eSMing Lei pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
514f67f129eSMing Lei "caused the event to drop!\n",
515f67f129eSMing Lei kobject_name(kobj), kobj, __func__);
516f67f129eSMing Lei return 0;
517f67f129eSMing Lei }
5185f123fbdSKay Sievers /* skip the event, if the filter returns zero. */
519312c004dSKay Sievers if (uevent_ops && uevent_ops->filter)
520cf6299b6SGreg Kroah-Hartman if (!uevent_ops->filter(kobj)) {
5219f66fa2aSGreg Kroah-Hartman pr_debug("kobject: '%s' (%p): %s: filter function "
5229f66fa2aSGreg Kroah-Hartman "caused the event to drop!\n",
523810304dbSHarvey Harrison kobject_name(kobj), kobj, __func__);
524542cfce6SAneesh Kumar K.V return 0;
525542cfce6SAneesh Kumar K.V }
5265f123fbdSKay Sievers
52786406245SKay Sievers /* originating subsystem */
52886406245SKay Sievers if (uevent_ops && uevent_ops->name)
529cf6299b6SGreg Kroah-Hartman subsystem = uevent_ops->name(kobj);
53086406245SKay Sievers else
53186406245SKay Sievers subsystem = kobject_name(&kset->kobj);
53286406245SKay Sievers if (!subsystem) {
5339f66fa2aSGreg Kroah-Hartman pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
5349f66fa2aSGreg Kroah-Hartman "event to drop!\n", kobject_name(kobj), kobj,
535810304dbSHarvey Harrison __func__);
53686406245SKay Sievers return 0;
53786406245SKay Sievers }
53886406245SKay Sievers
5397eff2e7aSKay Sievers /* environment buffer */
5407eff2e7aSKay Sievers env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
5417eff2e7aSKay Sievers if (!env)
542542cfce6SAneesh Kumar K.V return -ENOMEM;
5431da177e4SLinus Torvalds
5445f123fbdSKay Sievers /* complete object path */
5455f123fbdSKay Sievers devpath = kobject_get_path(kobj, GFP_KERNEL);
546542cfce6SAneesh Kumar K.V if (!devpath) {
547542cfce6SAneesh Kumar K.V retval = -ENOENT;
5485f123fbdSKay Sievers goto exit;
549542cfce6SAneesh Kumar K.V }
5501da177e4SLinus Torvalds
5515f123fbdSKay Sievers /* default keys */
5527eff2e7aSKay Sievers retval = add_uevent_var(env, "ACTION=%s", action_string);
5537eff2e7aSKay Sievers if (retval)
5547eff2e7aSKay Sievers goto exit;
5557eff2e7aSKay Sievers retval = add_uevent_var(env, "DEVPATH=%s", devpath);
5567eff2e7aSKay Sievers if (retval)
5577eff2e7aSKay Sievers goto exit;
5587eff2e7aSKay Sievers retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
5597eff2e7aSKay Sievers if (retval)
5607eff2e7aSKay Sievers goto exit;
5617eff2e7aSKay Sievers
5627eff2e7aSKay Sievers /* keys passed in from the caller */
5637eff2e7aSKay Sievers if (envp_ext) {
5647eff2e7aSKay Sievers for (i = 0; envp_ext[i]; i++) {
565c65b9145STejun Heo retval = add_uevent_var(env, "%s", envp_ext[i]);
5667eff2e7aSKay Sievers if (retval)
5677eff2e7aSKay Sievers goto exit;
5687eff2e7aSKay Sievers }
5697eff2e7aSKay Sievers }
5701da177e4SLinus Torvalds
5715f123fbdSKay Sievers /* let the kset specific function add its stuff */
572312c004dSKay Sievers if (uevent_ops && uevent_ops->uevent) {
573cf6299b6SGreg Kroah-Hartman retval = uevent_ops->uevent(kobj, env);
5741da177e4SLinus Torvalds if (retval) {
5759f66fa2aSGreg Kroah-Hartman pr_debug("kobject: '%s' (%p): %s: uevent() returned "
5769f66fa2aSGreg Kroah-Hartman "%d\n", kobject_name(kobj), kobj,
577810304dbSHarvey Harrison __func__, retval);
5781da177e4SLinus Torvalds goto exit;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds
5826878e7deSDmitry Torokhov switch (action) {
5836878e7deSDmitry Torokhov case KOBJ_ADD:
5840f4dafc0SKay Sievers /*
5856878e7deSDmitry Torokhov * Mark "add" event so we can make sure we deliver "remove"
5866878e7deSDmitry Torokhov * event to userspace during automatic cleanup. If
5876878e7deSDmitry Torokhov * the object did send an "add" event, "remove" will
5886878e7deSDmitry Torokhov * automatically generated by the core, if not already done
5896878e7deSDmitry Torokhov * by the caller.
5900f4dafc0SKay Sievers */
5910f4dafc0SKay Sievers kobj->state_add_uevent_sent = 1;
5926878e7deSDmitry Torokhov break;
5936878e7deSDmitry Torokhov
5946878e7deSDmitry Torokhov case KOBJ_UNBIND:
5956878e7deSDmitry Torokhov zap_modalias_env(env);
5966878e7deSDmitry Torokhov break;
5976878e7deSDmitry Torokhov
5986878e7deSDmitry Torokhov default:
5996878e7deSDmitry Torokhov break;
6006878e7deSDmitry Torokhov }
6010f4dafc0SKay Sievers
6027eff2e7aSKay Sievers /* we will send an event, so request a new sequence number */
6032444a80cSEric Dumazet retval = add_uevent_var(env, "SEQNUM=%llu",
6042444a80cSEric Dumazet atomic64_inc_return(&uevent_seqnum));
6052444a80cSEric Dumazet if (retval)
6067eff2e7aSKay Sievers goto exit;
6072444a80cSEric Dumazet
60816dff336SEric Dumazet retval = kobject_uevent_net_broadcast(kobj, env, action_string,
60916dff336SEric Dumazet devpath);
6105f123fbdSKay Sievers
61186d56134SMichael Marineau #ifdef CONFIG_UEVENT_HELPER
6125f123fbdSKay Sievers /* call uevent_helper, usually only enabled during early boot */
613417daa1eSEric W. Biederman if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
614bcccff93SVladimir Davydov struct subprocess_info *info;
6155f123fbdSKay Sievers
6167eff2e7aSKay Sievers retval = add_uevent_var(env, "HOME=/");
6177eff2e7aSKay Sievers if (retval)
6187eff2e7aSKay Sievers goto exit;
619e374a2bfSGreg Kroah-Hartman retval = add_uevent_var(env,
620e374a2bfSGreg Kroah-Hartman "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
6217eff2e7aSKay Sievers if (retval)
6227eff2e7aSKay Sievers goto exit;
623bcccff93SVladimir Davydov retval = init_uevent_argv(env, subsystem);
624bcccff93SVladimir Davydov if (retval)
625bcccff93SVladimir Davydov goto exit;
6267eff2e7aSKay Sievers
627bcccff93SVladimir Davydov retval = -ENOMEM;
628bcccff93SVladimir Davydov info = call_usermodehelper_setup(env->argv[0], env->argv,
629bcccff93SVladimir Davydov env->envp, GFP_KERNEL,
630bcccff93SVladimir Davydov NULL, cleanup_uevent_env, env);
631bcccff93SVladimir Davydov if (info) {
632bcccff93SVladimir Davydov retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
633bcccff93SVladimir Davydov env = NULL; /* freed by cleanup_uevent_env */
634bcccff93SVladimir Davydov }
6355f123fbdSKay Sievers }
63686d56134SMichael Marineau #endif
6371da177e4SLinus Torvalds
6381da177e4SLinus Torvalds exit:
6395f123fbdSKay Sievers kfree(devpath);
6407eff2e7aSKay Sievers kfree(env);
641542cfce6SAneesh Kumar K.V return retval;
6421da177e4SLinus Torvalds }
6438a82472fSCornelia Huck EXPORT_SYMBOL_GPL(kobject_uevent_env);
6448a82472fSCornelia Huck
6458a82472fSCornelia Huck /**
646f6e6e779SXiaotian Feng * kobject_uevent - notify userspace by sending an uevent
6478a82472fSCornelia Huck *
6488a82472fSCornelia Huck * @kobj: struct kobject that the action is happening to
649edbbf994SJulia Lawall * @action: action that is happening
650542cfce6SAneesh Kumar K.V *
651542cfce6SAneesh Kumar K.V * Returns 0 if kobject_uevent() is completed with success or the
652542cfce6SAneesh Kumar K.V * corresponding error when it fails.
6538a82472fSCornelia Huck */
kobject_uevent(struct kobject * kobj,enum kobject_action action)654542cfce6SAneesh Kumar K.V int kobject_uevent(struct kobject *kobj, enum kobject_action action)
6558a82472fSCornelia Huck {
656542cfce6SAneesh Kumar K.V return kobject_uevent_env(kobj, action, NULL);
6578a82472fSCornelia Huck }
658312c004dSKay Sievers EXPORT_SYMBOL_GPL(kobject_uevent);
6591da177e4SLinus Torvalds
6601da177e4SLinus Torvalds /**
6617eff2e7aSKay Sievers * add_uevent_var - add key value string to the environment buffer
6627eff2e7aSKay Sievers * @env: environment buffer structure
6637eff2e7aSKay Sievers * @format: printf format for the key=value pair
6641da177e4SLinus Torvalds *
6651da177e4SLinus Torvalds * Returns 0 if environment variable was added successfully or -ENOMEM
6661da177e4SLinus Torvalds * if no space was available.
6671da177e4SLinus Torvalds */
add_uevent_var(struct kobj_uevent_env * env,const char * format,...)6687eff2e7aSKay Sievers int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds va_list args;
6717eff2e7aSKay Sievers int len;
6721da177e4SLinus Torvalds
6737eff2e7aSKay Sievers if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
6745cd2b459SArjan van de Ven WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
6751da177e4SLinus Torvalds return -ENOMEM;
6767eff2e7aSKay Sievers }
6771da177e4SLinus Torvalds
6781da177e4SLinus Torvalds va_start(args, format);
6797eff2e7aSKay Sievers len = vsnprintf(&env->buf[env->buflen],
6807eff2e7aSKay Sievers sizeof(env->buf) - env->buflen,
6817eff2e7aSKay Sievers format, args);
6821da177e4SLinus Torvalds va_end(args);
6831da177e4SLinus Torvalds
6847eff2e7aSKay Sievers if (len >= (sizeof(env->buf) - env->buflen)) {
6855cd2b459SArjan van de Ven WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
6861da177e4SLinus Torvalds return -ENOMEM;
6877eff2e7aSKay Sievers }
6881da177e4SLinus Torvalds
6897eff2e7aSKay Sievers env->envp[env->envp_idx++] = &env->buf[env->buflen];
6907eff2e7aSKay Sievers env->buflen += len + 1;
6911da177e4SLinus Torvalds return 0;
6921da177e4SLinus Torvalds }
693312c004dSKay Sievers EXPORT_SYMBOL_GPL(add_uevent_var);
6941da177e4SLinus Torvalds
6954d17ffdaSKay Sievers #if defined(CONFIG_NET)
uevent_net_broadcast(struct sock * usk,struct sk_buff * skb,struct netlink_ext_ack * extack)696692ec06dSChristian Brauner static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb,
697692ec06dSChristian Brauner struct netlink_ext_ack *extack)
698692ec06dSChristian Brauner {
699692ec06dSChristian Brauner /* u64 to chars: 2^64 - 1 = 21 chars */
700692ec06dSChristian Brauner char buf[sizeof("SEQNUM=") + 21];
701692ec06dSChristian Brauner struct sk_buff *skbc;
702692ec06dSChristian Brauner int ret;
703692ec06dSChristian Brauner
704692ec06dSChristian Brauner /* bump and prepare sequence number */
7052444a80cSEric Dumazet ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu",
7062444a80cSEric Dumazet atomic64_inc_return(&uevent_seqnum));
707692ec06dSChristian Brauner if (ret < 0 || (size_t)ret >= sizeof(buf))
708692ec06dSChristian Brauner return -ENOMEM;
709692ec06dSChristian Brauner ret++;
710692ec06dSChristian Brauner
711692ec06dSChristian Brauner /* verify message does not overflow */
712692ec06dSChristian Brauner if ((skb->len + ret) > UEVENT_BUFFER_SIZE) {
713692ec06dSChristian Brauner NL_SET_ERR_MSG(extack, "uevent message too big");
714692ec06dSChristian Brauner return -EINVAL;
715692ec06dSChristian Brauner }
716692ec06dSChristian Brauner
717692ec06dSChristian Brauner /* copy skb and extend to accommodate sequence number */
718692ec06dSChristian Brauner skbc = skb_copy_expand(skb, 0, ret, GFP_KERNEL);
719692ec06dSChristian Brauner if (!skbc)
720692ec06dSChristian Brauner return -ENOMEM;
721692ec06dSChristian Brauner
722692ec06dSChristian Brauner /* append sequence number */
723692ec06dSChristian Brauner skb_put_data(skbc, buf, ret);
724692ec06dSChristian Brauner
725692ec06dSChristian Brauner /* remove msg header */
726692ec06dSChristian Brauner skb_pull(skbc, NLMSG_HDRLEN);
727692ec06dSChristian Brauner
728692ec06dSChristian Brauner /* set portid 0 to inform userspace message comes from kernel */
729692ec06dSChristian Brauner NETLINK_CB(skbc).portid = 0;
730692ec06dSChristian Brauner NETLINK_CB(skbc).dst_group = 1;
731692ec06dSChristian Brauner
732692ec06dSChristian Brauner ret = netlink_broadcast(usk, skbc, 0, 1, GFP_KERNEL);
733692ec06dSChristian Brauner /* ENOBUFS should be handled in userspace */
734692ec06dSChristian Brauner if (ret == -ENOBUFS || ret == -ESRCH)
735692ec06dSChristian Brauner ret = 0;
736692ec06dSChristian Brauner
737692ec06dSChristian Brauner return ret;
738692ec06dSChristian Brauner }
739692ec06dSChristian Brauner
uevent_net_rcv_skb(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)740692ec06dSChristian Brauner static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
741692ec06dSChristian Brauner struct netlink_ext_ack *extack)
742692ec06dSChristian Brauner {
743692ec06dSChristian Brauner struct net *net;
744692ec06dSChristian Brauner int ret;
745692ec06dSChristian Brauner
746692ec06dSChristian Brauner if (!nlmsg_data(nlh))
747692ec06dSChristian Brauner return -EINVAL;
748692ec06dSChristian Brauner
749692ec06dSChristian Brauner /*
750692ec06dSChristian Brauner * Verify that we are allowed to send messages to the target
751692ec06dSChristian Brauner * network namespace. The caller must have CAP_SYS_ADMIN in the
752692ec06dSChristian Brauner * owning user namespace of the target network namespace.
753692ec06dSChristian Brauner */
754692ec06dSChristian Brauner net = sock_net(NETLINK_CB(skb).sk);
755692ec06dSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, CAP_SYS_ADMIN)) {
756692ec06dSChristian Brauner NL_SET_ERR_MSG(extack, "missing CAP_SYS_ADMIN capability");
757692ec06dSChristian Brauner return -EPERM;
758692ec06dSChristian Brauner }
759692ec06dSChristian Brauner
760692ec06dSChristian Brauner ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack);
761692ec06dSChristian Brauner
762692ec06dSChristian Brauner return ret;
763692ec06dSChristian Brauner }
764692ec06dSChristian Brauner
uevent_net_rcv(struct sk_buff * skb)765692ec06dSChristian Brauner static void uevent_net_rcv(struct sk_buff *skb)
766692ec06dSChristian Brauner {
767692ec06dSChristian Brauner netlink_rcv_skb(skb, &uevent_net_rcv_skb);
768692ec06dSChristian Brauner }
769692ec06dSChristian Brauner
uevent_net_init(struct net * net)77007e98962SEric W. Biederman static int uevent_net_init(struct net *net)
7715f123fbdSKay Sievers {
77207e98962SEric W. Biederman struct uevent_sock *ue_sk;
773a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = {
774a31f2d17SPablo Neira Ayuso .groups = 1,
775692ec06dSChristian Brauner .input = uevent_net_rcv,
776692ec06dSChristian Brauner .flags = NL_CFG_F_NONROOT_RECV
777a31f2d17SPablo Neira Ayuso };
77807e98962SEric W. Biederman
77907e98962SEric W. Biederman ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
78007e98962SEric W. Biederman if (!ue_sk)
78107e98962SEric W. Biederman return -ENOMEM;
78207e98962SEric W. Biederman
7839f00d977SPablo Neira Ayuso ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
78407e98962SEric W. Biederman if (!ue_sk->sk) {
785b3fa29adSBo YU pr_err("kobject_uevent: unable to create netlink socket!\n");
786743db2d9SDan Carpenter kfree(ue_sk);
7875f123fbdSKay Sievers return -ENODEV;
7885f123fbdSKay Sievers }
78994e5e308SChristian Brauner
79094e5e308SChristian Brauner net->uevent_sock = ue_sk;
79194e5e308SChristian Brauner
792a3498436SChristian Brauner /* Restrict uevents to initial user namespace. */
793a3498436SChristian Brauner if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
79407e98962SEric W. Biederman mutex_lock(&uevent_sock_mutex);
79507e98962SEric W. Biederman list_add_tail(&ue_sk->list, &uevent_sock_list);
79607e98962SEric W. Biederman mutex_unlock(&uevent_sock_mutex);
797a3498436SChristian Brauner }
798a3498436SChristian Brauner
7995f123fbdSKay Sievers return 0;
8005f123fbdSKay Sievers }
8015f123fbdSKay Sievers
uevent_net_exit(struct net * net)80207e98962SEric W. Biederman static void uevent_net_exit(struct net *net)
80307e98962SEric W. Biederman {
80494e5e308SChristian Brauner struct uevent_sock *ue_sk = net->uevent_sock;
80507e98962SEric W. Biederman
806a3498436SChristian Brauner if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
80707e98962SEric W. Biederman mutex_lock(&uevent_sock_mutex);
80807e98962SEric W. Biederman list_del(&ue_sk->list);
80907e98962SEric W. Biederman mutex_unlock(&uevent_sock_mutex);
810a3498436SChristian Brauner }
81107e98962SEric W. Biederman
81207e98962SEric W. Biederman netlink_kernel_release(ue_sk->sk);
81307e98962SEric W. Biederman kfree(ue_sk);
81407e98962SEric W. Biederman }
81507e98962SEric W. Biederman
81607e98962SEric W. Biederman static struct pernet_operations uevent_net_ops = {
81707e98962SEric W. Biederman .init = uevent_net_init,
81807e98962SEric W. Biederman .exit = uevent_net_exit,
81907e98962SEric W. Biederman };
82007e98962SEric W. Biederman
kobject_uevent_init(void)82107e98962SEric W. Biederman static int __init kobject_uevent_init(void)
82207e98962SEric W. Biederman {
82307e98962SEric W. Biederman return register_pernet_subsys(&uevent_net_ops);
82407e98962SEric W. Biederman }
82507e98962SEric W. Biederman
82607e98962SEric W. Biederman
8275f123fbdSKay Sievers postcore_initcall(kobject_uevent_init);
8284d17ffdaSKay Sievers #endif
829