Copilot commented on code in PR #3765:
URL: https://github.com/apache/brpc/pull/3765#discussion_r4658409709


##########
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:
   `SHM_SEGMENT_MODE` is also passed to the UBS-Mem backend here, but the PR 
description and both UBRing docs describe the same-user compatibility change 
only for POSIX IPC. The UBS-Mem API defines `mode` as Unix read/write 
permission bits and says mapping fails when the caller lacks permission (see 
`openeuler-mirror/ubs-mem` [API 
docs](https://github.com/openeuler-mirror/ubs-mem/blob/d31f57035f8a0ca6c9dd36e45b1c5f430d9b7f00/src/app_lib/include/ubs_mem.h#L127-L134)),
 so changing UBS allocations from 0666 to 0600 can break existing cross-node 
deployments whose client and server use different UIDs. Either keep a 
backend-specific mode for UBS if this compatibility change is IPC-only, or 
document/test the same-user requirement for UBS as well and call out the 
broader breaking change.
   
   This issue also appears on line 146 of the same file.



##########
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:
   `GetProcessAbsolutePath` explicitly does not append a NUL terminator (see 
`src/butil/process_util.h:35-38`), but `execv` requires `exe` to be a 
terminated path. Since this buffer is only zero-initialized nowhere and the 
call passes `sizeof(exe)`, the bytes after the returned path are indeterminate; 
the helper test can fail to exec or read past the path. Capture the returned 
length, reserve one byte, and terminate `exe` before constructing `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]

Reply via email to