2

Although What is the best way to store password in memory (RAM) in Java? covers the same territory, this question has the aspect of noticing what other people do and wondering why. The question on SO: https://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords is a better duplicate, though that does not work cross-SE-site!


I note that in Java, the String type is immutable and safe, yet using char[] for password handling is pretty common.

Two concrete examples are:

I mean, look at this code, (taken from the JAAS tutorial) ... mostly avoidable if PasswordCallback#getPassword() would just return a String.

        callbackHandler.handle(callbacks);
        char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
        if (tmpPassword == null) {
            // treat a NULL password as an empty password
            tmpPassword = new char[0];
        }
        password = new char[tmpPassword.length];
        System.arraycopy(tmpPassword, 0,
                    password, 0, tmpPassword.length);
        ((PasswordCallback)callbacks[1]).clearPassword();

I feel like I'm missing why smart system-programmers go to such trouble (at least, they did in the late 90's, in the infancy of Java). But to me the above hoo-hah just achieves moving the information from one char[] on the heap to another char[] that's presently only on the stack? (I'm not sure enough of my JVM primitive array allocations, to really be sure of this).

Is this:

  • just a habit brought across from C/C++ where an improperly terminated string could cause a buffer-overflow
  • a way to force programmers to avoid string literals that would be 'interned' by the compiler when testing for 'does the input match the hard-coded-secret' (which isn't going to happen except in throwaway code anyway)
  • a strong and reasonable commitment to ensuring that 'insofar as it depends on me', clear-text secrets are kept clear in memory for the absolute shortest amount of time, in case of an attacker with permissions to inspect memory? (That degree of access by an attacker is basically game-over anyway: sure, we can make it just that little bit more hard for them, but really, this is called doubting the integrity of your execution environment, and that's ... a really heavy burden for an application!)

What design force am I not seeing?

David Bullock
  • 542
  • 4
  • 10
  • 1
    I think your last point is spot on. It's done so passwords can be scrubbed from memory. Doing so is mostly security theatre, although I'd personally do it anyway as a matter of good design. – paj28 Feb 09 '21 at 23:38
  • 4
    @paj28 I feel like something can either be security theater or good design, but not both. Maybe that's just me though? – Conor Mancone Feb 10 '21 at 00:08
  • 2
    This is a duplicate of a question on SO: https://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords, and this question on Security SE: https://security.stackexchange.com/questions/73637/what-is-the-best-way-to-store-password-in-memory-ram-in-java. I suggest to close it. – mentallurg Feb 10 '21 at 00:18
  • 3
  • @ConorMancone: You are right, security theater by design. – mentallurg Feb 10 '21 at 00:21
  • Am agreed with closing this question. https://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords is the better 'duplicate', but I don't know if you can dup against a different SE site. – David Bullock Feb 10 '21 at 03:45
  • My feeling after reviewing all the info is that, despite what trouble you take, the chances of the secret leaking to places in the JVM memory where you've got not control over it, are quite high. So I am not normally going to trouble myself about it in situations where I can't in general, prove that the entire platform hosting the VM where my app is running, hasn't been pwned already. – David Bullock Feb 10 '21 at 03:55
  • I also would like to differentiate between 'any passphrase' and 'passphrases transiently supplied at runtime, using I/O, specifically in order to authenticate'. In the former case, any secret that the application must ultimately read from disk is often not worth the effort of scrubbing from memory (unless the attack is a 'read the leftovers from the memory page I just got handed' attack on an otherwise well-configured system). In the latter case, yes, it may be worth the effort. – David Bullock Feb 10 '21 at 04:28
  • @ConorMancone - fair enough. What do you suggest then? – paj28 Feb 10 '21 at 13:58

0 Answers0