Skip to content

Fix nil pointer dereference in container spec memory metrics#12492

Merged
fuweid merged 1 commit intocontainerd:mainfrom
ymichaelson:fix-memory-metrics-nil-check
Nov 7, 2025
Merged

Fix nil pointer dereference in container spec memory metrics#12492
fuweid merged 1 commit intocontainerd:mainfrom
ymichaelson:fix-memory-metrics-nil-check

Conversation

@ymichaelson
Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

This PR fixes a potential nil pointer dereference panic in extractContainerSpecMetrics when collecting container resource metrics.

The issue occurs when memory resource fields (Limit, Reservation, Swap) are nil, which can happen when:

  • Container doesn't have memory limits set
  • Only some memory constraints are configured
  • Reservation is not set (it's only set via NRI plugins)

Which issue(s) this PR fixes:

Fixes potential panic when collecting metrics from containers without full memory limit configuration.

Special notes for your reviewer:

This fix makes the memory resource handling consistent with:

  1. CPU resource handling in the same function (lines 993-1012)
  2. Memory resource handling in helpers.go (lines 296-303)

Each pointer field now has an independent nil check before dereferencing.

Does this PR introduce a user-facing change?

Fix potential panic in ListPodSandboxMetrics when containers don't have all memory limits configured

@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Nov 6, 2025
@dosubot dosubot Bot added the area/cri Container Runtime Interface (CRI) label Nov 6, 2025
Add nil checks for Memory.Limit, Memory.Reservation, and Memory.Swap
before dereferencing them in extractContainerSpecMetrics. These fields
are optional pointers in OCI runtime spec and can be nil when not set.

- Memory.Limit: Set from Pod resources.limits.memory
- Memory.Reservation: Only set via NRI plugins (rarely used)
- Memory.Swap: Set from memory_swap_limit_in_bytes (optional)

This prevents potential panic when collecting metrics from containers
that don't have all memory limits configured, and makes the code
consistent with CPU resource handling and helpers.go.

Signed-off-by: You Binhao <[email protected]>
@ymichaelson ymichaelson force-pushed the fix-memory-metrics-nil-check branch from 19ed9a1 to 6b82f03 Compare November 6, 2025 12:07
@github-project-automation github-project-automation Bot moved this from Needs Triage to Review In Progress in Pull Request Review Nov 7, 2025
@fuweid fuweid added this pull request to the merge queue Nov 7, 2025
Merged via the queue into containerd:main with commit ba0a05a Nov 7, 2025
52 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Nov 7, 2025
ChengyuZhu6 pushed a commit to ChengyuZhu6/containerd that referenced this pull request Nov 14, 2025
…cs-nil-check

Fix nil pointer dereference in container spec memory metrics
@akhilerm
Copy link
Copy Markdown
Member

/cherrypick release/2.2

@akhilerm akhilerm added the cherry-picked/2.2.x PR commits are cherry-picked into release/2.2 branch label Dec 27, 2025
@k8s-infra-cherrypick-robot
Copy link
Copy Markdown

@akhilerm: new pull request created: #12731

Details

In response to this:

/cherrypick release/2.2

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

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

Labels

area/cri Container Runtime Interface (CRI) cherry-picked/2.2.x PR commits are cherry-picked into release/2.2 branch kind/bug size/M

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants