1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

Size: px
Start display at page:

Download "1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:"

Transcription

1 This assignment refers to concepts discussed in the course notes on gdb and the book The Art of Debugging by Matloff & Salzman. The questions are definitely "hands-on" and will require some reading beyond the course notes. Download the file HW10.tar and unpack it on a Linux system. It contains files you will need for this assignment. You may work in pairs for this assignment. If you choose to work with a partner, make sure only one of you submits a solution and that the file lists names and PIDs for both of you. Prepare your answers to the following questions in a single plain ASCII text file. Submit your file to the Curator system by the posted deadline for this assignment. No late submissions will be accepted. You will submit your answers to the Curator System ( under the heading HW A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced: Q1: pushl %ebp # 1 movl %esp, %ebp # 2 subl $16, %esp # 3 movl 8(%ebp), %eax # 4 andl $1, %eax # 5 movl %eax, -4(%ebp) # 6 sall $31, -4(%ebp) # 7 sarl $31, -4(%ebp) # 8 movl -4(%ebp), %eax # 9 leave # 10 ret # 11 The code uses some instructions that are covered in the supplementary notes posted with the assignment; be sure to read the notes! a) [5 points] The first three instructions create the stack frame for the function. How many bytes are allocated for the frame? Justify your answer. b) [5 points] The function receives one parameter and uses one local automatic variable. Explain how we know this. c) [10 points] Assume the function is named Q1, the parameter is named P1, and the local variable is called L1. Write C code for the function that could have yielded the x86-32 assembly code given above. (Be advised: you may derive a perfectly acceptable answer and find that when you compile it you get slightly different assembly code, so don't use that as your goal.) 1

2 2. A student is testing an implementation of a different C function; when compiled with gcc, the following x86-32 assembly code is produced: Q2: pushl %ebp # 1 movl %esp, %ebp # 2 subl $16, %esp # 3 movl $0, -4(%ebp) # 4 jmp.l2 # 5.L4: # 6 movl -4(%ebp), %eax # 7 sall $2, %eax # 8 addl 8(%ebp), %eax # 9 movl (%eax), %eax # 10 cmpl 12(%ebp), %eax # 11 jg.l6 # 12.L2: # 13 movl -4(%ebp), %eax # 14 cmpl 16(%ebp), %eax # 15 jl.l4 # 16 jmp.l3 # 17.L6: # 18 nop # 19.L3: # 20 movl -4(%ebp), %eax # 21 leave # 22 ret # 23 The code uses some instructions that are covered in the supplementary notes posted with the assignment; be sure to read the notes! a) [3 points] How many parameters does the C function receive? Justify your answer. b) [3 points] How many local automatic variables does the C function use? Justify your answer. c) [3 points] The C function contains a while loop or a do-while loop. Which type of loop is shown above? Justify your answer, and state the line numbers of the instructions that make up the while loop, including the loop test and everything else necessary to execute the loop. d) [3 points] There is also an if-statement in the C function. State the line numbers of the instructions that make up the if-statement, including the test. e) [10 points] Assume the function is named Q2. Suppose that any parameters are named P1, P2, etc, in the order the paraters would be listed in the C code. Suppose the local variables are called L1, L2, etc, in the order they occur in the stack frame for the function, from high addresses to low addresses. Write C code for the function that could have yielded the x86-32 assembly code given above. (Be advised: you may derive a perfectly acceptable answer and find that when you compile it you get slightly different assembly code, so don't use that as your goal.) 2

3 3. A student writes the following C function, which she wants to use to examine the bytes of a 32-bit integer variable; most of the code for that function is shown below: void writebytes(uint32_t N) { printf("n is %"PRIX32"\n", N); // 1 uint8_t* p = (uint8_t*) &N; // 2 uint8_t b3 = *p; // 3 uint8_t b2 = *(p+1); // 4 uint8_t b1 = *(p+2); // 5 uint8_t b0 = *(p+3); // 6 }... When the student tests the function, the results are initially puzzling. a) [2 points] The posted tar file includes a Linux executable, Q3. The executable includes a main() function that calls the function shown above. Execute the program. What does it print? b) [3 points] Start gdb on Q3, and set a breakpoint at writebytes(). Now run Q3; it will halt execution just after entering writebytes(). Using the gdb print command, display the value of N in hex; what is it? c) [6 points] Step (in gdb) until lines 1 through 6 shown above have been executed. Now, use gdb to prove that p does indeed point to N. Describe how you did this; copy/paste the relevant parts of your gdb session in your answer. d) [4 points] Again, using gdb, find the values of the four local variables b3, b2, b1 and b0. State the values, expressed in hex. e) [4 points] Consider your answer to part a), and the fact you proved in part c). Explain precisely why the value you found for b0 makes sense. Warning: a correct answer must take data representation into account. 3

4 4. An executable Q4 segfaults when it is run. A short session with gdb shows some puzzling results: #1054 wdm@centos65:q4> gdb Q4 (gdb) run Starting program: /home/wdm/2505/hw10/q4/q4 Program received signal SIGSEGV, Segmentation fault. 0x080483f5 in Q4 (Sz= ) at Q4.c:18 18 List[idx] = idx * idx; (gdb) print idx $1 = 856 There are at least two strange things here: The value of idx is 856, but the loop is controlled by the test "idx < Sz", where Sz is passed to the function via the call in main(), and the actual parameter there is 10 (see the source code for main(), provided in the HW10.tar). The parameter passed to the function Q4() is huge, not 10. How could that have happened? Let's try another gdb session: (gdb) break Q4 Breakpoint 1 at 0x80483e2: file Q4.c, line 17. (gdb) run Starting program: /home/wdm/2505/hw10/q4/q4 Breakpoint 1, Q4 (Sz=10) at Q4.c:17 17 for (int idx = 0; idx < Sz; idx++) { Missing separate debuginfos, use: debuginfo-install glibc el6_5.4.i686 (gdb) continue Continuing. Breakpoint 1, Q4 (Sz= ) at Q4.c:17 17 for (int idx = 0; idx < Sz; idx++) { a) [2 points] Explain why the information above shows that Q4() is being executed twice. But, there's only one call to Q4() in main(). How can this be? What's on the stack at the moment? (gdb) backtrace #0 Q4 (Sz= ) at Q4.c:18 #1 0x a in?? () #2 0x in puts@plt () #3 0x b in libc_csu_init () #4 0x00b64d36 in libc_start_main () from /lib/libc.so.6 #5 0x in _start () Well, that doesn't look right. There should be a frame for main() and one for Q4(), but we just have frames for library/system functions, one for "??" whatever that is, and we do have one for Q4(). 4

5 Here's what we should have expected to see (restarting execution from the beginning): (gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /home/wdm/2505/hw10/q4/q4 Breakpoint 1, Q4 (Sz=10) at Q4.c:17 17 for (int idx = 0; idx < Sz; idx++) { (gdb) backtrace #0 Q4 (Sz=10) at Q4.c:17 #1 0x080483a9 in main () at Q4.c:8 So, on the previous run, the stack's been corrupted somehow let's look at the stack in detail (starting where we left off above, so we've run until main() calls Q4(), and nothing bad seems to have happened yet). Here is information about frame 0 (the one for Q4()): (gdb) info frame 0 Stack frame at 0xffffd2d0: eip = 0x80483b6 in Q4 (Q4.c:17); saved eip 0x80483a9 called by frame at 0xffffd2f0 source language c. Arglist at 0xffffd2c8, args: Sz=10 Locals at 0xffffd2c8, Previous frame's sp is 0xffffd2d0 Saved registers: ebp at 0xffffd2c8, eip at 0xffffd2cc b) [4 points] At what address does the frame for Q4() begin? How do you know that? c) [5 points] What does the following gdb output tell us? (gdb) p/x *(int)($ebp + 4) $8 = 0x80483a9 d) [5 points] The assembly code for main() is shown below. Explain precisely what this tells us about the value that was displayed in the previous part. (gdb) disassem main Dump of assembler code for function main: 0x <+0>: push %ebp 0x <+1>: mov %esp,%ebp 0x <+3>: and $0xfffffff0,%esp 0x a <+6>: sub $0x10,%esp 0x d <+9>: movl $0xa,(%esp) 0x080483a4 <+16>: call 0x80483b0 <Q4> 0x080483a9 <+21>: mov $0x0,%eax 0x080483ae <+26>: leave 0x080483af <+27>: ret End of assembler dump. 5

6 Just for completeness, here's information about frame 1 (the one for main()): (gdb) info frame 1 Stack frame at 0xffffd2f0: eip = 0x80483a9 in main (Q4.c:8); saved eip 0xb64d36 caller of frame at 0xffffd2d0 source language c. Arglist at 0xffffd2e8, args: Locals at 0xffffd2e8, Previous frame's sp is 0xffffd2f0 Saved registers: ebp at 0xffffd2e8, eip at 0xffffd2ec So, now the question is, what happens when we run to the end of Q4()? Let's set a breakpoint at the last instruction before the end of Q4() and run to it: (gdb) break Q4.c:21 Breakpoint 2 at 0x80483d9: file Q4.c, line 21. (gdb) continue Continuing. Breakpoint 2, Q4 (Sz=10) at Q4.c:21 21!! I'm going to be annoying here and NOT show you that C code.!! e) [5 points] Below, we display the contents of the same memory location that was examined in part c). The value has not changed: (gdb) print/x *(int*)($ebp + 4) $3 = 0x80483a9 Now, execute that last instruction in Q4() and see what happens to the value: (gdb) next 22 } (gdb) print/x *(int*)($ebp + 4) $4 = 0x80483b0 Now, value has changed. Below we have part of the code for Q4(): (gdb) disassem Q4 Dump of assembler code for function Q4: 0x080483b0 <+0>: push %ebp 0x080483b1 <+1>: mov %esp,%ebp 0x080483b3 <+3>: sub $0x30,%esp... End of assembler dump. Now, consider the code for Q4(), and the change in that aforementioned value. Explain precisely how this information explains the fact that Q4() was executed twice. 6

