cgroups/fs2/CreateCgroupPath: reinstate check

This check was removed in commit 5406833a65. Now, when this
function is called from a few places, it is no longer obvious
that the path always starts with /sys/fs/cgroup/, so reinstate
the check just to be on the safe side.

This check also ensures that elements[3:] can be used safely.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2020-04-07 12:43:36 -07:00
parent 813cb3eb94
commit b5c1949f2a
+5
View File
@@ -2,6 +2,7 @@ package fs2
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -11,6 +12,10 @@ import (
// CreateCgroupPath creates cgroupv2 path, enabling all the
// available controllers in the process.
func CreateCgroupPath(path string) (Err error) {
if !strings.HasPrefix(path, UnifiedMountpoint) {
return fmt.Errorf("invalid cgroup path %s", path)
}
const file = UnifiedMountpoint + "/cgroup.controllers"
content, err := ioutil.ReadFile(file)
if err != nil {