CVE-2023-40014 (published 2023-08-10T20:15Z)

OpenZeppelin Contracts is a library for secure smart contract development. Starting in version 4.0.0 and prior to version 4.9.3, contracts using `ERC2771Context` along with a custom trusted forwarder may see `_msgSender` return `address(0)` in calls that originate from the forwarder with calldata shorter than 20 bytes. This combination of circumstances does not appear to be common, in particular it is not the case for `MinimalForwarder` from OpenZeppelin Contracts, or any deployed forwarder the team is aware of, given that the signer address is appended to all calls that originate from these forwarders. The problem has been patched in v4.9.3.\n

CVSS v3 base score: 5.3, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N)

Weakness information:
cwe_id: CWE-116
cwe_name: Improper Encoding or Escaping of Output
description: The product prepares a structured message for communication with another component, but encoding or escaping of the data is either missing or done incorrectly. As a result, the intended structure of the message is not preserved.
extended_description: 
            Improper encoding or escaping can allow attackers to change the commands that are sent to another component, inserting malicious commands instead.
            Most products follow a certain protocol that uses structured messages for communication between components, such as queries or commands. These structured messages can contain raw data interspersed with metadata or control information. For example, "GET /index.html HTTP/1.1" is a structured message containing a command ("GET") with a single argument ("/index.html") and metadata about which protocol version is being used ("HTTP/1.1").
            If an application uses attacker-supplied inputs to construct a structured message without properly encoding or escaping, then the attacker could insert special characters that will cause the data to be interpreted as control information or metadata. Consequently, the component that receives the output will perform the wrong operations, or otherwise interpret the data incorrectly.
         
         

Fix information:
repo_url: https://github.com/OpenZeppelin/openzeppelin-contracts
commit_details: repo_url: https://github.com/OpenZeppelin/openzeppelin-contracts
author: Francisco
author_date: 2023-07-27 22:30:41+02:00
msg: Adjust ERC2771Context._msgData for msg.data.length < 20 (#4484)
repository: repo_url: https://github.com/OpenZeppelin/openzeppelin-contracts
repo_name: OpenZeppelin/openzeppelin-contracts
description: OpenZeppelin Contracts is a library for secure smart contract development.
repo_language: JavaScript
owner: OpenZeppelin
file_changes: filename: warm-guests-rule.md
change_type: ModificationType.ADD
diff: @@ -0,0 +1,5 @@
+---
+'openzeppelin-solidity': patch
+---
+
+`ERC2771Context`: Prevent revert in `_msgData()` when a call originating from a trusted forwarder is not long enough to contain the request signer address (i.e. `msg.data.length` is less than 20 bytes). Return the full calldata in that case.

programming_language: Markdown
filename: ERC2771Context.sol
change_type: ModificationType.MODIFY
diff: @@ -34,7 +34,7 @@ abstract contract ERC2771Context is Context {
     }
 
     function _msgData() internal view virtual override returns (bytes calldata) {
-        if (isTrustedForwarder(msg.sender)) {
+        if (isTrustedForwarder(msg.sender) && msg.data.length >= 20) {
             return msg.data[:msg.data.length - 20];
         } else {
             return super._msgData();

programming_language: JavaScript
filename: ContextMock.sol
change_type: ModificationType.MODIFY
diff: @@ -16,6 +16,12 @@ contract ContextMock is Context {
     function msgData(uint256 integerValue, string memory stringValue) public {
         emit Data(_msgData(), integerValue, stringValue);
     }
+
+    event DataShort(bytes data);
+
+    function msgDataShort() public {
+        emit DataShort(_msgData());
+    }
 }
 
 contract ContextMockCaller {

programming_language: TypeScript
filename: ERC2771Context.test.js
change_type: ModificationType.MODIFY
diff: @@ -12,7 +12,7 @@ const ContextMockCaller = artifacts.require('ContextMockCaller');
 const { shouldBehaveLikeRegularContext } = require('../utils/Context.behavior');
 
 contract('ERC2771Context', function (accounts) {
-  const [, anotherAccount] = accounts;
+  const [, trustedForwarder] = accounts;
 
   const MAX_UINT48 = web3.utils.toBN(1).shln(48).subn(1).toString();
 
@@ -84,11 +84,11 @@ contract('ERC2771Context', function (accounts) {
 
       it('returns the original sender when calldata length is less than 20 bytes (address length)', async function () {
         // The forwarder doesn't produce calls with calldata length less than 20 bytes
-        const recipient = await ERC2771ContextMock.new(anotherAccount);
+        const recipient = await ERC2771ContextMock.new(trustedForwarder);
 
-        const { receipt } = await recipient.msgSender({ from: anotherAccount });
+        const { receipt } = await recipient.msgSender({ from: trustedForwarder });
 
-        await expectEvent(receipt, 'Sender', { sender: anotherAccount });
+        await expectEvent(receipt, 'Sender', { sender: trustedForwarder });
       });
     });
 
@@ -117,5 +117,15 @@ contract('ERC2771Context', function (accounts) {
         await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Data', { data, integerValue, stringValue });
       });
     });
+
+    it('returns the full original data when calldata length is less than 20 bytes (address length)', async function () {
+      // The forwarder doesn't produce calls with calldata length less than 20 bytes
+      const recipient = await ERC2771ContextMock.new(trustedForwarder);
+
+      const { receipt } = await recipient.msgDataShort({ from: trustedForwarder });
+
+      const data = recipient.contract.methods.msgDataShort().encodeABI();
+      await expectEvent(receipt, 'DataShort', { data });
+    });
   });
 });

programming_language: TypeScript