by acatton · 3y ago · 0 comments
With garbage collected language, programmers don't know when their variable is "free()ed", since it could be held in multiple thread, and the last thread dying will release the memory for this variable. Since programmers often don't know when the password variable will be "free()ed", it is very unlikely that they scrambled the password before releasing the memory. Thus, leading to the password being kept in some region of the program memory, in clear text, exploitable by a buffer overflow & co. for an indefinite amount of time.
> G1 reclaims space mostly by using evacuation: live objects found within selected memory areas to collect are copied into new memory areas, compacting them in the process. After an evacuation has been completed, the space previously occupied by live objects is reused for allocation by the application.
[1] https://docs.oracle.com/en/java/javase/18/gctuning/garbage-f...
If you are worried, you can store the password in a byte array and zero that out.
But further, a buffer overflow is practically impossible with a GCed language (especially a popular one). A programmer using a GCed language cannot write code which has a buffer overflow. That must come from a bug in the runtime itself. Not likely.
As far as I'm aware, every gced language has bounds checks on arrays. Certainly all the popular ones.
> the last thread dying will release the memory for this variable.
Really not how GCed languages work. Memory lifetime is not bound by thread lifetime except in the rare case when memory is bound to a thread (static/global variables).
The majority of GCs on the market are tracing collectors. Memory is periodically collected when it is not longer referenced (mark and sweep). What triggers that collection is a whole host of potentials interactions. How frequently and deep it runs also depends on a lot of application characteristics.
if it's a copying collector this is no guarantee of anything
but the same is true of general memory allocated with the OS (swap/THP/...), unless you use something like mlock()
Because the JVM had no buffer overflow? [1] Also I purposefully wrote "buffer overflow & co" because buffer overflow are not the only possibility. Shellcodes could ptrace() and inspect the memory of the program.
> > the last thread dying will release the memory for this variable.
> Really not how GCed languages work. Memory lifetime is not bound by thread lifetime except in the rare case when memory is bound to a thread (static/global variables).
That was bad phrasing from by part. You should have read "the last thread releasing the variable reference".
[1] https://www.cvedetails.com/vulnerability-list.php?vendor_id=...
Happens extremely rarely and is frequently not in JVM core code but rather something like the 2d renderer or applets. Code not likely to be executed on a server.
Take a deeper look into those CVEs and count how many are for Java 8+ and server code (it's a pretty short list).
You might as well argue the linux kernel is insecure because there's been buffer overflows in the various drivers.
> Shellcodes could ptrace() and inspect the memory of the program.
Certainly, and they can redirect socket traffic and inject a MITM for any process to directly intercept a password. Even if you are zeroing memory, there will be a period of time when a password is present in memory which means the ptrace attack also works with C.
The bad part of a managed language is that passwords stay in memory for longer, but that risk is somewhat moot considering exploiting requires a compromised system. In which case, there's little reason to pull out passwords by sniffing memory.
Take 2 classes that reference each other, but nothing references either.
In your example they won't get cleaned up. You'll want a tree-based collector. If an object is no longer referenced from the tree it can be cleaned up.
And there is simply no way to deal with this, technology just isn't there yet.
What does the attack that does this actually look like? Lastpass reads text off an html page and decides when to inject auto-fill prompts and/or enter in a password. Is it possible for a buffer overflow to be exploited there, that lets an attacker (who controls the site) gain access to a password for a different site? How does that work?
Is there some other attack possible here? Ie: is it possible another user-space application can read passwords from memory used by the password manager? How does another app know it's a password? How does it trigger a buffer overflow?
(I'm ignoring apps running with kernel or privileged access: that seems like game over)
Your best bet is to ensure no references to the password string exist - including in library code you may use, which means constant revalidations whenever you update a library or your underlying language runtime. Once you do that, you can force garbage collection, either by some explicit language mechanism to request garbage collection, or by trying to allocate gobs of memory in some way that can't be easily optimized out.
I won't get into the question of whether a programmer should also know all that, as there is another consideration which renders it moot: Even with a non-GC'd language, a programmer could leave variables with sensitive information in memory for the remaining duration of the program (e.g. local variables left deep in the stack when a function returns or throws an exception - do you know when they will be overwritten?) What matters here is that the programmer understands the risks, knows what constitutes the most sensitive data, and acts accordingly - but, armed with that knowledge, the programmer can just as well ameliorate the risk in a GC'd language as one that is not.
The issue is: what if thread A and thread B hold a reference to the password variable? And you don't know in which order they will execute. In which thread do you scramble the password before releasing the variable reference?
From what I understand, OP's point is: because of the nature of C, you have to know where your variable is "free()ed", because you have to do it yourself. Therefore you can scramble there. Even if you have two thread doing:
n = decr_reference(password)
if (n == 0) {
scramble(password)
free(password)
}This assumes you can write destructor at all and that it's run deterministically.
> though you can probably force that to happen in most GCed languages
I'd actually be surprised if any GC'd language allows this. You normally need to resort to different kinds of tricks for RAII-like behavior.
Or at the least, let a program implement parts of the gc api and plug it in themselves.
https://docs.oracle.com/javase/9/docs/api/java/lang/ref/Clea...
However, the issue isn't so much that doing that, rather it's that GC will potentially never collect the memory. In the JVM, the GC primarily runs when enough allocations happen. It's not on some timer.
So the concern would then be having a password sitting in memory for hours (or even days) on end.
This wouldn't help once you create your own objects however, e.g. JSON parse from a decrypted block.
However, using them correctly in the context of memory leakage and safety is non-intuitive and is definitely a bit tricky.
I'm not sure I see the issue, you just implement a memory-safe string class that manages arbitrary bytes, it's slower but that wasn't the goal.