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