Table of contents
Introduction:
What the heck is access control???
In software development, access control refers to the practice of granting users or administrators the ability to access certain functions or options and modify certain things based on predefined criteria.
What is the use of it in Solidity?
In Solidity, access control restricts certain functions or variables to specific parties using modifiers. This ensures the security and integrity of smart contracts by preventing unauthorized access or modifications.
Incorrect use or placement of access controls in functions can create serious security risks. Important functions with sensitive information should include address checks (like "owner" or "controller") to ensure only authorized parties can access them. This is crucial when using modifiers, as failing to add these checks can let hackers exploit critical logic.
How does this attack happen?
Access control in Solidity restricts functions or variables to specific parties to ensure security. Despite this, attackers can sometimes find ways to access a contract's private values and logic. There are various vulnerabilities that can occur in Solidity contracts, including:
Using
tx.origin
(deprecated) for checking the addressUsing lengthy authorization logic with multiple
require
statementsMaking reckless delegate calls to proxy libraries or contracts
Code example:
In the contract code below, anyone can access the function and set the address to authorized. This lack of proper access control means that any user, regardless of their permissions, can call this function and assign themselves or others as authorized users. This can lead to unauthorized access and potential misuse of the contract's functionality. The function should include proper checks to ensure that only specific, trusted addresses (like the contract owner or an admin) can modify the authorization status. Here is an example of such a vulnerable code snippet:
pragma solidity ^0.8.0;
contract AccessControlExample {
address public authorizedAddress;
function setAuthorizedAddress(address _address) public {
authorizedAddress = _address;
}
}
In this example, the setAuthorizedAddress
function is publicly accessible, allowing anyone to call it and change the authorizedAddress
. To fix this, we should add a modifier that restricts access to this function, ensuring only the contract owner or another trusted entity can call it. Here is an improved version of the code:
pragma solidity ^0.8.0;
contract AccessControlExample {
address public owner;
address public authorizedAddress;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
function setAuthorizedAddress(address _address) public onlyOwner {
authorizedAddress = _address;
}
}
Conclusion:
In conclusion, proper access control in smart contracts is essential to prevent unauthorized changes and security breaches. The initial example showed a vulnerability where the setAuthorizedAddress
function was publicly accessible, allowing any user to change the authorizedAddress
. To fix this, an improved version of the contract was introduced with an onlyOwner
modifier, ensuring only the contract owner can call the function. This added security restricts access to trusted entities, protecting the contract from unauthorized changes. Implementing such access control measures is a best practice in smart contract development to maintain security and integrity.