Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
every 100 ms, and the flusher skips its stripe sweep when nothing is
pending. Flush and sync intervals, and the final flush and sync on
`stop()`, are unchanged.
- POSIX: a start or config test as root no longer leaves a cache file the
workers cannot use: with the new `VolumeConfig::owner`, a process running
as root creates the volume file for the configured user. A volume path that
is not a plain file is reported (new, tail-appended
`CacheError::VolumeFileRefused`; C API: `CYCLONE_IO_ERROR`) and not used.
- Windows: cache and volume paths with non-ASCII characters (for example a
localized user or folder name) are opened through the wide-character file
API, so UTF-8 paths are no longer mangled by the ANSI code page.
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ if(CYCLONE_BUILD_TESTS)
tests/integration/test_tail_fill.cpp
tests/integration/test_write_behind.cpp
tests/integration/test_cloexec.cpp
tests/integration/test_volume_file_checks.cpp
tests/integration/test_lock_holder_wait.cpp
tests/integration/test_write_lock_liveness.cpp
tests/integration/test_exit_with_open_cache.cpp
Expand Down
18 changes: 18 additions & 0 deletions include/cyclone/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <thread>

Expand Down Expand Up @@ -884,6 +885,23 @@ struct VolumeConfig {
// CacheConfig fields of the same name (see their documentation there).
std::chrono::milliseconds read_lease_duration{5000};
std::chrono::milliseconds lease_wrap_ceiling{60000};

// The user (and group) the volume file must belong to. POSIX only;
// ignored on Windows. Unset (the default): no owner requirement.
//
// The volume must be a plain file owned by the configured user. When
// set, open() uses the file only if `uid` owns it; a process running as
// root creates the file for `uid`:`gid` (and gives it a root-owned volume
// an earlier root run left behind). Otherwise open() reports
// CacheError::VolumeFileRefused and does not use the file.
//
// Independent of this setting, a volume path that is not a plain file is
// reported and not used.
struct Owner {
uint32_t uid = 0;
uint32_t gid = 0;
};
std::optional<Owner> owner;
};

} // namespace cyclone
8 changes: 7 additions & 1 deletion include/cyclone/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ enum class CacheError : std::uint8_t {
// to drain the peer. This error string is the ONLY
// signal a refusal has -- the Cache never starts, so
// no stats reader can observe it.
ObjectTooLarge // Write content exceeds the configured max_object_size
ObjectTooLarge, // Write content exceeds the configured max_object_size
VolumeFileRefused // The volume must be a plain file owned by the
// configured user (VolumeConfig::owner); this one
// is not, so it is not used.
};

