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