Open
fix(selinux): setenforce 只改运行态,补充持久化检查与提示(fix #16) #282
fix(selinux): setenforce 只改运行态,补充持久化检查与提示(fix #16) #282
Open
yaqiangchenAdd on 1 day ago
yaqiangchen
yaqiangchen
1 day ago

fix #16

问题

部署脚本里只有 setenforce 0:

# deploy/build.sh
command -v setenforce >/dev/null 2>&1 && setenforce 0

它只修改当前内核的 SELinux 模式。/etc/selinux/config 仍是 SELINUX=enforcing 时:

  • 部署当天一切正常(permissive 只记录 AVC 不拦截,问题被掩盖);
  • 节点重启后回到 enforcing,containerd / harbor 被 AVC 拒绝(execute_no_trans 等)→ 无限重启。

deploy/check-env.sh 原来也只检查运行态("SELinux 未关闭,建议 setenforce 0"),同样发现不了这个陷阱;文档里也只写了 setenforce 0。

改动

deploy/common.sh 新增三个共用函数(build.sh / deploy.sh 都 source 本文件):

函数 作用
selinux_runtime_vs_config() 比较 getenforce 运行态与 /etc/selinux/config 的 SELINUX=,不一致返回非 0;未启用 SELinux 或配置缺键时返回 0(不误报)
selinux_persist_hint() 打印两种持久化方案
disable_selinux_runtime() 替代裸 setenforce 0:临时置 permissive 后,若检测到「重启会回退」立即告警并给出命令

deploy/build.sh:install() 与 install_client() 里的 setenforce 0 改为 disable_selinux_runtime,不再静默改变安全姿态却不说后果。

deploy/check-env.sh:SELinux 检查从「运行态未关闭就 warn」改为「运行态与持久配置必须一致」,不一致列为 FAIL:

✘ SELinux 运行态与持久配置一致(运行态: permissive / 配置: enforcing)
    -> 两者不一致:重启后会回到 enforcing,containerd / harbor 可能无限重启(issue #16)。
       持久化二选一:
         a) 保持关闭:sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config   # 或 disabled
         b) 保持 enforcing:对容器数据目录执行 restorecon -R,并为容器运行时加载/编写策略,再重启验证

放在 check_common() 里,--install 与 --start 都会检查,部署前就能拦住,而不是重启后才发现。

文档:docs/zh/e2b_user_guide.md 4.3、deploy/DEPLOY_DESIGN.md 10.1 补充「setenforce 只对当前内核生效」以及两种持久化方案。

验证

  • bash -n deploy/common.sh deploy/build.sh deploy/check-env.sh 全部通过;
  • 一致性判断覆盖 5 种情况,结果符合预期:
    运行态 配置 结果
    permissive permissive 一致
    permissive enforcing MISMATCH(#16 场景)
    disabled disabled 一致
    permissive 缺 SELINUX= 键 跳过(不误报)
    permissive Enforcing(大小写混写) MISMATCH
  • 用替身 getenforce 实跑 check-env.sh --install:SELinux 分支输出正常、脚本未中断。

设计取舍

  1. 没有自动改写 /etc/selinux/config。那属于静默修改系统安全姿态,部署脚本不该替用户决定;这里只做「检测 + 明确告知 + 给出命令」。
  2. 不一致按 FAIL 而非 WARN。按 issue 的实测,重启后是节点级不可用(containerd + harbor 双挂),比 WARN 该有的分量重得多。
  3. 配置缺 SELINUX= 键或未装 SELinux 时跳过,避免在容器/裁剪系统里误报。
likedislike
Merge blocked
yaqiangchenyaqiangchen
1 day ago Created pull request, commit 76934905
yaqiangchenyaqiangchen
1 day ago Associated issue: E2B主节点重启后,e2b主节点上containerd无限重启,harbor服务也起不来
atomgit-bot
atomgit-bot
1 day ago Comments:

变更摘要

本 PR 针对 issue #16 中「setenforce 0 只修改 SELinux 运行态、重启后回落到 /etc/selinux/config 配置的 enforcing 模式,导致 containerd / harbor 被 AVC 拒绝并无限重启」的问题,将 SELinux 处理从单一运行态操作升级为「运行态与持久配置一致性」检查。核心改动是在 deploy/common.sh 中新增 selinux_runtime_vs_config()、selinux_persist_hint()、disable_selinux_runtime() 三个共用函数,deploy/build.sh 改用 disable_selinux_runtime 替代裸 setenforce 0,deploy/check-env.sh 把不一致场景提升为 FAIL 并在部署前拦截;同时补充了相关文档说明。

主要改动

  • 新增 SELinux 一致性判断函数: deploy/common.sh 增加 selinux_runtime_vs_config(),将 getenforce 结果与 /etc/selinux/config 中 SELINUX= 取值统一小写后比较,不一致返回非 0;未安装 getenforce 或配置缺键时返回 0 以避免误报。
  • 新增提示与运行态关闭封装: 同文件新增 selinux_persist_hint() 输出两种持久化方案(改写配置为 permissive/disabled,或 restorecon -R 加策略后再验证),以及 disable_selinux_runtime() 在 setenforce 0 后若检测到「重启会回退」即通过 warn 告警。
  • 构建流程替换裸命令: deploy/build.sh 的 install() 与 install_client() 中 command -v setenforce >/dev/null 2>&1 && setenforce 0 均替换为 disable_selinux_runtime,避免静默改变安全姿态而未告知后果。
  • 环境检查判定升级为 FAIL: deploy/check-env.sh 的 check_common() 中原「未关闭则 warn」逻辑改为对运行态与配置一致性执行 check,不一致时记为失败并给出重启风险说明与两种修复命令,使 --install 与 --start 均能在部署前拦截。
  • 文档补充持久化说明: docs/zh/e2b_user_guide.md 4.3 与 deploy/DEPLOY_DESIGN.md 10.1 补充「setenforce 仅对当前内核生效」以及持久化二选一方案。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
1 day ago Comments:

代码审查

审查结论

逐文件确认

文件 结论
deploy/build.sh 只有两处替换(setenforce 0 → disable_selinux_runtime),common.sh 在 build.sh:91 已先 source,warn 定义在 common.sh:14,调用顺序正确;脚本无 set -e,函数返回非 0 不会中断。无新增问题(仍是上文 common.sh 缺陷的调用方)。
deploy/check-env.sh 有问题(1 项:方向无关的相等性判断导致误报 FAIL)。脚本无 set -e,"$([ ... ]; echo $?)" 的写法在本文件中与既有风格一致,不会取到空值。
deploy/common.sh 有问题(2 项:setenforce 0 失败被吞导致静默继续;持久化提示文案方向错误)。
docs/zh/e2b_user_guide.md 有问题(1 项:声称 check-env.sh 部署前即可检出,与实际必然通过不符)。
deploy/DEPLOY_DESIGN.md 无独立问题(10.1 内容与代码基本一致,仅"build.sh 的 disable_selinux_runtime"归属描述及"部署前即可发现"这两处准确性/表述问题,其中后者已并入文档项,其余属文档措辞,不单列)。

发现统计

  • P2 × 2:check-env.sh:93-98 方向无关判断导致误报 FAIL;common.sh:59-63 setenforce 0 失败被 || true 吞掉后仍静默在 enforcing 下部署。
  • P3 × 2:common.sh:48-51 提示文案假设"重启后变严格";docs/zh/e2b_user_guide.md:421 关于部署前可检出的描述不成立。
  • P0/P1:无。

总体风险判断

改动方向正确(补齐 setenforce 只改运行态这一根因的检测与告知,且没有自动改写 /etc/selinux/config 这一危险动作),不引入构建/语法/安全回归。主要风险集中在判断语义而非脚本健壮性:把"运行态与配置不相等"等同于"重启后会被 enforcing 拦截",会在用户按文档改用 disabled/临时切回 enforcing 等安全方向上产生误报 FAIL 和相反结论的提示;同时 setenforce 0 的失败被完全吞掉,使 #16 的"静默掩盖"在最需要提示的场景下仍可能复现。建议合并前修正判据方向与失败可见性,并校正两处文档中"部署前即可发现"的说法。

类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

likedislike
Incorrect?
openeuler-ci-botopeneuler-ci-botCollaborators
1 day ago Set weibaohui,yangzhao_kl,jimmieme,duguhaotian,wonleing,zmr_2020,gaodaweiky,lu-wei-army,feijianqiang,zhaoyifan28 as approver
openeuler-ci-botopeneuler-ci-botCollaborators
1 day ago Added label: sig/sig-CloudNative
openeuler-ci-bot
openeuler-ci-botCollaborators
1 day ago Comments:

Welcome To openEuler Community

Hey @yaqiangchen , 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: sig-CloudNative ,
and any of the maintainers: @duguhaotian, @feijianqiang, @gaodaweiky, @jimmieme, @lu-wei-army, @weibaohui, @wonleing, @yangzhao_kl, @zhaoyifan28, @zmr_2020 ,
and any of the committers: @feijianqiang, @fly_1997, @zhourenjianz .

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
1 day ago Added label: openeuler-cla/yes
openeuler-ci-bot
openeuler-ci-botCollaborators
1 day ago Comments:

CLA Signature Pass

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

likedislike
openeuler-botopeneuler-botCollaborators
1 day ago Added label: ci_processing
openeuler-bot
openeuler-botCollaborators
1 day ago Comments:

门禁正在运行, 您可以通过以下链接查看实时门禁检查结果.
若您对门禁结果含义不清晰或者遇到问题不知如何解决,可参考门禁指导手册
门禁入口及编码规范检查: multiarch/openeuler/trigger/KASandbox/649

likedislike
atomgit-bot
atomgit-bot1 day agoStart code review1
deploy/common.sh
@@ -26,0 +60,4 @@
60+ 
61+ if ! selinux_runtime_vs_config; then
62+ warn "SELinux 已临时置为 Permissive,但它只对当前内核生效。"
63+ selinux_persist_hint
atomgit-bot
atomgit-bot1 day agoComments:

🟡 Medium Priority

改动行:deploy/common.sh:59 setenforce 0 2>/dev/null || true,替代了 deploy/build.sh:627 / deploy/build.sh:652 原来的裸 setenforce 0。

失败链条:2>/dev/null 丢弃了 stderr,|| true 丢弃了退出码 → 无论 setenforce 0 是否成功,函数都继续;紧随其后的唯一判据是 selinux_runtime_vs_config()(deploy/common.sh:32-42),它比较"运行态 vs 配置"。当 setenforce 0 失败时运行态仍是 enforcing,而配置通常也是 enforcing,两者相等 → 返回 0 → disable_selinux_runtime 不打印任何警告,warn 分支根本不进入。

触发条件:setenforce 存在但写入 /sys/fs/selinux/enforce 失败(容器化/裁剪环境、内核以 selinux=0 启动但用户态留了 setenforce、非预期权限上下文等)。结果:部署在 enforcing 下继续推进,而 issue #16 的容器 AVC 拦截问题在部署阶段就存在(只是被 selinux 策略拦在别处先失败),操作者拿不到任何提示——这正是本 PR 想消除的"静默执行让操作者误以为 SELinux 已关闭"。另外此时 deploy/common.sh:62 的文案"已临时置为 Permissive"也与事实不符(实际仍是 enforcing)。

建议:去掉 || true,在 setenforce 0 失败时告警并输出当前运行态;同时校验执行后的 getenforce 结果,只有确认为 permissive/disabled 时才走"持久化提示"分支,否则直接提示 SELinux 未关闭、containerd/harbor 可能启动失败。

Suggestions
63
+ setenforce 0 2>/dev/null || {
64
+ warn "setenforce 0 执行失败,SELinux 仍为 $(getenforce 2>/dev/null || echo 未知),containerd / harbor 可能无法启动"
65
+ }
66
+
67
+ if ! selinux_runtime_vs_config; then
68
+ warn "SELinux 已临时置为 Permissive,但它只对当前内核生效。"
63
- selinux_persist_hint
69
+ selinux_persist_hint
70
+ fi
Apply suggestion
likedislike
Incorrect?
atomgit-bot
atomgit-bot1 day agoStart code review1
deploy/check-env.sh
@@ -92,0 +95,4 @@
95+ "两者不一致:重启后会回到 $selinux_config,containerd / harbor 可能无限重启(issue #16)。
96+ 持久化二选一:
97+ a) 保持关闭:sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config # 或 disabled
98+ b) 保持 enforcing:对容器数据目录执行 restorecon -R,并为容器运行时加载/编写策略,再重启验证"
atomgit-bot
atomgit-bot1 day agoComments:

