Skip to content
Memory corruption Write-up Advanced

Authenticated Users Can Trigger a Heap OOB Read in pgPointcloud via pcpatch

The pgPointcloud extension trusts a 32-bit size field in the dimensional patch format without checking it against the remaining input length, so a crafted pcpatch causes an out-of-bounds heap read. An authenticated PostgreSQL user could read adjacent heap memory back through patch accessors and force a near-4 GB allocation, causing denial of service.

Harsh Raj Singhania Follow

Published on medium.com

Cybersecurity

Harsh Raj Singhania, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

pgPointcloud is a PostgreSQL extension for storing LiDAR and other point-cloud data. You register a schema that describes the dimensions (X, Y, Z, intensity, and whatever else you need), then store collections of points as pcpatch values. Patches can be stored uncompressed, with dimensional compression, or with LAZ.

The dimensional format is the one that matters here. Instead of interleaving every point's dimensions, it stores each dimension as its own compressed byte array. The on-wire and on-disk layout of a dimensional patch is roughly:

  • 1 byte endianness
  • 4 bytes pcid
  • 4 bytes compression type (dimensional)
  • 4 bytes number of points
  • then, for each dimension: 1 byte compression subtype, 4 bytes size, and the data itself

That per-dimension size field is where the problem lives.

Setup

The researcher built a small harness that links only a few of the library's object files and runs under AddressSanitizer. No full PostgreSQL instance was needed to reproduce the fault, and the write-up notes that a full PostgreSQL plus extension test was not re-run end to end; SQL reachability is argued from the call chain and the public function names.

The code was tested against master, with the latest tagged release at the time of writing being v1.2.5 (2023-09-19). The vulnerable code is present on the current master branch of the GitHub repository. No public CVE or advisory was found for this issue at the time of research, so it should be treated as unpublished or newly identified.

Finding it

The researcher was looking at how pgPointcloud turns WKB into patches and hit a path that felt thinner than the rest of the code. When PostgreSQL receives a hex string or bytea that is supposed to be a pcpatch, the call chain eventually reaches pc_patch_dimensional_from_wkb(). That function walks the dimensions and calls pc_bytes_deserialize() for each one.

The deserializer reads a 32-bit integer straight from the attacker-controlled buffer and uses it as the length for both the allocation and the memcpy. There is no check that buf + 5 + pcb->size is still inside the original WKB. The relevant part of the deserializer, simplified, is:

C
int pc_bytes_deserialize(const uint8_t *buf, const PCDIMENSION *dim,
                         PCBYTES *pcb, int readonly, int flip_endian)
{
  pcb->compression = buf[0];
  pcb->size = wkb_get_int32(buf + 1, flip_endian);
  ...
  if (readonly) {
    pcb->bytes = (uint8_t *)(buf + 5);
  } else {
    pcb->bytes = pcalloc(pcb->size);
    memcpy(pcb->bytes, buf + 5, pcb->size);
    ...
  }
  ...
}

The caller does not help either. In pc_patch_dimensional_from_wkb() the loop simply advances the read pointer by whatever size pc_bytes_deserialize just claimed:

C
buf = wkb + hdrsz;
for (i = 0; i < ndims; i++) {
  ...
  pc_bytes_deserialize(buf, dim, pcb, PC_FALSE, swap_endian);
  pcb->npoints = npoints;
  buf += pc_bytes_serialized_size(pcb);
}

There is no remaining-length tracking and no end pointer. The uncompressed path, by contrast, does perform a total-size check:

C
(wkbsize - hdrsz) != (s->size * npoints)

The dimensional path has no equivalent.

Exploitation

Step 1: Build a minimal input

The harness needs only a few of the library's object files and AddressSanitizer. The input is deliberately tiny: 13 real bytes, with the size field claiming 65536.

C
size_t avail = 1 + 4 + 8;
uint8_t *buf = malloc(avail);
buf[0] = 0;
int32_t claimed = 65536;
memcpy(buf+1, &claimed, 4);
memset(buf+5, 0x41, 8);

The first byte is set to zero, the next four bytes hold the claimed size (65536), and the remaining eight bytes are filler. The buffer is 13 bytes in total.

Step 2: Call the deserializer

C
pc_bytes_deserialize(buf, &dim, &pcb, PC_FALSE, 0);

With readonly false, the function allocates a buffer of the claimed size and copies that many bytes from buf + 5, which is far beyond the end of the 13-byte region.

Step 3: Observe the crash

AddressSanitizer reports immediately:

Text
==ERROR: AddressSanitizer: heap-buffer-overflow
  READ of size 65536
  #0 memcpy
  #1 pc_bytes_deserialize lib/pc_bytes.c:136
30 bytes after 13-byte region

This is exactly what the code path predicts: the library allocated a 64 KB buffer and then read far past the end of the 13-byte input.

Step 4: Reach it from SQL

The entry points are the normal ones:

  • casting a hex string to pcpatch
  • PC_PatchFromWKB()
  • inserting into a table that has a pointcloud column

All of them eventually call pc_patch_from_wkb → pc_patch_dimensional_from_wkb → pc_bytes_deserialize. Any authenticated PostgreSQL user who can supply a pcpatch value can hit it. No superuser is required.

Root cause

The dimensional serializer and deserializer assume the per-dimension size field is trustworthy. It is read as a full 32-bit integer from the input and used directly as the length for the allocation and the memcpy, with no check that the claimed size fits within the remaining WKB. The caller advances the read pointer by the claimed size without tracking how much input is left, so the loop follows the false size off the end of the buffer.

The uncompressed path was written with a total-size sanity check; the dimensional path was written later and never received the same treatment. The dimensional path was written later and never received the same treatment.

Impact

The out-of-bounds bytes are copied into the patch's internal buffers. Later calls such as PC_Get, PC_AsText, or other patch accessors can surface those bytes back to the attacker, giving a heap-memory disclosure primitive. Because the size field is a full 32-bit value, an attacker can also force a near-4 GB allocation, which is a straightforward denial of service against the backend process.

The researcher did not demonstrate code execution and does not claim it. The concrete results are the ASan crash, the oversized allocation, and the fact that the copied data lives inside the patch object where it can be read back. Impact is limited to what was demonstrated: out-of-bounds read, large allocation and denial of service, and potential disclosure of the copied bytes.

Fix

The fix is to pass the end of the WKB buffer all the way into the dimensional loop and into pc_bytes_deserialize. Before the memcpy, verify that the claimed size still fits. Mirror the total-size validation that already exists on the uncompressed path. A hard upper bound on the number of points would not hurt either.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

A security researcher found a bug in a free add-on for the PostgreSQL database called pgPointcloud. The add-on lets people store and work with point clouds: huge collections of 3D points, the kind of data a laser scanner (LiDAR) produces when it maps a building, a road or a forest.

To store a point cloud, the add-on uses a compact binary format. Inside that format there is a small number that says how long one piece of data is. The bug is that the program believes that number without checking it. If someone sends a piece of data that claims to be enormous, the program tries to read that many bytes from a much smaller input and keeps reading past the end of it, into memory it should never touch.

The researcher proved this with a tiny 13-byte input and a tool called AddressSanitizer, which watches memory use and shouts when a program reads outside its own buffers. The result was a crash. The same flaw can also make the database server try to reserve almost 4 GB of memory, which can knock it offline. The researcher did not show that an attacker could run their own code, and does not claim it.

From the original on medium.com

The article's title and byline, as published on Medium.

Background: what the pieces are

Before the bug makes sense, a few everyday ideas need names.

PostgreSQL is a database: a program that stores information in tables and answers questions about it. Think of a very large, very strict spreadsheet that many people can use at once.

An extension is an optional add-on that teaches the database a new trick. pgPointcloud is one: it teaches PostgreSQL about point clouds.

LiDAR is a way of measuring the world with laser pulses. A scanner fires millions of pulses and records how long each takes to bounce back, which gives the distance to a surface. The result is a cloud of points, each with an X, Y and Z position, plus often extra measurements such as how strong the reflection was (intensity).

A dimension is one of those measurements. X is a dimension, Y is a dimension, Z is a dimension, intensity is a dimension. A point cloud's schema is simply the list of dimensions it has.

A patch (pcpatch in the add-on) is a bundle of points stored together as one value in the database. You can think of it as a box of points rather than one row per point.

WKB stands for Well-Known Binary. It is a standard way of writing geometric data as a stream of bytes so it can travel between programs or be saved to disk. When someone hands PostgreSQL a hex string or a bytea value (a raw blob of bytes) that is supposed to be a patch, the add-on has to turn those bytes back into a patch. That turning-back process is called deserialisation.

