github / github/codeql

FP in UseAfterFree with reallocating data structures

Aperta
#20,577 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
C++ false-positive
Lingua principale
CodeQL
Stelle
10.1k
Fork
2.1k
Merge medio
2g 15h
PR unite (30g)
141

Descrizione

**Description of the false positive**

UseAfterFree.ql raises a false positive when run on a data structure with reallocated internal storage. I've observed with in several different data structure implementations, but the example below triggers the FP.

```cpp
typedef unsigned long long size_t;

template class Foo {
public:
Foo(void): capacity(0), size(0), items(nullptr) {}

~Foo(void) { Clear(); }

void Clear(void) {
if (items != nullptr) {
delete[] items;
items = nullptr;
size = 0;
capacity = 0;
}
}

T *Set(size_t index, T *item) {
if (index >= capacity) {
size_t new_capacity = capacity == 0 ? 4 : capacity;
while (index >= new_capacity) {
new_capacity *= 2;
}
T **new_items = new T *[new_capacity];
for (size_t i = 0; i < size; i++) {
new_items[i] = items[i];
}
if (items != nullptr) {
delete[] items;
}
items = new_items;
capacity = new_capacity;
}

if (index >= size) {
for (size_t i = size; i < index; i++) {
items[i] = nullptr;
}
size = index + 1;
}

items[index] = item;
return item;
}

T *Insert(size_t index, T *item) {
for (size_t i = index; i < size; i++) {
if (Set(i + 1, Get(i))) {
return nullptr;
}
}
return Set(index, item);
}

T *Get(size_t index) {
if (size <= index) {
return nullptr;
}
return items[index];
}

protected:
size_t capacity;
size_t size;
T **items;
};

int main() {
Foo foo;
foo.Set(1024, new size_t(44));
foo.Clear();
foo.Insert(1025, new size_t(46));
foo.Get(1025);
return 0;
}
```

To save anyone else the effort, this does not actually have a UAF exercised according to ASAN :)

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start by locating UseAfterFree.ql and reproducing the supplied C++ example to inspect why the reallocating data structure is reported. Compare the result with ASAN, then verify that the example is no longer flagged while genuine use-after-free cases remain detected.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp
Ambito
security
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.