This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Similar documents
C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

25.2 Opening and Closing a File

Input/Output and the Operating Systems

Stream Model of I/O. Basic I/O in C

File IO and command line input CSE 2451

File Access. FILE * fopen(const char *name, const char * mode);

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Getting Started. Project 1

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

Pointers, Arrays, and Strings. CS449 Spring 2016

Dynamic memory allocation

Lab # 4. Files & Queues in C

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Computer Programming Unit v

Course organization. Course introduction ( Week 1)

ECE551 Midterm Version 1

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

Memory Management. CSC215 Lecture

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

LSN 3 C Concepts for OS Programming

System Programming. Standard Input/Output Library (Cont d)

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

File Handling. Reference:

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

System Software Experiment 1 Lecture 7

CS240: Programming in C

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

ECE551 Midterm Version 1

Lecture 05 Pointers ctd..

CSC 1107: Structured Programming

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

2009 S2 COMP File Operations

CSC 1107: Structured Programming

Chapter 14 - Advanced C Topics

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

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

C mini reference. 5 Binary numbers 12

Exercise Session 3 Systems Programming and Computer Architecture

Standard File Pointers

Standard I/O in C, Computer System and programming in C

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

8. Characters, Strings and Files

EL2310 Scientific Programming

EM108 Software Development for Engineers

ECE551 Midterm Version 2

File I/O. Arash Rafiey. November 7, 2017

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

BİL200 TUTORIAL-EXERCISES Objective:

CSE 124 Discussion (10/3) C/C++ Basics

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

211: Computer Architecture Summer 2016

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

1. We have a code sequence that is potentially executed twice.

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

CS240: Programming in C. Lecture 14: Errors

C-Refresher: Session 10 Disk IO

Summer June 15, 2010

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

C programming basics T3-1 -

CSI 402 Lecture 2 Working with Files (Text and Binary)

Jacobs University Bremen February 7 th, 2017 Dr. Kinga Lipskoch. Practice Sheet. First Name:... Last Name:... Matriculation Number:...

ECE 551D Spring 2018 Midterm Exam

Subject: Fundamental of Computer Programming 2068

EL2310 Scientific Programming

Introduction to C Programming (Part C) Copyright 2008 W. W. Norton & Company. All rights Reserved

Dynamic memory allocation (malloc)

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

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

ECE 551D Spring 2018 Midterm Exam

C Programming 1. File Access. Goutam Biswas. Lect 29

Input / Output Functions

HIGH LEVEL FILE PROCESSING

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

Midterm Review. What makes a file executable? Dr. Jack Lange 2/10/16. In UNIX: Everything is a file

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

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

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Chapter 10. The UNIX System Interface

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

C for C++ Programmers

EE458 - Embedded Systems Lecture 4 Embedded Devel.

Computer Programming Unit 3

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Transcription:

Malicious Code Insertion Example This code has a bug that allows a hacker to take control of its execution and run evilfunc(). #include <stdio.h> // obviously it is compiler dependent // but on my system this function is // located at address x300e (12302 decimal) void evilfunc() printf("evil! EVIL!\n"); int getsum() int nums[3]; // this block ensures that the nums // array is the first local variable // in the stackframe. int i = 0; int sum = 0; int input = 0; printf("enter up to 3 integers. Type 0 to quit.\n"); /* scanf gets an integer that the user enters */ scanf("%d", &input); while(input!= 0) nums[i] = input; printf("entered %d\n", nums[i]); sum = sum + nums[i]; i++; scanf("%d", &input); return sum; int main() int result = getsum(); printf("sum is %d", result);

