Skip to content
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

Fix UBSAN failure caused by left-shift of negative number #1921

Merged
merged 4 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/codeql-queries/exiv2-cpp-queries/signed_shift.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @name Signed shift
* @description Shifting a negative number is undefined behavior,
* so it is risky to shift a signed number.
* @kind problem
* @problem.severity warning
* @id cpp/signed-shift
* @tags security
* external/cwe/cwe-758
*/

// See the "Bitwise shift operators" section here:
// https://en.cppreference.com/w/cpp/language/operator_arithmetic
import cpp
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis

from BinaryBitwiseOperation shift, Expr lhs
where
(shift instanceof LShiftExpr or shift instanceof RShiftExpr) and
lhs = shift.getLeftOperand().getFullyConverted() and
lowerBound(lhs) < 0
select shift,
"This signed shift could cause undefined behavior if the value is negative. Type of lhs: " +
lhs.getType().toString()
2 changes: 1 addition & 1 deletion src/pentaxmn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ namespace Exiv2 {
std::ostream& PentaxMakerNote::printDate(std::ostream& os, const Value& value, const ExifData*)
{
/* I choose same format as is used inside EXIF itself */
os << ((value.toLong(0) << 8) + value.toLong(1));
os << ((static_cast<uint64_t>(value.toLong(0)) << 8) + value.toLong(1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uint32_t doesn't cut it here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually uint16_t is probably sufficient for the intended purpose here. I think this is probably a similar scenario to #1706. The value is supposed to be a byte, but there's no validation on that, so a malicious file can contain a long instead. For the valid cases, we only need to extract the byte.

I'll change it.

os << ":";
os << std::setw(2) << std::setfill('0') << value.toLong(2);
os << ":";
Expand Down
Binary file added test/data/issue_1920_poc.tiff
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/bugfixes/github/test_issue_1920.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-

from system_tests import CaseMeta, path, check_no_ASAN_UBSAN_errors

class PentaxMakerNotePrintTimeSignedLeftShift(metaclass=CaseMeta):
"""
Regression test for the bug described in:
https://github.com/Exiv2/exiv2/issues/1920
"""
url = "https://github.com/Exiv2/exiv2/issues/1920"

filename = path("$data_path/issue_1920_poc.tiff")
commands = ["$exiv2 -q -Pt $filename"]
stderr = [""]
retval = [0]

compare_stdout = check_no_ASAN_UBSAN_errors