Loading services/core/java/com/android/server/ConnectivityService.java +11 −1 Original line number Diff line number Diff line Loading @@ -222,6 +222,7 @@ import java.io.FileDescriptor; import java.io.PrintWriter; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; Loading Loading @@ -989,6 +990,15 @@ public class ConnectivityService extends IConnectivityManager.Stub return NetworkUtils.queryUserAccess(uid, netId); } /** * Gets the UID that owns a socket connection. Needed because opening SOCK_DIAG sockets * requires CAP_NET_ADMIN, which the unit tests do not have. */ public int getConnectionOwnerUid(int protocol, InetSocketAddress local, InetSocketAddress remote) { return InetDiagMessage.getConnectionOwnerUid(protocol, local, remote); } /** * @see MultinetworkPolicyTracker */ Loading Loading @@ -8350,7 +8360,7 @@ public class ConnectivityService extends IConnectivityManager.Stub throw new IllegalArgumentException("Unsupported protocol " + connectionInfo.protocol); } final int uid = InetDiagMessage.getConnectionOwnerUid(connectionInfo.protocol, final int uid = mDeps.getConnectionOwnerUid(connectionInfo.protocol, connectionInfo.local, connectionInfo.remote); /* Filter out Uids not associated with the VPN. */ Loading tests/net/java/com/android/server/ConnectivityServiceTest.java +5 −7 Original line number Diff line number Diff line Loading @@ -8355,13 +8355,14 @@ public class ConnectivityServiceTest { private void setupConnectionOwnerUid(int vpnOwnerUid, @VpnManager.VpnType int vpnType) throws Exception { final Set<UidRange> vpnRange = Collections.singleton(UidRange.createForUser(PRIMARY_USER)); mMockVpn.setVpnType(vpnType); mMockVpn.establish(new LinkProperties(), vpnOwnerUid, vpnRange); assertVpnUidRangesUpdated(true, vpnRange, vpnOwnerUid); mMockVpn.setVpnType(vpnType); final UnderlyingNetworkInfo underlyingNetworkInfo = new UnderlyingNetworkInfo(vpnOwnerUid, VPN_IFNAME, new ArrayList<String>()); mMockVpn.setUnderlyingNetworkInfo(underlyingNetworkInfo); when(mDeps.getConnectionOwnerUid(anyInt(), any(), any())).thenReturn(42); } private void setupConnectionOwnerUidAsVpnApp(int vpnOwnerUid, @VpnManager.VpnType int vpnType) Loading Loading @@ -8410,8 +8411,7 @@ public class ConnectivityServiceTest { final int myUid = Process.myUid(); setupConnectionOwnerUidAsVpnApp(myUid, VpnManager.TYPE_VPN_SERVICE); // TODO: Test the returned UID mService.getConnectionOwnerUid(getTestConnectionInfo()); assertEquals(42, mService.getConnectionOwnerUid(getTestConnectionInfo())); } @Test Loading @@ -8421,8 +8421,7 @@ public class ConnectivityServiceTest { mServiceContext.setPermission( android.Manifest.permission.NETWORK_STACK, PERMISSION_GRANTED); // TODO: Test the returned UID mService.getConnectionOwnerUid(getTestConnectionInfo()); assertEquals(42, mService.getConnectionOwnerUid(getTestConnectionInfo())); } @Test Loading @@ -8433,8 +8432,7 @@ public class ConnectivityServiceTest { mServiceContext.setPermission( NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, PERMISSION_GRANTED); // TODO: Test the returned UID mService.getConnectionOwnerUid(getTestConnectionInfo()); assertEquals(42, mService.getConnectionOwnerUid(getTestConnectionInfo())); } private static PackageInfo buildPackageInfo(boolean hasSystemPermission, int uid) { Loading Loading
services/core/java/com/android/server/ConnectivityService.java +11 −1 Original line number Diff line number Diff line Loading @@ -222,6 +222,7 @@ import java.io.FileDescriptor; import java.io.PrintWriter; import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; Loading Loading @@ -989,6 +990,15 @@ public class ConnectivityService extends IConnectivityManager.Stub return NetworkUtils.queryUserAccess(uid, netId); } /** * Gets the UID that owns a socket connection. Needed because opening SOCK_DIAG sockets * requires CAP_NET_ADMIN, which the unit tests do not have. */ public int getConnectionOwnerUid(int protocol, InetSocketAddress local, InetSocketAddress remote) { return InetDiagMessage.getConnectionOwnerUid(protocol, local, remote); } /** * @see MultinetworkPolicyTracker */ Loading Loading @@ -8350,7 +8360,7 @@ public class ConnectivityService extends IConnectivityManager.Stub throw new IllegalArgumentException("Unsupported protocol " + connectionInfo.protocol); } final int uid = InetDiagMessage.getConnectionOwnerUid(connectionInfo.protocol, final int uid = mDeps.getConnectionOwnerUid(connectionInfo.protocol, connectionInfo.local, connectionInfo.remote); /* Filter out Uids not associated with the VPN. */ Loading
tests/net/java/com/android/server/ConnectivityServiceTest.java +5 −7 Original line number Diff line number Diff line Loading @@ -8355,13 +8355,14 @@ public class ConnectivityServiceTest { private void setupConnectionOwnerUid(int vpnOwnerUid, @VpnManager.VpnType int vpnType) throws Exception { final Set<UidRange> vpnRange = Collections.singleton(UidRange.createForUser(PRIMARY_USER)); mMockVpn.setVpnType(vpnType); mMockVpn.establish(new LinkProperties(), vpnOwnerUid, vpnRange); assertVpnUidRangesUpdated(true, vpnRange, vpnOwnerUid); mMockVpn.setVpnType(vpnType); final UnderlyingNetworkInfo underlyingNetworkInfo = new UnderlyingNetworkInfo(vpnOwnerUid, VPN_IFNAME, new ArrayList<String>()); mMockVpn.setUnderlyingNetworkInfo(underlyingNetworkInfo); when(mDeps.getConnectionOwnerUid(anyInt(), any(), any())).thenReturn(42); } private void setupConnectionOwnerUidAsVpnApp(int vpnOwnerUid, @VpnManager.VpnType int vpnType) Loading Loading @@ -8410,8 +8411,7 @@ public class ConnectivityServiceTest { final int myUid = Process.myUid(); setupConnectionOwnerUidAsVpnApp(myUid, VpnManager.TYPE_VPN_SERVICE); // TODO: Test the returned UID mService.getConnectionOwnerUid(getTestConnectionInfo()); assertEquals(42, mService.getConnectionOwnerUid(getTestConnectionInfo())); } @Test Loading @@ -8421,8 +8421,7 @@ public class ConnectivityServiceTest { mServiceContext.setPermission( android.Manifest.permission.NETWORK_STACK, PERMISSION_GRANTED); // TODO: Test the returned UID mService.getConnectionOwnerUid(getTestConnectionInfo()); assertEquals(42, mService.getConnectionOwnerUid(getTestConnectionInfo())); } @Test Loading @@ -8433,8 +8432,7 @@ public class ConnectivityServiceTest { mServiceContext.setPermission( NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, PERMISSION_GRANTED); // TODO: Test the returned UID mService.getConnectionOwnerUid(getTestConnectionInfo()); assertEquals(42, mService.getConnectionOwnerUid(getTestConnectionInfo())); } private static PackageInfo buildPackageInfo(boolean hasSystemPermission, int uid) { Loading