7 5. Unpacking the posted tar file HW10.tar will create a subdirectory Q5 containing the following files: Q5driver.c Q5bomb.h Q5bomb.o Q5 a driver file for Q5bomb() header file for importing Q5bomb() to Q5driver.c x86-32 binary for Q5bomb() x86-32 executable for the program The executable Q5 was produced with the command: gcc o Q5 m32 O0 ggdb3 Wall Q5driver.c Q5bomb.o If you want to edit Q5driver.c for some reason, use the command above to recompile. This will not work correctly on your 64-bit Linux install unless you've followed the instructions on the course Forum and enabled 32-bit builds. If that's a problem, complete this question on rlogin. Try executing Q5, and you'll see a message saying you must supply a string on the command line. Try running Q5 with a string of your choosing; unless you are very lucky, a segfault will occur while the function Q5bomb() is running. Your job is now to find a string that will NOT lead to a segfault when you execute Q5. There are many strings that will work. You might find one by trial and error, but that will not yield any credit. You must use gdb to analyze what happens when you execute Q5, and deduce the characteristics of a string that will prevent the segfault. An acceptable answer to this question will: Give a precise explanation of the characteristics a string must have to "defuse" the segfault. Show how you used gdb to determine the answer to the previous part. Here's a sample of my gdb session to give you some inspiration: Start gdb on the executable: #1038 wdm@centos65:q5> gdb Q5 Set execution to halt when Q5bomb() is entered: (gdb) break Q5bomb Breakpoint 1 at 0x804845a Run the program with a string, so we can examine execution: (gdb) run pleasedonotcrash Starting program: /home/wdm/2505/hw10/q5/q5 pleasedonotcrash Breakpoint 1, 0x a in Q5bomb () Let's look at the code for Q5bomb() and see what we can figure out. Unfortunately, whoever compiled Q5bomb.c did not generate debugging information, so all we can do is look at assembly code. That does give us a lot of information, but it will take some digging to get the details. 7

