Week11: Race Conditions Pascal Meunier, Ph.D., M.Sc., CISSP November 1, 2006 Developed thanks to the support of Symantec Corporation, NSF SFS Capacity.

Slides:



Advertisements
Similar presentations
TOCTTOU Attacks Don Porter CS 380S
Advertisements

Windows XP Tutorial Securing Windows. Introduction This presentation will guide you through basic security principles for Windows XP.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
CMSC 414 Computer (and Network) Security Lecture 13 Jonathan Katz.
Race Conditions. Isolated & Non-Isolated Processes Isolated: Do not share state with other processes –The output of process is unaffected by run of other.
Chapter 9 Chapter 9: Managing Groups, Folders, Files, and Object Security.
Exploring the UNIX File System and File Security
1 Distributed File System, and Disk Quotas (Week 7, Thursday 2/21/2007) © Abdou Illia, Spring 2007.
Lecture 10: The FAT, VFAT, and NTFS Filesystems 6/17/2003 CSCE 590 Summer 2003.
File management in UNIX and windows 2000
Linux+ Guide to Linux Certification, Second Edition
Week11: Race Conditions Pascal Meunier, Ph.D., M.Sc., CISSP March 28, 2007 Developed thanks to the support of Symantec Corporation, NSF SFS Capacity Building.
Building Secure Software Chapter 9 Race Conditions.
Operating Systems File systems
70-270, MCSE/MCSA Guide to Installing and Managing Microsoft Windows XP Professional and Windows Server 2003 Chapter Nine Managing File System Access.
Guide To UNIX Using Linux Third Edition
Linux Operating System
File Management System The way a user or application may access files Programmer does not need to develop file management software You take files for granted.
Database Management Systems (DBMS)
Copyright © 2002 ProsoftTraining. All rights reserved. Operating System Security.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Filesystem Hierarchy Standard (FHS) –Standard of outlining the location of set files and directories on a Linux system –Gives Linux software developers.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Files & Directories Objectives –to be able to describe and use the Unix file system model and concepts Contents –directory structure –file system concepts.
1Fall 2008, Chapter 11 Disk Hardware Arm can move in and out Read / write head can access a ring of data as the disk rotates Disk consists of one or more.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
Unix Basics Chapter 4.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
CIS 450 – Network Security Chapter 16 – Covering the Tracks.
Linux+ Guide to Linux Certification, Second Edition
C HAPTER 7 Managing Disk and File System. I NTRODUCING DISK MANAGEMENT 2 types of hard disk storage supported by Windows XP are: basic hard disk & dynamic.
Chapter Two Exploring the UNIX File System and File Security.
1 Administering Shared Folders Understanding Shared Folders Planning Shared Folders Sharing Folders Combining Shared Folder Permissions and NTFS Permissions.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
UNIX Files File organization and a few primitives.
Unit OS8: File System 8.6. Lab Manual. 2 Copyright Notice © David A. Solomon and Mark Russinovich These materials are part of the Windows Operating.
Chapter Two Exploring the UNIX File System and File Security.
CE Operating Systems Lecture 21 Operating Systems Protection with examples from Linux & Windows.
G53SEC 1 Reference Monitors Enforcement of Access Control.
Storing and Retrieving Data
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
GLOBAL EDGE SOFTWERE LTD1 R EMOTE F ILE S HARING - Ardhanareesh Aradhyamath.
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
Lecture 18 Windows – NT File System (NTFS)
Copyright © Curt Hill Concurrent Execution An Overview for Database.
The Unix File system (UFS) Presented by: Gurpreet Singh Assistant Professor Department of School of Computing and Engineering Galgotias University.
Chapter 9: Networking with Unix and Linux. Objectives: Describe the origins and history of the UNIX operating system Identify similarities and differences.
UNIX File System By Vishal Desai. Introduction Basic purpose of file system: Represent and organize the system resources. But UNIX File System also maps.
Design Principles and Common Security Related Programming Problems
CHAPTER 5 MANAGING USER ACCOUNTS & GROUPS. User Accounts Windows 95, 98 & Me do not need a user account like Windows XP Professional to access computer.
Slide 6-1 Chapter 6 System Software Considerations Introduction to Information Systems Judith C. Simon.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Race conditions and synchronization issues Exploiting UNIX.
1 File Permissions Look at permissions with ls -l Output: –-rw-r--r-- 1 enda users 1234 Jun 2 10:51 file.ext.
The Unix File System R Bigelow. The UNIX File System The file system refers to the way in which UNIX implements files and directories. The UNIX file system.
Page 1 Viruses. Page 2 What Is a Virus A virus is basically a computer program that has been written to perform a specific set of tasks. Unfortunately,
W4118 Operating Systems Instructor: Junfeng Yang.
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Chapter 2: System Structures
File Management.
Exploring the UNIX File System and File Security
CE Operating Systems Lecture 21
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
Operating System Security
Race Condition Vulnerability
Presentation transcript:

