CWE-732: Incorrect Permission Assignment for Critical Resource

Description

The product specifies permissions for a security-critical resource in a way that allows that resource to be read or modified by unintended actors.

Submission Date :

Sept. 8, 2008, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

When a resource is given a permission setting that provides access to a wider range of actors than required, it could lead to the exposure of sensitive information, or the modification of that resource by unintended parties. This is especially dangerous when the resource is related to program configuration, execution, or sensitive user data. For example, consider a misconfigured storage account for the cloud that can be read or written by a public or anonymous user.

Example Vulnerable Codes

Example - 1

The following code sets the umask of the process to 0 before creating a file and writing "Hello world" into the file.


// /* Ignore link following (CWE-59) for brevity */// 

fprintf(out, "hello world!\n");fclose(out);#define OUTFILE "hello.out"umask(0);FILE *out;out = fopen(OUTFILE, "w");if (out) {}

After running this program on a UNIX system, running the "ls -l" command might return the following output:

-rw-rw-rw- 1 username 13 Nov 24 17:58 hello.out

The "rw-rw-rw-" string indicates that the owner, group, and world (all users) can read the file and write to it.

Example - 2

This code creates a home directory for a new user, and makes that user the owner of the directory. If the new directory cannot be owned by the user, the directory is deleted.


return false;

rmdir($path);return false;
$path = '/home/'.$username;if(!mkdir($path)){}if(!chown($path,$username)){}return true;function createUserDir($username){}

Because the optional "mode" argument is omitted from the call to mkdir(), the directory is created with the default permissions 0777. Simply setting the new user as the owner of the directory does not explicitly change the permissions of the directory, leaving it with the default. This default allows any user to read and write to the directory, allowing an attack on the user's files. The code also fails to change the owner group of the directory, which may result in access by unexpected groups.

This code may also be vulnerable to Path Traversal (CWE-22) attacks if an attacker supplies a non alphanumeric username.

Example - 3

The following code snippet might be used as a monitor to periodically record whether a web site is alive. To ensure that the file can always be modified, the code uses chmod() to make the file world-writable.


chmod 0777, $fileName;
ExitError("Couldn't append to $fileName: $!");
$fileName = "secretFile.out";if (-e $fileName) {}my $outFH;if (! open($outFH, ">>$fileName")) {}my $dateString = FormatCurrentTime();my $status = IsHostAlive("cwe.mitre.org");print $outFH "$dateString cwe status: $status!\n";close($outFH);

The first time the program runs, it might create a new file that inherits the permissions from its environment. A file listing might look like:

-rw-r--r-- 1 username 13 Nov 24 17:58 secretFile.out

This listing might occur when the user has a default umask of 022, which is a common setting. Depending on the nature of the file, the user might not have intended to make it readable by everyone on the system.

The next time the program runs, however - and all subsequent executions - the chmod will set the file's permissions so that the owner, group, and world (all users) can read the file and write to it:

-rw-rw-rw- 1 username 13 Nov 24 17:58 secretFile.out

Perhaps the programmer tried to do this because a different process uses different permissions that might prevent the file from being updated.

Example - 4

This program creates and reads from an admin file to determine privilege information.

If the admin file doesn't exist, the program will create one. In order to create the file, the program must have write privileges to write to the file. After the file is created, the permissions need to be changed to read only.



return err
file, err := os.Create(adminFile)if err != nil {}return nilconst adminFile = "/etc/admin-users"func createAdminFileIfNotExists() error {}

return err
fileMode := os.FileMode(0440)if err := os.Chmod(adminFile, fileMode); err != nil {}return nilfunc changeModeOfAdminFile() error {}

os.Create will create a file with 0666 permissions before umask if the specified file does not exist. A typical umask of 0022 would result in the file having 0644 permissions. That is, the file would have world-writable and world-readable permissions.

In this scenario, it is advised to use the more customizable method of os.OpenFile with the os.O_WRONLY and os.O_CREATE flags specifying 0640 permissions to create the admin file.

This is because on a typical system where the umask is 0022, the perm 0640 applied in os.OpenFile will result in a file of 0620 where only the owner and group can write.

Example - 5

The following command recursively sets world-readable permissions for a directory and all of its children:

chmod -R ugo+r DIRNAME

If this command is run from a program, the person calling the program might not expect that all the files under the directory will be world-readable. If the directory is expected to contain private data, this could become a security problem.

Example - 6

The following Azure command updates the settings for a storage account:

az storage account update --name <storage-account> --resource-group <resource-group> --allow-blob-public-access true

However, "Allow Blob Public Access" is set to true, meaning that anonymous/public users can access blobs.

The command could be modified to disable "Allow Blob Public Access" by setting it to false.

az storage account update --name <storage-account> --resource-group <resource-group> --allow-blob-public-access false

Example - 7

The following Google Cloud Storage command gets the settings for a storage account named 'BUCKET_NAME':


gsutil iam get gs://BUCKET_NAME

Suppose the command returns the following result:





"projectEditor: PROJECT-ID","projectOwner: PROJECT-ID"
"members":[],"role":"roles/storage.legacyBucketOwner"

"allUsers","projectViewer: PROJECT-ID"],"role":"roles/storage.legacyBucketReader"
"members":[}
"bindings":[{},{]
{}

This result includes the "allUsers" or IAM role added as members, causing this policy configuration to allow public access to cloud storage resources. There would be a similar concern if "allAuthenticatedUsers" was present.

The command could be modified to remove "allUsers" and/or "allAuthenticatedUsers" as follows:


gsutil iam ch -d allUsers gs://BUCKET_NAMEgsutil iam ch -d allAuthenticatedUsers gs://BUCKET_NAME

Visit http://cwe.mitre.org/ for more details.

© cvefeed.io
Latest DB Update: Nov. 24, 2024 4:20