6.temp & rand

34
Course 2: Programming Issues, Section 6 Pascal Meunier, Ph.D., M.Sc., CISSP May 2004; updated September 28, 2004 Developed thanks to the support of Symantec Corporation, NSF SFS Capacity Building Program (Award Number 0113725) and the Purdue e-Enterprise Center Copyright (2004) Purdue Research Foundation. All rights reserved.

Upload: phanleson

Post on 20-Jun-2015

824 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: 6.Temp & Rand

Course 2: Programming Issues,Section 6Pascal Meunier, Ph.D., M.Sc., CISSPMay 2004; updated September 28, 2004Developed thanks to the support of Symantec Corporation,NSF SFS Capacity Building Program (Award Number 0113725) andthe Purdue e-Enterprise CenterCopyright (2004) Purdue Research Foundation. All rights reserved.

Page 2: 6.Temp & Rand

About These Slides

Developed thanks to Symantec’s support

Reviewed by Symantec engineers

– Special thanks to:

Jared Robinson

Alan Krassowski

Craig Ozancin

Free to use

– Notes, comments, suggestions, or modified slides areappreciated ([email protected])

If you modify them, please keep this slide and adda note stating that you modified them

Page 3: 6.Temp & Rand

Course 2 Learning Plan

Buffer Overflows

Format String Vulnerabilities

Code Injection and Input Validation

Cross-site Scripting Vulnerabilities

Links and Race Conditions

Temporary Files and Randomness

Canonicalization and Directory Traversal

Page 4: 6.Temp & Rand

Learning objectives

Understand why creating files in insecuredirectories like /tmp is difficult but useful

Learn why OS-provided function calls helptremendously

Understand the need for good randomness

Learn which OS-provided function calls helpprovide good random numbers

Learn how to create random file names

Page 5: 6.Temp & Rand

Temporary Files and Randomness: Outline

Temporary Files

– Problem Statement

– Survey of functions

UNIX

Windows

Randomness

– Need

– Types of random numbers

– Devices

– Windows API

Page 6: 6.Temp & Rand

Temporary Files

Space for temporary files is found in directoriessuch as /tmp, /var/tmp or C:\TEMP, whereeveryone can write

Space may be purged regularly (e.g., "every night,files older than 5 days are deleted") and duringreboot

Space used by many UNIX or Windows utilities,installers and programs

UNIX systems are often configured so that thisspace is not counted as part of user quota

– Allow large, temporary jobs

Page 7: 6.Temp & Rand

Temporary Files Issues

Need an unpredictable name to avoid a collisionbetween links and your files or directories

There's a race condition between testing if a fileexists and creating it

Need correct permissions

There's a race condition between creating andsetting permissions

Need OS support!

Page 8: 6.Temp & Rand

Name Collisions Attacks

What if the name of your temporary file (lock file orother) in /tmp is constant or predictable?

– Your program using a lock file may never run or do whatit's supposed to!

Run the lock.c example from part A, but this time, create alock file beforehand... Your program will never get past thelock file test (obviously)

Lock files need to be put where other users can't create files

– It's easy to make a symlink pointing to a sensitive file

Symlink attacks are easier if the name of thetemporary file is predictable

Page 9: 6.Temp & Rand

How Not to Choose a Random Name

Use the process ID

Use the user ID

Use the time of day

Use a counter

Use a bad random number generator

etc...

Page 10: 6.Temp & Rand

OS Support for Temporary Files

The following take a filename “template” as input

– mktemp - generate temporary file name (unique)

– mkstemp - also create the file

– mkstemps - generate temporary file name with suffix

– mkdtemp - create a directory

Overwrite part of a template to create a uniquename

Some of these functions used to create namesusing parts of the date or process ID, etc... andwere insecure

Page 11: 6.Temp & Rand

mktemp (1) (3)

Section (1): command line (shell scripts)

– BSD/MacOS X:

– creates file with mode 0600unique name

Section (3): C programs

– Race condition between getting the name and creatingthe file!

– The program must use "open" with the O_CREAT |O_EXCL flags, and loop until the file is successfullycreated, or use a different function

Page 12: 6.Temp & Rand

Command Line Example

% mktemp "testXXXX"

testpnbE

% ls -al

-rw------- pascal staff testpnbE

Page 13: 6.Temp & Rand

mkstemp

Creates name

Creates file open for reading and writing with mode0600

Returns a file descriptor

No race condition!

Recommended function

Usage for extremely paranoid people:“Unlink” the hard link pointing to the descriptorimmediately afterwards (this is a race condition)

The file still exists but nobody else (except withdifficulty, the superuser) can access it

Page 14: 6.Temp & Rand

Mini Lab

Take the previous lock.c example

Modify it to use mkstemp to generate a temporary

file with a unique name

Of course, the temporary file created that way isnot a lock file anymore, and would be used to storetemporary data instead

Page 15: 6.Temp & Rand

Windows

No equivalent to mkstemp()

GetTempFileName

– Creates names by incrementing a counter!

– Predictable file name

Race condition between getting the name andcreating the file

– Attacker could create the file to prevent you from using it

– If you use the CREATE_ALWAYS flag, see next slide

Under Windows, you have no choice but to writeyour own function

Still a race condition, limitation due to lack of OSsupport

Page 16: 6.Temp & Rand

Windows CreateFile Problems

Recommended use with the "CREATE_ALWAYS"flag is dangerous

– "CREATE_ALWAYS" flag recommended by MSDN,Howard and Leblanc 2003

Overwrites the file

Does not set the security descriptor specified by theSECURITY_ATTRIBUTES structure

