Ghidra is a software reverse engineering (SRE) framework
Corrected cleanup for server authentication modules
ghidra17b33dd7cc270
diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/JAASAuthenticationModule.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/JAASAuthenticationModule.javaindex ac9c079461..8dfafb8d5f 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/JAASAuthenticationModule.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/JAASAuthenticationModule.java@@ -121,6 +121,13 @@ public class JAASAuthenticationModule implements AuthenticationModule {}throw new FailedLoginException(e.getMessage());}+ finally {+ PasswordCallback srcPcb =+ AuthenticationModule.getFirstCallbackOfType(PasswordCallback.class, callbacks);+ if (srcPcb != null) {+ srcPcb.clearPassword();+ }+ }String loginNameResult = loginName.get();return (loginNameResult != null) ? loginNameResult : principal.getName();diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/Krb5ActiveDirectoryAuthenticationModule.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/Krb5ActiveDirectoryAuthenticationModule.javaindex a55c495bee..9f128657a3 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/Krb5ActiveDirectoryAuthenticationModule.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/Krb5ActiveDirectoryAuthenticationModule.java@@ -94,49 +94,59 @@ public class Krb5ActiveDirectoryAuthenticationModule implements AuthenticationMoAtomicReference<String> userName = new AtomicReference<>();- LoginContext lc = new LoginContext("", null, (loginmodule_callbacks) -> {- // The Krb5LoginModule tends to call this callback handler multiple times.- // Once for name, and then again for password.- PasswordCallback srcPcb = AuthenticationModule.getFirstCallbackOfType(- PasswordCallback.class, ghidra_callbacks);- NameCallback srcNcb =- AuthenticationModule.getFirstCallbackOfType(NameCallback.class, ghidra_callbacks);- String tmpName =- (allowUserToSpecifyName && srcNcb != null) ? srcNcb.getName() : principal.getName();- if (stripDomainFromUsername && tmpName.contains("\\")) {- tmpName = tmpName.replaceFirst("^.*\\\\", "");- }-- if (tmpName == null || srcPcb == null || tmpName.isBlank()) {- throw new IOException("Missing username or password values");- }+ NameCallback srcNcb =+ AuthenticationModule.getFirstCallbackOfType(NameCallback.class, ghidra_callbacks);- NameCallback destNcb = AuthenticationModule.getFirstCallbackOfType(NameCallback.class,- loginmodule_callbacks);- PasswordCallback destPcb = AuthenticationModule.getFirstCallbackOfType(- PasswordCallback.class, loginmodule_callbacks);+ PasswordCallback srcPcb =+ AuthenticationModule.getFirstCallbackOfType(PasswordCallback.class, ghidra_callbacks);- if (destNcb != null) {- destNcb.setName(tmpName);+ try {+ LoginContext lc = new LoginContext("", null, (loginmodule_callbacks) -> {+ // The Krb5LoginModule tends to call this callback handler multiple times.+ // Once for name, and then again for password.++ String tmpName = (allowUserToSpecifyName && srcNcb != null) ? srcNcb.getName()+ : principal.getName();+ if (stripDomainFromUsername && tmpName.contains("\\")) {+ tmpName = tmpName.replaceFirst("^.*\\\\", "");+ }++ if (tmpName == null || srcPcb == null || tmpName.isBlank()) {+ throw new IOException("Missing username or password values");+ }++ NameCallback destNcb = AuthenticationModule.getFirstCallbackOfType(+ NameCallback.class, loginmodule_callbacks);+ PasswordCallback destPcb = AuthenticationModule.getFirstCallbackOfType(+ PasswordCallback.class, loginmodule_callbacks);++ if (destNcb != null) {+ destNcb.setName(tmpName);+ }++ if (destPcb != null) {+ destPcb.setPassword(srcPcb.getPassword());+ }++ userName.set(tmpName);+ }, new JAASConfiguration("com.sun.security.auth.module.Krb5LoginModule"));++ try {+ lc.login();}-- if (destPcb != null) {- destPcb.setPassword(srcPcb.getPassword());+ catch (LoginException e) {+ // Convert plain LoginExceptions to FailedLoginExceptions to enable+ // the client to retry the login if desired.+ if (e instanceof FailedLoginException) {+ throw e;+ }+ throw new FailedLoginException(e.getMessage());}-- userName.set(tmpName);- }, new JAASConfiguration("com.sun.security.auth.module.Krb5LoginModule"));-- try {- lc.login();}- catch (LoginException e) {- // Convert plain LoginExceptions to FailedLoginExceptions to enable- // the client to retry the login if desired.- if (e instanceof FailedLoginException) {- throw e;+ finally {+ if (srcPcb != null) {+ srcPcb.clearPassword();}- throw new FailedLoginException(e.getMessage());}return userName.get();diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/loginmodule/ExternalProgramLoginModule.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/loginmodule/ExternalProgramLoginModule.javaindex 0b163d6b35..90cf3325d5 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/loginmodule/ExternalProgramLoginModule.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/security/loginmodule/ExternalProgramLoginModule.java@@ -234,6 +234,8 @@ public class ExternalProgramLoginModule implements LoginModule {throw new LoginException("Error executing external program");}finally {+ Arrays.fill(password, '\0');+ password = null;Process p = process.get();if (p != null && p.isAlive()) {if (p.isAlive()) {