AI Arena - aariiif's results

In AI Arena you train an AI character to battle in a platform fighting game. Imagine a cross between Pokรฉmon and Super Smash Bros, but the characters are AIs, and you can train them to learn almost any skill in preparation for battle.

General Information

Platform: Code4rena

Start Date: 09/02/2024

Pot Size: $60,500 USDC

Total HM: 17

Participants: 283

Period: 12 days

Judge:

Id: 328

League: ETH

AI Arena

Findings Distribution

Researcher Performance

Rank: 41/283

Findings: 1

Award: $166.17

๐ŸŒŸ Selected for report: 0

๐Ÿš€ Solo Findings: 0

Analysis Approach

I reviewed the core smart contracts comprising the AI Arena platform, including FighterFarm, RankedBattle, Neuron token, and supporting contracts like MergingPool and GameItems.

My analysis looked at:

  • Architecture: How components are designed and fit together
  • Security: Vulnerabilities, access controls, trust assumptions
  • Mechanisms: Analysis of core game mechanics and economic incentives
  • Code Quality: Use of standards/best practices, comments, testing
  • Centralization Risks: Over-reliance on privileged roles
  • Systemic Risks: Susceptibility to threats like manipulation or exploits

Architecture

AI Arena follows a modular architecture with well-scoped contracts for NFTs, staking, battles, etc. This provides separation of concerns.

There is still strong centralization around owner addresses that poses a risk. Suggest defining more granular roles using AccessControl as opposed to reliance on _ownerAddress.

Overall the architecture sets a good foundation, but removing single points of failure via decentralized governance will strengthen integrity over time.

Code Quality

I found extensive natspec documentation in modules, following Solidity coding standards, and clear logical flow in code.

Adding unit test coverage would significantly improve robustness against edge cases. Formal verification would also mathematically prove properties.

But logical commenting allows the modules to be understood at a glance.

Security Analysis

The contracts utilize access controls in functions via checks on msg.sender, but privilege separation could be stronger. Owner accounts have far reaching power that poses centralization risks if keys are compromised.

No safeguards against threats like frontrunning or integer overflows are apparent. Additional validation on untrusted inputs would improve safety.

There are also no revocation or pause mechanisms if vulnerabilities emerge. So while the code avoids major issues like reentrancy, risk surfaces remain.

The FighterFarm, RankedBattle, and other core contracts designate an _ownerAddress that has sweeping control over crucial functions like minting NFTs, updating match results, distributing rewards, etc.

For example, in FighterFarm.sol the owner can: https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L129-L134

function incrementGeneration(uint8 fighterType) external returns (uint8) {
  require(msg.sender == _ownerAddress);
  // increment generation 
  return generation[fighterType]; 
}

function addMinter(address newMinterAddress) external {
 require(msg.sender == _ownerAddress);
 // add new minter
}

If the key controlling the owner address were to be compromised, the attacker could exploit these privileged functions to:

  • Mint an excessive number of rare fighter NFTs to themselves, stealing value from existing NFT holders
  • Manipulate match results to unfairly reward certain addresses
  • Adjust ranking criteria or eligibility to favor certain players
  • Drain funds from the contracts into the owner address

They can essentially extract full value out of the contracts since the owner role overrides all other privilege restrictions.

This is quite likely to happen if measures are not taken to restrict access to the owner keys. Private keys can be lost, stolen, socially engineered, etc if not properly secured. Keys held on internet-connected devices have higher risks.

An attacker would first need to gain access to the private key controlling the owner address, whether via hacking, physical access to devices, or social engineering tactics. Once they have the key, they can easily trigger the privileged functions through contract calls.

Preventative Techniques

  1. Require multi-signature schemes to invoke the sensitive owner admin functions
  2. Add time delays to executions like minting or transfers to detect abuse
  3. Build alerts and monitors to track anomalies in admin behaviors
  4. Reduce reliance on the owner address by assigning roles more granularly

The broad owner privileges in these contracts violate the principle of least authority by allowing a single compromised key to exploit critical functions. Implementing controls around the owner address is crucial.

In the RankedBattle.sol contract, the _gameServerAddress is authorized to call sensitive functions like https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L322-L349

function updateBattleRecord(
  uint256 tokenId, 
  uint8 battleResult,
  uint256 eloFactor,
  bool initiatorBool
) 
  external 
{

  require(msg.sender == _gameServerAddress);

  // update match records
  // calculate rewards
  // handle stake
}

This allows the game server to fully control match results, ELO changes, and reward distributions without checks.

Since match outcomes determine the earning potential of NFTs, inconsistent or tampered results from a centralized authority presents risks:

  • Admin could always make certain addresses win, modifying ELOs to maximize rewards
  • Bad actors could hold influence over the project to unfairly benefit themselves
  • Losers could be disproportionately penalized by tweaking input parameters
  • Reduce confidence in the legitimacy of match outcomes

This hurts the sustainability of the play-to-earn model.

