blob: 827170a14be9cb22bf53e0163f40cf5137632917 [file] [log] [blame]
Vernon Tang3f582e92011-04-25 13:08:17 +10001/*
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 Zavin2d55e022011-08-31 18:07:36 -070020#include <stdbool.h>
21#include <string.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100022#include <strings.h>
Dima Zavin2d55e022011-08-31 18:07:36 -070023#include <sys/socket.h>
24#include <sys/un.h>
25#include <unistd.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100026
27#include <linux/netlink.h>
28
29/**
30 * Like recv(), but checks that messages actually originate from the kernel.
31 */
Geremy Condra15621e02012-03-29 14:46:07 -070032ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length)
33{
Jeff Sharkey9a20e672014-10-30 14:51:59 -070034 uid_t uid = -1;
35 return uevent_kernel_multicast_uid_recv(socket, buffer, length, &uid);
Geremy Condra15621e02012-03-29 14:46:07 -070036}
37
38/**
Jeff Sharkey9a20e672014-10-30 14:51:59 -070039 * Like the above, but passes a uid_t in by pointer. In the event that this
Geremy Condra15621e02012-03-29 14:46:07 -070040 * 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 Sharkey9a20e672014-10-30 14:51:59 -070047ssize_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
52ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid)
Geremy Condra15621e02012-03-29 14:46:07 -070053{
Vernon Tang3f582e92011-04-25 13:08:17 +100054 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 Sharkey9a20e672014-10-30 14:51:59 -070067 *uid = -1;
Vernon Tang3f582e92011-04-25 13:08:17 +100068 ssize_t n = recvmsg(socket, &hdr, 0);
69 if (n <= 0) {
70 return n;
71 }
72
Vernon Tang3f582e92011-04-25 13:08:17 +100073 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 Sharkey9a20e672014-10-30 14:51:59 -070080 *uid = cred->uid;
Vernon Tang3f582e92011-04-25 13:08:17 +100081 if (cred->uid != 0) {
82 /* ignoring netlink message from non-root user */
83 goto out;
84 }
85
Jeff Sharkey9a20e672014-10-30 14:51:59 -070086 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 Condra15621e02012-03-29 14:46:07 -070092 goto out;
93 }
94
Vernon Tang3f582e92011-04-25 13:08:17 +100095 return n;
96
97out:
98 /* clear residual potentially malicious data */
99 bzero(buffer, length);
100 errno = EIO;
101 return -1;
102}
Dima Zavin2d55e022011-08-31 18:07:36 -0700103
104int 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, 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}