Skip to content

Commit ea8d766

Browse files
authored
Merge pull request #20242 from MathiasVP/fprintf-partial-flow
C++: Mark the write to `fprintf`'s 0'th argument as partial
2 parents 4684ac1 + af00e46 commit ea8d766

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaImpl.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,9 @@ private predicate modeledFlowBarrier(Node n) {
756756
partialFlowFunc = call.getStaticCallTarget() and
757757
not partialFlowFunc.isPartialWrite(output)
758758
|
759-
call.getStaticCallTarget().(DataFlow::DataFlowFunction).hasDataFlow(_, output)
759+
partialFlowFunc.(DataFlow::DataFlowFunction).hasDataFlow(_, output)
760760
or
761-
call.getStaticCallTarget().(Taint::TaintFunction).hasTaintFlow(_, output)
761+
partialFlowFunc.(Taint::TaintFunction).hasTaintFlow(_, output)
762762
)
763763
or
764764
exists(Operand operand, Instruction instr, Node n0, int indirectionIndex |

cpp/ql/lib/semmle/code/cpp/models/interfaces/FormattingFunction.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
170170
output.isParameterDeref(this.getOutputParameterIndex(_))
171171
)
172172
}
173+
174+
final override predicate isPartialWrite(FunctionOutput output) {
175+
exists(int outputParameterIndex |
176+
output.isParameterDeref(outputParameterIndex) and
177+
// We require the output to be a stream since that definitely means that
178+
// it's a partial write. If it's not a stream then it will most likely
179+
// fill the whole buffer.
180+
outputParameterIndex = this.getOutputParameterIndex(true)
181+
)
182+
}
173183
}
174184