Such abuse is likely given the lack of checks and balances on the centralized control. There are no visibility measures that would deter malicious admin behaviors.

The game server admins would simply need to pass biased outputs for match results, ranking changes, and rewards distributions into the updateBattleRecord function to unfairly benefit addresses they select.

Preventative Techniques

  1. Build visibility by emitting all critical state changes
  2. Implement statistical checks to detect anomaly patterns
  3. Assign oversight abilities to a DAO treasury board unaffiliated with the project team
  4. Query different oracle services to determine consensus

Centralized control without mitigating controls introduces unacceptable manipulation risks destabilizing the in-game economy. Adding layers of transparency and accountability for such privileged roles would align incentives through community oversight.

Positives

  • Square root curves on staking reduce runaway leader dominance

  • Points decay over time to require ongoing participation

  • Relative rankings determine distributions rather than absolute thresholds

  • Voltage costs rate limit pace of matches to avoid exploitation

  • Cyclical rounds reset stakes to rebalance accessibility

Risks

  • No mechanisms to handle rampant pooling/collusion across factions

  • No protections against whale-dominated saturation staking

  • Chance for complex smart contract interactions leading to edge cases

  • Subjective administrative overrides of rankings/eligibility

Potential Enhancements

  • Implement token bonding curves to help stabilize valuations

  • Introduce randomness and raffles as alternative reward channels

  • Cap max percentage share of distributions per address

  • Formal verification of contract logic flows

The dual token model provides levers to balance scalability, while still encouraging participation with Achievement-based distribution qualifiers.

Adding decentralized governance for parameter changes would help sustain integrity over time. But the core design seems economically sound, with a few tweaks needed around preventing systemic gaming.

I also performed an analysis of the AI Arena's smart contracts to assess protections against common vulnerabilities, and adherence to best practices:

Reentrancy

  • State changes in external calls like transfers happen before calculations and rewards updates
  • Follows checks-effects-interactions pattern

โœ… Mitigated

Integer Overflows

  • Lack of validation on parameters like ELO factors leaves risk
  • No upper bound protections on arrays when updating stats
  • Time representations may be vulnerable

โ›”๏ธ Improvements needed

Frontrunning

  • Match trigger transactions could be exploited to steal rewards
  • Leaderboards changes not committed atomically

โ›”๏ธ Risks possible

Additional Analysis

  • No zero address validation on critical parameters
  • Explicit event emissions for state changes
  • Time delays on certain executions
  • CustomErrors used for precise exceptions

โœ… Follows many best practices

Suggestions

  • Formal verification to mathematically prove properties
  • Slither static analysis of code flows
  • Extensive unit test cases coverage

There are moderate protections against major risks like reentrancy, but gaps remain around integer bounds, frontrunning protections, and validating inputs that open up vectors for potential exploitation.

Adding security-focused code auditing, static analysis scanning, and formal verification would significantly improve the contracts' reliability against threats.

Based on my analysis, the AI Arena contracts do make efforts to apply access control protections, but there are still gaps that provide opportunities for more privilege risks:

Positives

  • Use of AccessControl roles in NRN token for managing permissions
  • Restricting calling of sensitive functions to contract owner
  • Mapping control over admin functions to designated addresses

Gaps

  • Owner addresses in core contracts have sweeping privileges
  • Lack of checks around game server's update abilities
  • No revocation mechanisms for compromised roles
  • Minimal time delays on critical operations

Potential Enhancements

  • Leverage AccessControl roles more extensively
  • Require multi-sig schemes for powerful functions
  • Revoke compromised roles via governance votes
  • Whitelist critical functionality to enhance protections
  • Add more rate limiting constraints

There are good starting points in applying access restrictions like checks on msg.sender and use of admin mappings. However, over-reliance on owner addresses and game servers with no takeback mechanisms still pose too much risk.

A revamped access controls architecture could help minimize attack surfaces by more granularly restricting the capabilities of any single account compromise using whitelists, multi-sig, revocations, and enhanced ratelimiting.

Economic Sustainability

The dual token model aligns incentives towards participation while allowing leveraging of monetary policy variables to moderate volatility. Cyclical rounds with decaying rewards promote ongoing engagement.

Risks around insider manipulation or whale collusions should be monitored, but game theory checks seem reasonably balanced. Randomness could be improved via Chainlink VRF.

So at a high level, the system seems structured to align incentives and drive recurring participation.

Suggested Improvements

  • Decentralize control from owner addresses via DAO or multi-sig
  • Add reentrancy and overflow protections in places
  • Enhance access control architecture with more granular roles
  • Expand unit test coverage and consider formal verification
  • Monitor systemic risks as adoption grows

Time spent:

22 hours

#0 - c4-pre-sort

2024-02-25T20:23:15Z

raymondfam marked the issue as sufficient quality report

#1 - c4-judge

2024-03-19T08:24:07Z

HickupHH3 marked the issue as grade-a

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax ยฉ 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter