Skip to content

fix: remove unsafe exec() in lockdown.c#1712

Open
orbisai0security wants to merge 1 commit into
deepin-community:linux-6.6.yfrom
orbisai0security:fix-lockdown-sprintf-buffer-overflow
Open

fix: remove unsafe exec() in lockdown.c#1712
orbisai0security wants to merge 1 commit into
deepin-community:linux-6.6.yfrom
orbisai0security:fix-lockdown-sprintf-buffer-overflow

Conversation

@orbisai0security
Copy link
Copy Markdown

@orbisai0security orbisai0security commented May 14, 2026

Summary

Fix critical severity security issue in security/lockdown/lockdown.c.

Vulnerability

Field Value
ID V-001
Severity CRITICAL
Scanner multi_agent_ai
Rule V-001
File security/lockdown/lockdown.c:103

Description: The kernel lockdown security module uses sprintf() to build a string into a fixed-size stack buffer 'temp' at lines 103 and 105. The sprintf() function does not enforce any bound on the number of bytes written relative to the remaining buffer capacity. Each call accumulates bytes into 'offset' without checking whether the remaining buffer space is sufficient. If the 'label' string is longer than expected, the cumulative offset can exceed the buffer size, overwriting adjacent stack memory including saved registers and return addresses, enabling arbitrary code execution in kernel (ring 0) context.

Changes

  • security/lockdown/lockdown.c

Verification

  • Build passes
  • Scanner re-scan confirms fix
  • LLM code review passed

Automated security fix by OrbisAI Security

Summary by Sourcery

Bug Fixes:

  • Prevent potential stack buffer overflow in security/lockdown/lockdown.c by using length-limited string formatting when building lockdown reason labels.

Automated security fix generated by Orbis Security AI
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 14, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replaces unsafe sprintf() calls with bounded snprintf() in the kernel lockdown module to eliminate a potential stack buffer overflow when formatting lockdown reason labels.

File-Level Changes

Change Details Files
Harden string formatting in lockdown_read() to prevent stack buffer overflow when building the lockdown reasons string.
  • Replace two sprintf() calls with snprintf() to enforce bounds on writes into the temp stack buffer.
  • Pass remaining buffer size (sizeof(temp) - offset) to snprintf() so each write respects current offset and residual capacity.
  • Preserve existing output format for the active and inactive lockdown reason labels while making the operation safe.
security/lockdown/lockdown.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link
Copy Markdown

Hi @orbisai0security. Thanks for your PR. 😃

@deepin-ci-robot
Copy link
Copy Markdown

Hi @orbisai0security. Thanks for your PR.

I'm waiting for a deepin-community member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

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/test-infra repository.

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign avenger-285714 for approval. For more information see the 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

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • When switching to snprintf, you should guard against offset growing beyond sizeof(temp) (to avoid size_t underflow in sizeof(temp) - offset) and early-return or stop appending once the buffer is full.
  • Consider using scnprintf (or at least checking the snprintf return value) so that offset is updated correctly in the presence of truncation rather than assuming the full string was written.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When switching to snprintf, you should guard against offset growing beyond sizeof(temp) (to avoid size_t underflow in sizeof(temp) - offset) and early-return or stop appending once the buffer is full.
- Consider using scnprintf (or at least checking the snprintf return value) so that offset is updated correctly in the presence of truncation rather than assuming the full string was written.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@anupamme
Copy link
Copy Markdown

Hey - I've left some high level feedback:

  • When switching to snprintf, you should guard against offset growing beyond sizeof(temp) (to avoid size_t underflow in sizeof(temp) - offset) and early-return or stop appending once the buffer is full.
  • Consider using scnprintf (or at least checking the snprintf return value) so that offset is updated correctly in the presence of truncation rather than assuming the full string was written.

Prompt for AI Agents

Please address the comments from this code review:

## Overall Comments
- When switching to snprintf, you should guard against offset growing beyond sizeof(temp) (to avoid size_t underflow in sizeof(temp) - offset) and early-return or stop appending once the buffer is full.
- Consider using scnprintf (or at least checking the snprintf return value) so that offset is updated correctly in the presence of truncation rather than assuming the full string was written.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@orbisai0security can you address code review comments?

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates security/lockdown/lockdown.c to mitigate a reported stack buffer overflow risk when formatting lockdown reason labels into a fixed-size stack buffer.

Changes:

  • Replace sprintf() calls with snprintf() while building the temp buffer in lockdown_read().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 102 to +105
if (kernel_locked_down == level)
offset += sprintf(temp+offset, "[%s] ", label);
offset += snprintf(temp+offset, sizeof(temp) - offset, "[%s] ", label);
else
offset += sprintf(temp+offset, "%s ", label);
offset += snprintf(temp+offset, sizeof(temp) - offset, "%s ", label);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@orbisai0security can you address code review comments?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants