Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming...

18
Safety in the C programming Language Peter Wihl May 26 th , 2005 CS 297 Security and Programming Languages

Transcript of Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming...

Page 1: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

Safety in the C programming Language

Peter WihlMay 26th, 2005

CS 297 Security and Programming Languages

Page 2: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

Overall Issue: Safety in C

• Best feature of C:– Gives programmer access to the lowest levels

of the machine

• Worst feature of C:– Gives programmer access to the lowest levels

of the machine

Page 3: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

The Problem of Memory Manipulation

• Bad Pointer Arithmetic

• Defining the end of a string, the NULL termination

• Trespassing: When a pointer goes out of its bounds

• “The design of the C programming language encourages programming at the edge of safety.” –A1

Page 4: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

The Band Aid Approach

• Create guidelines for the use of the existing language

• Examples:– DECOS: Dependable Embedded Components and

Systems used in Europe and designed by comity – DOE-STD-1172-2003: Safety Software Quality

guidelines for Nuclear Facilities– NASA C Programming Style Guide: From Goddard

Space Flight Center– MISRA: Motor Industry Software Reliability

Association

Page 5: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

The Next Approach

• Create a modification of the C language– Cyclone– CCured

Page 6: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

Cyclone

• Automatically insert run-time NULL checks when pointers are used

• Defined two new types of pointers:– Never-NULL pointer

• ‘@’ instead of ‘*’

– Fat pointer• ‘?’ instead of ‘*’• permits pointer arithmetic• ?-pointer represented by an address + bounds

Page 7: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

Cyclone

• Uninitialized pointers: Static analysis to detect them

• Dangling pointers: to prevent dereferencing of a dangling pointer it performs a “region analysis” on the code.

• Freeing memory:– “growable regions” lives on the heap and are

accessed though handles.• Tagged Unions: used to control type-varying

arguments, the tags distinguish the cases of the unions to know which types are being used in a particular call.

Page 8: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

CCured

• Deals only with pointers

• Classifies them in two groups:

• Statically typed pointers

• Dynamically-typed pointers

Page 9: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

CCured

• Defines two types classes of pointers: Static and dynamic

• CCured does not allow these two pointer conditions.– Cannot have both a dynamically-typed and a statically

typed pointer pointing to the same location– Cannot have a statically type pointer stored in an area

pointed to by a dynamic pointer

• Deallocation is handled though built in garbage collection

Page 10: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

CCured: Statically Typed Pointer

• The SEQ (“sequence”) pointer– Can be used in pointer arithmetic but are

required to carry bounds

• The SAFE pointer– Can be NULL but does not allow for pointer

arithmetic

Page 11: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.
Page 12: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

CCured: Dynamically Typed Pointer

• DYN pointer

• Contains two fields, the base and the pointer field

• Base field points to the start of a dynamically typed area that is processed by a length and followed by tag bits

Page 13: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.
Page 14: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

Possible Problems With These Solutions

• Application level programming vs. system level programming

• Manually setting the address of a data pointer

• Needed for Memory mapped I/O

• Separating regions of code in systems with no OS

Page 15: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

An example

• You are writing code for an embedded system with no OS and limited run time environment

• System architecture has two memory maps, boot time and run time.

• Build two separate execution regions:

• Boot and Main

Page 16: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

Example (continued)

• …..

• void *Jump(void);

• Jump = 0;

• Jump();

• What am I doing here?!?! This is evil code!

• (it was written by Justin R. Cutler )

Page 17: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

Example (continued)

• This is a soft reset that jumps out of Boot code and goes to the start of Main that is now at address location 0x000000

• Would this be allowed by Cyclone or CCured? Something to talk about or maybe not.

Page 18: Safety in the C programming Language Peter Wihl May 26 th, 2005 CS 297 Security and Programming Languages.

References

• Software Safety Home Page:– http://www.softwaresafety.net/Guidelines/