Blog/Blockchain·8 min read·2/15/2026

Bảo mật Smart Contract: 5 lỗ hổng phổ biến và cách phòng tránh

Tại sao bảo mật smart contract quan trọng?

Khác với phần mềm truyền thống, smart contract trên blockchain là immutable — không thể patch sau khi deploy. Một bug nhỏ có thể dẫn đến mất toàn bộ tiền của user. Chỉ riêng năm 2025, hơn 1.8 tỷ USD đã bị mất do các lỗ hổng smart contract.

1. Reentrancy Attack

Lỗ hổng kinh điển nhất. Kẻ tấn công gọi lại function withdraw trước khi state được cập nhật:

// SAI — dễ bị reentrancy
function withdraw(uint256 amount) external {
    require(balances[msg.sender] >= amount);
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success);
    balances[msg.sender] -= amount; // Cập nhật SAU khi gửi tiền

// ĐÚNG — checks-effects-interactions pattern function withdraw(uint256 amount) external nonReentrant { require(balances[msg.sender] >= amount); balances[msg.sender] -= amount; // Cập nhật TRƯỚC khi gửi tiền (bool success, ) = msg.sender.call{value: amount}(""); require(success); } `

Phòng tránh: Luôn cập nhật state trước khi gọi external call. Dùng modifier nonReentrant từ OpenZeppelin.

2. Integer Overflow / Precision Loss

Solidity 0.8+ đã có built-in overflow check, nhưng precision loss khi chia vẫn là vấn đề lớn trong DeFi:

// Mất precision
uint256 reward = (userStake * rewardRate) / totalStake;

// Giảm thiểu bằng cách nhân với precision factor uint256 PRECISION = 1e18; uint256 reward = (userStake * rewardRate * PRECISION) / totalStake / PRECISION; `

3. Access Control thiếu chặt

Quên kiểm tra quyền truy cập trên function quan trọng:

// SAI — ai cũng gọi được
function setPrice(uint256 newPrice) external {
    price = newPrice;

// ĐÚNG — chỉ owner function setPrice(uint256 newPrice) external onlyOwner { price = newPrice; } `

Phòng tránh: Dùng OpenZeppelin AccessControl cho role-based permission.

4. Front-running

Trên blockchain công khai, mọi giao dịch đều visible trong mempool trước khi được mine. Kẻ tấn công có thể "chạy trước" giao dịch của bạn:

Phòng tránh: - Dùng commit-reveal scheme cho auction, voting - Set slippage tolerance cho DEX swap - Dùng Flashbots hoặc private mempool

5. Thiếu validation input

// SAI — không check address(0)
function transfer(address to, uint256 amount) external {
    balances[msg.sender] -= amount;
    balances[to] += amount;

// ĐÚNG function transfer(address to, uint256 amount) external { require(to != address(0), "Invalid address"); require(amount > 0, "Amount must be > 0"); require(balances[msg.sender] >= amount, "Insufficient balance"); balances[msg.sender] -= amount; balances[to] += amount; } `

Checklist bảo mật trước khi deploy

  1. 01Chạy Slither + Mythril static analysis
  2. 02Unit test coverage > 95%
  3. 03Fuzz testing với Foundry
  4. 04Ít nhất 1 independent audit
  5. 05Deploy trên testnet và test kỹ
  6. 06Setup monitoring với Forta Network
  7. 07Có emergency pause mechanism

Kết luận

Bảo mật smart contract không phải optional — đó là requirement. Chi phí audit (5-20K USD) luôn rẻ hơn nhiều so với chi phí bị hack. Nếu bạn đang xây dựng dự án blockchain, hãy đầu tư đúng mức vào bảo mật ngay từ đầu.