mirror of
https://github.com/Wei-Shaw/sub2api
synced 2026-12-06 01:12:31 +00:00
fix(sysutil): confine cgroup limit reads to the mount with os.Root
gosec G703 flagged the cgroup path taken from /proc/self/cgroup flowing into os.ReadFile. Read the limit files through os.Root instead, so the path can never resolve outside the cgroup mount (including via symlinks), and cover the escape cases in tests.
This commit is contained in:
1 parent
c004c962a7
commit
03bd718abe
2 files changed
+28
-5
No files matched your search
@@ -49,24 +49,32 @@ func cgroupMemoryLimit(root, selfCgroupFile string) uint64 {
|
||||
}
|
||||
}
|
||||
// Mount roots cover an unreadable /proc/self/cgroup.
|
||||
limit = minPositive(limit, readCgroupLimitFile(filepath.Join(root, "memory.max")))
|
||||
return minPositive(limit, readCgroupLimitFile(filepath.Join(root, "memory", "memory.limit_in_bytes")))
|
||||
limit = minPositive(limit, cgroupPathMemoryLimit(root, "/", "memory.max"))
|
||||
return minPositive(limit, cgroupPathMemoryLimit(filepath.Join(root, "memory"), "/", "memory.limit_in_bytes"))
|
||||
}
|
||||
|
||||
// cgroupPathMemoryLimit walks from the cgroup up to the mount root. Levels not
|
||||
// visible in this mount (e.g. a host path inside a container) are skipped.
|
||||
// Reads go through os.Root, so a cgroup path from /proc/self/cgroup can never
|
||||
// resolve outside the mount.
|
||||
func cgroupPathMemoryLimit(mount, cgroupPath, limitFile string) uint64 {
|
||||
mountRoot, err := os.OpenRoot(mount)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
defer func() { _ = mountRoot.Close() }()
|
||||
var limit uint64
|
||||
for dir := path.Clean("/" + cgroupPath); ; dir = path.Dir(dir) {
|
||||
limit = minPositive(limit, readCgroupLimitFile(filepath.Join(mount, filepath.FromSlash(dir), limitFile)))
|
||||
name := filepath.Join(filepath.FromSlash(strings.TrimPrefix(dir, "/")), limitFile)
|
||||
limit = minPositive(limit, readCgroupLimitFile(mountRoot, name))
|
||||
if dir == "/" {
|
||||
return limit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readCgroupLimitFile(file string) uint64 {
|
||||
raw, err := os.ReadFile(file)
|
||||
func readCgroupLimitFile(mountRoot *os.Root, name string) uint64 {
|
||||
raw, err := mountRoot.ReadFile(name)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -78,6 +78,21 @@ func TestCgroupMemoryLimit(t *testing.T) {
|
||||
require.Equal(t, uint64(2<<30), cgroupMemoryLimit(root, self))
|
||||
})
|
||||
|
||||
t.Run("cgroup path cannot escape the mount", func(t *testing.T) {
|
||||
parent := t.TempDir()
|
||||
root := filepath.Join(parent, "cgroup")
|
||||
self := filepath.Join(t.TempDir(), "cgroup")
|
||||
writeFile(t, filepath.Join(parent, "outside", "memory.max"), "1073741824\n")
|
||||
writeFile(t, filepath.Join(root, "memory.max"), "max\n")
|
||||
require.NoError(t, os.Symlink(filepath.Join(parent, "outside"), filepath.Join(root, "link")))
|
||||
|
||||
writeFile(t, self, "0::/../outside\n")
|
||||
require.Zero(t, cgroupMemoryLimit(root, self))
|
||||
|
||||
writeFile(t, self, "0::/link\n")
|
||||
require.Zero(t, cgroupMemoryLimit(root, self))
|
||||
})
|
||||
|
||||
t.Run("unavailable", func(t *testing.T) {
|
||||
require.Zero(t, cgroupMemoryLimit(t.TempDir(), filepath.Join(t.TempDir(), "missing")))
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user