Skip to content

Commit

Permalink
pkg/specgen: Don't crash for device spec with...
Browse files Browse the repository at this point in the history
...empty destination path

This fixes a server-side crash for command lines like:

  # podman run -ti --rm --device /dev/mem::rw alpine sh

Fixes containers#19335.

Signed-off-by: Doug Rabson <dfr@rabson.org>
  • Loading branch information
dfr committed Jul 24, 2023
1 parent 03ea93c commit eee2817
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/specgen/generate/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func ParseDevice(device string) (string, string, string, error) {
if IsValidDeviceMode(arr[1]) {
permissions = arr[1]
} else {
if arr[1][0] != '/' {
if len(arr[1]) > 0 && arr[1][0] != '/' {
return "", "", "", fmt.Errorf("invalid device mode: %s", arr[1])
}
dst = arr[1]
Expand Down
22 changes: 22 additions & 0 deletions pkg/specgen/generate/config_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,25 @@ func TestShouldMask(t *testing.T) {
assert.Equal(t, val, test.shouldMask)
}
}

func TestParseDevice(t *testing.T) {
tests := []struct {
device string
src string
dst string
perm string
}{
{"/dev/foo", "/dev/foo", "/dev/foo", "rwm"},
{"/dev/foo:/dev/bar", "/dev/foo", "/dev/bar", "rwm"},
{"/dev/foo:/dev/bar:rw", "/dev/foo", "/dev/bar", "rw"},
{"/dev/foo:rw", "/dev/foo", "/dev/foo", "rw"},
{"/dev/foo::rw", "/dev/foo", "/dev/foo", "rw"},
}
for _, test := range tests {
src, dst, perm, err := ParseDevice(test.device)
assert.NoError(t, err)
assert.Equal(t, src, test.src)
assert.Equal(t, dst, test.dst)
assert.Equal(t, perm, test.perm)
}
}

0 comments on commit eee2817

Please sign in to comment.