How to Open a File and Not Get Hacked

How to Open a File and Not Get Hacked

How to Open a File and Not Get Hacked JamesA.Kupsch BartonP.Miller Computer Sciences Department University of Wisconsin Madison, WI 53706-1685 USA {kupsch,bart}@cs.wisc.edu Abstract or modification of files in unexpected locations as in IBM’s DB2 [3] and Xsession [2], and (4) weak file permissions Careless attention to opening files, often caused by prob- resulting from incorrect use of the API. lems with path traversal or shared directories, can expose This paper is organized into two main parts. The first applications to attacks on the file names that they use. In part (Section 2) shows how to detect if a file name is safe this paper we present criteria to determine if a path is safe from adversarial attacks; we encapsulate this detection in an from attack and how previous algorithms are not sufficient algorithm that checks if a file name is a trusted path. The to protect against such attacks. We then describe an algo- second part (Section 3) describes functions that are safe re- rithm to safely open a file when in the presence of an attack placements for the standard library functions for opening (and how to detect the presence of such an attack), and pro- and creating files. These functions also provide a mech- vide a new library of file open routines that embodies our anism for an application to detect if file names used with algorithm. These routines can be used as one-for-one sub- these functions are being manipulated to refer to different stitutes for conventional POSIX open and fopen calls. file system objects as might occur during an active attack on the file name. The use of these new routines alone does not guarantee 1. Introduction correct security behavior as it is still possible for a design flaw or coding errors in the use of these functions to cause a vulnerability in the program. However the use of our new Common programming idioms can allow adversaries to routines should significantly reduce the risk of many com- violate security constraints. Some of these programming mon security exploits. idioms that allow exploits to occur involve opening, creat- ing and performing other operations on files. In this paper we focus on opening and performing file operations in such 2. Trusted Paths way that an adversary will not be able to subvert security. In particular we are assuming the adversary has local ac- A path is a string used to refer to files to perform op- cess to the machine running the program, but not as a user erations such as opening a file. An example of a path is the program must trust, such as the root account. The ac- /home/user/report.pdf. Each time a path is presented cess to the untrusted account may be achieved by numerous to the operating system, it traverses the file system direc- means, including having a legitimate account, breaking into tory by directory to find the file system object referred to the account, or by exploiting a service with a network inter- by the path. If an attacker can manipulate this traversal by face. We assume the adversary has the capability to create, making changes to directory entries, the same path can refer remove and otherwise manipulate files and directories any- to different file system objects and can be used to exploit a where the permissions of an untrusted account allows. programby making it think that it is acting on one file when Specific types of attacks that can occur against opening it is really acting on a different one. include (1) race conditions when the idiom uses a file name This section describes how to assess if a path traversal or multiple times such as between checking for the existence contentsof a file can be manipulatedby a malicioususerin a of a file and creating it [4, pages 528–530], (2) race condi- POSIX [7] environment as found in operating systems such tion between opening or creating the file and checking the as UNIX and Linux. File system objects have one owning ownership and permissions of the file to prevent confiden- user and groupid, plus three sets of capabilities (the set cho- tial data from being disclosed as in Globus’s gt4 [5], (3) in- sen is based on the file’s owning user id, group id, and those advertently following symbolic links allowing the creation of the process). Of the capabilities in each set, the paper is This research funded in part by National Science Foundation under subcontract with San Diego Supercomputer Center and NATO CLG-983049. c 2008 IEEE. Personal use of this material is permitted. However, permission to reprint/republish this material for advertising or promotional purposes or for creating new collective works for resale or redistribution to servers or lists, or to reuse any copyrighted component of this work in other works must be obtained from the IEEE. concerned with the write flag as it controls modifications to an untrusted directory along the path traversal to make the the object. path refer to any arbitrary file system object to be opened or To meet security requirements, programs often need as- possibly created. surances that the contents of a file could have only been Hard link attack - is an attack where it appears that a modifiedby processesthatwere run with a userid and group trusted process created a file with a particular path, when ids from a set that the program trusts not to be malicious. it did not. The attack is possible in any directory in which The set of ids that the program trusts not to be malicious an untrusted process can create a file, such as a sticky bit will be referred to as trusted ids. A process that has at least directory like /tmp. The attack is based on the fact that one trusted id for its user or groups will be referred to as any process can create a hard link to any file regardless of a trusted process. The root user id is trusted, since file ac- the file’s ownership and permissions. This link can be cre- cess for a process running with the root user id is always ated anywhere the untrusted process could create an ordi- granted. Other trusted ids are determined by the applica- nary file. The newly created hard link is indistinguishable tions requirements, and may also vary from file to file. For from the original file and any changes made to one are seen instance, an application may use a trusted id list of root for by the other including ownership, permissions and contents. its configuration file, and root and app-user for its data files. If an application uses /tmp directly for storing files and in- A trusted path is one where the permissions are such that correctly assumes any file with the correct ownership and only a trusted set of user and group ids can manipulate the permissions are trusted files, then an attacker can create ad- traversal to the file system object or contents of the object ditional files with these properties at a given path by using referred to by the path. Only trusted processes can modify a hard link to an existing file. the meaning of a trusted path, and they should not modify For this reason, any file system object that allows a hard the path in a way that violates the security requirements of link (everything except directories) should never be trusted the program. These properties allow a trusted path to be in a sticky bit directory. used in ways that would create security problems if the path Race conditions - if the program uses the same path were not trusted, such as assuming the contents of a file multiple times in system calls, combinations of the previ- do not change, or multiple uses of a path always access the ously described attacks can be used to make the program same file. A function to determine if a path is trusted is not a access one file system object in one call and different one in standard operating system or library service, and published a subsequent call. If some property of the file is checked in algorithms are deficient in some respect. the first call and then conditionally used in subsequent calls, The rest of this section presents the types of exploits pos- this is known as a time of check, time of use (TOCTOU) at- sible if a path is not trusted, how to check if a path is trusted, tack. and a list of properties that an algorithm should have to de- termine the trust of a path. We then describe two previous 2.2. Checking the trust of a path algorithms for determining if a path is trusted, and evaluate how well they meet the properties given. Finally we present This section explains how to determine if a path is a new algorithm that satisfies all the desired properties. trusted. First, we show how to assess if a directory entry is trusted. Then, we show how to use a directory entry be- 2.1. Possible Attacks ing trusted to assess if a whole path is trusted. Finally, we discuss how to tell if a file system object is trusted, which These are different types of exploits that an untrusted means that there is a path to the file system object that is a process could accomplish if a path is untrusted and would trusted path. There are three possible outcomes for trust: be prevented if the path is trusted.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us