Skip to content

fix(acl): prevent NULL pointer dereference on malformed selector in ACL file#4214

Open
magic-peach wants to merge 1 commit into
valkey-io:unstablefrom
magic-peach:fix/acl-null-deref
Open

fix(acl): prevent NULL pointer dereference on malformed selector in ACL file#4214
magic-peach wants to merge 1 commit into
valkey-io:unstablefrom
magic-peach:fix/acl-null-deref

Conversation

@magic-peach

Copy link
Copy Markdown

Summary

  • Fix NULL pointer dereference when loading ACL file with unmatched parenthesis in selector definition

Bug

When ACLMergeSelectorArguments() returns NULL due to an unmatched parenthesis, the error message is set but execution continues to a for loop that dereferences acl_args (which is NULL), causing a server crash.

Fix

Add continue after the error message to skip the processing loop when acl_args is NULL.

…CL file

Signed-off-by: Akanksha Trehun <akankshatrehun@gmail.com>
@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

ACLLoadFromFile now frees per-line split arguments and skips malformed ACL selector entries when unmatched parentheses cause selector parsing to fail.

Changes

ACL loading

Layer / File(s) Summary
Selector error cleanup
src/acl.c
Unmatched-parenthesis errors now release argv and continue processing subsequent ACL lines.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: enjoy-binbin

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main fix for malformed ACL selectors causing a null dereference.
Description check ✅ Passed The description directly matches the code change and explains the crash scenario and fix.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

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

🧹 Nitpick comments (1)
src/acl.c (1)

2583-2584: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Add a regression test for malformed ACL files.

The cleanup and continue path is correct, but please add a tests/ Tcl integration test that loads an ACL file containing an unmatched selector parenthesis, verifies the expected error, confirms the server remains responsive, and checks that the previous ACL rules remain active.

As per coding guidelines: “Code changes should include relevant tests when the repository has a matching test location.”

🤖 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 `@src/acl.c` around lines 2583 - 2584, Add a Tcl integration regression test
under tests/ for loading an ACL file with an unmatched selector parenthesis.
Assert the expected load error, verify the server remains responsive afterward,
and confirm the previously loaded ACL rules are still enforced; keep the
existing cleanup and continue behavior unchanged.

Source: Coding guidelines

🤖 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.

Nitpick comments:
In `@src/acl.c`:
- Around line 2583-2584: Add a Tcl integration regression test under tests/ for
loading an ACL file with an unmatched selector parenthesis. Assert the expected
load error, verify the server remains responsive afterward, and confirm the
previously loaded ACL rules are still enforced; keep the existing cleanup and
continue behavior unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5b24794d-c8ac-45cb-806d-1caf51b56ccb

📥 Commits

Reviewing files that changed from the base of the PR and between 6b006c9 and f63d898.

📒 Files selected for processing (1)
  • src/acl.c

@valkey-review-bot valkey-review-bot 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.

The fix is correct: ACLMergeSelectorArguments frees the partial result but leaves *merged_argc at the partial count when it returns NULL (src/acl.c:2286-2291), so the old fall-through dereferenced the NULL acl_args whenever at least one complete argument preceded the unmatched parenthesis (e.g. user u on (+get). The added cleanup matches every sibling error path in this loop, and the created user is discarded with the fresh Users rax on the error exit, so there is no leak. Two minor notes inline.

Comment thread src/acl.c
@@ -2580,6 +2580,8 @@ static sds ACLLoadFromFile(const char *filename) {
if (!acl_args) {
errors = sdscatprintf(errors, "%s:%d: Unmatched parenthesis in selector definition.", server.acl_filename,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Every other error message in this function ends with a trailing ". " separator (src/acl.c:2537, 2559, 2569, 2600); this one doesn't. Now that this path no longer crashes, the message actually surfaces, and the missing space makes the concatenated output run together with the next line's error or with the final suffix appended at src/acl.c:2679: ...selector definition.WARNING: ACL errors detected....

Suggested change
errors = sdscatprintf(errors, "%s:%d: Unmatched parenthesis in selector definition.", server.acl_filename,
errors = sdscatprintf(errors, "%s:%d: Unmatched parenthesis in selector definition. ", server.acl_filename,

Comment thread src/acl.c
errors = sdscatprintf(errors, "%s:%d: Unmatched parenthesis in selector definition.", server.acl_filename,
linenum);
sdsfreesplitres(argv, argc);
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This deserves a regression test, since the crash is trivially reachable from an aclfile: ACLMergeSelectorArguments leaves *merged_argc at the partial count on the NULL return (src/acl.c:2286-2291), so any line with a complete argument before the unmatched open paren (e.g. user paren on (+get) segfaulted in the loop below. tests/unit/acl.tcl already has aclfile-backed start_server blocks that rewrite the file and reload (e.g. the duplicate-users test around tests/unit/acl.tcl:1316) — append a user paren on (+get line to the acl file, catch {r ACL LOAD} err, assert_match "*Unmatched parenthesis*" $err, and verify the server still answers r PING.

@enjoy-binbin
enjoy-binbin requested a review from madolson July 20, 2026 02:27
@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.87%. Comparing base (6b006c9) to head (f63d898).

Files with missing lines Patch % Lines
src/acl.c 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #4214      +/-   ##
============================================
+ Coverage     76.81%   76.87%   +0.06%     
============================================
  Files           162      162              
  Lines         81455    81457       +2     
============================================
+ Hits          62570    62621      +51     
+ Misses        18885    18836      -49     
Files with missing lines Coverage Δ
src/acl.c 92.53% <0.00%> (-0.12%) ⬇️

... and 21 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant