mtd: RAM consolidation reads past its block buffer on a malformed entry

オープン 初心者向け
#22,319 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c

調査の方向性

不具合は drivers/mtd/mtd_config.c の mtdconfig_ramconsolidate() にあります。約685行目のペイロードのコピーで、ソース側の境界チェックなしに phdr->len が使われています。直前のヘッダ解析を読んでから、src_offset + sizeof(hdr) + phdr->len <= dev->erasesize を確認するチェックを追加し、失敗したら errout へジャンプしてください。不正なエントリがエラーを返し、CONFIG_MTD_CONFIG_RAM_CONSOLIDATE=1 を指定した ASan ハーネスがオーバーフローを報告しなければ完了です。

索引モデルが issue の本文から書いたものです。

説明

Mechanism. mtdconfig_ramconsolidate() allocates pbuf = kmm_malloc(dev->erasesize) (:579) and walks the source entries of one block. At :685 it copies the entry payload with

mtdconfig_writebytes(dev, dst_offset, &pbuf[src_offset + sizeof(hdr)], phdr->len)

using the on-flash phdr->len without checking that src_offset + sizeof(hdr) + phdr->len fits inside dev->erasesize. Destination space is checked; the source bound is not.

Effect. A header at offset 499 with len = 200 passes the header read (499 + 6 = 505 ≤ 512) but starts a 200-byte copy at 505, so 119 bytes are read past the 512-byte pbuf.

Trigger. RAM consolidation processes a malformed active entry. Corrupt flash is a robustness case; this function-level test does not establish that an attacker can supply such an image on a deployed device.

Location. NuttX nuttx-13.1.0, commit 2b5c0de5e933281c209f954a63b99cf10c132346, drivers/mtd/mtd_config.c, sha256 30b2de5e6c7e4fc28df2401d3b6d13926abe84d70274c227efbdb28c12ab6aa3:

Anchor Line Code
allocation :579 pbuf = kmm_malloc(dev->erasesize);
payload copy :685 mtdconfig_writebytes(dev, dst_offset, &pbuf[src_offset + sizeof(hdr)], phdr->len)
error exit :730 label errout:

Line numbers in other renderings differ; the code text locates each anchor.

Evidence. One before/after function-level ASan run, harness geometry blocksize=128, erasesize=512, neraseblocks=2, CONFIG_MTD_CONFIG_RAM_CONSOLIDATE=1, image with 31 well-formed active entries followed by an active header at offset 499 with len=200: before.log reports heap-buffer-overflow, READ of size 119 at mtd_config.c:685, past a 512-byte region allocated at :579; with the patch, returned -5 and no ASan report.

Expected. Consolidation rejects an entry whose declared payload runs past the block end instead of copying it.

Patch. Validate src_offset + sizeof(hdr) + phdr->len <= dev->erasesize before the copy and take the errout path when it does not hold.

主要言語
C
スター
4.1k
フォーク
1.8k
平均マージ
1日 17時間
マージ済み PR(30日)
900

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/nuttx のほかの issue

apache/nuttx の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。