blob: 95db303349200e3ba8985530fc340d34a9c18249 [file] [log] [blame]
Josh Gaoc50f38f2019-01-07 19:16:21 -08001/*
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 Gaoc50f38f2019-01-07 19:16:21 -080019#include <errno.h>
Josh Gao23de6f62019-02-19 14:27:49 -080020#include <fcntl.h>
Josh Gaoc50f38f2019-01-07 19:16:21 -080021#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
29namespace android {
30namespace base {
31
Josh Gaoe4401992019-04-25 14:04:57 -070032ssize_t SendFileDescriptorVector(borrowed_fd sockfd, const void* data, size_t len,
Josh Gaoc50f38f2019-01-07 19:16:21 -080033 const std::vector<int>& fds) {
Dan Willemsen15ec2c72020-07-14 17:25:43 -070034 static const size_t page_size = sysconf(_SC_PAGE_SIZE);
Josh Gaoc50f38f2019-01-07 19:16:21 -080035 size_t cmsg_space = CMSG_SPACE(sizeof(int) * fds.size());
36 size_t cmsg_len = CMSG_LEN(sizeof(int) * fds.size());
Dan Willemsen15ec2c72020-07-14 17:25:43 -070037 if (cmsg_space >= page_size) {
Josh Gaoc50f38f2019-01-07 19:16:21 -080038 errno = ENOMEM;
39 return -1;
40 }
41
42 alignas(struct cmsghdr) char cmsg_buf[cmsg_space];
43 iovec iov = {.iov_base = const_cast<void*>(data), .iov_len = len};
44 msghdr msg = {
45 .msg_name = nullptr,
46 .msg_namelen = 0,
47 .msg_iov = &iov,
48 .msg_iovlen = 1,
49 .msg_control = cmsg_buf,
Josh Gao23de6f62019-02-19 14:27:49 -080050 // We can't cast to the actual type of the field, because it's different across platforms.
51 .msg_controllen = static_cast<unsigned int>(cmsg_space),
Josh Gaoc50f38f2019-01-07 19:16:21 -080052 .msg_flags = 0,
53 };
54
55 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
56 cmsg->cmsg_level = SOL_SOCKET;
57 cmsg->cmsg_type = SCM_RIGHTS;
58 cmsg->cmsg_len = cmsg_len;
59
60 int* cmsg_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
61 for (size_t i = 0; i < fds.size(); ++i) {
62 cmsg_fds[i] = fds[i];
63 }
64
Josh Gao23de6f62019-02-19 14:27:49 -080065#if defined(__linux__)
66 int flags = MSG_NOSIGNAL;
67#else
68 int flags = 0;
69#endif
70
Josh Gaoe4401992019-04-25 14:04:57 -070071 return TEMP_FAILURE_RETRY(sendmsg(sockfd.get(), &msg, flags));
Josh Gaoc50f38f2019-01-07 19:16:21 -080072}
73
Josh Gaoe4401992019-04-25 14:04:57 -070074ssize_t ReceiveFileDescriptorVector(borrowed_fd sockfd, void* data, size_t len, size_t max_fds,
Josh Gaoc50f38f2019-01-07 19:16:21 -080075 std::vector<unique_fd>* fds) {
76 fds->clear();
77
Dan Willemsen15ec2c72020-07-14 17:25:43 -070078 static const size_t page_size = sysconf(_SC_PAGE_SIZE);
Josh Gaoc50f38f2019-01-07 19:16:21 -080079 size_t cmsg_space = CMSG_SPACE(sizeof(int) * max_fds);
Dan Willemsen15ec2c72020-07-14 17:25:43 -070080 if (cmsg_space >= page_size) {
Josh Gaoc50f38f2019-01-07 19:16:21 -080081 errno = ENOMEM;
82 return -1;
83 }
84
85 alignas(struct cmsghdr) char cmsg_buf[cmsg_space];
86 iovec iov = {.iov_base = const_cast<void*>(data), .iov_len = len};
87 msghdr msg = {
88 .msg_name = nullptr,
89 .msg_namelen = 0,
90 .msg_iov = &iov,
91 .msg_iovlen = 1,
92 .msg_control = cmsg_buf,
Josh Gao23de6f62019-02-19 14:27:49 -080093 // We can't cast to the actual type of the field, because it's different across platforms.
94 .msg_controllen = static_cast<unsigned int>(cmsg_space),
Josh Gaoc50f38f2019-01-07 19:16:21 -080095 .msg_flags = 0,
96 };
97
Josh Gao23de6f62019-02-19 14:27:49 -080098 int flags = MSG_TRUNC | MSG_CTRUNC;
99#if defined(__linux__)
100 flags |= MSG_CMSG_CLOEXEC | MSG_NOSIGNAL;
101#endif
102
Josh Gaoe4401992019-04-25 14:04:57 -0700103 ssize_t rc = TEMP_FAILURE_RETRY(recvmsg(sockfd.get(), &msg, flags));
Josh Gao23de6f62019-02-19 14:27:49 -0800104
Josh Gaoc50f38f2019-01-07 19:16:21 -0800105 if (rc == -1) {
106 return -1;
107 }
108
109 int error = 0;
110 if ((msg.msg_flags & MSG_TRUNC)) {
111 LOG(ERROR) << "message was truncated when receiving file descriptors";
112 error = EMSGSIZE;
113 } else if ((msg.msg_flags & MSG_CTRUNC)) {
114 LOG(ERROR) << "control message was truncated when receiving file descriptors";
115 error = EMSGSIZE;
116 }
117
118 std::vector<unique_fd> received_fds;
119 struct cmsghdr* cmsg;
120 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
121 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
122 LOG(ERROR) << "received unexpected cmsg: [" << cmsg->cmsg_level << ", " << cmsg->cmsg_type
123 << "]";
124 error = EBADMSG;
125 continue;
126 }
127
128 // There isn't a macro that does the inverse of CMSG_LEN, so hack around it ourselves, with
Josh Gao23de6f62019-02-19 14:27:49 -0800129 // some asserts to ensure that CMSG_LEN behaves as we expect.
130#if defined(__linux__)
131#define CMSG_ASSERT static_assert
132#else
133// CMSG_LEN is somehow not constexpr on darwin.
134#define CMSG_ASSERT CHECK
135#endif
136 CMSG_ASSERT(CMSG_LEN(0) + 1 * sizeof(int) == CMSG_LEN(1 * sizeof(int)));
137 CMSG_ASSERT(CMSG_LEN(0) + 2 * sizeof(int) == CMSG_LEN(2 * sizeof(int)));
138 CMSG_ASSERT(CMSG_LEN(0) + 3 * sizeof(int) == CMSG_LEN(3 * sizeof(int)));
139 CMSG_ASSERT(CMSG_LEN(0) + 4 * sizeof(int) == CMSG_LEN(4 * sizeof(int)));
Josh Gaoc50f38f2019-01-07 19:16:21 -0800140
141 if (cmsg->cmsg_len % sizeof(int) != 0) {
142 LOG(FATAL) << "cmsg_len(" << cmsg->cmsg_len << ") not aligned to sizeof(int)";
143 } else if (cmsg->cmsg_len <= CMSG_LEN(0)) {
144 LOG(FATAL) << "cmsg_len(" << cmsg->cmsg_len << ") not long enough to hold any data";
145 }
146
147 int* cmsg_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
148 size_t cmsg_fdcount = static_cast<size_t>(cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
149 for (size_t i = 0; i < cmsg_fdcount; ++i) {
Josh Gao23de6f62019-02-19 14:27:49 -0800150#if !defined(__linux__)
151 // Linux uses MSG_CMSG_CLOEXEC instead of doing this manually.
152 fcntl(cmsg_fds[i], F_SETFD, FD_CLOEXEC);
153#endif
Josh Gaoc50f38f2019-01-07 19:16:21 -0800154 received_fds.emplace_back(cmsg_fds[i]);
155 }
156 }
157
158 if (error != 0) {
159 errno = error;
160 return -1;
161 }
162
163 if (received_fds.size() > max_fds) {
164 LOG(ERROR) << "received too many file descriptors, expected " << fds->size() << ", received "
165 << received_fds.size();
166 errno = EMSGSIZE;
167 return -1;
168 }
169
170 *fds = std::move(received_fds);
171 return rc;
172}
173
174} // namespace base
175} // namespace android