@@ -24,13 +24,13 @@ import (
2424 "strings"
2525)
2626
27- // GetDeviceCGroupMountPath returns the mount path for the device cgroup controller associated with pid
28- func (c * cgroupv1 ) GetDeviceCGroupMountPath (procRootPath string , pid int ) (string , error ) {
27+ // GetDeviceCGroupMountPath returns the mount path (and its prefix) for the device cgroup controller associated with pid
28+ func (c * cgroupv1 ) GetDeviceCGroupMountPath (procRootPath string , pid int ) (string , string , error ) {
2929 // Open the pid's mountinfo file in /proc.
3030 path := fmt .Sprintf (filepath .Join (procRootPath , "proc" , "%v" , "mountinfo" ), pid )
3131 file , err := os .Open (path )
3232 if err != nil {
33- return "" , err
33+ return "" , "" , err
3434 }
3535 defer file .Close ()
3636
@@ -43,7 +43,7 @@ func (c *cgroupv1) GetDeviceCGroupMountPath(procRootPath string, pid int) (strin
4343 // Split each entry by '[space]'
4444 parts := strings .Split (scanner .Text (), " " )
4545 if len (parts ) < 5 {
46- return "" , fmt .Errorf ("malformed mountinfo entry: %v" , scanner .Text ())
46+ return "" , "" , fmt .Errorf ("malformed mountinfo entry: %v" , scanner .Text ())
4747 }
4848 // Look for an entry with cgroup as the mount type.
4949 if parts [len (parts )- 3 ] != "cgroup" {
@@ -53,15 +53,21 @@ func (c *cgroupv1) GetDeviceCGroupMountPath(procRootPath string, pid int) (strin
5353 if filepath .Base (parts [4 ]) != "devices" {
5454 continue
5555 }
56- // Return the 4th element as the mount point of the devices cgroup.
57- return parts [4 ], nil
56+ // Make sure the mount prefix is not a relative path.
57+ if strings .HasPrefix (parts [3 ], "/.." ) {
58+ return "" , "" , fmt .Errorf ("relative path in mount prefix: %v" , parts [3 ])
59+ }
60+ // Return the 3rd element as the prefix of the mount point for
61+ // the devices cgroup and the 4th element as the mount point of
62+ // the devices cgroup itself.
63+ return parts [3 ], parts [4 ], nil
5864 }
5965
60- return "" , fmt .Errorf ("no cgroup filesystem mounted for the devices subsytem in mountinfo file" )
66+ return "" , "" , fmt .Errorf ("no cgroup filesystem mounted for the devices subsytem in mountinfo file" )
6167}
6268
63- // GetDeviceCGroupMountPath returns the root path for the device cgroup controller associated with pid
64- func (c * cgroupv1 ) GetDeviceCGroupRootPath (procRootPath string , pid int ) (string , error ) {
69+ // GetDeviceCGroupRootPath returns the root path for the device cgroup controller associated with pid
70+ func (c * cgroupv1 ) GetDeviceCGroupRootPath (procRootPath string , prefix string , pid int ) (string , error ) {
6571 // Open the pid's cgroup file in /proc.
6672 path := fmt .Sprintf (filepath .Join (procRootPath , "proc" , "%v" , "cgroup" ), pid )
6773 file , err := os .Open (path )
@@ -81,12 +87,16 @@ func (c *cgroupv1) GetDeviceCGroupRootPath(procRootPath string, pid int) (string
8187 if len (parts ) != 3 {
8288 return "" , fmt .Errorf ("malformed cgroup entry: %v" , scanner .Text ())
8389 }
84- // Look for the devices subsystem in the 2st element.
90+ // Look for the devices subsystem in the 1st element.
8591 if parts [1 ] != "devices" {
8692 continue
8793 }
88- // Return the cgroup root from the 2nd element.
89- return parts [2 ], nil
94+ // Return the cgroup root from the 2nd element
95+ // (with the prefix possibly stripped off).
96+ if prefix == "/" {
97+ return parts [2 ], nil
98+ }
99+ return strings .TrimPrefix (parts [2 ], prefix ), nil
90100 }
91101
92102 return "" , fmt .Errorf ("no devices cgroup entries found" )
@@ -96,17 +106,17 @@ func (c *cgroupv1) GetDeviceCGroupRootPath(procRootPath string, pid int) (string
96106func (c * cgroupv1 ) AddDeviceRules (cgroupPath string , rules []DeviceRule ) error {
97107 // Loop through all rules in the set of device rules and add that rule to the device.
98108 for _ , rule := range rules {
99- err := c .addDeviceRule (cgroupPath , & rule )
100- if err != nil {
101- return err
102- }
109+ err := c .addDeviceRule (cgroupPath , & rule )
110+ if err != nil {
111+ return err
112+ }
103113 }
104114
105115 return nil
106116}
107117
108118func (c * cgroupv1 ) addDeviceRule (cgroupPath string , rule * DeviceRule ) error {
109- // Check the major/minor numbers of the device in the device rule.
119+ // Check the major/minor numbers of the device in the device rule.
110120 if rule .Major == nil {
111121 return fmt .Errorf ("no major set in device rule" )
112122 }
@@ -126,7 +136,7 @@ func (c *cgroupv1) addDeviceRule(cgroupPath string, rule *DeviceRule) error {
126136 if err != nil {
127137 return err
128138 }
129- defer file .Close ()
139+ defer file .Close ()
130140
131141 // Write the device rule into the file.
132142 _ , err = file .WriteString (fmt .Sprintf ("%s %d:%d %s" , rule .Type , * rule .Major , * rule .Minor , rule .Access ))
0 commit comments