1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

oci.Device() fix FileMode to match runtime spec

The runtime spec expects the FileMode field to only hold file permissions,
however `unix.Stat_t.Mode` contains both file type and mode.

This patch strips file type so that only file mode is included in the Device.

Thanks to Iceber Gu, who noticed the same issue in containerd and runc.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-02-17 14:55:50 +01:00
parent 546c33cede
commit 1cd1925acd
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 33 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices" "github.com/opencontainers/runc/libcontainer/devices"
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
) )
// Device transforms a libcontainer configs.Device to a specs.LinuxDevice object. // Device transforms a libcontainer configs.Device to a specs.LinuxDevice object.
@ -18,7 +19,7 @@ func Device(d *configs.Device) specs.LinuxDevice {
Path: d.Path, Path: d.Path,
Major: d.Major, Major: d.Major,
Minor: d.Minor, Minor: d.Minor,
FileMode: fmPtr(int64(d.FileMode)), FileMode: fmPtr(int64(d.FileMode &^ unix.S_IFMT)), // strip file type, as OCI spec only expects file-mode to be included
UID: u32Ptr(int64(d.Uid)), UID: u32Ptr(int64(d.Uid)),
GID: u32Ptr(int64(d.Gid)), GID: u32Ptr(int64(d.Gid)),
} }

31
oci/devices_linux_test.go Normal file
View file

@ -0,0 +1,31 @@
package oci
import (
"os"
"testing"
"github.com/opencontainers/runc/libcontainer/configs"
"golang.org/x/sys/unix"
"gotest.tools/v3/assert"
)
func TestDeviceMode(t *testing.T) {
tests := []struct {
name string
in os.FileMode
out os.FileMode
}{
{name: "regular permissions", in: 0777, out: 0777},
{name: "block device", in: 0777 | unix.S_IFBLK, out: 0777},
{name: "character device", in: 0777 | unix.S_IFCHR, out: 0777},
{name: "fifo device", in: 0777 | unix.S_IFIFO, out: 0777},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
d := Device(&configs.Device{FileMode: tc.in})
assert.Equal(t, *d.FileMode, tc.out)
})
}
}