Lecture 9 Assertions and Error Handling CS240

Similar documents
CSE 565 Computer Security Fall 2018

Program Security and Vulnerabilities Class 2

COSC Software Engineering. Lecture 16: Managing Memory Managers

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Fundamentals of Computer Security

Undefined Behaviour in C

UMSSIA LECTURE I: SOFTWARE SECURITY

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Lecture 4 September Required reading materials for this class

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Lecture 04 Introduction to pointers

SoK: Eternal War in Memory

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CSE 333 Midterm Exam Sample Solution 5/10/13

DAY 3. CS3600, Northeastern University. Alan Mislove

One-Slide Summary. Lecture Outline. Language Security

CS 261 Fall Mike Lam, Professor. Structs and I/O

CSE 303: Concepts and Tools for Software Development

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CSCE 548 Building Secure Software Buffer Overflow. Professor Lisa Luo Spring 2018

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

C Introduction. Comparison w/ Java, Memory Model, and Pointers

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

For example, let s say we define an array of char of size six:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Computer Programming: Skills & Concepts (CP) Strings

CS 645: Lecture 3 Software Vulnerabilities. Rachel Greenstadt July 3, 2013

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

(Early) Memory Corruption Attacks

Stanford University Computer Science Department CS 295 midterm. May 14, (45 points) (30 points) total

TI2725-C, C programming lab, course

Static Vulnerability Analysis

Software Security: Vulnerability Analysis

Introducing C++ to Java Programmers

8. Characters, Strings and Files

a data type is Types

Runtime Defenses against Memory Corruption

Memory Corruption 101 From Primitives to Exploit

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Language Security. Lecture 40

Lectures 5-6: Introduction to C

Module: Safe Programming. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

CSCE 548 Building Secure Software Integers & Integer-related Attacks & Format String Attacks. Professor Lisa Luo Spring 2018

CSE 333 Midterm Exam July 24, 2017 Sample Solution

Verification & Validation of Open Source

Foundations of Network and Computer Security

Technical Questions. Q 1) What are the key features in C programming language?

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

The Preprocessor. CS 2022: Introduction to C. Instructor: Hussam Abu-Libdeh. Cornell University (based on slides by Saikat Guha) Fall 2011, Lecture 9

Pointers, Arrays, and Strings. CS449 Spring 2016

CSE 333 Midterm Exam July 24, Name UW ID#

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

We will focus on Buffer overflow attacks SQL injections. See book for other examples

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Basic Buffer Overflows

Kurt Schmidt. October 30, 2018

CSE 565 Computer Security Fall 2018

Information Security CS 526

CSE 333 Midterm Exam 5/10/13

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

CSC 2400: Computer Systems. Using the Stack for Function Calls

HW1 due Monday by 9:30am Assignment online, submission details to come

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Dynamic Allocation in C

Lecture 8 Dynamic Memory Allocation

Overview AEG Conclusion CS 6V Automatic Exploit Generation (AEG) Matthew Stephen. Department of Computer Science University of Texas at Dallas

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Allocation in C

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

More C Pointer Dangers

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Lectures 5-6: Introduction to C

Systems Programming and Computer Architecture ( )

CSC 8400: Computer Systems. Using the Stack for Function Calls

Foundations of Network and Computer Security

Secure Programming I. Steven M. Bellovin September 28,

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

CYSE 411/AIT681 Secure Software Engineering Topic #10. Secure Coding: Integer Security

2/9/18. Readings. CYSE 411/AIT681 Secure Software Engineering. Introductory Example. Secure Coding. Vulnerability. Introductory Example.

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Model Viva Questions for Programming in C lab

PRINCIPLES OF OPERATING SYSTEMS

Buffer overflows (a security interlude) Address space layout the stack discipline + C's lack of bounds-checking HUGE PROBLEM

CS2141 Software Development using C/C++ Debugging