8 At the moment, we just entered he body of the function Q5bomb(), we can use the disassem command in gdb to generate the assembly code from the machine code that's in the executable: (gdb) disassem Q5bomb Dump of assembler code for function Q5bomb: 0x <+0>: push %ebp 0x <+1>: mov %esp,%ebp 0x <+3>: sub $0x28,%esp => 0x a <+6>: cmpl $0x0,0x8(%ebp) 0x e <+10>: je 0x80484bb <Q5bomb+103>... Aha! There's a parameter (well, we knew that if we looked in Q5driver.c). Let's see if it's correct; it should be "pleasedontcrash". How can we display it? Let's try: (gdb) print $ebp + 8 $5 = (void *) 0xffffd2c0 That's the address of the parameter not what we want. Let's try: (gdb) print *($ebp + 8) Attempt to dereference a generic pointer. That doesn t work; $ebp + 8 is a void*, as it says above. We need a typecast; let's try: (gdb) print *(char*)($ebp + 8) $6 = 44 ',' That doesn t work either, at least not the way we wanted; when we dereference $ebp + 8, we get the first byte of the thing it's pointing to, which is actually a pointer to a char array. Remember, the parameter is a char*; let's try: What's going on there? (gdb) print *(*(char**)($ebp+8)) $2 = 112 'p' OK, so the first character is 'p', which is correct. Let's try a little more pointer arithmetic and see what comes next: (gdb) print *(*(char**)($ebp+8)+1) $3 = 108 'l' (gdb) print *(*(char**)($ebp+8)+2) $4 = 101 'e' ($ebp + 8) is the address of the parameter, which is a pointer to the parameter string, which is a char*. So, ($ebp + 8) is a pointer to a char*, hence a char**. We need to typecast it correctly before gdb will let us dereference it, and get string. That's all what I expected, but tedious. Let's try a memory display command: (gdb) x/sb (*(char**)($ebp+8)) 0xffffd52c: "pleasedontcrash" So, *(char**)($ebp + 8) gives us string. Then, we dereference THAT to get the first element of the array. And what's going on here? Well, we know that *(char**)($ebp + 8) gives us string. The gdb command x/nfu is used to display the contents of memory. The parameter to x/nfu is a pointer to a block of memory you want to view. You can look up the details, and the options, but the 's' means treat the block like a null-terminated C-string, if that's possible. Since string points to a null-terminated char array, we see the string "pleasedontcrash". 8

