-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathLocalUnvalidatedArithmetic.qhelp
More file actions
45 lines (42 loc) · 1.74 KB
/
LocalUnvalidatedArithmetic.qhelp
File metadata and controls
45 lines (42 loc) · 1.74 KB
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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
It is dangerous to use the result of a virtual method call in pointer arithmetic without validation
if external users can provide their own implementation of the virtual method. For example, if
the analyzed project is distributed as a library or framework, then the end-user could provide a new
implementation that returns any value.
</p>
</overview>
<recommendation>
<p>
Always validate the result of virtual methods calls before performing pointer arithmetic to avoid
reading or writing outside the bounds of an allocated buffer.
</p>
</recommendation>
<example>
<p>
In this example, we write to a given element of an array, using an instance of the
<code>PossiblyOverridableClass</code> to determine which element to write to.
</p>
<p>
In the first case, the <code>GetElementNumber</code> method is called, and the result is used in
pointer arithmetic without any validation. If the user can define a subtype of
<code>PossiblyOverridableClass</code>, they can create an implementation of
<code>GetElementNumber</code> that returns an invalid element number. This would lead to a write
occurring outside the bounds of the <code>charArray</code>.
</p>
<p>
In the second case, the result of <code>GetElementNumber</code> is stored, and confirmed to be
within the bounds of the array. Note that it is not sufficient to check that it is smaller than the
length. We must also ensure that it's greater than zero, to prevent writes to locations before the
buffer as well as afterwards.
</p>
<sample src="LocalUnvalidatedArithmetic.cs" />
</example>
<references>
<li>Microsoft: <a href="https://msdn.microsoft.com/en-us/library/t2yzs44b.aspx">Unsafe Code and Pointers</a>.</li>
</references>
</qhelp>