From 0b753e550ebd73d2ff3ce743dc0c94115bb6b669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Mon, 16 Nov 2026 23:15:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(libraryagent):=20=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E6=98=BE=E5=BC=8F=20peer=20uid=20=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 容器里的治理进程以固定非 root 用户运行,agent 原先只接受与自身同 uid 的连接, socket 挂进容器后一律被拒(peer uid is not allowed)。新增 --allow-peer-uid, 默认仍只允许同 uid,放行清单只能由部署参数给出,避免"挂上就能连"。 --- CHANGELOG.md | 8 ++++++++ cmd/libraryagent/main.go | 16 ++++++++++++++++ internal/libraryagent/agent.go | 11 +++++++++-- internal/libraryagent/peer_linux.go | 6 ++++-- internal/libraryagent/peer_other.go | 2 +- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e78d16..c1b1a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 开发快照 0.1.382(采集 agent 允许显式 peer uid) + +### Added + +- 采集 agent 新增 `--allow-peer-uid`:默认仍只允许与 agent 同 uid 的连接;容器以固定 + 非 root 用户运行时,由部署参数显式列出该 uid。socket 文件权限之外再加一层 uid 校验, + 放行清单来自部署,不是"谁都能连"。 + ## 开发快照 0.1.381(维护窗口支持显式 no-gate 测试模式) ### Added diff --git a/cmd/libraryagent/main.go b/cmd/libraryagent/main.go index 9517dd7..d401add 100644 --- a/cmd/libraryagent/main.go +++ b/cmd/libraryagent/main.go @@ -10,6 +10,8 @@ import ( "os" "os/signal" "path/filepath" + "strconv" + "strings" "syscall" "time" @@ -33,9 +35,23 @@ func main() { flag.StringVar(&config.Project, "project", "weknora071full", "Compose project owning the writers") flag.StringVar(&config.Writers, "writers", "app", "declared writer services") nonWriters := flag.String("non-writers", defaultNonWriters, "service=reason for every other project service") + allowUIDs := flag.String("allow-peer-uid", "", "extra peer uids allowed to connect (comma separated; default: same uid only)") flag.DurationVar(&config.MaxDuration, "max-duration", 10*time.Minute, "upper bound for one operation") describe := flag.Bool("describe", false, "print the fixed configuration and exit") flag.Parse() + if strings.TrimSpace(*allowUIDs) != "" { + for _, raw := range strings.Split(*allowUIDs, ",") { + raw = strings.TrimSpace(raw) + if raw == "" { + continue + } + uid, err := strconv.ParseUint(raw, 10, 32) + if err != nil { + log.Fatalf("--allow-peer-uid 需要十进制 uid:%q", raw) + } + config.AllowedPeerUIDs = append(config.AllowedPeerUIDs, uint32(uid)) + } + } config.NonWriters = *nonWriters agent, err := libraryagent.New(config) diff --git a/internal/libraryagent/agent.go b/internal/libraryagent/agent.go index d871a27..43765a6 100644 --- a/internal/libraryagent/agent.go +++ b/internal/libraryagent/agent.go @@ -49,7 +49,10 @@ type Config struct { Project string Writers string NonWriters string - MaxDuration time.Duration + // AllowedPeerUIDs 是除"与 agent 同 uid"之外额外放行的连接方 uid。 + // 默认空:只允许同 uid。容器里固定以某个 uid 运行时,由部署参数显式列出。 + AllowedPeerUIDs []uint32 + MaxDuration time.Duration // StopGrace bounds the graceful stop of one operation: SIGTERM first (the // child aborts the maintenance window and restores writers), then SIGKILL // only after this grace period. It must exceed the child's own restore budget. @@ -214,7 +217,11 @@ func (a *Agent) Serve(ctx context.Context) error { func (a *Agent) handle(ctx context.Context, conn net.Conn) { defer conn.Close() - if err := peerAllowed(conn); err != nil { + allowed := make(map[uint32]bool, len(a.config.AllowedPeerUIDs)) + for _, uid := range a.config.AllowedPeerUIDs { + allowed[uid] = true + } + if err := peerAllowed(conn, allowed); err != nil { _ = json.NewEncoder(conn).Encode(response{OK: false, Error: err.Error()}) return } diff --git a/internal/libraryagent/peer_linux.go b/internal/libraryagent/peer_linux.go index 07887af..491ca51 100644 --- a/internal/libraryagent/peer_linux.go +++ b/internal/libraryagent/peer_linux.go @@ -11,7 +11,7 @@ import ( // agent. File mode 0600 already limits connect() to that uid; the credential // check makes the boundary explicit and fails closed if the socket is ever // reached another way (bind mount into a container with a different uid). -func peerAllowed(conn net.Conn) error { +func peerAllowed(conn net.Conn, allowed map[uint32]bool) error { unixConn, ok := conn.(*net.UnixConn) if !ok { return errors.New("unsupported transport") @@ -30,7 +30,9 @@ func peerAllowed(conn net.Conn) error { if controlErr != nil || credential == nil { return errors.New("peer credentials unavailable") } - if credential.Uid != uint32(os.Geteuid()) { + // 默认只允许与 agent 同 uid 的连接;显式声明过的 uid(例如容器里的 + // 固定运行用户)才放行。放行清单来自部署参数,不是"任何人"。 + if credential.Uid != uint32(os.Geteuid()) && !allowed[credential.Uid] { return errors.New("peer uid is not allowed") } return nil diff --git a/internal/libraryagent/peer_other.go b/internal/libraryagent/peer_other.go index a7eb1fb..f1ccf8d 100644 --- a/internal/libraryagent/peer_other.go +++ b/internal/libraryagent/peer_other.go @@ -9,6 +9,6 @@ import ( // The capture host is Linux (Docker + unix socket); other platforms refuse // rather than guess at peer credentials. -func peerAllowed(net.Conn) error { +func peerAllowed(net.Conn, map[uint32]bool) error { return errors.New("library agent requires Linux peer credentials") }