已合并
【回合7.0.0】修复 DB-04 HADR/SW 复制发送方绕过内部连接校验的认证绕过问题 #9757
【回合7.0.0】修复 DB-04 HADR/SW 复制发送方绕过内部连接校验的认证绕过问题 #9757
已合并
luodongxu创建于 9月10日
luodongxu
luodongxu成员
9月10日

【标题】回合7.0.0:修复 DB-04 HADR/SW 复制发送方绕过内部连接校验的认证绕过问题

【回合说明】

  • 本 PR 为 master 分支 PR !9364 到 7.0.0 分支的回合(backport)。
  • 源 PR:https://gitcode.com/opengauss/openGauss-server/merge_requests/9364
  • 源提交:7ef9e2ceb5114c046cf89ba1983d1d7ee3134d37(security(hba): require internal connection for HADR/SW replication senders)
  • 回合方式:git cherry-pick -x 7ef9e2ceb,无冲突。
  • 一致性核对:hba.cpp、replication_standbywrite_security 测试三件套(input .source/.py、output .source)、parallel_schedule 与源 PR 逐字节一致;parallel_schedule0 因 7.0.0 分支已有新增用例(如 gpi_merge_upsert_concurrent 等),cherry-pick 自动合并并保留双方改动。

【实现内容】

  • 在 hba_getauthmethod() 中要求 SW_SENDER 同样经过 is_node_internal_connection(port) 校验。
  • 移除 AM_WAL_HADR_SENDER / SW_SENDER 直接调用 check_hba_replication() 的无条件绕过。
  • 修复 check_db():SW_SENDER 在落入普通 check_hba() 路径后,需要像其它 walsender 一样能够匹配 replication HBA token,否则远程 trust 规则无法被正常匹配并触发“禁止远程 trust”策略。
  • 外部复制连接不再进入弱化匹配路径,而是落入普通 check_hba(),从而触发“禁止远程 trust”策略。
  • 新增回归测试 replication_standbywrite_security,验证来自非内部 IP 的 replication=standbywrite 连接会被远程 trust 禁止策略拒绝;测试走 HA 端口(PostPortNumber + 1)以适配 enable_thread_pool = on 环境。

【根因分析】

  • check_hba_replication() 仅做连接类型、SSL、IP、role、replication token 匹配,缺少普通 check_hba() 的远程 trust 禁止、完整 database/role 匹配等校验。
  • hba_getauthmethod() 原本对 AM_WAL_HADR_SENDER 和 SW_SENDER 直接进入 check_hba_replication(),不要求 is_node_internal_connection。
  • SW_SENDER 在 ProcessStartupPacket 的连接接受分支为 // nothing to do,不执行 ReplConnInfo channel_adapt 校验。
  • 非内部的 SW_SENDER 被修正到普通 check_hba() 后,又因 check_db() 不识别 SW_SENDER 为 walsender,导致 replication 规则无法匹配,无法触发远程 trust 禁止。
  • 因此攻击者若能在 pg_hba.conf 中匹配到 replication trust 规则,即可绕过远程 trust 禁止策略完成认证。

【实现方案】

  • 修改 src/common/backend/libpq/hba.cpp(与源 PR 一致):
    • hba_getauthmethod():将 SW_SENDER 纳入要求 is_node_internal_connection(port) 的集合,并删除 HADR/SW 的无条件绕过。
    • check_db():允许 SW_SENDER 匹配 replication HBA token,保证普通 HBA 路径对非内部 SW 复制连接仍能匹配 replication 规则并触发远程 trust 禁止。
  • 新增测试文件:
    • src/test/regress/input/replication_standbywrite_security.source
    • src/test/regress/output/replication_standbywrite_security.source
    • src/test/regress/input/replication_standbywrite_security.py
    • 并在 parallel_schedule / parallel_schedule0 中注册。

【关联需求或issue】
https://gitcode.com/opengauss/openGauss-server/issues/8245

