Giter Site home page Giter Site logo

Example #2 Owner Checks about sealevel-attacks HOT 2 OPEN

iurage avatar iurage commented on September 23, 2024
Example #2 Owner Checks

from sealevel-attacks.

Comments (2)

iurage avatar iurage commented on September 23, 2024

I am guessing this is because there isn't a check that the account is owned by the TokenProgram?

from sealevel-attacks.

FrodoBaggins74524 avatar FrodoBaggins74524 commented on September 23, 2024

First, the code attempts to log a message after deserializing the provided account (token) using the SplTokenAccount::unpack utility from the spl_token crate. However, it does not verify that the token account is indeed owned by the expected token program (such as the SPL Token program).
Second, although the code performs an owner check using ctx.accounts.authority.key != &token.owner, where ctx.accounts.authority is expected to be the authorized signer, this check only verifies that the signer (ctx.accounts.authority) is the owner of the token account, not that the token account itself is a valid SPL Token account. This means that an attacker could create a malicious program that creates a fake account and sets any arbitrary owner address to pass this check.
As a result of these issues, a malicious actor could potentially pass any arbitrary account, and as long as they have control over the account (i.e., the private key corresponding to the authority), the program will not throw an error. This could lead to unintended consequences, such as reading sensitive data from unrelated accounts or performing unauthorized actions.
To ensure security, it's crucial to verify that the provided token account is a legitimate SPL Token account owned by the SPL Token program before performing any operations or checks on it. This can be achieved by checking the account's program_id field against the correct program ID for the SPL Token program. The correct program ID can be obtained using spl_token::id().
Here's an example of how to fix the code by adding the necessary checks:

use anchor_lang::prelude::*;
use anchor_lang::solana_program::program_error::ProgramError;
use anchor_lang::solana_program::program_pack::Pack;
use spl_token::state::Account as SplTokenAccount;

// ... (rest of the code)

#[program]
pub mod owner_checks_secure {
use super::*;

pub fn log_message(ctx: Context<LogMessage>) -> ProgramResult {
    let token = SplTokenAccount::unpack(&ctx.accounts.token.data.borrow())?;
    
    // Ensure the token account is owned by the correct Token program
    if *ctx.accounts.token.to_account_info().key != spl_token::id() {
        return Err(ProgramError::InvalidAccountData);
    }
    
    // Perform the authority check to ensure the signer is the owner of the token account.
    if ctx.accounts.authority.key != &token.owner {
        return Err(ProgramError::InvalidAccountData);
    }
    
    msg!("Your account balance is: {}", token.amount);
    Ok(())
}

}

// ... (rest of the code)

By adding the check to ensure the token account is owned by the correct Token program, the code becomes more secure and prevents unauthorized accounts from passing the checks. This way, we can avoid potential security vulnerabilities and maintain the integrity of the program's operations.

from sealevel-attacks.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.