Week11: Race Conditions Pascal Meunier, Ph.D., M.Sc., CISSP November 1, 2006 Developed thanks to the support of Symantec Corporation, NSF SFS Capacity Building Program (Award Number ) and the Purdue e-Enterprise Center Copyright (2006 Purdue Research Foundation. All rights reserved.

Race Conditions  Definition: security issues resulting from concurrent or interruptible code –Timing issues  Related concepts: –Concurrency, Multi-threading  Thread safety –Interrupts  Re-entrant code –Atomicity –Transactions –Late binding of labels to objects

Time of Check, Time of Use Problem  TOC/TOU Problem happens with these operations –Check if something is OK to do –Do it  In a pre-emptively multi-tasked environment, anything could happen in-between the execution of two assembly code operations  Conditions may change in-between and the check becomes invalid

Example Race Condition  User 1 creates a file with world-writable permissions  User 1 wants to change the permissions to exclude others with " chmod 700 filename "  User 2 tries to overwrite the file in-between  Will user 1 or user 2 succeed? –User 1 should have set the umask correctly!

Database Race Condition  If (condition for field 1) then do something to field 2  However process 2 changes field 1 in-between...  Result: invalid combination of values (e.g., bank account balance) –Program invariant is violated –Integrity of records is violated

Example Race Condition  Two processes: red and blue  Red: Check that user 1 has enough money to pay check #y  Blue: Check that user 1 has enough money to pay check #x  Red: Pay check #y  Is there really enough money to pay check #x?

Handling Signals  Signals are another way in which your program flow can get interrupted  If your program gets interrupted while managing data structures, it is dangerous for the signal handler to attempt to use those structures  Signal handlers can get interrupted by other (different) signals! –What if the same handler is called for multiple signals?  Is the handler's code re-entrant?  Example attack: calling free in non-re-entrant clean-up code that is interrupted and called again –free may get called twice (see double-frees in NVD)

Solutions  Atomic operations: –An operation that can't be interrupted is called "atomic"  Mechanisms that prevent concurrent access to, or modification of, an object by different processes or threads –Semaphores (up to the programmer to do correctly) –Lock files vs file locking –Database locks –Critical regions (enforced by compilers) –Monitors (enforced by compilers) –Assembly code instructions that detect if a memory location was accessed between two operations (e.g., PowerPC)

Database Locks  Race conditions described above can be prevented while using a database to store the data –Table and row locks  Nobody else can write to a database object while you have a read lock  Nobody else can read or write to a database object while you have a write lock –Transactions  Instead of locking, if the database can't complete the operation (due to competing accesses), the database can revert to a valid (past) state –Database integrity is preserved –However the operation you were attempting can fail

Lock Files  Typically used for: –Checking if another copy of a program (e.g., a server or an installer) is running  Typically is either empty or contains the pid of a process  Can be used like a primitive semaphore  Compliance is purely voluntary and isn't enforced by the OS

File Locking  UNIX file locks are "advisory", i.e., not enforced by the operating system –shared –exclusive  Windows: files aren't shared by default –Files may have locks if shared:  shared  exclusive  byte-ranged –Sometimes this is advisory, sometimes it is enforced. One needs to be careful

Mini-Lab: Lock File  Make a "C" program that creates a lock file  If the lock file already exists, it should print a message stating so and exit.  Otherwise, it should sleep 20 seconds, print that it was successful, delete the lock file and exit.  Try to run it twice simultaneously (e.g., two terminals). What happens?  Is it possible in your implementation to have a race condition if two copies are started simultaneously ? –Check all the flags that you can use with "open"  Due November 15 before start of class

File System Race Conditions  Files can be accessed by either name, or a file handle (Windows) or file descriptor (UNIX)  Files can be renamed, deleted, moved, etc...  Names and paths are bound to files at the time of a function call  The same path and name can refer to different objects, given different times  Typical TOC/TOU issue: –Permissions, file length, ownership, etc... are checked first –Then an operation is attempted –An attacker will substitute another object for the second operation

UNIX Filenames vs File Descriptors  Filenames and directory structure are changeable  Open file descriptors are fixed –System calls that use a file descriptor are to be used whenever possible instead of the equivalent functionality using paths  fchmod(int fd, mode_t mode);  fchown(int fd, uid_t owner, gid_t group);  fchdir(int fd);  fstat(int fd, struct stat *sb); –File descriptors specify an inode (see next slide)

Inodes  An inode is a data structure containing user, group and access control information (and more)  The inode specifies the location of the file on the disk  Hard links associate a name to an inode –Several hard links can point to a single inode  There's no difference between the "first" hard link and others  Inodes are deleted only when all references have been deleted –Open file descriptors and hard links count as references –Directories also have inodes

Hard Links in UNIX  By creating hard links, an attacker could make you: –Change the permissions of an unintended file –Change the contents of an unintended file  Defenses: –Manipulate files inside safe directories (with correctly set permissions) –Don't open and manipulate files as root if you don't need to –Don't re-open temporary files in shared directories (more on this later)

File System Links  Hard links –Windows: CreateHardLink –UNIX: ln  Symbolic links –UNIX:  ln -s –Windows:  a.k.a. "directory junctions" in NTFS  Manually: use Linkd.exe (Win 2K resource kit) or "junction" freeware  Virtual Drives –"subst" command

Virtual Drives (Windows)  Similar to symbolic links but limited functionality  Effect –A drive letter ("X:") actually points to a directory on a physical drive –Overcomes limits on path length  NTFS has limit of characters  Windows has a limit of 256 characters –NT, 2000, XP –"Path too long" error  Malicious software can hide inside very long paths –Setup using a "subst" virtual drive –Remove drive, and virus scanner can't find it! –

Subst Vulnerability  Virtual drives persist after a user logs off (NT)  If next user tries to use that same letter drive, they may use a folder of the attacker's choosing –Store confidential files –Run trojans  Example: Network-mapped home drive –Mounting operation used to fail silently if the drive letter was already in use –CVE

Hard Links  Indistinguishable from original entry (peer)  May not refer to directories or span file systems  Created link is subject to the same, normal file access permissions.  Deleting a hard link doesn't delete the file unless all references to the file have been deleted  A reference is either a hard link or an open file descriptor  In Windows, a hard link exists at the NTFS file system level and is not supported by FAT32 –Note: Different from a Windows shortcut

Example  % ls -al.localized -rw-r--r-- root wheel.localized % ln.localized pascal/hard.loc % ls -al pascal/hard.loc -rw-r--r-- root wheel hard.loc % rm pascal/hard.loc override rw-r--r-- root/wheel for pascal/hard.loc? yes % ls -al.localized -rw-r--r-- root wheel.localized  Note that the hard link showed the same permissions  Note that deleting the hard link didn't delete the file (the reference count was not zero)

Hard Link Vulnerability Example  Fool audit logging programs by using a hard link to a sensitive file  Audit trails record benign name instead of sensitive file access  CVE (NTFS)

Symbolic Links  Windows: –Directory junctions apply to directories only  Can refer to directories on different computers –Jargon: "File system reparse points" –Contain parameters resolved at access time –Several operations, complex setup (see tion)  UNIX: –Contain a path, which is resolved at access time –May refer to directories and files –May span file systems –Permissions appear different from the original

Symbolic Link Example  Using the same starting file as for the hard link example:  % ln -s.localized pascal/sym.loc  % ll pascal/sym.loc lrwxr-xr-x 1 pascal staff pascal/sym.loc ->.localized  Note: –The “->”) –The permissions (see the “l”?) –The owner and group are different (they were root/wheel for ".localized") –Deleting the symlink doesn't delete the file

