Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CUPS_DEBUG_LOG support for "%d" is not properly guarded #1066

Closed
frostb1ten opened this issue Sep 30, 2024 · 3 comments
Closed

CUPS_DEBUG_LOG support for "%d" is not properly guarded #1066

frostb1ten opened this issue Sep 30, 2024 · 3 comments
Assignees
Labels
bug Something isn't working priority-medium
Milestone

Comments

@frostb1ten
Copy link

Description
A format string vulnerability exists in CUPS when compiled with debugging enabled. The vulnerability arises from improper sanitization of the CUPS_DEBUG_LOG environment variable in the _cups_debug_set() function. Specifically, this variable is passed to snprintf() without validation, allowing user input with format specifiers (e.g., %p, %n) to be processed.

This can lead to:

  • Memory Address Leakage: The use of %p allows a regular user to leak memory addresses, potentially aiding in bypassing security mechanisms such as ASLR.
  • Memory Write Attempt via %n: Although %n is blocked in modern environments, it still presents a potential security issue where this protection is absent.

This finding was originally discovered while reviewing the code at:
https://github.com/OpenPrinting/cups/blob/master/cups/debug.c#L98:

 _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"), getenv("CUPS_DEBUG_FILTER"), 0);

In this code, the CUPS_DEBUG_LOG environment variable is passed to the _cups_debug_set() function, which processes the environment data. The vulnerability occurs when the function then reaches the following line: https://github.com/OpenPrinting/cups/blob/master/cups/debug.c#L278:

snprintf(buffer, sizeof(buffer), logfile, getpid());

Here, snprintf() is used to format the log file string, which can lead to a format string vulnerability if user-controlled input contains specifiers like %p or %n. This allows an attacker to leak memory addresses or attempt arbitrary memory writes in environments where protections like %n blocking may not be in place.

To Reproduce
Steps to reproduce the behavior:

  1. Compile CUPS with debug mode enabled:
git clone https://github.com/OpenPrinting/cups.git
cd cups
./configure --enable-debug --enable-debug-printfs --disable-shared --disable-gssapi
make
  1. Set the CUPS_DEBUG_LOG environment variable with a format string and launch cupsd:
mkdir /tmp/test
export CUPS_DEBUG_LOG="/tmp/test/%p %p"
./scheduler/cupsd -f  # Break out of this after it starts running.
  1. Observe the output in /tmp/test2/, where the file is named using leaked memory addresses due to the %p specifiers.
    cca25949-59ac-43ce-904c-ccffa1ee6180

Expected behavior
The CUPS_DEBUG_LOG variable should be treated as a plain string, and any format specifiers should not be interpreted by snprintf(). The log file should be created without leaking memory addresses or allowing unintended memory writes.

System Information:

@michaelrsweet michaelrsweet changed the title NON-PRODUCTION-CODE: Format String Vulnerability in CUPS Debug Logging (CUPS_DEBUG_LOG) Leading to Memory Disclosure CUPS_DEBUG_LOG support for "%d" is not properly guarded Sep 30, 2024
@michaelrsweet michaelrsweet self-assigned this Sep 30, 2024
@michaelrsweet michaelrsweet added bug Something isn't working priority-medium labels Sep 30, 2024
@michaelrsweet michaelrsweet added this to the v2.5 milestone Sep 30, 2024
@michaelrsweet
Copy link
Member

Note: Not treating as a security issue because debug printfs are a developer tool that is not normally compiled into the library.

CUPS_DEBUG_LOG is defined as supporting only a single "%d" to insert the current process ID. Update the code to look for this and substitute manually rather than using snprintf to do the heavy lifting.

@michaelrsweet
Copy link
Member

Oh, and thank you for finding and reporting this issue... :)

@michaelrsweet
Copy link
Member

[master 0da38a5] Harden debug printf PID support (Issue #1066)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority-medium
Projects
None yet
Development

No branches or pull requests

2 participants