-
Notifications
You must be signed in to change notification settings - Fork 623
Tolerate mountinfo lines with an empty mount source #7044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arpitjain099
wants to merge
2
commits into
spiffe:main
Choose a base branch
from
arpitjain099:fix/mountinfo-empty-source-7036
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| //go:build !windows | ||
|
|
||
| package containerinfo | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| // mountInfo holds the subset of /proc/<pid>/mountinfo fields that the | ||
| // container info extractor consumes. The full mountinfo line format is | ||
| // documented in proc(5). | ||
| type mountInfo struct { | ||
| // Root is the pathname of the directory in the filesystem which forms the | ||
| // root of this mount (field 4). | ||
| Root string | ||
| // FsType is the filesystem type (the first field after the "-" separator). | ||
| FsType string | ||
| } | ||
|
|
||
| // parseMountInfo parses /proc/<pid>/mountinfo. | ||
| // | ||
| // It exists because k8s.io/mount-utils ParseMountInfo splits each line with | ||
| // strings.Fields, which collapses runs of whitespace and therefore drops an | ||
| // empty mount source field. Per proc(5) the mount source (the field after the | ||
| // filesystem type) may be empty (for example a tmpfs mount has no source), and | ||
| // the kernel escapes any real whitespace in a path as octal (\040). A line | ||
| // like: | ||
| // | ||
| // 119 206 0:68 / /local rw,relatime - tmpfs rw,size=8192k | ||
| // | ||
| // is therefore valid: the double space between "tmpfs" and "rw,size=8192k" | ||
| // unambiguously means an empty source. The upstream parser counts that as 9 | ||
| // fields (it expects at least 10) and rejects the entire file, which made the | ||
| // docker and k8s workload attestors fail attestation outright. This parser | ||
| // uses strings.Split (single-space delimiter) so the empty source field is | ||
| // preserved as an empty string rather than collapsed. | ||
| func parseMountInfo(filename string) ([]mountInfo, error) { | ||
| f, err := os.Open(filename) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var infos []mountInfo | ||
| scanner := bufio.NewScanner(f) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if line == "" { | ||
| continue | ||
| } | ||
| info, err := parseMountInfoLine(line) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| infos = append(infos, info) | ||
| } | ||
| if err := scanner.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
| return infos, nil | ||
| } | ||
|
|
||
| // parseMountInfoLine parses a single mountinfo line. The format (see proc(5)) | ||
| // is, in order: | ||
| // | ||
| // (1) mount ID (2) parent ID (3) major:minor (4) root (5) mount point | ||
| // (6) mount options (7..) zero or more optional fields (8) a "-" separator | ||
| // (9) filesystem type (10) mount source (11) super options | ||
| // | ||
| // Only the fields the extractor needs (root and filesystem type) are returned. | ||
| // The line is split on single spaces so that an empty mount source (field 10) | ||
| // is preserved as an empty string rather than collapsed by strings.Fields. | ||
| func parseMountInfoLine(line string) (mountInfo, error) { | ||
| // Split on single spaces so empty fields (e.g. an empty mount source | ||
| // between "tmpfs" and the super options) survive as empty strings. | ||
| fields := strings.Split(line, " ") | ||
|
|
||
| // Locate the "-" separator. Everything before it is the fixed + | ||
| // optional-tag fields; everything after is fstype, source, super options. | ||
| sepIdx := -1 | ||
| for i, f := range fields { | ||
| if f == "-" { | ||
| sepIdx = i | ||
| break | ||
| } | ||
| } | ||
| if sepIdx < 0 { | ||
| return mountInfo{}, fmt.Errorf("missing separator in mountinfo line: %s", line) | ||
| } | ||
|
|
||
| // Before the separator: mount ID (0), parent ID (1), major:minor (2), | ||
| // root (3), mount point (4), mount options (5), then zero or more | ||
| // optional fields. We need at least 6. | ||
| if sepIdx < 6 { | ||
| return mountInfo{}, fmt.Errorf("expected at least 6 fields before separator in mountinfo line: %s", line) | ||
| } | ||
|
|
||
| // After the separator: filesystem type (sepIdx+1), mount source | ||
| // (sepIdx+2, may be empty), super options (sepIdx+3). We only need | ||
| // the filesystem type. | ||
| if sepIdx+1 >= len(fields) || fields[sepIdx+1] == "" { | ||
| return mountInfo{}, fmt.Errorf("missing filesystem type in mountinfo line: %s", line) | ||
| } | ||
|
|
||
| return mountInfo{ | ||
| Root: fields[3], | ||
| FsType: fields[sepIdx+1], | ||
| }, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| //go:build !windows | ||
|
|
||
| package containerinfo | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestParseMountInfoLine(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| name string | ||
| line string | ||
| wantRoot string | ||
| wantType string | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "normal cgroup2 line", | ||
| line: "1543 1542 0:32 /some/root /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw,nsdelegate", | ||
| wantRoot: "/some/root", | ||
| wantType: "cgroup2", | ||
| }, | ||
| { | ||
| name: "optional fields before separator", | ||
| line: "573 572 0:33 /docker/abc /sys/fs/cgroup/systemd ro,nosuid,nodev,noexec,relatime master:11 - cgroup cgroup rw,name=systemd", | ||
| wantRoot: "/docker/abc", | ||
| wantType: "cgroup", | ||
| }, | ||
| { | ||
| // Regression for #7036: a tmpfs mount has no source, so the field | ||
| // after the filesystem type is empty. strings.Fields would collapse | ||
| // it and the upstream parser rejected the whole file. | ||
| name: "tmpfs with empty source", | ||
| line: "119 206 0:68 / /local rw,relatime - tmpfs rw,size=8192k", | ||
| wantRoot: "/", | ||
| wantType: "tmpfs", | ||
| }, | ||
| { | ||
| name: "missing separator", | ||
| line: "1543 1542 0:32 /some/root /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime cgroup2 cgroup rw", | ||
| wantErr: "missing separator", | ||
| }, | ||
| { | ||
| name: "too few fields before separator", | ||
| line: "1543 1542 0:32 - cgroup2 cgroup rw", | ||
| wantErr: "expected at least 6 fields before separator", | ||
| }, | ||
| { | ||
| name: "missing filesystem type after separator", | ||
| line: "1543 1542 0:32 /some/root /sys/fs/cgroup rw - ", | ||
| wantErr: "missing filesystem type", | ||
| }, | ||
| } { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| info, err := parseMountInfoLine(tt.line) | ||
| if tt.wantErr != "" { | ||
| assert.ErrorContains(t, err, tt.wantErr) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.wantRoot, info.Root) | ||
| assert.Equal(t, tt.wantType, info.FsType) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestParseMountInfo(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "mountinfo") | ||
| content := "" + | ||
| "2356 2355 0:30 /../containerid /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw\n" + | ||
| "119 206 0:68 / /local rw,relatime - tmpfs rw,size=8192k\n" | ||
| require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) | ||
|
|
||
| infos, err := parseMountInfo(path) | ||
| require.NoError(t, err) | ||
| require.Len(t, infos, 2) | ||
|
|
||
| assert.Equal(t, "/../containerid", infos[0].Root) | ||
| assert.Equal(t, "cgroup2", infos[0].FsType) | ||
|
|
||
| // The tmpfs line with an empty source must still parse. | ||
| assert.Equal(t, "/", infos[1].Root) | ||
| assert.Equal(t, "tmpfs", infos[1].FsType) | ||
| } |
2 changes: 2 additions & 0 deletions
2
pkg/common/containerinfo/testdata/docker/tmpfs-empty-source/proc/123/mountinfo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 2356 2355 0:30 /../0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw | ||
| 119 206 0:68 / /local rw,relatime - tmpfs rw,size=8192k |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work here, but might need an extra import: