-
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 4-dos-unbounded-operation
- Loading branch information
Showing
23 changed files
with
621 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "avoid-core-mem-forget" | ||
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
2 changes: 1 addition & 1 deletion
2
detectors/core-mem-forget/Cargo.toml → detectors/set-contract-storage/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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "core-mem-forget" | ||
name = "set-contract-storage" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
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,89 @@ | ||
#![feature(rustc_private)] | ||
#![warn(unused_extern_crates)] | ||
|
||
extern crate rustc_hir; | ||
extern crate rustc_span; | ||
|
||
use rustc_hir::def_id::LocalDefId; | ||
use rustc_hir::intravisit::Visitor; | ||
use rustc_hir::intravisit::{walk_expr, FnKind}; | ||
use rustc_hir::{Body, FnDecl}; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_span::Span; | ||
use scout_audit_internal::Detector; | ||
|
||
dylint_linting::declare_late_lint! { | ||
/// ### What it does | ||
/// Checks for calls to env.storage() without a prior call to env.require_auth() | ||
/// | ||
/// ### Why is this bad? | ||
/// Functions using keys as variables without proper access control or input sanitation can allow users to perform changes in arbitrary memory locations. | ||
/// | ||
/// ### Known problems | ||
/// Only check the function call, so false positives could result. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// fn set_contract_storage(env: Env) { | ||
/// let _storage = env.storage().instance(); | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// fn set_contract_storage(env: Env, user: Address) { | ||
/// user.require_auth(); | ||
/// let _storage = env.storage().instance(); | ||
/// } | ||
/// ``` | ||
pub SET_STORAGE_WARN, | ||
Warn, | ||
Detector::SetContractStorage.get_lint_message() | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for SetStorageWarn { | ||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'tcx>, | ||
_: FnKind<'tcx>, | ||
_: &'tcx FnDecl<'_>, | ||
body: &'tcx Body<'_>, | ||
_: Span, | ||
_: LocalDefId, | ||
) { | ||
struct SetContractStorageVisitor { | ||
found_auth: bool, | ||
storage_without_auth: Vec<Span>, | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for SetContractStorageVisitor { | ||
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) { | ||
if let ExprKind::MethodCall(path, _, _, _) = &expr.kind { | ||
let method_name = path.ident.name.as_str(); | ||
if method_name == "require_auth" { | ||
self.found_auth = true; | ||
} else if method_name == "storage" && !self.found_auth { | ||
self.storage_without_auth.push(expr.span); | ||
} | ||
} | ||
walk_expr(self, expr); | ||
} | ||
} | ||
|
||
let mut visitor = SetContractStorageVisitor { | ||
found_auth: false, | ||
storage_without_auth: Vec::new(), | ||
}; | ||
|
||
walk_expr(&mut visitor, body.value); | ||
|
||
for span in visitor.storage_without_auth { | ||
Detector::SetContractStorage.span_lint_and_help( | ||
cx, | ||
SET_STORAGE_WARN, | ||
span, | ||
"Ensure that the caller is authorized to use storage", | ||
); | ||
} | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
test-cases/avoid-core-mem-forget/avoid-core-mem-forget-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,30 @@ | ||
[package] | ||
name = "avoid-core-mem-forget-1-remediated" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
soroban-sdk = { version = "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 |
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
30 changes: 30 additions & 0 deletions
30
test-cases/avoid-core-mem-forget/avoid-core-mem-forget-1/vulnerable-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,30 @@ | ||
[package] | ||
name = "avoid-core-mem-forget-1-vulnerable" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
soroban-sdk = { version = "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 |
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
2 changes: 1 addition & 1 deletion
2
...em-forget-1/vulnerable-example/Cargo.toml → ...t-storage-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
Oops, something went wrong.