6 ms·
I have a question: How do Rust and Go compare in terms of security? Or are they both very similar in terms of mitigating certain classic C vulnerability classes
by Someone1234 12y ago
I have a question: How do Rust and Go compare in terms of security? Or are they both very similar in terms of mitigating certain classic C vulnerability classes.
- nothrabannosir 12y agoZero experience with Rust beyond watching tutorials and talks, please please correct me if Im wrong. Rust has one killer feature that Go does not have: the typesystem has semantics for memory sharing. You can declare how a pointer can be used: is it accessed concurrently, is it local to me, &c. This helps the typesystem detect race conditions where two threads access a block of memory in an unsafe way. Go does not have this. In Go, you "share memory by communicating", instead of "communicating by sharing memory." But if you do want to share memory, you are back to using locks and your own conventions and mechanics to prevent concurrent access. The language doesn't help you. So it's a different programming paradigm. And if you use Go like you would use Rust (memory sharing), then Go is less safe. (...right?) EDIT: This sounds one-sided, but I'm only talking about sharing memory, here. If you don't do that, and stick to communicating, then Go can blow you away. I don't know how Rust deals with coroutines, but Go has super light threads (goroutines) that make concurrent behavior pure joy. All your threads can so easily talk to eachother, and wait for eachother, and it's all so light and native and good! E.g. you have a handler for a POST, but you only want to close the connection when another thread has finished doing something. And you don't want to tie up an entire OS thread, because it could take a while and that's just too expensive. In go: super easy, just do a blocking channel read. In another language? Wow, where would I start... (How does rust do that?)
- slimsag 12y agoUsing a plain-old mutex in Go isn't encouraged. As you said, the memory model is _share by communicating_. Sometimes, yes, using a mutex makes more sense -- and that does introduce the possibility of a data race. If you follow effective Go code -- I imagine the code is on average about just as safe as Rust code is. It is partially comparable to using `unsafe` blocks in Rust -- don't do it, unless you know what you're doing. Stick with channels.
- nothrabannosir 12y agoExample from personal experience: I'm working on an audio app right now, and I have high bandwidth, low latency data. I'm CPU bound, allocations and the GC are my enemy. It's just not feasible to constantly copy those buffers around. :( Don't take this the wrong way, I'm not complaining about Go. Just know that sometimes, there are real reasons to share memory. And when you do: Rust has got your back, but in Go you're on your own.
- giovannibajo1 12y agoNot sure what you mean; channels don't force you to copy buffers, you can simply pass the pointer/slice through the channel, but the point is that the paradigm encourages you to pass the ownership of that buffer through the channel.
- Animats 12y ago"the paradigm encourages you to pass the ownership of that buffer through the channel." Rust transfers ownership when you send a single-owner reference over a channel. The sender can no longer access the referenced object; the compiler prevents that. That's the safe way to do it. This is a significant advance in multi-thread programming. The rules that make this work are strikingly simple. Somebody should have thought of this 20 or 30 years ago, but as far as I know, nobody did. Rust pushes the single-owner model hard, and it seems to work. The C++ crowd beat their head against the wall on this for decades, through three generations of auto_ptr, then unique_ptr. Really getting it right requires something like the Rust borrow checker, which C++ does not have. Go does not do anything to prevent sharing data between threads. It's very easy in Go to unwittingly create shared data, because slices are references. After you've sent a reference, the sender still has access to the shared data, as does the receiver, so there's a potential race condition. Go does nothing to prevent this. There's a lot of dancing around this issue in "(In)effective Go", and long discussions of "is this a race condition" in Go forums. Go is a useful language, but it's in spite of Go's concurrency system, not because of it.
- rdtsc 12y ago
- jamii 12y ago> (How does rust do that?) My first thought would be to attach the connection object to the data the other thread is working on. When it is done it processing your message it drops the whole thing, closing the connection. If you want to hold it open for multiple workers you could make it an Arc<Connection> so it doesn't close until they all drop it.
- slimsag 12y agoBackground: I have been developing in Go for ~5 years, and Rust for probably less than 2 months. - Both Go and Rust use bounds checking on arrays/slices (which means out-of-bounds accesses are not allowed). - Go is garbage collected, so memory leaks cannot occur. - Rust encourages an ownership/lifetime model, so memory leaks cannot occur. - Go can still have data races between multiple concurrent threads. To catch them, you must use the race detector _at run time_. Note however that Go encourages communicating information over channels, and if idiomatic code is being written the chance of data races is next to none. - Rust cannot have data races in most cases, as it's ownership/lifetime model allows for catching them _at compile-time_. - Both of these assumptions are based on no external C or unsafe code being in the picture. TL;DR: Very similar. EDIT: I used the wrong terminology -- sorry. Instead of _memory leak_ I should have said _dangling pointers_ (memory leaks can occur in _any_ language, Go/Rust/Java/JS/etc).
- readymade 12y agoMemory leaks are still possible when using GC, of course, unless one operates with a strict definition that only includes unreferenced objects.
- osn 12y agoGC doesn't preven all the memory leaks. En easy example is a stack class backeed by an array. If you need more members, you allocate a bigger array and copy members. (The GC takes care of the smaller old array). So far so good. But You forget to allocate smaller array (and copy members) when you consume too much memory. So 1000000* push(something) followed by 1000000*pop() keeps plenty allocated memory not in usage. (It's not as brutal as malloc() and forget the result but still pretty awfull situation in a large project.)
- Dewie 12y agoAccording to that definition of memory leak, I guess you can call anything that doesn't eagerly free memory for memory leak. And the term gets quite weak and vague in that case.
- deleted 12y ago[deleted]
- mavdi 12y agoI've played around with both languages and to my limited knowledge Go just seems like a more modern C language with a GC and easy multithreading. Rust on the other hand is a completely different animal to anything I've used before. It has one of the most annoying compilers but that is for a reason, if your code compiles then it's memory safe (with a few exceptions, like 'unsafe' block)
- zmanian 12y agoRust is one of rare class of memory safe languages that are - easily callable from other languages because of the C ABI - runtime free and not Posix dependent - portable across CPU architectures. Rust will provide you memory safety where other languages can't even run.
- bjwbell 12y ago- easily call C libraries, practically trivial. I hated using the FFI's in Java & C#. There's many things I don't like about rust but the above list make it very hard to beat.
- ngrilly 12y agoRust is not "runtime free". Rust needs a "minimal runtime", as stated on the project home page. You need at least a memory allocator. Even C programs are usually linked to a runtime library (like glibc).
- Jweb_Guru 12y agoRust is usable with an extremely minimal runtime that approximates having no runtime at all (with #[no_std]). There are a few intrinsics you have to define, but none of them involve memory allocation. It also has a core crate that does not expose anything that requires allocation.
- ngrilly 12y agoInteresting! I was unaware of the existence of #[no_std]. My mistake. Thanks for putting it right :)
- plq 12y agoThey're practically same, but rust pulled it off without a gc.
- Symmetry 12y agoExactly. I can easily see someone re-implementing OpenSSL in Rust. Not so much with Go.
- slimsag 12y agoRust does not yet have a SSL library implemented as far as I am aware of. Go, however, does indeed have it's own pure-Go SSL library[1] and does not use OpenSSL at all. [1] http://golang.org/pkg/crypto/tls/ http://golang.org/pkg/crypto/tls/
- Symmetry 12y agoI should have been more clear. By "re-implementing OpenSSL in Rust" I meant making an implementation of SSL that would be used the same way as OpenSSL is, dynamically linked from C or various other programming languages. If you want a static library to compile into your Go application then of course doing it in Go is the most straightforward way to do it.
- tptacek 12y agoGolang has possibly the best TLS library implemented in any language. Maybe you instead mean that nobody would drop a Golang TLS library into a C program, but that they might do that with a Rust TLS library? That's truey, but not particularly interesting. Most languages can't be dropped easily into C programs.
- tsuraan 12y agoMaking a library that can be linked into a C program is interesting because it can also be linked into a program written in pretty much any language. OpenSSL is awful, but it has bindings to pretty much any language that you want, so it's very commonly used. Go's TLS will never have that, but one implemented in Rust could.
- pcwalton 12y agoI can't speak to security as a whole, since security encompasses more than memory safety. But regarding memory safety, they're similar. Rust has an edge in that it can use hardware parallelism while remaining memory safe, while Go is not memory safe if GOMAXPROCS > 1. However, the memory-unsafe part of Go is a corner (racing on a map or slice) that hasn't caused any real-world security problems I'm aware of. (Disclaimer: I'm a Rust developer.)
- slimsag 12y agoI generally agree with what you're saying here. The difference appears to be that Rust forces you on a compiler level to be safe of any potential race-condition. Go doesn't enforce this on a compiler level -- but instead it encourages the use of sharing by communicating (over channels, where data races cannot occur), instead of sharing memory e.g. via a mutex (where you can run into a data race).
- steveklabnik 12y agoImportant distinction: Rust forces you to be free of data races, which are a subset of race conditions. A very important subset, mind you, but they're different. It's not really possible to prevent general race conditions, like any form of logic error.