CVE-2022-46149: Out-of-bounds Read (CWE-125)
value: Cap'n Proto is a data interchange format and remote procedure call (RPC) system. Cap'n Proro prior to versions 0.7.1, 0.8.1, 0.9.2, and 0.10.3, as well as versions of Cap'n Proto's Rust implementation prior to 0.13.7, 0.14.11, and 0.15.2 are vulnerable to out-of-bounds read due to logic error handling list-of-list. This issue may lead someone to remotely segfault a peer by sending it a malicious message, if the victim performs certain actions on a list-of-pointer type. Exfiltration of memory is possible if the victim performs additional certain actions on a list-of-pointer type. To be vulnerable, an application must perform a specific sequence of actions, described in the GitHub Security Advisory. The bug is present in inlined code, therefore the fix will require rebuilding dependent applications. Cap'n Proto has C++ fixes available in versions 0.7.1, 0.8.1, 0.9.2, and 0.10.3. The `capnp` Rust crate has fixes available in versions 0.13.7, 0.14.11, and 0.15.2.
CWE description: The product reads data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 5.4
Published: 2022-11-30T17:15Z
Repository: https://github.com/capnproto/capnproto
Language: Other
Files changed: c++/src/capnp/layout.c++, c++/src/capnp/layout.h
Commit date: 2022-11-23 12:12:06 -0600
Commit message: Apply data offset for list-of-pointers at access time rather than ListReader creation time.

Baking this offset into `ptr` reduced ops needed at access time but made the interpretation of `ptr` inconsistent depending on what type of list was expected.

Diff: diff --git a/c++/src/capnp/layout.c++ b/c++/src/capnp/layout.c++
index dc756892e..e2fa84df6 100644
--- a/c++/src/capnp/layout.c++
+++ b/c++/src/capnp/layout.c++
@@ -2358,14 +2358,10 @@ struct WireHelpers {
               goto useDefault;
             }
             break;
 
           case ElementSize::POINTER:
-            // We expected a list of pointers but got a list of structs.  Assuming the first field
-            // in the struct is the pointer we were looking for, we want to munge the pointer to
-            // point at the first element's pointer section.
-            ptr += tag->structRef.dataSize.get();
             KJ_REQUIRE(tag->structRef.ptrCount.get() > ZERO * POINTERS,
                        "Schema mismatch: Expected a pointer list, but got a list of data-only "
                        "structs.") {
               goto useDefault;
             }
diff --git a/c++/src/capnp/layout.h b/c++/src/capnp/layout.h
index c8d533cff..7a27f68a1 100644
--- a/c++/src/capnp/layout.h
+++ b/c++/src/capnp/layout.h
@@ -1225,12 +1225,16 @@ template <>
 inline Void ListReader::getDataElement<Void>(ElementCount index) const {
   return VOID;
 }
 
 inline PointerReader ListReader::getPointerElement(ElementCount index) const {
+  // If the list elements have data sections we need to skip those. Note that for pointers to be
+  // present at all (which already must be true if we get here), then `structDataSize` must be a
+  // whole number of words, so we don't have to worry about unaligned reads here.
+  auto offset = structDataSize / BITS_PER_BYTE;
   return PointerReader(segment, capTable, reinterpret_cast<const WirePointer*>(
-      ptr + upgradeBound<uint64_t>(index) * step / BITS_PER_BYTE), nestingLimit);
+      ptr + offset + upgradeBound<uint64_t>(index) * step / BITS_PER_BYTE), nestingLimit);
 }
 
 // -------------------------------------------------------------------
 
 inline OrphanBuilder::OrphanBuilder(OrphanBuilder&& other) noexcept

Vulnerable code:             // We expected a list of pointers but got a list of structs.  Assuming the first field
            // in the struct is the pointer we were looking for, we want to munge the pointer to
            // point at the first element's pointer section.
            ptr += tag->structRef.dataSize.get();
      ptr + upgradeBound<uint64_t>(index) * step / BITS_PER_BYTE), nestingLimit);
Fixed code:   // If the list elements have data sections we need to skip those. Note that for pointers to be
  // present at all (which already must be true if we get here), then `structDataSize` must be a
  // whole number of words, so we don't have to worry about unaligned reads here.
  auto offset = structDataSize / BITS_PER_BYTE;
      ptr + offset + upgradeBound<uint64_t>(index) * step / BITS_PER_BYTE), nestingLimit);