Skip to content

Commit 5e1dcdf

Browse files
committed
libct/cg: add internal/path.Inner
The code which determines inner cgroup path from cgroup config is identical in fs and fs2 drivers, and it is using utils.CleanPath. In preparation to move libcontainer/cgroups to a separate repo, we have to get rid of libcontainer/utils dependency. So, - copy the utils.CleanPath implementation to internal/path; - consolidate the two innerPath implementations to internal/path. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 271aa88 commit 5e1dcdf

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

libcontainer/cgroups/fs/paths.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"golang.org/x/sys/unix"
1010

1111
"github.com/opencontainers/runc/libcontainer/cgroups"
12-
"github.com/opencontainers/runc/libcontainer/utils"
12+
"github.com/opencontainers/runc/libcontainer/cgroups/internal/path"
1313
)
1414

1515
// The absolute path to the root of the cgroup hierarchies.
@@ -26,7 +26,7 @@ func initPaths(cg *cgroups.Cgroup) (map[string]string, error) {
2626
return nil, err
2727
}
2828

29-
inner, err := innerPath(cg)
29+
inner, err := path.Inner(cg)
3030
if err != nil {
3131
return nil, err
3232
}
@@ -135,22 +135,6 @@ func rootPath() (string, error) {
135135
return cgroupRoot, nil
136136
}
137137

138-
func innerPath(c *cgroups.Cgroup) (string, error) {
139-
if (c.Name != "" || c.Parent != "") && c.Path != "" {
140-
return "", errors.New("cgroup: either Path or Name and Parent should be used")
141-
}
142-
143-
// XXX: Do not remove CleanPath. Path safety is important! -- cyphar
144-
innerPath := utils.CleanPath(c.Path)
145-
if innerPath == "" {
146-
cgParent := utils.CleanPath(c.Parent)
147-
cgName := utils.CleanPath(c.Name)
148-
innerPath = filepath.Join(cgParent, cgName)
149-
}
150-
151-
return innerPath, nil
152-
}
153-
154138
func subsysPath(root, inner, subsystem string) (string, error) {
155139
// If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
156140
if filepath.IsAbs(inner) {

libcontainer/cgroups/fs/paths_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/opencontainers/runc/libcontainer/cgroups"
9+
"github.com/opencontainers/runc/libcontainer/cgroups/internal/path"
910
)
1011

1112
func TestInvalidCgroupPath(t *testing.T) {
@@ -66,7 +67,7 @@ func TestInvalidCgroupPath(t *testing.T) {
6667
t.Run(tc.test, func(t *testing.T) {
6768
config := &cgroups.Cgroup{Path: tc.path, Name: tc.name, Parent: tc.parent}
6869

69-
inner, err := innerPath(config)
70+
inner, err := path.Inner(config)
7071
if err != nil {
7172
t.Fatalf("couldn't get cgroup data: %v", err)
7273
}

libcontainer/cgroups/fs2/defaultpath.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,15 @@ import (
2525
"strings"
2626

2727
"github.com/opencontainers/runc/libcontainer/cgroups"
28-
"github.com/opencontainers/runc/libcontainer/utils"
28+
"github.com/opencontainers/runc/libcontainer/cgroups/internal/path"
2929
)
3030

3131
const UnifiedMountpoint = "/sys/fs/cgroup"
3232

3333
func defaultDirPath(c *cgroups.Cgroup) (string, error) {
34-
if (c.Name != "" || c.Parent != "") && c.Path != "" {
35-
return "", errors.New("cgroup: either Path or Name and Parent should be used")
36-
}
37-
38-
// XXX: Do not remove CleanPath. Path safety is important! -- cyphar
39-
innerPath := utils.CleanPath(c.Path)
40-
if innerPath == "" {
41-
cgParent := utils.CleanPath(c.Parent)
42-
cgName := utils.CleanPath(c.Name)
43-
innerPath = filepath.Join(cgParent, cgName)
34+
innerPath, err := path.Inner(c)
35+
if err != nil {
36+
return "", err
4437
}
4538

4639
if filepath.IsAbs(innerPath) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package path
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/opencontainers/runc/libcontainer/cgroups"
9+
)
10+
11+
// Inner returns a path to cgroup relative to a cgroup mount point, based
12+
// on cgroup configuration, or an error, if cgroup configuration is invalid.
13+
// To be used only by fs cgroup managers (systemd has different path rules).
14+
func Inner(c *cgroups.Cgroup) (string, error) {
15+
if (c.Name != "" || c.Parent != "") && c.Path != "" {
16+
return "", errors.New("cgroup: either Path or Name and Parent should be used")
17+
}
18+
19+
// XXX: Do not remove cleanPath. Path safety is important! -- cyphar
20+
innerPath := cleanPath(c.Path)
21+
if innerPath == "" {
22+
cgParent := cleanPath(c.Parent)
23+
cgName := cleanPath(c.Name)
24+
innerPath = filepath.Join(cgParent, cgName)
25+
}
26+
27+
return innerPath, nil
28+
}
29+
30+
// cleanPath is a copy of github.com/opencontainers/runc/libcontainer/utils.CleanPath.
31+
func cleanPath(path string) string {
32+
// Deal with empty strings nicely.
33+
if path == "" {
34+
return ""
35+
}
36+
37+
// Ensure that all paths are cleaned (especially problematic ones like
38+
// "/../../../../../" which can cause lots of issues).
39+
40+
if filepath.IsAbs(path) {
41+
return filepath.Clean(path)
42+
}
43+
44+
// If the path isn't absolute, we need to do more processing to fix paths
45+
// such as "../../../../<etc>/some/path". We also shouldn't convert absolute
46+
// paths to relative ones.
47+
path = filepath.Clean(string(os.PathSeparator) + path)
48+
// This can't fail, as (by definition) all paths are relative to root.
49+
path, _ = filepath.Rel(string(os.PathSeparator), path)
50+
51+
return path
52+
}

0 commit comments

Comments
 (0)