Docsity

Buffer Overflow Attacks: Understanding and Preventing Security Vulnerabilities, Slides of Software Engineering

Partial preview of the text

Download Buffer Overflow Attacks: Understanding and Preventing Security Vulnerabilities and more Slides Software Engineering in PDF only on Docsity!

Buffer Overflow I: Attack Introduction

The Problem

void foo(char *s) {

char buf[10];

strcpy(buf,s);

printf(“buf is %s\n”,s);

foo(“thisstringistolongforfoo”);

Necessary Background

  • C functions and the stack.
  • A little knowledge of assembly/machine

language.

  • How system calls are made (at the level of

machine code level).

  • exec() system calls
    • How to “guess” some key parameters.

What is a Buffer Overflow?

  • Intent
    • Arbitrary code execution
      • Spawn a remote shell or infect with worm/virus
    • Denial of service
      • Cause software to crash
        • E.g., ping of death attack
  • Steps
    • Inject attack code into buffer
    • Overflow return address
    • Redirect control flow to attack code
    • Execute attack code

• Stack Overflow Overview

Address Space

8

0x

code 0x

static data

bss

heap

shared library

stack

kernel space

0x

0xC

0xFFFFFFFF

From Dawn Song’s RISE: http://research.microsoft.com/projects/SWSecInstitute/slides/Song.ppt

A Stack Frame

Parameters

Return Address

Calling Stack Pointer

Local Variables

10

00000000

Addresses

SP

SP+ offset

SP: stack pointer BP: base/frame pointer

BP

Sample

Stack

18 addressof(y=3) return address saved stack pointer buf y x

11

x=2;

foo(18);

y=3;

void foo(int j) { int x,y; char buf[100]; x=j; … }

Before and After

13

*void foo(char s) { char buf[100]; strcpy(buf,s); …

address of s

return-address saved sp

buf

address of s

pointer to pgm

Small Program

• What causes buffer overflow?

Example: strcpy()

char dest[20];

strcpy(dest, src); // copies string src to dest

  • strcpy assumes dest is long enough , and

assumes src is null-terminated

  • Use strncpy(dest, src, size) instead

Spot the defect! (1)

char buf[20];

char prefix[] = ”http://”;

...

strcpy(buf, prefix);

// copies the string prefix to buf

strncat(buf, path, sizeof(buf));

// concatenates path to the string buf

Spot the defect! (2)

char src[9]; char dest[9];

char base_url = ”www.ru.nl”;

strncpy(src, base_url, 9);

// copies base_url to src strcpy(dest, src);

// copies src to dest

19

base_url is 10 chars long, incl. its null terminator, so src won’t be not null-terminated

so strcpy will overrun the buffer dest

Example: strcpy and strncpy

  • Don’t replace strcpy(dest, src) by
    • strncpy(dest, src, sizeof(dest))
  • but by
    • strncpy(dest, src, sizeof(dest)-1)
    • dest[sizeof(dest)-1] = \0;
    • if dest should be null-terminated!
  • A strongly typed programming language could of course enforce that strings are always null- terminated...