Skip to content

Add support for extra device cgroup rules - #363

Open
marquiz wants to merge 4 commits into
cncf-tags:mainfrom
marquiz:devel/device-cgroup-rules
Open

marquiz wants to merge 4 commits into
cncf-tags:mainfrom
marquiz:devel/device-cgroup-rules

Conversation

@marquiz

@marquiz marquiz commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Adds a deviceCgroupRules field to containerEdits, for granting access to a set of devices in the devices cgroup without creating any device nodes with it. The main motivation is hotplug in device management and device monitoring scenarios, where the device numbers that appear on the host after the container was created cannot be known at container creation time. An omitted minor is a wildcard matching all device numbers. An example:

cdiVersion: "1.2.0"
kind: "vendor.com/device"
devices:
  - name: "all"
    containerEdits:
      deviceCgroupRules:
        - type: "c"
          major: 226
          permissions: "rw"
        - type: "b"
          major: 8
          minor: 0

Rules are applied as allow entries in linux.resources.devices of the OCI runtime spec.

Also bumps the spec version to v1.2.0.

@marquiz
marquiz requested review from bart0sh, kad and klihub September 8, 2026 11:35

@eero-t eero-t left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

An omitted major or minor is a wildcard matching all device numbers

May be safer to start by erroring in this case (and relaxing it only if somebody provides strong enough use-case for it, that cannot be handled reasonable in any other way).

Comment thread pkg/cdi/container-edits.go Outdated
@klihub

klihub commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

An omitted major or minor is a wildcard matching all device numbers

May be safer to start by erroring in this case (and relaxing it only if somebody provides strong enough use-case for it, that cannot be handled reasonable in any other way).

Well, I think we at least must allow omitting the minor number, otherwise this can't really be used for what is stated as the primary use case in the description: "The main motivation is hotplug in device management and device monitoring scenarios, where the device numbers that appear on the host after the container was created cannot be known at container creation time."

@marquiz But I was also wondering if we really need to allow such wide permissions, or if we could instead get along with only allowing the minor number to be omitted. Do we really have a use case for injecting access permissions to all block or character devices ?

@marquiz

marquiz commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

But I was also wondering if we really need to allow such wide permissions, or if we could instead get along with only allowing the minor number to be omitted. Do we really have a use case for injecting access permissions to all block or character devices ?

Yeah, agree. I think allowing "wildcard" on minor only is a sound choice (nobody should have the need to allow everything with one single rule). I'll change the PR

Add a DeviceCgroupRule type and a DeviceCgroupRules field to
ContainerEdits, for granting a container access to a set of devices in
the devices cgroup without creating any device node in it.

The main motivation is hotplug in device management and device
monitoring usage scenarios. Device numbers that appear on the host after
the container was created cannot be known at container creation time.

An empty (null) Major or Minor is a wildcard matching all device
numbers, which makes it possible to grant access to e.g. all DRM
devices, present and future.

Also, bump the spec version to v1.2.0.

Signed-off-by: Markus Lehtonen <markus.lehtonen@intel.com>
@marquiz
marquiz force-pushed the devel/device-cgroup-rules branch from 3a12805 to 7a9e6f5 Compare September 9, 2026 14:17
@marquiz

marquiz commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

But I was also wondering if we really need to allow such wide permissions, or if we could instead get along with only allowing the minor number to be omitted. Do we really have a use case for injecting access permissions to all block or character devices ?

Done. Major is now required (and must be > 0)

