wwbmmm commented on code in PR #3765:
URL: https://github.com/apache/brpc/pull/3765#discussion_r4658525033
##########
src/brpc/ubshm/shm/shm_ubs.cpp:
##########
@@ -135,7 +134,7 @@ static uint64_t AquireFlagIfWrDelayComp(const uint64_t flag)
RETURN_CODE UbsShmLocalMalloc(SHM *shm)
{
- int ret = ubsmem_shmem_allocate(g_region_name, shm->name, shm->len,
SHM_RIGHT_MODE,
+ int ret = ubsmem_shmem_allocate(g_region_name, shm->name, shm->len,
SHM_SEGMENT_MODE,
Review Comment:
[replied by brpc-oncall robot] Keeping 0600 for UBS is intentional: the
change has to cover both allocation paths, and the UBS SDK treats `mode` as
Unix permission bits, so owner-only is the correct value there as well. I
extended both docs (`docs/en/ubring.md`, `docs/cn/ubring.md`) to state that the
processes which create and map a UBS segment must run as the same user, and the
constant test now spells out that both backends share this requirement. The PR
description is updated to call out the broader compatibility change. The UBS
path itself cannot be exercised in unit tests because it needs the external
`libubsm_sdk.so`, so only the constant passed to `ubsmem_shmem_allocate` is
covered. 0660 with a dedicated group would be an option if group-based sharing
is required, but no such requirement is known.
##########
test/brpc_ubring_unittest.cpp:
##########
@@ -821,6 +827,98 @@ TEST(UBRingConfigurationTest,
time_flags_include_units_and_expected_defaults) {
USEC_TO_NSEC);
}
+// Newly created segments must not be accessible to other users of the host.
+// The UBS backend allocates through the external ubsm SDK, which is not
+// available in unit tests, so only the constant it passes to
+// ubsmem_shmem_allocate is checked here.
+TEST(UBRingShmModeTest, segment_mode_is_owner_only) {
+ EXPECT_EQ(0600, SHM_SEGMENT_MODE);
+ EXPECT_EQ(SHM_SEGMENT_MODE, SHM_IPC_MODE);
+}
+
+namespace {
+// Only set in the child process spawned by the test below, which needs a fresh
+// single-threaded process to change its umask.
+const char kUmaskChildEnv[] = "BRPC_UBRING_UMASK_CHILD";
+
+std::string NewSegmentName() {
+ static std::atomic<uint32_t> name_sequence(0);
+ return "/brpc_mode_" + std::to_string(getpid()) + "_" +
+ std::to_string(name_sequence.fetch_add(1, std::memory_order_relaxed));
+}
+} // namespace
+
+// Re-run in a fresh process with umask 0 by
+// segment_mode_is_checked_under_umask_zero; a plain run checks the ambient
+// umask of the test process.
+TEST(UBRingShmModeTest, creates_owner_only_segment) {
+ if (getenv(kUmaskChildEnv) != nullptr) {
+ umask(0);
+ }
+ const std::string name = NewSegmentName();
+ brpc::ubring::SHM shm = {nullptr, 4 * 1024 * 1024, 0, {0}, 0};
+ ASSERT_LT(name.size(), sizeof(shm.name));
+ memcpy(shm.name, name.c_str(), name.size() + 1);
+ ASSERT_EQ(0, brpc::ubring::IpcShmLocalMalloc(&shm));
+ ASSERT_NE(nullptr, shm.addr);
+
+ const int fd = shm_open(shm.name, O_RDONLY, 0);
+ ASSERT_GE(fd, 0);
+ struct stat st;
+ ASSERT_EQ(0, fstat(fd, &st));
+ close(fd);
+ EXPECT_EQ(mode_t(0600), st.st_mode & 0777);
+ EXPECT_EQ(0, brpc::ubring::IpcShmLocalFree(&shm));
+}
+
+TEST(UBRingShmModeTest, segment_mode_is_checked_under_umask_zero) {
+ // A permissive umask must not widen the mode of a new segment. umask is
+ // process-wide and brpc tests run with background threads, so the check
+ // runs in its own process: the child is exec'd right after fork() so that
+ // no non-async-signal-safe code runs in a child of a multithreaded
process.
+ char exe[PATH_MAX];
+ ASSERT_GT(butil::GetProcessAbsolutePath(exe, sizeof(exe)), 0);
+ std::string filter =
Review Comment:
[replied by brpc-oncall robot] Fixed in 7929c9d9: the returned length is
captured and the path is explicitly terminated (`exe[exe_len] = '\0'`) before
building `child_argv`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]