9 Now, you need to analyze the assembly code (disassem) and determine what's going on in the function Q5bomb(). That will give you all the information you need to answer the questions below. Your score on this question will depend largely on providing a good explanation of your analysis, illustrated with "snapshots" of your gdb session(s). You should look at the gdb sessions in this assignment and the course notes for inspiration. a) [6 points] Give a precise explanation of the characteristics a string must have to "defuse" the segfault. This should be as general as possible (e.g., all strings that are exactly 73 characters long and contain the substring "skunk"). b) [12 points] Show how you used gdb to determine the answer to the previous part. For this, you should write explanatory descriptions of what you did, and copy/paste in relevant parts of your gdb session. 9

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced: This assignment refers to concepts discussed in sections 2.1.1 2.1.3, 2.1.8, 2.2.1 2.2.6, 3.2, 3.4, and 3.7.1of csapp; see that material for discussions of x86 assembly language and its relationship to

More information

A short session with gdb verifies a few facts; the student has made notes of some observations:

A short session with gdb verifies a few facts; the student has made notes of some observations: This assignment refers to concepts discussed in the course notes on gdb and the book The Art of Debugging by Matloff & Salzman. The questions are definitely "hands-on" and will require some reading beyond

More information

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5]; This assignment refers to concepts discussed in the course notes on gdb and the book The Art of Debugging by Matloff & Salzman. The questions are definitely "hands-on" and will require some reading beyond

More information

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices

More information

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices

More information

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices

More information

You may work with a partner on this quiz; both of you must submit your answers.

You may work with a partner on this quiz; both of you must submit your answers. Instructions: Choose the best answer for each of the following questions. It is possible that several answers are partially correct, but one answer is best. It is also possible that several answers are

More information

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11 X86 Debug Computer Systems Section 3.11 GDB is a Source Level debugger We have learned how to debug at the C level Now, C has been translated to X86 assembler! How does GDB play the shell game? Makes it

More information

CS 2505 Computer Organization I

CS 2505 Computer Organization I Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may

More information

Buffer Overflow Attack

Buffer Overflow Attack Buffer Overflow Attack What every applicant for the hacker should know about the foundation of buffer overflow attacks By (Dalgona@wowhacker.org) Email: zinwon@gmail.com 2005 9 5 Abstract Buffer overflow.

More information

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only Instructions: The following questions use the AT&T (GNU) syntax for x86-32 assembly code, as in the course notes. Submit your answers to these questions to the Curator as OQ05 by the posted due date and

More information

Download the tarball for this session. It will include the following files:

Download the tarball for this session. It will include the following files: Getting Started 1 Download the tarball for this session. It will include the following files: driver driver.c bomb.h bomb.o 64-bit executable C driver source declaration for "bomb" 64-bit object code for

More information

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page fact sheet. Your fact sheet may contain definitions and examples,

More information

CS , Fall 2002 Exam 1

CS , Fall 2002 Exam 1 Andrew login ID: Full Name: CS 15-213, Fall 2002 Exam 1 October 8, 2002 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front. Write

More information

Project 1 Notes and Demo

Project 1 Notes and Demo Project 1 Notes and Demo Overview You ll be given the source code for 7 short buggy programs (target[1-7].c). These programs will be installed with setuid root Your job is to write exploits (sploit[1-7].c)

More information

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction E I P CPU isters Condition Codes Addresses Data Instructions Memory Object Code Program Data OS Data Topics Assembly Programmer

More information

buffer overflow exploitation

buffer overflow exploitation buffer overflow exploitation Samuele Andreoli, Nicolò Fornari, Giuseppe Vitto May 11, 2016 University of Trento Introduction 1 introduction A Buffer Overflow is an anomaly where a program, while writing

More information

Introduction Presentation A

Introduction Presentation A CSE 2421/5042: Systems I Low-Level Programming and Computer Organization Introduction Presentation A Read carefully: Bryant Chapter 1 Study: Reek Chapter 2 Skim: Reek Chapter 1 08/22/2018 Gojko Babić Some