【开发自验报告】

  1. 请附上自验结果(内容或者截图)
    • 回合一致性:已基于 origin/7.0.0(9945f2770)完成 cherry-pick,无冲突;除 parallel_schedule0 保留 7.0.0 已有用例外,其余文件与源 PR 完全一致。
    • 源 PR !9364 已在 openEuler aarch64(gcc 10.3.1,release)完成全量编译,pg_regress 新增用例通过(实际输出 REJECTED_BY_REMOTE_TRUST_POLICY,All 1 tests passed),并完成一主一备端到端验证(详见源 PR 描述与截图)。
    • 本分支验证:提交 CI 由 fastcheck 运行新增用例 replication_standbywrite_security 验证。
  2. 是否可以添加fastcheck测试用例,如是,请补充fastcheck用例
    • 已随回合携带 replication_standbywrite_security(已在 parallel_schedule / parallel_schedule0 注册)。
  3. 是否涉及资料修改,如是,在docs仓库补充资料
    • 否。
  4. 是否考虑升级场景(系统表修改、日志持久化以及修改执行态数据格式)
    • 不涉及系统表、日志持久化或数据格式变更。
  5. 是否考虑在线扩容等扩展场景
    • 不影响在线扩容逻辑。
  6. 是否考虑异常场景/并发场景/前向兼容/性能场景
    • 前向兼容:内部节点复制连接行为不变;外部连接从绕过变为正常校验。
    • 性能影响:仅增加一次已有的 is_node_internal_connection 判断,可忽略。
  7. 是否对其他模块产生影响
    • 仅影响 HBA 认证路径。
  8. 是否需要回合补丁版本(5.0、6.0)
    • 本 PR 即为 7.0.0 分支回合;其它版本回合由维护者评估。
  9. 针对Plugin仓dolphin插件,新增/修改的代码是否已使用 DOLPHIN 宏进行控制
    • 不适用,未修改 dolphin 插件相关代码。

【其他说明】

  • 无。
likedislike
Pull Request已成功合入, 合并人@opengauss_bot
(感谢 luodongxu 的贡献)
luodongxuluodongxu成员
9月10日 关联了issue:[Bug]: ustore下autovacuum不执行vacuum死亡元组不清零
opengauss-ci-bot成员
9月10日 评论:

openGauss pipeline is running. Please wait a moment...

likedislike
opengauss_botopengauss_bot成员
9月10日 将ywzq1161327784,chenxiaobin19,totaj,congzhou2603,shenzheng4,chendong76,dodders,muyulinzhong,shirley_zhengx,quemingjian,jemappellehc,wofanzheng,wlff234,libiao2024,hwworkholic,CarrotGo,superlchf设为审查人
Oopengauss-ci-bot成员
9月10日 添加了label:codecheck-running
opengauss_botopengauss_bot成员
9月10日 添加了label:sig/StorageEnginesig/SQLEnginesig/SecurityTechnologysig/CMsig/CloudNativesig/AI
opengauss_bot
opengauss_bot成员
9月10日 评论:

Welcome To openGauss Community

Hey @luodongxu , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands. You can self-configure the PR merge rules for this repository. For more details, please refer to Here.

Contact Guide

If you have any questions, please contact the SIG: StorageEngine, AI, CM, CloudNative, SecurityTechnology, SQLEngine ,
and any of the maintainers: @CarrotGo, @chendong76, @chenxiaobin19, @congzhou2603, @dodders, @hwworkholic, @jemappellehc, @libiao2024, @muyulinzhong, @quemingjian, @shenzheng4, @shirley_zhengx, @superlchf, @totaj, @wlff234, @wofanzheng, @ywzq1161327784 ,
and any of the committers: @Igali, @bihua111, @cailei19, @h_ray, @levy53071, @libiao2024, @lihaixiao, @mrzack, @wangfeihuo, @wuyuechuan, @xiong_xjun, @zhangfengzhi123, @zhangxubo, @zhangzq131, @zjh_hw .

