Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 specic language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 17 | #include "libappfuse/FuseBuffer.h" |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 18 | |
| 19 | #include <inttypes.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <algorithm> |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 24 | #include <type_traits> |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 25 | |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 26 | #include <sys/socket.h> |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 27 | #include <sys/uio.h> |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 28 | |
Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 29 | #include <android-base/file.h> |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/macros.h> |
| 32 | |
| 33 | namespace android { |
Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 34 | namespace fuse { |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 35 | namespace { |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 36 | |
| 37 | template <typename T> |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 38 | bool CheckHeaderLength(const FuseMessage<T>* self, const char* name, size_t max_size) { |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 39 | const auto& header = static_cast<const T*>(self)->header; |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 40 | if (header.len >= sizeof(header) && header.len <= max_size) { |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 41 | return true; |
| 42 | } else { |
| 43 | LOG(ERROR) << "Invalid header length is found in " << name << ": " << header.len; |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | template <typename T> |
| 49 | ResultOrAgain ReadInternal(FuseMessage<T>* self, int fd, int sockflag) { |
| 50 | char* const buf = reinterpret_cast<char*>(self); |
| 51 | const ssize_t result = sockflag ? TEMP_FAILURE_RETRY(recv(fd, buf, sizeof(T), sockflag)) |
| 52 | : TEMP_FAILURE_RETRY(read(fd, buf, sizeof(T))); |
| 53 | |
| 54 | switch (result) { |
| 55 | case 0: |
| 56 | // Expected EOF. |
| 57 | return ResultOrAgain::kFailure; |
| 58 | case -1: |
| 59 | if (errno == EAGAIN) { |
| 60 | return ResultOrAgain::kAgain; |
| 61 | } |
| 62 | PLOG(ERROR) << "Failed to read a FUSE message"; |
| 63 | return ResultOrAgain::kFailure; |
| 64 | } |
| 65 | |
| 66 | const auto& header = static_cast<const T*>(self)->header; |
| 67 | if (result < static_cast<ssize_t>(sizeof(header))) { |
| 68 | LOG(ERROR) << "Read bytes " << result << " are shorter than header size " << sizeof(header); |
| 69 | return ResultOrAgain::kFailure; |
| 70 | } |
| 71 | |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 72 | if (!CheckHeaderLength<T>(self, "Read", sizeof(T))) { |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 73 | return ResultOrAgain::kFailure; |
| 74 | } |
| 75 | |
| 76 | if (static_cast<uint32_t>(result) != header.len) { |
| 77 | LOG(ERROR) << "Read bytes " << result << " are different from header.len " << header.len; |
| 78 | return ResultOrAgain::kFailure; |
| 79 | } |
| 80 | |
| 81 | return ResultOrAgain::kSuccess; |
| 82 | } |
| 83 | |
| 84 | template <typename T> |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 85 | ResultOrAgain WriteInternal(const FuseMessage<T>* self, int fd, int sockflag, const void* data, |
| 86 | size_t max_size) { |
| 87 | if (!CheckHeaderLength<T>(self, "Write", max_size)) { |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 88 | return ResultOrAgain::kFailure; |
| 89 | } |
| 90 | |
| 91 | const char* const buf = reinterpret_cast<const char*>(self); |
| 92 | const auto& header = static_cast<const T*>(self)->header; |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 93 | |
| 94 | int result; |
| 95 | if (sockflag) { |
| 96 | CHECK(data == nullptr); |
| 97 | result = TEMP_FAILURE_RETRY(send(fd, buf, header.len, sockflag)); |
| 98 | } else if (data) { |
| 99 | const struct iovec vec[] = {{const_cast<char*>(buf), sizeof(header)}, |
| 100 | {const_cast<void*>(data), header.len - sizeof(header)}}; |
| 101 | result = TEMP_FAILURE_RETRY(writev(fd, vec, arraysize(vec))); |
| 102 | } else { |
| 103 | result = TEMP_FAILURE_RETRY(write(fd, buf, header.len)); |
| 104 | } |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 105 | |
| 106 | if (result == -1) { |
| 107 | if (errno == EAGAIN) { |
| 108 | return ResultOrAgain::kAgain; |
| 109 | } |
| 110 | PLOG(ERROR) << "Failed to write a FUSE message"; |
| 111 | return ResultOrAgain::kFailure; |
| 112 | } |
| 113 | |
| 114 | CHECK(static_cast<uint32_t>(result) == header.len); |
| 115 | return ResultOrAgain::kSuccess; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static_assert(std::is_standard_layout<FuseBuffer>::value, |
| 120 | "FuseBuffer must be standard layout union."); |
| 121 | |
| 122 | bool SetupMessageSockets(base::unique_fd (*result)[2]) { |
| 123 | base::unique_fd fds[2]; |
| 124 | { |
| 125 | int raw_fds[2]; |
| 126 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_fds) == -1) { |
| 127 | PLOG(ERROR) << "Failed to create sockets for proxy"; |
| 128 | return false; |
| 129 | } |
| 130 | fds[0].reset(raw_fds[0]); |
| 131 | fds[1].reset(raw_fds[1]); |
| 132 | } |
| 133 | |
| 134 | constexpr int kMaxMessageSize = sizeof(FuseBuffer); |
| 135 | if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &kMaxMessageSize, sizeof(int)) != 0 || |
| 136 | setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, &kMaxMessageSize, sizeof(int)) != 0) { |
| 137 | PLOG(ERROR) << "Failed to update buffer size for socket"; |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | (*result)[0] = std::move(fds[0]); |
| 142 | (*result)[1] = std::move(fds[1]); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 143 | return true; |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 144 | } |
| 145 | |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 146 | template <typename T> |
| 147 | bool FuseMessage<T>::Read(int fd) { |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 148 | return ReadInternal(this, fd, 0) == ResultOrAgain::kSuccess; |
| 149 | } |
Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 150 | |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 151 | template <typename T> |
| 152 | ResultOrAgain FuseMessage<T>::ReadOrAgain(int fd) { |
| 153 | return ReadInternal(this, fd, MSG_DONTWAIT); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 156 | template <typename T> |
| 157 | bool FuseMessage<T>::Write(int fd) const { |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 158 | return WriteInternal(this, fd, 0, nullptr, sizeof(T)) == ResultOrAgain::kSuccess; |
| 159 | } |
| 160 | |
| 161 | template <typename T> |
| 162 | bool FuseMessage<T>::WriteWithBody(int fd, size_t max_size, const void* data) const { |
| 163 | CHECK(data != nullptr); |
| 164 | return WriteInternal<T>(this, fd, 0, data, max_size) == ResultOrAgain::kSuccess; |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 165 | } |
Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 166 | |
Daichi Hirono | 57b780f | 2017-03-06 14:02:42 +0900 | [diff] [blame] | 167 | template <typename T> |
| 168 | ResultOrAgain FuseMessage<T>::WriteOrAgain(int fd) const { |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 169 | return WriteInternal(this, fd, MSG_DONTWAIT, nullptr, sizeof(T)); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 172 | void FuseRequest::Reset( |
| 173 | uint32_t data_length, uint32_t opcode, uint64_t unique) { |
| 174 | memset(this, 0, sizeof(fuse_in_header) + data_length); |
| 175 | header.len = sizeof(fuse_in_header) + data_length; |
| 176 | header.opcode = opcode; |
| 177 | header.unique = unique; |
| 178 | } |
| 179 | |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 180 | template <size_t N> |
| 181 | void FuseResponseBase<N>::ResetHeader(uint32_t data_length, int32_t error, uint64_t unique) { |
| 182 | CHECK_LE(error, 0) << "error should be zero or negative."; |
| 183 | header.len = sizeof(fuse_out_header) + data_length; |
| 184 | header.error = error; |
| 185 | header.unique = unique; |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 186 | } |
| 187 | |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 188 | template <size_t N> |
| 189 | void FuseResponseBase<N>::Reset(uint32_t data_length, int32_t error, uint64_t unique) { |
| 190 | memset(this, 0, sizeof(fuse_out_header) + data_length); |
| 191 | ResetHeader(data_length, error, unique); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void FuseBuffer::HandleInit() { |
| 195 | const fuse_init_in* const in = &request.init_in; |
| 196 | |
| 197 | // Before writing |out|, we need to copy data from |in|. |
| 198 | const uint64_t unique = request.header.unique; |
| 199 | const uint32_t minor = in->minor; |
| 200 | const uint32_t max_readahead = in->max_readahead; |
| 201 | |
| 202 | // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out |
| 203 | // defined (fuse version 7.6). The structure is the same from 7.6 through |
| 204 | // 7.22. Beginning with 7.23, the structure increased in size and added |
| 205 | // new parameters. |
| 206 | if (in->major != FUSE_KERNEL_VERSION || in->minor < 6) { |
| 207 | LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " << in->major |
| 208 | << "." << in->minor << " Expected at least " << FUSE_KERNEL_VERSION |
| 209 | << ".6"; |
| 210 | response.Reset(0, -EPERM, unique); |
| 211 | return; |
| 212 | } |
| 213 | |
Daichi Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame] | 214 | // We limit ourselves to minor=15 because we don't handle BATCH_FORGET yet. |
| 215 | // Thus we need to use FUSE_COMPAT_22_INIT_OUT_SIZE. |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 216 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
| 217 | // FUSE_KERNEL_VERSION >= 23. |
Daichi Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame] | 218 | const size_t response_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 219 | #else |
| 220 | const size_t response_size = sizeof(fuse_init_out); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 221 | #endif |
| 222 | |
| 223 | response.Reset(response_size, kFuseSuccess, unique); |
| 224 | fuse_init_out* const out = &response.init_out; |
| 225 | out->major = FUSE_KERNEL_VERSION; |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 226 | out->minor = std::min(minor, 15u); |
| 227 | out->max_readahead = max_readahead; |
| 228 | out->flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
| 229 | out->max_background = 32; |
| 230 | out->congestion_threshold = 32; |
| 231 | out->max_write = kFuseMaxWrite; |
| 232 | } |
| 233 | |
| 234 | void FuseBuffer::HandleNotImpl() { |
| 235 | LOG(VERBOSE) << "NOTIMPL op=" << request.header.opcode << " uniq=" |
| 236 | << request.header.unique << " nid=" << request.header.nodeid; |
| 237 | const uint64_t unique = request.header.unique; |
| 238 | response.Reset(0, -ENOSYS, unique); |
| 239 | } |
| 240 | |
Daichi Hirono | 0bb22bd | 2017-03-22 16:41:46 +0900 | [diff] [blame^] | 241 | template class FuseMessage<FuseRequest>; |
| 242 | template class FuseMessage<FuseResponse>; |
| 243 | template class FuseMessage<FuseSimpleResponse>; |
| 244 | template struct FuseResponseBase<0u>; |
| 245 | template struct FuseResponseBase<kFuseMaxRead>; |
| 246 | |
Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 247 | } // namespace fuse |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 248 | } // namespace android |