[io] LZF image reader wraps compressed_size + header_size, and Bayer debayer wraps width-2

Abierto
#6,542 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp

Línea de trabajo

Read io/src/lzf_image_io.cpp around LZFImageReader::loadImageBlob and io/src/debayer.cpp around debayerEdgeAware and the matching loops at lines 588 and 726. Start by checking how the file header sizes and minimum Bayer dimensions are validated before allocation or writes; the issue includes an untested patch and a regression test for the compressed size case, but no patch for the Bayer case. Done means both crafted inputs are rejected safely, with regression coverage for each finding.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

We looked for a private security contact for this project (there is no SECURITY.md, GitHub private vulnerability reporting is disabled, and no security email is listed in the README, CONTRIBUTING.md, or the website), so we are reporting these here.

Our code review of PointCloudLibrary/pcl at commit fd39f32af932d50e033dba17b29e6d5fa4f64255 (master; that was also the current HEAD when we read it) found two memory-safety bugs in the LZF image readers. This is source review only; nothing was run or compiled.

  1. io/src/lzf_image_io.cpp, LZFImageReader::loadImageBlob: line 429 checks compressed_size + header_size against the mapped file size. compressed_size is a uint32 taken from the file (lines 426-427) and header_size is 37 (line 48), so the sum is computed in 32 bits and can wrap. When that check passes, line 443 resizes a buffer to compressed_size and line 444 copies that many bytes out of the mapping. A crafted .pclzf can therefore cause a very large allocation and a copy from a short mapping. This is reachable through pcl_pclzf2pcd, ImageGrabber in pclzf mode, and the public LZF*ImageReader::read.

An untested fix is included below as pcl-301-f2.patch (apply with git apply, not git am). It compares the sum in 64 bits and adds a regression test in test/io/test_io.cpp. It has not been compiled or run; it passes git apply --check on the commit above.

This is distinct from merged #6504, which already does the width × height uncompressed-size comparison in 64 bits (io/include/pcl/io/impl/lzf_image_io.hpp line 447). That change does not cover the compressed_size + header_size addition at line 429, which is still in 32 bits on this commit.

  1. io/src/debayer.cpp, debayerEdgeAware: lines 439-465 write the first pixels unconditionally, including past rgb_buffer[rgb_line_step + 5]. Line 471 (and the same pattern at lines 588 and 726; the outer loop at line 546 is height - 2) is for (unsigned xIdx = 2; xIdx < width - 2; ...). When width is below 2, the bound wraps. The only size check on this path is uncompressed_size == width × height (lzf_image_io.hpp line 447); line 463 allocates width × height × 3 bytes and line 465 calls the debayer. A small Bayer .pclzf that passes that check can therefore write past a small RGB buffer and then loop for a very large count. We do not have a patch for this finding; none was prepared. In words, the reader should refuse images smaller than the minimum this routine assumes, before it writes anything.

Merged #6504 mentioned the Bayer reader only for the large-dimension width × height wrap, which that change already closed. The width-2 loop bound is a separate leftover.

pcl-301-f2.patch (untested)
diff --git a/io/src/lzf_image_io.cpp b/io/src/lzf_image_io.cpp
index 5c01cfb..803abae 100644
--- a/io/src/lzf_image_io.cpp
+++ b/io/src/lzf_image_io.cpp
@@ -426,7 +426,9 @@ pcl::io::LZFImageReader::loadImageBlob (const std::string &filename,
   std::uint32_t compressed_size;
   memcpy (&compressed_size,   &map[29], sizeof (std::uint32_t));
 
-  if (compressed_size + header_size != data_size)
+  // Compare in 64 bits: compressed_size comes from the file, and adding the header size to it in
+  // 32-bit unsigned arithmetic wraps, so a tiny file can claim a ~4 GB payload and pass this check
+  if (static_cast<std::uint64_t> (compressed_size) + header_size != static_cast<std::uint64_t> (data_size))
   {
     PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Number of bytes to decompress written in file (%u) differs from what it should be (%u)!\n", compressed_size, data_size - header_size);
 #ifdef _WIN32

diff --git a/test/io/test_io.cpp b/test/io/test_io.cpp
index 9632bbf..4478c78 100644
--- a/test/io/test_io.cpp
+++ b/test/io/test_io.cpp
@@ -1849,6 +1913,29 @@ TEST (PCL, LZFImageWrappedDimensions)
   remove ("test_lzf_bayer.pclzf");
 }
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+TEST (PCL, LZFImageWrappedCompressedSize)
+{
+  // compressed_size is read from the file and was added to the 37-byte header size in 32-bit
+  // unsigned arithmetic, so this 36-byte file claiming 0xFFFFFFFF compressed bytes passed the size
+  // check and the reader went on to copy ~4 GB out of a one-page mapping.
+  char header[36] = {};
+  const std::uint32_t width = 1, height = 1, compressed_size = 0xFFFFFFFF;
+  memcpy (&header[0], "PCLZF", 5);
+  memcpy (&header[5], &width, sizeof (width));
+  memcpy (&header[9], &height, sizeof (height));
+  memcpy (&header[13], "depth16", 7);
+  memcpy (&header[29], &compressed_size, sizeof (compressed_size));
+  std::ofstream fs ("wrapped_compressed_size.pclzf", std::ios::binary);
+  fs.write (header, sizeof (header));
+  fs.close ();
+
+  PointCloud<PointXYZ> depth_cloud;
+  pcl::io::LZFDepth16ImageReader reader;
+  EXPECT_FALSE (reader.read ("wrapped_compressed_size.pclzf", depth_cloud));
+  remove ("wrapped_compressed_size.pclzf");
+}
+
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 TEST (PCL, Locale)
 {

Credit: Ugur Celik (ucsurf), @ucsurf

— Ugur Celik (ucsurf) · https://uc.surf
We find and report security issues in open source before attackers do. If our work helped you, a small contribution keeps it going and makes the internet a little safer for everyone:
https://buymeacoffee.com/ucsurf

Lenguaje dominante
C++
Estrellas
11.2k
Forks
4.7k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de PointCloudLibrary/pcl

Todos los issues de PointCloudLibrary/pcl

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.