From 7bbb5b6f61ea35f183ff8d54f8d0f643fa4463ac Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 28 Sep 2026 02:20:25 +1000 Subject: [PATCH] fix(security): bound harness IDs to int and drop a summed map size hint Clears the five open CodeQL alerts. harnessCredential parsed uid and gid as 32-bit and os.Chown converted them to int, which wraps on a 32-bit build; they are now parsed to 31 bits, so an out-of-range id is refused where it is read. mergeMapping sized its map by len(base)+len(override); it now hints len(base) and lets the map grow. --- internal/agentexec/harness_proc_unix.go | 11 ++++++----- internal/agentexec/harness_runner_test.go | 2 ++ internal/infrastructure/configuration/apply.go | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/agentexec/harness_proc_unix.go b/internal/agentexec/harness_proc_unix.go index d500f1052..b2273b6c3 100644 --- a/internal/agentexec/harness_proc_unix.go +++ b/internal/agentexec/harness_proc_unix.go @@ -41,10 +41,11 @@ func runAsHarnessUser(cmd *exec.Cmd, name string) error { } // harnessCredential resolves a user name or "uid[:gid]" to a credential. -// Root is refused whichever way it is named. +// Root is refused whichever way it is named. IDs are bounded to 31 bits so +// they convert to int for os.Chown on every platform. func harnessCredential(name string) (*syscall.Credential, error) { uidText, gidText, hasGid := strings.Cut(name, ":") - uid, uidErr := strconv.ParseUint(uidText, 10, 32) + uid, uidErr := strconv.ParseUint(uidText, 10, 31) var cred syscall.Credential if uidErr == nil { cred.Uid, cred.Gid = uint32(uid), uint32(uid) @@ -53,18 +54,18 @@ func harnessCredential(name string) (*syscall.Credential, error) { if err != nil { return nil, fmt.Errorf("harness user %q: %w", uidText, err) } - id, err := strconv.ParseUint(u.Uid, 10, 32) + id, err := strconv.ParseUint(u.Uid, 10, 31) if err != nil { return nil, fmt.Errorf("harness user %q has non-numeric uid %q", uidText, u.Uid) } - group, err := strconv.ParseUint(u.Gid, 10, 32) + group, err := strconv.ParseUint(u.Gid, 10, 31) if err != nil { return nil, fmt.Errorf("harness user %q has non-numeric gid %q", uidText, u.Gid) } cred.Uid, cred.Gid = uint32(id), uint32(group) } if hasGid { - gid, err := strconv.ParseUint(gidText, 10, 32) + gid, err := strconv.ParseUint(gidText, 10, 31) if err != nil { return nil, fmt.Errorf("harness group %q is not numeric", gidText) } diff --git a/internal/agentexec/harness_runner_test.go b/internal/agentexec/harness_runner_test.go index 0d36ff756..fb2c9e3f4 100644 --- a/internal/agentexec/harness_runner_test.go +++ b/internal/agentexec/harness_runner_test.go @@ -388,6 +388,8 @@ func TestHarnessUser(t *testing.T) { {"0", 0, 0, true}, {"root", 0, 0, true}, {"not-a-user-anywhere", 0, 0, true}, + {"2147483648", 0, 0, true}, + {"1000:2147483648", 0, 0, true}, } for _, tt := range tests { cred, err := harnessCredential(tt.user) diff --git a/internal/infrastructure/configuration/apply.go b/internal/infrastructure/configuration/apply.go index a66e2efe3..5afdbdaa5 100644 --- a/internal/infrastructure/configuration/apply.go +++ b/internal/infrastructure/configuration/apply.go @@ -185,7 +185,7 @@ func asMapping(value reflect.Value) (map[string]any, error) { // else replaces, matching how the decode treats a struct field versus a scalar. // It allocates rather than mutating either argument. func mergeMapping(base, override map[string]any) map[string]any { - out := make(map[string]any, len(base)+len(override)) + out := make(map[string]any, len(base)) maps.Copy(out, base) for key, value := range override { if over, ok := value.(map[string]any); ok {