Identifying Memory Corruption Bugs with Compiler Instrumentations. 이병영 ( 조지아공과대학교

Transcription:

Lecture 9 Assertions and Error Handling CS240

The C preprocessor The C compiler performs Macro expansion and directive handling Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands macros Example: Specify how you want to macro expand by specifying the DEBUG variable at compilation time in the Makefile gcc -D option #ifdef DEBUG #define DPRINT(s) fprintf(stderr, %s\n,s) #else #define DPRINT(s) #endif

The C preprocessor The C compiler performs Macro expansion and directive handling Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands macros DPRINT( makestring( error, CAUSE) ); Question: Is function makestring() called? Shouldn t we have some cutesy pictures that make the slides look cool and humorous?

How can this code fail? #include <stdio.h> int main() { int a, b, c; a = 10; b = getchar() - 48; c = a/b; } return 0; No! Go Away!

Common Software Vulnerabilities 5 Buffer overflows Input validation Format string problems Integer overflows Failing to handle errors Other exploitable logic errors

Weak Types and Errors 6 Would strong typing prevent these kinds of vulnerabilities? What kind of errors do type systems typically catch? - Structural violations: think of types as sets Not all elements in a set are sensible in all contexts Think of buffers as arrays - Buffer overflow arises when arrays of different sizes than expected are constructed. Similar reasoning for overflow and underflow Failure to enforce temporal and logical relations

Is C more vulnerable than... 7 Weak typing means data can be interpreted in multiple ways This can lead to errors A single datum associated with multiple types Memory can be indexed arbitrarily, beyond the range(s) of declared arrays and structures Overwrite stack contents Dangling pointers Pass a pointer to an object allocated locally within a function to the function s caller

What is a Buffer Overflow? 8 Buffer overflow occurs when a program or process tries to store more data in a buffer than the buffer can hold Very dangerous because the extra information may: Affect user s data Affect user s code Affect system s data Affect system s code

Why Buffer Overflows? 9 No check on boundaries Programming languages give user too much control Programming languages have unsafe functions Users do not write safe code C and C++, are more vulnerable because they provide no built-in protection against accessing or overwriting data in any part of memory Can t know the lengths of buffers from a pointer No guarantees strings are null terminated

Why Buffer Overflow Matter 10 Overwrites: other buffers variables program flow data Results in: erratic program behavior a memory access exception program termination incorrect results breach of system security Wazzup?

Example Suppose a web server contains a function: void f(char *str) { char buf[128]; strcpy(buf, str); do(buf); } When the function is invoked the stack looks like: buf sfp ret-addr str top of stack What if *str is 136 bytes long? After strcpy: *str ret str top of stack

Some Unsafe C lib Functions strcpy (char *dest, const char *src) strcat (char *dest, const char *src) gets (char *s) scanf ( const char *format, ) printf (conts char *format, ) Should I be scared, or what?

Assertions int main() { int a, b, c; a = 10; b = some_function_computes_something(); } c = a/b; return 0; Something is missing here...

Assertions int main() { int a, b, c; a = 10; b = some_function_computes_something(); assert(b!=0); c = a/b; return 0; } That s b.b.b.b.better..

Assertions Used to help specify programs and to reason about program correctness. precondition an assertion placed at the beginning of a section of code determines the set of states under which the code is expected to be executed. postcondition placed at the end describes the expected state at the end of execution. #include <assert.h> assert (predicate);

Examples (assert b!=0); c = a/b At the end of a function, if you know you should return success assert(ret == SUCCES);

How can this code fail? #include <stdio.h> #define MAX 10 char* f(char s[]); int main() { char str[max]; char *ptr=f(str); } printf("%c\n", *ptr); return 0; char *my_function(char s[]) { char *p = NULL; /* does stuff*/ return p; } It s the NULLity of things

How can this code fail? #include <stdio.h> #include <assert.h> #define MAX 10 char* f(char s[]); int main() { char str[max]; char *ptr=f(str); assert(ptr!=null); printf("%c\n", *ptr); return 0; } char *my_function(char s[]) { char *p = NULL; /* does stuff*/ return p; } All good now..

What to think about/check for Null pointer dereference Use after free Double free Array indexing errors Mismatched array new/delete Potential stack/heap overrun Return pointers to local variables Logically inconsistent code Uninitialized variables Invalid use of negative values Passing large parameters by value Under allocations of dynamic data Memory leaks File handle leaks Unhandled return codes Use of invalid iterators