kobject_uevent.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. u64 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. #endif
  41. /* This lock protects uevent_seqnum and uevent_sock_list */
  42. static DEFINE_MUTEX(uevent_sock_mutex);
  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(sizeof(*env), GFP_KERNEL);
  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 void *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 = strlcpy(&env->buf[env->buflen], subsystem, buffer_size);
  217. if (len >= buffer_size) {
  218. pr_warn("init_uevent_argv: buffer size of %d too small, needed %d\n",
  219. buffer_size, len);
  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. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  267. struct sock *uevent_sock = ue_sk->sk;
  268. if (!netlink_has_listeners(uevent_sock, 1))
  269. continue;
  270. if (!skb) {
  271. retval = -ENOMEM;
  272. skb = alloc_uevent_skb(env, action_string, devpath);
  273. if (!skb)
  274. continue;
  275. }
  276. retval = netlink_broadcast(uevent_sock, skb_get(skb), 0, 1,
  277. GFP_KERNEL);
  278. /* ENOBUFS should be handled in userspace */
  279. if (retval == -ENOBUFS || retval == -ESRCH)
  280. retval = 0;
  281. }
  282. consume_skb(skb);
  283. return retval;
  284. }
  285. static int uevent_net_broadcast_tagged(struct sock *usk,
  286. struct kobj_uevent_env *env,
  287. const char *action_string,
  288. const char *devpath)
  289. {
  290. struct user_namespace *owning_user_ns = sock_net(usk)->user_ns;
  291. struct sk_buff *skb = NULL;
  292. int ret = 0;
  293. skb = alloc_uevent_skb(env, action_string, devpath);
  294. if (!skb)
  295. return -ENOMEM;
  296. /* fix credentials */
  297. if (owning_user_ns != &init_user_ns) {
  298. struct netlink_skb_parms *parms = &NETLINK_CB(skb);
  299. kuid_t root_uid;
  300. kgid_t root_gid;
  301. /* fix uid */
  302. root_uid = make_kuid(owning_user_ns, 0);
  303. if (uid_valid(root_uid))
  304. parms->creds.uid = root_uid;
  305. /* fix gid */
  306. root_gid = make_kgid(owning_user_ns, 0);
  307. if (gid_valid(root_gid))
  308. parms->creds.gid = root_gid;
  309. }
  310. ret = netlink_broadcast(usk, skb, 0, 1, GFP_KERNEL);
  311. /* ENOBUFS should be handled in userspace */
  312. if (ret == -ENOBUFS || ret == -ESRCH)
  313. ret = 0;
  314. return ret;
  315. }
  316. #endif
  317. static int kobject_uevent_net_broadcast(struct kobject *kobj,
  318. struct kobj_uevent_env *env,
  319. const char *action_string,
  320. const char *devpath)
  321. {
  322. int ret = 0;
  323. #ifdef CONFIG_NET
  324. const struct kobj_ns_type_operations *ops;
  325. const struct net *net = NULL;
  326. ops = kobj_ns_ops(kobj);
  327. if (!ops && kobj->kset) {
  328. struct kobject *ksobj = &kobj->kset->kobj;
  329. if (ksobj->parent != NULL)
  330. ops = kobj_ns_ops(ksobj->parent);
  331. }
  332. /* kobjects currently only carry network namespace tags and they
  333. * are the only tag relevant here since we want to decide which
  334. * network namespaces to broadcast the uevent into.
  335. */
  336. if (ops && ops->netlink_ns && kobj->ktype->namespace)
  337. if (ops->type == KOBJ_NS_TYPE_NET)
  338. net = kobj->ktype->namespace(kobj);
  339. if (!net)
  340. ret = uevent_net_broadcast_untagged(env, action_string,
  341. devpath);
  342. else
  343. ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
  344. action_string, devpath);
  345. #endif
  346. return ret;
  347. }
  348. static void zap_modalias_env(struct kobj_uevent_env *env)
  349. {
  350. static const char modalias_prefix[] = "MODALIAS=";
  351. size_t len;
  352. int i, j;
  353. for (i = 0; i < env->envp_idx;) {
  354. if (strncmp(env->envp[i], modalias_prefix,
  355. sizeof(modalias_prefix) - 1)) {
  356. i++;
  357. continue;
  358. }
  359. len = strlen(env->envp[i]) + 1;
  360. if (i != env->envp_idx - 1) {
  361. memmove(env->envp[i], env->envp[i + 1],
  362. env->buflen - len);
  363. for (j = i; j < env->envp_idx - 1; j++)
  364. env->envp[j] = env->envp[j + 1] - len;
  365. }
  366. env->envp_idx--;
  367. env->buflen -= len;
  368. }
  369. }
  370. /**
  371. * kobject_uevent_env - send an uevent with environmental data
  372. *
  373. * @kobj: struct kobject that the action is happening to
  374. * @action: action that is happening
  375. * @envp_ext: pointer to environmental data
  376. *
  377. * Returns 0 if kobject_uevent_env() is completed with success or the
  378. * corresponding error when it fails.
  379. */
  380. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  381. char *envp_ext[])
  382. {
  383. struct kobj_uevent_env *env;
  384. const char *action_string = kobject_actions[action];
  385. const char *devpath = NULL;
  386. const char *subsystem;
  387. struct kobject *top_kobj;
  388. struct kset *kset;
  389. const struct kset_uevent_ops *uevent_ops;
  390. int i = 0;
  391. int retval = 0;
  392. /*
  393. * Mark "remove" event done regardless of result, for some subsystems
  394. * do not want to re-trigger "remove" event via automatic cleanup.
  395. */
  396. if (action == KOBJ_REMOVE)
  397. kobj->state_remove_uevent_sent = 1;
  398. pr_debug("kobject: '%s' (%p): %s\n",
  399. kobject_name(kobj), kobj, __func__);
  400. /* search the kset we belong to */
  401. top_kobj = kobj;
  402. while (!top_kobj->kset && top_kobj->parent)
  403. top_kobj = top_kobj->parent;
  404. if (!top_kobj->kset) {
  405. pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
  406. "without kset!\n", kobject_name(kobj), kobj,
  407. __func__);
  408. return -EINVAL;
  409. }
  410. kset = top_kobj->kset;
  411. uevent_ops = kset->uevent_ops;
  412. /* skip the event, if uevent_suppress is set*/
  413. if (kobj->uevent_suppress) {
  414. pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
  415. "caused the event to drop!\n",
  416. kobject_name(kobj), kobj, __func__);
  417. return 0;
  418. }
  419. /* skip the event, if the filter returns zero. */
  420. if (uevent_ops && uevent_ops->filter)
  421. if (!uevent_ops->filter(kset, kobj)) {
  422. pr_debug("kobject: '%s' (%p): %s: filter function "
  423. "caused the event to drop!\n",
  424. kobject_name(kobj), kobj, __func__);
  425. return 0;
  426. }
  427. /* originating subsystem */
  428. if (uevent_ops && uevent_ops->name)
  429. subsystem = uevent_ops->name(kset, kobj);
  430. else
  431. subsystem = kobject_name(&kset->kobj);
  432. if (!subsystem) {
  433. pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
  434. "event to drop!\n", kobject_name(kobj), kobj,
  435. __func__);
  436. return 0;
  437. }
  438. /* environment buffer */
  439. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  440. if (!env)
  441. return -ENOMEM;
  442. /* complete object path */
  443. devpath = kobject_get_path(kobj, GFP_KERNEL);
  444. if (!devpath) {
  445. retval = -ENOENT;
  446. goto exit;
  447. }
  448. /* default keys */
  449. retval = add_uevent_var(env, "ACTION=%s", action_string);
  450. if (retval)
  451. goto exit;
  452. retval = add_uevent_var(env, "DEVPATH=%s", devpath);
  453. if (retval)
  454. goto exit;
  455. retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
  456. if (retval)
  457. goto exit;
  458. /* keys passed in from the caller */
  459. if (envp_ext) {
  460. for (i = 0; envp_ext[i]; i++) {
  461. retval = add_uevent_var(env, "%s", envp_ext[i]);
  462. if (retval)
  463. goto exit;
  464. }
  465. }
  466. /* let the kset specific function add its stuff */
  467. if (uevent_ops && uevent_ops->uevent) {
  468. retval = uevent_ops->uevent(kset, kobj, env);
  469. if (retval) {
  470. pr_debug("kobject: '%s' (%p): %s: uevent() returned "
  471. "%d\n", kobject_name(kobj), kobj,
  472. __func__, retval);
  473. goto exit;
  474. }
  475. }
  476. switch (action) {
  477. case KOBJ_ADD:
  478. /*
  479. * Mark "add" event so we can make sure we deliver "remove"
  480. * event to userspace during automatic cleanup. If
  481. * the object did send an "add" event, "remove" will
  482. * automatically generated by the core, if not already done
  483. * by the caller.
  484. */
  485. kobj->state_add_uevent_sent = 1;
  486. break;
  487. case KOBJ_UNBIND:
  488. zap_modalias_env(env);
  489. break;
  490. default:
  491. break;
  492. }
  493. mutex_lock(&uevent_sock_mutex);
  494. /* we will send an event, so request a new sequence number */
  495. retval = add_uevent_var(env, "SEQNUM=%llu", ++uevent_seqnum);
  496. if (retval) {
  497. mutex_unlock(&uevent_sock_mutex);
  498. goto exit;
  499. }
  500. retval = kobject_uevent_net_broadcast(kobj, env, action_string,
  501. devpath);
  502. mutex_unlock(&uevent_sock_mutex);
  503. #ifdef CONFIG_UEVENT_HELPER
  504. /* call uevent_helper, usually only enabled during early boot */
  505. if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
  506. struct subprocess_info *info;
  507. retval = add_uevent_var(env, "HOME=/");
  508. if (retval)
  509. goto exit;
  510. retval = add_uevent_var(env,
  511. "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  512. if (retval)
  513. goto exit;
  514. retval = init_uevent_argv(env, subsystem);
  515. if (retval)
  516. goto exit;
  517. retval = -ENOMEM;
  518. info = call_usermodehelper_setup(env->argv[0], env->argv,
  519. env->envp, GFP_KERNEL,
  520. NULL, cleanup_uevent_env, env);
  521. if (info) {
  522. retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
  523. env = NULL; /* freed by cleanup_uevent_env */
  524. }
  525. }
  526. #endif
  527. exit:
  528. kfree(devpath);
  529. kfree(env);
  530. return retval;
  531. }
  532. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  533. /**
  534. * kobject_uevent - notify userspace by sending an uevent
  535. *
  536. * @kobj: struct kobject that the action is happening to
  537. * @action: action that is happening
  538. *
  539. * Returns 0 if kobject_uevent() is completed with success or the
  540. * corresponding error when it fails.
  541. */
  542. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  543. {
  544. return kobject_uevent_env(kobj, action, NULL);
  545. }
  546. EXPORT_SYMBOL_GPL(kobject_uevent);
  547. /**
  548. * add_uevent_var - add key value string to the environment buffer
  549. * @env: environment buffer structure
  550. * @format: printf format for the key=value pair
  551. *
  552. * Returns 0 if environment variable was added successfully or -ENOMEM
  553. * if no space was available.
  554. */
  555. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  556. {
  557. va_list args;
  558. int len;
  559. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  560. WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
  561. return -ENOMEM;
  562. }
  563. va_start(args, format);
  564. len = vsnprintf(&env->buf[env->buflen],
  565. sizeof(env->buf) - env->buflen,
  566. format, args);
  567. va_end(args);
  568. if (len >= (sizeof(env->buf) - env->buflen)) {
  569. WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
  570. return -ENOMEM;
  571. }
  572. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  573. env->buflen += len + 1;
  574. return 0;
  575. }
  576. EXPORT_SYMBOL_GPL(add_uevent_var);
  577. #if defined(CONFIG_NET)
  578. static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb,
  579. struct netlink_ext_ack *extack)
  580. {
  581. /* u64 to chars: 2^64 - 1 = 21 chars */
  582. char buf[sizeof("SEQNUM=") + 21];
  583. struct sk_buff *skbc;
  584. int ret;
  585. /* bump and prepare sequence number */
  586. ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu", ++uevent_seqnum);
  587. if (ret < 0 || (size_t)ret >= sizeof(buf))
  588. return -ENOMEM;
  589. ret++;
  590. /* verify message does not overflow */
  591. if ((skb->len + ret) > UEVENT_BUFFER_SIZE) {
  592. NL_SET_ERR_MSG(extack, "uevent message too big");
  593. return -EINVAL;
  594. }
  595. /* copy skb and extend to accommodate sequence number */
  596. skbc = skb_copy_expand(skb, 0, ret, GFP_KERNEL);
  597. if (!skbc)
  598. return -ENOMEM;
  599. /* append sequence number */
  600. skb_put_data(skbc, buf, ret);
  601. /* remove msg header */
  602. skb_pull(skbc, NLMSG_HDRLEN);
  603. /* set portid 0 to inform userspace message comes from kernel */
  604. NETLINK_CB(skbc).portid = 0;
  605. NETLINK_CB(skbc).dst_group = 1;
  606. ret = netlink_broadcast(usk, skbc, 0, 1, GFP_KERNEL);
  607. /* ENOBUFS should be handled in userspace */
  608. if (ret == -ENOBUFS || ret == -ESRCH)
  609. ret = 0;
  610. return ret;
  611. }
  612. static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
  613. struct netlink_ext_ack *extack)
  614. {
  615. struct net *net;
  616. int ret;
  617. if (!nlmsg_data(nlh))
  618. return -EINVAL;
  619. /*
  620. * Verify that we are allowed to send messages to the target
  621. * network namespace. The caller must have CAP_SYS_ADMIN in the
  622. * owning user namespace of the target network namespace.
  623. */
  624. net = sock_net(NETLINK_CB(skb).sk);
  625. if (!netlink_ns_capable(skb, net->user_ns, CAP_SYS_ADMIN)) {
  626. NL_SET_ERR_MSG(extack, "missing CAP_SYS_ADMIN capability");
  627. return -EPERM;
  628. }
  629. mutex_lock(&uevent_sock_mutex);
  630. ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack);
  631. mutex_unlock(&uevent_sock_mutex);
  632. return ret;
  633. }
  634. static void uevent_net_rcv(struct sk_buff *skb)
  635. {
  636. netlink_rcv_skb(skb, &uevent_net_rcv_skb);
  637. }
  638. static int uevent_net_init(struct net *net)
  639. {
  640. struct uevent_sock *ue_sk;
  641. struct netlink_kernel_cfg cfg = {
  642. .groups = 1,
  643. .input = uevent_net_rcv,
  644. .flags = NL_CFG_F_NONROOT_RECV
  645. };
  646. ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
  647. if (!ue_sk)
  648. return -ENOMEM;
  649. ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
  650. if (!ue_sk->sk) {
  651. pr_err("kobject_uevent: unable to create netlink socket!\n");
  652. kfree(ue_sk);
  653. return -ENODEV;
  654. }
  655. net->uevent_sock = ue_sk;
  656. /* Restrict uevents to initial user namespace. */
  657. if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
  658. mutex_lock(&uevent_sock_mutex);
  659. list_add_tail(&ue_sk->list, &uevent_sock_list);
  660. mutex_unlock(&uevent_sock_mutex);
  661. }
  662. return 0;
  663. }
  664. static void uevent_net_exit(struct net *net)
  665. {
  666. struct uevent_sock *ue_sk = net->uevent_sock;
  667. if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
  668. mutex_lock(&uevent_sock_mutex);
  669. list_del(&ue_sk->list);
  670. mutex_unlock(&uevent_sock_mutex);
  671. }
  672. netlink_kernel_release(ue_sk->sk);
  673. kfree(ue_sk);
  674. }
  675. static struct pernet_operations uevent_net_ops = {
  676. .init = uevent_net_init,
  677. .exit = uevent_net_exit,
  678. };
  679. static int __init kobject_uevent_init(void)
  680. {
  681. return register_pernet_subsys(&uevent_net_ops);
  682. }
  683. postcore_initcall(kobject_uevent_init);
  684. #endif