More information

Intro x86 Part 3: Linux Tools & Analysis

Intro x86 Part 3: Linux Tools & Analysis Intro x86 Part 3: Linux Tools & Analysis Xeno Kovah 2009/2010 xkovah at gmail Approved for Public Release: 10-3348. Distribution Unlimited All materials is licensed under a Creative Commons Share Alike

More information

CS 2505 Computer Organization I

CS 2505 Computer Organization I Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may

More information

CS , Fall 2004 Exam 1

CS , Fall 2004 Exam 1 Andrew login ID: Full Name: CS 15-213, Fall 2004 Exam 1 Tuesday October 12, 2004 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front.

More information

CS , Fall 2001 Exam 1

CS , Fall 2001 Exam 1 Andrew login ID: Full Name: CS 15-213, Fall 2001 Exam 1 October 9, 2001 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front. Write

More information

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G CS 33: Week 3 Discussion x86 Assembly (v1.0) Section 1G Announcements - HW2 due Sunday - MT1 this Thursday! - Lab2 out Info Name: Eric Kim (Section 1G, 2-4 PM, BH 5419) Office Hours (Boelter 2432) - Wed

More information

Lab 10: Introduction to x86 Assembly

Lab 10: Introduction to x86 Assembly CS342 Computer Security Handout # 8 Prof. Lyn Turbak Wednesday, Nov. 07, 2012 Wellesley College Revised Nov. 09, 2012 Lab 10: Introduction to x86 Assembly Revisions: Nov. 9 The sos O3.s file on p. 10 was

More information

Pointer Casts and Data Accesses

Pointer Casts and Data Accesses C Programming Pointer Casts and Data Accesses For this assignment, you will implement a C function similar to printf(). While implementing the function you will encounter pointers, strings, and bit-wise

More information

ANITA S SUPER AWESOME RECITATION SLIDES

ANITA S SUPER AWESOME RECITATION SLIDES ANITA S SUPER AWESOME RECITATION SLIDES 15/18-213: Introduction to Computer Systems Stacks and Buflab, 11 Jun 2013 Anita Zhang, Section M WHAT S NEW (OR NOT) Bomblab is due tonight, 11:59 PM EDT Your late

More information

Download the tarball for this session. It will include the following files:

Download the tarball for this session. It will include the following files: Getting Started 1 Download the tarball for this session. It will include the following files: driver driver.c bomb.h bomb.o 64-bit executable C driver source declaration for "bomb" 64-bit object code for

More information

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature CSE2421 FINAL EXAM SPRING 2013 Name KEY Instructions: This is a closed-book, closed-notes, closed-neighbor exam. Only a writing utensil is needed for this exam. No calculators allowed. If you need to go

More information

15-213/18-213, Fall 2011 Exam 1

15-213/18-213, Fall 2011 Exam 1 Andrew ID (print clearly!): Full Name: 15-213/18-213, Fall 2011 Exam 1 Tuesday, October 18, 2011 Instructions: Make sure that your exam is not missing any sheets, then write your Andrew ID and full name

More information

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32 GDB Tutorial Young W. Lim 2017-02-14 Tue Young W. Lim GDB Tutorial 2017-02-14 Tue 1 / 32 Outline 1 Introduction Young W. Lim GDB Tutorial 2017-02-14 Tue 2 / 32 Based on "Self-service Linux: Mastering the

More information

CPS104 Recitation: Assembly Programming

CPS104 Recitation: Assembly Programming CPS104 Recitation: Assembly Programming Alexandru Duțu 1 Facts OS kernel and embedded software engineers use assembly for some parts of their code some OSes had their entire GUIs written in assembly in

More information

CSE 374 Final Exam 3/15/17. Name UW ID#

CSE 374 Final Exam 3/15/17. Name UW ID# Name UW ID# There are 10 questions worth a total of 110 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control. C Flow Control David Chisnall February 1, 2011 Outline What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope Disclaimer! These slides contain a lot of

More information

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110 Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question

More information

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers. 15-213 Introduction to Computer Systems Exam 1 February 22, 2005 Name: Andrew User ID: Recitation Section: This is an open-book exam. Notes are permitted, but not computers. Write your answer legibly in

More information

Accessing Data in Memory

Accessing Data in Memory Accessing Data in Memory You will implement a simple C function that parses a tangled list of binary records in memory, processing them nonsequentially, and produces a simple text report. The function

More information

Understanding C/C++ Strict Aliasing

