Skip to content

guard nvalues before reading values[nvalues-1] in cgi var getters - #1684

Merged
michaelrsweet merged 1 commit into
OpenPrinting:masterfrom
aizu-m:cgi-var-empty-array-bounds
Aug 28, 2026
Merged

guard nvalues before reading values[nvalues-1] in cgi var getters#1684
michaelrsweet merged 1 commit into
OpenPrinting:masterfrom
aizu-m:cgi-var-empty-array-bounds

Conversation

@aizu-m

@aizu-m aizu-m commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

cgiGetVariable, cgiGetCheckbox and cgiGetTextfield read values[nvalues - 1] without the bounds check cgiGetArray already has, so a variable emptied by cgiSetSize(name, 0) indexes values[-1] and the stale word is then dereferenced as a char *; guard nvalues first.

@michaelrsweet

Copy link
Copy Markdown
Member

Have you actually reproduced this issue happening?

@michaelrsweet michaelrsweet self-assigned this Aug 27, 2026
@michaelrsweet michaelrsweet added the investigating Investigating the issue label Aug 27, 2026
@aizu-m

aizu-m commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

Yes. Found it while running the cgi-bin code under AddressSanitizer.

Minimal repro, linked against the project's var.c + libcups:

#include "cgi.h"
int main(void) {
  cgiSetVariable("FOO", "bar");   /* nvalues = 1 */
  cgiSetSize("FOO", 0);           /* nvalues = 0, values buffer kept */
  return cgiGetVariable("FOO") ? 0 : 1;  /* reads values[-1] */
}

Before the patch ASAN flags a heap-buffer-overflow READ of size 8 at var.c:316, 8 bytes before the values region, and that stale word is then handed to strdup(). cgiGetCheckbox (var.c:193) and cgiGetTextfield (var.c:270) do the same values[nvalues-1] read.

The reachable path is the printer options page. admin.c empties PARAMS/PARAMTEXT/PARAMVALUE/INPUTTYPE with cgiSetSize(name, 0) around line 2908, and for a standard (non-custom) option those are never refilled, so they sit in the table with nvalues=0. The template then reads them as scalars in the {#name?...} existence test, which goes through cgiGetVariable (template.c:454) and hits the values[-1] read. cgiGetArray already guards element against nvalues; these three getters don't, which is all the patch lines up.

@michaelrsweet michaelrsweet added bug Something isn't working and removed investigating Investigating the issue labels Aug 28, 2026
@michaelrsweet michaelrsweet added this to the v2.5 milestone Aug 28, 2026
@michaelrsweet
michaelrsweet merged commit 0c43228 into OpenPrinting:master Aug 28, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants