When the method that reads the password pops out of the stack, the array object referenced by the passwd local variable will be eligible for garbage collection. No one (even an attacker) could obtain a reference to that array, assuming that the array does not escape the method scope.
Alas, it is not quite this simple: The Java language gives no guarantees how soon a garbage collection will occur, and it may be possible for an attacker to read the memory in the mean time.
to minimize the lifetime of sensitive data in memory
This advice obviously targets a threat model where the attacker has intermittent read access to heap memory, and we must still do our best to make it harder (but not necessarily impossible) for the attacker to aquire sensitive data.
Whether you have this threat model is something only you can assess, but it not as absurd as it may seem at first glance, because the various systems involved do not necessarily guarantee the privacy of main memory, for instance:
JVM
- a heap dump also contains unreachable objects (some tools filter these when opening the dump file, but they are present in the file and can be recovered)
- most garbage collectors copy the live objects from the old to the new memory space. These copies may outlive the object itself, and show up in a dump of the process' memory.
operating system
- the operating system's virtual memory manager may elect to swap memory to disk, where it may remain after shut down, and recovered (for instance by booting into another operating system)
- the operating system's hibernation feature also writes main memory to disk
- some operating system's virtual memory manager do not clear physical memory when reassigning it to another process. That other process can then read the data left behind by the previous process.
- the operating system may allow a process to debug another, giving one process access to another process' memory.
hypervisor
- when the hypervisor takes a snapshot of a VM, it writes virtual main memory to the disk. If the attacker gets access to that snapshot ...
hardware
- the main memory of the system may retain state even after power off, enabling the data to be inspected from another operating system or computer. Perhaps suprisingly, most modern DRAM chips fall into this category (source)
- and of course, hardware could contain a back door, as reported for Cisco's routers
That said, most of these attacks presume a level of access that would enable far more serious attacks, and should therefore be prevented anyway, in which case reducing the life time of sensitive data in memory is rather redundant. Personally, I would not defend data but storing it in memory for as short a time as possible, but defend the privacy of memory by using trusted hardware, physically secured against tampering, secure operating systems configured by professionals, with os-level access restricted as far as practical, and secure storage for backups and diagnostic dumps of any kind.
Therefore, it is my opinion that the use of char[]
for passwords is rarely useful. Incidentally, this opinion appears to be shared by the designers of the JDBC API, which requires connection passwords to be passed as String
, preventing them from being cleared after the connection is established.
Nevertheless, there may be cases where the privacy of main memory can not be guaranteed, and damage mitigation by keeping the window of vulnerability as small as possible is appropriate. Your threat model should answer whether this is such a case.