Skip to content

Stack buffer overflow in ipptool with_value() for long octetString WITH-VALUE #1542

@Tomer-PL

Description

@Tomer-PL

Summary

In tools/ipptool.c:7654-7656, the with_value() function copies a literal string into withdata[1023] via memcpy with no length check. The hex-encoded path (line 7616) correctly validates length, but the literal string path does not. A .test file with a WITH-VALUE longer than 1023 bytes overflows the stack buffer by up to ~1024 bytes.

Details

// tools/ipptool.c:7608
unsigned char withdata[1023];

// Line 7654-7656: literal string path -- NO bounds check
withlen = strlen(value);
memcpy(withdata, value, (size_t)withlen);  // OVERFLOW if value > 1023

// Compare with hex path at line 7616: HAS bounds check
if (withlen > (int)(2 * (sizeof(withdata) + 1)))
    // ... error handling

Reproducer

Create a .test file:

{
    OPERATION Get-Printer-Attributes
    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR naturalLanguage attributes-natural-language en
    ATTR uri printer-uri $uri
    EXPECT job-password-encryption OF-TYPE octetString
    WITH-VALUE "AAAA...2048 A's...AAAA"
}
gcc -g -fsanitize=address -I. -L./cups -Wl,-rpath,./cups \
    -o ipptool_asan tools/ipptool.c -lcups
./ipptool_asan -v http://localhost:631/ /tmp/overflow.test

ASan output:

ERROR: AddressSanitizer: stack-buffer-overflow
WRITE of size 2047 overflowing 1023-byte withdata variable

Suggested Fix

  withlen = strlen(value);
+ if (withlen > (int)sizeof(withdata))
+ {
+     print_fatal_error(data, "WITH-VALUE too long for octetString comparison.");
+     return (false);
+ }
  memcpy(withdata, value, (size_t)withlen);

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions