CWE-243ΒΆ

Creation of chroot Jail Without Changing Working Directory. [Privilege-Issues, Improper-Control-Of-A-Resource-Through-Its-Lifetime]

Required inputs: IR

The product uses the chroot() system call to create a jail, but does not change the working directory afterward. This does not prevent access to files outside of the jail. Improper use of chroot() may allow attackers to escape from the chroot jail. The chroot() function call does not change the process's current working directory, so relative paths may still refer to file system resources outside of the chroot jail after chroot() has been called.
Background Details Demonstrative Examples
The chroot() system call allows a process to change its perception of the root directory of the file system. After properly invoking chroot(), a process cannot access any files outside the directory tree defined by the new root directory. Such an environment is called a chroot jail and is commonly used to prevent the possibility that a processes could be subverted and used to access unauthorized files. For instance, many FTP servers run in chroot jails to prevent an attacker who discovers a new vulnerability in the server from being able to download the password file or other sensitive files on the system.
Background Details Demonstrative Examples
Example 1

Consider the following source code from a (hypothetical) FTP server:

Example Language:C
    chroot("/var/ftproot");
    ...
    fgets(filename, sizeof(filename), network);
    localfile = fopen(filename, "r");
    while ((len = fread(buf, 1, sizeof(buf), localfile)) != EOF) {
        fwrite(buf, 1, sizeof(buf), network);
    }
    fclose(localfile);

This code is responsible for reading a filename from the network, opening the corresponding file on the local machine, and sending the contents over the network. This code could be used to implement the FTP GET command. The FTP server calls chroot() in its initialization routines in an attempt to prevent access to files outside of /var/ftproot. But because the server does not change the current working directory by calling chdir("/"), an attacker could request the file "../../../../../etc/passwd" and obtain a copy of the system password file.

Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

missing_cd

The program uses the chroot() system call to create a jail, but does not change the working directory afterward. This does not prevent access to files outside of the jail

None

False

Options