Hawk High

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

[H-3] Unbounded Loop in `LevelOne::removeTeacher()` enables Denial Of Service (DOS)

Description:

The removeTeacher(address _teacher) function in LevelOne.sol contract iterates over the dynamic array listOfTeachers using a for-loop to find and remove a specific teacher. This loop has no upper bound, and as the number of teachers grows, the gas required to execute the loop increases linearly. If the array becomes sufficiently large, the function may exceed the block gas limit, rendering it uncallable even by the principal. This could permanently prevent the removal of teachers from the contract.

Impact:

An attacker or unintended behavior could cause the listOfTeachers array to grow indefinitely. This would lead to the removeTeacher() function failing due to out-of-gas errors, resulting in a Denial of Service. The principal would be unable to manage the teacher list effectively, impacting contract governance and functionality.

Tools used:

Manual reviews

Remediation:

Replace the unbounded loop with a constant-time removal pattern using a mapping to track each teacher's index in the array. This allows efficient lookup and removal without iteration. Example mitigation:

mapping(address => uint256) private teacherIndex;
function removeTeacher(address _teacher) external onlyPrincipal {
require(_teacher != address(0), "Zero address");
require(isTeacher[_teacher], "Teacher doesn't exist");
uint256 idx = teacherIndex[_teacher];
uint256 lastIdx = listOfTeachers.length - 1;
if (idx != lastIdx) {
address lastTeacher = listOfTeachers[lastIdx];
listOfTeachers[idx] = lastTeacher;
teacherIndex[lastTeacher] = idx;
}
listOfTeachers.pop();
delete isTeacher[_teacher];
delete teacherIndex[_teacher];
emit TeacherRemoved(_teacher);
}

This approach ensures constant gas cost regardless of the array size and eliminates the DoS vector.

Updates

Lead Judging Commences

yeahchibyke Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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