5 ms·
Less than a year after this was published, Tencent released the Magellan series of sqlite RCEs. I think this is a fine page and it is eminently reasonable that
by tptacek 5y ago
Less than a year after this was published, Tencent released the Magellan series of sqlite RCEs.
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.
- nine_k 5y agoI suppose that a version of SQLite written in a safe(r) language will eventually appear, and hopefully become popular. 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.
- tptacek 5y agoI don't like C (correction: I love writing C; I don't like running code written in C), but I'd probably stick with sqlite for several years after the introduction of a competing Rust sqlike. But I also wouldn't expose sqlite directly to untrusted users (for instance, in the hellworld where I'm a designer on a major browser, I wouldn't make sqlite part of the Javascript interface of that browser).
- nine_k 5y agoI see; I share much of the sentiment. 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?
- masklinn 5y ago> SQLite the project cannot switch languages, sadly. 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).
- Jtsummers 5y agoAdding Rust support would reduce (presently) the number of architectures and platforms that SQLite can target. That would greatly reduce its utility for a lot of customers. Once Rust supports the same variety of architectures that C presently supports this will become a non-issue, but that's unlikely to happen in the near term.
- masklinn 5y ago> Adding Rust support would reduce (presently) the number of architectures and platforms that SQLite can target. 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.
- Jtsummers 5y agoOk, yes. SQLite can switch languages if the objective is to cutoff a large number of customers. Until Rust is well-supported on the variety of architectures that C is supported on, then that will be the result.
- justin66 5y ago> Until Rust is well-supported on the variety of architectures that C is supported on There's no particular reason to think that this will ever happen. SQLite runs on a lot of crazy stuff that Rust won't want to bother with.
- dathinab 5y agoI don't think a rust version of sqlite would make sense, rewriting a very mature and well tested library rarely makes sense. 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.
- tptacek 5y agoThe problem with the first sentence of this comment is that it also justifies keeping stuff like ImageMagick around.
- woodruffw 5y ago> 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. 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/year-2020/Sqlite.html https://www.cvedetails.com/vulnerability-list/vendor_id-9237...
- PaulDavisThe1st 5y ago
- CraigJPerry 5y agoI think https://gitlab.com/cznic/sqlite https://gitlab.com/cznic/sqlite has become pretty popular. It’s a pure go implementation
- masklinn 5y agoIt is popular not because it's a good idea but because go: using cgo is a huge imposition, so recoding everything in go so it can be used from go is basically the norm. That is useless to sqlite itself, this exists only because of go's issues and is essentially unusable outside of the go ecosystem.
- pjmlp 5y agoDislike for cgo, is similar to the old JNI displeasure, nowadays plenty of Java libraries don't have any issues making use of JNI. 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.
- CraigJPerry 5y agoI’ve noticed cgo dependencies massively slow down compilation. E.g. the hugo project built with and without extended features. Build without and it’s all go, it compiles in the blink of an eye. The tooling in go, from a devops point of view, is surprisingly good. It’s not just compile speed. Build with all the c deps and well you’re compiling more code so of course it’s going to be slower but it’s disproportionately so.
- nikki93 5y agoFWIW, the overhead of calls across the cgo barrier is quite high, which can have API architecture implications -- https://github.com/dyu/ffi-overhead#results-500m-calls https://github.com/dyu/ffi-overhead#results-500m-calls.
- pjmlp 5y ago
- eloff 5y agoPresumably people would want to rewrite sqlite into Rust. But it's still a database, a low level, high performance software system. Even if you write it in rust, some percentage of the code will be unsafe rust or rust that calls a library written in C. It will be safer, but still not a panacea. It would be more viable in my opinion to improve the safety of the C code that currently makes up sqlite.
- tptacek 5y agoI do not agree with this at all, for what it's worth.
- wk_end 5y agoThat's not a useful or constructive comment. At best it is, indeed, worthless; at worst, you're using your stature on HN to bully another commenter by implying that their opinion can be dismissed out of hand, because you, tptacek, say so. 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.
- eloff 5y agoCan you clarify why?
- masklinn 5y agoThe amount of code which would have to be `unsafe` in a library like sqlite would be absolutely minuscule if it would exist at all, and much easier to check for than literally all of the codebase?
- eloff 5y agoWell, yeah, it's safer, there's no question about it. Is it sufficiently much safer to warrant the massive investment and the bugs (including security bugs) which will result from a rewrite? If I was the project manager I would not sign off on that. This is a volunteer project. Someone can of course do all that work free of charge, but they'd really just creating a fork of sqlite unless they also get most of the other major volunteers to agree.
- cormacrelf 5y ago> in practice, people will deal with the exposure either by treating it as an externality ... or by carefully positioning sqlite so it isn't a meaningful part of the attack surface. 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.
- ksec 5y ago>But the "security" paragraphs in this page do the rest of the argument a disservice. 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.
- GoblinSlayer 5y agoNot sure if sqlite can have RCEs, it's an embedded sql engine, not a remotely accessible sql server, so presumably those RCEs are vulnerabilities in programs that expose sqlite to remote access.
- mdaniel 5y ago> 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. 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