Understanding C/C++ Strict Aliasing Understanding C/C++ Strict Aliasing or - Why won't the #$@##@^% compiler let me do what I need to do! by Patrick Horgan There's a lot of confusion about strict aliasing rules. The main source of people's

More information

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics. Lecture 6B Machine-Level Programming V: Miscellaneous Topics Topics Linux Memory Layout Understanding Pointers Buffer Overflow Upper 2 hex digits of address Red Hat v. 6.2 ~1920MB memory limit FF C0 Used

More information

CS241 Computer Organization Spring 2015 IA

CS241 Computer Organization Spring 2015 IA CS241 Computer Organization Spring 2015 IA-32 2-10 2015 Outline! Review HW#3 and Quiz#1! More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add,

More information

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp 15-213 Introduction to Computer Systems Exam 1 February 22, 2005 Name: Andrew User ID: Recitation Section: Model Solution fp This is an open-book exam. Notes are permitted, but not computers. Write your

More information

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux CNIT 127: Exploit Development Ch 2: Stack Overflows in Linux Stack-based Buffer Overflows Most popular and best understood exploitation method Aleph One's "Smashing the Stack for Fun and Profit" (1996)

More information

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from s Many of the following slides are based on those from 1 Complete Powerpoint Lecture Notes for Computer Systems: A Programmer's Perspective (CS:APP) Randal E. Bryant and David R. O'Hallaron http://csapp.cs.cmu.edu/public/lectures.html

More information

Machine Programming 3: Procedures

Machine Programming 3: Procedures Machine Programming 3: Procedures CS61, Lecture 5 Prof. Stephen Chong September 15, 2011 Announcements Assignment 2 (Binary bomb) due next week If you haven t yet please create a VM to make sure the infrastructure

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID: Fall term 2012 KAIST EE209 Programming Structures for EE Final exam Thursday Dec 20, 2012 Student's name: Student ID: The exam is closed book and notes. Read the questions carefully and focus your answers

More information

Machine Programming 1: Introduction

Machine Programming 1: Introduction Machine Programming 1: Introduction CS61, Lecture 3 Prof. Stephen Chong September 8, 2011 Announcements (1/2) Assignment 1 due Tuesday Please fill in survey by 5pm today! Assignment 2 will be released

More information

CS 201 Winter 2014 (Karavanic) Final Exam

CS 201 Winter 2014 (Karavanic) Final Exam CS 201 Winter 2014 (Karavanic) Final Exam Your Name: (1 point) Instructions: - Be sure to write your name on the first sheet. - All answers, and all work submitted in support of answers, should be written

More information

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1 Homework In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, 361-367 Practice Exam 1 1 In-line Assembly Code The gcc compiler allows you to put assembly instructions in-line

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron

More information

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2014 Midterm Exam (29 October 2014) CSE351 Autumn 2014 Midterm Exam (29 October 2014) (Version A) Please read through the entire examination first! We designed this exam so that it can be completed in 50 minutes and, hopefully, this estimate

More information

15-213/18-243, Fall 2010 Exam 1 - Version A

15-213/18-243, Fall 2010 Exam 1 - Version A Andrew login ID: Full Name: Section: 15-213/18-243, Fall 2010 Exam 1 - Version A Tuesday, September 28, 2010 Instructions: Make sure that your exam is not missing any sheets, then write your Andrew login

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

More information

CS356: Discussion #5 Debugging with GDB. Marco Paolieri

CS356: Discussion #5 Debugging with GDB. Marco Paolieri CS356: Discussion #5 Debugging with GDB Marco Paolieri (paolieri@usc.edu) Schedule: Exams and Assignments Week 1: Binary Representation HW0. Week 2: Integer Operations Week 3: Floating-Point Operations

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron

More information

x86 assembly CS449 Fall 2017

x86 assembly CS449 Fall 2017 x86 assembly CS449 Fall 2017 x86 is a CISC CISC (Complex Instruction Set Computer) e.g. x86 Hundreds of (complex) instructions Only a handful of registers RISC (Reduced Instruction Set Computer) e.g. MIPS

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc -S) text Asm

More information

CSE 361S Intro to Systems Software Lab Assignment #4

CSE 361S Intro to Systems Software Lab Assignment #4 Due: Thursday, October 23, 2008. CSE 361S Intro to Systems Software Lab Assignment #4 In this lab, you will mount a buffer overflow attack on your own program. As stated in class, we do not condone using

More information

War Industries Presents: An Introduction to Programming for Hackers Part V - Functions. By Lovepump, Visit:

War Industries Presents: An Introduction to Programming for Hackers Part V - Functions. By Lovepump, Visit: War Industries Presents: An Introduction to Programming for Hackers Part V - Functions By Lovepump, 2004 Visit: www.warindustries.com Goals: At the end of Part IV, you should be able to competently code

