| Luis Hector Chavez | e97a4b9 | 2017-11-02 14:17:43 -0700 | [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> | 
|  | 20 | #include <stdint.h> | 
|  | 21 | #include <stdio.h> | 
|  | 22 | #include <string.h> | 
|  | 23 | #include <strings.h> | 
|  | 24 | #include <sys/socket.h> | 
|  | 25 | #include <sys/un.h> | 
|  | 26 | #include <unistd.h> | 
|  | 27 |  | 
|  | 28 | #include <linux/netlink.h> | 
|  | 29 |  | 
| Luis Hector Chavez | e97a4b9 | 2017-11-02 14:17:43 -0700 | [diff] [blame] | 30 | extern "C" { | 
|  | 31 |  | 
|  | 32 | /** | 
|  | 33 | * Like recv(), but checks that messages actually originate from the kernel. | 
|  | 34 | */ | 
|  | 35 | ssize_t uevent_kernel_multicast_recv(int socket, void* buffer, size_t length) { | 
|  | 36 | uid_t uid = -1; | 
|  | 37 | return uevent_kernel_multicast_uid_recv(socket, buffer, length, &uid); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | /** | 
|  | 41 | * Like the above, but passes a uid_t in by pointer. In the event that this | 
|  | 42 | * fails due to a bad uid check, the uid_t will be set to the uid of the | 
|  | 43 | * socket's peer. | 
|  | 44 | * | 
|  | 45 | * If this method rejects a netlink message from outside the kernel, it | 
|  | 46 | * returns -1, sets errno to EIO, and sets "user" to the UID associated with the | 
|  | 47 | * message. If the peer UID cannot be determined, "user" is set to -1." | 
|  | 48 | */ | 
|  | 49 | ssize_t uevent_kernel_multicast_uid_recv(int socket, void* buffer, size_t length, uid_t* uid) { | 
|  | 50 | return uevent_kernel_recv(socket, buffer, length, true, uid); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | ssize_t uevent_kernel_recv(int socket, void* buffer, size_t length, bool require_group, uid_t* uid) { | 
| Luis Hector Chavez | e97a4b9 | 2017-11-02 14:17:43 -0700 | [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, sizeof(addr), &iov, 1, control, sizeof(control), 0, | 
|  | 59 | }; | 
|  | 60 | struct ucred* cred; | 
|  | 61 |  | 
|  | 62 | *uid = -1; | 
|  | 63 | ssize_t n = recvmsg(socket, &hdr, 0); | 
|  | 64 | if (n <= 0) { | 
|  | 65 | return n; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr); | 
|  | 69 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { | 
|  | 70 | /* ignoring netlink message with no sender credentials */ | 
|  | 71 | goto out; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | cred = (struct ucred*)CMSG_DATA(cmsg); | 
|  | 75 | *uid = cred->uid; | 
| Luis Hector Chavez | e97a4b9 | 2017-11-02 14:17:43 -0700 | [diff] [blame] | 76 |  | 
|  | 77 | if (addr.nl_pid != 0) { | 
|  | 78 | /* ignore non-kernel */ | 
|  | 79 | goto out; | 
|  | 80 | } | 
|  | 81 | if (require_group && addr.nl_groups == 0) { | 
|  | 82 | /* ignore unicast messages when requested */ | 
|  | 83 | goto out; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | return n; | 
|  | 87 |  | 
|  | 88 | out: | 
|  | 89 | /* clear residual potentially malicious data */ | 
|  | 90 | bzero(buffer, length); | 
|  | 91 | errno = EIO; | 
|  | 92 | return -1; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | int uevent_open_socket(int buf_sz, bool passcred) { | 
|  | 96 | struct sockaddr_nl addr; | 
|  | 97 | int on = passcred; | 
|  | 98 | int s; | 
|  | 99 |  | 
|  | 100 | memset(&addr, 0, sizeof(addr)); | 
|  | 101 | addr.nl_family = AF_NETLINK; | 
|  | 102 | addr.nl_pid = getpid(); | 
|  | 103 | addr.nl_groups = 0xffffffff; | 
|  | 104 |  | 
|  | 105 | s = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT); | 
|  | 106 | if (s < 0) return -1; | 
|  | 107 |  | 
|  | 108 | /* buf_sz should be less than net.core.rmem_max for this to succeed */ | 
|  | 109 | if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buf_sz, sizeof(buf_sz)) < 0) { | 
|  | 110 | close(s); | 
|  | 111 | return -1; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | 
|  | 115 |  | 
|  | 116 | if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) { | 
|  | 117 | close(s); | 
|  | 118 | return -1; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | return s; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | }  // extern "C" |