Signed overflow in cbor_value_get_int64() / cbor_value_get_int() for out-of-range negative integers
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
Direzione di ricerca
Le funzioni interessate sono cbor_value_get_int64() e cbor_value_get_int() in src/cbor.h; inizia leggendo come gestiscono gli interi negativi e la documentazione sulla conversione non verificata. Riproduci i casi limite segnalati con UndefinedBehaviorSanitizer, quindi aggiungi test per entrambi gli input. Il lavoro è concluso quando entrambe le funzioni evitano l’overflow degli interi con segno e i test passano con il sanitizer.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- C++
- Stelle
- 641
- Fork
- 225
- Merge medio
- 2g 3h
- PR unite (30g)
- 4
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di intel/tinycbor
-
CborValidateTagUse rejects valid tag 1 (epoch) with a floating-point value, and restricts tags 21-23 too muchForse già presa @thiagomacieira l’ha presa 28 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
-
CBOR validation not worksAperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Tutte le issue di intel/tinycbor
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
ROCm/FastFlowLM#887 ·
I maintainer di solito rispondono entro 1 giorno
-
bug medium priority openmp tokenizer wrong result
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
good first issue invalid
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
DanielJerolm/gpcc#74 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
patrick-hudson/put.gd-desktop#808 ·
I maintainer di solito rispondono entro 1 giorno
-
DPI change is not fully handled in the cascading window on WindowsForse già presa @yukawa l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 4 giorni