Why is char[] preferred over String for passwords?

后端 未结 17 3645
清歌不尽
清歌不尽 2020-11-21 04:34

In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String)

相关标签:
17条回答
  • 2020-11-21 05:07

    These are all the reasons, one should choose a char[] array instead of String for a password.

    1. Since Strings are immutable in Java, if you store the password as plain text it will be available in memory until the Garbage collector clears it, and since String is used in the String pool for reusability there is a pretty high chance that it will remain in memory for a long duration, which poses a security threat.

    Since anyone who has access to the memory dump can find the password in clear text, that's another reason you should always use an encrypted password rather than plain text. Since Strings are immutable there is no way the contents of Strings can be changed because any change will produce a new String, while if you use a char[] you can still set all the elements as blank or zero. So storing a password in a character array clearly mitigates the security risk of stealing a password.

    2. Java itself recommends using the getPassword() method of JPasswordField which returns a char[], instead of the deprecated getText() method which returns passwords in clear text stating security reasons. It's good to follow advice from the Java team and adhere to standards rather than going against them.

    3. With String there is always a risk of printing plain text in a log file or console but if you use an Array you won't print contents of an array, but instead its memory location gets printed. Though not a real reason, it still makes sense.

    String strPassword="Unknown";
    char[] charPassword= new char[]{'U','n','k','w','o','n'};
    System.out.println("String password: " + strPassword);
    System.out.println("Character password: " + charPassword);
    
    String password: Unknown
    Character password: [C@110b053
    

    Referenced from this blog. I hope this helps.

    0 讨论(0)
  • 2020-11-21 05:07

    String is immutable and it goes to the string pool. Once written, it cannot be overwritten.

    char[] is an array which you should overwrite once you used the password and this is how it should be done:

    char[] passw = request.getPassword().toCharArray()
    if (comparePasswords(dbPassword, passw) {
     allowUser = true;
     cleanPassword(passw);
     cleanPassword(dbPassword);
     passw=null;
    }
    
    private static void cleanPassword (char[] pass) {
    
    Arrays.fill(pass, '0');
    }
    

    One scenario where the attacker could use it is a crashdump - when the JVM crashes and generates a memory dump - you will be able to see the password.

    That is not necessarily a malicious external attacker. This could be a support user that has access to the server for monitoring purposes. He could peek into a crashdump and find the passwords.

    0 讨论(0)
  • 2020-11-21 05:09

    The short and straightforward answer would be because char[] is mutable while String objects are not.

    Strings in Java are immutable objects. That is why they can't be modified once created, and therefore the only way for their contents to be removed from memory is to have them garbage collected. It will be only then when the memory freed by the object can be overwritten, and the data will be gone.

    Now garbage collection in Java doesn't happen at any guaranteed interval. The String can thus persist in memory for a long time, and if a process crashes during this time, the contents of the string may end up in a memory dump or some log.

    With a character array, you can read the password, finish working with it as soon as you can, and then immediately change the contents.

    0 讨论(0)
  • 2020-11-21 05:13

    Case String:

        String password = "ill stay in StringPool after Death !!!";
        // some long code goes
        // ...Now I want to remove traces of password
        password = null;
        password = "";
        // above attempts wil change value of password
        // but the actual password can be traced from String pool through memory dump, if not garbage collected
    

    Case CHAR ARRAY:

        char[] passArray = {'p','a','s','s','w','o','r','d'};
        // some long code goes
        // ...Now I want to remove traces of password
        for (int i=0; i<passArray.length;i++){
            passArray[i] = 'x';
        }
        // Now you ACTUALLY DESTROYED traces of password form memory
    
    0 讨论(0)
  • 2020-11-21 05:14

    Strings are immutable. That means once you've created the String, if another process can dump memory, there's no way (aside from reflection) you can get rid of the data before garbage collection kicks in.

    With an array, you can explicitly wipe the data after you're done with it. You can overwrite the array with anything you like, and the password won't be present anywhere in the system, even before garbage collection.

    So yes, this is a security concern - but even using char[] only reduces the window of opportunity for an attacker, and it's only for this specific type of attack.

    As noted in the comments, it's possible that arrays being moved by the garbage collector will leave stray copies of the data in memory. I believe this is implementation-specific - the garbage collector may clear all memory as it goes, to avoid this sort of thing. Even if it does, there's still the time during which the char[] contains the actual characters as an attack window.

    0 讨论(0)
  • 2020-11-21 05:14

    I don't think this is a valid suggestion, but, I can at least guess at the reason.

    I think the motivation is wanting to make sure that you can erase all trace of the password in memory promptly and with certainty after it is used. With a char[] you could overwrite each element of the array with a blank or something for sure. You can't edit the internal value of a String that way.

    But that alone isn't a good answer; why not just make sure a reference to the char[] or String doesn't escape? Then there's no security issue. But the thing is that String objects can be intern()ed in theory and kept alive inside the constant pool. I suppose using char[] forbids this possibility.

    0 讨论(0)
提交回复
热议问题