r/androiddev • u/_5er_ • 9d ago
Question Password in memory
Is it worth bothering with clearing password/pin from HEAP memory?
The issue is described in following OWASP link. In general it says, you should consider using `ByteArray` instead of `String`, so you can clear it after.
https://mas.owasp.org/MASTG/knowledge/android/MASVS-STORAGE/MASTG-KNOW-0051
I'm wondering to what degree this is an actual issue. Memory is mostly encrypted these days. And is probably not an issue, unless using a rooted device.
Would you implement this precaution for sensitive apps, such as trading, banking or similar apps?
3
Upvotes
13
u/tadfisher 9d ago
A lot of OWASP's recommendations are old and paranoid. That particular issue has this disclaimer at the bottom:
If your attacker has root, they can keylog the password, no need to figure out your heap layout.
4
u/SnipesySpecial 9d ago
It helps in the niche scenario some form of leakage does exist, and it’s slow (otherwise they could just read the split second it’s in memory and it doesn’t matter).
Those are very real attacks, and I’ve broken hardware crypto wallets doing this kind of stuff. In that scenario simply clearing the key would have made the attack take years instead of seconds.
So…. Quite real.
2
u/Hour-Measurement-835 9d ago
We do clearPassword() after PBKDF2, but the keypad builds a fresh Compose String on every keypress before that. Has anyone actually managed to wipe those?
2
u/Opulence_Deficit 7d ago
This recommendation is precisely not about heap.
Strings is not a primitive, the way int is, that's a lesson from C. So, in order for JVM to pretend that strings behave as primitives, it has to have some kind of "string pool". And you never know how that thing works. It usually isn't in the heap space you can access. E.g. it theoretically could be systemwide, shared between apps, or kept between instances of your app.
That's why it's important to never put anything important in a String, because it goes straight to some system you have absolutely no control about.
It's not about HAVING to clear it from memory, it's about reminding that x=null does not clear the String from memory, and there will be absolutely no way to do it if you ever need to.
1
u/gil99915 9d ago
To be fair. It's a very small change (code wise) and in theory it's more secure, so why not.
You can probably bake it right into a modifier or something and create a detekt rule to enforce it
0
u/AutoModerator 9d ago
Please note that we also have a very active Discord server where you can interact directly with other community members!
Join us on Discord
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/[deleted] 9d ago
[removed] — view removed comment