Skip to content

Commit

Permalink
[analyzer] If memory region is tainted mark data as tainted.
Browse files Browse the repository at this point in the history
+ random comments

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146199 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
AnnaZaks committed Dec 8, 2011
1 parent 1e4f68c commit 5fc7def
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SymbolData : public SymExpr {
}
};

/// A symbol representing the value of a MemRegion.
///\brief A symbol representing the value stored at a MemRegion.
class SymbolRegionValue : public SymbolData {
const TypedValueRegion *R;

Expand Down
5 changes: 5 additions & 0 deletions lib/StaticAnalyzer/Core/ProgramState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,11 @@ bool ProgramState::isTainted(const SymExpr* Sym, TaintTagType Kind) const {
// If this is a SymbolDerived with a tainted parent, it's also tainted.
if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);

// If memory region is tainted, data is also tainted.
if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI))
Tainted = Tainted || isTainted(SRV->getRegion(), Kind);

if (Tainted)
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) {
return UnknownVal();
}

// If value is a non integer constant, produce unknown.
if (!isa<nonloc::ConcreteInt>(val))
return UnknownVal();

// Only handle casts from integers to integers.
// Only handle casts from integers to integers - if val is an integer constant
// being cast to a non integer type, produce unknown.
if (!isLocType && !castTy->isIntegerType())
return UnknownVal();

Expand Down
25 changes: 24 additions & 1 deletion test/Analysis/taint-tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ int Buffer[BUFSIZE];

struct XYStruct {
int x;
float y;
int y;
char z;
};

void taintTracking(int x) {
Expand All @@ -26,9 +27,31 @@ void taintTracking(int x) {
// Tainted ptr arithmetic/array element address.
int tprtarithmetic1 = *(addr+1); // expected-warning 2 {{tainted}}

// Dereference.
int *ptr;
scanf("%p", &ptr);
int ptrDeref = *ptr; // expected-warning 2 {{tainted}}
int _ptrDeref = ptrDeref + 13; // expected-warning 2 {{tainted}}

// Pointer arithmetic + dereferencing.
// FIXME: We fail to propagate the taint here because RegionStore does not
// handle ElementRegions with symbolic indexes.
int addrDeref = *addr; // expected-warning {{tainted}}
int _addrDeref = addrDeref;

// Tainted struct address, casts.
struct XYStruct *xyPtr = 0;
scanf("%p", &xyPtr);
void *tXYStructPtr = xyPtr; // expected-warning 2 {{tainted}}
struct XYStruct *xyPtrCopy = tXYStructPtr; // expected-warning 2 {{tainted}}
int ptrtx = xyPtr->x;// expected-warning 2 {{tainted}}
int ptrty = xyPtr->y;// expected-warning 2 {{tainted}}

// Taint on fields of a struct.
struct XYStruct xy = {2, 3, 11};
scanf("%f", &xy.y);
scanf("%f", &xy.x);
int tx = xy.x; // expected-warning {{tainted}}
int ty = xy.y; // FIXME: This should be tainted as well.
char ntz = xy.z;// no warning
}

0 comments on commit 5fc7def

Please sign in to comment.