A buffer is a chunk of memory set aside to hold bytes. Out-of-bounds (OOB) read means a program reads bytes from outside the buffer it was given. Imagine being handed a 13-page booklet but told to read page 65,000: you would be reading whatever happens to be lying on the desk next to it, which might be someone else's private notes.

A heap is the area of memory a program uses for data it creates while running. A heap-buffer-overflow is an out-of-bounds read or write on that kind of memory.

AddressSanitizer (ASan) is a debugging tool that adds invisible guards around memory. When a program steps outside a buffer, ASan stops it and prints a report instead of letting it carry on silently.

A denial of service (DoS) is an attack that does not steal data but stops a service from working, for example by making it use up all its memory.

A CVE is a public catalogue number for a known security flaw. The researcher found no CVE or public advisory for this issue at the time of research, so it should be treated as unpublished or newly identified.

The format, and where the problem sits

pgPointcloud can store a patch in three ways: uncompressed, with dimensional compression, or with LAZ (a compressed LiDAR format). The dimensional format is the one that matters here.

Instead of writing every point's dimensions one after another, the dimensional format groups them. It stores each dimension as its own compressed block of bytes. The layout of a dimensional patch, as the researcher describes it, is roughly:

  • 1 byte for endianness (which order the bytes of a number are written in)
  • 4 bytes for the pcid (an identifier for the patch)
  • 4 bytes for the compression type (here, dimensional)
  • 4 bytes for the number of points
  • then, for each dimension: 1 byte for the compression subtype, 4 bytes for a size, and the data itself

That per-dimension size field is where the problem lives. It is a number written by whoever created the patch, and the program treats it as the truth.

Finding it

The researcher was reading how pgPointcloud turns WKB into patches and noticed one path that felt thinner than the rest of the code, meaning less carefully checked.

When PostgreSQL receives a hex string or bytea that is supposed to be a pcpatch, the call chain eventually reaches a function called pc_patch_dimensional_from_wkb(). A call chain is just the sequence of functions that call one another: A calls B, B calls C, and so on. This function walks through the dimensions one by one and calls pc_bytes_deserialize() for each.

Here is the relevant part of the deserialiser, simplified as it appears in the write-up:

C
int pc_bytes_deserialize(const uint8_t *buf, const PCDIMENSION *dim,
                         PCBYTES *pcb, int readonly, int flip_endian)
{
  pcb->compression = buf[0];
  pcb->size = wkb_get_int32(buf + 1, flip_endian);
  ...
  if (readonly) {
    pcb->bytes = (uint8_t *)(buf + 5);
  } else {
    pcb->bytes = pcalloc(pcb->size);
    memcpy(pcb->bytes, buf + 5, pcb->size);
    ...
  }
  ...
}

Line by line, in plain words:

  1. int pc_bytes_deserialize(...) declares a function that takes a pointer to the input bytes (buf), a description of the dimension, a place to store the result (pcb), a flag saying whether the caller only wants to read (readonly), and a flag for byte order (flip_endian).
  2. pcb->compression = buf[0]; reads the first byte of the input and stores it as the compression subtype.
  3. pcb->size = wkb_get_int32(buf + 1, flip_endian); reads the next four bytes as a 32-bit integer and stores it as the size. This number comes straight from the input, so the person who made the input chooses it.
  4. if (readonly) { pcb->bytes = (uint8_t *)(buf + 5); } in read-only mode, it simply points at the data that starts five bytes in, without copying anything.
  5. else { pcb->bytes = pcalloc(pcb->size); memcpy(pcb->bytes, buf + 5, pcb->size); ... } otherwise it reserves a new block of memory as large as the claimed size, then copies that many bytes from five bytes into the input.

The critical point: nothing checks that buf + 5 + pcb->size is still inside the original WKB. The program trusts the size number completely.

The caller does not help either. In pc_patch_dimensional_from_wkb() the loop simply moves the read pointer forward by whatever size pc_bytes_deserialize just claimed:

C
buf = wkb + hdrsz;
for (i = 0; i < ndims; i++) {
  ...
  pc_bytes_deserialize(buf, dim, pcb, PC_FALSE, swap_endian);
  pcb->npoints = npoints;
  buf += pc_bytes_serialized_size(pcb);
}

