kobject_uevent.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // 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. #include <linux/spinlock.h>
  16. #include <linux/string.h>
  17. #include <linux/kobject.h>
  18. #include <linux/export.h>
  19. #include <linux/kmod.h>
  20. #include <linux/slab.h>
  21. #include <linux/socket.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/netlink.h>
  24. #include <linux/uuid.h>
  25. #include <linux/ctype.h>
  26. #include <net/sock.h>
  27. #include <net/net_namespace.h>
  28. u64 uevent_seqnum;
  29. #ifdef CONFIG_UEVENT_HELPER
  30. char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
  31. #endif
  32. #ifdef CONFIG_NET
  33. struct uevent_sock {
  34. struct list_head list;
  35. struct sock *sk;
  36. };
  37. static LIST_HEAD(uevent_sock_list);
  38. #endif
  39. /* This lock protects uevent_seqnum and uevent_sock_list */
  40. static DEFINE_MUTEX(uevent_sock_mutex);
  41. /* the strings here must match the enum in include/linux/kobject.h */
  42. static const char *kobject_actions[] = {
  43. [KOBJ_ADD] = "add",
  44. [KOBJ_REMOVE] = "remove",
  45. [KOBJ_CHANGE] = "change",
  46. [KOBJ_MOVE] = "move",
  47. [KOBJ_ONLINE] = "online",
  48. [KOBJ_OFFLINE] = "offline",
  49. [KOBJ_BIND] = "bind",
  50. [KOBJ_UNBIND] = "unbind",
  51. };
  52. static int kobject_action_type(const char *buf, size_t count,
  53. enum kobject_action *type,
  54. const char **args)
  55. {
  56. enum kobject_action action;
  57. size_t count_first;
  58. const char *args_start;
  59. int ret = -EINVAL;
  60. if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
  61. count--;
  62. if (!count)
  63. goto out;
  64. args_start = strnchr(buf, count, ' ');
  65. if (args_start) {
  66. count_first = args_start - buf;
  67. args_start = args_start + 1;
  68. } else
  69. count_first = count;
  70. for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
  71. if (strncmp(kobject_actions[action], buf, count_first) != 0)
  72. continue;
  73. if (kobject_actions[action][count_first] != '\0')
  74. continue;
  75. if (args)
  76. *args = args_start;
  77. *type = action;
  78. ret = 0;
  79. break;
  80. }
  81. out:
  82. return ret;
  83. }
  84. static const char *action_arg_word_end(const char *buf, const char *buf_end,
  85. char delim)
  86. {
  87. const char *next = buf;
  88. while (next <= buf_end && *next != delim)
  89. if (!isalnum(*next++))
  90. return NULL;
  91. if (next == buf)
  92. return NULL;
  93. return next;
  94. }
  95. static int kobject_action_args(const char *buf, size_t count,
  96. struct kobj_uevent_env **ret_env)
  97. {
  98. struct kobj_uevent_env *env = NULL;
  99. const char *next, *buf_end, *key;
  100. int key_len;
  101. int r = -EINVAL;
  102. if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
  103. count--;
  104. if (!count)
  105. return -EINVAL;
  106. env = kzalloc(sizeof(*env), GFP_KERNEL);
  107. if (!env)
  108. return -ENOMEM;
  109. /* first arg is UUID */
  110. if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
  111. add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
  112. goto out;
  113. /*
  114. * the rest are custom environment variables in KEY=VALUE
  115. * format with ' ' delimiter between each KEY=VALUE pair
  116. */
  117. next = buf + UUID_STRING_LEN;
  118. buf_end = buf + count - 1;
  119. while (next <= buf_end) {
  120. if (*next != ' ')
  121. goto out;
  122. /* skip the ' ', key must follow */
  123. key = ++next;
  124. if (key > buf_end)
  125. goto out;
  126. buf = next;
  127. next = action_arg_word_end(buf, buf_end, '=');
  128. if (!next || next > buf_end || *next != '=')
  129. goto out;
  130. key_len = next - buf;
  131. /* skip the '=', value must follow */
  132. if (++next > buf_end)
  133. goto out;
  134. buf = next;
  135. next = action_arg_word_end(buf, buf_end, ' ');
  136. if (!next)
  137. goto out;
  138. if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
  139. key_len, key, (int) (next - buf), buf))
  140. goto out;
  141. }
  142. r = 0;
  143. out:
  144. if (r)
  145. kfree(env);
  146. else
  147. *ret_env = env;
  148. return r;
  149. }
  150. /**
  151. * kobject_synth_uevent - send synthetic uevent with arguments
  152. *
  153. * @kobj: struct kobject for which synthetic uevent is to be generated
  154. * @buf: buffer containing action type and action args, newline is ignored
  155. * @count: length of buffer
  156. *
  157. * Returns 0 if kobject_synthetic_uevent() is completed with success or the
  158. * corresponding error when it fails.
  159. */
  160. int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
  161. {
  162. char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
  163. enum kobject_action action;
  164. const char *action_args;
  165. struct kobj_uevent_env *env;
  166. const char *msg = NULL, *devpath;
  167. int r;
  168. r = kobject_action_type(buf, count, &action, &action_args);
  169. if (r) {
  170. msg = "unknown uevent action string\n";
  171. goto out;
  172. }
  173. if (!action_args) {
  174. r = kobject_uevent_env(kobj, action, no_uuid_envp);
  175. goto out;
  176. }
  177. r = kobject_action_args(action_args,
  178. count - (action_args - buf), &env);
  179. if (r == -EINVAL) {
  180. msg = "incorrect uevent action arguments\n";
  181. goto out;
  182. }
  183. if (r)
  184. goto out;
  185. r = kobject_uevent_env(kobj, action, env->envp);
  186. kfree(env);
  187. out:
  188. if (r) {
  189. devpath = kobject_get_path(kobj, GFP_KERNEL);
  190. printk(KERN_WARNING "synth uevent: %s: %s",
  191. devpath ?: "unknown device",
  192. msg ?: "failed to send uevent");
  193. kfree(devpath);
  194. }
  195. return r;
  196. }
  197. #ifdef CONFIG_NET
  198. static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
  199. {
  200. struct kobject *kobj = data, *ksobj;
  201. const struct kobj_ns_type_operations *ops;
  202. ops = kobj_ns_ops(kobj);
  203. if (!ops && kobj->kset) {
  204. ksobj = &kobj->kset->kobj;
  205. if (ksobj->parent != NULL)
  206. ops = kobj_ns_ops(ksobj->parent);
  207. }
  208. if (ops && ops->netlink_ns && kobj->ktype->namespace) {
  209. const void *sock_ns, *ns;
  210. ns = kobj->ktype->namespace(kobj);
  211. sock_ns = ops->netlink_ns(dsk);
  212. return sock_ns != ns;
  213. }
  214. return 0;
  215. }
  216. #endif
  217. #ifdef CONFIG_UEVENT_HELPER
  218. static int kobj_usermode_filter(struct kobject *kobj)
  219. {
  220. const struct kobj_ns_type_operations *ops;
  221. ops = kobj_ns_ops(kobj);
  222. if (ops) {
  223. const void *init_ns, *ns;
  224. ns = kobj->ktype->namespace(kobj);
  225. init_ns = ops->initial_ns();
  226. return ns != init_ns;
  227. }
  228. return 0;
  229. }
  230. static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
  231. {
  232. int len;
  233. len = strlcpy(&env->buf[env->buflen], subsystem,
  234. sizeof(env->buf) - env->buflen);
  235. if (len >= (sizeof(env->buf) - env->buflen)) {
  236. WARN(1, KERN_ERR "init_uevent_argv: buffer size too small\n");
  237. return -ENOMEM;
  238. }
  239. env->argv[0] = uevent_helper;
  240. env->argv[1] = &env->buf[env->buflen];
  241. env->argv[2] = NULL;
  242. env->buflen += len + 1;
  243. return 0;
  244. }
  245. static void cleanup_uevent_env(struct subprocess_info *info)
  246. {
  247. kfree(info->data);
  248. }
  249. #endif
  250. static int kobject_uevent_net_broadcast(struct kobject *kobj,
  251. struct kobj_uevent_env *env,
  252. const char *action_string,
  253. const char *devpath)
  254. {
  255. int retval = 0;
  256. #if defined(CONFIG_NET)
  257. struct sk_buff *skb = NULL;
  258. struct uevent_sock *ue_sk;
  259. /* send netlink message */
  260. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  261. struct sock *uevent_sock = ue_sk->sk;
  262. if (!netlink_has_listeners(uevent_sock, 1))
  263. continue;
  264. if (!skb) {
  265. /* allocate message with the maximum possible size */
  266. size_t len = strlen(action_string) + strlen(devpath) + 2;
  267. char *scratch;
  268. retval = -ENOMEM;
  269. skb = alloc_skb(len + env->buflen, GFP_KERNEL);
  270. if (!skb)
  271. continue;
  272. /* add header */
  273. scratch = skb_put(skb, len);
  274. sprintf(scratch, "%s@%s", action_string, devpath);
  275. skb_put_data(skb, env->buf, env->buflen);
  276. NETLINK_CB(skb).dst_group = 1;
  277. }
  278. retval = netlink_broadcast_filtered(uevent_sock, skb_get(skb),
  279. 0, 1, GFP_KERNEL,
  280. kobj_bcast_filter,
  281. kobj);
  282. /* ENOBUFS should be handled in userspace */
  283. if (retval == -ENOBUFS || retval == -ESRCH)
  284. retval = 0;
  285. }
  286. consume_skb(skb);
  287. #endif
  288. return retval;
  289. }
  290. static void zap_modalias_env(struct kobj_uevent_env *env)
  291. {
  292. static const char modalias_prefix[] = "MODALIAS=";
  293. int i;
  294. for (i = 0; i < env->envp_idx;) {
  295. if (strncmp(env->envp[i], modalias_prefix,
  296. sizeof(modalias_prefix) - 1)) {
  297. i++;
  298. continue;
  299. }
  300. if (i != env->envp_idx - 1)
  301. memmove(&env->envp[i], &env->envp[i + 1],
  302. sizeof(env->envp[i]) * env->envp_idx - 1);
  303. env->envp_idx--;
  304. }
  305. }
  306. /**
  307. * kobject_uevent_env - send an uevent with environmental data
  308. *
  309. * @kobj: struct kobject that the action is happening to
  310. * @action: action that is happening
  311. * @envp_ext: pointer to environmental data
  312. *
  313. * Returns 0 if kobject_uevent_env() is completed with success or the
  314. * corresponding error when it fails.
  315. */
  316. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  317. char *envp_ext[])
  318. {
  319. struct kobj_uevent_env *env;
  320. const char *action_string = kobject_actions[action];
  321. const char *devpath = NULL;
  322. const char *subsystem;
  323. struct kobject *top_kobj;
  324. struct kset *kset;
  325. const struct kset_uevent_ops *uevent_ops;
  326. int i = 0;
  327. int retval = 0;
  328. pr_debug("kobject: '%s' (%p): %s\n",
  329. kobject_name(kobj), kobj, __func__);
  330. /* search the kset we belong to */
  331. top_kobj = kobj;
  332. while (!top_kobj->kset && top_kobj->parent)
  333. top_kobj = top_kobj->parent;
  334. if (!top_kobj->kset) {
  335. pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
  336. "without kset!\n", kobject_name(kobj), kobj,
  337. __func__);
  338. return -EINVAL;
  339. }
  340. kset = top_kobj->kset;
  341. uevent_ops = kset->uevent_ops;
  342. /* skip the event, if uevent_suppress is set*/
  343. if (kobj->uevent_suppress) {
  344. pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
  345. "caused the event to drop!\n",
  346. kobject_name(kobj), kobj, __func__);
  347. return 0;
  348. }
  349. /* skip the event, if the filter returns zero. */
  350. if (uevent_ops && uevent_ops->filter)
  351. if (!uevent_ops->filter(kset, kobj)) {
  352. pr_debug("kobject: '%s' (%p): %s: filter function "
  353. "caused the event to drop!\n",
  354. kobject_name(kobj), kobj, __func__);
  355. return 0;
  356. }
  357. /* originating subsystem */
  358. if (uevent_ops && uevent_ops->name)
  359. subsystem = uevent_ops->name(kset, kobj);
  360. else
  361. subsystem = kobject_name(&kset->kobj);
  362. if (!subsystem) {
  363. pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
  364. "event to drop!\n", kobject_name(kobj), kobj,
  365. __func__);
  366. return 0;
  367. }
  368. /* environment buffer */
  369. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  370. if (!env)
  371. return -ENOMEM;
  372. /* complete object path */
  373. devpath = kobject_get_path(kobj, GFP_KERNEL);
  374. if (!devpath) {
  375. retval = -ENOENT;
  376. goto exit;
  377. }
  378. /* default keys */
  379. retval = add_uevent_var(env, "ACTION=%s", action_string);
  380. if (retval)
  381. goto exit;
  382. retval = add_uevent_var(env, "DEVPATH=%s", devpath);
  383. if (retval)
  384. goto exit;
  385. retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
  386. if (retval)
  387. goto exit;
  388. /* keys passed in from the caller */
  389. if (envp_ext) {
  390. for (i = 0; envp_ext[i]; i++) {
  391. retval = add_uevent_var(env, "%s", envp_ext[i]);
  392. if (retval)
  393. goto exit;
  394. }
  395. }
  396. /* let the kset specific function add its stuff */
  397. if (uevent_ops && uevent_ops->uevent) {
  398. retval = uevent_ops->uevent(kset, kobj, env);
  399. if (retval) {
  400. pr_debug("kobject: '%s' (%p): %s: uevent() returned "
  401. "%d\n", kobject_name(kobj), kobj,
  402. __func__, retval);
  403. goto exit;
  404. }
  405. }
  406. switch (action) {
  407. case KOBJ_ADD:
  408. /*
  409. * Mark "add" event so we can make sure we deliver "remove"
  410. * event to userspace during automatic cleanup. If
  411. * the object did send an "add" event, "remove" will
  412. * automatically generated by the core, if not already done
  413. * by the caller.
  414. */
  415. kobj->state_add_uevent_sent = 1;
  416. break;
  417. case KOBJ_REMOVE:
  418. kobj->state_remove_uevent_sent = 1;
  419. break;
  420. case KOBJ_UNBIND:
  421. zap_modalias_env(env);
  422. break;
  423. default:
  424. break;
  425. }
  426. mutex_lock(&uevent_sock_mutex);
  427. /* we will send an event, so request a new sequence number */
  428. retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
  429. if (retval) {
  430. mutex_unlock(&uevent_sock_mutex);
  431. goto exit;
  432. }
  433. retval = kobject_uevent_net_broadcast(kobj, env, action_string,
  434. devpath);
  435. mutex_unlock(&uevent_sock_mutex);
  436. #ifdef CONFIG_UEVENT_HELPER
  437. /* call uevent_helper, usually only enabled during early boot */
  438. if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
  439. struct subprocess_info *info;
  440. retval = add_uevent_var(env, "HOME=/");
  441. if (retval)
  442. goto exit;
  443. retval = add_uevent_var(env,
  444. "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  445. if (retval)
  446. goto exit;
  447. retval = init_uevent_argv(env, subsystem);
  448. if (retval)
  449. goto exit;
  450. retval = -ENOMEM;
  451. info = call_usermodehelper_setup(env->argv[0], env->argv,
  452. env->envp, GFP_KERNEL,
  453. NULL, cleanup_uevent_env, env);
  454. if (info) {
  455. retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
  456. env = NULL; /* freed by cleanup_uevent_env */
  457. }
  458. }
  459. #endif
  460. exit:
  461. kfree(devpath);
  462. kfree(env);
  463. return retval;
  464. }
  465. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  466. /**
  467. * kobject_uevent - notify userspace by sending an uevent
  468. *
  469. * @kobj: struct kobject that the action is happening to
  470. * @action: action that is happening
  471. *
  472. * Returns 0 if kobject_uevent() is completed with success or the
  473. * corresponding error when it fails.
  474. */
  475. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  476. {
  477. return kobject_uevent_env(kobj, action, NULL);
  478. }
  479. EXPORT_SYMBOL_GPL(kobject_uevent);
  480. /**
  481. * add_uevent_var - add key value string to the environment buffer
  482. * @env: environment buffer structure
  483. * @format: printf format for the key=value pair
  484. *
  485. * Returns 0 if environment variable was added successfully or -ENOMEM
  486. * if no space was available.
  487. */
  488. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  489. {
  490. va_list args;
  491. int len;
  492. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  493. WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
  494. return -ENOMEM;
  495. }
  496. va_start(args, format);
  497. len = vsnprintf(&env->buf[env->buflen],
  498. sizeof(env->buf) - env->buflen,
  499. format, args);
  500. va_end(args);
  501. if (len >= (sizeof(env->buf) - env->buflen)) {
  502. WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
  503. return -ENOMEM;
  504. }
  505. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  506. env->buflen += len + 1;
  507. return 0;
  508. }
  509. EXPORT_SYMBOL_GPL(add_uevent_var);
  510. #if defined(CONFIG_NET)
  511. static int uevent_net_init(struct net *net)
  512. {
  513. struct uevent_sock *ue_sk;
  514. struct netlink_kernel_cfg cfg = {
  515. .groups = 1,
  516. .flags = NL_CFG_F_NONROOT_RECV,
  517. };
  518. ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
  519. if (!ue_sk)
  520. return -ENOMEM;
  521. ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
  522. if (!ue_sk->sk) {
  523. printk(KERN_ERR
  524. "kobject_uevent: unable to create netlink socket!\n");
  525. kfree(ue_sk);
  526. return -ENODEV;
  527. }
  528. mutex_lock(&uevent_sock_mutex);
  529. list_add_tail(&ue_sk->list, &uevent_sock_list);
  530. mutex_unlock(&uevent_sock_mutex);
  531. return 0;
  532. }
  533. static void uevent_net_exit(struct net *net)
  534. {
  535. struct uevent_sock *ue_sk;
  536. mutex_lock(&uevent_sock_mutex);
  537. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  538. if (sock_net(ue_sk->sk) == net)
  539. goto found;
  540. }
  541. mutex_unlock(&uevent_sock_mutex);
  542. return;
  543. found:
  544. list_del(&ue_sk->list);
  545. mutex_unlock(&uevent_sock_mutex);
  546. netlink_kernel_release(ue_sk->sk);
  547. kfree(ue_sk);
  548. }
  549. static struct pernet_operations uevent_net_ops = {
  550. .init = uevent_net_init,
  551. .exit = uevent_net_exit,
  552. };
  553. static int __init kobject_uevent_init(void)
  554. {
  555. return register_pernet_subsys(&uevent_net_ops);
  556. }
  557. postcore_initcall(kobject_uevent_init);
  558. #endif