2017-06-04 22:51:48 +02:00
|
|
|
#!/usr/bin/env python
|
2015-08-18 16:14:53 +02:00
|
|
|
#
|
|
|
|
# Cert: Some extra CERT checkers
|
|
|
|
#
|
|
|
|
# Cppcheck itself handles many CERT rules. Cppcheck warns when there is undefined behaviour.
|
2018-03-16 08:12:39 +01:00
|
|
|
# CERT Homepage: https://www.cert.org/secure-coding/
|
2015-08-18 16:14:53 +02:00
|
|
|
#
|
|
|
|
# Example usage of this addon (scan a sourcefile main.cpp)
|
|
|
|
# cppcheck --dump main.cpp
|
|
|
|
# python cert.py main.cpp.dump
|
|
|
|
|
|
|
|
import cppcheckdata
|
|
|
|
import sys
|
2015-08-28 18:07:12 +02:00
|
|
|
import re
|
2015-08-21 10:55:19 +02:00
|
|
|
|
2017-10-15 16:18:29 +02:00
|
|
|
VERIFY = ('-verify' in sys.argv)
|
|
|
|
VERIFY_EXPECTED = []
|
|
|
|
VERIFY_ACTUAL = []
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2017-08-15 20:34:54 +02:00
|
|
|
def reportError(token, severity, msg, id):
|
2017-10-15 16:18:29 +02:00
|
|
|
if VERIFY:
|
|
|
|
VERIFY_ACTUAL.append(str(token.linenr) + ':' + id)
|
|
|
|
else:
|
|
|
|
sys.stderr.write(
|
|
|
|
'[' + token.file + ':' + str(token.linenr) + '] (' + severity + '): ' + msg + ' [' + id + ']\n')
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2017-10-14 22:24:24 +02:00
|
|
|
def simpleMatch(token, pattern):
|
|
|
|
for p in pattern.split(' '):
|
|
|
|
if not token or token.str != p:
|
|
|
|
return False
|
|
|
|
token = token.next
|
|
|
|
return True
|
2015-08-21 10:55:19 +02:00
|
|
|
|
2018-01-23 17:18:47 +01:00
|
|
|
def isUnpackedStruct(token):
|
|
|
|
if token.valueType is None:
|
|
|
|
return False
|
|
|
|
if token.valueType.typeScope is None:
|
2018-08-05 20:36:21 +02:00
|
|
|
return False
|
2018-01-23 17:18:47 +01:00
|
|
|
if token.valueType.typeScope.type != "Struct":
|
|
|
|
return False
|
2018-04-28 23:06:54 +02:00
|
|
|
startToken = token.valueType.typeScope.bodyStart
|
2018-01-23 17:18:47 +01:00
|
|
|
|
|
|
|
linenr = int(startToken.linenr)
|
|
|
|
for line in open(startToken.file):
|
|
|
|
linenr -= 1
|
|
|
|
if linenr == 0:
|
|
|
|
return True
|
|
|
|
if linenr < 3 and re.match(r'#pragma\s+pack\s*\(', line):
|
|
|
|
return False
|
|
|
|
return True
|
2015-08-28 18:07:12 +02:00
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2015-08-28 18:07:12 +02:00
|
|
|
def isLocalUnpackedStruct(arg):
|
2015-08-19 10:21:25 +02:00
|
|
|
if arg and arg.str == '&' and not arg.astOperand2:
|
|
|
|
arg = arg.astOperand1
|
2018-01-23 17:18:47 +01:00
|
|
|
return arg and arg.variable and (arg.variable.isLocal or arg.variable.isArgument) and isUnpackedStruct(arg)
|
2015-08-21 10:55:19 +02:00
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
|
2015-08-18 16:14:53 +02:00
|
|
|
def isBitwiseOp(token):
|
2017-06-04 22:51:48 +02:00
|
|
|
return token and (token.str in {'&', '|', '^'})
|
2015-08-18 16:14:53 +02:00
|
|
|
|
2015-08-21 10:55:19 +02:00
|
|
|
|
2015-08-18 16:14:53 +02:00
|
|
|
def isComparisonOp(token):
|
2017-06-04 22:51:48 +02:00
|
|
|
return token and (token.str in {'==', '!=', '>', '>=', '<', '<='})
|
2015-08-18 16:14:53 +02:00
|
|
|
|
2017-10-14 22:24:24 +02:00
|
|
|
def isCast(expr):
|
|
|
|
if not expr or expr.str != '(' or not expr.astOperand1 or expr.astOperand2:
|
|
|
|
return False
|
|
|
|
if simpleMatch(expr, '( )'):
|
|
|
|
return False
|
|
|
|
return True
|
2015-08-19 10:21:25 +02:00
|
|
|
|
2018-04-03 13:35:19 +02:00
|
|
|
# Get function arguments
|
|
|
|
def getArgumentsRecursive(tok, arguments):
|
|
|
|
if tok is None:
|
|
|
|
return
|
|
|
|
if tok.str == ',':
|
|
|
|
getArgumentsRecursive(tok.astOperand1, arguments)
|
|
|
|
getArgumentsRecursive(tok.astOperand2, arguments)
|
|
|
|
else:
|
2018-08-05 20:36:21 +02:00
|
|
|
arguments.append(tok)
|
|
|
|
|
2018-04-03 13:35:19 +02:00
|
|
|
|
|
|
|
def getArguments(ftok):
|
|
|
|
arguments = []
|
|
|
|
getArgumentsRecursive(ftok.astOperand2, arguments)
|
|
|
|
return arguments
|
|
|
|
|
|
|
|
# EXP05-C
|
|
|
|
# do not attempt to cast away const
|
|
|
|
def exp05(data):
|
2018-04-03 15:12:01 +02:00
|
|
|
# TODO Reuse code in misra rule 11.8
|
2018-04-03 13:35:19 +02:00
|
|
|
for token in data.tokenlist:
|
|
|
|
if isCast(token):
|
|
|
|
# C-style cast
|
|
|
|
if not token.valueType:
|
|
|
|
continue
|
|
|
|
if not token.astOperand1.valueType:
|
2018-04-03 15:12:01 +02:00
|
|
|
continue
|
|
|
|
if token.valueType.pointer == 0:
|
|
|
|
continue
|
|
|
|
if token.astOperand1.valueType.pointer == 0:
|
|
|
|
continue
|
2018-04-03 13:35:19 +02:00
|
|
|
const1 = token.valueType.constness
|
|
|
|
const2 = token.astOperand1.valueType.constness
|
|
|
|
if (const1 % 2) < (const2 % 2):
|
|
|
|
reportError(token, 'style', "Attempt to cast away const", 'cert-EXP05-C')
|
|
|
|
|
|
|
|
elif token.str == '(' and token.astOperand1 and token.astOperand2 and token.astOperand1.function:
|
|
|
|
function = token.astOperand1.function
|
|
|
|
arguments = getArguments(token)
|
|
|
|
for argnr, argvar in function.argument.items():
|
|
|
|
if argnr < 1 or argnr > len(arguments):
|
|
|
|
continue
|
2018-04-03 15:12:01 +02:00
|
|
|
if not argvar.isPointer:
|
|
|
|
continue
|
2018-04-12 20:23:50 +02:00
|
|
|
if (argvar.constness % 2) == 1: # data is const
|
|
|
|
continue
|
2018-04-03 13:35:19 +02:00
|
|
|
argtok = arguments[argnr - 1]
|
|
|
|
if not argtok.valueType:
|
|
|
|
continue
|
2018-04-03 15:12:01 +02:00
|
|
|
if argtok.valueType.pointer == 0:
|
|
|
|
continue
|
2018-04-03 13:35:19 +02:00
|
|
|
const2 = arguments[argnr - 1].valueType.constness
|
2018-04-12 20:23:50 +02:00
|
|
|
if (const2 % 2) == 1:
|
2018-04-03 13:35:19 +02:00
|
|
|
reportError(token, 'style', "Attempt to cast away const", 'cert-EXP05-C')
|
|
|
|
|
|
|
|
|
2015-08-19 10:21:25 +02:00
|
|
|
# EXP42-C
|
|
|
|
# do not compare padding data
|
|
|
|
def exp42(data):
|
|
|
|
for token in data.tokenlist:
|
2018-01-23 17:18:47 +01:00
|
|
|
if token.str != '(' or not token.astOperand1 or token.astOperand1.str != 'memcmp':
|
2015-08-19 10:21:25 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
arg1 = None
|
|
|
|
arg2 = None
|
|
|
|
if token.astOperand2 and token.astOperand2.str == ',':
|
|
|
|
if token.astOperand2.astOperand1 and token.astOperand2.astOperand1.str == ',':
|
|
|
|
arg1 = token.astOperand2.astOperand1.astOperand1
|
|
|
|
arg2 = token.astOperand2.astOperand1.astOperand2
|
|
|
|
|
2018-01-23 17:18:47 +01:00
|
|
|
if isLocalUnpackedStruct(arg1) or isLocalUnpackedStruct(arg2):
|
2015-12-14 09:37:26 +01:00
|
|
|
reportError(
|
2017-08-15 20:34:54 +02:00
|
|
|
token, 'style', "Comparison of struct padding data " +
|
2017-08-15 21:44:43 +02:00
|
|
|
"(fix either by packing the struct using '#pragma pack' or by rewriting the comparison)", 'cert-EXP42-C')
|
2017-06-04 22:51:48 +02:00
|
|
|
|
2015-08-19 10:21:25 +02:00
|
|
|
|
2015-08-18 16:14:53 +02:00
|
|
|
# EXP46-C
|
|
|
|
# Do not use a bitwise operator with a Boolean-like operand
|
|
|
|
# int x = (a == b) & c;
|
|
|
|
def exp46(data):
|
|
|
|
for token in data.tokenlist:
|
|
|
|
if isBitwiseOp(token) and (isComparisonOp(token.astOperand1) or isComparisonOp(token.astOperand2)):
|
2015-12-14 09:37:26 +01:00
|
|
|
reportError(
|
2017-08-15 21:44:43 +02:00
|
|
|
token, 'style', 'Bitwise operator is used with a Boolean-like operand', 'cert-EXP46-c')
|
2015-08-18 16:14:53 +02:00
|
|
|
|
2017-10-14 22:24:24 +02:00
|
|
|
# INT31-C
|
|
|
|
# Ensure that integer conversions do not result in lost or misinterpreted data
|
|
|
|
def int31(data, platform):
|
|
|
|
if not platform:
|
|
|
|
return
|
|
|
|
for token in data.tokenlist:
|
|
|
|
if not isCast(token):
|
|
|
|
continue
|
|
|
|
if not token.valueType or not token.astOperand1.values:
|
|
|
|
continue
|
|
|
|
bits = None
|
|
|
|
if token.valueType.type == 'char':
|
|
|
|
bits = platform.char_bit
|
|
|
|
elif token.valueType.type == 'short':
|
|
|
|
bits = platform.short_bit
|
|
|
|
elif token.valueType.type == 'int':
|
|
|
|
bits = platform.int_bit
|
|
|
|
elif token.valueType.type == 'long':
|
|
|
|
bits = platform.long_bit
|
2017-10-16 13:35:07 +02:00
|
|
|
elif token.valueType.type == 'long long':
|
|
|
|
bits = platform.long_long_bit
|
2017-10-14 22:24:24 +02:00
|
|
|
else:
|
|
|
|
continue
|
2017-10-16 13:35:07 +02:00
|
|
|
if token.valueType.sign == 'unsigned':
|
|
|
|
found = False
|
|
|
|
for value in token.astOperand1.values:
|
|
|
|
if value.intvalue < 0:
|
|
|
|
found = True
|
|
|
|
reportError(
|
|
|
|
token,
|
|
|
|
'style',
|
|
|
|
'Ensure that integer conversions do not result in lost or misinterpreted data (casting ' + str(value.intvalue) + ' to unsigned ' + token.valueType.type + ')',
|
|
|
|
'cert-INT31-c')
|
|
|
|
break
|
|
|
|
if found:
|
|
|
|
continue
|
2017-10-14 22:24:24 +02:00
|
|
|
if bits >= 64:
|
|
|
|
continue
|
2017-10-16 13:35:07 +02:00
|
|
|
minval = 0
|
|
|
|
maxval = 1
|
|
|
|
if token.valueType.sign == 'signed':
|
|
|
|
minval = -(1 << (bits - 1))
|
|
|
|
maxval = ((1 << (bits - 1)) - 1)
|
|
|
|
else:
|
|
|
|
minval = 0
|
|
|
|
maxval = ((1 << bits) - 1)
|
2017-10-14 22:24:24 +02:00
|
|
|
for value in token.astOperand1.values:
|
2017-10-16 13:35:07 +02:00
|
|
|
if value.intvalue < minval or value.intvalue > maxval:
|
|
|
|
destType = ''
|
|
|
|
if token.valueType.sign:
|
|
|
|
destType = token.valueType.sign + ' ' + token.valueType.type
|
|
|
|
else:
|
|
|
|
destType = token.valueType.type
|
2017-10-14 22:24:24 +02:00
|
|
|
reportError(
|
|
|
|
token,
|
|
|
|
'style',
|
2017-10-16 13:35:07 +02:00
|
|
|
'Ensure that integer conversions do not result in lost or misinterpreted data (casting ' + str(value.intvalue) + ' to ' + destType + ')',
|
2017-10-14 22:24:24 +02:00
|
|
|
'cert-INT31-c')
|
2017-10-16 13:35:07 +02:00
|
|
|
break
|
2017-10-14 22:24:24 +02:00
|
|
|
|
2015-08-18 16:14:53 +02:00
|
|
|
for arg in sys.argv[1:]:
|
2017-10-15 16:18:29 +02:00
|
|
|
if arg == '-verify':
|
|
|
|
VERIFY = True
|
|
|
|
continue
|
2015-08-21 10:55:19 +02:00
|
|
|
print('Checking ' + arg + '...')
|
|
|
|
data = cppcheckdata.parsedump(arg)
|
2017-10-15 16:18:29 +02:00
|
|
|
|
|
|
|
if VERIFY:
|
|
|
|
VERIFY_ACTUAL = []
|
|
|
|
VERIFY_EXPECTED = []
|
|
|
|
for tok in data.rawTokens:
|
|
|
|
if tok.str.startswith('//') and 'TODO' not in tok.str:
|
|
|
|
for word in tok.str[2:].split(' '):
|
2017-10-16 13:35:07 +02:00
|
|
|
if re.match(r'cert-[A-Z][A-Z][A-Z][0-9][0-9].*',word):
|
2017-10-15 16:18:29 +02:00
|
|
|
VERIFY_EXPECTED.append(str(tok.linenr) + ':' + word)
|
|
|
|
|
2015-12-14 09:37:26 +01:00
|
|
|
for cfg in data.configurations:
|
|
|
|
if len(data.configurations) > 1:
|
|
|
|
print('Checking ' + arg + ', config "' + cfg.name + '"...')
|
2018-04-03 13:35:19 +02:00
|
|
|
exp05(cfg)
|
2015-12-14 09:37:26 +01:00
|
|
|
exp42(cfg)
|
|
|
|
exp46(cfg)
|
2017-10-14 22:24:24 +02:00
|
|
|
int31(cfg, data.platform)
|
2017-10-15 16:18:29 +02:00
|
|
|
|
|
|
|
if VERIFY:
|
|
|
|
for expected in VERIFY_EXPECTED:
|
|
|
|
if expected not in VERIFY_ACTUAL:
|
|
|
|
print('Expected but not seen: ' + expected)
|
|
|
|
sys.exit(1)
|
|
|
|
for actual in VERIFY_ACTUAL:
|
|
|
|
if actual not in VERIFY_EXPECTED:
|
|
|
|
print('Not expected: ' + actual)
|
|
|
|
sys.exit(1)
|