Skip to content

Buffer overflow vulnerability in the Zephyr STM32 Crypto driver

Moderate
ceolin published GHSA-rhrc-pcxp-4453 Oct 26, 2023

Package

Zephyr (west)

Affected versions

<= 3.4.0

Patched versions

None

Description

Summary

I spotted a buffer overflow vulnerability at the following location in the Zephyr STM32 Crypto driver source code:
https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/crypto/crypto_stm32.c

Details

Buffer overflow due to ineffective assert check in /drivers/crypto/crypto_stm32.c:

static void copy_reverse_words(uint8_t *dst_buf, int dst_len,
			       uint8_t *src_buf, int src_len)
{
	int i;

	__ASSERT_NO_MSG(dst_len >= src_len); /* VULN: assert */
	__ASSERT_NO_MSG((dst_len % 4) == 0);

	memcpy(dst_buf, src_buf, src_len); /* VULN: buffer overflow */
	for (i = 0; i < dst_len; i += sizeof(uint32_t)) {
		sys_mem_swap(&dst_buf[i], sizeof(uint32_t));
	}
}

The function copy_reverse_words() is called in a few locations that might be problematic, because src_len might be attacker-controlled:

  • crypto_stm32_ctr_encrypt()
  • crypto_stm32_ctr_decrypt()
  • crypto_stm32_session_setup()

PoC

I haven't tried to reproduce these potential vulnerabilities against a live install of the Zephyr OS.

Impact

If the unchecked length above is confirmed to be attacker-controlled and if input crosses a security boundary, the impact of the buffer overflow vulnerability could range from denial of service to arbitrary code execution.

Patches

main: #61839

Severity

Moderate

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Local
Attack complexity
Low
Privileges required
Low
User interaction
None
Scope
Unchanged
Confidentiality
Low
Integrity
None
Availability
Low

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:L/I:N/A:L

CVE ID

CVE-2023-5139

Weaknesses

Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')

The product copies an input buffer to an output buffer without verifying that the size of the input buffer is less than the size of the output buffer, leading to a buffer overflow. Learn more on MITRE.

Credits