-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C++: Fix more FPs in cpp/overflow-buffer
#18629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3591f84
941ad87
403a0eb
9fa3ff7
839640a
764a846
02cf458
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: feature | ||
--- | ||
* A new predicate `getOffsetInClass` was added to the `Field` class, which computes the byte offset of a field relative to a given `Class`. |
Original file line number | Diff line number | Diff line change | |||
---|---|---|---|---|---|
|
@@ -24,6 +24,74 @@ predicate memberMayBeVarSize(Class c, MemberVariable v) { | ||||
exists(ArrayType t | t = v.getUnspecifiedType() | not t.getArraySize() > 1) | |||||
} | |||||
|
|||||
/** | |||||
* Given a chain of accesses of the form `x.f1.f2...fn` this | |||||
* predicate gives the type of `x`. Note that `x` may be an implicit | |||||
* `this` expression. | |||||
*/ | |||||
private Class getRootType(FieldAccess fa) { | |||||
// If the object is accessed inside a member function then the root will | |||||
// be a(n implicit) `this`. And the root type will be the type of `this`. | |||||
exists(VariableAccess root | | |||||
root = fa.getQualifier*() and | |||||
result = | |||||
root.getQualifier() | |||||
.(ThisExpr) | |||||
.getUnspecifiedType() | |||||
.(PointerType) | |||||
.getBaseType() | |||||
.getUnspecifiedType() | |||||
) | |||||
or | |||||
// Otherwise, if this is not inside a member function there will not be | |||||
// a(n implicit) `this`. And the root type is the type of the outermost | |||||
// access. | |||||
exists(VariableAccess root | | |||||
root = fa.getQualifier+() and | |||||
not exists(root.getQualifier()) and | |||||
result = root.getUnspecifiedType() | |||||
) | |||||
} | |||||
|
|||||
/** | |||||
* Gets the size of the buffer access at `va`. | |||||
*/ | |||||
private int getSize(VariableAccess va) { | |||||
exists(Variable v | va.getTarget() = v | | |||||
// If `v` is not a field then the size of the buffer is just | |||||
// the size of the type of `v`. | |||||
exists(Type t | | |||||
t = v.getUnspecifiedType() and | |||||
not v instanceof Field and | |||||
not t instanceof ReferenceType and | |||||
result = t.getSize() | |||||
) | |||||
or | |||||
exists(Class c | | |||||
// Otherwise, we find the "outermost" object and compute the size | |||||
// as the difference between the size of the type of the "outermost | |||||
// object" and the offset of the field relative to that type. | |||||
// For example, consider the following structs: | |||||
// ``` | |||||
// struct S { | |||||
// uint32_t x; | |||||
// uint32_t y; | |||||
// }; | |||||
// struct S2 { | |||||
// S s; | |||||
// uint32_t z; | |||||
// }; | |||||
// ``` | |||||
// Given an object `S2 s2` the size of the buffer `&s2.s.y` | |||||
// is the size of the base object type (i.e., `S2`) minutes the offset | |||||
// of `y` relative to the type `S2` (i.e., `4`). So the size of the | |||||
// buffer is `12 - 4 = 8`. | |||||
Comment on lines
+85
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So "size" here includes everything between the start of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. This is sensible when you consider how
So we compute the buffer starting at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, so you're basically interested in not writing beyond the end of the struct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! |
|||||
c = getRootType(va) and | |||||
result = c.getSize() - v.(Field).getOffsetInClass(c) | |||||
) | |||||
) | |||||
} | |||||
|
|||||
/** | |||||
* Holds if `bufferExpr` is an allocation-like expression. | |||||
* | |||||
|
@@ -54,37 +122,11 @@ private int isSource(Expr bufferExpr, Element why) { | ||||
result = bufferExpr.(AllocationExpr).getSizeBytes() and | |||||
why = bufferExpr | |||||
or | |||||
exists(Type bufferType, Variable v | | |||||
exists(Variable v | | |||||
v = why and | |||||
// buffer is the address of a variable | |||||
why = bufferExpr.(AddressOfExpr).getAddressable() and | |||||
bufferType = v.getUnspecifiedType() and | |||||
not bufferType instanceof ReferenceType and | |||||
not any(Union u).getAMemberVariable() = why | |||||
| | |||||
not v instanceof Field and | |||||
result = bufferType.getSize() | |||||
or | |||||
// If it's an address of a field (i.e., a non-static member variable) | |||||
// then it's okay to use that address to access the other member variables. | |||||
// For example, this is okay: | |||||
// ``` | |||||
// struct S { uint8_t a, b, c; }; | |||||
// S s; | |||||
// memset(&s.a, 0, sizeof(S) - offsetof(S, a)); | |||||
exists(Field f | | |||||
v = f and | |||||
result = f.getDeclaringType().getSize() - f.getByteOffset() | |||||
) | |||||
) | |||||
or | |||||
exists(Union bufferType | | |||||
// buffer is the address of a union member; in this case, we | |||||
// take the size of the union itself rather the union member, since | |||||
// it's usually OK to access that amount (e.g. clearing with memset). | |||||
why = bufferExpr.(AddressOfExpr).getAddressable() and | |||||
bufferType.getAMemberVariable() = why and | |||||
result = bufferType.getSize() | |||||
result = getSize(bufferExpr.(AddressOfExpr).getOperand()) | |||||
) | |||||
} | |||||
|
|||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we do need a change note for the predicate that's being added here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's a good point. I'll add that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 02cf458