Skip to content

Commit a746c53

Browse files
authored
Merge pull request #4831 from marquiz/devel/rdt-root
libcontainer/intelrdt: refactor path handling
2 parents d2e86c0 + 3a96265 commit a746c53

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"sync"
1212

13+
securejoin "github.com/cyphar/filepath-securejoin"
1314
"github.com/moby/sys/mountinfo"
1415
"golang.org/x/sys/unix"
1516

@@ -159,10 +160,25 @@ func NewManager(config *configs.Config, id string, path string) *Manager {
159160
if config.IntelRdt == nil {
160161
return nil
161162
}
162-
if _, err := Root(); err != nil {
163-
// Intel RDT is not available.
163+
164+
rootPath, err := Root()
165+
if err != nil {
164166
return nil
165167
}
168+
// NOTE: Should we check if the path provided as arg matches the path
169+
// constructed below? If not, we're screwed as we've effectively lost resctrl
170+
// control of the container (e.g. because the resctrl fs was unmounted or
171+
// remounted elsewhere). All operations are deemed to fail.
172+
if path == "" {
173+
clos := id
174+
if config.IntelRdt.ClosID != "" {
175+
clos = config.IntelRdt.ClosID
176+
}
177+
if path, err = securejoin.SecureJoin(rootPath, clos); err != nil {
178+
return nil
179+
}
180+
}
181+
166182
return newManager(config, id, path)
167183
}
168184

@@ -434,32 +450,14 @@ func IsMBAEnabled() bool {
434450
return mbaEnabled
435451
}
436452

437-
// Get the path of the clos group in "resource control" filesystem that the container belongs to
438-
func (m *Manager) getIntelRdtPath() (string, error) {
439-
rootPath, err := Root()
440-
if err != nil {
441-
return "", err
442-
}
443-
444-
clos := m.id
445-
if m.config.IntelRdt != nil && m.config.IntelRdt.ClosID != "" {
446-
clos = m.config.IntelRdt.ClosID
447-
}
448-
449-
return filepath.Join(rootPath, clos), nil
450-
}
451-
452453
// Apply applies Intel RDT configuration to the process with the specified pid.
453454
func (m *Manager) Apply(pid int) (err error) {
454455
// If intelRdt is not specified in config, we do nothing
455456
if m.config.IntelRdt == nil {
456457
return nil
457458
}
458459

459-
path, err := m.getIntelRdtPath()
460-
if err != nil {
461-
return err
462-
}
460+
path := m.GetPath()
463461

464462
m.mu.Lock()
465463
defer m.mu.Unlock()
@@ -503,9 +501,6 @@ func (m *Manager) Destroy() error {
503501
// GetPath returns Intel RDT path to save in a state file and to be able to
504502
// restore the object later.
505503
func (m *Manager) GetPath() string {
506-
if m.path == "" {
507-
m.path, _ = m.getIntelRdtPath()
508-
}
509504
return m.path
510505
}
511506

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ func TestApply(t *testing.T) {
101101
helper := NewIntelRdtTestUtil(t)
102102

103103
const closID = "test-clos"
104+
closPath := filepath.Join(helper.IntelRdtPath, closID)
104105

105106
helper.config.IntelRdt.ClosID = closID
106-
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
107+
intelrdt := newManager(helper.config, "container-1", closPath)
107108
if err := intelrdt.Apply(1234); err == nil {
108109
t.Fatal("unexpected success when applying pid")
109110
}
110-
if _, err := os.Stat(filepath.Join(helper.IntelRdtPath, closID)); err == nil {
111+
if _, err := os.Stat(closPath); err == nil {
111112
t.Fatal("closid dir should not exist")
112113
}
113114

0 commit comments

Comments
 (0)