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 #pragma once
18
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include <type_traits>
23 #include <vector>
24
25 #include <android-base/collections.h>
26 #include <android-base/macros.h>
27 #include <android-base/unique_fd.h>
28
29 namespace android {
30 namespace base {
31
32 #if !defined(_WIN32)
33
34 // Helpers for sending and receiving file descriptors across Unix domain sockets.
35 //
36 // The cmsg(3) API is very hard to get right, with multiple landmines that can
37 // lead to death. Almost all of the uses of cmsg in Android make at least one of
38 // the following mistakes:
39 //
40 // - not aligning the cmsg buffer
41 // - leaking fds if more fds are received than expected
42 // - blindly dereferencing CMSG_DATA without checking the header
43 // - using CMSG_SPACE instead of CMSG_LEN for .cmsg_len
44 // - using CMSG_LEN instead of CMSG_SPACE for .msg_controllen
45 // - using a length specified in number of fds instead of bytes
46 //
47 // These functions wrap the hard-to-use cmsg API with an easier to use abstraction.
48
49 // Send file descriptors across a Unix domain socket.
50 //
51 // Note that the write can return short if the socket type is SOCK_STREAM. When
52 // this happens, file descriptors are still sent to the other end, but with
53 // truncated data. For this reason, using SOCK_SEQPACKET or SOCK_DGRAM is recommended.
54 ssize_t SendFileDescriptorVector(borrowed_fd sock, const void* data, size_t len,
55 const std::vector<int>& fds);
56
57 // Receive file descriptors from a Unix domain socket.
58 //
59 // If more FDs (or bytes, for datagram sockets) are received than expected,
60 // -1 is returned with errno set to EMSGSIZE, and all received FDs are thrown away.
61 ssize_t ReceiveFileDescriptorVector(borrowed_fd sock, void* data, size_t len, size_t max_fds,
62 std::vector<android::base::unique_fd>* fds);
63
64 // Helper for SendFileDescriptorVector that constructs a std::vector for you, e.g.:
65 // SendFileDescriptors(sock, "foo", 3, std::move(fd1), std::move(fd2))
66 template <typename... Args>
SendFileDescriptors(borrowed_fd sock,const void * data,size_t len,Args &&...sent_fds)67 ssize_t SendFileDescriptors(borrowed_fd sock, const void* data, size_t len, Args&&... sent_fds) {
68 // Do not allow implicit conversion to int: people might try to do something along the lines of:
69 // SendFileDescriptors(..., std::move(a_unique_fd))
70 // and be surprised when the unique_fd isn't closed afterwards.
71 AssertType<int>(std::forward<Args>(sent_fds)...);
72 std::vector<int> fds;
73 Append(fds, std::forward<Args>(sent_fds)...);
74 return SendFileDescriptorVector(sock, data, len, fds);
75 }
76
77 // Helper for ReceiveFileDescriptorVector that receives an exact number of file descriptors.
78 // If more file descriptors are received than requested, -1 is returned with errno set to EMSGSIZE.
79 // If fewer file descriptors are received than requested, -1 is returned with errno set to ENOMSG.
80 // In both cases, all arguments are cleared and any received FDs are thrown away.
81 template <typename... Args>
ReceiveFileDescriptors(borrowed_fd sock,void * data,size_t len,Args &&...received_fds)82 ssize_t ReceiveFileDescriptors(borrowed_fd sock, void* data, size_t len, Args&&... received_fds) {
83 std::vector<unique_fd*> fds;
84 Append(fds, std::forward<Args>(received_fds)...);
85
86 std::vector<unique_fd> result;
87 ssize_t rc = ReceiveFileDescriptorVector(sock, data, len, fds.size(), &result);
88 if (rc == -1 || result.size() != fds.size()) {
89 int err = rc == -1 ? errno : ENOMSG;
90 for (unique_fd* fd : fds) {
91 fd->reset();
92 }
93 errno = err;
94 return -1;
95 }
96
97 for (size_t i = 0; i < fds.size(); ++i) {
98 *fds[i] = std::move(result[i]);
99 }
100 return rc;
101 }
102
103 #endif
104
105 } // namespace base
106 } // namespace android
107