| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 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 <android-base/cmsg.h> | 
|  | 18 |  | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 19 | #include <errno.h> | 
| Josh Gao | d338738 | 2019-02-19 14:27:49 -0800 | [diff] [blame] | 20 | #include <fcntl.h> | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 21 | #include <stdlib.h> | 
|  | 22 | #include <sys/socket.h> | 
|  | 23 | #include <sys/user.h> | 
|  | 24 |  | 
|  | 25 | #include <memory> | 
|  | 26 |  | 
|  | 27 | #include <android-base/logging.h> | 
|  | 28 |  | 
|  | 29 | namespace android { | 
|  | 30 | namespace base { | 
|  | 31 |  | 
|  | 32 | ssize_t SendFileDescriptorVector(int sockfd, const void* data, size_t len, | 
|  | 33 | const std::vector<int>& fds) { | 
|  | 34 | size_t cmsg_space = CMSG_SPACE(sizeof(int) * fds.size()); | 
|  | 35 | size_t cmsg_len = CMSG_LEN(sizeof(int) * fds.size()); | 
|  | 36 | if (cmsg_space >= PAGE_SIZE) { | 
|  | 37 | errno = ENOMEM; | 
|  | 38 | return -1; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | alignas(struct cmsghdr) char cmsg_buf[cmsg_space]; | 
|  | 42 | iovec iov = {.iov_base = const_cast<void*>(data), .iov_len = len}; | 
|  | 43 | msghdr msg = { | 
|  | 44 | .msg_name = nullptr, | 
|  | 45 | .msg_namelen = 0, | 
|  | 46 | .msg_iov = &iov, | 
|  | 47 | .msg_iovlen = 1, | 
|  | 48 | .msg_control = cmsg_buf, | 
| Josh Gao | d338738 | 2019-02-19 14:27:49 -0800 | [diff] [blame] | 49 | // We can't cast to the actual type of the field, because it's different across platforms. | 
|  | 50 | .msg_controllen = static_cast<unsigned int>(cmsg_space), | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 51 | .msg_flags = 0, | 
|  | 52 | }; | 
|  | 53 |  | 
|  | 54 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); | 
|  | 55 | cmsg->cmsg_level = SOL_SOCKET; | 
|  | 56 | cmsg->cmsg_type = SCM_RIGHTS; | 
|  | 57 | cmsg->cmsg_len = cmsg_len; | 
|  | 58 |  | 
|  | 59 | int* cmsg_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 
|  | 60 | for (size_t i = 0; i < fds.size(); ++i) { | 
|  | 61 | cmsg_fds[i] = fds[i]; | 
|  | 62 | } | 
|  | 63 |  | 
| Josh Gao | d338738 | 2019-02-19 14:27:49 -0800 | [diff] [blame] | 64 | #if defined(__linux__) | 
|  | 65 | int flags = MSG_NOSIGNAL; | 
|  | 66 | #else | 
|  | 67 | int flags = 0; | 
|  | 68 | #endif | 
|  | 69 |  | 
|  | 70 | return TEMP_FAILURE_RETRY(sendmsg(sockfd, &msg, flags)); | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
|  | 73 | ssize_t ReceiveFileDescriptorVector(int sockfd, void* data, size_t len, size_t max_fds, | 
|  | 74 | std::vector<unique_fd>* fds) { | 
|  | 75 | fds->clear(); | 
|  | 76 |  | 
|  | 77 | size_t cmsg_space = CMSG_SPACE(sizeof(int) * max_fds); | 
|  | 78 | if (cmsg_space >= PAGE_SIZE) { | 
|  | 79 | errno = ENOMEM; | 
|  | 80 | return -1; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | alignas(struct cmsghdr) char cmsg_buf[cmsg_space]; | 
|  | 84 | iovec iov = {.iov_base = const_cast<void*>(data), .iov_len = len}; | 
|  | 85 | msghdr msg = { | 
|  | 86 | .msg_name = nullptr, | 
|  | 87 | .msg_namelen = 0, | 
|  | 88 | .msg_iov = &iov, | 
|  | 89 | .msg_iovlen = 1, | 
|  | 90 | .msg_control = cmsg_buf, | 
| Josh Gao | d338738 | 2019-02-19 14:27:49 -0800 | [diff] [blame] | 91 | // We can't cast to the actual type of the field, because it's different across platforms. | 
|  | 92 | .msg_controllen = static_cast<unsigned int>(cmsg_space), | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 93 | .msg_flags = 0, | 
|  | 94 | }; | 
|  | 95 |  | 
| Josh Gao | d338738 | 2019-02-19 14:27:49 -0800 | [diff] [blame] | 96 | int flags = MSG_TRUNC | MSG_CTRUNC; | 
|  | 97 | #if defined(__linux__) | 
|  | 98 | flags |= MSG_CMSG_CLOEXEC | MSG_NOSIGNAL; | 
|  | 99 | #endif | 
|  | 100 |  | 
|  | 101 | ssize_t rc = TEMP_FAILURE_RETRY(recvmsg(sockfd, &msg, flags)); | 
|  | 102 |  | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 103 | if (rc == -1) { | 
|  | 104 | return -1; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | int error = 0; | 
|  | 108 | if ((msg.msg_flags & MSG_TRUNC)) { | 
|  | 109 | LOG(ERROR) << "message was truncated when receiving file descriptors"; | 
|  | 110 | error = EMSGSIZE; | 
|  | 111 | } else if ((msg.msg_flags & MSG_CTRUNC)) { | 
|  | 112 | LOG(ERROR) << "control message was truncated when receiving file descriptors"; | 
|  | 113 | error = EMSGSIZE; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | std::vector<unique_fd> received_fds; | 
|  | 117 | struct cmsghdr* cmsg; | 
|  | 118 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | 
|  | 119 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { | 
|  | 120 | LOG(ERROR) << "received unexpected cmsg: [" << cmsg->cmsg_level << ", " << cmsg->cmsg_type | 
|  | 121 | << "]"; | 
|  | 122 | error = EBADMSG; | 
|  | 123 | continue; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | // There isn't a macro that does the inverse of CMSG_LEN, so hack around it ourselves, with | 
| Josh Gao | d338738 | 2019-02-19 14:27:49 -0800 | [diff] [blame] | 127 | // some asserts to ensure that CMSG_LEN behaves as we expect. | 
|  | 128 | #if defined(__linux__) | 
|  | 129 | #define CMSG_ASSERT static_assert | 
|  | 130 | #else | 
|  | 131 | // CMSG_LEN is somehow not constexpr on darwin. | 
|  | 132 | #define CMSG_ASSERT CHECK | 
|  | 133 | #endif | 
|  | 134 | CMSG_ASSERT(CMSG_LEN(0) + 1 * sizeof(int) == CMSG_LEN(1 * sizeof(int))); | 
|  | 135 | CMSG_ASSERT(CMSG_LEN(0) + 2 * sizeof(int) == CMSG_LEN(2 * sizeof(int))); | 
|  | 136 | CMSG_ASSERT(CMSG_LEN(0) + 3 * sizeof(int) == CMSG_LEN(3 * sizeof(int))); | 
|  | 137 | CMSG_ASSERT(CMSG_LEN(0) + 4 * sizeof(int) == CMSG_LEN(4 * sizeof(int))); | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 138 |  | 
|  | 139 | if (cmsg->cmsg_len % sizeof(int) != 0) { | 
|  | 140 | LOG(FATAL) << "cmsg_len(" << cmsg->cmsg_len << ") not aligned to sizeof(int)"; | 
|  | 141 | } else if (cmsg->cmsg_len <= CMSG_LEN(0)) { | 
|  | 142 | LOG(FATAL) << "cmsg_len(" << cmsg->cmsg_len << ") not long enough to hold any data"; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | int* cmsg_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 
|  | 146 | size_t cmsg_fdcount = static_cast<size_t>(cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); | 
|  | 147 | for (size_t i = 0; i < cmsg_fdcount; ++i) { | 
| Josh Gao | d338738 | 2019-02-19 14:27:49 -0800 | [diff] [blame] | 148 | #if !defined(__linux__) | 
|  | 149 | // Linux uses MSG_CMSG_CLOEXEC instead of doing this manually. | 
|  | 150 | fcntl(cmsg_fds[i], F_SETFD, FD_CLOEXEC); | 
|  | 151 | #endif | 
| Josh Gao | 14f9500 | 2019-01-07 19:16:21 -0800 | [diff] [blame] | 152 | received_fds.emplace_back(cmsg_fds[i]); | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | if (error != 0) { | 
|  | 157 | errno = error; | 
|  | 158 | return -1; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | if (received_fds.size() > max_fds) { | 
|  | 162 | LOG(ERROR) << "received too many file descriptors, expected " << fds->size() << ", received " | 
|  | 163 | << received_fds.size(); | 
|  | 164 | errno = EMSGSIZE; | 
|  | 165 | return -1; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | *fds = std::move(received_fds); | 
|  | 169 | return rc; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | }  // namespace base | 
|  | 173 | }  // namespace android |