Skip to content

fix(scheduler): deny privileged containers in webhook - #2141

Closed
aryansri05 wants to merge 1 commit into
Project-HAMi:masterfrom
aryansri05:fix/deny-privileged-webhook
Closed

fix(scheduler): deny privileged containers in webhook#2141
aryansri05 wants to merge 1 commit into
Project-HAMi:masterfrom
aryansri05:fix/deny-privileged-webhook

Conversation

@aryansri05

@aryansri05 aryansri05 commented Jul 27, 2026

Copy link
Copy Markdown

What changed

  • Return a denied admission response as soon as the webhook sees a privileged container.
  • Add a regression test for a pod that combines a privileged sidecar with a GPU-requesting workload container.

Why

The webhook previously logged that it was denying privileged containers but then continued processing the pod. If another container requested GPU resources, the webhook could still allow and mutate the pod.

Fixes #2138.

Validation

  • go test ./pkg/scheduler

Summary by CodeRabbit

  • Bug Fixes

    • Pod admission is now denied when any container is configured as privileged.
    • Prevents privileged containers from bypassing admission checks while other containers continue processing.
  • Tests

    • Added coverage verifying that pods containing privileged containers are rejected.

@hami-robot

hami-robot Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: aryansri05
Once this PR has been reviewed and has the lgtm label, please assign shouren for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The webhook now immediately denies pods containing privileged containers. A unit test covers a pod combining a privileged container with a GPU-requesting container and verifies that admission is rejected.

Changes

Privileged admission enforcement

Layer / File(s) Summary
Deny privileged containers and verify admission response
pkg/scheduler/webhook.go, pkg/scheduler/webhook_test.go
The webhook returns a denial when any container is privileged, and a test confirms mixed privileged/GPU pod admission is rejected.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: wawa0210

Poem

