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

未关闭
#6,542 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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

主要语言
C++
星标
11.2k
派生
4.7k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PointCloudLibrary/pcl 的其他 Issue

查看 PointCloudLibrary/pcl 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。