More information

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

CPEG421/621 Tutorial

CPEG421/621 Tutorial CPEG421/621 Tutorial Compiler data representation system call interface calling convention Assembler object file format object code model Linker program initialization exception handling relocation model

More information

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic: Pointer Manipulations Pointer Casts and Data Accesses Viewing Memory The contents of a block of memory may be viewed as a collection of hex nybbles indicating the contents of the byte in the memory region;

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 10 Activation Records THEORY OF COMPILATION EranYahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec10.pptx Reference: Dragon 7.1,7.2. MCD 6.3,6.4.2 1 You are here Compiler txt Source Lexical

More information

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Implementing Threads Operating Systems In Depth II 1 Copyright 2018 Thomas W Doeppner All rights reserved The Unix Address Space stack dynamic bss data text Operating Systems In Depth II 2 Copyright 2018

More information

CS 3214 Computer Systems. Do not start the test until instructed to do so! printed

CS 3214 Computer Systems. Do not start the test until instructed to do so! printed Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may

More information

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006 C Compilation Model Comp-206 : Introduction to Software Systems Lecture 9 Alexandre Denault Computer Science McGill University Fall 2006 Midterm Date: Thursday, October 19th, 2006 Time: from 16h00 to 17h30

More information

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2014 Midterm Exam (29 October 2014) CSE351 Autumn 2014 Midterm Exam (29 October 2014) Please read through the entire examination first! We designed this exam so that it can be completed in 50 minutes and, hopefully, this estimate will prove

More information

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning

More information

CSE 351: Week 4. Tom Bergan, TA

