MISRA C - Safety v Security
Fifth VDA Automotive SYS Conference
Andrew Banks
BSc IEng MIET FBCS CITP
Frazer-Nash Research Limited, and Chairman, MISRA C Working Group
MISRA C - Safety v Security Fifth VDA Automotive SYS Conference - - PowerPoint PPT Presentation
MISRA C - Safety v Security Fifth VDA Automotive SYS Conference Andrew Banks BSc IEng MIET FBCS CITP Frazer-Nash Research Limited, and Chairman, MISRA C Working Group MISRA C A Quick History 2 July 14, 2015 MISRA-C The Rationale
MISRA C - Safety v Security
Fifth VDA Automotive SYS Conference
Andrew Banks
BSc IEng MIET FBCS CITP
Frazer-Nash Research Limited, and Chairman, MISRA C Working Group
July 14, 2015 2
MISRA-C – The Rationale
Despite its popularity, there are several drawbacks with the C language, eg:
MISRA C is one solution...
July 14, 2015 3
MISRA-C – A Quick History
MISRA-C:1998 (aka MISRA-C1)
MISRA-C:2004 (aka MISRA-C2)
MISRA C:2012 (aka MISRA-C3)
July 14, 2015 4
MISRA-C – The 2012 Edition
Published early 2013 159 Guidelines in total
A compliance and deviation policy
July 14, 2015 5
MISRA-C – The Vision
The vision of MISRA C is set out in the opening paragraph of the Guidelines: The MISRA C Guidelines define a subset of the C language in which the opportunity to make mistakes is either removed or reduced. Many standards for the development of safety-related software require, or recommend, the use of a language subset, and this can also be used to develop any application with high integrity or high reliability requirements.
July 14, 2015 6
MISRA-C – Work In Progress
MISRA C:2012 Technical Corrigendum 1
MISRA Compliance
And a few other things...
July 14, 2015 7
July 14, 2015 8
Myth Busting #1
The Misunderstanding
The History
industry... and we are proud of our automotive heritage. The Reality
aerospace, space and defence. eg:
July 14, 2015 9
Myth Busting #2
The Misunderstanding
The History
The Reality
with high integrity or high reliability requirements
July 14, 2015 10
ESCAR 2014, Hamburg
July 14, 2015 11
July 14, 2015 12
MISRA C Directives – What is a Directive?
From the MISRA C:2012
description necessary to perform a check for compliance.
requirement specifications, is required in order to be able to perform the check.
directives, but different tools may place different interpretations on what constitutes non-compliance. Note: Compliance is still required – just as for the rules!
July 14, 2015 13
MISRA C Directives – Directive 4.1
From the MISRA C:2012 headline
Rationale
What does this mean?
for example in design standards, test plans and code review checklists.
errors to occur Problem areas
arithmetic/de-referencing
July 14, 2015 14
MISRA C Directives – Directive 4.11
From the MISRA C:2012 headline
Rationale
What does this mean?
errors to occur Problem areas
July 14, 2015 15
July 14, 2015 16
July 14, 2015 17
ISO/IEC TS 17961 – C Secure Coding Rules
Produced by ISO/IEC JTC 1/SC 22/WG 14 – the same people responsible for the C standard itself Originally proposed to be based on CERT-C (see later) but significantly rationalised From the document’s Background:
requirements”
secure coding rules that can be automatically enforced to detect security flaws in C-conforming applications”
July 14, 2015 18
ISO/IEC TS 17961 – C Secure Coding Coverage
Coverage Method # Comments MISRA covers fully – explicitly 22 Some rules are stricter than SecureC MISRA covers fully – broad 11 Eg: bans dynamic memory, signal.h MISRA covers fully – implicitly 6 Undefined/unspecified behaviour 3 Standard library MISRA covers partially – broad 2 MISRA does not cover directly 2 46
July 14, 2015 19
ISO/IEC TS 17961 – The Gaps
The gaps (partial or not covered) can be grouped as follows:
2 rules
1 rule
1 rule Proposal
July 14, 2015 20
The Gaps – Taintedness
C Secure
MISRA C:2012
Proposed way ahead
data to protect against taintedness.
July 14, 2015 21
The Gaps – Use of stdlib.h environment functions
C Secure
MISRA C:2012
localeconv(), setlocale() and strerror() Ideal Solution
const char * rather than straight char *
Proposed way ahead
and to prevent wrong data being used after multiple calls.
July 14, 2015 22
The Gaps – Use of sizeof() on a pointer parameter
C Secure
MISRA C:2012
Proposed way ahead
July 14, 2015 23
The Gaps – Comparison of padding data
C Secure
MISRA C:2012
Proposed way ahead
structures or unions.
character strings – use strcmp() or strncmp() instead.
July 14, 2015 24
ISO/IEC TS 17961 – The Broad Approaches
Some C Secure rules are implicitly fully covered by broad approaches
1 rule
3 rules
3 rules
4 rules
Proposal
July 14, 2015 25
The Broad – string formatting functions
C Secure
MISRA C:2012
Possible way ahead
July 14, 2015 26
The Broad – The use of EOF
C Secure
MISRA C:2012
Ideal Solution
was raised at the C99 CD2 ballot, that is not likely to happen! “Has been like this for at least 10 years, no need to change. Already known problem with too much existing practice.” Possible way ahead
EOF
July 14, 2015 27
ISO/IEC TS 17961 – The Implicit?
Many of the Secure C rules are implicitly covered by Directives
Some of these may benefit from additional, focussed, rules
1 rule
1 rules
1 rule
July 14, 2015 28
The Implicit – Use of errno
C Secure
MISRA C:2012
Possible way ahead
inappropriate use.
July 14, 2015 29
The Implicit – string copying functions
C Secure
MISRA C:2012
Possible way ahead
July 14, 2015 30
ISO/IEC TS 17961 – Revised C Secure Coverage
Coverage Method # Comments MISRA covers fully – explicitly 31 MISRA covers fully – broad approach 7 Eg: bans dynamic memory, signals MISRA covers fully – implicitly 3 Taint 5 Undefined/unspecified behaviour MISRA covers partially or not at all 46
July 14, 2015 31
ISO/IEC TS 17961 – The Next Steps
Proposed Next Steps
July 14, 2015 32
July 14, 2015 33
CERT-C – Secure Coding Standard
What is CERT-C
University.
progressed by WG14, who progressed ISO/IEC TS 17961 instead. The MISRA C Position
July 14, 2015 34
CERT-C (April 2014) – MISRA C:2012 Coverage
Coverage Method #1 #2 Comments MISRA covers – fully 36 42 MISRA covers – partially 18 22 MISRA does not cover explicitly 41 33 But many are covered by directives Possible Contradictions! 1 1 96 98
#1 Assessment presented at escrypt. #2 MISRA C Working Group assessment (MISRA C:2012 against CERT-C:Apr14)
July 14, 2015 35
CERT-C v MISRA C:2012 Contradiction?
Array bound specification on initialization with string literals
the array shall be specified explicitly.
Example Cited
// ... null-terminated string of three characters
// ... probably wrong size of array length Note: MISRA C rule R9.5 only applies to Designated Initializers
July 14, 2015 36
CERT-C v MISRA C:2012 Contradiction?
Alternative example #1
= { [0]=“a” }; // Compliant to CERT-C but not MISRA C // ... but really only a single character array?
// Compliant to MISRA C but not CERT-C // ... we really wanted 10 characters Alternative example #2
= { [0]=“abc” }; // Compliant to CERT-C but not MISRA C // ... how big should that array be?
= { [0]=“abc” }; // Compliant to MISRA C but not CERT-C // ... three characters plus null-terminator
= { [0]=“abc” }; // Compliant to MISRA C but not CERT-CC // ... three characters without null-terminator
= { [0]=“abcd” }; // Constraint error
July 14, 2015 37
CERT-C v MISRA C:2012 Contradiction?
Let me repeat: MISRA C:2012 rule R9.5 only applies to Designated Initializers Compare with advisory MISRA C:2012 rule R8.11
specified.
analysis without needing to analyse more than one translation unit.
initialization.
No other MISRA C:2012 rule requires the array size to be explicitly specified.
July 14, 2015 38
July 14, 2015 39
MISRA C – In Summary
MISRA C is
Proposed Way Ahead
July 14, 2015 40
July 14, 2015 41
I would like to acknowledge the support of the members of the MISRA C Working Group for their assistance in preparing this presentation.
July 14, 2015 42
References
MISRA C:2012
http://misra.org.uk/
Embedded Security in Cars (November 2014, Hamburg)
https://www.escar.info/history/escar-europe/escar-europe-2014-lectures-and-program-committee.html
ISO/IEC TS 17961:2013 – C secure coding rules
http://www.iso.org/iso/catalogue_detail.htm?csnumber=61134
CERT-C
https://www.securecoding.cert.org
ISO/IEC 9899 CD2 comments and decisions
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n847.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/n872.htm
July 14, 2015 43
About the speaker
Biography
embedded software systems, across a number of industries
Social Media AndrewBanks.com @AndrewBanks https://linkedin.com/in/AndrewBanks
July 14, 2015 44