Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

networking - Acccess to /proc/net/tcp in Android Q

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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...