-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match VT device paths to be blocked from mounting exactly
As @mheon pointed out in PR #17055[^1], isVirtualConsoleDevice() does not only matches VT device paths but also devices named like /dev/tty0abcd. This causes that non VT device paths named /dev/tty[0-9]+[A-Za-z]+ are not mounted into privileged container and systemd containers accidentally. This is an unlikely issue because the Linux kernel does not use device paths like that. To make it failproof and prevent issues in unlikely scenarios, change isVirtualConsoleDevice() to exactly match ^/dev/tty[0-9]+$ paths. Because it is not possible to match this path exactly with Glob syntax, the path is now checked with strings.TrimPrefix() and strconv.ParseUint(). ParseUint uses a bitsize of 16, this is sufficient because the max number of TTY devices is 512 in Linux 6.1.5. (Checked via 'git grep -e '#define' --and -e 'TTY_MINORS'). The commit also adds a unit-test for isVirtualConsoleDevice(). Fixes: f4c81b0 ("Only prevent VTs to be mounted inside...") [^1]: #17055 (comment) Signed-off-by: Fabian Holler <mail@fholler.de>
- Loading branch information
Showing
2 changed files
with
77 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetImageConfigStopSignal(t *testing.T) { | ||
// Linux-only because parsing signal names is not supported on non-Linux systems by | ||
// pkg/signal. | ||
stopSignalValidInt, err := GetImageConfig([]string{"STOPSIGNAL 9"}) | ||
require.Nil(t, err) | ||
assert.Equal(t, stopSignalValidInt.StopSignal, "9") | ||
|
||
stopSignalValidString, err := GetImageConfig([]string{"STOPSIGNAL SIGKILL"}) | ||
require.Nil(t, err) | ||
assert.Equal(t, stopSignalValidString.StopSignal, "9") | ||
|
||
_, err = GetImageConfig([]string{"STOPSIGNAL 0"}) | ||
assert.NotNil(t, err) | ||
|
||
_, err = GetImageConfig([]string{"STOPSIGNAL garbage"}) | ||
assert.NotNil(t, err) | ||
|
||
_, err = GetImageConfig([]string{"STOPSIGNAL "}) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestIsVirtualConsoleDevice(t *testing.T) { | ||
testcases := []struct { | ||
expectedResult bool | ||
path string | ||
}{ | ||
{ | ||
expectedResult: true, | ||
path: "/dev/tty10", | ||
}, | ||
{ | ||
expectedResult: false, | ||
path: "/dev/tty", | ||
}, | ||
{ | ||
expectedResult: false, | ||
path: "/dev/ttyUSB0", | ||
}, | ||
|
||
{ | ||
expectedResult: false, | ||
path: "/dev/tty0abcd", | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.path, func(t *testing.T) { | ||
result := isVirtualConsoleDevice(tc.path) | ||
if result != tc.expectedResult { | ||
t.Errorf("isVirtualConsoleDevice returned %t, expected %t", result, tc.expectedResult) | ||
} | ||
}) | ||
} | ||
} |