From 59940afb16782d2cad2a7a1adbe653c2f5eb9812 Mon Sep 17 00:00:00 2001 From: Jeff Vander Stoep Date: Thu, 26 Sep 2019 13:56:50 +0200 Subject: Fix FD leak in ConnectivityManager.getConnectionOwnerUid Add unit tests to verify that bug has been fixed. Re-enable testGetConnectionOwnerUid() unit tests in presubmit. These were disabled due to test flakiness caused by expected failures passing as a result of other sockets on the system. This is fixed by checking that failures do not have the UID of the calling process instead of INVALID_UID since previously some Qualcomm telephony sockets were causing lookup successes. Test: atest InetDiagSocketTest#testGetConnectionOwnerUid Test: ls -1 /proc//fd | wca Test: atest --generate-new-metrics 200 InetDiagSocketTest#testGetConnectionOwnerUid To verify flakes have been cleaned up. Bug: 141603906 Bug: 141459241 Change-Id: Ib76674f10e4bd24952c557bac7b9c65fba42fdb2 --- .../java/android/net/netlink/InetDiagMessage.java | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'services/net/java') diff --git a/services/net/java/android/net/netlink/InetDiagMessage.java b/services/net/java/android/net/netlink/InetDiagMessage.java index af9e601da9ec..31a2556f2041 100644 --- a/services/net/java/android/net/netlink/InetDiagMessage.java +++ b/services/net/java/android/net/netlink/InetDiagMessage.java @@ -16,26 +16,23 @@ package android.net.netlink; -import static android.os.Process.INVALID_UID; import static android.net.netlink.NetlinkConstants.SOCK_DIAG_BY_FAMILY; import static android.net.netlink.NetlinkSocket.DEFAULT_RECV_BUFSIZE; import static android.net.netlink.StructNlMsgHdr.NLM_F_DUMP; import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST; +import static android.os.Process.INVALID_UID; import static android.system.OsConstants.AF_INET; import static android.system.OsConstants.AF_INET6; import static android.system.OsConstants.IPPROTO_UDP; import static android.system.OsConstants.NETLINK_INET_DIAG; -import android.os.Build; -import android.os.Process; +import android.net.util.SocketUtils; import android.system.ErrnoException; import android.util.Log; import java.io.FileDescriptor; +import java.io.IOException; import java.io.InterruptedIOException; -import java.net.DatagramSocket; -import java.net.DatagramSocket; -import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetSocketAddress; @@ -163,17 +160,25 @@ public class InetDiagMessage extends NetlinkMessage { */ public static int getConnectionOwnerUid(int protocol, InetSocketAddress local, InetSocketAddress remote) { + int uid = INVALID_UID; + FileDescriptor fd = null; try { - final FileDescriptor fd = NetlinkSocket.forProto(NETLINK_INET_DIAG); + fd = NetlinkSocket.forProto(NETLINK_INET_DIAG); NetlinkSocket.connectToKernel(fd); - - return lookupUid(protocol, local, remote, fd); - + uid = lookupUid(protocol, local, remote, fd); } catch (ErrnoException | SocketException | IllegalArgumentException | InterruptedIOException e) { Log.e(TAG, e.toString()); + } finally { + if (fd != null) { + try { + SocketUtils.closeSocket(fd); + } catch (IOException e) { + Log.e(TAG, e.toString()); + } + } } - return INVALID_UID; + return uid; } @Override -- cgit v1.2.3-59-g8ed1b