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 <stdbool.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <unistd.h>
26 
27 #include <linux/netlink.h>
28 
29 /**
30  * Like recv(), but checks that messages actually originate from the kernel.
31  */
uevent_kernel_multicast_recv(int socket,void * buffer,size_t length)32 ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length)
33 {
34     uid_t uid = -1;
35     return uevent_kernel_multicast_uid_recv(socket, buffer, length, &uid);
36 }
37 
38 /**
39  * Like the above, but passes a uid_t in by pointer. In the event that this
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  */
uevent_kernel_multicast_uid_recv(int socket,void * buffer,size_t length,uid_t * uid)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 
uevent_kernel_recv(int socket,void * buffer,size_t length,bool require_group,uid_t * uid)52 ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid)
53 {
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 
67     *uid = -1;
68     ssize_t n = recvmsg(socket, &hdr, 0);
69     if (n <= 0) {
70         return n;
71     }
72 
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);
80     *uid = cred->uid;
81     if (cred->uid != 0) {
82         /* ignoring netlink message from non-root user */
83         goto out;
84     }
85 
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 */
92         goto out;
93     }
94 
95     return n;
96 
97 out:
98     /* clear residual potentially malicious data */
99     bzero(buffer, length);
100     errno = EIO;
101     return -1;
102 }
103 
uevent_open_socket(int buf_sz,bool passcred)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 
115     s = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
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 }
129