mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
850b2c47b2
Commit88e8350de2, among the other things, replaced filepath.Join with securejoin.SecureJoin for both reads and writes to cgroupfs. Commitse76ac1c054and31f0f5b7e0switched more code to use fscommon.ReadFile (and thus securejoin). Commit0228226e6dintroduced fscommon.OpenFile (which uses securejoin as the fallback if openat2(2) is not available, which is the case for older kernels), and commitc95e69007cswitched most of cgroup/fs[2] code to use it. As a result, fs.GetStats() method became noticeable slower, mostly due to securejoin calling os.Lstat and filepath.Clean. Using securejoin as a security measure for cgroupfs files is not well justified, as cgroupfs do not contain symlinks, and none of the code using it have uncleaned paths. In particular, fs/fs2/systemd managers do check and sanitize their paths. This commit modifies the code to not use securejoin. Instead, it checks that the opened file is indeed on cgroupfs. Using BenchmarkGetStats on a CentOS 8 VM, I see the following improvement: Before: > BenchmarkGetStats-8 8376 625135 ns/op After: > BenchmarkGetStats-8 12226 485015 ns/op An intermediate version, with no fstatfs to check fstype: > BenchmarkGetStats-8 13162 452281 ns/op Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
102 lines
2.1 KiB
Go
102 lines
2.1 KiB
Go
// +build linux
|
|
|
|
package fscommon
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
cgroupFile = "cgroup.file"
|
|
floatValue = 2048.0
|
|
floatString = "2048"
|
|
)
|
|
|
|
func init() {
|
|
TestMode = true
|
|
}
|
|
|
|
func TestGetCgroupParamsInt(t *testing.T) {
|
|
// Setup tempdir.
|
|
tempDir, err := ioutil.TempDir("", "cgroup_utils_test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
tempFile := filepath.Join(tempDir, cgroupFile)
|
|
|
|
// Success.
|
|
err = ioutil.WriteFile(tempFile, []byte(floatString), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err := GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != floatValue {
|
|
t.Fatalf("Expected %d to equal %f", value, floatValue)
|
|
}
|
|
|
|
// Success with new line.
|
|
err = ioutil.WriteFile(tempFile, []byte(floatString+"\n"), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != floatValue {
|
|
t.Fatalf("Expected %d to equal %f", value, floatValue)
|
|
}
|
|
|
|
// Success with negative values
|
|
err = ioutil.WriteFile(tempFile, []byte("-12345"), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != 0 {
|
|
t.Fatalf("Expected %d to equal %d", value, 0)
|
|
}
|
|
|
|
// Success with negative values lesser than min int64
|
|
s := strconv.FormatFloat(math.MinInt64, 'f', -1, 64)
|
|
err = ioutil.WriteFile(tempFile, []byte(s), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != 0 {
|
|
t.Fatalf("Expected %d to equal %d", value, 0)
|
|
}
|
|
|
|
// Not a float.
|
|
err = ioutil.WriteFile(tempFile, []byte("not-a-float"), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err == nil {
|
|
t.Fatal("Expecting error, got none")
|
|
}
|
|
|
|
// Unknown file.
|
|
err = os.Remove(tempFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err == nil {
|
|
t.Fatal("Expecting error, got none")
|
|
}
|
|
}
|