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


##########
test/brpc_ubring_unittest.cpp:
##########
@@ -821,6 +824,46 @@ 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);
+}
+
+TEST(UBRingShmModeTest, ipc_segment_is_mapped_by_peer_only) {
+    static std::atomic<uint32_t> name_sequence(0);
+    const std::string name = "/brpc_mode_" + std::to_string(getpid()) + "_" +
+        std::to_string(name_sequence.fetch_add(1, std::memory_order_relaxed));
+    brpc::ubring::SHM local_shm = {nullptr, 4 * 1024 * 1024, 0, {0}, 0};
+    ASSERT_LT(name.size(), sizeof(local_shm.name));
+    memcpy(local_shm.name, name.c_str(), name.size() + 1);
+
+    // A permissive umask must not widen the mode of a new segment.
+    const mode_t saved_umask = umask(0);
+    const int ret = brpc::ubring::IpcShmLocalMalloc(&local_shm);

Review Comment:
   [replied by brpc-oncall robot] Good catch. Fixed in b1b0386d: the 
permissive-umask case now runs in a forked child process that sets umask(0), 
creates the segment, checks its mode and exits, so the test process never 
modifies its umask. Only the child touches the umask, and only syscalls are 
used after the fork.



##########
src/brpc/ubshm/shm/shm_def.h:
##########
@@ -33,6 +33,9 @@
 #define SHM_MAX_NAME_BUFF_LEN 48 // byte, buffer size, ubsm_sdk need name to 
be below 48byte
 #define SHM_MAX_NAME_LEN (SHM_MAX_NAME_BUFF_LEN - 1) // byte, string length
 #define SHM_ALLOC_UNIT_SIZE (4 * 1024 * 1024) // 4MB
+// Permission bits of newly created shm segments. Only the peer of the creating
+// user is allowed to access the segment, so group and other get no access.
+#define SHM_SEGMENT_MODE 0600

Review Comment:
   [replied by brpc-oncall robot] Name squatting is pre-existing behavior and 
is not made worse by this change: a squatted name only makes `shm_open(O_CREAT 
| O_EXCL)` fail with EEXIST, and the connection then falls back to TCP, so no 
segment payload is exposed. This PR only stops another user from 
opening/mapping an existing segment. Making the names unguessable or passing 
the fds over the TCP connection is a larger redesign, so it is left for a 
separate change.



-- 
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