Skip to content
Smart contracts Audit finding Intermediate

HexUtils.hexStringToBytes32() and HexUtils.hexToAddress() may return incorrect results

The HexUtils.hexStringToBytes32() and hexToAddress() functions do not validate the length of the input hex string, so they can return incorrect results when the input is not exactly 32 (or 20) bytes. An attacker could exploit this to cause the contract to behave abnormally or for malicious purposes.

hihen Follow with chaduke, eierina and 3 others

Originally disclosed on Code4rena

Submitted by hihen, also found by chaduke, eierina, AkshaySrivastav, Kek, and chaduke

https://github.com/code-423n4/2023-04-ens/blob/45ea10bacb2a398e14d711fe28d1738271cd7640/contracts/utils/HexUtils.sol#L11 https://github.com/code-423n4/2023-04-ens/blob/45ea10bacb2a398e14d711fe28d1738271cd7640/contracts/utils/HexUtils.sol#L68

The HexUtils.hexStringToBytes32() and HexUtils.hexToAddress() may return incorrect results if the input data provided is not in a standard format.

This could cause the contract to behave abnormally in some scenarios or be exploited for malicious purposes.

Proof of Concept

The function HexUtils.hexStringToBytes32(bytes memory str, uint256 idx, uint256 lastIdx) is used to convert a hexadecimal string str[idx...lastIndx] to a bytes32.

However, the function lacks some critical checks on the input data, resulting in the following situations:

  1. If the length lastIdx - idx is odd, it will not revert, but will read an additional out-of-range byte str[lastIdx] and return it.
  2. If the length lstIdx - idx > 32, it will not revert, but will discard the excess data at the beginning and return the last 32 bytes.
  3. If the length lstIdx - idx < 32, it will not revert, but will pad the data with zeros at the beginning.

The following test code verifies these situations:

diff --git a/test/utils/HexUtils.js b/test/utils/HexUtils.js
index 296eadf..e12e11c 100644
--- a/test/utils/HexUtils.js
+++ b/test/utils/HexUtils.js
@@ -16,6 +16,44 @@ describe('HexUtils', () => {
     HexUtils = await HexUtilsFactory.deploy()
   })

+  describe.only('Special cases for hexStringToBytes32()', () => {
+    const hex32Bytes = '5cee339e13375638553bdf5a6e36ba80fb9f6a4f0783680884d92b558aa471da'
+    it('odd length 1', async () => {
+      let [bytes32, valid] = await HexUtils.hexStringToBytes32(
+        toUtf8Bytes(hex32Bytes), 0, 63,
+      )
+      expect(valid).to.equal(true)
+      // the last 4 bits (half byte) of hex32Bytes is out of range but read
+      expect(bytes32).to.equal('0x' + hex32Bytes)
+    })
+    it('odd length 2', async () => {
+      let [bytes32, valid] = await HexUtils.hexStringToBytes32(
+        toUtf8Bytes(hex32Bytes + '00'), 1, 64,
+      )
+      expect(valid).to.equal(true)
+      // the first half byte of '00' is out of range but read
+      expect(bytes32).to.equal('0x' + hex32Bytes.substring(1) + '0')
+    })
+    it('not enough length', async () => {
+      let [bytes32, valid] = await HexUtils.hexStringToBytes32(
+        toUtf8Bytes(hex32Bytes), 0, 2,
+      )
+      expect(valid).to.equal(true)
+      // only one byte is read, but it is expanded to 32 bytes
+      expect(bytes32).to.equal(
+        '0x000000000000000000000000000000000000000000000000000000000000005c',
+      )
+    })
+    it('exceed length', async () => {
+      let [bytes32, valid] = await HexUtils.hexStringToBytes32(
+        toUtf8Bytes(hex32Bytes + "1234"), 0, 64 + 4,
+      )
+      expect(valid).to.equal(true)
+      // 34 bytes is read, and returns the last 32 bytes
+      expect(bytes32).to.equal('0x' + hex32Bytes.substring(4) + '1234')
+    })
+  })
+
   describe('hexStringToBytes32()', () => {
     it('Converts a hex string to bytes32', async () => {
       let [bytes32, valid] = await HexUtils.hexStringToBytes32(

Test code outputs:

  HexUtils
    Special cases for hexStringToBytes32()
      ✓ odd length 1
      ✓ odd length 2
      ✓ not enough length
      ✓ exceed length

Since HexUtils.hexToAddress() is implemented by directly calling HexUtils.hexStringToBytes32(), it also has similar issues.

Tools Used

VS Code

Recommended Mitigation Steps

Should revert the function if the input length lastIdx - idx is odd.

For cases where the length is greater than or less than 32 (or 20)

  • if the current implementation meets the requirements, the design should be detailed in a comment
  • otherwise the function should revert if the length is not 32 (or 20)

Arachnid (ENS) confirmed


The original report as published on Code4rena, by hihen. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The functions lack checks on the input length, allowing odd-length strings to read an extra byte, and strings longer or shorter than the expected size to be silently truncated or zero-padded.

Impact

The contract could behave abnormally in some scenarios or be exploited for malicious purposes.

What the attacker needed

None

The fix

The function should revert if the input length is odd, and should either revert or document the behaviour for lengths not equal to 32 (or 20).

How it was found

  1. Call hexStringToBytes32 with an odd-length range (e.g., 0 to 63) and observe it returns a value instead of reverting.
  2. Call hexStringToBytes32 with a range longer than 32 bytes and observe it returns the last 32 bytes.
  3. Call hexStringToBytes32 with a range shorter than 32 bytes and observe it returns a zero-padded value.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

These functions convert a hexadecimal string (a string of characters representing bytes) into a fixed-size value. They do not check that the string has the right length, so if it is too short, too long, or has an odd number of characters, the function still returns a value instead of an error. This can lead to unexpected behaviour in the smart contract.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by hihen

View all

10 more disclosed reports, best first.

More from ENS

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.