diff --git a/user/user.go b/user/user.go index 1f79132..66b4a4b 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,23 +587,36 @@ 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. +// 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) } +// 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") 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 {