修复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

+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 {