CVE-2022-29527 (published 2022-04-20T10:15Z)

Amazon AWS amazon-ssm-agent before 3.1.1208.0 creates a world-writable sudoers file, which allows local attackers to inject Sudo rules and escalate privileges to root. This occurs in certain situations involving a race condition.

Severity: MEDIUM
CVSS v2 base score: 6.9 (AV:L/AC:M/Au:N/C:C/I:C/A:C)
CVSS v3 base score: 7.0, HIGH (CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-362
cwe_name: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
description: The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
extended_description: 
            This can have security implications when the expected synchronization is in security-critical code, such as recording whether a user is authenticated or modifying important state information that should not be influenced by an outsider.
            A race condition occurs within concurrent environments, and is effectively a property of a code sequence. Depending on the context, a code sequence may be in the form of a function call, a small number of instructions, a series of program invocations, etc.
            A race condition violates these properties, which are closely related:
               
                  Exclusivity - the code sequence is given exclusive access to the shared resource, i.e., no other code sequence can modify properties of the shared resource before the original sequence has completed execution.
                  Atomicity - the code sequence is behaviorally atomic, i.e., no other thread or process can concurrently execute the same sequence of instructions (or a subset) against the same resource.
               
            A race condition exists when an "interfering code sequence" can still access the shared resource, violating exclusivity. Programmers may assume that certain code sequences execute too quickly to be affected by an interfering code sequence; when they are not, this violates atomicity. For example, the single "x++" statement may appear atomic at the code layer, but it is actually non-atomic at the instruction layer, since it involves a read (the original value of x), followed by a computation (x+1), followed by a write (save the result to x).
            The interfering code sequence could be "trusted" or "untrusted." A trusted interfering code sequence occurs within the product; it cannot be modified by the attacker, and it can only be invoked indirectly. An untrusted interfering code sequence can be authored directly by the attacker, and typically it is external to the vulnerable product.
         
         
cwe_id: CWE-732
cwe_name: 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.
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.
         

Fix information:
repo_url: https://github.com/aws/amazon-ssm-agent
commit_details: repo_url: https://github.com/aws/amazon-ssm-agent
author: Yuting Fan
author_date: 2022-03-31 14:01:00-07:00
msg: Create ssm-agent-users sudoer file with constrained file permission

cr: https://code.amazon.com/reviews/CR-67315538
repository: repo_url: https://github.com/aws/amazon-ssm-agent
repo_name: aws/amazon-ssm-agent
description: An agent to enable remote management of your EC2 instances, on-premises servers, or virtual machines (VMs).
repo_language: Go
owner: aws
file_changes: filename: utility_unix.go
change_type: ModificationType.MODIFY
diff: @@ -37,12 +37,13 @@ var ShellPluginCommandName = "sh"
 var ShellPluginCommandArgs = []string{"-c"}
 
 const (
-	sudoersFile     = "/etc/sudoers.d/ssm-agent-users"
-	sudoersFileMode = 0440
-	fs_ioc_getflags = uintptr(0x80086601)
-	fs_ioc_setflags = uintptr(0x40086602)
-	FS_APPEND_FL    = 0x00000020 /* writes to file may only append */
-	FS_RESET_FL     = 0x00000000 /* reset file property */
+	sudoersFile                = "/etc/sudoers.d/ssm-agent-users"
+	sudoersFileCreateWriteMode = 0640
+	sudoersFileReadOnlyMode    = 0440
+	fs_ioc_getflags            = uintptr(0x80086601)
+	fs_ioc_setflags            = uintptr(0x40086602)
+	FS_APPEND_FL               = 0x00000020 /* writes to file may only append */
+	FS_RESET_FL                = 0x00000000 /* reset file property */
 )
 
 // ResetPasswordIfDefaultUserExists resets default RunAs user password if user exists
@@ -106,8 +107,8 @@ func (u *SessionUtil) createSudoersFileIfNotPresent(log log.T) error {
 		return err
 	}
 
-	// Create a sudoers file for ssm-user
-	file, err := os.Create(sudoersFile)
+	// Create a sudoers file for ssm-user with read/write access
+	file, err := os.OpenFile(sudoersFile, os.O_WRONLY|os.O_CREATE, sudoersFileCreateWriteMode)
 	if err != nil {
 		log.Errorf("Failed to add %s to sudoers file: %v", appconfig.DefaultRunAsUserName, err)
 		return err
@@ -132,12 +133,12 @@ func (u *SessionUtil) createSudoersFileIfNotPresent(log log.T) error {
 // changeModeOfSudoersFile will change the sudoersFile mode to 0440 (read only).
 // This file is created with mode 0666 using os.Create() so needs to be updated to read only with chmod.
 func (u *SessionUtil) changeModeOfSudoersFile(log log.T) error {
-	fileMode := os.FileMode(sudoersFileMode)
+	fileMode := os.FileMode(sudoersFileReadOnlyMode)
 	if err := os.Chmod(sudoersFile, fileMode); err != nil {
-		log.Errorf("Failed to change mode of %s to %d: %v", sudoersFile, sudoersFileMode, err)
+		log.Errorf("Failed to change mode of %s to %d: %v", sudoersFile, sudoersFileReadOnlyMode, err)
 		return err
 	}
-	log.Infof("Successfully changed mode of %s to %d", sudoersFile, sudoersFileMode)
+	log.Infof("Successfully changed mode of %s to %d", sudoersFile, sudoersFileReadOnlyMode)
 	return nil
 }
 

programming_language: Go