cmsg.cpp 5.3 KB

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