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 */
32ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length)
33{
34    uid_t user = -1;
35    return uevent_kernel_multicast_uid_recv(socket, buffer, length, &user);
36}
37
38/**
39 * Like the above, but passes a uid_t in by reference. 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 */
47ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer,
48                                         size_t length, uid_t *user)
49{
50    struct iovec iov = { buffer, length };
51    struct sockaddr_nl addr;
52    char control[CMSG_SPACE(sizeof(struct ucred))];
53    struct msghdr hdr = {
54        &addr,
55        sizeof(addr),
56        &iov,
57        1,
58        control,
59        sizeof(control),
60        0,
61    };
62
63    *user = -1;
64    ssize_t n = recvmsg(socket, &hdr, 0);
65    if (n <= 0) {
66        return n;
67    }
68
69    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr);
70    if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
71        /* ignoring netlink message with no sender credentials */
72        goto out;
73    }
74
75    struct ucred *cred = (struct ucred *)CMSG_DATA(cmsg);
76    *user = cred->uid;
77    if (cred->uid != 0) {
78        /* ignoring netlink message from non-root user */
79        goto out;
80    }
81
82    if (addr.nl_groups == 0 || addr.nl_pid != 0) {
83        /* ignoring non-kernel or unicast netlink message */
84        goto out;
85    }
86
87    return n;
88
89out:
90    /* clear residual potentially malicious data */
91    bzero(buffer, length);
92    errno = EIO;
93    return -1;
94}
95
96int uevent_open_socket(int buf_sz, bool passcred)
97{
98    struct sockaddr_nl addr;
99    int on = passcred;
100    int s;
101
102    memset(&addr, 0, sizeof(addr));
103    addr.nl_family = AF_NETLINK;
104    addr.nl_pid = getpid();
105    addr.nl_groups = 0xffffffff;
106
107    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
108    if(s < 0)
109        return -1;
110
111    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &buf_sz, sizeof(buf_sz));
112    setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
113
114    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
115        close(s);
116        return -1;
117    }
118
119    return s;
120}
121