code-423n4 / 2021-10-ambire-findings

0 stars 0 forks source link

Compare with 0 and 1 in a more efficient way #15

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Handle

gpersoon

Vulnerability details

Impact

In the function setAddrPrivilege of Identity.sol the value of privileges[addr] is compare to 0 and 1 in the following way: "if (privileges[addr] != bytes32(0) && privileges[addr] != bytes32(uint(1)))"

As 0 and 1 are adjacent, you could also check "uint(privileges[addr]) > 1". This saves a (small amount) of gas.

Proof of Concept

https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/Identity.sol#L59

Tools Used

Recommended Mitigation Steps

replace if (privileges[addr] != bytes32(0) && privileges[addr] != bytes32(uint(1))) ... with if (uint(privileges[addr]) > 1) ...

Ivshti commented 3 years ago

resolved in https://github.com/AmbireTech/adex-protocol-eth/commit/f1f18f07ad7943c93132a25f0020f2dec07856cd

GalloDaSballo commented 3 years ago

Clever way of typecasting the priviliges in to uint to save gas

The sponsor has applied the improvement