1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/platform_shared_memory_region.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <optional>
#include "base/check_op.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/threading/thread_restrictions.h"
#include "base/types/expected.h"
#include "build/build_config.h"
namespace base::subtle {
namespace {
struct ScopedPathUnlinkerTraits {
static const FilePath* InvalidValue() { return nullptr; }
static void Free(const FilePath* path) {
if (unlink(path->value().c_str())) {
PLOG(WARNING) << "unlink";
}
}
};
// Unlinks the FilePath when the object is destroyed.
using ScopedPathUnlinker =
ScopedGeneric<const FilePath*, ScopedPathUnlinkerTraits>;
enum class FDAccessModeError {
kFcntlFailed,
kMismatch,
};
std::optional<FDAccessModeError> CheckFDAccessMode(int fd, int expected_mode) {
int fd_status = fcntl(fd, F_GETFL);
if (fd_status == -1) {
// TODO(crbug.com/40574272): convert to DLOG when bug fixed.
PLOG(ERROR) << "fcntl(" << fd << ", F_GETFL) failed";
return FDAccessModeError::kFcntlFailed;
}
int mode = fd_status & O_ACCMODE;
if (mode != expected_mode) {
return FDAccessModeError::kMismatch;
}
return std::nullopt;
}
} // namespace
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// static
ScopedFD PlatformSharedMemoryRegion::ExecutableRegion::CreateFD(size_t size) {
PlatformSharedMemoryRegion region =
Create(Mode::kUnsafe, size, true /* executable */);
if (region.IsValid()) {
return region.PassPlatformHandle().fd;
}
return ScopedFD();
}
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// static
expected<PlatformSharedMemoryRegion, PlatformSharedMemoryRegion::TakeError>
PlatformSharedMemoryRegion::TakeOrFail(ScopedFDPair handle,
Mode mode,
size_t size,
const UnguessableToken& guid) {
if (!handle.fd.is_valid()) {
return {};
}
if (size == 0) {
return {};
}
if (size > static_cast<size_t>(std::numeric_limits<int>::max())) {
return {};
}
expected<void, TakeError> result =
CheckPlatformHandlePermissionsCorrespondToMode(handle.get(), mode, size);
if (!result.has_value()) {
return unexpected(result.error());
}
switch (mode) {
case Mode::kReadOnly:
case Mode::kUnsafe:
// TODO(dcheng): This may not be reachable given the above.
if (handle.readonly_fd.is_valid()) {
handle.readonly_fd.reset();
DLOG(WARNING) << "Readonly handle shouldn't be valid for a "
"non-writable memory region; closing";
}
break;
case Mode::kWritable:
// TODO(dcheng): This may not be reachable given the above.
if (!handle.readonly_fd.is_valid()) {
DLOG(ERROR)
<< "Readonly handle must be valid for writable memory region";
return {};
}
break;
}
return PlatformSharedMemoryRegion(std::move(handle), mode, size, guid);
}
// static
PlatformSharedMemoryRegion PlatformSharedMemoryRegion::Take(
ScopedFD handle,
Mode mode,
size_t size,
const UnguessableToken& guid) {
CHECK_NE(mode, Mode::kWritable);
return Take(ScopedFDPair(std::move(handle), ScopedFD()), mode, size, guid);
}
FDPair PlatformSharedMemoryRegion::GetPlatformHandle() const {
return handle_.get();
}
bool PlatformSharedMemoryRegion::IsValid() const {
return handle_.fd.is_valid() &&
(mode_ == Mode::kWritable ? handle_.readonly_fd.is_valid() : true);
}
PlatformSharedMemoryRegion PlatformSharedMemoryRegion::Duplicate() const {
if (!IsValid()) {
return {};
}
CHECK_NE(mode_, Mode::kWritable)
<< "Duplicating a writable shared memory region is prohibited";
ScopedFD duped_fd(HANDLE_EINTR(dup(handle_.fd.get())));
if (!duped_fd.is_valid()) {
DPLOG(ERROR) << "dup(" << handle_.fd.get() << ") failed";
return {};
}
return PlatformSharedMemoryRegion({std::move(duped_fd), ScopedFD()}, mode_,
size_, guid_);
}
bool PlatformSharedMemoryRegion::ConvertToReadOnly() {
if (!IsValid()) {
return false;
}
CHECK_EQ(mode_, Mode::kWritable)
<< "Only writable shared memory region can be converted to read-only";
handle_.fd.reset(handle_.readonly_fd.release());
mode_ = Mode::kReadOnly;
return true;
}
bool PlatformSharedMemoryRegion::ConvertToUnsafe() {
if (!IsValid()) {
return false;
}
CHECK_EQ(mode_, Mode::kWritable)
<< "Only writable shared memory region can be converted to unsafe";
handle_.readonly_fd.reset();
mode_ = Mode::kUnsafe;
return true;
}
// static
PlatformSharedMemoryRegion PlatformSharedMemoryRegion::Create(Mode mode,
size_t size
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
,
bool executable
#endif
) {
if (size == 0) {
return {};
}
if (size > static_cast<size_t>(std::numeric_limits<int>::max())) {
return {};
}
CHECK_NE(mode, Mode::kReadOnly) << "Creating a region in read-only mode will "
"lead to this region being non-modifiable";
// This function theoretically can block on the disk, but realistically
// the temporary files we create will just go into the buffer cache
// and be deleted before they ever make it out to disk.
ScopedAllowBlocking scoped_allow_blocking;
// We don't use shm_open() API in order to support the --disable-dev-shm-usage
// flag.
FilePath directory;
if (!GetShmemTempDir(
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
executable,
#else
false /* executable */,
#endif
&directory)) {
return {};
}
FilePath path;
ScopedFD fd = CreateAndOpenFdForTemporaryFileInDir(directory, &path);
File shm_file(fd.release());
if (!shm_file.IsValid()) {
PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed";
FilePath dir = path.DirName();
if (access(dir.value().c_str(), W_OK | X_OK) < 0) {
PLOG(ERROR) << "Unable to access(W_OK|X_OK) " << dir.value();
if (dir.value() == "/dev/shm") {
LOG(FATAL) << "This is frequently caused by incorrect permissions on "
<< "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix.";
}
}
return {};
}
// Deleting the file prevents anyone else from mapping it in (making it
// private), and prevents the need for cleanup (once the last fd is
// closed, it is truly freed).
ScopedPathUnlinker path_unlinker(&path);
ScopedFD readonly_fd;
if (mode == Mode::kWritable) {
// Also open as readonly so that we can ConvertToReadOnly().
readonly_fd.reset(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)));
if (!readonly_fd.is_valid()) {
DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed";
return {};
}
}
if (!AllocateFileRegion(&shm_file, 0, size)) {
return {};
}
if (readonly_fd.is_valid()) {
stat_wrapper_t shm_stat;
if (File::Fstat(shm_file.GetPlatformFile(), &shm_stat) != 0) {
DPLOG(ERROR) << "fstat(fd) failed";
return {};
}
stat_wrapper_t readonly_stat;
if (File::Fstat(readonly_fd.get(), &readonly_stat) != 0) {
DPLOG(ERROR) << "fstat(readonly_fd) failed";
return {};
}
if (shm_stat.st_dev != readonly_stat.st_dev ||
shm_stat.st_ino != readonly_stat.st_ino) {
LOG(ERROR) << "Writable and read-only inodes don't match; bailing";
return {};
}
}
return PlatformSharedMemoryRegion(
{ScopedFD(shm_file.TakePlatformFile()), std::move(readonly_fd)}, mode,
size, UnguessableToken::Create());
}
expected<void, PlatformSharedMemoryRegion::TakeError>
PlatformSharedMemoryRegion::CheckPlatformHandlePermissionsCorrespondToMode(
PlatformSharedMemoryHandle handle,
Mode mode,
size_t size) {
if (auto result = CheckFDAccessMode(
handle.fd, mode == Mode::kReadOnly ? O_RDONLY : O_RDWR);
result.has_value()) {
switch (*result) {
case FDAccessModeError::kFcntlFailed:
return unexpected(TakeError::kFcntlFailed);
case FDAccessModeError::kMismatch:
return unexpected(mode == Mode::kReadOnly
? TakeError::kExpectedReadOnlyButNot
: TakeError::kExpectedWritableButNot);
}
}
if (mode == Mode::kWritable) {
if (auto result = CheckFDAccessMode(handle.readonly_fd, O_RDONLY);
result.has_value()) {
switch (*result) {
case FDAccessModeError::kFcntlFailed:
return unexpected(TakeError::kFcntlFailed);
case FDAccessModeError::kMismatch:
return unexpected(TakeError::kReadOnlyFdNotReadOnly);
}
}
return ok();
}
// The second descriptor must be invalid in kReadOnly and kUnsafe modes.
if (handle.readonly_fd != -1) {
return unexpected(TakeError::kUnexpectedReadOnlyFd);
}
return ok();
}
PlatformSharedMemoryRegion::PlatformSharedMemoryRegion(
ScopedFDPair handle,
Mode mode,
size_t size,
const UnguessableToken& guid)
: handle_(std::move(handle)), mode_(mode), size_(size), guid_(guid) {}
} // namespace base::subtle
|