Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * kernel userspace event delivery
0004  *
0005  * Copyright (C) 2004 Red Hat, Inc.  All rights reserved.
0006  * Copyright (C) 2004 Novell, Inc.  All rights reserved.
0007  * Copyright (C) 2004 IBM, Inc. All rights reserved.
0008  *
0009  * Authors:
0010  *  Robert Love     <rml@novell.com>
0011  *  Kay Sievers     <kay.sievers@vrfy.org>
0012  *  Arjan van de Ven    <arjanv@redhat.com>
0013  *  Greg Kroah-Hartman  <greg@kroah.com>
0014  */
0015 
0016 #include <linux/spinlock.h>
0017 #include <linux/string.h>
0018 #include <linux/kobject.h>
0019 #include <linux/export.h>
0020 #include <linux/kmod.h>
0021 #include <linux/slab.h>
0022 #include <linux/socket.h>
0023 #include <linux/skbuff.h>
0024 #include <linux/netlink.h>
0025 #include <linux/uidgid.h>
0026 #include <linux/uuid.h>
0027 #include <linux/ctype.h>
0028 #include <net/sock.h>
0029 #include <net/netlink.h>
0030 #include <net/net_namespace.h>
0031 
0032 
0033 u64 uevent_seqnum;
0034 #ifdef CONFIG_UEVENT_HELPER
0035 char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
0036 #endif
0037 
0038 struct uevent_sock {
0039     struct list_head list;
0040     struct sock *sk;
0041 };
0042 
0043 #ifdef CONFIG_NET
0044 static LIST_HEAD(uevent_sock_list);
0045 #endif
0046 
0047 /* This lock protects uevent_seqnum and uevent_sock_list */
0048 static DEFINE_MUTEX(uevent_sock_mutex);
0049 
0050 /* the strings here must match the enum in include/linux/kobject.h */
0051 static const char *kobject_actions[] = {
0052     [KOBJ_ADD] =        "add",
0053     [KOBJ_REMOVE] =     "remove",
0054     [KOBJ_CHANGE] =     "change",
0055     [KOBJ_MOVE] =       "move",
0056     [KOBJ_ONLINE] =     "online",
0057     [KOBJ_OFFLINE] =    "offline",
0058     [KOBJ_BIND] =       "bind",
0059     [KOBJ_UNBIND] =     "unbind",
0060 };
0061 
0062 static int kobject_action_type(const char *buf, size_t count,
0063                    enum kobject_action *type,
0064                    const char **args)
0065 {
0066     enum kobject_action action;
0067     size_t count_first;
0068     const char *args_start;
0069     int ret = -EINVAL;
0070 
0071     if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
0072         count--;
0073 
0074     if (!count)
0075         goto out;
0076 
0077     args_start = strnchr(buf, count, ' ');
0078     if (args_start) {
0079         count_first = args_start - buf;
0080         args_start = args_start + 1;
0081     } else
0082         count_first = count;
0083 
0084     for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
0085         if (strncmp(kobject_actions[action], buf, count_first) != 0)
0086             continue;
0087         if (kobject_actions[action][count_first] != '\0')
0088             continue;
0089         if (args)
0090             *args = args_start;
0091         *type = action;
0092         ret = 0;
0093         break;
0094     }
0095 out:
0096     return ret;
0097 }
0098 
0099 static const char *action_arg_word_end(const char *buf, const char *buf_end,
0100                        char delim)
0101 {
0102     const char *next = buf;
0103 
0104     while (next <= buf_end && *next != delim)
0105         if (!isalnum(*next++))
0106             return NULL;
0107 
0108     if (next == buf)
0109         return NULL;
0110 
0111     return next;
0112 }
0113 
0114 static int kobject_action_args(const char *buf, size_t count,
0115                    struct kobj_uevent_env **ret_env)
0116 {
0117     struct kobj_uevent_env *env = NULL;
0118     const char *next, *buf_end, *key;
0119     int key_len;
0120     int r = -EINVAL;
0121 
0122     if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
0123         count--;
0124 
0125     if (!count)
0126         return -EINVAL;
0127 
0128     env = kzalloc(sizeof(*env), GFP_KERNEL);
0129     if (!env)
0130         return -ENOMEM;
0131 
0132     /* first arg is UUID */
0133     if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
0134         add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
0135         goto out;
0136 
0137     /*
0138      * the rest are custom environment variables in KEY=VALUE
0139      * format with ' ' delimiter between each KEY=VALUE pair
0140      */
0141     next = buf + UUID_STRING_LEN;
0142     buf_end = buf + count - 1;
0143 
0144     while (next <= buf_end) {
0145         if (*next != ' ')
0146             goto out;
0147 
0148         /* skip the ' ', key must follow */
0149         key = ++next;
0150         if (key > buf_end)
0151             goto out;
0152 
0153         buf = next;
0154         next = action_arg_word_end(buf, buf_end, '=');
0155         if (!next || next > buf_end || *next != '=')
0156             goto out;
0157         key_len = next - buf;
0158 
0159         /* skip the '=', value must follow */
0160         if (++next > buf_end)
0161             goto out;
0162 
0163         buf = next;
0164         next = action_arg_word_end(buf, buf_end, ' ');
0165         if (!next)
0166             goto out;
0167 
0168         if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
0169                    key_len, key, (int) (next - buf), buf))
0170             goto out;
0171     }
0172 
0173     r = 0;
0174 out:
0175     if (r)
0176         kfree(env);
0177     else
0178         *ret_env = env;
0179     return r;
0180 }
0181 
0182 /**
0183  * kobject_synth_uevent - send synthetic uevent with arguments
0184  *
0185  * @kobj: struct kobject for which synthetic uevent is to be generated
0186  * @buf: buffer containing action type and action args, newline is ignored
0187  * @count: length of buffer
0188  *
0189  * Returns 0 if kobject_synthetic_uevent() is completed with success or the
0190  * corresponding error when it fails.
0191  */
0192 int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
0193 {
0194     char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
0195     enum kobject_action action;
0196     const char *action_args;
0197     struct kobj_uevent_env *env;
0198     const char *msg = NULL, *devpath;
0199     int r;
0200 
0201     r = kobject_action_type(buf, count, &action, &action_args);
0202     if (r) {
0203         msg = "unknown uevent action string";
0204         goto out;
0205     }
0206 
0207     if (!action_args) {
0208         r = kobject_uevent_env(kobj, action, no_uuid_envp);
0209         goto out;
0210     }
0211 
0212     r = kobject_action_args(action_args,
0213                 count - (action_args - buf), &env);
0214     if (r == -EINVAL) {
0215         msg = "incorrect uevent action arguments";
0216         goto out;
0217     }
0218 
0219     if (r)
0220         goto out;
0221 
0222     r = kobject_uevent_env(kobj, action, env->envp);
0223     kfree(env);
0224 out:
0225     if (r) {
0226         devpath = kobject_get_path(kobj, GFP_KERNEL);
0227         pr_warn("synth uevent: %s: %s\n",
0228                devpath ?: "unknown device",
0229                msg ?: "failed to send uevent");
0230         kfree(devpath);
0231     }
0232     return r;
0233 }
0234 
0235 #ifdef CONFIG_UEVENT_HELPER
0236 static int kobj_usermode_filter(struct kobject *kobj)
0237 {
0238     const struct kobj_ns_type_operations *ops;
0239 
0240     ops = kobj_ns_ops(kobj);
0241     if (ops) {
0242         const void *init_ns, *ns;
0243 
0244         ns = kobj->ktype->namespace(kobj);
0245         init_ns = ops->initial_ns();
0246         return ns != init_ns;
0247     }
0248 
0249     return 0;
0250 }
0251 
0252 static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
0253 {
0254     int buffer_size = sizeof(env->buf) - env->buflen;
0255     int len;
0256 
0257     len = strlcpy(&env->buf[env->buflen], subsystem, buffer_size);
0258     if (len >= buffer_size) {
0259         pr_warn("init_uevent_argv: buffer size of %d too small, needed %d\n",
0260             buffer_size, len);
0261         return -ENOMEM;
0262     }
0263 
0264     env->argv[0] = uevent_helper;
0265     env->argv[1] = &env->buf[env->buflen];
0266     env->argv[2] = NULL;
0267 
0268     env->buflen += len + 1;
0269     return 0;
0270 }
0271 
0272 static void cleanup_uevent_env(struct subprocess_info *info)
0273 {
0274     kfree(info->data);
0275 }
0276 #endif
0277 
0278 #ifdef CONFIG_NET
0279 static struct sk_buff *alloc_uevent_skb(struct kobj_uevent_env *env,
0280                     const char *action_string,
0281                     const char *devpath)
0282 {
0283     struct netlink_skb_parms *parms;
0284     struct sk_buff *skb = NULL;
0285     char *scratch;
0286     size_t len;
0287 
0288     /* allocate message with maximum possible size */
0289     len = strlen(action_string) + strlen(devpath) + 2;
0290     skb = alloc_skb(len + env->buflen, GFP_KERNEL);
0291     if (!skb)
0292         return NULL;
0293 
0294     /* add header */
0295     scratch = skb_put(skb, len);
0296     sprintf(scratch, "%s@%s", action_string, devpath);
0297 
0298     skb_put_data(skb, env->buf, env->buflen);
0299 
0300     parms = &NETLINK_CB(skb);
0301     parms->creds.uid = GLOBAL_ROOT_UID;
0302     parms->creds.gid = GLOBAL_ROOT_GID;
0303     parms->dst_group = 1;
0304     parms->portid = 0;
0305 
0306     return skb;
0307 }
0308 
0309 static int uevent_net_broadcast_untagged(struct kobj_uevent_env *env,
0310                      const char *action_string,
0311                      const char *devpath)
0312 {
0313     struct sk_buff *skb = NULL;
0314     struct uevent_sock *ue_sk;
0315     int retval = 0;
0316 
0317     /* send netlink message */
0318     list_for_each_entry(ue_sk, &uevent_sock_list, list) {
0319         struct sock *uevent_sock = ue_sk->sk;
0320 
0321         if (!netlink_has_listeners(uevent_sock, 1))
0322             continue;
0323 
0324         if (!skb) {
0325             retval = -ENOMEM;
0326             skb = alloc_uevent_skb(env, action_string, devpath);
0327             if (!skb)
0328                 continue;
0329         }
0330 
0331         retval = netlink_broadcast(uevent_sock, skb_get(skb), 0, 1,
0332                        GFP_KERNEL);
0333         /* ENOBUFS should be handled in userspace */
0334         if (retval == -ENOBUFS || retval == -ESRCH)
0335             retval = 0;
0336     }
0337     consume_skb(skb);
0338 
0339     return retval;
0340 }
0341 
0342 static int uevent_net_broadcast_tagged(struct sock *usk,
0343                        struct kobj_uevent_env *env,
0344                        const char *action_string,
0345                        const char *devpath)
0346 {
0347     struct user_namespace *owning_user_ns = sock_net(usk)->user_ns;
0348     struct sk_buff *skb = NULL;
0349     int ret = 0;
0350 
0351     skb = alloc_uevent_skb(env, action_string, devpath);
0352     if (!skb)
0353         return -ENOMEM;
0354 
0355     /* fix credentials */
0356     if (owning_user_ns != &init_user_ns) {
0357         struct netlink_skb_parms *parms = &NETLINK_CB(skb);
0358         kuid_t root_uid;
0359         kgid_t root_gid;
0360 
0361         /* fix uid */
0362         root_uid = make_kuid(owning_user_ns, 0);
0363         if (uid_valid(root_uid))
0364             parms->creds.uid = root_uid;
0365 
0366         /* fix gid */
0367         root_gid = make_kgid(owning_user_ns, 0);
0368         if (gid_valid(root_gid))
0369             parms->creds.gid = root_gid;
0370     }
0371 
0372     ret = netlink_broadcast(usk, skb, 0, 1, GFP_KERNEL);
0373     /* ENOBUFS should be handled in userspace */
0374     if (ret == -ENOBUFS || ret == -ESRCH)
0375         ret = 0;
0376 
0377     return ret;
0378 }
0379 #endif
0380 
0381 static int kobject_uevent_net_broadcast(struct kobject *kobj,
0382                     struct kobj_uevent_env *env,
0383                     const char *action_string,
0384                     const char *devpath)
0385 {
0386     int ret = 0;
0387 
0388 #ifdef CONFIG_NET
0389     const struct kobj_ns_type_operations *ops;
0390     const struct net *net = NULL;
0391 
0392     ops = kobj_ns_ops(kobj);
0393     if (!ops && kobj->kset) {
0394         struct kobject *ksobj = &kobj->kset->kobj;
0395 
0396         if (ksobj->parent != NULL)
0397             ops = kobj_ns_ops(ksobj->parent);
0398     }
0399 
0400     /* kobjects currently only carry network namespace tags and they
0401      * are the only tag relevant here since we want to decide which
0402      * network namespaces to broadcast the uevent into.
0403      */
0404     if (ops && ops->netlink_ns && kobj->ktype->namespace)
0405         if (ops->type == KOBJ_NS_TYPE_NET)
0406             net = kobj->ktype->namespace(kobj);
0407 
0408     if (!net)
0409         ret = uevent_net_broadcast_untagged(env, action_string,
0410                             devpath);
0411     else
0412         ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
0413                           action_string, devpath);
0414 #endif
0415 
0416     return ret;
0417 }
0418 
0419 static void zap_modalias_env(struct kobj_uevent_env *env)
0420 {
0421     static const char modalias_prefix[] = "MODALIAS=";
0422     size_t len;
0423     int i, j;
0424 
0425     for (i = 0; i < env->envp_idx;) {
0426         if (strncmp(env->envp[i], modalias_prefix,
0427                 sizeof(modalias_prefix) - 1)) {
0428             i++;
0429             continue;
0430         }
0431 
0432         len = strlen(env->envp[i]) + 1;
0433 
0434         if (i != env->envp_idx - 1) {
0435             memmove(env->envp[i], env->envp[i + 1],
0436                 env->buflen - len);
0437 
0438             for (j = i; j < env->envp_idx - 1; j++)
0439                 env->envp[j] = env->envp[j + 1] - len;
0440         }
0441 
0442         env->envp_idx--;
0443         env->buflen -= len;
0444     }
0445 }
0446 
0447 /**
0448  * kobject_uevent_env - send an uevent with environmental data
0449  *
0450  * @kobj: struct kobject that the action is happening to
0451  * @action: action that is happening
0452  * @envp_ext: pointer to environmental data
0453  *
0454  * Returns 0 if kobject_uevent_env() is completed with success or the
0455  * corresponding error when it fails.
0456  */
0457 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
0458                char *envp_ext[])
0459 {
0460     struct kobj_uevent_env *env;
0461     const char *action_string = kobject_actions[action];
0462     const char *devpath = NULL;
0463     const char *subsystem;
0464     struct kobject *top_kobj;
0465     struct kset *kset;
0466     const struct kset_uevent_ops *uevent_ops;
0467     int i = 0;
0468     int retval = 0;
0469 
0470     /*
0471      * Mark "remove" event done regardless of result, for some subsystems
0472      * do not want to re-trigger "remove" event via automatic cleanup.
0473      */
0474     if (action == KOBJ_REMOVE)
0475         kobj->state_remove_uevent_sent = 1;
0476 
0477     pr_debug("kobject: '%s' (%p): %s\n",
0478          kobject_name(kobj), kobj, __func__);
0479 
0480     /* search the kset we belong to */
0481     top_kobj = kobj;
0482     while (!top_kobj->kset && top_kobj->parent)
0483         top_kobj = top_kobj->parent;
0484 
0485     if (!top_kobj->kset) {
0486         pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
0487              "without kset!\n", kobject_name(kobj), kobj,
0488              __func__);
0489         return -EINVAL;
0490     }
0491 
0492     kset = top_kobj->kset;
0493     uevent_ops = kset->uevent_ops;
0494 
0495     /* skip the event, if uevent_suppress is set*/
0496     if (kobj->uevent_suppress) {
0497         pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
0498                  "caused the event to drop!\n",
0499                  kobject_name(kobj), kobj, __func__);
0500         return 0;
0501     }
0502     /* skip the event, if the filter returns zero. */
0503     if (uevent_ops && uevent_ops->filter)
0504         if (!uevent_ops->filter(kobj)) {
0505             pr_debug("kobject: '%s' (%p): %s: filter function "
0506                  "caused the event to drop!\n",
0507                  kobject_name(kobj), kobj, __func__);
0508             return 0;
0509         }
0510 
0511     /* originating subsystem */
0512     if (uevent_ops && uevent_ops->name)
0513         subsystem = uevent_ops->name(kobj);
0514     else
0515         subsystem = kobject_name(&kset->kobj);
0516     if (!subsystem) {
0517         pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
0518              "event to drop!\n", kobject_name(kobj), kobj,
0519              __func__);
0520         return 0;
0521     }
0522 
0523     /* environment buffer */
0524     env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
0525     if (!env)
0526         return -ENOMEM;
0527 
0528     /* complete object path */
0529     devpath = kobject_get_path(kobj, GFP_KERNEL);
0530     if (!devpath) {
0531         retval = -ENOENT;
0532         goto exit;
0533     }
0534 
0535     /* default keys */
0536     retval = add_uevent_var(env, "ACTION=%s", action_string);
0537     if (retval)
0538         goto exit;
0539     retval = add_uevent_var(env, "DEVPATH=%s", devpath);
0540     if (retval)
0541         goto exit;
0542     retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
0543     if (retval)
0544         goto exit;
0545 
0546     /* keys passed in from the caller */
0547     if (envp_ext) {
0548         for (i = 0; envp_ext[i]; i++) {
0549             retval = add_uevent_var(env, "%s", envp_ext[i]);
0550             if (retval)
0551                 goto exit;
0552         }
0553     }
0554 
0555     /* let the kset specific function add its stuff */
0556     if (uevent_ops && uevent_ops->uevent) {
0557         retval = uevent_ops->uevent(kobj, env);
0558         if (retval) {
0559             pr_debug("kobject: '%s' (%p): %s: uevent() returned "
0560                  "%d\n", kobject_name(kobj), kobj,
0561                  __func__, retval);
0562             goto exit;
0563         }
0564     }
0565 
0566     switch (action) {
0567     case KOBJ_ADD:
0568         /*
0569          * Mark "add" event so we can make sure we deliver "remove"
0570          * event to userspace during automatic cleanup. If
0571          * the object did send an "add" event, "remove" will
0572          * automatically generated by the core, if not already done
0573          * by the caller.
0574          */
0575         kobj->state_add_uevent_sent = 1;
0576         break;
0577 
0578     case KOBJ_UNBIND:
0579         zap_modalias_env(env);
0580         break;
0581 
0582     default:
0583         break;
0584     }
0585 
0586     mutex_lock(&uevent_sock_mutex);
0587     /* we will send an event, so request a new sequence number */
0588     retval = add_uevent_var(env, "SEQNUM=%llu", ++uevent_seqnum);
0589     if (retval) {
0590         mutex_unlock(&uevent_sock_mutex);
0591         goto exit;
0592     }
0593     retval = kobject_uevent_net_broadcast(kobj, env, action_string,
0594                           devpath);
0595     mutex_unlock(&uevent_sock_mutex);
0596 
0597 #ifdef CONFIG_UEVENT_HELPER
0598     /* call uevent_helper, usually only enabled during early boot */
0599     if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
0600         struct subprocess_info *info;
0601 
0602         retval = add_uevent_var(env, "HOME=/");
0603         if (retval)
0604             goto exit;
0605         retval = add_uevent_var(env,
0606                     "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
0607         if (retval)
0608             goto exit;
0609         retval = init_uevent_argv(env, subsystem);
0610         if (retval)
0611             goto exit;
0612 
0613         retval = -ENOMEM;
0614         info = call_usermodehelper_setup(env->argv[0], env->argv,
0615                          env->envp, GFP_KERNEL,
0616                          NULL, cleanup_uevent_env, env);
0617         if (info) {
0618             retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
0619             env = NULL; /* freed by cleanup_uevent_env */
0620         }
0621     }
0622 #endif
0623 
0624 exit:
0625     kfree(devpath);
0626     kfree(env);
0627     return retval;
0628 }
0629 EXPORT_SYMBOL_GPL(kobject_uevent_env);
0630 
0631 /**
0632  * kobject_uevent - notify userspace by sending an uevent
0633  *
0634  * @kobj: struct kobject that the action is happening to
0635  * @action: action that is happening
0636  *
0637  * Returns 0 if kobject_uevent() is completed with success or the
0638  * corresponding error when it fails.
0639  */
0640 int kobject_uevent(struct kobject *kobj, enum kobject_action action)
0641 {
0642     return kobject_uevent_env(kobj, action, NULL);
0643 }
0644 EXPORT_SYMBOL_GPL(kobject_uevent);
0645 
0646 /**
0647  * add_uevent_var - add key value string to the environment buffer
0648  * @env: environment buffer structure
0649  * @format: printf format for the key=value pair
0650  *
0651  * Returns 0 if environment variable was added successfully or -ENOMEM
0652  * if no space was available.
0653  */
0654 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
0655 {
0656     va_list args;
0657     int len;
0658 
0659     if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
0660         WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
0661         return -ENOMEM;
0662     }
0663 
0664     va_start(args, format);
0665     len = vsnprintf(&env->buf[env->buflen],
0666             sizeof(env->buf) - env->buflen,
0667             format, args);
0668     va_end(args);
0669 
0670     if (len >= (sizeof(env->buf) - env->buflen)) {
0671         WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
0672         return -ENOMEM;
0673     }
0674 
0675     env->envp[env->envp_idx++] = &env->buf[env->buflen];
0676     env->buflen += len + 1;
0677     return 0;
0678 }
0679 EXPORT_SYMBOL_GPL(add_uevent_var);
0680 
0681 #if defined(CONFIG_NET)
0682 static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb,
0683                 struct netlink_ext_ack *extack)
0684 {
0685     /* u64 to chars: 2^64 - 1 = 21 chars */
0686     char buf[sizeof("SEQNUM=") + 21];
0687     struct sk_buff *skbc;
0688     int ret;
0689 
0690     /* bump and prepare sequence number */
0691     ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu", ++uevent_seqnum);
0692     if (ret < 0 || (size_t)ret >= sizeof(buf))
0693         return -ENOMEM;
0694     ret++;
0695 
0696     /* verify message does not overflow */
0697     if ((skb->len + ret) > UEVENT_BUFFER_SIZE) {
0698         NL_SET_ERR_MSG(extack, "uevent message too big");
0699         return -EINVAL;
0700     }
0701 
0702     /* copy skb and extend to accommodate sequence number */
0703     skbc = skb_copy_expand(skb, 0, ret, GFP_KERNEL);
0704     if (!skbc)
0705         return -ENOMEM;
0706 
0707     /* append sequence number */
0708     skb_put_data(skbc, buf, ret);
0709 
0710     /* remove msg header */
0711     skb_pull(skbc, NLMSG_HDRLEN);
0712 
0713     /* set portid 0 to inform userspace message comes from kernel */
0714     NETLINK_CB(skbc).portid = 0;
0715     NETLINK_CB(skbc).dst_group = 1;
0716 
0717     ret = netlink_broadcast(usk, skbc, 0, 1, GFP_KERNEL);
0718     /* ENOBUFS should be handled in userspace */
0719     if (ret == -ENOBUFS || ret == -ESRCH)
0720         ret = 0;
0721 
0722     return ret;
0723 }
0724 
0725 static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
0726                   struct netlink_ext_ack *extack)
0727 {
0728     struct net *net;
0729     int ret;
0730 
0731     if (!nlmsg_data(nlh))
0732         return -EINVAL;
0733 
0734     /*
0735      * Verify that we are allowed to send messages to the target
0736      * network namespace. The caller must have CAP_SYS_ADMIN in the
0737      * owning user namespace of the target network namespace.
0738      */
0739     net = sock_net(NETLINK_CB(skb).sk);
0740     if (!netlink_ns_capable(skb, net->user_ns, CAP_SYS_ADMIN)) {
0741         NL_SET_ERR_MSG(extack, "missing CAP_SYS_ADMIN capability");
0742         return -EPERM;
0743     }
0744 
0745     mutex_lock(&uevent_sock_mutex);
0746     ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack);
0747     mutex_unlock(&uevent_sock_mutex);
0748 
0749     return ret;
0750 }
0751 
0752 static void uevent_net_rcv(struct sk_buff *skb)
0753 {
0754     netlink_rcv_skb(skb, &uevent_net_rcv_skb);
0755 }
0756 
0757 static int uevent_net_init(struct net *net)
0758 {
0759     struct uevent_sock *ue_sk;
0760     struct netlink_kernel_cfg cfg = {
0761         .groups = 1,
0762         .input = uevent_net_rcv,
0763         .flags  = NL_CFG_F_NONROOT_RECV
0764     };
0765 
0766     ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
0767     if (!ue_sk)
0768         return -ENOMEM;
0769 
0770     ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
0771     if (!ue_sk->sk) {
0772         pr_err("kobject_uevent: unable to create netlink socket!\n");
0773         kfree(ue_sk);
0774         return -ENODEV;
0775     }
0776 
0777     net->uevent_sock = ue_sk;
0778 
0779     /* Restrict uevents to initial user namespace. */
0780     if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
0781         mutex_lock(&uevent_sock_mutex);
0782         list_add_tail(&ue_sk->list, &uevent_sock_list);
0783         mutex_unlock(&uevent_sock_mutex);
0784     }
0785 
0786     return 0;
0787 }
0788 
0789 static void uevent_net_exit(struct net *net)
0790 {
0791     struct uevent_sock *ue_sk = net->uevent_sock;
0792 
0793     if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
0794         mutex_lock(&uevent_sock_mutex);
0795         list_del(&ue_sk->list);
0796         mutex_unlock(&uevent_sock_mutex);
0797     }
0798 
0799     netlink_kernel_release(ue_sk->sk);
0800     kfree(ue_sk);
0801 }
0802 
0803 static struct pernet_operations uevent_net_ops = {
0804     .init   = uevent_net_init,
0805     .exit   = uevent_net_exit,
0806 };
0807 
0808 static int __init kobject_uevent_init(void)
0809 {
0810     return register_pernet_subsys(&uevent_net_ops);
0811 }
0812 
0813 
0814 postcore_initcall(kobject_uevent_init);
0815 #endif