kobject_uevent.c 20 KB

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