修复Linux加固服务无法读取初始化网卡地址

This commit is contained in:
xuans committed 2026-09-17 11:17:48 +08:00
1 parent d46e84865a
commit 5573c64b20
3 files changed
+30 -3

No files matched your search

+2 -1
View File
@@ -202,7 +202,8 @@ func renderUnitFile(options Options) (string, error) {
"RestrictSUIDSGID=yes",
"RemoveIPC=yes",
"SystemCallArchitectures=native",
"RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6",
// 初始化证书枚举本机网卡需要 NETLINK_ROUTE,空能力集仍禁止网络管理操作。
"RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK",
)
if len(normalized.ReadWritePaths) > 0 {
quotedPaths := make([]string, 0, len(normalized.ReadWritePaths))
+26 -1
View File
@@ -3,6 +3,7 @@
package servicehost
import (
"os"
"path/filepath"
"strings"
"testing"
@@ -53,7 +54,7 @@ func TestRenderUnitFileIncludesExecutableConfigurationAndWorkDir(t *testing.T) {
"RestrictSUIDSGID=yes",
"RemoveIPC=yes",
"SystemCallArchitectures=native",
"RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6",
"RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK",
"ReadWritePaths=\"/var/lib/scert\" \"/var/log/scert\"",
"[Install]",
"WantedBy=multi-user.target",
@@ -64,6 +65,30 @@ func TestRenderUnitFileIncludesExecutableConfigurationAndWorkDir(t *testing.T) {
}
}
func TestPackagedUnitAllowsInterfaceDiscoveryWithoutCapabilities(t *testing.T) {
content, err := os.ReadFile(filepath.Join("..", "..", "..", "deploy", "systemd", "Scert.service"))
if err != nil {
t.Fatal(err)
}
for _, expected := range []string{
"RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK",
"CapabilityBoundingSet=",
"AmbientCapabilities=",
"NoNewPrivileges=yes",
"User=Scert",
} {
found := false
for _, line := range strings.Split(string(content), "\n") {
if strings.TrimSpace(line) == expected {
found = true
}
}
if !found {
t.Errorf("发布服务缺少安全配置:%s", expected)
}
}
}
func TestUnitFilePathRejectsPathTraversal(t *testing.T) {
for _, name := range []string{"", ".", "..", "../Scert", "a/b", `a\\b`} {
if _, err := unitFilePath(name); err == nil {