5 ms·
Secure Rust Guidelines
- jeroenhd 5y agoTook me a while to realise this is a project by the French National Cybersecurity Agency (ANSSI), not just some random github repos from someone with Important Opinions on how to use a programming language. All of this advice makes a lot of sense but a lot of recommendations cannot easily be checked for or enforced. It would be nice to be able to quickly put Clippy into "ANSSI mode" to check and enforce all of these requirements.
- Aissen 5y agoOn the other hand, if it could be easily checked, the document would be: "Enable ANSSI clippy mode" and not multiple pages.
- jeroenhd 5y agoExplaining why something is bad is still necessary if you rely on automated code review. Clippy does this, with specific codes for specific (potential) problems that the linter catches. You can learn a lot about patterns in Rust by just reading through the bad patterns and read their suggested alternatives.
- Aissen 5y agoI fully agree, but then it would be integrated with Clippy or in a wiki with a page per code (like shellcheck). The document in this post is helpful because it does the first step of explaining the rationals, that can then eventually be automated (and then/copy pasted in the explanations).
- noodledoodletwo 5y agoI agree with you all. Bake it into clippy or make a different clippy aka seclippy. Also, I appreciate the docs so I understand why with examples. Also make a attribute that blocks some of these things
- MrJohz 5y ago> seclippy According to a quick search, I propose "trombine" as the French version of clippy.
- rpigab 5y agoDon't you mean trombone? Or is there a hidden pun?
- manicpolymath 5y agoI detect a pun. _trombine_ is, according to Wiktionary anyway, a colloquial French word for _face_.
- MrJohz 5y agoAccording to French Wikipedia, one of the names for Clippy in French is Trombine. Given the other user mentioned that it means "face", and given trombone is, I believe, a paper clip (my French education unfortunately did not get this far), I suspect it's used as a kind of cute pun thing. Kind of like how "Clippy" is obviously derived from the word "paperclip", but has slightly cuter and more friendly connotations. (Except for those experienced with Clippy, who may have other connotations at this point...)
- liftm 5y ago> In a secure Rust development, the code must not leak memory or resource in particular via Box::leak. Uh, so is Box::leak forbidden in general? Because creating a &'static in some initialization code that lives for the rest of the program is a rather common use case… https://docs.rs/log/0.4.17/src/log/lib.rs.html#1408 https://docs.rs/log/0.4.17/src/log/lib.rs.html#1408 Or is it only forbidden if it's actually a leak?
- alpaca128 5y ago> creating a &'static in some initialization code that lives for the rest of the program is a rather common use case Isn't that the use-case for lazy_static? Or SyncOnceCell once it's in stable Rust. Though I'm not sure how it's implemented under the hood.
- liftm 5y agoHm, good point. (It's implemented as a Once (which is an atomic usize) and an UnsafeCell for holding the actual data.) And while using SyncOnceCell normally might incur some small checking overhead on each access, you can use it to obtain a &'static. https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=506632ca984ccc6732114373c535ce28 https://play.rust-lang.org/?version=nightly&mode=debug&editi... But then, by tialaramex's logic, isn't it just as bad as Box::leak? (Maybe not quite as, because it can leak maximally one thing. You can't do this in a loop.)
- tialaramex 5y agoI mean, it is actually a leak, that's exactly why you're doing it. This document says you should not because, by their definition, that's not "secure Rust development".
- mlindner 5y agoHow is it a leak if it still has a reference? I only would classify something as a leak if it has no remaining valid references to some piece of data.
- 5y ago
- Grimburger 5y agoDisappointed there's no mention of vendoring. Anyone with _proper_ security concerns should cargo vendor by default. Last I looked supply chain attackers don't respect semver and you are all one casual offhand cargo update away from catastrophe.
- viccuad 5y agoI don't think vendoring helps at all. People can't review all the dependencies' code. And recursively, for the dependencies of the dependencies. At some point, you delegate trust. What helps is having provenance information, signing and SBOM. One example. https://sigstore.dev https://sigstore.dev (vendor neutral effort from the Linux Foundation).
- estebank 5y agoFor applications (as opposed to libraries), Cargo.lock fulfills the same function as vendoring, but lets cargo audit continue working as expected.
- Grimburger 5y agoTo repeat: > one casual offhand cargo update away from catastrophe There is a scary amount of libraries who don't even bother specifying patch levels and will auto update everything upon request without question, even worse cargo doesn't make it explicit and as such version "1.0" is equivalent to "1.0.*" in the cargo manifest. Please refer to my previous comment about bad actors not respecting semver as much as many would love them to. `cargo audit` can be compromised by bad actors. `cargo update` also. If you have a security first application then always vendor, this isn't rust specific.
- ilammy 5y agoOh, so you trust "cargo build"? Why? Sure, "cargo update" will update the dependencies – that's what it's meant to do. The point of Cargo.lock is to ensure that all compilations use the same known set of versions, otherwise it's possible for developers to use one library and the users to use a different one. With Cargo.lock a new version of library won't be used without an explicit "cargo update".
- tonyzzz 5y agoThe link to "rust-bindgen" in page https://anssi-fr.github.io/rust-guide/07_ffi.html https://anssi-fr.github.io/rust-guide/07_ffi.html seems to be broken. I think it probably refers to "bindgen" (https://crates.io/crates/bindgen https://crates.io/crates/bindgen). EDIT: the broken link is introduced when replacing Github link to Crates.io link: https://github.com/ANSSI-FR/rust-guide/commit/49822911e4f14b7cfb4df47b5c295bc35aec0790#diff-cd01e52d548d81932fe4fcfe5eba53b64d70f9520f6e895550ef480e700a7639R198 https://github.com/ANSSI-FR/rust-guide/commit/49822911e4f14b...
- puffoflogic 5y agoSadly these guidelines are as incoherent as last time. For example, the rules absolutely forbid using an `unsafe` block to call a rust function (since this falls in none of the allowed uses of `unsafe`), but then the rules turn around and require the use of `unsafe` to zero out variables.