1 /*
2  * Copyright (C) 2024 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 "host/commands/process_sandboxer/proxy_common.h"
17 
18 #include <sys/socket.h>
19 
20 #include <cstdlib>
21 #include <string>
22 
23 #include <absl/status/status.h>
24 #include <absl/status/statusor.h>
25 #include <absl/strings/numbers.h>
26 
27 namespace cuttlefish::process_sandboxer {
28 
RecvFrom(int sock)29 absl::StatusOr<Message> Message::RecvFrom(int sock) {
30   msghdr empty_hdr = {};
31   int len = recvmsg(sock, &empty_hdr, MSG_PEEK | MSG_TRUNC);
32   if (len < 0) {
33     return absl::ErrnoToStatus(errno, "recvmsg with MSG_PEEK failed");
34   }
35 
36   Message message;
37   message.data_ = std::string(len, '\0');
38 
39   iovec msg_iovec = iovec{
40       .iov_base = reinterpret_cast<void*>(message.data_.data()),
41       .iov_len = static_cast<size_t>(len),
42   };
43 
44   union {
45     char buf[CMSG_SPACE(sizeof(ucred))];
46     struct cmsghdr align;
47   } cmsg_data;
48   std::memset(cmsg_data.buf, 0, sizeof(cmsg_data.buf));
49 
50   msghdr hdr = msghdr{
51       .msg_iov = &msg_iovec,
52       .msg_iovlen = 1,
53       .msg_control = cmsg_data.buf,
54       .msg_controllen = sizeof(cmsg_data.buf),
55   };
56 
57   auto recvmsg_ret = recvmsg(sock, &hdr, 0);
58   if (recvmsg_ret < 0) {
59     return absl::ErrnoToStatus(errno, "recvmsg failed");
60   }
61 
62   for (auto cmsg = CMSG_FIRSTHDR(&hdr); cmsg != nullptr;
63        cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
64     if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) {
65       message.credentials_ = *(ucred*)CMSG_DATA(cmsg);
66     }
67   }
68 
69   return message;
70 }
71 
Data() const72 const std::string& Message::Data() const { return data_; }
73 
Credentials() const74 const std::optional<ucred>& Message::Credentials() const {
75   return credentials_;
76 }
77 
SendStringMsg(int sock,std::string_view msg)78 absl::StatusOr<size_t> SendStringMsg(int sock, std::string_view msg) {
79   iovec msg_iovec = iovec{
80       .iov_base = (void*)msg.data(),
81       .iov_len = msg.length(),
82   };
83 
84   msghdr hdr = msghdr{
85       .msg_iov = &msg_iovec,
86       .msg_iovlen = 1,
87   };
88 
89   auto ret = sendmsg(sock, &hdr, MSG_EOR | MSG_NOSIGNAL);
90   return ret >= 0 ? absl::StatusOr<size_t>(ret)
91                   : absl::ErrnoToStatus(errno, "sendmsg failed");
92 }
93 
94 }  // namespace cuttlefish::process_sandboxer
95