🟡 Medium Priority

改动行:deploy/check-env.sh:93-98(以及 deploy/common.sh:39-41/48-51 同一判据的 selinux_runtime_vs_config / selinux_persist_hint)只比较 if [ "$selinux_runtime" = "$selinux_config" ],把任意运行态/配置差异都判定为"重启后 containerd / harbor 可能无限重启(issue #16)"。

#16 的危险方向只有一个:重启后回到 enforcing(config=enforcing 且运行态≠enforcing)。反之方向是安全的(重启后更宽松),但同样会走 FAIL 分支:

  1. 用户按本次新增文档 docs/zh/e2b_user_guide.md:415 / deploy/DEPLOY_DESIGN.md:680 的"a) 保持关闭"用例执行 sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config(文档明确写了"或 disabled"),在重启前运行 ./check-env.sh --start:运行态仍是 permissive(/etc/selinux/config 只在重启时生效),配置已是 disabled → 判为 FAIL,并打印"重启后会回到 disabled,containerd / harbor 可能无限重启(issue #16)"。该结论是错的:disabled 比 permissive 更宽松,重启不会产生 AVC 硬拦截。用户被要求去"修复"一个已经正确的配置,检查项退出码非 0(exit "$FAIL_COUNT")。
  2. 用户按"b) 保持 enforcing"改回 SELINUX=enforcing、尚未重启(或运维临时 setenforce 1):运行态 permissive / 配置 enforcing 以外的组合——例如运行态 enforcing、配置 permissive——同样 FAIL 并声称"重启后会回到 permissive,可能无限重启",与实际相反。

