Copilot commented on code in PR #3765:
URL: https://github.com/apache/brpc/pull/3765#discussion_r4652631982
##########
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:
This changes the permissions of an object after it has been created, but it
does not prevent name squatting: the IPC path still derives names predictably
and calls `shm_open(..., O_CREAT | O_EXCL, ...)`, so another local user can
pre-create the name with mode 0600 and force the legitimate allocation to fail
with `EEXIST`. If preventing squatting is part of this fix, use an unguessable
per-connection name (or an equivalent authenticated ownership/handshake
mechanism) rather than relying on the mode bits alone.
##########
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:
`umask` is process-wide, so changing it to 0 while this test calls
`IpcShmLocalMalloc` can race with any brpc background thread that creates a
file during this window and silently give that file broader permissions than
intended. Please isolate the mode check in a subprocess (or otherwise avoid
mutating the shared test process's umask) so the security test cannot perturb
unrelated activity.
--
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]