Skip to content

Restrict device plugin hostPath volumes to allowed prefixes#1292

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-sigs:mainfrom
TomerNewman:restrict-dp-mount
Jun 23, 2026
Merged

Restrict device plugin hostPath volumes to allowed prefixes#1292
kubernetes-prow[bot] merged 1 commit into
kubernetes-sigs:mainfrom
TomerNewman:restrict-dp-mount

Conversation

@TomerNewman

@TomerNewman TomerNewman commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add webhook validation that rejects hostPath volumes in spec.devicePlugin.volumes unless the path resolves under /dev, /sys, /var or /opt. This prevents device plugins from mounting arbitrary host directories such as / or /etc.

Details

  • New validateDevicePluginVolumes function in the webhook with filepath.Clean to prevent path traversal bypasses (e.g. /dev/../etc)
  • Called from validateModule on both create and update
  • Non-hostPath volumes (ConfigMap, Secret, EmptyDir, etc.) are allowed through since they don't expose host filesystem

Test plan

  • Unit tests added (13 cases covering nil, empty, allowed paths, rejected paths, prefix tricks, path traversal)
  • make fmt passes
  • make lint passes
  • make unit-test passes

/cc @ybettan @yevgeny-shnaidman
/assign @ybettan @yevgeny-shnaidman

@netlify

netlify Bot commented Jun 22, 2026

Copy link
Copy Markdown

Deploy Preview for kubernetes-sigs-kmm ready!

Name Link
🔨 Latest commit 93a10ba
🔍 Latest deploy log https://app.netlify.com/projects/kubernetes-sigs-kmm/deploys/6a3a21df934e100008885610
😎 Deploy Preview https://deploy-preview-1292--kubernetes-sigs-kmm.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kubernetes-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: TomerNewman

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 22, 2026
@codecov-commenter

codecov-commenter commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.57%. Comparing base (fa23a9b) to head (93a10ba).
⚠️ Report is 386 commits behind head on main.

Files with missing lines Patch % Lines
internal/webhook/module.go 88.88% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1292      +/-   ##
==========================================
- Coverage   79.09%   73.57%   -5.52%     
==========================================
  Files          51       67      +16     
  Lines        5109     5022      -87     
==========================================
- Hits         4041     3695     -346     
- Misses        882     1157     +275     
+ Partials      186      170      -16     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add webhook validation that rejects hostPath volumes in
spec.devicePlugin.volumes unless the path resolves under /dev, /sys,
/var or /opt. This prevents device plugins from mounting arbitrary
host directories such as / or /etc.
@yevgeny-shnaidman

Copy link
Copy Markdown
Contributor

please update the title and the commit message

@TomerNewman TomerNewman changed the title Restrict device plugin hostPath volumes to /dev and /sys Restrict device plugin hostPath volumes to allowed prefixes Jun 23, 2026
@TomerNewman

Copy link
Copy Markdown
Collaborator Author

please update the title and the commit message

Done

func isAllowedHostPath(hostPath string) bool {
p := filepath.Clean(hostPath)
for _, prefix := range allowedHostPathPrefixes {
if p == prefix || strings.HasPrefix(p, prefix+"/") {

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.

why do we need prefix + "/"? What happens if the volume is "/var"?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The check has two parts:

if cleanPath == prefix || strings.HasPrefix(cleanPath, prefix+"/") {
  • cleanPath == prefix handles the exact match — so /var by itself is allowed.
  • strings.HasPrefix(cleanPath, prefix+"/") handles subdirectories like /var/lib/kubelet.

The + "/" is specifically to prevent false positives: without it, strings.HasPrefix("/variable", "/var") would return true, incorrectly allowing a path like /variable. The trailing slash ensures we only match actual subdirectories.

@yevgeny-shnaidman

Copy link
Copy Markdown
Contributor

/lgtm

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 23, 2026
@kubernetes-prow kubernetes-prow Bot merged commit 29c905a into kubernetes-sigs:main Jun 23, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants