X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=udev%2Flib%2Flibudev-monitor.c;h=33a0605492a128f174b080b2b84b6206907fc81c;hb=1e03b754aef576a5cb75f01b1805cdc1f9cc292f;hp=a0f93546f7f8fb7c035c01f3ee03a49a3e88d26d;hpb=c7dff03e057a7548bc2f2adbd5d2798d209b56e6;p=elogind.git diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c index a0f93546f..33a060549 100644 --- a/udev/lib/libudev-monitor.c +++ b/udev/lib/libudev-monitor.c @@ -1,7 +1,7 @@ /* * libudev - interface to udev device information * - * Copyright (C) 2008 Kay Sievers + * Copyright (C) 2008-2009 Kay Sievers * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -32,15 +32,17 @@ struct udev_monitor { int refcount; int sock; struct sockaddr_nl snl; - struct sockaddr_nl snl_peer; + struct sockaddr_nl snl_trusted_sender; + struct sockaddr_nl snl_destination; struct sockaddr_un sun; socklen_t addrlen; struct udev_list_node filter_subsystem_list; }; enum udev_monitor_netlink_group { - UDEV_MONITOR_KERNEL = 1, - UDEV_MONITOR_UDEV = 2, + UDEV_MONITOR_NONE, + UDEV_MONITOR_KERNEL, + UDEV_MONITOR_UDEV, }; #define UDEV_MONITOR_MAGIC 0xcafe1dea @@ -56,11 +58,12 @@ struct udev_monitor_netlink_header { unsigned short properties_off; unsigned short properties_len; /* - * hashes of some common device propertie strings to filter with socket filters in + * hashes of some common device properties strings to filter with socket filters in * the client used in the kernel from socket filter rules; needs to be stored in * network order */ unsigned int filter_subsystem; + unsigned int filter_devtype; }; static struct udev_monitor *udev_monitor_new(struct udev *udev) @@ -112,16 +115,16 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char udev_monitor->sun.sun_family = AF_LOCAL; if (socket_path[0] == '@') { /* translate leading '@' to abstract namespace */ - util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path)); + util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path); udev_monitor->sun.sun_path[0] = '\0'; udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path); } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) { /* existing socket file */ - util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path)); + util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path); udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path); } else { /* no socket file, assume abstract namespace socket */ - util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1); + util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path); udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1; } udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0); @@ -170,11 +173,11 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char return NULL; if (name == NULL) - return NULL; - if (strcmp(name, "kernel") == 0) - group = UDEV_MONITOR_KERNEL; + group = UDEV_MONITOR_NONE; else if (strcmp(name, "udev") == 0) group = UDEV_MONITOR_UDEV; + else if (strcmp(name, "kernel") == 0) + group = UDEV_MONITOR_KERNEL; else return NULL; @@ -192,8 +195,10 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char udev_monitor->snl.nl_family = AF_NETLINK; udev_monitor->snl.nl_groups = group; - udev_monitor->snl_peer.nl_family = AF_NETLINK; - udev_monitor->snl_peer.nl_groups = UDEV_MONITOR_UDEV; + + /* default destination for sending */ + udev_monitor->snl_destination.nl_family = AF_NETLINK; + udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV; dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group); return udev_monitor; @@ -222,7 +227,7 @@ static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i, (*i)++; } -static int filter_apply(struct udev_monitor *udev_monitor) +int udev_monitor_filter_update(struct udev_monitor *udev_monitor) { static struct sock_filter ins[256]; static struct sock_fprog filter; @@ -236,23 +241,34 @@ static int filter_apply(struct udev_monitor *udev_monitor) memset(ins, 0x00, sizeof(ins)); i = 0; - /* load magic in accu */ + /* load magic in A */ bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic)); /* jump if magic matches */ bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0); - /* wrong magic, drop packet */ - bpf_stmt(ins, &i, BPF_RET|BPF_K, 0); + /* wrong magic, pass packet */ + bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); - /* load filter_subsystem value in accu */ - bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem)); /* add all subsystem match values */ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { - const char *subsys = udev_list_entry_get_name(list_entry); unsigned int hash; - hash = util_string_hash32(subsys); - /* jump if value does not match */ - bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); + /* load filter_subsystem value in A */ + bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem)); + hash = util_string_hash32(udev_list_entry_get_name(list_entry)); + if (udev_list_entry_get_value(list_entry) == NULL) { + /* jump if subsystem does not match */ + bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); + } else { + /* jump if subsystem does not match */ + bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3); + + /* load filter_devtype value in A */ + bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype)); + /* jump if value does not match */ + hash = util_string_hash32(udev_list_entry_get_value(list_entry)); + bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); + } + /* matched, pass packet */ bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); @@ -269,6 +285,12 @@ static int filter_apply(struct udev_monitor *udev_monitor) return err; } +int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender) +{ + udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid; + return 0; +} + int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) { int err; @@ -278,9 +300,22 @@ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen); } else if (udev_monitor->snl.nl_family != 0) { - filter_apply(udev_monitor); + udev_monitor_filter_update(udev_monitor); err = bind(udev_monitor->sock, (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl)); + if (err == 0) { + struct sockaddr_nl snl; + socklen_t addrlen; + + /* + * get the address the kernel has assigned us + * it is usually, but not neccessarily the pid + */ + addrlen = sizeof(struct sockaddr_nl); + err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen); + if (err == 0) + udev_monitor->snl.nl_pid = snl.nl_pid; + } } else { return -EINVAL; } @@ -302,6 +337,15 @@ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)); } +int udev_monitor_disconnect(struct udev_monitor *udev_monitor) +{ + int err; + + err = close(udev_monitor->sock); + udev_monitor->sock = -1; + return err; +} + /** * udev_monitor_ref: * @udev_monitor: udev monitor @@ -374,20 +418,29 @@ int udev_monitor_get_fd(struct udev_monitor *udev_monitor) static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device) { struct udev_list_entry *list_entry; - int pass = 0; if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL) return 1; udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { - const char *subsys = udev_device_get_subsystem(udev_device); + const char *subsys = udev_list_entry_get_name(list_entry); + const char *dsubsys = udev_device_get_subsystem(udev_device); + const char *devtype; + const char *ddevtype; - if (strcmp(udev_list_entry_get_name(list_entry), subsys) == 0) { - pass= 1; - break; - } + if (strcmp(dsubsys, subsys) != 0) + continue; + + devtype = udev_list_entry_get_value(list_entry); + if (devtype == NULL) + return 1; + ddevtype = udev_device_get_devtype(udev_device); + if (ddevtype == NULL) + continue; + if (strcmp(ddevtype, devtype) == 0) + return 1; } - return pass; + return 0; } /** @@ -424,6 +477,7 @@ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monito int action_set = 0; int maj = 0; int min = 0; + int is_kernel = 0; retry: if (udev_monitor == NULL) @@ -456,12 +510,18 @@ retry: if (udev_monitor->snl.nl_family != 0) { if (snl.nl_groups == 0) { - info(udev_monitor->udev, "unicast netlink message ignored\n"); - return NULL; - } - if ((snl.nl_groups == UDEV_MONITOR_KERNEL) && (snl.nl_pid > 0)) { - info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid); - return NULL; + /* unicast message, check if we trust the sender */ + if (udev_monitor->snl_trusted_sender.nl_pid == 0 || + snl.nl_pid != udev_monitor->snl_trusted_sender.nl_pid) { + info(udev_monitor->udev, "unicast netlink message ignored\n"); + return NULL; + } + } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) { + if (snl.nl_pid > 0) { + info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid); + return NULL; + } + is_kernel = 1; } } @@ -520,8 +580,7 @@ retry: if (strncmp(key, "DEVPATH=", 8) == 0) { char path[UTIL_PATH_SIZE]; - util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path)); - util_strlcat(path, &key[8], sizeof(path)); + util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_monitor->udev), &key[8], NULL); udev_device_set_syspath(udev_device, path); devpath_set = 1; } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) { @@ -530,13 +589,16 @@ retry: } else if (strncmp(key, "DEVTYPE=", 8) == 0) { udev_device_set_devtype(udev_device, &key[8]); } else if (strncmp(key, "DEVNAME=", 8) == 0) { - udev_device_set_devnode(udev_device, &key[8]); + if (is_kernel) + udev_device_set_knodename(udev_device, &key[8]); + else + udev_device_set_devnode(udev_device, &key[8]); } else if (strncmp(key, "DEVLINKS=", 9) == 0) { char devlinks[UTIL_PATH_SIZE]; char *slink; char *next; - util_strlcpy(devlinks, &key[9], sizeof(devlinks)); + util_strscpy(devlinks, sizeof(devlinks), &key[9]); slink = devlinks; next = strchr(slink, ' '); while (next != NULL) { @@ -558,15 +620,10 @@ retry: min = strtoull(&key[6], NULL, 10); } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) { udev_device_set_devpath_old(udev_device, &key[12]); - } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) { - udev_device_set_physdevpath(udev_device, &key[12]); } else if (strncmp(key, "SEQNUM=", 7) == 0) { udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10)); } else if (strncmp(key, "TIMEOUT=", 8) == 0) { udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10)); - } else if (strncmp(key, "PHYSDEV", 7) == 0) { - /* skip deprecated values */ - continue; } else { udev_device_add_property_from_string(udev_device, key); } @@ -599,7 +656,8 @@ retry: return udev_device; } -int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device) +int udev_monitor_send_device(struct udev_monitor *udev_monitor, + struct udev_monitor *destination, struct udev_device *udev_device) { struct msghdr smsg; struct iovec iov[2]; @@ -614,19 +672,17 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_devi if (udev_monitor->sun.sun_family != 0) { const char *action; char header[2048]; - size_t hlen; + char *s; /* header @ */ action = udev_device_get_action(udev_device); if (action == NULL) return -EINVAL; - util_strlcpy(header, action, sizeof(header)); - util_strlcat(header, "@", sizeof(header)); - hlen = util_strlcat(header, udev_device_get_devpath(udev_device), sizeof(header))+1; - if (hlen >= sizeof(header)) + s = header; + if (util_strpcpyl(&s, sizeof(header), action, "@", udev_device_get_devpath(udev_device), NULL) == 0) return -EINVAL; iov[0].iov_base = header; - iov[0].iov_len = hlen; + iov[0].iov_len = (s - header)+1; /* add properties list */ iov[1].iov_base = (char *)buf; @@ -644,10 +700,13 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_devi /* add versioned header */ memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header)); - util_strlcpy(nlh.version, "udev-" VERSION, sizeof(nlh.version)); + util_strscpy(nlh.version, sizeof(nlh.version), "udev-" VERSION); nlh.magic = htonl(UDEV_MONITOR_MAGIC); val = udev_device_get_subsystem(udev_device); nlh.filter_subsystem = htonl(util_string_hash32(val)); + val = udev_device_get_devtype(udev_device); + if (val != NULL) + nlh.filter_devtype = htonl(util_string_hash32(val)); iov[0].iov_base = &nlh; iov[0].iov_len = sizeof(struct udev_monitor_netlink_header); @@ -660,8 +719,16 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_devi memset(&smsg, 0x00, sizeof(struct msghdr)); smsg.msg_iov = iov; smsg.msg_iovlen = 2; - /* no destination besides the muticast group, we will always get ECONNREFUSED */ - smsg.msg_name = &udev_monitor->snl_peer; + /* + * Use custom address for target, or the default one. + * + * If we send to a muticast group, we will get + * ECONNREFUSED, which is expected. + */ + if (destination != NULL) + smsg.msg_name = &destination->snl; + else + smsg.msg_name = &udev_monitor->snl_destination; smsg.msg_namelen = sizeof(struct sockaddr_nl); } else { return -1; @@ -672,14 +739,22 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_devi return count; } -int udev_monitor_filter_add_match_subsystem(struct udev_monitor *udev_monitor, const char *subsystem) +int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype) { if (udev_monitor == NULL) return -EINVAL; if (subsystem == NULL) return 0; if (udev_list_entry_add(udev_monitor->udev, - &udev_monitor->filter_subsystem_list, subsystem, NULL, 1, 0) == NULL) + &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL) return -ENOMEM; return 0; } + +int udev_monitor_filter_remove(struct udev_monitor *udev_monitor) +{ + static struct sock_fprog filter = { 0, NULL }; + + udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list); + return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)); +}