Headline
CVE-2021-34334: Extra checking to prevent loop counter from wrapping around by kevinbackhouse · Pull Request #1766 · Exiv2/exiv2
Exiv2 is a command-line utility and C++ library for reading, writing, deleting, and modifying the metadata of image files. An infinite loop is triggered when Exiv2 is used to read the metadata of a crafted image file. An attacker could potentially exploit the vulnerability to cause a denial of service, if they can trick the victim into running Exiv2 on a crafted image file. The bug is fixed in version v0.27.5.
I used two CodeQL queries to find loop conditions similar to the original bug. Both queries have quite a few false positives, so I ignored some of the results.
The first query looks for loop conditions of the form a < b where the value of b could be larger than the type of a is capable of representing. For example: a is a uint16_t and b is a size_t.
import cpp
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
from Loop loop, RelationalOperation cmp, Expr a, Expr b, LocalVariable v
where loop.getCondition().getAChild*() = cmp
and a = cmp.getLesserOperand()
and b = cmp.getGreaterOperand()
and exprMaxVal(a) < upperBound(b.getFullyConverted())
and a.getAChild*() = v.getAnAccess()
select cmp, a, b
The second query looks for loop conditions that do arithmetic in the comparison. For example: a < size-10.
import cpp
// Find loop conditions like this: `while (a < size-10)`.
// Conditions like that are often an overflow risk.
from Loop loop, RelationalOperation cmp, BinaryArithmeticOperation binop
where
loop.getCondition().getAChild*() = cmp and
binop = cmp.getAnOperand() and
not binop.isConstant() and
// Ignore results in the standard libraries and in the xmpsdk subdirectory.
exists (string path |
path = cmp.getLocation().getFile().getRelativePath() and
not path.matches("xmpsdk/%"))
select binop, "Arithmetic in loop condition."
Related news
Gentoo Linux Security Advisory 202312-6 - Multiple vulnerabilities have been discovered in Exiv2, the worst of which can lead to remote code execution. Versions greater than or equal to 0.28.1 are affected.