likedislike
Oopengauss-ci-bot成员
9月10日 添加了label:lite-ci-running
opengauss_botopengauss_bot成员
9月10日 添加了label:need-doc-pr
opengauss_botopengauss_bot成员
9月10日 添加了label:opengauss-cla/yes
opengauss_bot
opengauss_bot成员
9月10日 评论:

Linking PR Notice

@luodongxu , the pull request should be linked to at least one https://gitcode.com/opengauss/docs repository's PR.
If it is confirmed that no PR association is needed, you can remove the label by commenting /remove-need-doc .

likedislike
opengauss_bot
opengauss_bot成员
9月10日 评论:

CLA Signature Pass

luodongxu, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
luodongxu
luodongxu成员
9月10日 评论:

/remove-need-doc

likedislike
opengauss_botopengauss_bot成员
9月10日 删除了label:need-doc-pr
opengauss-ci-bot成员
9月10日 评论:

开源片段扫描

Check Name Build Details Check Result Check Detail
sca #42410 ✅ 通过 >>>
likedislike
Oopengauss-ci-bot成员
9月10日 添加了label:sca-success
opengauss-ci-bot成员
9月10日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
Oopengauss-ci-bot成员
9月10日 添加了label:docs-ci-pipeline-success
opengauss-ci-bot成员
9月10日 评论:

静态检查

Check Name Build Details Check Result Check Detail
CodeCheck #82464 ✅ 通过 >>>
likedislike
Oopengauss-ci-bot成员
9月10日 添加了label:codecheck-success
Oopengauss-ci-bot成员
9月10日 删除了label:codecheck-running
Oopengauss-ci-bot成员
9月10日 删除了label:lite-ci-running
Oopengauss-ci-bot成员
9月10日 添加了label:lite-ci-failed
Oopengauss-ci-bot成员
9月10日 删除了label:lite-ci-failed
Oopengauss-ci-bot成员
9月10日 添加了label:lite-ci-success
opengauss-ci-bot成员
9月10日 评论:
Check Name Build Result Build Details
openGauss_Lite_Pr ✅ SUCCESS #4097
likedislike
luodongxu
luodongxu成员
9月10日 评论:

/check-pr

likedislike
opengauss_bot
opengauss_bot成员
9月10日 评论:

Merge Verification Failed

@luodongxu, this pr is not mergeable and the reasons are below:

Not Enough Labels
  • the pull request needs 1 approved labels, but now gets 0.
  • the pull request needs 1 ci-pipeline-success labels, but now gets 0.
  • the pull request needs 2 lgtm labels, but now gets 0.
Label Usage Tips
  • approved: Agree to merge the pull request. This can be accomplished by commenting /approve
  • lgtm: The code has been reviewed. This can be accomplished by commenting /lgtm
likedislike
Oopengauss-ci-bot成员
9月10日 添加了label:ci-pipeline-failed
Oopengauss-ci-bot成员
9月11日 删除了label:ci-pipeline-failed
Oopengauss-ci-bot成员
9月11日 添加了label:ci-pipeline-success
opengauss-ci-bot成员
9月11日 评论:
Check Name Build Result Build Details
openGauss_server_pr ✅ Success #32161
likedislike
muyulinzhong
muyulinzhong成员
9月11日 评论:

/lgtm
/approve

likedislike
opengauss_botopengauss_bot成员
9月11日 添加了label:approvedlgtm-muyulinzhong
opengauss_bot
opengauss_bot成员
9月11日 评论:

Review Code Feedback

  • The label lgtm-muyulinzhong, approved was added to this pull request. It means that muyulinzhong reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄
likedislike
zhoucong<Okidoki>
zhoucong<Okidoki>成员
9月11日 评论:

/lgtm

