1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/filter.h>
5 #include <linux/netlink.h>
6 #include <sys/socket.h>
11 #include "MurmurHash2.h"
12 #include "alloc-util.h"
13 #include "device-monitor-private.h"
14 #include "device-private.h"
15 #include "device-util.h"
17 #include "format-util.h"
21 #include "mountpoint-util.h"
23 #include "socket-util.h"
24 #include "string-util.h"
27 struct sd_device_monitor
{
31 union sockaddr_union snl
;
32 union sockaddr_union snl_trusted_sender
;
35 Hashmap
*subsystem_filter
;
40 sd_event_source
*event_source
;
41 sd_device_monitor_handler_t callback
;
45 #define UDEV_MONITOR_MAGIC 0xfeedcafe
47 typedef struct monitor_netlink_header
{
48 /* "libudev" prefix to distinguish libudev and kernel messages */
50 /* Magic to protect against daemon <-> Library message format mismatch
51 * Used in the kernel from socket filter rules; needs to be stored in network order */
53 /* Total length of header structure known to the sender */
55 /* Properties string buffer */
56 unsigned properties_off
;
57 unsigned properties_len
;
58 /* Hashes of primary device properties strings, to let libudev subscribers
59 * use in-kernel socket filters; values need to be stored in network order */
60 unsigned filter_subsystem_hash
;
61 unsigned filter_devtype_hash
;
62 unsigned filter_tag_bloom_hi
;
63 unsigned filter_tag_bloom_lo
;
64 } monitor_netlink_header
;
66 static int monitor_set_nl_address(sd_device_monitor
*m
) {
67 union sockaddr_union snl
;
72 /* Get the address the kernel has assigned us.
73 * It is usually, but not necessarily the pid. */
74 addrlen
= sizeof(struct sockaddr_nl
);
75 if (getsockname(m
->sock
, &snl
.sa
, &addrlen
) < 0)
78 m
->snl
.nl
.nl_pid
= snl
.nl
.nl_pid
;
82 int device_monitor_allow_unicast_sender(sd_device_monitor
*m
, sd_device_monitor
*sender
) {
83 assert_return(m
, -EINVAL
);
84 assert_return(sender
, -EINVAL
);
86 m
->snl_trusted_sender
.nl
.nl_pid
= sender
->snl
.nl
.nl_pid
;
90 _public_
int sd_device_monitor_set_receive_buffer_size(sd_device_monitor
*m
, size_t size
) {
91 int r
, n
= (int) size
;
93 assert_return(m
, -EINVAL
);
94 assert_return((size_t) n
== size
, -EINVAL
);
96 if (setsockopt_int(m
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, n
) < 0) {
97 r
= setsockopt_int(m
->sock
, SOL_SOCKET
, SO_RCVBUF
, n
);
105 int device_monitor_disconnect(sd_device_monitor
*m
) {
108 m
->sock
= safe_close(m
->sock
);
112 int device_monitor_get_fd(sd_device_monitor
*m
) {
113 assert_return(m
, -EINVAL
);
118 int device_monitor_new_full(sd_device_monitor
**ret
, MonitorNetlinkGroup group
, int fd
) {
119 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*m
= NULL
;
120 _cleanup_close_
int sock
= -1;
123 assert_return(ret
, -EINVAL
);
124 assert_return(group
>= 0 && group
< _MONITOR_NETLINK_GROUP_MAX
, -EINVAL
);
126 if (group
== MONITOR_GROUP_UDEV
&&
127 access("/run/udev/control", F_OK
) < 0 &&
128 dev_is_devtmpfs() <= 0) {
131 * We do not support subscribing to uevents if no instance of
132 * udev is running. Uevents would otherwise broadcast the
133 * processing data of the host into containers, which is not
136 * Containers will currently not get any udev uevents, until
137 * a supporting infrastructure is available.
139 * We do not set a netlink multicast group here, so the socket
140 * will not receive any messages.
143 log_debug("sd-device-monitor: The udev service seems not to be active, disabling the monitor");
144 group
= MONITOR_GROUP_NONE
;
148 sock
= socket(PF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, NETLINK_KOBJECT_UEVENT
);
150 return log_debug_errno(errno
, "sd-device-monitor: Failed to create socket: %m");
153 m
= new(sd_device_monitor
, 1);
157 *m
= (sd_device_monitor
) {
159 .sock
= fd
>= 0 ? fd
: TAKE_FD(sock
),
161 .snl
.nl
.nl_family
= AF_NETLINK
,
162 .snl
.nl
.nl_groups
= group
,
166 r
= monitor_set_nl_address(m
);
168 return log_debug_errno(r
, "sd-device-monitor: Failed to set netlink address: %m");
175 _public_
int sd_device_monitor_new(sd_device_monitor
**ret
) {
176 return device_monitor_new_full(ret
, MONITOR_GROUP_UDEV
, -1);
179 _public_
int sd_device_monitor_stop(sd_device_monitor
*m
) {
180 assert_return(m
, -EINVAL
);
182 m
->event_source
= sd_event_source_unref(m
->event_source
);
183 (void) device_monitor_disconnect(m
);
188 static int device_monitor_event_handler(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
189 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
190 sd_device_monitor
*m
= userdata
;
194 if (device_monitor_receive_device(m
, &device
) <= 0)
198 return m
->callback(m
, device
, m
->userdata
);
203 _public_
int sd_device_monitor_start(sd_device_monitor
*m
, sd_device_monitor_handler_t callback
, void *userdata
) {
206 assert_return(m
, -EINVAL
);
209 r
= sd_device_monitor_attach_event(m
, NULL
);
214 r
= device_monitor_enable_receiving(m
);
218 m
->callback
= callback
;
219 m
->userdata
= userdata
;
221 r
= sd_event_add_io(m
->event
, &m
->event_source
, m
->sock
, EPOLLIN
, device_monitor_event_handler
, m
);
225 (void) sd_event_source_set_description(m
->event_source
, "sd-device-monitor");
230 _public_
int sd_device_monitor_detach_event(sd_device_monitor
*m
) {
231 assert_return(m
, -EINVAL
);
233 (void) sd_device_monitor_stop(m
);
234 m
->event
= sd_event_unref(m
->event
);
239 _public_
int sd_device_monitor_attach_event(sd_device_monitor
*m
, sd_event
*event
) {
242 assert_return(m
, -EINVAL
);
243 assert_return(!m
->event
, -EBUSY
);
246 m
->event
= sd_event_ref(event
);
248 r
= sd_event_default(&m
->event
);
256 _public_ sd_event
*sd_device_monitor_get_event(sd_device_monitor
*m
) {
257 assert_return(m
, NULL
);
262 _public_ sd_event_source
*sd_device_monitor_get_event_source(sd_device_monitor
*m
) {
263 assert_return(m
, NULL
);
265 return m
->event_source
;
268 int device_monitor_enable_receiving(sd_device_monitor
*m
) {
271 assert_return(m
, -EINVAL
);
273 r
= sd_device_monitor_filter_update(m
);
275 return log_debug_errno(r
, "sd-device-monitor: Failed to update filter: %m");
278 /* enable receiving of sender credentials */
279 r
= setsockopt_int(m
->sock
, SOL_SOCKET
, SO_PASSCRED
, true);
281 return log_debug_errno(r
, "sd-device-monitor: Failed to set socket option SO_PASSCRED: %m");
283 if (bind(m
->sock
, &m
->snl
.sa
, sizeof(struct sockaddr_nl
)) < 0)
284 return log_debug_errno(errno
, "sd-device-monitor: Failed to bind monitoring socket: %m");
288 r
= monitor_set_nl_address(m
);
290 return log_debug_errno(r
, "sd-device-monitor: Failed to set address: %m");
296 static sd_device_monitor
*device_monitor_free(sd_device_monitor
*m
) {
299 (void) sd_device_monitor_detach_event(m
);
301 hashmap_free_free_free(m
->subsystem_filter
);
302 set_free_free(m
->tag_filter
);
307 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_monitor
, sd_device_monitor
, device_monitor_free
);
309 static int passes_filter(sd_device_monitor
*m
, sd_device
*device
) {
310 const char *tag
, *subsystem
, *devtype
, *s
, *d
= NULL
;
314 assert_return(m
, -EINVAL
);
315 assert_return(device
, -EINVAL
);
317 if (hashmap_isempty(m
->subsystem_filter
))
320 r
= sd_device_get_subsystem(device
, &s
);
324 r
= sd_device_get_devtype(device
, &d
);
325 if (r
< 0 && r
!= -ENOENT
)
328 HASHMAP_FOREACH_KEY(devtype
, subsystem
, m
->subsystem_filter
, i
) {
329 if (!streq(s
, subsystem
))
338 if (streq(d
, devtype
))
345 if (set_isempty(m
->tag_filter
))
348 SET_FOREACH(tag
, m
->tag_filter
, i
)
349 if (sd_device_has_tag(device
, tag
) > 0)
355 int device_monitor_receive_device(sd_device_monitor
*m
, sd_device
**ret
) {
356 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
358 monitor_netlink_header nlh
;
363 .iov_len
= sizeof(buf
)
365 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
366 union sockaddr_union snl
;
367 struct msghdr smsg
= {
370 .msg_control
= cred_msg
,
371 .msg_controllen
= sizeof(cred_msg
),
373 .msg_namelen
= sizeof(snl
),
375 struct cmsghdr
*cmsg
;
377 ssize_t buflen
, bufpos
;
378 bool is_initialized
= false;
383 buflen
= recvmsg(m
->sock
, &smsg
, 0);
386 log_debug_errno(errno
, "sd-device-monitor: Failed to receive message: %m");
390 if (buflen
< 32 || (smsg
.msg_flags
& MSG_TRUNC
))
391 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
392 "sd-device-monitor: Invalid message length.");
394 if (snl
.nl
.nl_groups
== MONITOR_GROUP_NONE
) {
395 /* unicast message, check if we trust the sender */
396 if (m
->snl_trusted_sender
.nl
.nl_pid
== 0 ||
397 snl
.nl
.nl_pid
!= m
->snl_trusted_sender
.nl
.nl_pid
)
398 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
399 "sd-device-monitor: Unicast netlink message ignored.");
401 } else if (snl
.nl
.nl_groups
== MONITOR_GROUP_KERNEL
) {
402 if (snl
.nl
.nl_pid
> 0)
403 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
404 "sd-device-monitor: Multicast kernel netlink message from PID %"PRIu32
" ignored.", snl
.nl
.nl_pid
);
407 cmsg
= CMSG_FIRSTHDR(&smsg
);
408 if (!cmsg
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
)
409 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
410 "sd-device-monitor: No sender credentials received, message ignored.");
412 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
414 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
415 "sd-device-monitor: Sender uid="UID_FMT
", message ignored.", cred
->uid
);
417 if (streq(buf
.raw
, "libudev")) {
418 /* udev message needs proper version magic */
419 if (buf
.nlh
.magic
!= htobe32(UDEV_MONITOR_MAGIC
))
420 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
421 "sd-device-monitor: Invalid message signature (%x != %x)",
422 buf
.nlh
.magic
, htobe32(UDEV_MONITOR_MAGIC
));
424 if (buf
.nlh
.properties_off
+32 > (size_t) buflen
)
425 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
426 "sd-device-monitor: Invalid message length (%u > %zd)",
427 buf
.nlh
.properties_off
+32, buflen
);
429 bufpos
= buf
.nlh
.properties_off
;
431 /* devices received from udev are always initialized */
432 is_initialized
= true;
435 /* kernel message with header */
436 bufpos
= strlen(buf
.raw
) + 1;
437 if ((size_t) bufpos
< sizeof("a@/d") || bufpos
>= buflen
)
438 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
439 "sd-device-monitor: Invalid message length");
441 /* check message header */
442 if (!strstr(buf
.raw
, "@/"))
443 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
444 "sd-device-monitor: Invalid message header");
447 r
= device_new_from_nulstr(&device
, (uint8_t*) &buf
.raw
[bufpos
], buflen
- bufpos
);
449 return log_debug_errno(r
, "sd-device-monitor: Failed to create device from received message: %m");
452 device_set_is_initialized(device
);
454 /* Skip device, if it does not pass the current filter */
455 r
= passes_filter(m
, device
);
457 return log_device_debug_errno(device
, r
, "sd-device-monitor: Failed to check received device passing filter: %m");
459 log_device_debug(device
, "sd-device-monitor: Received device does not pass filter, ignoring");
461 *ret
= TAKE_PTR(device
);
466 static uint32_t string_hash32(const char *str
) {
467 return MurmurHash2(str
, strlen(str
), 0);
470 /* Get a bunch of bit numbers out of the hash, and set the bits in our bit field */
471 static uint64_t string_bloom64(const char *str
) {
473 uint32_t hash
= string_hash32(str
);
475 bits
|= 1LLU << (hash
& 63);
476 bits
|= 1LLU << ((hash
>> 6) & 63);
477 bits
|= 1LLU << ((hash
>> 12) & 63);
478 bits
|= 1LLU << ((hash
>> 18) & 63);
482 int device_monitor_send_device(
483 sd_device_monitor
*m
,
484 sd_device_monitor
*destination
,
487 monitor_netlink_header nlh
= {
489 .magic
= htobe32(UDEV_MONITOR_MAGIC
),
490 .header_size
= sizeof nlh
,
492 struct iovec iov
[2] = {
493 { .iov_base
= &nlh
, .iov_len
= sizeof nlh
},
495 struct msghdr smsg
= {
499 /* default destination for sending */
500 union sockaddr_union default_destination
= {
501 .nl
.nl_family
= AF_NETLINK
,
502 .nl
.nl_groups
= MONITOR_GROUP_UDEV
,
504 uint64_t tag_bloom_bits
;
505 const char *buf
, *val
;
513 r
= device_get_properties_nulstr(device
, (const uint8_t **) &buf
, &blen
);
515 return log_device_debug_errno(device
, r
, "sd-device-monitor: Failed to get device properties: %m");
517 log_device_debug(device
, "sd-device-monitor: Length of device property nulstr is too small to contain valid device information");
521 /* fill in versioned header */
522 r
= sd_device_get_subsystem(device
, &val
);
524 return log_device_debug_errno(device
, r
, "sd-device-monitor: Failed to get device subsystem: %m");
525 nlh
.filter_subsystem_hash
= htobe32(string_hash32(val
));
527 if (sd_device_get_devtype(device
, &val
) >= 0)
528 nlh
.filter_devtype_hash
= htobe32(string_hash32(val
));
530 /* add tag bloom filter */
532 FOREACH_DEVICE_TAG(device
, val
)
533 tag_bloom_bits
|= string_bloom64(val
);
535 if (tag_bloom_bits
> 0) {
536 nlh
.filter_tag_bloom_hi
= htobe32(tag_bloom_bits
>> 32);
537 nlh
.filter_tag_bloom_lo
= htobe32(tag_bloom_bits
& 0xffffffff);
540 /* add properties list */
541 nlh
.properties_off
= iov
[0].iov_len
;
542 nlh
.properties_len
= blen
;
543 iov
[1] = IOVEC_MAKE((char*) buf
, blen
);
546 * Use custom address for target, or the default one.
548 * If we send to a multicast group, we will get
549 * ECONNREFUSED, which is expected.
551 smsg
.msg_name
= destination
? &destination
->snl
: &default_destination
;
552 smsg
.msg_namelen
= sizeof(struct sockaddr_nl
);
553 count
= sendmsg(m
->sock
, &smsg
, 0);
555 if (!destination
&& errno
== ECONNREFUSED
) {
556 log_device_debug(device
, "sd-device-monitor: Passed to netlink monitor");
559 return log_device_debug_errno(device
, errno
, "sd-device-monitor: Failed to send device to netlink monitor: %m");
562 log_device_debug(device
, "sd-device-monitor: Passed %zi byte to netlink monitor", count
);
566 static void bpf_stmt(struct sock_filter
*ins
, unsigned *i
,
567 unsigned short code
, unsigned data
) {
568 ins
[(*i
)++] = (struct sock_filter
) {
574 static void bpf_jmp(struct sock_filter
*ins
, unsigned *i
,
575 unsigned short code
, unsigned data
,
576 unsigned short jt
, unsigned short jf
) {
577 ins
[(*i
)++] = (struct sock_filter
) {
585 _public_
int sd_device_monitor_filter_update(sd_device_monitor
*m
) {
586 struct sock_filter ins
[512] = {};
587 struct sock_fprog filter
;
588 const char *subsystem
, *devtype
, *tag
;
592 assert_return(m
, -EINVAL
);
594 if (m
->filter_uptodate
)
597 if (hashmap_isempty(m
->subsystem_filter
) &&
598 set_isempty(m
->tag_filter
)) {
599 m
->filter_uptodate
= true;
603 /* load magic in A */
604 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(monitor_netlink_header
, magic
));
605 /* jump if magic matches */
606 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, UDEV_MONITOR_MAGIC
, 1, 0);
607 /* wrong magic, pass packet */
608 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
610 if (!set_isempty(m
->tag_filter
)) {
611 int tag_matches
= set_size(m
->tag_filter
);
613 /* add all tags matches */
614 SET_FOREACH(tag
, m
->tag_filter
, it
) {
615 uint64_t tag_bloom_bits
= string_bloom64(tag
);
616 uint32_t tag_bloom_hi
= tag_bloom_bits
>> 32;
617 uint32_t tag_bloom_lo
= tag_bloom_bits
& 0xffffffff;
619 /* load device bloom bits in A */
620 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(monitor_netlink_header
, filter_tag_bloom_hi
));
621 /* clear bits (tag bits & bloom bits) */
622 bpf_stmt(ins
, &i
, BPF_ALU
|BPF_AND
|BPF_K
, tag_bloom_hi
);
623 /* jump to next tag if it does not match */
624 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_hi
, 0, 3);
626 /* load device bloom bits in A */
627 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(monitor_netlink_header
, filter_tag_bloom_lo
));
628 /* clear bits (tag bits & bloom bits) */
629 bpf_stmt(ins
, &i
, BPF_ALU
|BPF_AND
|BPF_K
, tag_bloom_lo
);
630 /* jump behind end of tag match block if tag matches */
632 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_lo
, 1 + (tag_matches
* 6), 0);
635 /* nothing matched, drop packet */
636 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
639 /* add all subsystem matches */
640 if (!hashmap_isempty(m
->subsystem_filter
)) {
641 HASHMAP_FOREACH_KEY(devtype
, subsystem
, m
->subsystem_filter
, it
) {
642 uint32_t hash
= string_hash32(subsystem
);
644 /* load device subsystem value in A */
645 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(monitor_netlink_header
, filter_subsystem_hash
));
647 /* jump if subsystem does not match */
648 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 1);
650 /* jump if subsystem does not match */
651 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 3);
652 /* load device devtype value in A */
653 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(monitor_netlink_header
, filter_devtype_hash
));
654 /* jump if value does not match */
655 hash
= string_hash32(devtype
);
656 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 1);
659 /* matched, pass packet */
660 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
662 if (i
+1 >= ELEMENTSOF(ins
))
666 /* nothing matched, drop packet */
667 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
670 /* matched, pass packet */
671 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
674 filter
= (struct sock_fprog
) {
678 if (setsockopt(m
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
)) < 0)
681 m
->filter_uptodate
= true;
685 _public_
int sd_device_monitor_filter_add_match_subsystem_devtype(sd_device_monitor
*m
, const char *subsystem
, const char *devtype
) {
686 _cleanup_free_
char *s
= NULL
, *d
= NULL
;
689 assert_return(m
, -EINVAL
);
690 assert_return(subsystem
, -EINVAL
);
692 s
= strdup(subsystem
);
702 r
= hashmap_ensure_allocated(&m
->subsystem_filter
, NULL
);
706 r
= hashmap_put(m
->subsystem_filter
, s
, d
);
711 m
->filter_uptodate
= false;
716 _public_
int sd_device_monitor_filter_add_match_tag(sd_device_monitor
*m
, const char *tag
) {
717 _cleanup_free_
char *t
= NULL
;
720 assert_return(m
, -EINVAL
);
721 assert_return(tag
, -EINVAL
);
727 r
= set_ensure_allocated(&m
->tag_filter
, &string_hash_ops
);
731 r
= set_put(m
->tag_filter
, t
);
738 m
->filter_uptodate
= false;
743 _public_
int sd_device_monitor_filter_remove(sd_device_monitor
*m
) {
744 static const struct sock_fprog filter
= { 0, NULL
};
746 assert_return(m
, -EINVAL
);
748 m
->subsystem_filter
= hashmap_free_free_free(m
->subsystem_filter
);
749 m
->tag_filter
= set_free_free(m
->tag_filter
);
751 if (setsockopt(m
->sock
, SOL_SOCKET
, SO_DETACH_FILTER
, &filter
, sizeof(filter
)) < 0)
754 m
->filter_uptodate
= true;