Skip to content

Ensure that filemode is set for device nodes #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions pkg/cdi/container-edits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cdi

import (
"os"
"runtime"
"testing"

Expand Down Expand Up @@ -306,6 +307,9 @@ func TestValidateContainerEdits(t *testing.T) {
func TestApplyContainerEdits(t *testing.T) {
nullDeviceMajor := int64(1)
nullDeviceMinor := int64(3)

mode := uint32(0666)
nullDeviceFileMode := (*os.FileMode)(&mode)
if runtime.GOOS == "darwin" {
nullDeviceMajor = 3
nullDeviceMinor = 2
Expand Down Expand Up @@ -352,10 +356,11 @@ func TestApplyContainerEdits(t *testing.T) {
Linux: &oci.Linux{
Devices: []oci.LinuxDevice{
{
Path: "/dev/null",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
Path: "/dev/null",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
FileMode: nullDeviceFileMode,
},
},
Resources: &oci.LinuxResources{
Expand Down Expand Up @@ -395,10 +400,11 @@ func TestApplyContainerEdits(t *testing.T) {
Linux: &oci.Linux{
Devices: []oci.LinuxDevice{
{
Path: "/dev/null",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
Path: "/dev/null",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
FileMode: nullDeviceFileMode,
},
},
Resources: &oci.LinuxResources{
Expand Down
52 changes: 41 additions & 11 deletions pkg/cdi/container-edits_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package cdi
import (
"errors"
"fmt"
"os"

"golang.org/x/sys/unix"
)
Expand All @@ -31,16 +32,28 @@ const (
fifoDevice = "p"
)

type deviceInfo struct {
// cgroup properties
deviceType string
major int64
minor int64

// device node properties
fileMode os.FileMode
}

// deviceInfoFromPath takes the path to a device and returns its type,
// major and minor device numbers.
//
// It was adapted from https://github.com/opencontainers/runc/blob/v1.1.9/libcontainer/devices/device_unix.go#L30-L69
func deviceInfoFromPath(path string) (devType string, major, minor int64, _ error) {
func deviceInfoFromPath(path string) (*deviceInfo, error) {
var stat unix.Stat_t
err := unix.Lstat(path, &stat)
if err != nil {
return "", 0, 0, err
return nil, err
}

var devType string
switch stat.Mode & unix.S_IFMT {
case unix.S_IFBLK:
devType = blockDevice
Expand All @@ -49,10 +62,18 @@ func deviceInfoFromPath(path string) (devType string, major, minor int64, _ erro
case unix.S_IFIFO:
devType = fifoDevice
default:
return "", 0, 0, errors.New("not a device node")
return nil, errors.New("not a device node")
}
devNumber := uint64(stat.Rdev) //nolint:unconvert // Rdev is uint32 on e.g. MIPS.
return devType, int64(unix.Major(devNumber)), int64(unix.Minor(devNumber)), nil

di := deviceInfo{
deviceType: devType,
major: int64(unix.Major(devNumber)),
minor: int64(unix.Minor(devNumber)),
fileMode: os.FileMode(stat.Mode &^ unix.S_IFMT),
}

return &di, nil
}

// fillMissingInfo fills in missing mandatory attributes from the host device.
Expand All @@ -65,22 +86,31 @@ func (d *DeviceNode) fillMissingInfo() error {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also set d.FileMode here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you set it to in this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is an early return meaning that we don't query the device node. I can make things a bit more consistent though. Let me see what I can come up with.

}

deviceType, major, minor, err := deviceInfoFromPath(d.HostPath)
di, err := deviceInfoFromPath(d.HostPath)
if err != nil {
return fmt.Errorf("failed to stat CDI host device %q: %w", d.HostPath, err)
}

if d.Type == "" {
d.Type = deviceType
d.Type = di.deviceType
} else {
if d.Type != deviceType {
if d.Type != di.deviceType {
return fmt.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)",
d.Path, d.HostPath, d.Type, deviceType)
d.Path, d.HostPath, d.Type, di.deviceType)
}
}
if d.Major == 0 && d.Type != "p" {
d.Major = major
d.Minor = minor

if d.FileMode == nil {
d.FileMode = &di.fileMode
}

if d.Type == "p" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if d.Type == "p" {
if d.Type == fifoDevice {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use "p" elsewhere in this function. I don't mind changing this to a constant, but would do so consistently across this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please do.

return nil
}

if d.Major == 0 {
d.Major = di.major
d.Minor = di.minor
}

return nil
Expand Down