TwentyOne

First Flight #29
Beginner FriendlyGameFiFoundrySolidity
100 EXP
View results
Submission Details
Severity: medium
Valid

Incorrect Logic for Dealer's Hand Fails to Follow Blackjack Rules for Stopping at 17

Summary

The documentation claims that the official blackjack rules are followed, but the contract does not correctly implement the rule that the dealer must stand on 17.

In the call function, the standThreshold is generated by adding a random value between 0 and 4 to 17, resulting in a value between 17 and 21.

However, due to this logic, if the standThreshold is greater than 17 (i.e., 18, 19, 20, or 21), the dealer will continue drawing cards even if the dealer already has 17. This violates the blackjack rule that requires the dealer to stop drawing cards and stand once the dealer reaches 17, as the dealer should not draw any more cards if the dealer's hand equals 17

Vulnerability Details

The problem occurs when uint256 standThreshold generates a value greater than 17. When executing while (dealersHand(msg.sender) < standThreshold), the dealer will continue drawing cards even though they should have stopped once their hand reaches 17.

This happens because the logic is configured to keep drawing cards until the dealer’s hand value is equal to or exceeds the standThreshold, which can cause the dealer to continue drawing even after reaching 17.

function call() public {
require(
playersDeck[msg.sender].playersCards.length > 0,
"Game not started"
);
uint256 playerHand = playersHand(msg.sender);
// Calculate the dealer's threshold for stopping (between 17 and 21)
uint256 standThreshold = (uint256(
keccak256(
abi.encodePacked(block.timestamp, msg.sender, block.prevrandao)
)
) % 5) + 17;
// Dealer draws cards until their hand reaches or exceeds the threshold
while (dealersHand(msg.sender) < standThreshold) {
uint256 newCard = drawCard(msg.sender);
addCardForDealer(msg.sender, newCard);
}
uint256 dealerHand = dealersHand(msg.sender)

Impact

This issue completely alters the game's logic and breaks the official blackjack rules.

Here’s one example:

  • The player stands with 17 and calls the function to have the dealer draw cards.

  • The dealer generates a stand threshold of 21, then the dealer draws cards (e.g., 10 + 9), bringing their total to 19. According to blackjack rules, the dealer should stop here and win, but since the stand threshold is set to 21, the dealer is forced to continue drawing cards. Statistically, it would be extremely difficult for the dealer to draw a 2 to continue winning, which forces the dealer into an unrealistic situation.

Tools Used

Manual Review.

Recommendations

Change the entire logic of the function so that when the dealer's hand reaches a value greater than or equal to 17, they stand and the rest of the function executes to determine the result.

Updates

Lead Judging Commences

inallhonesty Lead Judge 7 months ago
Submission Judgement Published
Validated
Assigned finding tags:

The Dealer's Play - Dealer must stand on 17

Support

FAQs

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