-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathinteger-truncation.yaml
113 lines (113 loc) · 4.53 KB
/
integer-truncation.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
rules:
- id: raptor-integer-truncation
metadata:
author: Marco Ivaldi <raptor@0xdeadbeef.info>
references:
- https://cwe.mitre.org/data/definitions/197
- https://cwe.mitre.org/data/definitions/681
- https://g.co/kgs/PCHQjJ
- https://github.com/struct/mms
confidence: MEDIUM
# NOTE: the deep expression operator is necessary to catch some
# matches because C/C++ integer promotions do not seem entirely
# supported by Semgrep; this may generate false positives.
# NOTE: some types (e.g., ptrs) and use cases (e.g., comparisons)
# are not covered.
message: >-
Truncation errors occur when a primitive is cast to a primitive of a
smaller size and data is lost in the conversion. The value cannot be
trusted and the application will be in an undefined state.
severity: WARNING
languages:
- c
- cpp
pattern-either:
# char: assignments
- pattern: (char $NARROW) = <... (short $LARGE) ...>
- pattern: (char $NARROW) = <... (short int $LARGE) ...>
- pattern: (char $NARROW) = <... (unsigned short $LARGE) ...>
- pattern: (char $NARROW) = <... (unsigned short int $LARGE) ...>
- pattern: (char $NARROW) = <... (int $LARGE) ...>
- pattern: (char $NARROW) = <... (unsigned $LARGE) ...>
- pattern: (char $NARROW) = <... (unsigned int $LARGE) ...>
- pattern: (char $NARROW) = <... (long $LARGE) ...>
- pattern: (char $NARROW) = <... (long int $LARGE) ...>
- pattern: (char $NARROW) = <... (unsigned long $LARGE) ...>
- pattern: (char $NARROW) = <... (unsigned long int $LARGE) ...>
# char: function return
- patterns:
- pattern-either:
- pattern: |
char $FUN(...)
{
...
return (short $LARGE);
}
- pattern: |
char $FUN(...)
{
...
return (int $LARGE);
}
- pattern: |
char $FUN(...)
{
...
return (long $LARGE);
}
# improve output readability
- focus-metavariable: $FUN
# short: assignments
- pattern: (short $NARROW) = <... (unsigned short $LARGE) ...>
- pattern: (short int $NARROW) = <... (unsigned short int $LARGE) ...>
- pattern: (short $NARROW) = <... (int $LARGE) ...>
- pattern: (short $NARROW) = <... (unsigned $LARGE) ...>
- pattern: (short int $NARROW) = <... (unsigned int $LARGE) ...>
- pattern: (short $NARROW) = <... (long $LARGE) ...>
- pattern: (short int $NARROW) = <... (long int $LARGE) ...>
- pattern: (short $NARROW) = <... (unsigned long $LARGE) ...>
- pattern: (short int $NARROW) = <... (unsigned long int $LARGE) ...>
- pattern: (unsigned short $NARROW) = <... (int $LARGE) ...>
- pattern: (unsigned short $NARROW) = <... (unsigned $LARGE) ...>
- pattern: (unsigned short int $NARROW) = <... (unsigned int $LARGE) ...>
- pattern: (unsigned short $NARROW) = <... (long $LARGE) ...>
- pattern: (unsigned short int $NARROW) = <... (long int $LARGE) ...>
- pattern: (unsigned short $NARROW) = <... (unsigned long $LARGE) ...>
- pattern: (unsigned short int $NARROW) = <... (unsigned long int $LARGE) ...>
# short: function return
- patterns:
- pattern-either:
- pattern: |
short $FUN(...)
{
...
return (int $LARGE);
}
- pattern: |
short $FUN(...)
{
...
return (long $LARGE);
}
# improve output readability
- focus-metavariable: $FUN
# int: assignments
- pattern: (int $NARROW) = <... (unsigned $LARGE) ...>
- pattern: (int $NARROW) = <... (unsigned int $LARGE) ...>
- pattern: (int $NARROW) = <... (long $LARGE) ...>
- pattern: (int $NARROW) = <... (long int $LARGE) ...>
- pattern: (int $NARROW) = <... (unsigned long $LARGE) ...>
- pattern: (int $NARROW) = <... (unsigned long int $LARGE) ...>
# int: function return
- patterns:
- pattern: |
int $FUN(...)
{
...
return (long $LARGE);
}
# improve output readability
- focus-metavariable: $FUN
# long: assignments
- pattern: (long $NARROW) = <... (unsigned long $LARGE) ...>
- pattern: (long int $NARROW) = <... (unsigned long int $LARGE) ...>