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.

Slides:



Advertisements
Similar presentations
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Advertisements

Windows XP Tutorial Securing Windows. Introduction This presentation will guide you through basic security principles for Windows XP.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
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.
1 Chapter 11: File-System Interface  File Concept  Access Methods  Directory Structure  File System Mounting  File Sharing  Protection  Chapter.
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.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment, Enhanced Chapter 7: Advanced File System Management.
Linux+ Guide to Linux Certification, Second Edition
Building Secure Software Chapter 9 Race Conditions.
Hands-On Microsoft Windows Server 2003 Administration Chapter 5 Administering File Resources.
70-270, MCSE/MCSA Guide to Installing and Managing Microsoft Windows XP Professional and Windows Server 2003 Chapter Nine Managing File System Access.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment Chapter 7: Advanced File System Management.
Operating Systems.
Database Management Systems (DBMS)
Operating System & Application Files BACS 371 Computer Forensics.
OS and Application Files BACS 371 Computer Forensics.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment, Enhanced Chapter 7: Advanced File System Management.
Copyright © 2002 ProsoftTraining. All rights reserved. Operating System Security.
Hands-On Microsoft Windows Server 2008 Chapter 5 Configuring, Managing, and Troubleshooting Resource Access.
Copyright 2007, Information Builders. Slide 1 Maintain & JavaScript: Two Great Tools that Work Great Together Mark Derwin and Mark Rawls Information Builders.
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.
70-290: MCSE Guide to Managing a Microsoft Windows Server 2003 Environment Chapter 7: Advanced File System Management.
5 Chapter Five Web Servers. 5 Chapter Objectives Learn about the Microsoft Personal Web Server Software Learn how to improve Web site performance Learn.
Copyright © 2007, Oracle. All rights reserved. Managing Concurrent Requests.
Linux+ Guide to Linux Certification, Second Edition
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.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
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.
CE Operating Systems Lecture 21 Operating Systems Protection with examples from Linux & Windows.
Downloading and Installing Autodesk Inventor Professional 2015 This is a 4 step process 1.Register with the Autodesk Student Community 2.Downloading the.
Linux Security. Authors:- Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel, of CodeSourcery LLC published by New Riders Publishing.
Week11: Race Conditions Pascal Meunier, Ph.D., M.Sc., CISSP November 1, 2006 Developed thanks to the support of Symantec Corporation, NSF SFS Capacity.
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
Manage Directories and Files in Linux Part 2. 2 Identify File Types in the Linux System The file types in Linux referred to as normal files and directories.
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
The Unix File system (UFS) Presented by: Gurpreet Singh Assistant Professor Department of School of Computing and Engineering Galgotias University.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
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.
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.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Protecting Memory What is there to protect in memory?
Processes and threads.
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Chapter 2: System Structures
File Management.
143a discussion session week 3
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
File System Management
CSE 153 Design of Operating Systems Winter 2019
Race Condition Vulnerability
Presentation transcript:

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 Program (Award Number ) and the Purdue e-Enterprise Center Copyright (2006 Purdue Research Foundation. All rights reserved.

Race Conditions (CWE ID 362)  Definition: security issues resulting from concurrent or interruptible code  Related concepts: –Concurrency (ID 557), Multi-threading (ID 366)  Thread safety (shared resources) –Interrupts  Re-entrant code (vs overwriting memory locations) –Atomicity –Transactions –Late binding of labels to objects –Security context switches (ID 368) –Filesystem links (ID 59)

Time of Check, Time of Use Problem (ID 367)  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: "switch" statement race condition (ID 365) –The variable used for the "switch" may change value during execution

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! (CWE ID 364) –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) –Special assembly code instructions for accessing and managing memory (e.g., multi-processor environments)

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  May introduce vulnerabilities –Unrestricted Critical Resource Lock (412)  CVE Predictable file names used for locking, allowing attacker to create the lock beforehand.

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"

What to Submit  Your code  A copy of terminal sessions showing that it operates correctly (as defined on the previous slide)  Due April 11 before start of class

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

Object Locks  The "synchronized" keyword (Java) locks an object – synchronized(this) { }  Be careful of conditions before entering a synchronized section, if the conditions depend on what happens in that section –Could trigger a race condition –Can happen in other languages as well

Example: Double-checked locking anti-pattern  Lazy initialization  if (helper == null) { synchronized(this) { if (helper == null) { helper = new Helper(); } } }  "helper" may be assigned a value by thread "A" before it is ready for use by other threads –Reference: "The "Double-Checked Locking is Broken" Declaration" at CheckedLocking.html

Problems in Context Switches (ID 368)  Context switch between operations done at different privilege levels and in different security contexts isn't done atomically  Case study: Web browsers –Navigating web sites with different security properties –CVE XSS when web browser executes Javascript events in the context of a new page while it's being loaded, allowing interaction with previous page in different domain.

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 and Windows Vista:  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  Shortcuts (.lnk files)

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

Windows Shortcuts (CWE ID 64)  Created with a.lnk file  Handled by the application, not automatically by the OS  Can link files to files, or urls to files  Someone typing in Internet Explorer may end up launching an executable instead of browsing therewww.microsoft.com –Firefox is not affected, because this is application- dependent  Reference: ck_is_a_feature_Microsoft/0, , ,00.htm

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 (CWE ID 62)  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 and Windows Vista –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 Symlink Attack (CWE ID 61)  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