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 <errno.h>
20 #include <fcntl.h>
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
SendFileDescriptorVector(borrowed_fd sockfd,const void * data,size_t len,const std::vector<int> & fds)32 ssize_t SendFileDescriptorVector(borrowed_fd sockfd, const void* data, size_t len,
33 const std::vector<int>& fds) {
34 static const size_t page_size = sysconf(_SC_PAGE_SIZE);
35 size_t cmsg_space = CMSG_SPACE(sizeof(int) * fds.size());
36 size_t cmsg_len = CMSG_LEN(sizeof(int) * fds.size());
37 if (cmsg_space >= page_size) {
38 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,
50 // 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),
52 .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
65 #if defined(__linux__)
66 int flags = MSG_NOSIGNAL;
67 #else
68 int flags = 0;
69 #endif
70
71 return TEMP_FAILURE_RETRY(sendmsg(sockfd.get(), &msg, flags));
72 }
73
ReceiveFileDescriptorVector(borrowed_fd sockfd,void * data,size_t len,size_t max_fds,std::vector<unique_fd> * fds)74 ssize_t ReceiveFileDescriptorVector(borrowed_fd sockfd, void* data, size_t len, size_t max_fds,
75 std::vector<unique_fd>* fds) {
76 fds->clear();
77
78 static const size_t page_size = sysconf(_SC_PAGE_SIZE);
79 size_t cmsg_space = CMSG_SPACE(sizeof(int) * max_fds);
80 if (cmsg_space >= page_size) {
81 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,
93 // 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),
95 .msg_flags = 0,
96 };
97
98 int flags = MSG_TRUNC | MSG_CTRUNC;
99 #if defined(__linux__)
100 flags |= MSG_CMSG_CLOEXEC | MSG_NOSIGNAL;
101 #endif
102
103 ssize_t rc = TEMP_FAILURE_RETRY(recvmsg(sockfd.get(), &msg, flags));
104
105 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
129 // 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)));
140
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) {
150 #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
154 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
176