Signed overflow in cbor_value_get_int64() / cbor_value_get_int() for out-of-range negative integers
評価
調査の方向性
影響を受ける関数は cbor_value_get_int64() と cbor_value_get_int() で、src/cbor.h にあります。まず、負の整数の処理と未チェック変換に関するドキュメントを読んでください。報告された境界ケースを UndefinedBehaviorSanitizer で再現し、その後、両方の入力を対象とするテストを追加してください。両方の関数が符号付き整数オーバーフローを回避し、sanitizer を使ってテストが通れば完了です。
索引モデルが issue の本文から書いたものです。
説明
cbor_value_get_int64() and cbor_value_get_int() in src/cbor.h do
*result = -*result - 1;
for negative integers. If the encoded magnitude is 2^63 (or 2^31 for the int version), *result is already INT64_MIN / INT_MIN at that point, so the negation overflows. That's UB, and it's reachable from untrusted input through the public API. The only precondition is cbor_value_is_integer(), which these inputs satisfy.
Inputs:
3b 80 00 00 00 00 00 00 00, i.e. -2^63 - 1, hitscbor_value_get_int64()3a 80 00 00 00, i.e. -2^31 - 1, hitscbor_value_get_int()
With clang -fsanitize=undefined on current main (da6421f):
src/cbor.h:437:19: runtime error: negation of -9223372036854775808 cannot be represented in type 'int64_t' (aka 'long')
src/cbor.h:446:19: runtime error: negation of -2147483648 cannot be represented in type 'int'
Minimal repro:
#include <stdio.h>
#include <stdint.h>
#include "cbor.h"
int main(void)
{
static const uint8_t data[] = { 0x3b, 0x80, 0, 0, 0, 0, 0, 0, 0 };
CborParser parser;
CborValue it;
int64_t v;
if (cbor_parser_init(data, sizeof(data), 0, &parser, &it) || !cbor_value_is_integer(&it))
return 1;
cbor_value_get_int64(&it, &v);
printf("%lld\n", (long long)v);
return 0;
}
I know the docs say to use the _checked variants if the value might not fit, and those are fine. But the unchecked ones are documented as returning a truncated value in that case, not as UB, and in practice a sanitizer build aborts. An optimising compiler is also free to assume the overflow can't happen.
I ran into this while writing CBMC harnesses and fuzzing tinycbor. Happy to open a PR with a fix (~*result instead of -*result - 1, same value, no overflow) and a test if that sounds good.
- 主要言語
- C++
- スター
- 641
- フォーク
- 225
- 平均マージ
- 2日 3時間
- マージ済み PR(30日)
- 4
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
intel/tinycbor のほかの issue
-
CborValidateTagUse rejects valid tag 1 (epoch) with a floating-point value, and restricts tags 21-23 too much対応中かも @thiagomacieira が 28 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
scp-fs2open/fs2open.github.com#7965 ·
メンテナーはふだん 1 日以内に返信
-
[Bug][CuTeDSL][Fuzz] `utils.gemm.sm100.epilogue` releases the accumulator before `tcgen05.ld` completes: no `tcgen05.wait::ld` before the arrive that lets the next MMA overwrite TMEM対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 4 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
elf-alchemist/elfbsp#113 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
patrick-hudson/put.gd-desktop#979 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Dao-AILab/flash-attention#3097 ·
メンテナーはふだん 3 日以内に返信