Hawk High

First Flight #39
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Impact: high
Likelihood: high
Invalid

[H-4] Unbounded Loop in expel() Function Enables Denial of Service (DoS)

Description:

The expel(address _student) function inside LevelOne.sol contract iterates over the dynamic listOfStudents array to find and remove a specified student. This linear search scales with the size of the array and has no limit, resulting in increased gas consumption as more students are added. Once the array is sufficiently large, the loop can consume all available gas, causing the transaction to revert. This can render the expel() function unusable and block the principal from managing the student list.

Impact:

A malicious user or unintended contract usage could cause listOfStudents to grow excessively. As a result, expel() would eventually consume more gas than the block limit allows, making it impossible to expel any student. This creates a Denial of Service vulnerability, harming the contract’s administration and potentially allowing students to remain in the system indefinitely against the principal's intent.

Tools used:

Manual reviews

Remediation:

Use a constant-time removal technique by tracking each student’s index with a mapping. This enables efficient removal without looping:

mapping(address => uint256) private studentIndex;
function expel(address _student) external onlyPrincipal {
require(inSession, "Session not active");
require(_student != address(0), "Zero address");
require(isStudent[_student], "Student doesn't exist");
uint256 idx = studentIndex[_student];
uint256 lastIdx = listOfStudents.length - 1;
if (idx != lastIdx) {
address lastStudent = listOfStudents[lastIdx];
listOfStudents[idx] = lastStudent;
studentIndex[lastStudent] = idx;
}
listOfStudents.pop();
delete isStudent[_student];
delete studentIndex[_student];
emit Expelled(_student);
}

This pattern ensures constant gas cost and prevents the function from becoming unusable due to out-of-gas errors.

Updates

Lead Judging Commences

yeahchibyke Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Too generic

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.