Mike's IO Functions Cheat Sheet Regular IO Functions FILE *fopen(const char *FILE, const char *MODE) open a file. Check man fopen for modes. int fclose(file *fp) close an open file. Zero return means no error. int putchar(int CH) write a character to stdout int putc(int CH, FILE *FP) write a character to the file int getchar() read a character from stdin. Returns EOF if at the end of the file (check for this). int getc(file *FP) read a character from the file. Returns EOF if at the end of the file (check for this). char *fgets(char *s, int size, FILE *stream) - reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer. This is usually the preferred method for reading in string data. Formatted IO Functions int printf(const char *format,...); int fprintf(file *stream, const char *format,...); int sprintf(char *str, const char *format,...); int scanf(const char *format,...); int fscanf(file *stream, const char *format,...); int sscanf(const char *str, const char *format,...); Example #include <stdio.h> int main() /* open testfile.txt for reading and appending */ char *filename; FILE *myfile; char inputbuffer[10]; int intone; filename = "testfile.txt"; myfile = fopen(filename,"a+"); if(myfile == NULL) printf("could not open %s\n", filename); return(1); /* read up to 9 characters from the user, or to a newline */ fgets(inputbuffer, 10, stdin); /* write what was read to the file */ if(fprintf(myfile, "%s", inputbuffer) < 0) /* error handling. We'll close the file and quit. */ fclose(myfile); return(1); /* read what we wrote to the file or to a newline*/ fgets(inputbuffer, 10, myfile); printf("read %s from file\n", inputbuffer); /* try to parse an int out of the string we read */ if(sscanf(inputbuffer, "%d", &intone) == 1) /* we know an integer was sucessfuly read */ printf("read int %d\n", intone); fclose(myfile); Print formatted output to stdout/a file/a string respectively. Check man 3 printf for details. Reads data from stdin/a file/a string respectively. Has built in conversions for things like integers and floats. Returns the number of successfully matched items. Check man 3 scanf for details.

Mike's Varargs Cheat Sheet How to Write Your Varargs Function 1. #include <stdarg.h> 2. Decide how you want to know when you have processed all the arguments. You can pass along a parameter that tells you how many arguments to expect (either just a int or a fancy string like printf uses). You can also have a special stopping value like NULL that tells you you're done. 3. Write your header like int myfunc(int var1, char* var2,...) 4. Declare a va_list variable: va_list myargs; 5. Pass that variable to va_start along with the last normal parameter of your function: va_start(myargs, var2); 6. As long as you still need more parameters, you use va_arg and pass along the parameters type: myparameterint = va_arg(myargs, int); 7. When you know you're finished you call va_end: va_end(myargs); Example /* prints any number of string parameters on different lines */ void printonlines(int numberofitems,...) va_list args; int i; va_start(args, numberofitems); for(i = 0; i < numberofitems; i++) char * currentstring = va_arg(args, char *); printf("%s\n", currentstring); va_end(args); int main() printonlines(3, "Hello", "World","Varargs");

Mike's Malloc Realloc & Free Cheat Sheet void *malloc(size_t size) - allocates size bytes and returns a pointer to the allocated memory. If not enough memory is available, malloc will return null. You always must check for this. void free(void *ptr) frees memory that was allocated with malloc (or realloc). If the pointer you pass was not allocated with malloc or has already been freed once, free may corrupt memory in unpredictable ways. void *realloc(void *ptr, size_t size) allocates a new chunk of memory and copies everything that exists in the old location to the new one (this allows you to in essence extend a chunk of memory you already malloced). Returns a pointer to the new memory location, and frees the old memory location. If not enough memory is available, realloc will return null but the old memory will not be freed. You must always check for this. Sample code: #include <stdlib.h> /* dynamically allocates an array of ints */ int mallocsample(int n) int i; int *ip; int *tmp; /* first we allocate n ints */ if((ip = malloc(n*sizeof(*ip))) == NULL) /* Handle Error Here */ /* we initialize all those ints to zero */ for(i = 0; i < n; i++) ip[i] = 0; /* if we discover we need a little more space we can use realloc. Lets expand to 10 more ints than we did before */ if((tmp = malloc((n+10)*sizeof(*ip))) == NULL) /* Handle Error Here. Bear in mind that ip is not freed if there is an error. So if we want to free it we must say... */ free(ip); /* more error code here */ else /* tmp now points to our new data. ip has been freed. So we can say... */ ip = tmp; /* do some more stuff with ip, maybe initialize those 10 new values */ /* eventually we must free the memory we've allocated or its a memory leak. */ free(ip);

Kitten Explosion Exercise 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.