class CacheErrorCategory : public std::error_category {
Expand Down Expand Up @@ -117,6 +120,9 @@ class CacheErrorCategory : public std::error_category {
return "cache reset refused: another process still has this cache open";
case CacheError::ObjectTooLarge:
return "object exceeds configured max_object_size";
case CacheError::VolumeFileRefused:
return "cache volume not used: the volume must be a plain file owned "
"by the configured user";
default:
return "unknown error";
}
Expand Down
2 changes: 2 additions & 0 deletions src/c_api/c_error_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ inline CycloneError to_c_error(CacheError e) {
case CacheError::NoSpace:
return CYCLONE_NO_SPACE;
case CacheError::IoError:
case CacheError::VolumeFileRefused: // no C code of its own; the message
// (cyclone_get_last_error) says why
return CYCLONE_IO_ERROR;
case CacheError::Corrupted:
case CacheError::ChainCorrupted:
Expand Down
5 changes: 4 additions & 1 deletion src/core/mmap_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,10 @@ bool WriterLiveness::claim_locked(uint32_t fork_epoch) {
int fd = -1;
if (!fail) {
do {
fd = ::open(_path.c_str(), O_RDONLY | O_CLOEXEC);
// The volume must be a plain file owned by the configured user: no
// symbolic link at the path, and no wait on a path that is not a file.
fd =
::open(_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
} while (fd < 0 && errno == EINTR);
}
if (fd >= 0) {
Expand Down
113 changes: 108 additions & 5 deletions src/core/volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ using ssize_t = SSIZE_T;
// master -> workers) is unaffected; an exec'd helper no longer keeps the
// volume open, which would pin the file and hold its byte-range locks.
#define CYCLONE_NOINHERIT O_CLOEXEC
#define CYCLONE_OPEN_FLAGS (O_RDWR | O_CREAT | CYCLONE_NOINHERIT)
// The volume must be a plain file owned by the configured user: the open
// does not resolve a symbolic link at the volume path and does not wait on a
// path that is not a file (see check_volume_file()).
#define CYCLONE_VOLUME_OPEN_GUARD (O_NOFOLLOW | O_NONBLOCK)
#define CYCLONE_OPEN_FLAGS \
(O_RDWR | O_CREAT | CYCLONE_NOINHERIT | CYCLONE_VOLUME_OPEN_GUARD)
#define CYCLONE_OPEN_MODE 0666
#endif

Expand Down Expand Up @@ -1681,7 +1686,8 @@ void gc_superseded_volumes(const std::vector<Volume*>& live_volumes,
// O_RDWR (NOT O_RDONLY): an O_RDONLY fd rejects the F_WRLCK exclusive
// probe with EBADF, which we would misread as "no lock support". Any
// open failure (any errno) -> skip.
const int fd = CYCLONE_OPEN(f.c_str(), O_RDWR | CYCLONE_NOINHERIT);
const int fd = CYCLONE_OPEN(
f.c_str(), O_RDWR | CYCLONE_NOINHERIT | CYCLONE_VOLUME_OPEN_GUARD);
if (fd < 0) {
continue;
}
Expand Down Expand Up @@ -1799,6 +1805,73 @@ uint64_t Volume::checksum_revalidation_seed() noexcept {
return z != 0 ? z : 1;
}

#ifndef _WIN32
namespace {

// The volume must be a plain file owned by the configured user. Checked
// on the descriptor just opened, before anything is read, written, resized
// or mapped through it:
//
// - a regular file with a single link;
// - with VolumeConfig::owner set, owned by that user. A process running
// as root gives the owner a root-owned file it just created, or one that
// is empty or starts with a volume header (an earlier root run's), with
// fchown() on this descriptor. Any other file is not used.
//
// Also clears the O_NONBLOCK of the open. Prints the reason on stderr.
std::expected<void, CacheError> check_volume_file(int fd,
const VolumeConfig& config,
bool created_new) {
const char* const path = config.path.c_str();
struct stat st{};
if (::fstat(fd, &st) != 0) {
return make_unexpected(CacheError::IoError);
}
if (!S_ISREG(st.st_mode) || st.st_nlink > 1) {
std::fprintf(stderr, "cyclone: not using volume %s: not a plain file\n",
path);
return make_unexpected(CacheError::VolumeFileRefused);
}
if (config.owner.has_value()) {
const uid_t uid = static_cast<uid_t>(config.owner->uid);
const gid_t gid = static_cast<gid_t>(config.owner->gid);
if (st.st_uid != uid) {
bool give = st.st_uid == 0 && ::geteuid() == 0;
if (give && !created_new && st.st_size != 0) {
std::byte buf[VolumeHeader::kSize];
const ssize_t n = ::pread(fd, buf, sizeof(buf), 0);
give = n == static_cast<ssize_t>(sizeof(buf)) &&
VolumeHeader::deserialize(buf).is_valid();
}
if (!give) {
std::fprintf(stderr,
"cyclone: not using volume %s: owned by uid %llu, "
"expected uid %llu; delete the file to have it "
"recreated\n",
path, static_cast<unsigned long long>(st.st_uid),
static_cast<unsigned long long>(uid));
return make_unexpected(CacheError::VolumeFileRefused);
}
if (::fchown(fd, uid, gid) != 0) {
std::fprintf(stderr,
"cyclone: not using volume %s: could not give it to uid "
"%llu: %s\n",
path, static_cast<unsigned long long>(uid),
std::strerror(errno));
return make_unexpected(CacheError::VolumeFileRefused);
}
}
}
const int flags = ::fcntl(fd, F_GETFL);
if (flags < 0 || ::fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
return make_unexpected(CacheError::IoError);
}
return {};
}

} // namespace
#endif

std::expected<void, CacheError> Volume::open() {
if (_fd >= 0) {
return make_unexpected(CacheError::AlreadyOpen);
Expand All @@ -1821,9 +1894,10 @@ std::expected<void, CacheError> Volume::open() {
_fd = open_volume_fd(_config.path, /*create=*/true,
/*exclusive=*/true);
#else
_fd = CYCLONE_OPEN(_config.path.c_str(),
O_RDWR | O_CREAT | O_EXCL | CYCLONE_NOINHERIT,
CYCLONE_OPEN_MODE);
_fd = CYCLONE_OPEN(
_config.path.c_str(),
O_RDWR | O_CREAT | O_EXCL | CYCLONE_NOINHERIT | CYCLONE_VOLUME_OPEN_GUARD,
CYCLONE_OPEN_MODE);
#endif
if (_fd >= 0) {
created_new = true;
Expand All @@ -1842,9 +1916,25 @@ std::expected<void, CacheError> Volume::open() {
}

if (_fd < 0) {
#ifndef _WIN32
if (errno == ELOOP) {
// O_NOFOLLOW: the volume path is a symbolic link.
std::fprintf(stderr, "cyclone: not using volume %s: not a plain file\n",
_config.path.c_str());
return make_unexpected(CacheError::VolumeFileRefused);
}
#endif
return make_unexpected(CacheError::IoError);
}

#ifndef _WIN32
if (auto checked = check_volume_file(_fd, _config, created_new); !checked) {
CYCLONE_CLOSE(_fd);
_fd = -1;
return checked;
}
#endif

_reset_gate_degraded.store(false, std::memory_order_relaxed);
_whole_file_map_failed.store(false, std::memory_order_relaxed);

Expand Down Expand Up @@ -2123,6 +2213,19 @@ std::expected<void, CacheError> Volume::open_locked(bool created_new) {
if (!open_result) {
return make_unexpected(CacheError::IoError);
}
#ifndef _WIN32
// The volume must be a plain file owned by the configured user: the
// mapping opens the path again, so map only the file checked through _fd.
{
struct stat ours{};
struct stat mapped{};
if (::fstat(_fd, &ours) != 0 ||
::fstat(static_cast<int>(_mapped_file->native_fd()), &mapped) != 0 ||
ours.st_dev != mapped.st_dev || ours.st_ino != mapped.st_ino) {
return make_unexpected(CacheError::IoError);
}
}
#endif

// Map the entire volume file persistently. All subsequent map_region()
// calls become pointer arithmetic (zero syscalls). Failure is non-fatal:
Expand Down
4 changes: 3 additions & 1 deletion src/io/mapped_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ class PosixMappedFile : public MappedFile {
}

// Close-on-exec: an exec'd child must not inherit the mapping's fd.
_fd = ::open(path.c_str(), flags | O_CLOEXEC, perms);
// O_NOFOLLOW: the volume must be a plain file owned by the configured
// user.
_fd = ::open(path.c_str(), flags | O_CLOEXEC | O_NOFOLLOW, perms);
if (_fd < 0) {
return make_unexpected(
std::make_error_code(static_cast<std::errc>(errno)));
Expand Down
Loading
Loading