Line by line:

  1. buf = wkb + hdrsz; sets the read pointer to the start of the data, just after the fixed-size header.
  2. for (i = 0; i < ndims; i++) { repeats once for each dimension.
  3. pc_bytes_deserialize(buf, dim, pcb, PC_FALSE, swap_endian); deserialises the current dimension. PC_FALSE means not read-only, so the copying branch runs.
  4. pcb->npoints = npoints; records the number of points.
  5. buf += pc_bytes_serialized_size(pcb); moves the pointer forward by the size the deserialiser just reported.

There is no tracking of how much input is left and no end pointer marking the buffer's limit. The loop follows the claimed size wherever it leads.

The uncompressed path, by contrast, does perform a total-size check:

C
(wkbsize - hdrsz) != (s->size * npoints)

This compares the number of bytes actually available after the header with the number the data claims to need. If they do not match, the input is rejected. The dimensional path has no equivalent check.

Proving it

Step 1: Build a minimal input

The researcher built a small test program (a harness) that links only a few of the library's object files and runs under AddressSanitizer. No full PostgreSQL instance was needed to reproduce the fault. The input is deliberately tiny: 13 real bytes, with the size field claiming 65536.

C
size_t avail = 1 + 4 + 8;
uint8_t *buf = malloc(avail);
buf[0] = 0;
int32_t claimed = 65536;
memcpy(buf+1, &claimed, 4);
memset(buf+5, 0x41, 8);

Line by line:

  1. size_t avail = 1 + 4 + 8; works out the total size: one byte plus four bytes plus eight bytes, which is 13.
  2. uint8_t *buf = malloc(avail); asks the computer for a block of 13 bytes and keeps a pointer to it.
  3. buf[0] = 0; sets the first byte, the compression subtype, to zero.
  4. int32_t claimed = 65536; creates a 32-bit number holding 65536, the size the attacker will claim.
  5. memcpy(buf+1, &claimed, 4); copies those four bytes into the buffer starting at the second byte. This is the size field.
  6. memset(buf+5, 0x41, 8); fills the last eight bytes with the value 0x41 (the letter A) as filler.

The first byte is set to zero, the next four bytes hold the claimed size (65536), and the remaining eight bytes are filler. The buffer is 13 bytes in total.

Step 2: Call the deserialiser

C
pc_bytes_deserialize(buf, &dim, &pcb, PC_FALSE, 0);

This calls the vulnerable function on the 13-byte buffer. PC_FALSE means the read-only flag is off, so the function takes the copying branch. It reserves a buffer of the claimed size (65536 bytes) and copies that many bytes starting at buf + 5, which is far beyond the end of the 13-byte region. The program is now reading memory it was never given.

Step 3: Observe the crash

AddressSanitizer reports immediately:

Text
==ERROR: AddressSanitizer: heap-buffer-overflow
  READ of size 65536
  #0 memcpy
  #1 pc_bytes_deserialize lib/pc_bytes.c:136
30 bytes after 13-byte region

Reading this report:

  1. ==ERROR: AddressSanitizer: heap-buffer-overflow says ASan caught a read or write outside a heap buffer.
  2. READ of size 65536 says the program tried to read 65536 bytes in one go.
  3. #0 memcpy is the innermost function where it happened, the standard memory-copy routine.
  4. #1 pc_bytes_deserialize lib/pc_bytes.c:136 is the function that called it, in the file lib/pc_bytes.c at line 136.
  5. 30 bytes after 13-byte region says the read started 30 bytes past the end of the 13-byte block.

This is exactly what the code path predicts: the library allocated a 64 KB buffer and then read far past the end of the 13-byte input.

Step 4: Reach it from SQL

SQL is the language used to talk to a database. The entry points an ordinary user could use are the normal ones:

  • casting a hex string to pcpatch
  • PC_PatchFromWKB()
  • inserting into a table that has a pointcloud column

A cast is a conversion from one type to another, for example telling the database to treat a piece of text as a patch. All of these eventually call pc_patch_from_wkb → pc_patch_dimensional_from_wkb → pc_bytes_deserialize. Any authenticated PostgreSQL user who can supply a pcpatch value can hit it. No superuser (the database's all-powerful administrator account) is required.

Why it happens

The dimensional serialiser and deserialiser assume the per-dimension size field is trustworthy. It is read as a full 32-bit integer from the input and used directly as the length for the allocation and the memcpy, with no check that the claimed size fits within the remaining WKB. The caller moves the read pointer forward by the claimed size without tracking how much input is left, so the loop follows the false size off the end of the buffer.

The uncompressed path was written with a total-size sanity check; the dimensional path was written later and never received the same treatment. The dimensional path was written later and never received the same treatment.

What an attacker could do with it

The out-of-bounds bytes are copied into the patch's internal buffers. Later calls such as PC_Get, PC_AsText, or other patch accessors can surface those bytes back to the attacker. In plain terms, the program scoops up whatever memory sits next to the input and stores it inside the patch, and the attacker can then ask the patch to show it. That is a heap-memory disclosure primitive: a way to leak pieces of the server's memory.

Because the size field is a full 32-bit value, an attacker can also force a near-4 GB allocation. The server would try to reserve that much memory, which is a straightforward denial of service against the backend process: the part of PostgreSQL that handles one user's connection.

The researcher did not demonstrate code execution and does not claim it. The concrete results are the ASan crash, the oversized allocation, and the fact that the copied data lives inside the patch object where it can be read back. Impact is limited to what was demonstrated: out-of-bounds read, large allocation and denial of service, and potential disclosure of the copied bytes.

The fix

The fix is to pass the end of the WKB buffer all the way into the dimensional loop and into pc_bytes_deserialize. Before the memcpy, verify that the claimed size still fits. Mirror the total-size validation that already exists on the uncompressed path. A hard upper bound on the number of points would not hurt either. In everyday terms: tell the copying routine where the booklet ends, and make it stop at the last page.

Other places worth testing

Status and caveats

The code was tested against master, the main development branch of the repository, with the latest tagged release at the time of writing being v1.2.5 (2023-09-19). The vulnerable code is present on the current master branch of the GitHub repository. No public CVE or advisory was found for this issue at the time of research, so it should be treated as unpublished or newly identified.

The write-up notes that a full PostgreSQL plus extension test was not re-run end to end; SQL reachability is argued from the call chain and the public function names. The exact line number of the memcpy may shift slightly with future commits, though the logic itself is unchanged in the inspected source.

Glossary

  • AddressSanitizer (ASan): a debugging tool that watches memory and stops a program when it reads or writes outside a buffer.
  • API: a defined way for one program to ask another for something; here, the functions the extension exposes to the database.
  • Backend process: the part of PostgreSQL that handles a single user's connection.
  • Buffer: a block of memory set aside to hold bytes.
  • bytea: PostgreSQL's data type for a raw blob of bytes.
  • Call chain: the sequence of functions that call one another while handling a request.
  • Cast: converting a value from one data type to another.
  • CVE: a public catalogue number for a known security flaw.
  • Denial of service (DoS): an attack that stops a service from working, for example by exhausting its memory.
  • Deserialisation: turning a stream of bytes back into a usable data structure.
  • Dimension: one measurement of a point, such as X, Y, Z or intensity.
  • Endianness: the order in which the bytes of a number are written.
  • Extension: an optional add-on that gives a database new abilities.
  • Heap: the area of memory a program uses for data it creates while running.
  • Heap-buffer-overflow: reading or writing outside a buffer in heap memory.
  • Hex string: text that represents bytes using pairs of hexadecimal characters.
  • LAZ: a compressed format for LiDAR point-cloud data.
  • LiDAR: a scanning method that measures distances with laser pulses to build a cloud of 3D points.
  • memcpy: a standard routine that copies a given number of bytes from one place to another.
  • Out-of-bounds (OOB) read: reading bytes from outside the buffer a program was given.
  • Patch (pcpatch): a bundle of point-cloud points stored as one database value.
  • Point cloud: a large collection of 3D points, each with measurements such as position.
  • PostgreSQL: a free, widely used relational database.
  • Primitive: a basic building block of an attack, such as a way to leak memory.
  • Read-only branch: the code path that points at existing data instead of copying it.
  • Schema: the list of dimensions a point cloud has.
  • Serialisation: turning a data structure into a stream of bytes.
  • SQL: the language used to query and modify a database.
  • Superuser: the all-powerful administrator account in PostgreSQL.
  • WKB (Well-Known Binary): a standard binary format for geometric data.

More by Harsh Raj Singhania

View all

9 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.