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 | |
| 19 | #include <alloca.h> |
| 20 | #include <errno.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, |
| 49 | .msg_controllen = cmsg_space, |
| 50 | .msg_flags = 0, |
| 51 | }; |
| 52 | |
| 53 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 54 | cmsg->cmsg_level = SOL_SOCKET; |
| 55 | cmsg->cmsg_type = SCM_RIGHTS; |
| 56 | cmsg->cmsg_len = cmsg_len; |
| 57 | |
| 58 | int* cmsg_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 59 | for (size_t i = 0; i < fds.size(); ++i) { |
| 60 | cmsg_fds[i] = fds[i]; |
| 61 | } |
| 62 | |
| 63 | return TEMP_FAILURE_RETRY(sendmsg(sockfd, &msg, MSG_NOSIGNAL)); |
| 64 | } |
| 65 | |
| 66 | ssize_t ReceiveFileDescriptorVector(int sockfd, void* data, size_t len, size_t max_fds, |
| 67 | std::vector<unique_fd>* fds) { |
| 68 | fds->clear(); |
| 69 | |
| 70 | size_t cmsg_space = CMSG_SPACE(sizeof(int) * max_fds); |
| 71 | if (cmsg_space >= PAGE_SIZE) { |
| 72 | errno = ENOMEM; |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | alignas(struct cmsghdr) char cmsg_buf[cmsg_space]; |
| 77 | iovec iov = {.iov_base = const_cast<void*>(data), .iov_len = len}; |
| 78 | msghdr msg = { |
| 79 | .msg_name = nullptr, |
| 80 | .msg_namelen = 0, |
| 81 | .msg_iov = &iov, |
| 82 | .msg_iovlen = 1, |
| 83 | .msg_control = cmsg_buf, |
| 84 | .msg_controllen = cmsg_space, |
| 85 | .msg_flags = 0, |
| 86 | }; |
| 87 | |
| 88 | ssize_t rc = TEMP_FAILURE_RETRY( |
| 89 | recvmsg(sockfd, &msg, MSG_TRUNC | MSG_CTRUNC | MSG_CMSG_CLOEXEC | MSG_NOSIGNAL)); |
| 90 | if (rc == -1) { |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | int error = 0; |
| 95 | if ((msg.msg_flags & MSG_TRUNC)) { |
| 96 | LOG(ERROR) << "message was truncated when receiving file descriptors"; |
| 97 | error = EMSGSIZE; |
| 98 | } else if ((msg.msg_flags & MSG_CTRUNC)) { |
| 99 | LOG(ERROR) << "control message was truncated when receiving file descriptors"; |
| 100 | error = EMSGSIZE; |
| 101 | } |
| 102 | |
| 103 | std::vector<unique_fd> received_fds; |
| 104 | struct cmsghdr* cmsg; |
| 105 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 106 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
| 107 | LOG(ERROR) << "received unexpected cmsg: [" << cmsg->cmsg_level << ", " << cmsg->cmsg_type |
| 108 | << "]"; |
| 109 | error = EBADMSG; |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | // There isn't a macro that does the inverse of CMSG_LEN, so hack around it ourselves, with |
| 114 | // some static asserts to ensure that CMSG_LEN behaves as we expect. |
| 115 | static_assert(CMSG_LEN(0) + 1 * sizeof(int) == CMSG_LEN(1 * sizeof(int))); |
| 116 | static_assert(CMSG_LEN(0) + 2 * sizeof(int) == CMSG_LEN(2 * sizeof(int))); |
| 117 | static_assert(CMSG_LEN(0) + 3 * sizeof(int) == CMSG_LEN(3 * sizeof(int))); |
| 118 | static_assert(CMSG_LEN(0) + 4 * sizeof(int) == CMSG_LEN(4 * sizeof(int))); |
| 119 | |
| 120 | if (cmsg->cmsg_len % sizeof(int) != 0) { |
| 121 | LOG(FATAL) << "cmsg_len(" << cmsg->cmsg_len << ") not aligned to sizeof(int)"; |
| 122 | } else if (cmsg->cmsg_len <= CMSG_LEN(0)) { |
| 123 | LOG(FATAL) << "cmsg_len(" << cmsg->cmsg_len << ") not long enough to hold any data"; |
| 124 | } |
| 125 | |
| 126 | int* cmsg_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 127 | size_t cmsg_fdcount = static_cast<size_t>(cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); |
| 128 | for (size_t i = 0; i < cmsg_fdcount; ++i) { |
| 129 | received_fds.emplace_back(cmsg_fds[i]); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (error != 0) { |
| 134 | errno = error; |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | if (received_fds.size() > max_fds) { |
| 139 | LOG(ERROR) << "received too many file descriptors, expected " << fds->size() << ", received " |
| 140 | << received_fds.size(); |
| 141 | errno = EMSGSIZE; |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | *fds = std::move(received_fds); |
| 146 | return rc; |
| 147 | } |
| 148 | |
| 149 | } // namespace base |
| 150 | } // namespace android |