Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <cutils/uevent.h> |
| 18 | |
| 19 | #include <errno.h> |
Dima Zavin | 2d55e02 | 2011-08-31 18:07:36 -0700 | [diff] [blame] | 20 | #include <stdbool.h> |
| 21 | #include <string.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 22 | #include <strings.h> |
Dima Zavin | 2d55e02 | 2011-08-31 18:07:36 -0700 | [diff] [blame] | 23 | #include <sys/socket.h> |
| 24 | #include <sys/un.h> |
| 25 | #include <unistd.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 26 | |
| 27 | #include <linux/netlink.h> |
| 28 | |
| 29 | /** |
| 30 | * Like recv(), but checks that messages actually originate from the kernel. |
| 31 | */ |
Geremy Condra | 15621e0 | 2012-03-29 14:46:07 -0700 | [diff] [blame] | 32 | ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length) |
| 33 | { |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 34 | uid_t uid = -1; |
| 35 | return uevent_kernel_multicast_uid_recv(socket, buffer, length, &uid); |
Geremy Condra | 15621e0 | 2012-03-29 14:46:07 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 39 | * Like the above, but passes a uid_t in by pointer. In the event that this |
Geremy Condra | 15621e0 | 2012-03-29 14:46:07 -0700 | [diff] [blame] | 40 | * fails due to a bad uid check, the uid_t will be set to the uid of the |
| 41 | * socket's peer. |
| 42 | * |
| 43 | * If this method rejects a netlink message from outside the kernel, it |
| 44 | * returns -1, sets errno to EIO, and sets "user" to the UID associated with the |
| 45 | * message. If the peer UID cannot be determined, "user" is set to -1." |
| 46 | */ |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 47 | ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer, size_t length, uid_t *uid) |
| 48 | { |
| 49 | return uevent_kernel_recv(socket, buffer, length, true, uid); |
| 50 | } |
| 51 | |
| 52 | ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid) |
Geremy Condra | 15621e0 | 2012-03-29 14:46:07 -0700 | [diff] [blame] | 53 | { |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 54 | struct iovec iov = { buffer, length }; |
| 55 | struct sockaddr_nl addr; |
| 56 | char control[CMSG_SPACE(sizeof(struct ucred))]; |
| 57 | struct msghdr hdr = { |
| 58 | &addr, |
| 59 | sizeof(addr), |
| 60 | &iov, |
| 61 | 1, |
| 62 | control, |
| 63 | sizeof(control), |
| 64 | 0, |
| 65 | }; |
| 66 | |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 67 | *uid = -1; |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 68 | ssize_t n = recvmsg(socket, &hdr, 0); |
| 69 | if (n <= 0) { |
| 70 | return n; |
| 71 | } |
| 72 | |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 73 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr); |
| 74 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { |
| 75 | /* ignoring netlink message with no sender credentials */ |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | struct ucred *cred = (struct ucred *)CMSG_DATA(cmsg); |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 80 | *uid = cred->uid; |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 81 | if (cred->uid != 0) { |
| 82 | /* ignoring netlink message from non-root user */ |
| 83 | goto out; |
| 84 | } |
| 85 | |
Jeff Sharkey | 9a20e67 | 2014-10-30 14:51:59 -0700 | [diff] [blame] | 86 | if (addr.nl_pid != 0) { |
| 87 | /* ignore non-kernel */ |
| 88 | goto out; |
| 89 | } |
| 90 | if (require_group && addr.nl_groups == 0) { |
| 91 | /* ignore unicast messages when requested */ |
Geremy Condra | 15621e0 | 2012-03-29 14:46:07 -0700 | [diff] [blame] | 92 | goto out; |
| 93 | } |
| 94 | |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 95 | return n; |
| 96 | |
| 97 | out: |
| 98 | /* clear residual potentially malicious data */ |
| 99 | bzero(buffer, length); |
| 100 | errno = EIO; |
| 101 | return -1; |
| 102 | } |
Dima Zavin | 2d55e02 | 2011-08-31 18:07:36 -0700 | [diff] [blame] | 103 | |
| 104 | int uevent_open_socket(int buf_sz, bool passcred) |
| 105 | { |
| 106 | struct sockaddr_nl addr; |
| 107 | int on = passcred; |
| 108 | int s; |
| 109 | |
| 110 | memset(&addr, 0, sizeof(addr)); |
| 111 | addr.nl_family = AF_NETLINK; |
| 112 | addr.nl_pid = getpid(); |
| 113 | addr.nl_groups = 0xffffffff; |
| 114 | |
Nick Kralevich | 6d3cddb | 2015-02-26 13:32:52 -0800 | [diff] [blame] | 115 | s = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT); |
Dima Zavin | 2d55e02 | 2011-08-31 18:07:36 -0700 | [diff] [blame] | 116 | if(s < 0) |
| 117 | return -1; |
| 118 | |
| 119 | setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &buf_sz, sizeof(buf_sz)); |
| 120 | setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); |
| 121 | |
| 122 | if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 123 | close(s); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | return s; |
| 128 | } |