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