Power of Symbolic Links  You can create links to files that don’t exist yet  Symlinks continue to exist after the files they point to have been renamed, moved or deleted –They are not updated  You can create links to arbitrary files, even in file systems you can’t see  Symlinks can link to files located across partition and disk boundaries  Example: –You can change the version of an application in use, or even an entire web site, just by changing a symlink  Very convenient!

Basic Attack  Trick a process (with higher privileges) to operate on another file than the one it thinks it is.  Example: –Create the link "temp -> /etc/password" –A privileged process executes  truncate(“temp”, 0) –The "truncate" call follows symlinks –Changes the length of the file "temp" to 0 –But truncated /etc/password instead!  Note that the contents are deleted, not the file  Can be used for write or read operations –Or deletion if the symlink is in the path and not the end point

Conditions of Vulnerability  If you are operating in a secured directory, you don't need to worry about symlink attacks  A secured directory is one with permissions of all the directories from the root of the file system to your directory, set such that only you (or root) can make changes in your secured directory –Example: /home/me (user home directories are usually set by default with secure permissions)  You are at risk if you operate –In a shared directory such as /tmp –In someone else's directory, especially with elevated privileges  Example: an anti-virus program running as administrator

Example: CUPS Vulnerability  CVE  Common Unix Printing System (CUPS) through allows local users with lp privileges to create or overwrite arbitrary files via file race conditions, as demonstrated by "ice-cream".  Predictable file name: – '/etc/cups/certs/ ' –Shared directory with users that have "lp" privilege  "lp" privilege could be gained through another exploit  File manipulated using root privileges  Symlink redirected operations anywhere and allowed gaining root privileges

