Commit 061a9f65c6 for aom

commit 061a9f65c684a681fefefad2281cbcd310ff329d
Author: Jerome Jiang <jianj@google.com>
Date:   Wed Oct 21 17:24:03 2026 -0400

    Validate external reference count in AV1D_SET_EXT_REF_PTR

    ctrl_set_ext_ref_ptr() copied ext_frames->num entries into
    ctx->ext_refs.refs[MAX_EXTERNAL_REFERENCES] without checking
    bounds or null ext_frames->img, and mutated ext_frames->img.
    Passing num > MAX_EXTERNAL_REFERENCES caused an out-of-bounds
    write past ctx->ext_refs.refs[128].

    Reject null ext_frames->img, non-positive counts, and counts
    over MAX_EXTERNAL_REFERENCES with AOM_CODEC_INVALID_PARAM, and
    index ext_frames->img without mutating the caller's struct.

    TAG=agy
    CONV=74ece8ab-3f39-4cda-9df8-8325c945d9d9

    Bug: 574106934
    Bug: 576177995
    Change-Id: Ib9b2466dd054814be08fc9ab9c421363ffdc0a90

diff --git a/aom/aomdx.h b/aom/aomdx.h
index 4c32beb4f1..84e9564577 100644
--- a/aom/aomdx.h
+++ b/aom/aomdx.h
@@ -179,7 +179,7 @@ typedef struct aom_screen_content_tools_info {
 typedef struct av1_ext_ref_frame {
   /*! Start pointer of external references. */
   aom_image_t *img;
-  /*! Number of available external references. */
+  /*! Number of available external references. Valid range: 1..128. */
   int num;
 } av1_ext_ref_frame_t;

diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index 3fbe5e53f2..71f268232b 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -1351,18 +1351,19 @@ static aom_codec_err_t ctrl_get_tile_data(aom_codec_alg_priv_t *ctx,

 static aom_codec_err_t ctrl_set_ext_ref_ptr(aom_codec_alg_priv_t *ctx,
                                             va_list args) {
-  av1_ext_ref_frame_t *const data = va_arg(args, av1_ext_ref_frame_t *);
+  const av1_ext_ref_frame_t *const ext_frames =
+      va_arg(args, av1_ext_ref_frame_t *);

-  if (data) {
-    av1_ext_ref_frame_t *const ext_frames = data;
-    ctx->ext_refs.num = ext_frames->num;
-    for (int i = 0; i < ctx->ext_refs.num; i++) {
-      image2yuvconfig(ext_frames->img++, &ctx->ext_refs.refs[i]);
-    }
-    return AOM_CODEC_OK;
-  } else {
+  if (ext_frames == NULL || ext_frames->img == NULL || ext_frames->num <= 0 ||
+      ext_frames->num > MAX_EXTERNAL_REFERENCES) {
     return AOM_CODEC_INVALID_PARAM;
   }
+
+  ctx->ext_refs.num = ext_frames->num;
+  for (int i = 0; i < ctx->ext_refs.num; i++) {
+    image2yuvconfig(&ext_frames->img[i], &ctx->ext_refs.refs[i]);
+  }
+  return AOM_CODEC_OK;
 }

 static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
diff --git a/test/decode_api_test.cc b/test/decode_api_test.cc
index ca17433932..320d7ed8ff 100644
--- a/test/decode_api_test.cc
+++ b/test/decode_api_test.cc
@@ -9,6 +9,9 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */

+#include <climits>
+#include <vector>
+
 #include "gtest/gtest.h"

 #include "config/aom_config.h"
@@ -80,4 +83,64 @@ TEST(DecodeAPI, Buganizer503691210) {
   EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&dec));
 }

+TEST(DecodeAPI, SetExtRefPtr) {
+  aom_codec_iface_t *iface = aom_codec_av1_dx();
+  aom_codec_ctx_t dec;
+  EXPECT_EQ(AOM_CODEC_OK, aom_codec_dec_init(&dec, iface, nullptr, 0));
+
+  constexpr int kMaxExternalReferences = 128;
+  std::vector<aom_image_t> imgs(2048);
+  for (aom_image_t &img : imgs) {
+    img = {};
+    img.w = 1000;
+    img.h = 1000;
+    img.d_w = 1000;
+    img.d_h = 1000;
+    img.fmt = AOM_IMG_FMT_I420;
+  }
+
+  av1_ext_ref_frame_t ext_ref = { imgs.data(), kMaxExternalReferences + 1 };
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+
+  ext_ref = { imgs.data(), static_cast<int>(imgs.size()) };
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+
+  ext_ref = { imgs.data(), INT_MIN };
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+
+  ext_ref = { imgs.data(), -1 };
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+
+  ext_ref = { imgs.data(), 0 };
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, nullptr));
+
+  ext_ref = { nullptr, 0 };
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+
+  ext_ref = { nullptr, 1 };
+  EXPECT_EQ(AOM_CODEC_INVALID_PARAM,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+
+  ext_ref = { imgs.data(), 1 };
+  EXPECT_EQ(AOM_CODEC_OK,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+  EXPECT_EQ(ext_ref.img, imgs.data());
+
+  ext_ref = { imgs.data(), kMaxExternalReferences };
+  EXPECT_EQ(AOM_CODEC_OK,
+            aom_codec_control(&dec, AV1D_SET_EXT_REF_PTR, &ext_ref));
+  EXPECT_EQ(ext_ref.img, imgs.data());
+
+  EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&dec));
+}
+
 }  // namespace