1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <string.h>
30 #include <sys/socket.h>
31 
32 #include <async_safe/log.h>
33 
34 #include "private/bionic_fdtrack.h"
35 
36 extern "C" ssize_t __recvmsg(int __fd, struct msghdr* __msg, int __flags);
37 extern "C" int __recvmmsg(int __fd, struct mmsghdr* __msgs, unsigned int __msg_count, int __flags,
38                           const struct timespec* __timeout);
39 
track_fds(struct msghdr * msg,const char * function_name)40 static inline __attribute__((artificial)) __attribute__((always_inline)) void track_fds(
41     struct msghdr* msg, const char* function_name) {
42   if (!atomic_load(&__android_fdtrack_hook)) {
43     return;
44   }
45 
46   for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
47     if (cmsg->cmsg_type != SCM_RIGHTS) {
48       continue;
49     }
50 
51     if (cmsg->cmsg_len <= sizeof(struct cmsghdr)) {
52       continue;
53     }
54 
55     size_t data_length = cmsg->cmsg_len - sizeof(struct cmsghdr);
56     if (data_length % sizeof(int) != 0) {
57       async_safe_fatal("invalid cmsg length: %zu", data_length);
58     }
59 
60     for (size_t offset = 0; offset < data_length; offset += sizeof(int)) {
61       int fd;
62       memcpy(&fd, CMSG_DATA(cmsg) + offset, sizeof(int));
63       FDTRACK_CREATE_NAME(function_name, fd);
64     }
65   }
66 }
67 
recvmsg(int __fd,struct msghdr * __msg,int __flags)68 ssize_t recvmsg(int __fd, struct msghdr* __msg, int __flags) {
69   ssize_t rc = __recvmsg(__fd, __msg, __flags);
70   if (rc == -1) {
71     return -1;
72   }
73   track_fds(__msg, "recvmsg");
74   return rc;
75 }
76 
recvmmsg(int __fd,struct mmsghdr * __msgs,unsigned int __msg_count,int __flags,const struct timespec * __timeout)77 int recvmmsg(int __fd, struct mmsghdr* __msgs, unsigned int __msg_count, int __flags,
78              const struct timespec* __timeout) {
79   int rc = __recvmmsg(__fd, __msgs, __msg_count, __flags, __timeout);
80   if (rc == -1) {
81     return -1;
82   }
83   for (int i = 0; i < rc; ++i) {
84     track_fds(&__msgs[i].msg_hdr, "recvmmsg");
85   }
86   return rc;
87 }
88