CSE 351: Week 4. Tom Bergan, TA CSE 35 Week 4 Tom Bergan, TA Does this code look okay? int binarysearch(int a[], int length, int key) { int low = 0; int high = length - ; while (low

More information

Advanced Buffer Overflow

Advanced Buffer Overflow Pattern Recognition and Applications Lab Advanced Buffer Overflow Ing. Davide Maiorca, Ph.D. davide.maiorca@diee.unica.it Computer Security A.Y. 2016/2017 Department of Electrical and Electronic Engineering

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls 1 Lecture Goals Challenges of supporting functions Providing information for the called function Function arguments and local variables Allowing the

More information

Tales of the Unknown. Part One.

Tales of the Unknown. Part One. Tales of the Unknown Part One www.felinemenace.org Table of Contents Introduction... 3 Requisites... 3 The Story... 3 A look at Initialization...4 Compile time...4 Run time...5 A look at Dereferencing...

More information

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program: Question 1. (8 points) Suppose we have the following two statements in a C program: int *x = malloc(sizeof(int)); int *y = malloc(sizeof(int)); For each of the following expressions, write true if the

More information

Buffer-Overflow Attacks on the Stack

Buffer-Overflow Attacks on the Stack Computer Systems Buffer-Overflow Attacks on the Stack Introduction A buffer overflow occurs when a program, while writing data to a buffer, overruns the buffer's boundary and overwrites memory in adjacent

More information

UW CSE 351, Winter 2013 Midterm Exam

UW CSE 351, Winter 2013 Midterm Exam Full Name: Student ID: UW CSE 351, Winter 2013 Midterm Exam February 15, 2013 Instructions: Make sure that your exam is not missing any of the 9 pages, then write your full name and UW student ID on the

More information

Machine Programming 4: Structured Data

Machine Programming 4: Structured Data Machine Programming 4: Structured Data CS61, Lecture 6 Prof. Stephen Chong September 20, 2011 Announcements Assignment 2 (Binary bomb) due Thursday We are trying out Piazza to allow class-wide questions

More information

The following notes illustrate debugging a linked list implementation with gdb.

The following notes illustrate debugging a linked list implementation with gdb. Payload Type The following notes illustrate debugging a linked list implementation with. The example makes use of the following payload type: struct _WordRecord { char* Word; // zero-terminated C-string

More information

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009 CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009 Name Score out of 70 UMBC Username Notes: a. Please write clearly. Unreadable answers receive no credit. b. For TRUE/FALSE questions, write the

More information

Both parts center on the concept of a "mesa", and make use of the following data type:

Both parts center on the concept of a mesa, and make use of the following data type: C Programming Simple Array Processing This assignment consists of two parts. The first part focuses on array read accesses and computational logic. The second part requires solving the same problem using

More information

CS / ECE , Spring 2010 Exam 1

CS / ECE , Spring 2010 Exam 1 Andrew login ID: Full Name: Recitation Section: CS 15-213 / ECE 18-243, Spring 2010 Exam 1 Version 1100101 Tuesday, March 2nd, 2010 Instructions: Make sure that your exam is not missing any sheets, then

More information

Lecture 08 Control-flow Hijacking Defenses

Lecture 08 Control-flow Hijacking Defenses Lecture 08 Control-flow Hijacking Defenses Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Slides adapted from Miller, Bailey, and Brumley Control Flow Hijack: Always control + computation

More information

CS61, Fall 2012 Midterm Review Section

CS61, Fall 2012 Midterm Review Section CS61, Fall 2012 Midterm Review Section (10/16/2012) Q1: Hexadecimal and Binary Notation - Solve the following equations and put your answers in hex, decimal and binary. Hexadecimal Decimal Binary 15 +

More information

Stack overflow exploitation

Stack overflow exploitation Stack overflow exploitation In order to illustrate how the stack overflow exploitation goes I m going to use the following c code: #include #include #include static void

More information

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information

Machine-Level Programming I: Introduction Jan. 30, 2001

Machine-Level Programming I: Introduction Jan. 30, 2001 15-213 Machine-Level Programming I: Introduction Jan. 30, 2001 Topics Assembly Programmer s Execution Model Accessing Information Registers Memory Arithmetic operations IA32 Processors Totally Dominate

More information

Machine Language, Assemblers and Linkers"

Machine Language, Assemblers and Linkers Machine Language, Assemblers and Linkers 1 Goals for this Lecture Help you to learn about: IA-32 machine language The assembly and linking processes 2 1 Why Learn Machine Language Last stop on the language

More information

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here x86-64 Assembly Language Assembly language is a human-readable representation of machine code instructions

More information

Systems I. Machine-Level Programming I: Introduction

Systems I. Machine-Level Programming I: Introduction Systems I Machine-Level Programming I: Introduction Topics Assembly Programmerʼs Execution Model Accessing Information Registers IA32 Processors Totally Dominate General Purpose CPU Market Evolutionary

More information

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018 238P: Operating Systems Lecture 7: Basic Architecture of a Program Anton Burtsev January, 2018 What is a program? What parts do we need to run code? Parts needed to run a program Code itself By convention

More information

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016 CS 31: Intro to Systems Functions and the Stack Martin Gagne Swarthmore College February 23, 2016 Reminders Late policy: you do not have to send me an email to inform me of a late submission before the

More information

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24 GDB Tutorial Young W. Lim 2016-09-29 Thr Young W. Lim GDB Tutorial 2016-09-29 Thr 1 / 24 Outline 1 Introduction Young W. Lim GDB Tutorial 2016-09-29 Thr 2 / 24 Based on "Self-service Linux: Mastering the

More information

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize: Simple C Program Helloworld.c Programming and Debugging Assembly under Linux slides by Alexandre Denault int main(int argc, char *argv[]) { } printf("hello World"); Programming and Debugging Assembly under

More information

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017 CS 31: Intro to Systems ISAs and Assembly Martin Gagné Swarthmore College February 7, 2017 ANNOUNCEMENT All labs will meet in SCI 252 (the robot lab) tomorrow. Overview How to directly interact with hardware

More information

Assembly I: Basic Operations. Jo, Heeseung

Assembly I: Basic Operations. Jo, Heeseung Assembly I: Basic Operations Jo, Heeseung Moving Data (1) Moving data: movl source, dest Move 4-byte ("long") word Lots of these in typical code Operand types Immediate: constant integer data - Like C

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

Advanced Buffer Overflow

Advanced Buffer Overflow Pattern Recognition and Applications Lab Advanced Buffer Overflow Ing. Davide Maiorca, Ph.D. davide.maiorca@diee.unica.it Computer Security A.Y. 2017/2018 Department of Electrical and Electronic Engineering

More information

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University Assembly I: Basic Operations Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Moving Data (1) Moving data: movl source, dest Move 4-byte ( long )

More information

CIT Week13 Lecture

CIT Week13 Lecture CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there

More information

Giving credit where credit is due

Giving credit where credit is due CSCE 230J Computer Organization Machine-Level Programming I: Introduction Dr. Steve Goddard goddard@cse.unl.edu Giving credit where credit is due Most of slides for this lecture are based on slides created

More information

Full Name: CISC 360, Fall 2008 Example of Exam

Full Name: CISC 360, Fall 2008 Example of Exam Full Name: CISC 360, Fall 2008 Example of Exam Page 1 of 0 Problem 1. (12 points): Consider the following 8-bit floating point representation based on the IEEE floating point format: There is a sign bit

More information