Skip to content

Commit 40742f8

Browse files
fix(mount): reintroduce root path requirement
SearchForHostMountpoint now strictly requires its results to be mounted on the "/" root folder again. This is to prevent selecting an already OS managed mount which is non root located (for example in TAS and EKS). Previous inode and path selection code is left to accomodate those changes.
1 parent 604c391 commit 40742f8

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

pkg/mount/mount.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"os"
66
"path/filepath"
7+
"slices"
78
"strings"
89
"syscall"
910

@@ -217,11 +218,11 @@ func IsFileSystemSupported(fsType string) (bool, error) {
217218
// - int: The inode number of the matching mountpoint.
218219
// - error: Any error encountered while reading the /proc/mounts file.
219220
func SearchMountpointFromHost(fstype string, search string) (string, int, error) {
221+
const mountRootIndex = 3
220222
const mountpointIndex = 4
221-
const fsTypeIndex = 8
222223

223-
mp := ""
224-
inode := 0
224+
mp := "" // matched mountpoint search var
225+
inode := 0 // matched mountpoint's inode
225226

226227
file, err := os.Open(procMounts)
227228
if err != nil {
@@ -236,15 +237,33 @@ func SearchMountpointFromHost(fstype string, search string) (string, int, error)
236237
scanner := bufio.NewScanner(file)
237238
for scanner.Scan() {
238239
line := strings.Split(scanner.Text(), " ")
239-
if len(line) <= fsTypeIndex {
240-
continue // Skip lines that do not have enough fields
241-
}
242-
243-
mountpoint := line[mountpointIndex]
244-
currFstype := line[fsTypeIndex]
245240

246-
// Check if the current line matches the desired filesystem type and contains the search string.
247-
if fstype == currFstype && strings.Contains(mountpoint, search) {
241+
// fstype field is located right after "-"
242+
// before - there are optional fields, which makes the location of
243+
// the fstype field indeterminate
244+
sepIndex := slices.Index(line, "-")
245+
fsTypeIndex := sepIndex + 1
246+
247+
root := line[mountRootIndex] // current search mountpoint root
248+
mountpoint := line[mountpointIndex] // current search mountpoint path
249+
currFstype := line[fsTypeIndex] // current search mountpoint fs type
250+
251+
// First check for the following 3 conditions:
252+
// 1. The fs type is the one we search for
253+
// 2. The mountpoint contains the path we are searching
254+
// 3. The root path in the mounted filesystem is that of the host.
255+
// This means, that the root of the mounted filesystem is /.
256+
// For example, if we are searching for a mountpoint with cpuset we want
257+
// to be sure that it is not actually <some_other_dir>/.../.../...cpuset,
258+
// but strictly originating in the root fs.
259+
// EXAMPLE: EKS and TAS mount their cgroup controllers ontop of their pod
260+
// cgroup folder root:
261+
// /kubepods.slice/.../cri-containerd-abcdef123.scope -> /sys/fs/cgroup/cpuset
262+
// /garden/fc6c9886-cd3d-4d87-5053-c102 -> /sys/fs/cgroup/cpuset
263+
// Without strictly requiring the root path the resulting search path for cgroup path results in searching:
264+
// /kubepods.slice/.../cri-containerd-abcdef123.scope/sys/fs/cgroup/cpuset/kubepods.slice/.../cri-containerd-somecontainerid123
265+
// which doesn't exist.
266+
if fstype == currFstype && strings.Contains(mountpoint, search) && root == "/" {
248267
// Try to get the inode number of the current mountpoint.
249268
var stat syscall.Stat_t
250269
if err := syscall.Stat(mountpoint, &stat); err != nil {
@@ -253,9 +272,12 @@ func SearchMountpointFromHost(fstype string, search string) (string, int, error)
253272
}
254273
currInode := int(stat.Ino)
255274

256-
// Update the result if this is the first match or if the current mountpoint is older or shorter in path length.
275+
// Update the result if either apply:
276+
// 1. this is the first match
277+
// 2. the current mountpoint inode is lower than the currently matching mountpoint
278+
// 2. the current mountpoint shares an inode but its root has a shorter path
257279
if inode == 0 || currInode < inode ||
258-
(currInode == inode && len(mountpoint) < len(mp)) {
280+
(currInode == inode && len(mp) < len(mountpoint)) {
259281
mp = mountpoint
260282
inode = currInode
261283
}

0 commit comments

Comments
 (0)