I'm in the process of converting a C application to be used on an Android device. The program is being written in a mix of Kotlin and C, using the Android NDK to support the JNI calls. The application is using raw sockets to send a custom packet between two devices. When I go to create the raw socket, the error I get is: Operation not Permitted.

Things to know:

  1. The android device has been rooted and is running lineageos 14.1 on it
  2. The application is given root access on startup through the following command:
Process p = Runtime.getRuntime().exec("su");
  1. Within the android manifest, Internet has been granted and I have been able to create a normal socket
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.NET_ADMIN" tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.NET_RAW" tools:ignore="ProtectedPermissions"/>
  1. The Android device is connected to my network

This is the raw socket being called in the application

s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);

if (s == -1) {
    ktprint("[open_sockraw] socket(): %s\n", strerror(errno)); //the error being returned is Operation Not Permitted.
    return -1;
}
  1. I've attempted to set the UID of the user in the C code and I get the "operation not permitted" error.

I expect that since I've given the program root access to the device, I should be able to create the raw socket. Is there anything else I could be missing?

EDIT: I've also looked at Raw Sockets on Android, but it doesn't answer this question. The top-rated answer there says that it's possible to create raw sockets with a rooted device using C/C++, and I have a rooted device and can build the C/C++ part of the project without issue. Also, see e.g. nmap for Android, which uses raw sockets on rooted devices.

Edit:: The UID when in the java application is root. The UID of the process within the c code just before attempting to create the socket is of a different user. I'm unable to set the uid of the process (setuid function) and I get the "Operation Not Permitted" error when I attempt to.

0 Answers