A bunny guards the webhook gate,
No privileged pods may pass their fate.
GPU companions, side by side,
Now meet a firm and quick deny.
Tests hop happily: policy’s right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: denying privileged containers in the scheduler webhook.
Linked Issues check ✅ Passed The change matches issue #2138 by denying pods with privileged containers, including mixed privileged/GPU pods, and adds a regression test.
Out of Scope Changes check ✅ Passed The PR stays focused on the webhook denial fix and corresponding test, with no evident unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the kind/bug Something isn't working label Jul 27, 2026
@hami-robot hami-robot Bot added the size/M label Jul 27, 2026
@aryansri05
aryansri05 force-pushed the fix/deny-privileged-webhook branch from 0ca177f to 04e33b8 Compare July 27, 2026 05:40
Signed-off-by: Aryan Srivastava <aryansriva05@gmail.com>
@aryansri05
aryansri05 force-pushed the fix/deny-privileged-webhook branch from 04e33b8 to 29368dc Compare July 27, 2026 12:29
@aryansri05 aryansri05 changed the title fix webhook privileged container admission fix(scheduler): deny privileged containers in webhook Jul 27, 2026
@aryansri05
aryansri05 marked this pull request as ready for review July 27, 2026 14:07

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/scheduler/webhook.go`:
- Around line 74-77: The privileged-container validation in
pkg/scheduler/webhook.go around the existing SecurityContext check must run
before the preassigned SchedulerName allow path, and must inspect Containers,
InitContainers, and EphemeralContainers. Update the relevant admission logic
while preserving denial for any privileged container, then add regression
coverage in pkg/scheduler/webhook_test.go around the existing scheduler
admission tests for privileged init containers, privileged ephemeral containers,
and privileged pods using a non-default scheduler.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fde7ee19-d1a6-4141-842c-570ff4278de4

📥 Commits

Reviewing files that changed from the base of the PR and between c343242 and 29368dc.

📒 Files selected for processing (2)
  • pkg/scheduler/webhook.go
  • pkg/scheduler/webhook_test.go

Comment thread pkg/scheduler/webhook.go
Comment on lines 74 to +77
if ctr.SecurityContext != nil {
if ctr.SecurityContext.Privileged != nil && *ctr.SecurityContext.Privileged {
klog.Warningf(template+" - Denying admission as container %s is privileged", pod.Namespace, pod.Name, pod.UID, c.Name)
continue
return admission.Denied("privileged containers are not allowed")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== files =="
git ls-files | rg '(^|/)pkg/scheduler/(webhook\.go|webhook_test\.go)$|hack/.import-aliases' || true

echo "== webhook outline =="
ast-grep outline pkg/scheduler/webhook.go || true

echo "== relevant webhook.go =="
sed -n '1,130p' pkg/scheduler/webhook.go

echo "== relevant webhook_test.go =="
sed -n '530,655p' pkg/scheduler/webhook_test.go

echo "== privilege references =="
rg -n -C 3 "privilege|Privileged|InitContainers|EphemeralContainers|SchedulerName|Allowed|Denied" pkg/scheduler/webhook.go pkg/scheduler/webhook_test.go

Repository: Project-HAMi/HAMi

Length of output: 26556


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== inspect init-container tests around earlier scheduler-different tests =="
sed -n '160,245p' pkg/scheduler/webhook_test.go

echo "== behavioral source probe: does Handle return Allowed before privileged check under preassigned scheduler? =="
python3 - <<'PY'
from pathlib import Path
text = Path('pkg/scheduler/webhook.go').read_text()
checks = {
    'scheduler_allow_before_privileged_loop': text.index('return admission.Allowed("pod already has different scheduler assigned")') < text.index('// Denying admission as container %s is privileged'),
    'only_one_privileged_loop_in_containers': text.count('for idx, ctr := range pod.Spec.Containers') == 1,
    'contains_initcontainers_loop': 'for idx, ctr := range pod.Spec.InitContainers' in text,
    'contains_ephemeralcontainers_loop': 'for idx, ctr := range pod.Spec.EphemeralContainers' in text,
}
for k, v in checks.items():
    print(f'{k}={v}')
print('privileged check appears only inside first Containers loop:', 'Privileged' in text)
print('allowed response in Handle:', 'admission.Allowed' in text)
PY

Repository: Project-HAMi/HAMi

Length of output: 2573


Check privileged containers before allowing preassigned-scheduler pods and cover init/ephemeral containers.

A pod with SchedulerName set to a non-default scheduler returns admission.Allowed before reaching the privileged container deny path, and the validation only scans Containers. Move the privileged check ahead of the scheduler-name allow path and include InitContainers and EphemeralContainers; add regression tests for privileged init/ephemeral containers and a privileged pod with a different preassigned scheduler.

🧰 Tools
🪛 ast-grep (0.44.1)

[warning] 75-75: A log/format call (log.Print/Printf/Println, the Fatal/Panic variants, fmt.Sprintf, or a structured logger's Info/Warn/Error/Debug method) is given a message built by concatenating a string literal with a non-literal value such as request data. Unsanitized, attacker-controlled input written to logs enables log forging / CRLF injection: an attacker can inject newlines to spoof log entries or break log parsers. Do not concatenate raw input into the log message; pass it as a separate structured field/argument (e.g. 'log.Printf("user: %s", user)' or 'logger.Info("login", "user", user)') and strip or escape newline characters first.
Context: klog.Warningf(template+" - Denying admission as container %s is privileged", pod.Namespace, pod.Name, pod.UID, c.Name)
Note: [CWE-117] Improper Output Neutralization for Logs.

(log-injection-request-data-concat-go)

📍 Affects 2 files
  • pkg/scheduler/webhook.go#L74-L77 (this comment)
  • pkg/scheduler/webhook_test.go#L575-L630
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/scheduler/webhook.go` around lines 74 - 77, The privileged-container
validation in pkg/scheduler/webhook.go around the existing SecurityContext check
must run before the preassigned SchedulerName allow path, and must inspect
Containers, InitContainers, and EphemeralContainers. Update the relevant
admission logic while preserving denial for any privileged container, then add
regression coverage in pkg/scheduler/webhook_test.go around the existing
scheduler admission tests for privileged init containers, privileged ephemeral
containers, and privileged pods using a non-default scheduler.

@archlitchi

Copy link
Copy Markdown
Member

Fixed in #2139

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Mutating webhook allows pods with privileged containers when other containers request GPU resources

2 participants