summaryrefslogtreecommitdiff
path: root/utilities
diff options
context:
space:
mode:
authorMike Pattrick <mkp@redhat.com>2021-11-30 11:20:53 -0500
committerIlya Maximets <i.maximets@ovn.org>2022-01-04 19:14:11 +0100
commit0d1ffb77560fdbb96bded347fad59abd5798bb29 (patch)
treeba8641036aa01bd84ba14e2b513e042bd7954b35 /utilities
parent428b11caa75aae6da3a72e693fdcd287f5f8706c (diff)
downloadopenvswitch-0d1ffb77560fdbb96bded347fad59abd5798bb29.tar.gz
checkpatch: Detect "trojan source" attack.
Recently there has been a lot of press about the "trojan source" attack, where Unicode characters are used to obfuscate the true functionality of code. This attack didn't effect OVS, but adding the check here will help guard against it sneaking in later. Signed-off-by: Mike Pattrick <mkp@redhat.com> Acked-by: Gaetan Rivet <grive@u256.net> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Diffstat (limited to 'utilities')
-rwxr-xr-xutilities/checkpatch.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index caf10537b..8c7faa419 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -181,6 +181,7 @@ __regex_added_doc_rst = re.compile(
__regex_empty_return = re.compile(r'\s*return;')
__regex_if_macros = re.compile(r'^ +(%s) \([\S]([\s\S]+[\S])*\) { +\\' %
__parenthesized_constructs)
+__regex_nonascii_characters = re.compile("[^\u0000-\u007f]")
skip_leading_whitespace_check = False
skip_trailing_whitespace_check = False
@@ -294,6 +295,11 @@ def pointer_whitespace_check(line):
return __regex_ptr_declaration_missing_whitespace.search(line) is not None
+def nonascii_character_check(line):
+ """Return TRUE if inappropriate Unicode characters are detected """
+ return __regex_nonascii_characters.search(line) is not None
+
+
def cast_whitespace_check(line):
"""Return TRUE if there is no space between the '()' used in a cast and
the expression whose type is cast, i.e.: '(void *)foo'"""
@@ -566,6 +572,11 @@ checks = [
lambda: print_error("Inappropriate spacing in pointer declaration")},
{'regex': r'(\.c|\.h)(\.in)?$', 'match_name': None,
+ 'check': lambda x: nonascii_character_check(x),
+ 'print':
+ lambda: print_error("Inappropriate non-ascii characters detected.")},
+
+ {'regex': r'(\.c|\.h)(\.in)?$', 'match_name': None,
'prereq': lambda x: not is_comment_line(x),
'check': lambda x: cast_whitespace_check(x),
'print':
@@ -943,7 +954,7 @@ def ovs_checkpatch_print_result():
def ovs_checkpatch_file(filename):
try:
- mail = email.message_from_file(open(filename, 'r'))
+ mail = email.message_from_file(open(filename, 'r', encoding='utf8'))
except:
print_error("Unable to parse file '%s'. Is it a patch?" % filename)
return -1