Skip to content

Commit 3937714

Browse files
committed
C#: The CaptureSummaryModels query should only produce summary models that will not be discarded at run-time.
1 parent 784327c commit 3937714

File tree

3 files changed

+91
-81
lines changed

3 files changed

+91
-81
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/ExternalFlow.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,13 @@ Element interpretElement(
509509
)
510510
}
511511

512+
/**
513+
* Holds if `c` has a `generated` summary.
514+
*/
515+
predicate hasSummary(DataFlowCallable c, boolean generated) {
516+
summaryElement(c, _, _, _, generated)
517+
}
518+
512519
cached
513520
private module Cached {
514521
/**

csharp/ql/src/utils/model-generator/CaptureSummaryModels.ql

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,10 @@
44
* @id cs/utils/model-generator/summary-models
55
*/
66

7+
private import semmle.code.csharp.dataflow.ExternalFlow
78
private import internal.CaptureModels
8-
9-
/**
10-
* Capture fluent APIs that return `this`.
11-
* Example of a fluent API:
12-
* ```csharp
13-
* public class BasicFlow {
14-
* public BasicFlow ReturnThis(object input)
15-
* {
16-
* // some side effect
17-
* return this;
18-
* }
19-
* ```
20-
* Captured Model:
21-
* ```Summaries;BasicFlow;false;ReturnThis;(System.Object);Argument[Qualifier];ReturnValue;value```
22-
* Capture APIs that transfer taint from an input parameter to an output return
23-
* value or parameter.
24-
* Allows a sequence of read steps followed by a sequence of store steps.
25-
*
26-
* Examples:
27-
*
28-
* ```csharp
29-
* public class BasicFlow {
30-
* private string tainted;
31-
*
32-
* public String ReturnField()
33-
* {
34-
* return tainted;
35-
* }
36-
*
37-
* public void AssignFieldToArray(object[] target)
38-
* {
39-
* target[0] = tainted;
40-
* }
41-
* }
42-
* ```
43-
* Captured Models:
44-
* ```
45-
* Summaries;BasicFlow;false;ReturnField;();Argument[Qualifier];ReturnValue;taint |
46-
* Summaries;BasicFlow;false;AssignFieldToArray;(System.Object[]);Argument[Qualifier];Argument[0].Element;taint
47-
* ```
48-
*
49-
* ```csharp
50-
* public class BasicFlow {
51-
* private string tainted;
52-
*
53-
* public void SetField(string s)
54-
* {
55-
* tainted = s;
56-
* }
57-
* }
58-
* ```
59-
* Captured Model:
60-
* ```Summaries;BasicFlow;false;SetField;(System.String);Argument[0];Argument[Qualifier];taint```
61-
*
62-
* ```csharp
63-
* public class BasicFlow {
64-
* public void ReturnSubstring(string s)
65-
* {
66-
* return s.Substring(0, 1);
67-
* }
68-
* }
69-
* ```
70-
* Captured Model:
71-
* ```Summaries;BasicFlow;false;ReturnSubstring;(System.String);Argument[0];ReturnValue;taint```
72-
*
73-
* ```csharp
74-
* public class BasicFlow {
75-
* public void AssignToArray(int data, int[] target)
76-
* {
77-
* target[0] = data;
78-
* }
79-
* }
80-
* ```
81-
* Captured Model:
82-
* ```Summaries;BasicFlow;false;AssignToArray;(System.Int32,System.Int32[]);Argument[0];Argument[1].Element;taint```
83-
*/
84-
private string captureFlow(TargetApi api) {
85-
result = captureQualifierFlow(api) or
86-
result = captureThroughFlow(api)
87-
}
9+
private import internal.CaptureFlow
8810

8911
from TargetApi api, string flow
90-
where flow = captureFlow(api)
12+
where flow = captureFlow(api) and not hasSummary(api, false)
9113
select flow order by flow
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
private import CaptureModels
2+
3+
/**
4+
* Capture fluent APIs that return `this`.
5+
* Example of a fluent API:
6+
* ```csharp
7+
* public class BasicFlow {
8+
* public BasicFlow ReturnThis(object input)
9+
* {
10+
* // some side effect
11+
* return this;
12+
* }
13+
* ```
14+
* Captured Model:
15+
* ```Summaries;BasicFlow;false;ReturnThis;(System.Object);Argument[Qualifier];ReturnValue;value```
16+
* Capture APIs that transfer taint from an input parameter to an output return
17+
* value or parameter.
18+
* Allows a sequence of read steps followed by a sequence of store steps.
19+
*
20+
* Examples:
21+
*
22+
* ```csharp
23+
* public class BasicFlow {
24+
* private string tainted;
25+
*
26+
* public String ReturnField()
27+
* {
28+
* return tainted;
29+
* }
30+
*
31+
* public void AssignFieldToArray(object[] target)
32+
* {
33+
* target[0] = tainted;
34+
* }
35+
* }
36+
* ```
37+
* Captured Models:
38+
* ```
39+
* Summaries;BasicFlow;false;ReturnField;();Argument[Qualifier];ReturnValue;taint |
40+
* Summaries;BasicFlow;false;AssignFieldToArray;(System.Object[]);Argument[Qualifier];Argument[0].Element;taint
41+
* ```
42+
*
43+
* ```csharp
44+
* public class BasicFlow {
45+
* private string tainted;
46+
*
47+
* public void SetField(string s)
48+
* {
49+
* tainted = s;
50+
* }
51+
* }
52+
* ```
53+
* Captured Model:
54+
* ```Summaries;BasicFlow;false;SetField;(System.String);Argument[0];Argument[Qualifier];taint```
55+
*
56+
* ```csharp
57+
* public class BasicFlow {
58+
* public void ReturnSubstring(string s)
59+
* {
60+
* return s.Substring(0, 1);
61+
* }
62+
* }
63+
* ```
64+
* Captured Model:
65+
* ```Summaries;BasicFlow;false;ReturnSubstring;(System.String);Argument[0];ReturnValue;taint```
66+
*
67+
* ```csharp
68+
* public class BasicFlow {
69+
* public void AssignToArray(int data, int[] target)
70+
* {
71+
* target[0] = data;
72+
* }
73+
* }
74+
* ```
75+
* Captured Model:
76+
* ```Summaries;BasicFlow;false;AssignToArray;(System.Int32,System.Int32[]);Argument[0];Argument[1].Element;taint```
77+
*/
78+
string captureFlow(TargetApi api) {
79+
result = captureQualifierFlow(api) or
80+
result = captureThroughFlow(api)
81+
}

0 commit comments

Comments
 (0)