Comment thread pkg/cdi/container-edits.go Outdated
Comment thread pkg/cdi/container-edits.go
Comment thread SPEC.md
Comment on lines +259 to +265
* `deviceCgroupRules` (array of objects, OPTIONAL) describes rules to be added to the devices cgroup of the container, see [Device cgroup rules](#device-cgroup-rules). Added in v1.2.0.
* `type` (string, REQUIRED) type of the devices the rule applies to, one of:
* b - block device.
* c - character device.
* `major` (int64, REQUIRED) major number of the devices the rule applies to. Must be greater than 0.
* `minor` (int64, OPTIONAL) minor number of the devices the rule applies to. If not specified the rule applies to all minor numbers of the given major.
* `permissions` (string, OPTIONAL) Cgroups permissions to grant, with the same candidates as the `deviceNodes.permissions`. Omitted or empty default to `rwm`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@marquiz @elezar @bart0sh Should we explicitly spell out here, that an actual device injection in the Spec is always an implicit corresponding device cgroup rule injection. IOW, there is absolutely no need to manually specify device cgroup rules for devices you inject by a CDI Spec. You only need to specify device cgroup rules, if you do not inject the actual devices using a CDI Spec. This might not otherwise be fully obvious after this addition to the Spec, especially not for hasty readers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I think making it clear that on systems where cgroups are used, a device node implies a corresponding cgroup rule.

Add support for applying the device cgroup rules to the container spec.
Major and minor numbers are pointer fields so that unset values are
handled as an allow all wildcard. Omitted permissions default to "rwm".

Accept the block and character device types only, Note that the "a"
(allow every devices, akin to "privileged mode" is not allowed by this
patch.

Signed-off-by: Markus Lehtonen <markus.lehtonen@intel.com>
Signed-off-by: Markus Lehtonen <markus.lehtonen@intel.com>
Signed-off-by: Markus Lehtonen <markus.lehtonen@intel.com>
@marquiz
marquiz force-pushed the devel/device-cgroup-rules branch from 7a9e6f5 to c30f4af Compare September 14, 2026 09:38
@elezar

elezar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

I still need to do a proper review, but this seems to align with what I proposed in #314. My first question would be whether a separate containerEdits type is preferred over reusing "deviceNodes"? Using a specific type would help for clarity.

Comment thread specs-go/config.go
AdditionalGIDs []uint32 `json:"additionalGids,omitempty" yaml:"additionalGids,omitempty"` // Added in v0.7.0
Env []string `json:"env,omitempty" yaml:"env,omitempty"`
DeviceNodes []*DeviceNode `json:"deviceNodes,omitempty" yaml:"deviceNodes,omitempty"`
DeviceCgroupRules []*DeviceCgroupRule `json:"deviceCgroupRules,omitempty" yaml:"deviceCgroupRules,omitempty"` // Added in v1.2.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As far as I know these are Linux only? Should we name them as such? (we do so for the LinuxNetDevices).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, good point. "Linux" is probably is justified here. I'll change this

Comment thread specs-go/config.go
@klihub

klihub commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

I still need to do a proper review, but this seems to align with what I proposed in #314. My first question would be whether a separate containerEdits type is preferred over reusing "deviceNodes"? Using a specific type would help for clarity.

@elezar I think that is the major advantage of this PR's approach. It is rather explicit about what is happening so what users could expect. The wildcard approach is quite a bit more implicit. For instance, without (reading the) documentation some users might not get a clear idea whether any device nodes get injected. To some intuition might give the wrong impression that since its a globbing pattern, matching nodes will be injected. This approach does not suffer from that problem. It would also leave the door open for any future wildcard-based matching device injection if we'd ever to decide to consider adding something like that.

Comment thread SPEC.md
"type": "<type>",
"major": <int64>,
// An omitted or empty minor is a wildcard matching any minor number.
"minor": <int64> (optional),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a bit implicit. Would mandatory minor supporting wildcards be a safer option?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dunno 🤷‍♂️, what others think. The "wildcard" would be -1 then

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In order to collect data, how does Docker or Podman handle this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My rationale FWIW: If minor is unintentionally not added, all minors would be implicitly allowed and everything would work with wider permissions, allowing access to all the minors. This could cause security issues and after all this is not what user actually wanted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I understand. I suppose the issue is that communicating what -1 means may not be so straightforward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants