7

In my VPN application up until Android-9, it was possible to read the /proc/net/tcp files from apps targeting API level < 28. Access from apps no longer seems to work in Android Q. I'm getting an error /proc/net/tcp: open failed: EACCES (Permission denied) while trying to read the file.

In Android-Q privacy change google has addressed Restriction on access to /proc/net filesystem

And I think ConnectivityManager.getConnectionOwnerUid() can be used if the apps compileSDK version is 29. But unfortunately at the moment, I can't change my compileSDK version but I updated the targetSDK version to the latest IE, 29.

Any other possible way to read the file in Android-10? Posting my code for reference

public static final int INDEX_UID_COL = 7;
public static final int INDEX_LOCAL_ADDRESS_COL = 1;
public static final String PROC_FILE = "/proc/net/tcp";

 public static String getPackageName(Context context, int srcPort) {
        String packageName = "";
        try {

            BufferedReader br = new BufferedReader(new FileReader(PROC_FILE));

            //Ignore first line
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                /**
                 * Proc file table column sequence
                 * sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
                 */
                String[] parts = line.trim().split("\\s+");
                if (parts.length >= 8) {
                    String localAddress = parts[INDEX_LOCAL_ADDRESS_COL];
                    if (!localAddress.isEmpty()) {
                        String[] localAddressParts = localAddress.split(":");
                        if (localAddressParts.length == 2) {
                            int port = Integer.parseInt(localAddressParts[1], 16);
                            if (port == srcPort) {
                                int uid = Integer.parseInt(parts[INDEX_UID_COL]);
                                packageName = context.getPackageManager().getNameForUid(uid);
                                break;
                            }
                        }
                    }
                }
            }
            br.close();
        } catch (Exception ex) {
            Log.e("ProcFileParser", ex.getMessage());
        }
        return packageName;
    }
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
Stella
  • 1,817
  • 6
  • 30
  • 55
  • 3
    You can always change your `compileSdkVersion`. – ianhanniballake Oct 22 '19 at 05:25
  • @ianhanniballake We're about to rollout an update, while updating the compieSdkVersion all the test cases are failing since `AndroidTesCase` is removed in compileSKD 29. So we decided to fix critical issues and go live, then later migrate to new testing support library. So for the time being it's not possible to update compileSdk. – Stella Oct 22 '19 at 05:53
  • @ianhanniballake Is there any other way I can tackle this issue? Thanks for your support. – Stella Oct 22 '19 at 06:09

1 Answers1

5

ConnectivityManager is a platform level API. You can try java reflection in order to access the getConnectionOwnerUid in your context. But I suggest you to update compileSdk version to the latest.

import static android.os.Process.INVALID_UID;
import static android.system.OsConstants.IPPROTO_TCP;

InetSocketAddress remoteInetSocketAddress = new InetSocketAddress(finalHost, srcPort);
InetSocketAddress localInetSocketAddress = new InetSocketAddress(1234);

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Method method = ConnectivityManager.class.getMethod("getConnectionOwnerUid", int.class, InetSocketAddress.class, InetSocketAddress.class);
int uid = (int) method.invoke(connectivityManager, IPPROTO_TCP, localInetSocketAddress, remoteInetSocketAddress);
if (uid != INVALID_UID) {
       // UID access here         
  }

NOTE: The method only allows VPN apps to lookup the UID owner of a network connection. That means only connections for UIDs that apply to the calling VPN app will be resolved.

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73