The following code sets the umask of the process to 0 before creating a file and writing "Hello world" into the file.
After running this program on a UNIX system, running the "ls -l" command might return the following output:
BadC
#define OUTFILE "hello.out" umask(0);FILE *out; /* Ignore link following (CWE-59) for brevity */ out = fopen(OUTFILE, "w");if (out) {fprintf(out, "hello world!\n");fclose(out);}
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.
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.
BadPHP
function createUserDir($username){$path = '/home/'.$username;if(!mkdir($path)){return false;}if(!chown($path,$username)){rmdir($path);return false;}return true;}
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.
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:
BadPerl
$fileName = "secretFile.out"; if (-e $fileName) {chmod 0777, $fileName;} my $outFH;if (! open($outFH, ">>$fileName")) {ExitError("Couldn't append to $fileName: $!");}my $dateString = FormatCurrentTime();my $status = IsHostAlive("cwe.mitre.org");print $outFH "$dateString cwe status: $status!\n";close($outFH);
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.
BadGo
const adminFile = "/etc/admin-users" func createAdminFileIfNotExists() error { file, err := os.Create(adminFile) if err != nil { return err } return nil } func changeModeOfAdminFile() error { fileMode := os.FileMode(0440) if err := os.Chmod(adminFile, fileMode); err != nil { return err } return nil }
The following command recursively sets world-readable permissions for a directory and all of its children:
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.
BadShell
chmod -R ugo+r DIRNAME
The following Azure command updates the settings for a storage account:
However, "Allow Blob Public Access" is set to true, meaning that anonymous/public users can access blobs.
BadShell
az storage account update --name <storage-account> --resource-group <resource-group> --allow-blob-public-access true
The following Azure command updates the settings for a storage account:
However, "Allow Blob Public Access" is set to true, meaning that anonymous/public users can access blobs.
GoodShell
az storage account update --name <storage-account> --resource-group <resource-group> --allow-blob-public-access false
The following Google Cloud Storage command gets the settings for a storage account named 'BUCKET_NAME':
Suppose the command returns the following result:
BadJSON
{ "bindings":[{ "members":[ "projectEditor: PROJECT-ID", "projectOwner: PROJECT-ID" ], "role":"roles/storage.legacyBucketOwner" }, { "members":[ "allUsers", "projectViewer: PROJECT-ID" ], "role":"roles/storage.legacyBucketReader" } ] }
The following Google Cloud Storage command gets the settings for a storage account named 'BUCKET_NAME':
Suppose the command returns the following result:
GoodShell
gsutil iam ch -d allUsers gs://BUCKET_NAME gsutil iam ch -d allAuthenticatedUsers gs://BUCKET_NAME