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.
This commit is contained in:
sam committed 2026-09-28 02:20:25 +10:00
1 parent 621cfb30a0
commit 7bbb5b6f61
3 files changed
+9 -6

No files matched your search

+6 -5
View File
@@ -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)
}
@@ -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)
@@ -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 {