When a variable is assigned a tainted value or is modified by it, it becomes tainted. Given below are the examples of taint propagation.
Example1:
int sum(int x, int y) // Assumption: x and y are tainted.
{
int c=0; //c is untainted
c=x+y; //c gets modified by tainted values hence c is tainted here
return c; //c is tainted
}
Example2:
int sum(int x, int y) // Assumption: x and y are tainted.
{
int c=0; //c is untainted
c=x+y; //c gets modified by tainted values hence c is tainted here
c=5; // c is assigned a value hence c is untainted
return c; //c is untainted
}
Example3:
int sum(int x, int y) // Assumption: x and y are tainted.
{
d=5; //d is untainted
int c=0; //c is untainted
c=x+y; //c gets modified by tainted values hence c is tainted here
c=5; // c is assigned an untainted value hence c is untainted
return c; //c is untainted
}
Example4:
int sum(int x, int y) // Assumption: x and y are tainted.
{
x=4; y=5; //x and y are untainted
int c=0; //c is untainted
c=x+y; //c gets modified by untainted values hence c is untainted here
return c; //c is untainted
}
Example5:
int sum(int x, int y) // Assumption: x and y are tainted.
{
int c=0; //c is untainted
if(x>y)
{
c=x; //c gets modified by tainted values hence c is tainted here
}
else
{
c=2;// c is assigned an untainted value hence c is untainted
}
return c; //c is tainted
}
If a variable gets a tainted value from any path, it becomes tainted.