This is an automated email from the ASF dual-hosted git repository.
wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git
The following commit(s) were added to refs/heads/master by this push:
new c86b2b624 fix(ubshm): create ring segments with owner-only permissions
(#3765)
c86b2b624 is described below
commit c86b2b6243cd0c5e0bbb112cafd0835f3c26033f
Author: Weibing Wang <[email protected]>
AuthorDate: Wed Nov 25 20:01:56 2026 +0800
fix(ubshm): create ring segments with owner-only permissions (#3765)
* fix(ubshm): create ring segments with owner-only permissions
Ring segments were created with mode 0666, so any local user could open
and map an active connection's segments, whose names are predictable, and
then read or rewrite the payloads.
Create the segments with mode 0600 instead. Both the POSIX IPC backend and
the UBS backend now use a single SHM_SEGMENT_MODE constant, and the client
and the server are required to run as the same user.
Add a unittest that creates an IPC segment under a permissive umask, checks
that its permission bits are 0600, and verifies that a peer can still map
it. The UBS allocation depends on the external ubsm SDK, which is not
available in unit tests, so only the constant it passes to
ubsmem_shmem_allocate is checked.
* test(ubshm): check permissive umask case in a child process
umask is process-wide, so setting it to 0 in the test could widen the
permissions of a file created concurrently by a background thread. Fork a
child process for the permissive-umask check instead and keep the umask of
the test process untouched.
* test(ubshm): run the umask check in an exec'd process
Calling into the allocator in a child forked from the multithreaded test
process is unsafe: the child may inherit a mutex held by a thread that no
longer exists and deadlock, which also blocks the parent in waitpid.
Spawn the permissive-umask check as a fresh exec'd process instead. The
child is exec'd right after fork() and re-runs creates_owner_only_segment
with an env var that makes it set umask(0).
* docs(ubshm): note the same-user requirement applies to UBS too
UbsShmLocalMalloc passes the same 0600 creation mode to the ubsm SDK, so
processes that create and map a UBS segment must also run as the same
user. Document it and make the constant test spell out the implication.
Also terminate the executable path explicitly before execv(), since
butil::GetProcessAbsolutePath does not guarantee a NUL terminator.
---------
Co-authored-by: brpc-oncall <brpc-oncall@localhost>
---
docs/cn/ubring.md | 4 ++
docs/en/ubring.md | 4 ++
src/brpc/ubshm/shm/shm_def.h | 3 ++
src/brpc/ubshm/shm/shm_ipc.h | 2 +-
src/brpc/ubshm/shm/shm_ubs.cpp | 5 +-
test/brpc_ubring_unittest.cpp | 101 +++++++++++++++++++++++++++++++++++++++++
6 files changed, 115 insertions(+), 4 deletions(-)
diff --git a/docs/cn/ubring.md b/docs/cn/ubring.md
index 338b49a6f..6483c1964 100644
--- a/docs/cn/ubring.md
+++ b/docs/cn/ubring.md
@@ -21,10 +21,14 @@ UBRing 支持两种共享内存后端,通过 `ub_shm_type` 参数控制:
这是默认模式,使用标准 POSIX 共享内存进行本地 IPC。同一机器上的进程可以通过共享内存区域直接通信。
+共享内存段的创建模式为 0600,因此 client 与 server 需要以同一用户运行。
+
### 2. UBS-Mem 远端共享内存 (ub\_shm\_type = 2)
此模式使用 ubs-mem(Unified Block Storage Memory),这是来自 openEuler
的开源远端共享内存框架。它支持机架内节点之间的共享内存通信,类似于 RDMA 但部署要求更简单。
+共享内存段的创建模式同样为 0600,因此创建和映射同一共享内存段的进程需要以同一用户运行。
+
**UBS-Mem 开源地址**: <https://atomgit.com/openeuler/ubs-mem>
**所需库文件**:
diff --git a/docs/en/ubring.md b/docs/en/ubring.md
index 4779f9c2b..4f5676661 100644
--- a/docs/en/ubring.md
+++ b/docs/en/ubring.md
@@ -20,10 +20,14 @@ UBRing supports two types of shared memory backends,
controlled by the `ub_shm_t
This is the default mode, using standard POSIX shared memory for local IPC.
Processes on the same machine can communicate directly through shared memory
regions.
+The segments are created with mode 0600, so the client and the server must run
as the same user.
+
### 2. UBS-Mem Remote Shared Memory (ub_shm_type = 2)
This mode uses ubs-mem (Unified Block Storage Memory), an open-source remote
shared memory framework from openEuler. It enables shared memory communication
across nodes in a rack, similar to RDMA but with simpler deployment
requirements.
+The segments are also created with mode 0600, so the processes that create and
map a segment must run as the same user.
+
**UBS-Mem Open Source**: https://atomgit.com/openeuler/ubs-mem
**Required Libraries**:
diff --git a/src/brpc/ubshm/shm/shm_def.h b/src/brpc/ubshm/shm/shm_def.h
index f237ca939..3bc0f25f2 100644
--- a/src/brpc/ubshm/shm/shm_def.h
+++ b/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
namespace brpc {
namespace ubring {
diff --git a/src/brpc/ubshm/shm/shm_ipc.h b/src/brpc/ubshm/shm/shm_ipc.h
index 34e8307bb..6d8b099ed 100644
--- a/src/brpc/ubshm/shm/shm_ipc.h
+++ b/src/brpc/ubshm/shm/shm_ipc.h
@@ -20,7 +20,7 @@
#include "shm_def.h"
-#define SHM_IPC_MODE 0666
+#define SHM_IPC_MODE SHM_SEGMENT_MODE
namespace brpc {
namespace ubring {
diff --git a/src/brpc/ubshm/shm/shm_ubs.cpp b/src/brpc/ubshm/shm/shm_ubs.cpp
index ccbd32592..19804795b 100644
--- a/src/brpc/ubshm/shm/shm_ubs.cpp
+++ b/src/brpc/ubshm/shm/shm_ubs.cpp
@@ -38,7 +38,6 @@ namespace brpc {
namespace ubring {
#define UBRING_MK_UBSM(ret, fn, args) ret (*fn) args = nullptr
#include "brpc/ubshm/ubs_mem/declare_shm_ubs.h"
-#define SHM_RIGHT_MODE 0666
#define UBRING_REGION_NAME_PREFIX "UbrONE2ALLRegion"
DEFINE_uint32(node_location, 1, "Location of the ub machine.");
DEFINE_bool(shm_wr_delay_comp, true, "Indicates whether to enable the write
relay."
@@ -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,
AquireFlagIfWrDelayComp(UBSM_FLAG_ONLY_IMPORT_NONCACHE |
UBSM_FLAG_MEM_ANONYMOUS));
do {
if (ret == UBSM_ERR_ALREADY_EXIST) {
@@ -144,7 +143,7 @@ do {
return SHM_ERR_EXIST;
}
LOG(INFO) << "Ubs delete shm name=" << shm->name << " success, try to
recreate.";
- ret = ubsmem_shmem_allocate(g_region_name, shm->name, shm->len,
SHM_RIGHT_MODE,
+ ret = ubsmem_shmem_allocate(g_region_name, shm->name, shm->len,
SHM_SEGMENT_MODE,
AquireFlagIfWrDelayComp(UBSM_FLAG_ONLY_IMPORT_NONCACHE |
UBSM_FLAG_MEM_ANONYMOUS));
if (ret != UBSM_OK) {
LOG(ERROR) << "Ubs recreate shm name=" << shm->name << " failed,
ret=" << ret;
diff --git a/test/brpc_ubring_unittest.cpp b/test/brpc_ubring_unittest.cpp
index c71fa9615..807ce20d4 100644
--- a/test/brpc_ubring_unittest.cpp
+++ b/test/brpc_ubring_unittest.cpp
@@ -24,10 +24,16 @@
#include <string>
#include <thread>
#include "butil/macros.h"
+#include "butil/process_util.h"
#include "butil/sys_byteorder.h"
#include "brpc/socket.h"
#if BRPC_WITH_UBRING
+#include <fcntl.h>
+#include <limits.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
#include <unistd.h>
#include "brpc/ubshm/common/common.h"
#include "brpc/ubshm/ub_endpoint.h"
@@ -836,6 +842,101 @@ 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. Both backends therefore require the
+// peers of a segment to run as the same user (see docs/en/ubring.md).
+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 + 1];
+ const ssize_t exe_len = butil::GetProcessAbsolutePath(exe, sizeof(exe) -
1);
+ ASSERT_GT(exe_len, 0);
+ exe[exe_len] = '\0';
+ std::string filter =
+ "--gtest_filter=UBRingShmModeTest.creates_owner_only_segment";
+ char* const child_argv[] = {exe, const_cast<char*>(filter.c_str()),
nullptr};
+ ASSERT_EQ(0, setenv(kUmaskChildEnv, "1", 1));
+ const pid_t pid = fork();
+ ASSERT_GE(pid, 0);
+ if (pid == 0) {
+ execv(exe, child_argv);
+ _exit(1);
+ }
+ unsetenv(kUmaskChildEnv);
+ int status = 0;
+ ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ ASSERT_TRUE(WIFEXITED(status));
+ EXPECT_EQ(0, WEXITSTATUS(status));
+}
+
+TEST(UBRingShmModeTest, ipc_segment_mode_and_peer_mapping) {
+ const std::string name = NewSegmentName();
+ 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);
+ ASSERT_EQ(0, brpc::ubring::IpcShmLocalMalloc(&local_shm));
+ ASSERT_NE(nullptr, local_shm.addr);
+
+ const int fd = shm_open(local_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);
+
+ // The legitimate peer, running as the same user, still maps it.
+ brpc::ubring::SHM remote_shm = {nullptr, local_shm.len, 0, {0}, 0};
+ memcpy(remote_shm.name, local_shm.name, sizeof(remote_shm.name));
+ EXPECT_EQ(0, brpc::ubring::IpcShmRemoteMalloc(&remote_shm));
+ EXPECT_NE(nullptr, remote_shm.addr);
+ EXPECT_EQ(0, brpc::ubring::IpcShmRemoteFree(&remote_shm));
+ EXPECT_EQ(0, brpc::ubring::IpcShmLocalFree(&local_shm));
+}
+
namespace brpc {
namespace ubring {
class UBShmEndpointTest : public ::testing::Test {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]