Reorder the commands in whitelist chains.

Currently FirewallController::replaceUidChain uses the same
layout when building whitelist and blacklist chains: first it
writes the exception rules (e.g., system apps, RST packets,
ICMPv6 packets, etc.), and then the UIDs in the chain.

This works, but it looks strange because unlike whitelist chains,
insertion into whitelist chains always happens at the front of
the chain. Make whitelist chains start with the UIDs, so that
when UIDs are added at the beginning, they are contiguous to the
UIDs that are already there.

Bug: 32073253
Test: netd_{unit,integration}_test passes
Test: bullhead builds, boots
Test: fw_powersave chain looks sane
Change-Id: I8a0ac7a33604455171b56e1d503cfe028a37a062
diff --git a/server/FirewallController.cpp b/server/FirewallController.cpp
index 4693206..a33d92e 100644
--- a/server/FirewallController.cpp
+++ b/server/FirewallController.cpp
@@ -233,6 +233,17 @@
     std::string commands;
     StringAppendF(&commands, "*filter\n:%s -\n", name);
 
+    // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'.
+    if (isWhitelist) {
+        for (auto uid : uids) {
+            StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid);
+        }
+
+        // Always whitelist system UIDs.
+        StringAppendF(&commands,
+                "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
+    }
+
     // Always allow networking on loopback.
     StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name);
     StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name);
@@ -249,16 +260,13 @@
                        name, ICMPV6_TYPES[i]);
             }
         }
-
-        // Always whitelist system UIDs.
-        StringAppendF(&commands,
-                "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
     }
 
-    // Whitelist or blacklist the specified UIDs.
-    const char *action = isWhitelist ? "RETURN" : "DROP";
-    for (auto uid : uids) {
-        StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j %s\n", name, uid, action);
+    // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'.
+    if (!isWhitelist) {
+        for (auto uid : uids) {
+            StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid);
+        }
     }
 
     // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a