Disable sockets and DNS if process lacks INTERNET permission.

This is a Client-only solution.
  - Add to NetdClient a per-process std::atomic_boolean
    similar to netIdForProcess and netIdForResolv.
  - The boolean says whether the process should be
    allowed Internet connectivity.
  - Add an @hide method to NetUtils.java to set the boolean;
    call it from the initialization code of the new
    process just after forking from zygote.
  - Make netdClientSocket and dnsOpenProxy check the
    boolean. If the boolean is false, return EPERM from
    socket calls.

Bug: 150028556
Test: atest NetworkUtilsTest
Test: atest CtsAppSecurityHostTestCases:UseProcessTest
(cherry-pick from internal branch, ag/11881939)
Merged-In: If002280fbad493dfc2db3d9d505c0257d49a9056
Change-Id: If002280fbad493dfc2db3d9d505c0257d49a9056
This commit is contained in:
Luke Huang
2020-06-16 19:10:02 +08:00
parent f1140d1a9c
commit caaee1d0ec
3 changed files with 76 additions and 0 deletions

View File

@@ -16,10 +16,24 @@
package android.net;
import static android.system.OsConstants.AF_INET;
import static android.system.OsConstants.AF_INET6;
import static android.system.OsConstants.AF_UNIX;
import static android.system.OsConstants.EPERM;
import static android.system.OsConstants.SOCK_DGRAM;
import static android.system.OsConstants.SOCK_STREAM;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.system.ErrnoException;
import android.system.Os;
import androidx.test.runner.AndroidJUnit4;
import libcore.io.IoUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -125,4 +139,50 @@ public class NetworkUtilsTest {
assertEquals(BigInteger.valueOf(7l - 4 + 4 + 16 + 65536),
NetworkUtils.routedIPv6AddressCount(set));
}
private static void expectSocketSuccess(String msg, int domain, int type) {
try {
IoUtils.closeQuietly(Os.socket(domain, type, 0));
} catch (ErrnoException e) {
fail(msg + e.getMessage());
}
}
private static void expectSocketPemissionError(String msg, int domain, int type) {
try {
IoUtils.closeQuietly(Os.socket(domain, type, 0));
fail(msg);
} catch (ErrnoException e) {
assertEquals(msg, e.errno, EPERM);
}
}
private static void expectHasNetworking() {
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
AF_UNIX, SOCK_STREAM);
expectSocketSuccess("Creating a AF_INET socket shouldn't have thrown ErrnoException",
AF_INET, SOCK_DGRAM);
expectSocketSuccess("Creating a AF_INET6 socket shouldn't have thrown ErrnoException",
AF_INET6, SOCK_DGRAM);
}
private static void expectNoNetworking() {
expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException",
AF_UNIX, SOCK_STREAM);
expectSocketPemissionError(
"Creating a AF_INET socket should have thrown ErrnoException(EPERM)",
AF_INET, SOCK_DGRAM);
expectSocketPemissionError(
"Creating a AF_INET6 socket should have thrown ErrnoException(EPERM)",
AF_INET6, SOCK_DGRAM);
}
@Test
public void testSetAllowNetworkingForProcess() {
expectHasNetworking();
NetworkUtils.setAllowNetworkingForProcess(false);
expectNoNetworking();
NetworkUtils.setAllowNetworkingForProcess(true);
expectHasNetworking();
}
}