Catch SecurityExceptions from all ConnectivityManager calls.

This issue occurs on Android 11 and no fix is expected. When the issue occurs, Tor connectivity and outgoing LAN connectivity will be broken until the app is restarted.
This commit is contained in:
akwizgran
2022-04-19 11:03:08 +01:00
parent 174ca3cfb8
commit e7fc37d81e
3 changed files with 101 additions and 71 deletions

View File

@@ -111,6 +111,8 @@ class AndroidNetworkManager implements NetworkManager, Service {
@Override @Override
public NetworkStatus getNetworkStatus() { public NetworkStatus getNetworkStatus() {
// https://issuetracker.google.com/issues/175055271
try {
NetworkInfo net = connectivityManager.getActiveNetworkInfo(); NetworkInfo net = connectivityManager.getActiveNetworkInfo();
boolean connected = net != null && net.isConnected(); boolean connected = net != null && net.isConnected();
boolean wifi = false, ipv6Only = false; boolean wifi = false, ipv6Only = false;
@@ -120,6 +122,10 @@ class AndroidNetworkManager implements NetworkManager, Service {
else ipv6Only = areAllAvailableNetworksIpv6Only(); else ipv6Only = areAllAvailableNetworksIpv6Only();
} }
return new NetworkStatus(connected, wifi, ipv6Only); return new NetworkStatus(connected, wifi, ipv6Only);
} catch (SecurityException e) {
logException(LOG, WARNING, e);
return new NetworkStatus(false, false, false);
}
} }
/** /**
@@ -130,6 +136,8 @@ class AndroidNetworkManager implements NetworkManager, Service {
*/ */
@TargetApi(23) @TargetApi(23)
private boolean isActiveNetworkIpv6Only() { private boolean isActiveNetworkIpv6Only() {
// https://issuetracker.google.com/issues/175055271
try {
Network net = connectivityManager.getActiveNetwork(); Network net = connectivityManager.getActiveNetwork();
if (net == null) { if (net == null) {
LOG.info("No active network"); LOG.info("No active network");
@@ -147,6 +155,10 @@ class AndroidNetworkManager implements NetworkManager, Service {
if (!addr.isMulticastAddress()) hasIpv6Unicast = true; if (!addr.isMulticastAddress()) hasIpv6Unicast = true;
} }
return hasIpv6Unicast; return hasIpv6Unicast;
} catch (SecurityException e) {
logException(LOG, WARNING, e);
return false;
}
} }
/** /**

View File

@@ -175,17 +175,25 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
@TargetApi(21) @TargetApi(21)
@Nullable @Nullable
private InetAddress getWifiClientIpv6Address() { private InetAddress getWifiClientIpv6Address() {
// https://issuetracker.google.com/issues/175055271
try {
for (Network net : connectivityManager.getAllNetworks()) { for (Network net : connectivityManager.getAllNetworks()) {
NetworkCapabilities caps = NetworkCapabilities caps =
connectivityManager.getNetworkCapabilities(net); connectivityManager.getNetworkCapabilities(net);
if (caps == null || !caps.hasTransport(TRANSPORT_WIFI)) continue; if (caps == null || !caps.hasTransport(TRANSPORT_WIFI)) {
LinkProperties props = connectivityManager.getLinkProperties(net); continue;
}
LinkProperties props =
connectivityManager.getLinkProperties(net);
if (props == null) continue; if (props == null) continue;
for (LinkAddress linkAddress : props.getLinkAddresses()) { for (LinkAddress linkAddress : props.getLinkAddresses()) {
InetAddress addr = linkAddress.getAddress(); InetAddress addr = linkAddress.getAddress();
if (isIpv6LinkLocalAddress(addr)) return addr; if (isIpv6LinkLocalAddress(addr)) return addr;
} }
} }
} catch (SecurityException e) {
logException(LOG, WARNING, e);
}
return null; return null;
} }
@@ -227,6 +235,8 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
// network's socket factory may try to connect via another network // network's socket factory may try to connect via another network
private SocketFactory getSocketFactory() { private SocketFactory getSocketFactory() {
if (SDK_INT < 21) return SocketFactory.getDefault(); if (SDK_INT < 21) return SocketFactory.getDefault();
// https://issuetracker.google.com/issues/175055271
try {
for (Network net : connectivityManager.getAllNetworks()) { for (Network net : connectivityManager.getAllNetworks()) {
NetworkCapabilities caps = NetworkCapabilities caps =
connectivityManager.getNetworkCapabilities(net); connectivityManager.getNetworkCapabilities(net);
@@ -234,6 +244,9 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
return net.getSocketFactory(); return net.getSocketFactory();
} }
} }
} catch (SecurityException e) {
logException(LOG, WARNING, e);
}
LOG.warning("Could not find suitable socket factory"); LOG.warning("Could not find suitable socket factory");
return SocketFactory.getDefault(); return SocketFactory.getDefault();
} }

View File

@@ -198,6 +198,8 @@ class BriarReportCollector {
private ReportItem getConnectivity() { private ReportItem getConnectivity() {
MultiReportInfo connectivityInfo = new MultiReportInfo(); MultiReportInfo connectivityInfo = new MultiReportInfo();
// https://issuetracker.google.com/issues/175055271
try {
// Is mobile data available? // Is mobile data available?
ConnectivityManager cm = requireNonNull( ConnectivityManager cm = requireNonNull(
getSystemService(ctx, ConnectivityManager.class)); getSystemService(ctx, ConnectivityManager.class));
@@ -231,16 +233,19 @@ class BriarReportCollector {
boolean wifiAvailable = wifi != null && wifi.isAvailable(); boolean wifiAvailable = wifi != null && wifi.isAvailable();
connectivityInfo.add("WifiAvailable", wifiAvailable); connectivityInfo.add("WifiAvailable", wifiAvailable);
// Is wifi connected?
boolean wifiConnected = wifi != null && wifi.isConnected();
connectivityInfo.add("WifiConnected", wifiConnected);
} catch (SecurityException e) {
connectivityInfo.add("ConnectivityManagerException", e.toString());
}
// Is wifi enabled? // Is wifi enabled?
WifiManager wm = getSystemService(ctx, WifiManager.class); WifiManager wm = getSystemService(ctx, WifiManager.class);
boolean wifiEnabled = wm != null && boolean wifiEnabled = wm != null &&
wm.getWifiState() == WIFI_STATE_ENABLED; wm.getWifiState() == WIFI_STATE_ENABLED;
connectivityInfo.add("WifiEnabled", wifiEnabled); connectivityInfo.add("WifiEnabled", wifiEnabled);
// Is wifi connected?
boolean wifiConnected = wifi != null && wifi.isConnected();
connectivityInfo.add("WifiConnected", wifiConnected);
// Is wifi direct supported? // Is wifi direct supported?
boolean wifiDirect = ctx.getSystemService(WIFI_P2P_SERVICE) != null; boolean wifiDirect = ctx.getSystemService(WIFI_P2P_SERVICE) != null;
connectivityInfo.add("WiFiDirectSupported", wifiDirect); connectivityInfo.add("WiFiDirectSupported", wifiDirect);