175185
/**

cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7767,6 +7767,10 @@ WARNING: module 'TaintTracking' has been deprecated and may be removed in future
77677767
| taint.cpp:830:20:830:34 | call to indirect_source | taint.cpp:832:23:832:24 | in | |
77687768
| taint.cpp:831:15:831:17 | out | taint.cpp:832:18:832:20 | out | |
77697769
| taint.cpp:831:15:831:17 | out | taint.cpp:833:8:833:10 | out | |
7770+
| taint.cpp:841:21:841:35 | call to indirect_source | taint.cpp:842:11:842:12 | fp | |
7771+
| taint.cpp:841:21:841:35 | call to indirect_source | taint.cpp:843:16:843:17 | fp | |
7772+
| taint.cpp:842:11:842:12 | ref arg fp | taint.cpp:843:16:843:17 | fp | |
7773+
| taint.cpp:842:15:842:16 | | taint.cpp:842:11:842:12 | ref arg fp | TAINT |
77707774
| thread.cpp:10:27:10:27 | s | thread.cpp:10:27:10:27 | s | |
77717775
| thread.cpp:10:27:10:27 | s | thread.cpp:11:8:11:8 | s | |
77727776
| thread.cpp:14:26:14:26 | s | thread.cpp:15:8:15:8 | s | |

cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,15 @@ void test_write_to_const_ptr_ptr() {
831831
const char* out;
832832
take_const_ptr(out, in);
833833
sink(out); // $ SPURIOUS: ast
834+
}
835+
836+
void indirect_sink(FILE *fp);
837+
int fprintf(FILE *fp, const char *format, ...);
838+
839+
int f7(void)
840+
{
841+
FILE* fp = (FILE*)indirect_source();
842+
fprintf(fp, "");
843+
indirect_sink(fp); // $ ir MISSING: ast
844+
return 0;
834845
}

cpp/ql/test/library-tests/dataflow/taint-tests/taint.ql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ module IRTest {
117117
call.getTarget().getName() = "sink" and
118118
[sink.asExpr(), sink.asIndirectExpr()] = call.getAnArgument()
119119
)
120+
or
121+
exists(FunctionCall call |
122+
call.getTarget().getName() = "indirect_sink" and
123+
sink.asIndirectExpr() = call.getAnArgument()
124+
)
120125
}
121126

122127
predicate isBarrier(DataFlow::Node barrier) {

cpp/ql/test/library-tests/dataflow/taint-tests/test_mad-signatures.expected

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17670,6 +17670,55 @@ signatureMatches
1767017670
| taint.cpp:822:6:822:19 | take_const_ptr | (unsigned long *,const char *) | | set_cert_ex | 1 |
1767117671
| taint.cpp:822:6:822:19 | take_const_ptr | (unsigned long *,const char *) | | set_name_ex | 1 |
1767217672
| taint.cpp:822:6:822:19 | take_const_ptr | (uv_pipe_t *,const char *) | | uv_pipe_bind | 1 |
17673+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_default_uflow | 0 |
17674+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_feof | 0 |
17675+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_ferror | 0 |
17676+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_file_close_mmap | 0 |
17677+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_file_underflow_mmap | 0 |
17678+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_ftell | 0 |
17679+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_getc | 0 |
17680+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_getwc | 0 |
17681+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_new_file_underflow | 0 |
17682+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_peekc_locked | 0 |
17683+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_str_count | 0 |
17684+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_str_underflow | 0 |
17685+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_sungetc | 0 |
17686+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_sungetwc | 0 |
17687+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_wdefault_uflow | 0 |
17688+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_wfile_underflow | 0 |
17689+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_wfile_underflow_mmap | 0 |
17690+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_wstr_count | 0 |
17691+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | _IO_wstr_underflow | 0 |
17692+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __fbufsize | 0 |
17693+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __feof_unlocked | 0 |
17694+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __ferror_unlocked | 0 |
17695+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __fileno | 0 |
17696+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __flbf | 0 |
17697+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __fopen_maybe_mmap | 0 |
17698+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __fpending | 0 |
17699+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __ftello | 0 |
17700+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __fwriting | 0 |
17701+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __getc_unlocked | 0 |
17702+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __getwc_unlocked | 0 |
17703+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __uflow | 0 |
17704+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __underflow | 0 |
17705+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __wuflow | 0 |
17706+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | __wunderflow | 0 |
17707+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | feof_unlocked | 0 |
17708+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | ferror_unlocked | 0 |
17709+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | fgetc_unlocked | 0 |
17710+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | fgetgrent | 0 |
17711+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | fgetpwent | 0 |
17712+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | fgetsgent | 0 |
17713+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | fgetspent | 0 |
17714+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | getc_unlocked | 0 |
17715+
| taint.cpp:836:6:836:18 | indirect_sink | (FILE *) | | getmntent | 0 |
17716+
| taint.cpp:837:5:837:11 | fprintf | (CURLSH *,CURLSHoption,...) | | curl_share_setopt | 2 |
17717+
| taint.cpp:837:5:837:11 | fprintf | (Jim_Interp *,const char *,...) | | Jim_SetResultFormatted | 1 |
17718+
| taint.cpp:837:5:837:11 | fprintf | (Jim_Interp *,const char *,...) | | Jim_SetResultFormatted | 2 |
17719+
| taint.cpp:837:5:837:11 | fprintf | (char **,const char *,...) | | ___asprintf | 1 |
17720+
| taint.cpp:837:5:837:11 | fprintf | (char **,const char *,...) | | ___asprintf | 2 |
17721+
| taint.cpp:837:5:837:11 | fprintf | (curl_httppost **,curl_httppost **,...) | | curl_formadd | 2 |
1767317722
| thread.cpp:4:6:4:9 | sink | (int) | | ASN1_STRING_type_new | 0 |
1767417723
| thread.cpp:4:6:4:9 | sink | (int) | | ASN1_tag2bit | 0 |
1767517724
| thread.cpp:4:6:4:9 | sink | (int) | | ASN1_tag2str | 0 |
@@ -47191,6 +47240,10 @@ getParameterTypeName
4719147240
| taint.cpp:817:6:817:27 | write_to_const_ptr_ptr | 1 | const char ** |
4719247241
| taint.cpp:822:6:822:19 | take_const_ptr | 0 | const char * |
4719347242
| taint.cpp:822:6:822:19 | take_const_ptr | 1 | const char * |
47243+
| taint.cpp:836:6:836:18 | indirect_sink | 0 | FILE * |
47244+
| taint.cpp:837:5:837:11 | fprintf | 0 | FILE * |
47245+
| taint.cpp:837:5:837:11 | fprintf | 1 | const char * |
47246+
| taint.cpp:837:5:837:11 | fprintf | 2 | ... |
4719447247
| thread.cpp:4:6:4:9 | sink | 0 | int |
4719547248
| thread.cpp:6:8:6:8 | operator= | 0 | S && |
4719647249
| thread.cpp:6:8:6:8 | operator= | 0 | const S & |

0 commit comments

Comments
 (0)