likedislike
opengauss_botopengauss_bot成员
9月11日 添加了label:lgtm-congzhou2603
opengauss_bot
opengauss_bot成员
9月11日 评论:

Review Code Feedback

  • The label lgtm-congzhou2603 was added to this pull request. It means that congzhou2603 reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄
likedislike
opengauss_botopengauss_bot成员
9月11日 合入了pull request,合并节点 SHA:bce9fd654a4b90ce3bdfd571f69e764513a22b7c
Oopengauss-ci-bot成员
9月11日 删除了label:codecheck-success
Oopengauss-ci-bot成员
9月11日 添加了label:codecheck-running
opengauss-ci-bot成员
9月11日 评论:

开源片段扫描

Check Name Build Details Check Result Check Detail
sca #42447 ✅ 通过 >>>
likedislike
opengauss-ci-bot成员
9月11日 评论:

静态检查

Check Name Build Details Check Result Check Detail
CodeCheck #82513 ✅ 通过 >>>
likedislike
Oopengauss-ci-bot成员
9月11日 添加了label:codecheck-success
Oopengauss-ci-bot成员
9月11日 删除了label:codecheck-running
luodongxu
luodongxu成员
9月14日 评论:

✅ PR 9757 回归验证报告

结论:修复有效、回归用例有效、相邻用例无回归 —— 回归验证通过。

验证对象:security-7.0.0 -> 7.0.0,回合提交 339214dd8(源 PR !9364 / 源提交 7ef9e2ceb)。

PR 9757 回归验证结果

1. 回合一致性(git 逐字节比对)

文件 结果
src/common/backend/libpq/hba.cpp ✅ 与源 commit 7ef9e2ceb 逐字节一致
input/replication_standbywrite_security.py ✅ 逐字节一致
input/replication_standbywrite_security.source ✅ 逐字节一致
output/replication_standbywrite_security.source ✅ 逐字节一致
parallel_schedule ✅ 逐字节一致
parallel_schedule0 ✅ 仅 7.0.0 分支已有用例差异(smp / gpi_merge_upsert / prefix_sort),双方改动均保留

2. 全量编译

sh build.sh -m release -3rd <binarylibs> --cmake → BUILD_EXIT=0。

3. 新增回归用例(修复后)

make fastcheck_single PART=9757(schedule: test: replication_standbywrite_security):

  • test(1/1) replication_standbywrite_security .... ok
  • All 1 tests passed.
  • 实际输出:REJECTED_BY_REMOTE_TRUST_POLICY

4. 负向对照(还原 hba.cpp 至修复前 9945f2770 后增量重编)

  • 1 of 1 tests failed.
  • 实际输出:TRUST_ACCEPTED(期望 REJECTED_BY_REMOTE_TRUST_POLICY)
  • 说明:修复前来自非内部 IP 的 replication=standbywrite 连接确实可绕过远程 trust 禁止策略完成认证,证明漏洞可复现、新增用例非空转。

5. 相邻安全用例回归(修复态同批运行)

  • test(1/2) remotetype_trust_security .... ok
  • test(2/2) replication_standbywrite_security .... ok
  • All 2 tests passed.

代码层面复核

  • AM_WAL_SENDER 宏已包含 HADR 角色;check_db() 增加 t_thrd.role == SW_SENDER 后,非内部 SW/HADR 连接落入普通 check_hba() 并匹配 replication token,从而触发远程 trust 禁止策略;
  • 内部复制连接(is_node_internal_connection 为真)仍走 check_hba_replication(),行为不变,gs_basebackup 兼容逻辑保留。

环境说明

  • pg_regress 与 gaussdb 均拒绝 root 运行,测试以普通用户执行;
  • /tmp 为 tmpfs 不支持 O_DIRECT(initdb double-write 文件创建会 EINVAL),测试数据目录临时置于 /var/tmp,测试后已恢复;
  • 磁盘受限,gaussdb 仅执行 strip --strip-debug(去调试符号,不影响行为)。
likedislike