Skip to content

dest.c: stop cups_dnssd_unquote() reading past a trailing backslash - #224

Open
ManasBagul23 wants to merge 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix-dnssd-unquote-trailing-backslash-overread
Open

ManasBagul23 wants to merge 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix-dnssd-unquote-trailing-backslash-overread

Conversation

@ManasBagul23

Copy link
Copy Markdown
Contributor

Summary

`cups_dnssd_unquote()` unescapes a DNS-SD service name in place.

The bug

When the name has a literal backslash as its very last character (nothing follows it before the terminating NUL), the function advances past the backslash with `src++` and unconditionally does `*dst++ = *src++` without checking whether that landed on the NUL. This copies the NUL into the destination (prematurely truncating the unescaped name) and advances `src` one byte past the string's own allocation; the loop's own `while (*src ...)` condition then dereferences that out-of-bounds byte.

This function is reached directly from DNS-SD/mDNS discovery callbacks (`cups_dest_query_cb()`, `cups_dest_browse_cb()`, `cups_dest_resolve()`) on attacker-influenceable network data: any device on the local network broadcasting a service name ending in an unescaped backslash reaches this path.

Fix

Only copy the byte after the backslash when there is one.

Test plan

  • Reproduced the exact one-byte overread with a guard-page test: placed the string so its trailing NUL sits on the last byte of a mapped page, with the following page unmapped. The unpatched function reliably segfaults; the fixed version returns cleanly, treating the trailing backslash as having nothing left to unescape.

cups_dnssd_unquote() unescapes a DNS-SD service name in place. When
the name has a literal backslash as its very last character (nothing
follows it before the terminating NUL), the function advances past
the backslash with "src++" and unconditionally does "*dst++ = *src++"
without checking whether that landed on the NUL. This copies the NUL
into the destination (prematurely truncating the unescaped name) and
advances src one byte past the string's own allocation; the loop's
own "while (*src ...)" condition then dereferences that out-of-bounds
byte.

This function is reached directly from DNS-SD/mDNS discovery callbacks
(cups_dest_query_cb(), cups_dest_browse_cb(), cups_dest_resolve()) on
attacker-influenceable network data: any device on the local network
broadcasting a service name ending in an unescaped backslash reaches
this path. Reproduced the exact one-byte overread with a guard-page
test (placing the string so its trailing NUL sits on the last byte of
a mapped page, with the following page unmapped): the unpatched
function reliably segfaults; the fixed version returns cleanly,
treating the trailing backslash as having nothing left to unescape.

Only copy the byte after the backslash when there is one.

This branch has not been deployed

No deployments
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