Hacker News new | ask | show | jobs
by hnarn 19 days ago
How would Rust have fixed this security issue?
4 comments

One of the core claims of the language is data race safety because in safe rust, no memory location can be written by two or more owners at the same time. (see https://doc.rust-lang.org/nomicon/races.html for a more detailed discussion) Consequently, the kind of memory corruptions that this vulnerability seems to exploit are not possible in safe rust. Now, we don't know if a hypothetical rust-based apache implementation would have reached for unsafe in this context or done any other wild thing, but it's fair to assume that the likelihood of an exploitable RCE through memory corruption trends towards zero in programs written in Rust.
The details I've seen are too sparse to say with certainty, but it seems like the issue is concurrent access to the same value through a pointer.

In particular it's `req` in the function with this signature:

    static void assign_headers(request_rec *r, const h2_request *req,
                               int no_body, int is_connect)
The fix being:

                - apr_table_setn(req->headers, "Content-Length", "0");
                + apr_table_setn(r->headers_in, "Content-Length", "0");
My read on this is that `req` is meant to be a readonly value in this function, which is why it's marked `const`. And being readonly, it would be safe to access concurrently. However, someone accidentally wrote through this pointer.

In Rust this would've been `req: &h2_request` and the accidental write wouldn't have been possible (barring interior mutability). Even with interior mutability there is no thread interleaving during the write, which happens through a `&mut h2_request` probably obtained through a lock.

For what it is worth, the code block that precedes this one is an if-condition that has a call to apr_table_setn with as `r` vs. req as the first parameter. This could very well be a case of a developer missing one code block. It could also be that the repo's compiler settings aren't strict enough to flag this as an issue -> writing to a const.

Where this is one such oversight, there are likely others. An LLM isn't needed to find these, likely stricter compiler flags.

Here's the commit from 4 years ago that refactored the code into its current state:

https://github.com/icing/mod_h2/commit/1b468851c89cdbf6d0f98...

Looks like he made the change on the Transfer-Encoding line when he was moving things around, but missed doing the same on the Content-Length line.

I don't believe C's const catches this regardless of compiler settings. What's disallowed by const here is `req->header = x`, but the type of `req->header` is a pointer, which is not const. The constness of pointers is not transitive like that. By contrast, in Rust: `t.u` when `t` is `&T`, is `&U`.
> > a concurrent execution race condition within the assign_headers() function when handling shared resources [...] can corrupt heap memory to achieve arbitrary code execution (RCE)

To my understanding, in safe Rust to share between threads the compiler will require something like Arc<Mutex<...>> - which prevents unsynchronized access or freeing while another thread is still using it.

They've published an article last month on the Nginx vulnerabilities in light of their Rust implementation.

https://xin.dev/blog/multiple-nginx-vulnerabilities/

If you’re going to respond, at least make an attempt to answer the question.
It's not for Apache, that may be too recent (no blog post yet. For if you want to see how Rust can prevent vulnrs, the blog post explains it nicely.

Sorry for you I did not meet your standards. Pfff.

The question was how rust would prevent this vulnerability.
Rust doesn't let you write to read only variables, duh.