Merge "Skip Anonymous Identity check in SIM/AKA/AKA' networks"
diff --git a/libwifi_hal/driver_tool.cpp b/libwifi_hal/driver_tool.cpp
index 3089ee0..0b393d7 100644
--- a/libwifi_hal/driver_tool.cpp
+++ b/libwifi_hal/driver_tool.cpp
@@ -14,10 +14,13 @@
  * limitations under the License.
  */
 
+#include <grp.h>
+#include <pwd.h>
+#include <sys/types.h>
+
 #include "wifi_hal/driver_tool.h"
 
 #include <android-base/logging.h>
-#include <private/android_filesystem_config.h>
 
 #include "hardware_legacy/wifi.h"
 
@@ -35,7 +38,29 @@
     return true;  // HAL doesn't think we need to load firmware for any mode.
   }
 
-  if (chown(WIFI_DRIVER_FW_PATH_PARAM, AID_WIFI, AID_WIFI) != 0) {
+  errno = 0;
+  struct passwd *pwd = getpwnam("wifi");
+  if (pwd == nullptr) {
+    if (errno == 0) {
+      PLOG(ERROR) << "No user 'wifi' found";
+    } else {
+      PLOG(ERROR) << "Error getting uid for wifi: " << strerror(errno);
+    }
+    return false;
+  }
+
+  errno = 0;
+  struct group *grp = getgrnam("wifi");
+  if (grp == nullptr) {
+    if (errno == 0) {
+      PLOG(ERROR) << "No group 'wifi' found";
+    } else {
+      PLOG(ERROR) << "Error getting gid for wifi: " << strerror(errno);
+    }
+    return false;
+  }
+
+  if (chown(WIFI_DRIVER_FW_PATH_PARAM, pwd->pw_uid, grp->gr_gid) != 0) {
     PLOG(ERROR) << "Error changing ownership of '" << WIFI_DRIVER_FW_PATH_PARAM
                 << "' to wifi:wifi";
     return false;
diff --git a/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java b/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java
index 5bf44ce..03925e8 100644
--- a/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java
+++ b/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java
@@ -2339,6 +2339,8 @@
         public void onNetworkRemoved(int id) {
             synchronized (mLock) {
                 logCallback("onNetworkRemoved");
+                // Reset 4way handshake state since network has been removed.
+                mStateIsFourway = false;
             }
         }
 
diff --git a/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java b/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
index b4ea5af..eded7fc 100644
--- a/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
@@ -1201,6 +1201,30 @@
                 eq(WifiManager.ERROR_AUTH_FAILURE_WRONG_PSWD), eq(-1));
     }
 
+    /**
+     * Tests the handling of incorrect network passwords, edge case.
+     *
+     * If the network is removed during 4-way handshake, do not call it a password mismatch.
+     */
+    @Test
+    public void testNetworkRemovedDuring4way() throws Exception {
+        executeAndValidateInitializationSequence();
+        assertNotNull(mISupplicantStaIfaceCallback);
+
+        int reasonCode = 3;
+
+        mISupplicantStaIfaceCallback.onStateChanged(
+                ISupplicantStaIfaceCallback.State.FOURWAY_HANDSHAKE,
+                NativeUtil.macAddressToByteArray(BSSID),
+                SUPPLICANT_NETWORK_ID,
+                NativeUtil.decodeSsid(SUPPLICANT_SSID));
+        mISupplicantStaIfaceCallback.onNetworkRemoved(SUPPLICANT_NETWORK_ID);
+        mISupplicantStaIfaceCallback.onDisconnected(
+                NativeUtil.macAddressToByteArray(BSSID), true, reasonCode);
+        verify(mWifiMonitor, times(0)).broadcastAuthenticationFailureEvent(any(), anyInt(),
+                anyInt());
+    }
+
      /**
       * Tests the handling of incorrect network passwords, edge case.
       *