-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into core-mem-forget
- Loading branch information
Showing
14 changed files
with
428 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "insufficiently-random-values" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
clippy_utils = { workspace = true } | ||
dylint_linting = { workspace = true } | ||
if_chain = { workspace = true } | ||
|
||
scout-audit-internal = { workspace = true } | ||
|
||
[dev-dependencies] | ||
dylint_testing = { workspace = true } | ||
|
||
[package.metadata.rust-analyzer] | ||
rustc_private = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Insuficciently random values | ||
|
||
### What it does | ||
Checks the usage of `ledger().timestamp()` or `ledger().sequence()` for generation of random numbers. | ||
|
||
### Why is this bad? | ||
Using `ledger().timestamp()` is not recommended because it could be potentially manipulated by validator. On the other hand, `ledger().sequence()` is publicly available, an attacker could predict the random number to be generated. | ||
|
||
### Example | ||
|
||
```rust | ||
#[contractimpl] | ||
impl Contract { | ||
pub fn generate_random_value_timestamp(env: Env, max_val: u64) -> Result<u64, Error> { | ||
if max_val == 0 { | ||
Err(Error::MaxValZero) | ||
} else { | ||
let val = env.ledger().timestamp() % max_val; | ||
Ok(val) | ||
} | ||
} | ||
pub fn generate_random_value_sequence(env: Env, max_val: u32) -> Result<u32, Error> { | ||
if max_val == 0 { | ||
Err(Error::MaxValZero) | ||
} else { | ||
let val = env.ledger().sequence() % max_val; | ||
Ok(val) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Use instead: | ||
|
||
```rust | ||
#[contractimpl] | ||
impl Contract { | ||
pub fn generate_random_value(env: Env, max_val: u64) -> Result<u64, Error> { | ||
if max_val == 0 { | ||
Err(Error::MaxValZero) | ||
} else { | ||
let val = env.prng().u64_in_range(0..max_val); | ||
Ok(val) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Implementation | ||
|
||
The detector's implementation can be found at [this link](https://github.com/CoinFabrik/scout-soroban/tree/main/detectors/insufficiently-random-values). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![feature(rustc_private)] | ||
|
||
extern crate rustc_hir; | ||
|
||
use if_chain::if_chain; | ||
use rustc_hir::{BinOpKind, Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use scout_audit_internal::Detector; | ||
|
||
dylint_linting::declare_late_lint! { | ||
/// ### What it does | ||
/// This detector prevents the usage of timestamp/sequence number and modulo operator as a random number source. | ||
/// | ||
/// ### Why is this bad? | ||
/// The value of the block timestamp and block sequence can be manipulated by validators, which means they're not a secure source of randomness. Therefore, they shouldn't be used for generating random numbers, especially in the context of a betting contract where the outcomes of bets could be manipulated. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let pseudo_random = env.ledger().timestamp() % max_val; | ||
/// ``` | ||
/// | ||
pub INSUFFICIENTLY_RANDOM_VALUES, | ||
Warn, | ||
Detector::InsufficientlyRandomValues.get_lint_message() | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for InsufficientlyRandomValues { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
if_chain! { | ||
if let ExprKind::Binary(op, lexp, _rexp) = expr.kind; | ||
if op.node == BinOpKind::Rem; | ||
if let ExprKind::MethodCall(path, _, _, _) = lexp.kind; | ||
if path.ident.as_str() == "timestamp" || | ||
path.ident.as_str() == "sequence"; | ||
then { | ||
Detector::InsufficientlyRandomValues.span_lint_and_help( | ||
cx, | ||
INSUFFICIENTLY_RANDOM_VALUES, | ||
expr.span, | ||
&format!("This expression seems to use ledger().{}() as a pseudo random number",path.ident.as_str()), | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
detectors/unprotected-update-current-contract-wasm/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Unprotected update of current contract wasm | ||
|
||
### What it does | ||
|
||
It warns you if `update_current_contract_wasm()` function is called without a previous check of the address of the caller. | ||
|
||
### Why is this bad? | ||
|
||
If users are allowed to call `update_current_contract_wasm()`, they can intentionally modify the contract behaviour, leading to the loss of all associated data/tokens and functionalities given by this contract or by others that depend on it. | ||
|
||
### Example | ||
|
||
```rust | ||
#[contractimpl] | ||
impl UpgradeableContract { | ||
pub fn init(e: Env, admin: Address) { | ||
e.storage().instance().set(&DataKey::Admin, &admin); | ||
} | ||
|
||
pub fn version() -> u32 { | ||
1 | ||
} | ||
|
||
pub fn upgrade(e: Env, new_wasm_hash: BytesN<32>) { | ||
let admin: Address = e.storage().instance().get(&DataKey::Admin).unwrap(); | ||
|
||
e.deployer().update_current_contract_wasm(new_wasm_hash); | ||
} | ||
} | ||
``` | ||
|
||
Use instead: | ||
|
||
```rust | ||
#[contractimpl] | ||
impl UpgradeableContract { | ||
pub fn init(e: Env, admin: Address) { | ||
e.storage().instance().set(&DataKey::Admin, &admin); | ||
} | ||
|
||
pub fn version() -> u32 { | ||
1 | ||
} | ||
|
||
pub fn upgrade(e: Env, new_wasm_hash: BytesN<32>) { | ||
let admin: Address = e.storage().instance().get(&DataKey::Admin).unwrap(); | ||
admin.require_auth(); | ||
|
||
e.deployer().update_current_contract_wasm(new_wasm_hash); | ||
} | ||
} | ||
``` | ||
|
||
### Implementation | ||
|
||
The detector's implementation can be found at [this link](https://github.com/CoinFabrik/scout-soroban/tree/main/detectors/unprotected-update-current-contract-wasm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...insufficiently-random-values/insufficiently-random-values-1/remediated-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
[package] | ||
name = "insufficiently-random-values-remediated-1" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
soroban-sdk = "20.0.0-rc2" | ||
|
||
[dev_dependencies] | ||
soroban-sdk = { version = "20.0.0-rc2", features = ["testutils"] } | ||
|
||
[features] | ||
testutils = ["soroban-sdk/testutils"] | ||
|
||
[profile.release] | ||
opt-level = "z" | ||
overflow-checks = true | ||
debug = 0 | ||
strip = "symbols" | ||
debug-assertions = false | ||
panic = "abort" | ||
codegen-units = 1 | ||
lto = true | ||
|
||
[profile.release-with-logs] | ||
inherits = "release" | ||
debug-assertions = true |
54 changes: 54 additions & 0 deletions
54
...insufficiently-random-values/insufficiently-random-values-1/remediated-example/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#![no_std] | ||
|
||
use soroban_sdk::{contract, contracterror, contractimpl, Env}; | ||
|
||
#[contracterror] | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
#[repr(u32)] | ||
pub enum Error { | ||
MaxValZero = 1, | ||
} | ||
|
||
#[contract] | ||
pub struct Contract; | ||
|
||
#[contractimpl] | ||
impl Contract { | ||
pub fn generate_random_value(env: Env, max_val: u64) -> Result<u64, Error> { | ||
if max_val == 0 { | ||
Err(Error::MaxValZero) | ||
} else { | ||
let val = env.prng().u64_in_range(0..max_val); | ||
Ok(val) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use soroban_sdk::Env; | ||
|
||
use crate::{Contract, ContractClient}; | ||
|
||
#[test] | ||
fn random_value_sequence() { | ||
// Given | ||
let env = Env::default(); | ||
let contract_id = env.register_contract(None, Contract); | ||
let client = ContractClient::new(&env, &contract_id); | ||
|
||
// When | ||
let first_random_value = client.generate_random_value(&10); | ||
let second_random_value = client.generate_random_value(&10); | ||
let third_random_value = client.generate_random_value(&10); | ||
let fourth_random_value = client.generate_random_value(&10); | ||
let fifth_random_value = client.generate_random_value(&10); | ||
|
||
// Then | ||
assert_eq!(first_random_value, 6); | ||
assert_eq!(second_random_value, 5); | ||
assert_eq!(third_random_value, 8); | ||
assert_eq!(fourth_random_value, 8); | ||
assert_eq!(fifth_random_value, 4); | ||
} | ||
} |
Oops, something went wrong.