github / github/codeql

how to filter out this situation?

Aperta
#19,838 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
question
Lingua principale
CodeQL
Stelle
10.1k
Fork
2.1k
Merge medio
2g 15h
PR unite (30g)
141

Descrizione

Hi, I have been learning to use CodeQL recently. I was trying to find all expressions that reach the `len` parameter of `memcpy`, and in the results, there is a case like the following.

```c
attr->val.octets = _malloc(attr->length);
if (!attr->val.octets)
goto out_err_mem;
memcpy(attr->val.octets, orig_avp_val, attr->length);
```
In this part, the `len` parameter of `memcpy` is exactly the same as the parameter of `_malloc`, which I consider to be safe. Therefore, I would like to exclude this situation. However, first, I want to identify this pattern, so I have written the following code.

```ql
import cpp
import semmle.code.cpp.dataflow.new.DataFlow
import semmle.code.cpp.dataflow.new.TaintTracking

// class MallocSize extends Expr {
// MallocSize() {
// exists(FunctionCall fc |
// fc.getTarget().hasName("malloc") and
// this = fc.getArgument(0)
// )
// }
// }

module RecvToMemcpyConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
exists(Expr e | source.asExpr() = e
and not e.isConstant()
)
}

predicate isSink(DataFlow::Node sink) {
exists(FunctionCall fc, FunctionCall mc |
fc.getTarget().hasName("memcpy") and mc.getTarget().hasName("malloc")
and sink.asExpr() = fc.getArgument(2)
and fc.getArgument(2) = mc.getArgument(0)
)
and not sink.asExpr().isConstant()
}
}

module RecvToMemcpyFlow = TaintTracking::Global;

from RecvToMemcpyFlow::PathNode source, RecvToMemcpyFlow::PathNode sink
where RecvToMemcpyFlow::flowPath(source, sink)
select
source,
sink,
sink.getNode().getFunction().getFile(),
source.getNode().getFunction().getFile()
```

Although I think this code may not handle the following situation, I believe it should be able to handle the case where the `size` parameter of `malloc` and the `len` parameter of `memcpy` are exactly the same, meaning when the expressions are an exact match.

```c
b = _malloc(sizeof(*b) + size);
b->size = size;
memcpy(b->buf, buf, size);
```

But the result returns 0 results.

I also tried other approaches, but none of them met my expectations. How should I correctly handle this situation?

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia con gli import di DataFlow e TaintTracking, quindi esamina RecvToMemcpyConfig, in particolare isSource, isSink e la query flowPath. Riproduci la query sui due esempi in C e determina se vengono restituiti i casi previsti di espressioni esatte ed espressioni correlate. Il lavoro è completo quando la query identifica le relazioni sicure tra le dimensioni di malloc e memcpy descritte dall’issue.

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

Valutazione

Stack tecnologico
cpp
Ambito
devtools, security
Tipo di issue
Funzionalità
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.