Skip to content

Commit

Permalink
Match VT device paths to be blocked from mounting exactly
Browse files Browse the repository at this point in the history
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
fho committed Jan 13, 2023
1 parent 9311846 commit e6be66e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/util/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"

Expand Down Expand Up @@ -71,7 +71,7 @@ func FindDeviceNodes() (map[string]string, error) {
return nodes, nil
}

func isVirtualConsoleDevice(device string) bool {
func isVirtualConsoleDevice(path string) bool {
/*
Virtual consoles are of the form `/dev/tty\d+`, any other device such as
/dev/tty, ttyUSB0, or ttyACM0 should not be matched.
Expand All @@ -83,8 +83,14 @@ func isVirtualConsoleDevice(device string) bool {
increase the startup time needlessly or made the code more complex
than needed.
*/
matched, _ := path.Match("/dev/tty[0-9]*", device)
return matched
suffix := strings.TrimPrefix(path, "/dev/tty")
if suffix == path || suffix == "" {
return false
}

// 16bit because, max. supported TTY devices is 512 in Linux 6.1.5.
_, err := strconv.ParseUint(suffix, 10, 16)
return err == nil
}

func AddPrivilegedDevices(g *generate.Generator, systemdMode bool) error {
Expand Down
34 changes: 34 additions & 0 deletions pkg/util/utils_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,37 @@ func TestGetImageConfigStopSignal(t *testing.T) {
_, 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)
}
})
}
}

0 comments on commit e6be66e

Please sign in to comment.