Skip to content

Commit 83863a6

Browse files
committed
specgen: parse devices even with privileged set
When a users asks for specific devices we should still add them and not ignore them just because privileged adds all of them. Most notably if you set --device /dev/null:/dev/test you expect /dev/test in the container, however as we ignored them this was not the case. Another side effect is that the input was not validated at at all. This leads to confusion as descriped in the issue. Fixes #23132 Signed-off-by: Paul Holzinger <[email protected]>
1 parent 8650348 commit 83863a6

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

pkg/specgen/generate/oci_linux.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -254,24 +254,21 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt
254254
}
255255

256256
var userDevices []spec.LinuxDevice
257-
258-
if !s.IsPrivileged() {
259-
// add default devices from containers.conf
260-
for _, device := range rtc.Containers.Devices.Get() {
261-
if err = DevicesFromPath(&g, device); err != nil {
262-
return nil, err
263-
}
264-
}
265-
if len(compatibleOptions.HostDeviceList) > 0 && len(s.Devices) == 0 {
266-
userDevices = compatibleOptions.HostDeviceList
267-
} else {
268-
userDevices = s.Devices
257+
// add default devices from containers.conf
258+
for _, device := range rtc.Containers.Devices.Get() {
259+
if err = DevicesFromPath(&g, device); err != nil {
260+
return nil, err
269261
}
270-
// add default devices specified by caller
271-
for _, device := range userDevices {
272-
if err = DevicesFromPath(&g, device.Path); err != nil {
273-
return nil, err
274-
}
262+
}
263+
if len(compatibleOptions.HostDeviceList) > 0 && len(s.Devices) == 0 {
264+
userDevices = compatibleOptions.HostDeviceList
265+
} else {
266+
userDevices = s.Devices
267+
}
268+
// add default devices specified by caller
269+
for _, device := range userDevices {
270+
if err = DevicesFromPath(&g, device.Path); err != nil {
271+
return nil, err
275272
}
276273
}
277274
s.HostDeviceList = userDevices

pkg/util/utils_linux.go

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ func AddPrivilegedDevices(g *generate.Generator, systemdMode bool) error {
106106
if err != nil {
107107
return err
108108
}
109-
g.ClearLinuxDevices()
110109

111110
if rootless.IsRootless() {
112111
mounts := make(map[string]interface{})

test/e2e/run_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,19 @@ VOLUME %s`, ALPINE, volPath, volPath)
16871687
Expect(session).Should(ExitCleanly())
16881688
})
16891689

1690+
It("podman run --device and --privileged", func() {
1691+
session := podmanTest.Podman([]string{"run", "--device", "/dev/null:/dev/testdevice", "--privileged", ALPINE, "ls", "/dev"})
1692+
session.WaitWithDefaultTimeout()
1693+
Expect(session).Should(ExitCleanly())
1694+
Expect(session.OutputToString()).To(ContainSubstring(" testdevice "), "our custom device")
1695+
// assumes that /dev/mem always exists
1696+
Expect(session.OutputToString()).To(ContainSubstring(" mem "), "privileged device")
1697+
1698+
session = podmanTest.Podman([]string{"run", "--device", "invalid-device", "--privileged", ALPINE, "ls", "/dev"})
1699+
session.WaitWithDefaultTimeout()
1700+
Expect(session).Should(ExitWithError(125, "stat invalid-device: no such file or directory"))
1701+
})
1702+
16901703
It("podman run --replace", func() {
16911704
// Make sure we error out with --name.
16921705
session := podmanTest.Podman([]string{"create", "--replace", ALPINE, "/bin/sh"})

0 commit comments

Comments
 (0)