From 63534df0dc0aaea9c16dc457a931b76e5bade341 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 24 Nov 2026 10:33:01 +0100 Subject: [PATCH 1/2] user: add GoDoc for subordinate ID and ID mapping functions Add missing GoDoc comments to the exported ParseSubID and ParseIDMap functions and their File and Filter variants. Signed-off-by: Sebastiaan van Stijn --- user/user.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/user/user.go b/user/user.go index 1f79132..b6b20e4 100644 --- a/user/user.go +++ b/user/user.go @@ -526,14 +526,18 @@ func (s SubID) valid() bool { s.Count >= 0 && s.Count <= min(maxSubID+1-s.SubID, maxSubID) } +// ParseSubIDFile parses subordinate UID/GID ranges from the specified file. func ParseSubIDFile(path string) ([]SubID, error) { return ParseSubIDFileFilter(path, nil) } +// ParseSubID parses subordinate UID/GID ranges from the provided reader. func ParseSubID(subid io.Reader) ([]SubID, error) { return ParseSubIDFilter(subid, nil) } +// ParseSubIDFileFilter parses subordinate UID/GID ranges from the specified +// file, returning only entries matching filter. A nil filter matches all entries. func ParseSubIDFileFilter(path string, filter func(SubID) bool) ([]SubID, error) { subid, err := openUserFile(path) if err != nil { @@ -543,6 +547,8 @@ func ParseSubIDFileFilter(path string, filter func(SubID) bool) ([]SubID, error) return ParseSubIDFilter(subid, filter) } +// ParseSubIDFilter parses subordinate UID/GID ranges from the provided reader, +// returning only entries matching filter. A nil filter matches all entries. func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error) { if r == nil { return nil, errors.New("nil source for subid-formatted data") @@ -581,14 +587,18 @@ func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error) { return out, nil } +// ParseIDMapFile parses ID mappings from the specified file. func ParseIDMapFile(path string) ([]IDMap, error) { return ParseIDMapFileFilter(path, nil) } +// ParseIDMap parses ID mappings from r. func ParseIDMap(r io.Reader) ([]IDMap, error) { return ParseIDMapFilter(r, nil) } +// ParseIDMapFileFilter parses ID mappings from the specified file, +// returning only entries matching filter. A nil filter matches all entries. func ParseIDMapFileFilter(path string, filter func(IDMap) bool) ([]IDMap, error) { r, err := os.Open(path) if err != nil { @@ -598,6 +608,8 @@ func ParseIDMapFileFilter(path string, filter func(IDMap) bool) ([]IDMap, error) return ParseIDMapFilter(r, filter) } +// ParseIDMapFilter parses ID mappings from r, returning only entries +// matching filter. A nil filter matches all entries. func ParseIDMapFilter(r io.Reader, filter func(IDMap) bool) ([]IDMap, error) { if r == nil { return nil, errors.New("nil source for idmap-formatted data") From 3b40b0f788b5693f4080c20cc8be3dbf32c96e9d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 24 Nov 2026 10:46:23 +0100 Subject: [PATCH 2/2] user: limit reads when parsing ID mapping files Use the existing limitedFile reader in ParseIDMapFileFilter to limit reads to 10 MiB. Unlike other user database files, ID mapping files may be procfs-backed, so they cannot be restricted to regular files. Reuse the read limit introduced in bba2f13154cb6 while preserving support for non-regular files such as /proc/self/uid_map and /proc/self/gid_map. See: https://github.com/moby/sys/commit/bba2f13154cb607bfbc7e209f8d9100ef4000329 Signed-off-by: Sebastiaan van Stijn --- user/user.go | 11 +++++-- user/user_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++ user/user_utils.go | 8 +++-- 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/user/user.go b/user/user.go index b6b20e4..66b4a4b 100644 --- a/user/user.go +++ b/user/user.go @@ -599,12 +599,19 @@ func ParseIDMap(r io.Reader) ([]IDMap, error) { // ParseIDMapFileFilter parses ID mappings from the specified file, // returning only entries matching filter. A nil filter matches all entries. +// Reads are limited to 10 MiB, but non-regular files are allowed to support +// procfs-backed UID/GID mapping files. func ParseIDMapFileFilter(path string, filter func(IDMap) bool) ([]IDMap, error) { - r, err := os.Open(path) + // ID mapping files are typically procfs files, so we cannot use + // openUserFile's regular-file check. Enforce the read limit separately. + f, err := os.Open(path) if err != nil { return nil, err } - defer r.Close() + + r := newLimitedFile(f) + defer func() { _ = r.Close() }() + return ParseIDMapFilter(r, filter) } diff --git a/user/user_test.go b/user/user_test.go index 1e9be2c..c58dbd3 100644 --- a/user/user_test.go +++ b/user/user_test.go @@ -1175,3 +1175,80 @@ func largeGroup() (res string) { } return b.String() } + +// TestParseIDMapFileCapsReads asserts the boundary behavior of the read cap: +// well below, ending exactly at, and past maxUserFileBytes. +func TestParseIDMapFileCapsReads(t *testing.T) { + beyond := []byte("\n0 100000 65536\n") + + for _, tc := range []struct { + name string + padBytes int + expIDMaps []IDMap + expErr string + }{ + { + name: "pad below cap, beyond is parsed", + padBytes: 100, + expIDMaps: []IDMap{{ID: 0, ParentID: 100000, Count: 65536}}, + }, + { + name: "beyond ends exactly at cap, is parsed", + padBytes: maxUserFileBytes - len(beyond), + expIDMaps: []IDMap{{ID: 0, ParentID: 100000, Count: 65536}}, + }, + { + name: "pad past cap, read errors out", + padBytes: maxUserFileBytes, + expErr: "file exceeds", + }, + } { + t.Run(tc.name, func(t *testing.T) { + fileName := filepath.Join(t.TempDir(), "uid_map") + + pad := bytes.Repeat([]byte("\n"), tc.padBytes) + data := append(pad, beyond...) + + if err := os.WriteFile(fileName, data, 0o644); err != nil { + t.Fatal(err) + } + + idmaps, err := ParseIDMapFile(fileName) + if tc.expErr != "" { + if err == nil || !strings.Contains(err.Error(), tc.expErr) { + t.Fatalf("expected %q error, got %v", tc.expErr, err) + } + return + } + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(idmaps, tc.expIDMaps) { + t.Fatalf("unexpected idmaps: got %v, want %v", idmaps, tc.expIDMaps) + } + }) + } +} + +// TestParseIDMapFileProcfs verifies that procfs-backed ID mapping files +// remain supported. +func TestParseIDMapFileProcfs(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("requires Linux procfs") + } + + for _, path := range []string{ + "/proc/self/uid_map", + "/proc/self/gid_map", + } { + t.Run(filepath.Base(path), func(t *testing.T) { + idmaps, err := ParseIDMapFile(path) + if err != nil { + t.Fatal(err) + } + if len(idmaps) == 0 { + t.Fatal("expected at least one ID mapping") + } + }) + } +} diff --git a/user/user_utils.go b/user/user_utils.go index 495603e..86fe883 100644 --- a/user/user_utils.go +++ b/user/user_utils.go @@ -37,14 +37,18 @@ func openUserFile(path string) (*limitedFile, error) { } } + return newLimitedFile(f), nil +} + +func newLimitedFile(f *os.File) *limitedFile { return &limitedFile{ File: f, // Allow one byte past the cap so an overflow surfaces as an // error rather than a silent EOF that the parser would treat as // a clean end-of-file (and miss any entries past the cap). LimitedReader: &io.LimitedReader{R: f, N: maxUserFileBytes + 1}, - name: path, - }, nil + name: f.Name(), + } } type limitedFile struct {