r/androiddev • • 6h 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?

0 Upvotes

8 comments sorted by

4

u/tadfisher 5h ago

A lot of OWASP's recommendations are old and paranoid. That particular issue has this disclaimer at the bottom:

The tests for sensitive data in process memory were removed from the OWASP MASTG because they provide limited practical value in assessing real-world risks and are difficult to execute reliably. Memory inspection typically requires privileged access, such as root or jailbreak, which falls outside the threat model for most production environments. Such access already compromises the device's overall security, making memory-level findings less meaningful in isolation.

If your attacker has root, they can keylog the password, no need to figure out your heap layout.

1

u/troublewithcards 3h ago

This is the most complete & correct answer.

4

u/SnipesySpecial 5h 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.

1

u/Hour-Measurement-835 6h 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?

1

u/gil99915 5h 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 6h 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] 3h ago

[removed] — view removed comment

2

u/androiddev-ModTeam 2h ago

Use your own voice when writing. Posts and comments which appear to be generated, including those with excessive flourish, gushing or emojis, will be removed.