CWE-1389: Incorrect Parsing of Numbers with Different Radices (Base)

Description: The product parses numeric input assuming base 10 (decimal) values, but it does not account for inputs that use a different base number (radix).

Extended Description: Frequently, a numeric input that begins with 0 is treated as octal, or 0x causes it to be treated as hexadecimal, e.g. by the inet_addr() function. For example, 023 (octal) is 35 decimal, or 0x31 is 49 decimal. Other bases may be used as well. If the developer assumes decimal-only inputs, the code could produce incorrect numbers when the inputs are parsed using a different base. This can result in unexpected and/or dangerous behavior. For example, a 0127.0.0.1 IP address is parsed as octal due to the leading 0, whose numeric value would be the same as 87.0.0.1 (decimal), where the developer likely expected to use 127.0.0.1. The consequences vary depending on the surrounding code in which this weakness occurs, but they can include bypassing network-based access control using unexpected IP addresses or netmasks, or causing apparently-symbolic identifiers to be processed as if they are numbers. In web applications, this can enable bypassing of SSRF restrictions.



Applicable Platforms: ::LANGUAGE CLASS:Not Language-Specific:LANGUAGE PREVALENCE:Undetermined::TECHNOLOGY CLASS:Not Technology-Specific:TECHNOLOGY PREVALENCE:Undetermined::

Modes of Introduction: ::PHASE:Implementation:NOTE:Input validation used may assume decimal bases during conditional checks, when it may not always be the case.::PHASE:Implementation:NOTE:The application may rely on a service that supports different numerical bases.::



Common Consequences: ::SCOPE:Confidentiality:IMPACT:Read Application Data:LIKELIHOOD:Unknown:NOTE:An attacker may use an unexpected numerical base to access private application resources.::SCOPE:Integrity:IMPACT:Bypass Protection Mechanism:IMPACT:Alter Execution Logic:LIKELIHOOD:Unknown:NOTE:An attacker may use an unexpected numerical base to bypass or manipulate access control mechanisms.::


Potential Mitigations: ::PHASE:Implementation:STRATEGY:Enforcement by Conversion:DESCRIPTION:If only decimal-based values are expected in the application, conditional checks should be created in a way that prevent octal or hexadecimal strings from being checked. This can be achieved by converting any numerical string to an explicit base-10 integer prior to the conditional check, to prevent octal or hex values from ever being checked against the condition.::PHASE:Implementation:STRATEGY:Input Validation:DESCRIPTION:If various numerical bases do need to be supported, check for leading values indicating the non-decimal base you wish to support (such as 0x for hex) and convert the numeric strings to integers of the respective base. Reject any other alternative-base string that is not intentionally supported by the application.::PHASE:Implementation:STRATEGY:Input Validation:DESCRIPTION:If regular expressions are used to validate IP addresses, ensure that they are bounded using ^ and $ to prevent base-prepended IP addresses from being matched.::

Observed Examples: ::REFERENCE:CVE-2021-29662:DESCRIPTION:Chain: Use of zero-prepended IP addresses in Perl-based IP validation module can lead to an access control bypass.:LINK:https://www.cve.org/CVERecord?id=CVE-2021-29662::REFERENCE:CVE-2021-28918:DESCRIPTION:Chain: Use of zero-prepended IP addresses in a product that manages IP blocks can lead to an SSRF.:LINK:https://www.cve.org/CVERecord?id=CVE-2021-28918::REFERENCE:CVE-2021-29921:DESCRIPTION:Chain: Use of zero-prepended IP addresses in a Python standard library package can lead to an SSRF.:LINK:https://www.cve.org/CVERecord?id=CVE-2021-29921::REFERENCE:CVE-2021-29923:DESCRIPTION:Chain: Use of zero-prepended IP addresses in the net Golang library can lead to an access control bypass.:LINK:https://www.cve.org/CVERecord?id=CVE-2021-29923::REFERENCE:CVE-2021-29424:DESCRIPTION:Chain: Use of zero-prepended IP addresses in Perl netmask module allows bypass of IP-based access control.:LINK:https://www.cve.org/CVERecord?id=CVE-2021-29424::REFERENCE:CVE-2016-4029:DESCRIPTION:Chain: incorrect validation of intended decimal-based IP address format (CWE-1286) enables parsing of octal or hexadecimal formats (CWE-1389), allowing bypass of an SSRF protection mechanism (CWE-918).:LINK:https://www.cve.org/CVERecord?id=CVE-2016-4029::REFERENCE:CVE-2020-13776:DESCRIPTION:Mishandling of hex-valued usernames leads to unexpected decimal conversion and privilege escalation in the systemd Linux suite.:LINK:https://www.cve.org/CVERecord?id=CVE-2020-13776::