Assignment: Audit a Smart Contract
Objective:
The purpose of this assignment is to provide students with hands-on experience in auditing and implementing smart contracts. Students will be given a basic Solana smart contract written in the Rust programming language and are expected to identify any bugs or security risks and provide a report of their findings, as well as a new implementation of the smart contract that fixes any identified issues.
Starter Code
use solana_sdk::{
account::Account,
entrypoint::{entrypoint, Entrypoint},
program_error::ProgramError,
pubkey::Pubkey,
};
struct Token {
owner: Pubkey,
balance: u64,
}
impl Token {
fn new(owner: Pubkey) -> Self {
Self { owner, balance: 0 }
}
fn deposit(&mut self, amount: u64) {
self.balance += amount;
}
fn transfer(&mut self, to: Pubkey, amount: u64) -> Result<(), ProgramError> {
if self.balance < amount {
return Err(ProgramError::InsufficientFunds);
}
if self.owner == to {
return Err(ProgramError::InvalidAccount);
}
self.balance -= amount;
Ok(())
}
}
#[entrypoint]
// Don't worry about this
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[Account],
instruction_data: &[u8],
) -> Result<(), ProgramError> {
let account_metas = &accounts[0..2];
let source_account = &account_metas[0];
let destination_account = &account_metas[1];
let source_data = &source_account.data;
let destination_data = &destination_account.data;
let source_token = Token::deserialize(source_data).unwrap();
let mut destination_token = Token::deserialize(destination_data).unwrap();
match instruction_data[0] {
0 => source_token.transfer(destination_account.key, 1)?,
1 => destination_token.deposit(1),
2 => destination_token.transfer(source_account.key, 1)?,
_ => return Err(ProgramError::InvalidInstructionData),
}
source_account.data = source_token.serialize();
destination_account.data = destination_token.serialize();
Ok(())
}
Instructions:
Familiarize yourself with the given Solana smart contract code.
Conduct a thorough code review and identify any bugs or security risks in the smart contract.
Write a report detailing all of the bugs and security risks you have identified and the risks associated with them.
Provide a new implementation of the smart contract that addresses the issues you identified in your report.
Report Requirements:
A clear and concise description of the bugs and security risks you have identified.
A discussion of the risks associated with each issue.
A detailed explanation of how each issue can be fixed.
New Implementation Requirements:
The new implementation must address all of the issues identified in the report.
The new implementation must be clearly and concisely written.
The new implementation must be well-documented with comments explaining the purpose of each section of code.
Submission:
Submit your report in a pdf file format.
Submit the new implementation in a .rs file format.
Submit both files by pushing to the GitHub repo
Last updated