blob: 3ac2679fed09090f49267fa69c03967f54d547ee [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * kernel userspace event delivery
4 *
5 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2004 Novell, Inc. All rights reserved.
7 * Copyright (C) 2004 IBM, Inc. All rights reserved.
8 *
9 * Authors:
10 * Robert Love <rml@novell.com>
11 * Kay Sievers <kay.sievers@vrfy.org>
12 * Arjan van de Ven <arjanv@redhat.com>
13 * Greg Kroah-Hartman <greg@kroah.com>
14 */
15
16#include <linux/spinlock.h>
17#include <linux/string.h>
18#include <linux/kobject.h>
19#include <linux/export.h>
20#include <linux/kmod.h>
21#include <linux/slab.h>
22#include <linux/socket.h>
23#include <linux/skbuff.h>
24#include <linux/netlink.h>
25#include <linux/uidgid.h>
26#include <linux/uuid.h>
27#include <linux/ctype.h>
28#include <net/sock.h>
29#include <net/netlink.h>
30#include <net/net_namespace.h>
31
32
33u64 uevent_seqnum;
34#ifdef CONFIG_UEVENT_HELPER
35char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
36#endif
37
38struct uevent_sock {
39 struct list_head list;
40 struct sock *sk;
41};
42
43#ifdef CONFIG_NET
44static LIST_HEAD(uevent_sock_list);
45#endif
46
47/* This lock protects uevent_seqnum and uevent_sock_list */
48static DEFINE_MUTEX(uevent_sock_mutex);
49
50/* the strings here must match the enum in include/linux/kobject.h */
51static const char *kobject_actions[] = {
52 [KOBJ_ADD] = "add",
53 [KOBJ_REMOVE] = "remove",
54 [KOBJ_CHANGE] = "change",
55 [KOBJ_MOVE] = "move",
56 [KOBJ_ONLINE] = "online",
57 [KOBJ_OFFLINE] = "offline",
58 [KOBJ_BIND] = "bind",
59 [KOBJ_UNBIND] = "unbind",
60};
61
62static int kobject_action_type(const char *buf, size_t count,
63 enum kobject_action *type,
64 const char **args)
65{
66 enum kobject_action action;
67 size_t count_first;
68 const char *args_start;
69 int ret = -EINVAL;
70
71 if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
72 count--;
73
74 if (!count)
75 goto out;
76
77 args_start = strnchr(buf, count, ' ');
78 if (args_start) {
79 count_first = args_start - buf;
80 args_start = args_start + 1;
81 } else
82 count_first = count;
83
84 for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
85 if (strncmp(kobject_actions[action], buf, count_first) != 0)
86 continue;
87 if (kobject_actions[action][count_first] != '\0')
88 continue;
89 if (args)
90 *args = args_start;
91 *type = action;
92 ret = 0;
93 break;
94 }
95out:
96 return ret;
97}
98
99static const char *action_arg_word_end(const char *buf, const char *buf_end,
100 char delim)
101{
102 const char *next = buf;
103
104 while (next <= buf_end && *next != delim)
105 if (!isalnum(*next++))
106 return NULL;
107
108 if (next == buf)
109 return NULL;
110
111 return next;
112}
113
114static int kobject_action_args(const char *buf, size_t count,
115 struct kobj_uevent_env **ret_env)
116{
117 struct kobj_uevent_env *env = NULL;
118 const char *next, *buf_end, *key;
119 int key_len;
120 int r = -EINVAL;
121
122 if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
123 count--;
124
125 if (!count)
126 return -EINVAL;
127
128 env = kzalloc(sizeof(*env), GFP_KERNEL);
129 if (!env)
130 return -ENOMEM;
131
132 /* first arg is UUID */
133 if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
134 add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
135 goto out;
136
137 /*
138 * the rest are custom environment variables in KEY=VALUE
139 * format with ' ' delimiter between each KEY=VALUE pair
140 */
141 next = buf + UUID_STRING_LEN;
142 buf_end = buf + count - 1;
143
144 while (next <= buf_end) {
145 if (*next != ' ')
146 goto out;
147
148 /* skip the ' ', key must follow */
149 key = ++next;
150 if (key > buf_end)
151 goto out;
152
153 buf = next;
154 next = action_arg_word_end(buf, buf_end, '=');
155 if (!next || next > buf_end || *next != '=')
156 goto out;
157 key_len = next - buf;
158
159 /* skip the '=', value must follow */
160 if (++next > buf_end)
161 goto out;
162
163 buf = next;
164 next = action_arg_word_end(buf, buf_end, ' ');
165 if (!next)
166 goto out;
167
168 if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
169 key_len, key, (int) (next - buf), buf))
170 goto out;
171 }
172
173 r = 0;
174out:
175 if (r)
176 kfree(env);
177 else
178 *ret_env = env;
179 return r;
180}
181
182u64 uevent_next_seqnum(void)
183{
184 u64 seq;
185
186 mutex_lock(&uevent_sock_mutex);
187 seq = ++uevent_seqnum;
188 mutex_unlock(&uevent_sock_mutex);
189
190 return seq;
191}
192EXPORT_SYMBOL_GPL(uevent_next_seqnum);
193
194/**
195 * kobject_synth_uevent - send synthetic uevent with arguments
196 *
197 * @kobj: struct kobject for which synthetic uevent is to be generated
198 * @buf: buffer containing action type and action args, newline is ignored
199 * @count: length of buffer
200 *
201 * Returns 0 if kobject_synthetic_uevent() is completed with success or the
202 * corresponding error when it fails.
203 */
204int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
205{
206 char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
207 enum kobject_action action;
208 const char *action_args;
209 struct kobj_uevent_env *env;
210 const char *msg = NULL, *devpath;
211 int r;
212
213 r = kobject_action_type(buf, count, &action, &action_args);
214 if (r) {
215 msg = "unknown uevent action string";
216 goto out;
217 }
218
219 if (!action_args) {
220 r = kobject_uevent_env(kobj, action, no_uuid_envp);
221 goto out;
222 }
223
224 r = kobject_action_args(action_args,
225 count - (action_args - buf), &env);
226 if (r == -EINVAL) {
227 msg = "incorrect uevent action arguments";
228 goto out;
229 }
230
231 if (r)
232 goto out;
233
234 r = kobject_uevent_env(kobj, action, env->envp);
235 kfree(env);
236out:
237 if (r) {
238 devpath = kobject_get_path(kobj, GFP_KERNEL);
239 pr_warn("synth uevent: %s: %s\n",
240 devpath ?: "unknown device",
241 msg ?: "failed to send uevent");
242 kfree(devpath);
243 }
244 return r;
245}
246
247#ifdef CONFIG_UEVENT_HELPER
248static int kobj_usermode_filter(struct kobject *kobj)
249{
250 const struct kobj_ns_type_operations *ops;
251
252 ops = kobj_ns_ops(kobj);
253 if (ops) {
254 const void *init_ns, *ns;
255
256 ns = kobj->ktype->namespace(kobj);
257 init_ns = ops->initial_ns();
258 return ns != init_ns;
259 }
260
261 return 0;
262}
263
264static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
265{
266 int buffer_size = sizeof(env->buf) - env->buflen;
267 int len;
268
269 len = strlcpy(&env->buf[env->buflen], subsystem, buffer_size);
270 if (len >= buffer_size) {
271 pr_warn("init_uevent_argv: buffer size of %d too small, needed %d\n",
272 buffer_size, len);
273 return -ENOMEM;
274 }
275
276 env->argv[0] = uevent_helper;
277 env->argv[1] = &env->buf[env->buflen];
278 env->argv[2] = NULL;
279
280 env->buflen += len + 1;
281 return 0;
282}
283
284static void cleanup_uevent_env(struct subprocess_info *info)
285{
286 kfree(info->data);
287}
288#endif
289
290#ifdef CONFIG_NET
291static struct sk_buff *alloc_uevent_skb(struct kobj_uevent_env *env,
292 const char *action_string,
293 const char *devpath)
294{
295 struct netlink_skb_parms *parms;
296 struct sk_buff *skb = NULL;
297 char *scratch;
298 size_t len;
299
300 /* allocate message with maximum possible size */
301 len = strlen(action_string) + strlen(devpath) + 2;
302 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
303 if (!skb)
304 return NULL;
305
306 /* add header */
307 scratch = skb_put(skb, len);
308 sprintf(scratch, "%s@%s", action_string, devpath);
309
310 skb_put_data(skb, env->buf, env->buflen);
311
312 parms = &NETLINK_CB(skb);
313 parms->creds.uid = GLOBAL_ROOT_UID;
314 parms->creds.gid = GLOBAL_ROOT_GID;
315 parms->dst_group = 1;
316 parms->portid = 0;
317
318 return skb;
319}
320
321static int uevent_net_broadcast_untagged(struct kobj_uevent_env *env,
322 const char *action_string,
323 const char *devpath)
324{
325 struct sk_buff *skb = NULL;
326 struct uevent_sock *ue_sk;
327 int retval = 0;
328
329 /* send netlink message */
330 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
331 struct sock *uevent_sock = ue_sk->sk;
332
333 if (!netlink_has_listeners(uevent_sock, 1))
334 continue;
335
336 if (!skb) {
337 retval = -ENOMEM;
338 skb = alloc_uevent_skb(env, action_string, devpath);
339 if (!skb)
340 continue;
341 }
342
343 retval = netlink_broadcast(uevent_sock, skb_get(skb), 0, 1,
344 GFP_KERNEL);
345 /* ENOBUFS should be handled in userspace */
346 if (retval == -ENOBUFS || retval == -ESRCH)
347 retval = 0;
348 }
349 consume_skb(skb);
350
351 return retval;
352}
353
354static int uevent_net_broadcast_tagged(struct sock *usk,
355 struct kobj_uevent_env *env,
356 const char *action_string,
357 const char *devpath)
358{
359 struct user_namespace *owning_user_ns = sock_net(usk)->user_ns;
360 struct sk_buff *skb = NULL;
361 int ret = 0;
362
363 skb = alloc_uevent_skb(env, action_string, devpath);
364 if (!skb)
365 return -ENOMEM;
366
367 /* fix credentials */
368 if (owning_user_ns != &init_user_ns) {
369 struct netlink_skb_parms *parms = &NETLINK_CB(skb);
370 kuid_t root_uid;
371 kgid_t root_gid;
372
373 /* fix uid */
374 root_uid = make_kuid(owning_user_ns, 0);
375 if (uid_valid(root_uid))
376 parms->creds.uid = root_uid;
377
378 /* fix gid */
379 root_gid = make_kgid(owning_user_ns, 0);
380 if (gid_valid(root_gid))
381 parms->creds.gid = root_gid;
382 }
383
384 ret = netlink_broadcast(usk, skb, 0, 1, GFP_KERNEL);
385 /* ENOBUFS should be handled in userspace */
386 if (ret == -ENOBUFS || ret == -ESRCH)
387 ret = 0;
388
389 return ret;
390}
391#endif
392
393static int kobject_uevent_net_broadcast(struct kobject *kobj,
394 struct kobj_uevent_env *env,
395 const char *action_string,
396 const char *devpath)
397{
398 int ret = 0;
399
400#ifdef CONFIG_NET
401 const struct kobj_ns_type_operations *ops;
402 const struct net *net = NULL;
403
404 ops = kobj_ns_ops(kobj);
405 if (!ops && kobj->kset) {
406 struct kobject *ksobj = &kobj->kset->kobj;
407
408 if (ksobj->parent != NULL)
409 ops = kobj_ns_ops(ksobj->parent);
410 }
411
412 /* kobjects currently only carry network namespace tags and they
413 * are the only tag relevant here since we want to decide which
414 * network namespaces to broadcast the uevent into.
415 */
416 if (ops && ops->netlink_ns && kobj->ktype->namespace)
417 if (ops->type == KOBJ_NS_TYPE_NET)
418 net = kobj->ktype->namespace(kobj);
419
420 if (!net)
421 ret = uevent_net_broadcast_untagged(env, action_string,
422 devpath);
423 else
424 ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
425 action_string, devpath);
426#endif
427
428 return ret;
429}
430
431static void zap_modalias_env(struct kobj_uevent_env *env)
432{
433 static const char modalias_prefix[] = "MODALIAS=";
434 size_t len;
435 int i, j;
436
437 for (i = 0; i < env->envp_idx;) {
438 if (strncmp(env->envp[i], modalias_prefix,
439 sizeof(modalias_prefix) - 1)) {
440 i++;
441 continue;
442 }
443
444 len = strlen(env->envp[i]) + 1;
445
446 if (i != env->envp_idx - 1) {
447 /* @env->envp[] contains pointers to @env->buf[]
448 * with @env->buflen chars, and we are removing
449 * variable MODALIAS here pointed by @env->envp[i]
450 * with length @len as shown below:
451 *
452 * 0 @env->buf[] @env->buflen
453 * ---------------------------------------------
454 * ^ ^ ^ ^
455 * | |-> @len <-| target block |
456 * @env->envp[0] @env->envp[i] @env->envp[i + 1]
457 *
458 * so the "target block" indicated above is moved
459 * backward by @len, and its right size is
460 * @env->buflen - (@env->envp[i + 1] - @env->envp[0]).
461 */
462 memmove(env->envp[i], env->envp[i + 1],
463 env->buflen - (env->envp[i + 1] - env->envp[0]));
464
465 for (j = i; j < env->envp_idx - 1; j++)
466 env->envp[j] = env->envp[j + 1] - len;
467 }
468
469 env->envp_idx--;
470 env->buflen -= len;
471 }
472}
473
474/**
475 * kobject_uevent_env - send an uevent with environmental data
476 *
477 * @kobj: struct kobject that the action is happening to
478 * @action: action that is happening
479 * @envp_ext: pointer to environmental data
480 *
481 * Returns 0 if kobject_uevent_env() is completed with success or the
482 * corresponding error when it fails.
483 */
484int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
485 char *envp_ext[])
486{
487 struct kobj_uevent_env *env;
488 const char *action_string = kobject_actions[action];
489 const char *devpath = NULL;
490 const char *subsystem;
491 struct kobject *top_kobj;
492 struct kset *kset;
493 const struct kset_uevent_ops *uevent_ops;
494 int i = 0;
495 int retval = 0;
496
497 /*
498 * Mark "remove" event done regardless of result, for some subsystems
499 * do not want to re-trigger "remove" event via automatic cleanup.
500 */
501 if (action == KOBJ_REMOVE)
502 kobj->state_remove_uevent_sent = 1;
503
504 pr_debug("kobject: '%s' (%p): %s\n",
505 kobject_name(kobj), kobj, __func__);
506
507 /* search the kset we belong to */
508 top_kobj = kobj;
509 while (!top_kobj->kset && top_kobj->parent)
510 top_kobj = top_kobj->parent;
511
512 if (!top_kobj->kset) {
513 pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
514 "without kset!\n", kobject_name(kobj), kobj,
515 __func__);
516 return -EINVAL;
517 }
518
519 kset = top_kobj->kset;
520 uevent_ops = kset->uevent_ops;
521
522 /* skip the event, if uevent_suppress is set*/
523 if (kobj->uevent_suppress) {
524 pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
525 "caused the event to drop!\n",
526 kobject_name(kobj), kobj, __func__);
527 return 0;
528 }
529 /* skip the event, if the filter returns zero. */
530 if (uevent_ops && uevent_ops->filter)
531 if (!uevent_ops->filter(kset, kobj)) {
532 pr_debug("kobject: '%s' (%p): %s: filter function "
533 "caused the event to drop!\n",
534 kobject_name(kobj), kobj, __func__);
535 return 0;
536 }
537
538 /* originating subsystem */
539 if (uevent_ops && uevent_ops->name)
540 subsystem = uevent_ops->name(kset, kobj);
541 else
542 subsystem = kobject_name(&kset->kobj);
543 if (!subsystem) {
544 pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
545 "event to drop!\n", kobject_name(kobj), kobj,
546 __func__);
547 return 0;
548 }
549
550 /* environment buffer */
551 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
552 if (!env)
553 return -ENOMEM;
554
555 /* complete object path */
556 devpath = kobject_get_path(kobj, GFP_KERNEL);
557 if (!devpath) {
558 retval = -ENOENT;
559 goto exit;
560 }
561
562 /* default keys */
563 retval = add_uevent_var(env, "ACTION=%s", action_string);
564 if (retval)
565 goto exit;
566 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
567 if (retval)
568 goto exit;
569 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
570 if (retval)
571 goto exit;
572
573 /* keys passed in from the caller */
574 if (envp_ext) {
575 for (i = 0; envp_ext[i]; i++) {
576 retval = add_uevent_var(env, "%s", envp_ext[i]);
577 if (retval)
578 goto exit;
579 }
580 }
581
582 /* let the kset specific function add its stuff */
583 if (uevent_ops && uevent_ops->uevent) {
584 retval = uevent_ops->uevent(kset, kobj, env);
585 if (retval) {
586 pr_debug("kobject: '%s' (%p): %s: uevent() returned "
587 "%d\n", kobject_name(kobj), kobj,
588 __func__, retval);
589 goto exit;
590 }
591 }
592
593 switch (action) {
594 case KOBJ_ADD:
595 /*
596 * Mark "add" event so we can make sure we deliver "remove"
597 * event to userspace during automatic cleanup. If
598 * the object did send an "add" event, "remove" will
599 * automatically generated by the core, if not already done
600 * by the caller.
601 */
602 kobj->state_add_uevent_sent = 1;
603 break;
604
605 case KOBJ_UNBIND:
606 zap_modalias_env(env);
607 break;
608
609 default:
610 break;
611 }
612
613 mutex_lock(&uevent_sock_mutex);
614 /* we will send an event, so request a new sequence number */
615 retval = add_uevent_var(env, "SEQNUM=%llu", ++uevent_seqnum);
616 if (retval) {
617 mutex_unlock(&uevent_sock_mutex);
618 goto exit;
619 }
620 retval = kobject_uevent_net_broadcast(kobj, env, action_string,
621 devpath);
622 mutex_unlock(&uevent_sock_mutex);
623
624#ifdef CONFIG_UEVENT_HELPER
625 /* call uevent_helper, usually only enabled during early boot */
626 if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
627 struct subprocess_info *info;
628
629 retval = add_uevent_var(env, "HOME=/");
630 if (retval)
631 goto exit;
632 retval = add_uevent_var(env,
633 "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
634 if (retval)
635 goto exit;
636 retval = init_uevent_argv(env, subsystem);
637 if (retval)
638 goto exit;
639
640 retval = -ENOMEM;
641 info = call_usermodehelper_setup(env->argv[0], env->argv,
642 env->envp, GFP_KERNEL,
643 NULL, cleanup_uevent_env, env);
644 if (info) {
645 retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
646 env = NULL; /* freed by cleanup_uevent_env */
647 }
648 }
649#endif
650
651exit:
652 kfree(devpath);
653 kfree(env);
654 return retval;
655}
656EXPORT_SYMBOL_GPL(kobject_uevent_env);
657
658/**
659 * kobject_uevent - notify userspace by sending an uevent
660 *
661 * @kobj: struct kobject that the action is happening to
662 * @action: action that is happening
663 *
664 * Returns 0 if kobject_uevent() is completed with success or the
665 * corresponding error when it fails.
666 */
667int kobject_uevent(struct kobject *kobj, enum kobject_action action)
668{
669 return kobject_uevent_env(kobj, action, NULL);
670}
671EXPORT_SYMBOL_GPL(kobject_uevent);
672
673/**
674 * add_uevent_var - add key value string to the environment buffer
675 * @env: environment buffer structure
676 * @format: printf format for the key=value pair
677 *
678 * Returns 0 if environment variable was added successfully or -ENOMEM
679 * if no space was available.
680 */
681int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
682{
683 va_list args;
684 int len;
685
686 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
687 WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
688 return -ENOMEM;
689 }
690
691 va_start(args, format);
692 len = vsnprintf(&env->buf[env->buflen],
693 sizeof(env->buf) - env->buflen,
694 format, args);
695 va_end(args);
696
697 if (len >= (sizeof(env->buf) - env->buflen)) {
698 WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
699 return -ENOMEM;
700 }
701
702 env->envp[env->envp_idx++] = &env->buf[env->buflen];
703 env->buflen += len + 1;
704 return 0;
705}
706EXPORT_SYMBOL_GPL(add_uevent_var);
707
708#if defined(CONFIG_NET)
709int broadcast_uevent(struct sk_buff *skb, __u32 pid, __u32 group,
710 gfp_t allocation)
711{
712 struct uevent_sock *ue_sk;
713 int err = 0;
714
715 /* send netlink message */
716 mutex_lock(&uevent_sock_mutex);
717 list_for_each_entry(ue_sk, &uevent_sock_list, list) {
718 struct sock *uevent_sock = ue_sk->sk;
719 struct sk_buff *skb2;
720
721 skb2 = skb_clone(skb, allocation);
722 if (!skb2)
723 break;
724
725 err = netlink_broadcast(uevent_sock, skb2, pid, group,
726 allocation);
727 if (err)
728 break;
729 }
730 mutex_unlock(&uevent_sock_mutex);
731
732 kfree_skb(skb);
733 return err;
734}
735#else
736int broadcast_uevent(struct sk_buff *skb, __u32 pid, __u32 group,
737 gfp_t allocation)
738{
739 kfree_skb(skb);
740 return 0;
741}
742#endif
743EXPORT_SYMBOL_GPL(broadcast_uevent);
744
745#if defined(CONFIG_NET)
746static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb,
747 struct netlink_ext_ack *extack)
748{
749 /* u64 to chars: 2^64 - 1 = 21 chars */
750 char buf[sizeof("SEQNUM=") + 21];
751 struct sk_buff *skbc;
752 int ret;
753
754 /* bump and prepare sequence number */
755 ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu", ++uevent_seqnum);
756 if (ret < 0 || (size_t)ret >= sizeof(buf))
757 return -ENOMEM;
758 ret++;
759
760 /* verify message does not overflow */
761 if ((skb->len + ret) > UEVENT_BUFFER_SIZE) {
762 NL_SET_ERR_MSG(extack, "uevent message too big");
763 return -EINVAL;
764 }
765
766 /* copy skb and extend to accommodate sequence number */
767 skbc = skb_copy_expand(skb, 0, ret, GFP_KERNEL);
768 if (!skbc)
769 return -ENOMEM;
770
771 /* append sequence number */
772 skb_put_data(skbc, buf, ret);
773
774 /* remove msg header */
775 skb_pull(skbc, NLMSG_HDRLEN);
776
777 /* set portid 0 to inform userspace message comes from kernel */
778 NETLINK_CB(skbc).portid = 0;
779 NETLINK_CB(skbc).dst_group = 1;
780
781 ret = netlink_broadcast(usk, skbc, 0, 1, GFP_KERNEL);
782 /* ENOBUFS should be handled in userspace */
783 if (ret == -ENOBUFS || ret == -ESRCH)
784 ret = 0;
785
786 return ret;
787}
788
789static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
790 struct netlink_ext_ack *extack)
791{
792 struct net *net;
793 int ret;
794
795 if (!nlmsg_data(nlh))
796 return -EINVAL;
797
798 /*
799 * Verify that we are allowed to send messages to the target
800 * network namespace. The caller must have CAP_SYS_ADMIN in the
801 * owning user namespace of the target network namespace.
802 */
803 net = sock_net(NETLINK_CB(skb).sk);
804 if (!netlink_ns_capable(skb, net->user_ns, CAP_SYS_ADMIN)) {
805 NL_SET_ERR_MSG(extack, "missing CAP_SYS_ADMIN capability");
806 return -EPERM;
807 }
808
809 mutex_lock(&uevent_sock_mutex);
810 ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack);
811 mutex_unlock(&uevent_sock_mutex);
812
813 return ret;
814}
815
816static void uevent_net_rcv(struct sk_buff *skb)
817{
818 netlink_rcv_skb(skb, &uevent_net_rcv_skb);
819}
820
821static int uevent_net_init(struct net *net)
822{
823 struct uevent_sock *ue_sk;
824 struct netlink_kernel_cfg cfg = {
825 .groups = 1,
826 .input = uevent_net_rcv,
827 .flags = NL_CFG_F_NONROOT_RECV
828 };
829
830 ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
831 if (!ue_sk)
832 return -ENOMEM;
833
834 ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
835 if (!ue_sk->sk) {
836 pr_err("kobject_uevent: unable to create netlink socket!\n");
837 kfree(ue_sk);
838 return -ENODEV;
839 }
840
841 net->uevent_sock = ue_sk;
842
843 /* Restrict uevents to initial user namespace. */
844 if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
845 mutex_lock(&uevent_sock_mutex);
846 list_add_tail(&ue_sk->list, &uevent_sock_list);
847 mutex_unlock(&uevent_sock_mutex);
848 }
849
850 return 0;
851}
852
853static void uevent_net_exit(struct net *net)
854{
855 struct uevent_sock *ue_sk = net->uevent_sock;
856
857 if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
858 mutex_lock(&uevent_sock_mutex);
859 list_del(&ue_sk->list);
860 mutex_unlock(&uevent_sock_mutex);
861 }
862
863 netlink_kernel_release(ue_sk->sk);
864 kfree(ue_sk);
865}
866
867static struct pernet_operations uevent_net_ops = {
868 .init = uevent_net_init,
869 .exit = uevent_net_exit,
870};
871
872static int __init kobject_uevent_init(void)
873{
874 return register_pernet_subsys(&uevent_net_ops);
875}
876
877
878postcore_initcall(kobject_uevent_init);
879#endif