– Do the SECURITY_ATTRIBUTES matter to your application?

Perfect opportunity to trick you into overwriting asensitive file

– e.g., with a hard link

– Can't use the flag to not follow reparse points

Page 17: 6.Temp & Rand

Windows CreateFile

TRUNCATE_EXISTING will follow a hard link andcould truncate something else than intended

Use "CREATE_NEW"

– "The function fails if the specified file already exists. "(MSDN)

– You need to check for errors and loop until the file issuccessfully created

Page 18: 6.Temp & Rand

GetTempPath

MSDN recommends that software use theGetTempPath function to get the location of thetemp dir, but this is dangerous

Checks for the existence of environment variablesin the following order and uses the first path found:

1. The path specified by the TMP environment variable.

2. The path specified by the TEMP environment variable.

3. The path specified by the USERPROFILE environmentvariable.

4. The Windows directory."

Are the environment variables safe to use?

– Probably not unless you set them yourself

Page 19: 6.Temp & Rand

Exercise (Windows): Creating Temporary Files

Go tohttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/creating_and_using_a_temporary_file.asp

Discuss things that you would do differently,compared to the example, when creating atemporary file in Windows

– Find the race condition (hint: MoveFileEx)

Page 20: 6.Temp & Rand

Exercise Answers

Possible answers:

– They used the CREATE_ALWAYS flag instead ofCREATE_NEW

Add a loop until success

– Use randomly generated file names

How to do that on Windows? (see next slides)

Page 21: 6.Temp & Rand

The Need for Random Numbers

Unique file or directory names

Session IDs that carry proof of authentication(nonces), passwords

Games (data, behavior, opponent generation,character generation)

Encryption

Cryptographic protocols

Page 22: 6.Temp & Rand

How Random Numbers Are Generated

Linear Congruential Generators

– Simple way to generate pseudo-random numbers

– Easily cracked

– Produce finite sequences of numbers

– Each number is tied to the others

– Some sequences of numbers will not ever be generated

Cryptographic random number generators

Entropy sensors (i.e., extracted randomness)

Page 23: 6.Temp & Rand

Seeded Random Number Generators

Pseudo-random generators depend solely on aseed, which determines the entire sequence ofnumbers returned

How random is the seed?

– Process ID, UserID: Bad Idea

– Current time: if you’re running NTP (Network TimeProtocol) all systems are synchronized up to someprecision. If you use the time, maybe I can guess whichseed you used (microsecond part might be difficult toguess, but is limited)

Page 24: 6.Temp & Rand

How to Cheat At Random Number Generation

Find a seed that will produce the numbers you want

Seed the generator with it

Convince someone: "it's random, see?"

– RPG Character generation, etc...

Page 25: 6.Temp & Rand

Roll Your Own Generator?

What matters is not only the average and thevariance of the numbers generated

All sequences of numbers must be possible

LCGs travel definite, limited “paths” through theuniverse of possible sequences

Need to incorporate entropy as it becomesavailable

Need to avoid betraying the internal state of thegenerator...

It's difficult to do correctly

Page 26: 6.Temp & Rand

Which Generator to use?

Read description, avoid Linear CongruentialGenerators such as these:

– “C” rand(3)

– rand (Windows)

– Perl rand

– C# Random

– PHP rand

Page 27: 6.Temp & Rand

Good Generators

Hardware-based

– Noise

Cryptographical quality software, entropy-seeded

– Fast, secure

Pure Entropy

– Random timing of events

Packets

Mouse movement, clicks

Keyboard

– Slow

Page 28: 6.Temp & Rand

Linux/UNIX Devices

/dev/random:

– MacOS X: same as urandom

– Linux: this is a blocking call that returns only whensufficient entropy has been captured

– Good for seeding pseudo-random number generators

/dev/urandom:

– Implements a fairly complex algorithm that variesbetween “random” and a well-seeded LCG depending onthe availability of entropy

– Non-blocking call

– Try "cat /dev/urandom"

Page 29: 6.Temp & Rand

Portability

FreeBSD, OpenBSD, NetBSD compatible

Several projects ported the functionality to Solaris,HP-UX, AIX, IRIX

MacOS X implements Yarrow for both random andurandom (so the behavior of “random” isunexpected).

Page 30: 6.Temp & Rand

Windows

Windows developers must use the functionCryptGenRandom(), which uses the same idea as/dev/urandom

There is no directly accessible entropy collectorprovided by the OS

– Reference: "Secure Programming Cookbook", section11.4 (Viega et al.)

Page 31: 6.Temp & Rand

Mini-lab

Take the previous mini-lab (lock.c)

Modify it to use random numbers from/dev/urandom instead of mkstemp, to generate a

temporary file with a unique name

– To obtain random bytes, open the device and read from it

Page 32: 6.Temp & Rand

Questions or Comments?

§

Page 33: 6.Temp & Rand

About These Slides

You are free to copy, distribute, display, and perform the work; and

to make derivative works, under the following conditions.

– You must give the original author and other contributors credit

– The work will be used for personal or non-commercial educational uses

only, and not for commercial activities and purposes

– For any reuse or distribution, you must make clear to others the terms of

use for this work

– Derivative works must retain and be subject to the same conditions, and

contain a note identifying the new contributor(s) and date of modification

– For other uses please contact the Purdue Office of Technology

Commercialization.

Developed thanks to the support of SymantecCorporation

Page 34: 6.Temp & Rand

Pascal [email protected]:Jared Robinson, Alan Krassowski, Craig Ozancin, TimBrown, Wes Higaki, Melissa Dark, Chris Clifton, GustavoRodriguez-Rivera