by tptacek · 5y ago · 0 comments
I think this is a fine page and it is eminently reasonable that sqlite remains a C codebase. In particular, I think he's right that rewriting sqlite in a memory-safe language would introduce a bunch of bugs and likely result in a couple of years of instability.
But the "security" paragraphs in this page do the rest of the argument a disservice. The fact is, C is a demonstrable security liability for sqlite. The real position of the project is that memory safety security vulnerabilities are an acceptable tradeoff for an otherwise reliable database engine; in practice, people will deal with the exposure either by treating it as an externality (ie: baking sqlite into products where it is directly exposed as part of attack surface, and then throwing up their hands and issuing patches when RCEs are discovered) or by carefully positioning sqlite so it isn't a meaningful part of the attack surface.
Both of these approaches are suboptimal --- that's why we call them "tradeoffs" --- and it is the case that if you held everything else equal (and you can't, but bear with me), sqlite would be a better piece of software written in, I guess, Rust; memory corruption wouldn't be one of the problems you need to consider (or blow off uncomfortably).
Again: the argument as a whole, and this page --- fine! I use and like sqlite.
But it will take a long time to mature. SQLite the project cannot switch languages, sadly. The only way to migrate is that another project would grow beside in the meantime, mature, and become a viable and compatible alternative.
But, tangentially, if not SQLite, what would you expose as a DB interface in a browser? SQL, while large and hairy, is both powerful and logical (in the relational part, not syntax). A plain KV store like dbm is no match to it. A Redis-like store is better but still more limited. Kdb's approach is sort of wonderful but geared towards time series and not much general-purpose. Is there an existing interface / language you would reuse to give the browser a rich database interface?
Only if either:
- there are anyway some major changes comming
- or major problems/improvements in maintainability and better external dev commitment/support
could rewriting it make sense IMHO.
Lets be honest many (most?) of the "recent" (~3years) security bugs sqlite had would most likely not have been prevented by using rust, go or similar.
EDIT:
I guess the biggest drawback of C is that it keeps contributors away, but I also might keep some of the contributors projects like sqlite might not want to have away. So depending on the maintainer it might be seen as a benefit not a drawback.
Using SQLite's CVE list from 2020[1], we see 12 vulnerabilities:
* Two NULL pointer dereferences; it's impossible to produce a NULL reference in safe Rust.
* Two integer overflows; Rust makes these harder, but not impossible.
* Three UAFs; these are impossible in safe Rust.
* One uncategorized segfault; there are impossible in safe Rust barring environmental constraints (like a stack overflow from unchecked recursion).
* One segmentation fault from incorrect object initialization; this is impossible in safe Rust.
* 3 SQL and table-level bugs; Rust is unlikely to have helped with these.
By my count, that's 6/12 bugs are would be impossible in plain old safe Rust, and another 3/12 that would likely be prevented by normal best practices in Rust. I still don't think this necessarily means that we should drop everything and rewrite SQLite in Rust, but the raw numbers don't back up the claim that doing so wouldn't eliminate the actual security bugs that SQLite is seeing.
[1]: https://www.cvedetails.com/vulnerability-list/vendor_id-9237...
But would a sqlite port limit itself to safe rust?
(As a side note wrt integer overflows, rust only makes it easier to detect them during tests and provides neat methods for integer overflow aware code, but just that.)
Anyway I'm surprised that there where more "preventable" bugs then I expected.
There's this idea floating around in some circles that if you could just adopt technology X, more people would contribute to a project.
I myself was guilty of this ... believing that if we added a web frontend to the DAW I've worked on for 21 years, all those JS/webtech developers would show up.
There's no platform or language you could choose for SQLite that would increase the number of contributors. C is already a wildly popular programming language, with far, far more practicing users than Rust. Rust may be The Cool New Thing at present, and in time it may possibly grow to something much bigger, bigger even than the C/C++ universe. But that's not true right now, and it also wouldn't be true even if SQLite was implemented using some impossibly performant JS or its cousin.
I'm sensible to the issues of tradeoffs (e.g. platform support which is a completely fair issue) and manpower requirements and whatnot, but I don't see why it can not switch language. Converting libraries "inside out" has been done. Adding Rust support inside sqlite's build system and migrating modules is technically feasible (again, not opining on whether it would be worth it).
I mentioned that but i don't think that's the subject as it does not warrant such a drastic assertion that sqlite can not switch language.
If can switch the officially endorsed "primary" implementation, but only after an alternative implementation has been around for a long time and was battle-tested, all the while the original C implementation continued to exist and develop.
At a purely technical level (ignoring issues of platform support and all) it pretty much can do that, actually: integrate XYZ into the build system, build new features in XYZ, start converting old features to XYZ, end up with an sqlite in XYZ.
That is useless to sqlite itself, this exists only because of go's issues and is essentially unusable outside of the go ecosystem.
The only issue I see with cgo is that they decided to follow such path instead of a proper FFI declaration support like most languages.
Even Java is now having such capability thanks Project Panama.
And the track record of that approach is, well, right there for you to see.
The idea that what's needed for sqlite's C to be safe is more of the approach sqlite already uses seems pre-falsified, doesn't it?
Any language that can produce a C library interface identical to the existing one for sqlite3 would be a good candidate for a reimplementation.
If you show me a Rust library that passes all the sqlite3 tests flawlessly on my platform (typically x86-64) then I'd include that in my project, and sleep soundly at night afterwards. There might be problems, but the chances are very low.
Most other libraries don't nave nearly as good coverage, and it is much riskier to switch over an implementation.
You realize that there are lots of test coverage metrics and none of them tell you anything about the correctness or safety of the code or about how thoroughly the edge cases have been tested, right? 100% branch coverage is neat and may indicate that the testing is thorough, or it might just indicate the project is chasing it as a metric. I’ll bet on compile time lifetime and ownership analysis guaranteed by the language every time over metrics of how good the test suite is (a test suite is important but classes of bugs are just impossible in Rust and don’t need testing/coverage in the first place).
> that is: they can't use Rust because they can't test safety checks that don't even exist in the language they use today
Can you back up this claim? This feels like a very FUD statement as any high level language (including C) has a risk that the compiler inserts branches that aren’t present in the code. Regardless, AFAIK, coverage instrumentation happens at a level below where the distinction between C and Rust matters, so any coverage should be the same. I buy the argument that replicating the test suite may be time consuming bit, but the claim that there’s something inherent about Rust preventing branch coverage feels extraordinary to me. Also, I’m not even sure where the extra branches are being inserted. Are you referring to drop statements the compiler injects for lifetimes?
> And the track record of that approach is, well, right there for you to see.
Constant CVEs and a fundamental inability to handle maliciously crafted files? I’m not shitting on the SQLite team. The project is amazing and a marvel. I’m just saying that its C heritage has some inescapable realities. Also remember that studies tend to show that the bug rate is pretty constant across languages in terms of LOC. This you want the language and stdlib doing as much as possible for you if you’re prioritizing correctness and security.
For a large existing code base that are well written I think it's easier to improve the safety of the existing code than rewrite it completely in a safer language.
My position right now, before hearing that war story, is that Rust vs. C is more than a difference of degree. It's a difference of degree in security writ large, to be sure. But for memory corruption? I'd say the distinction is close to categorical.
What would be the significance of that? Unsafe blocks in rust must still satisfy the borrow checker and all the compiler’s safety rules. The only things unsafe gives you are:
Dereference a raw pointer
Call an unsafe function or method
Access or modify a mutable static variable
Implement an unsafe trait
Access fields of unions
From https://doc.rust-lang.org/book/ch19-01-unsafe-rust.htmlThe use of unsafe blocks does not automatically mean unsafe code?
I don't really agree with it either - it overstates the issues with occasionally using escape hatches in Rust by implying that they're at all comparable with the problems inherent with using C. But yours was still an unnecessary reply.
You can use formal methods to sidestep this! But my argument would be that at that point, you're really writing C In Name Only. By all means, if the aesthetics of Rust are that painful to you (I get it!), write C against a formal verifier. :)
I mean the whole security and safe language paragraphs were added after people literally start either harassing sqlite or claiming SQLite is unsafe.
The paragraphs were an extremely polite way of saying No. We dont want to do switch to another language.
I think you're missing one of the ways. The one where people make no deliberate attempt to engage with the risk at all. No big throwing up of the hands. Sqlite is deeply embedded software, it takes years for a whole generation of smart TVs and security cameras and cars to find themselves in landfill and put a decade of vulnerabilities to bed.
AIUI, the SQLite test suite always is praised for being especially expansive(complete?), so assuming it's not testing C-isms, which would be weird, then I would expect a rewrite to be less risky than trying a trick like that in most business software