Suggested Workarounds 1) Store the file in a secured directory –It was stored in a shared directory 2)Relinquish root privileges before doing file operations (if not needed) 3)Use a random name 4)Create files with "umask 077" –New files will give no permissions to groups and others  What about third-party components that you utilize?

Question In Unix, which one of these allows an attacker to fool you into deleting a different file than you intended? a) Hard links b) Symbolic links in the path, excluding the file itself c) File descriptors d) Inodes

UNIX Defenses: Creating a file  int open(const char *path, int oflag, mode_t mode);  The flags should be O_CREAT | O_EXCL –If the last path item is a symbolic link and O_CREAT and O_EXCL are set, the link is not followed. –The check for symlinks and the file creation with the correct permissions (mode) are not subject to race conditions (it's an atomic operation)  The mode should be as narrow as practical  Can your lock file program be fooled by a symlink?  Is it possible for an attacker to delete your lock file and directory, create a link and make you delete something else?  Create a fake lock file before your program runs?

Conclusions  Race conditions are difficult to prevent when using late bindings –e.g., paths and file names in file systems that are accessed by different users and processes –Operating systems should always provide calls that unambiguously refer to objects (file systems and others) for all operations  but they don't  Most synchronization or serialization mechanisms depend on the good will of individual applications –Windows, UNIX and C/C++ give you many opportunities to mess up

Questions or Comments?

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 Symantec Corporation

Pascal Meunier Contributors: Jared Robinson, Alan Krassowski, Craig Ozancin, Tim Brown, Wes Higaki, Melissa Dark, Chris Clifton, Gustavo Rodriguez-Rivera