deploy/common.sh:48 的提示同样无条件写成"重启后会回到 config,containerd/harbor可能无限重启",仅当‘{config},containerd / harbor 可能无限重启",仅当 `{config}` 为 enforcing 时才成立。

建议:把判据从"是否相等"改为"重启后是否会回到 enforcing":只有 selinux_config=enforcing 且运行态不是 enforcing 时才 FAIL,其余(config 为 permissive/disabled)视为通过或降级为 WARN;同时把 check 描述改成"重启后不会回到 enforcing",避免描述与实际判据不符。最佳做法是直接复用 deploy/common.sh 的 selinux_runtime_vs_config(),删掉这里的重复 sed 解析。

Suggestions
98
- b) 保持 enforcing:对容器数据目录执行 restorecon -R,并为容器运行时加载/编写策略,再重启验证"
98
+ check "SELinux 重启后不会回到 enforcing(运行态: $selinux_runtime / 配置: $selinux_config)" \
99
+ "$([ "$selinux_runtime" = "$selinux_config" ] || [ "$selinux_config" != "enforcing" ]; echo $?)" \
Apply suggestion
likedislike
Incorrect?
openeuler-botopeneuler-botCollaborators
1 day ago Removed label: ci_processing
openeuler-bot
openeuler-botCollaborators
1 day ago Comments:
Check Name Build Result 详情 Build Details
check_package_license ✅SUCCESS #649
check_sca ✅SUCCESS
x86_64 check_build ✅SUCCESS #652
aarch64 check_build ✅SUCCESS #668
DT check_build ✅SUCCESS #665
check_interface check_build ✅SUCCESS #102
likedislike
openeuler-botopeneuler-botCollaborators
1 day ago Added label: ci_successful
openeuler-bot
openeuler-botCollaborators
1 day ago Comments:
likedislike