• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

frameworks/base


Commit MetaInfo

Revision2de275c9a3b0f472f617089f1f817b09f6dad493 (tree)
Time2015-09-29 08:29:49
AuthorPaul Jensen <pauljensen@goog...>
CommiterThe Android Automerger

Log Message

Fix NOT_RESTRICTED network capability and enforce it.

With this change:
1. NOT_RESTRICTED should be removed from NetworkRequests that bring up

special restricted carrier networks (e.g. IMS, FOTA).

2. NetworkRequests without NOT_RESTRICTED require CONNECTIVITY_INTERNAL

permission to register

3. Binding sockets to networks without NOT_RESTRICTED requires

CONNECTIVITY_INTERNAL permission

Bug:21637535
Change-Id: I5991d39facaa6b690e969fe15dcbeec52e918321
(cherry picked from commit 487ffe7d3d84bf65212158f7098e8a84b5b55e09)

Change Summary

Incremental Difference

--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -939,41 +939,6 @@ public class ConnectivityManager {
939939 return 1;
940940 }
941941
942- /**
943- * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
944- * NetworkCapabilities object if all the capabilities it provides are
945- * typically provided by restricted networks.
946- *
947- * TODO: consider:
948- * - Moving to NetworkCapabilities
949- * - Renaming it to guessRestrictedCapability and make it set the
950- * restricted capability bit in addition to clearing it.
951- * @hide
952- */
953- public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
954- for (int capability : nc.getCapabilities()) {
955- switch (capability) {
956- case NetworkCapabilities.NET_CAPABILITY_CBS:
957- case NetworkCapabilities.NET_CAPABILITY_DUN:
958- case NetworkCapabilities.NET_CAPABILITY_EIMS:
959- case NetworkCapabilities.NET_CAPABILITY_FOTA:
960- case NetworkCapabilities.NET_CAPABILITY_IA:
961- case NetworkCapabilities.NET_CAPABILITY_IMS:
962- case NetworkCapabilities.NET_CAPABILITY_RCS:
963- case NetworkCapabilities.NET_CAPABILITY_XCAP:
964- case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
965- continue;
966- default:
967- // At least one capability usually provided by unrestricted
968- // networks. Conclude that this network is unrestricted.
969- return;
970- }
971- }
972- // All the capabilities are typically provided by restricted networks.
973- // Conclude that this network is restricted.
974- nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
975- }
976-
977942 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
978943 if (networkType == TYPE_MOBILE) {
979944 int cap = -1;
@@ -996,14 +961,14 @@ public class ConnectivityManager {
996961 }
997962 NetworkCapabilities netCap = new NetworkCapabilities();
998963 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
999- maybeMarkCapabilitiesRestricted(netCap);
964+ netCap.maybeMarkCapabilitiesRestricted();
1000965 return netCap;
1001966 } else if (networkType == TYPE_WIFI) {
1002967 if ("p2p".equals(feature)) {
1003968 NetworkCapabilities netCap = new NetworkCapabilities();
1004969 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
1005970 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1006- maybeMarkCapabilitiesRestricted(netCap);
971+ netCap.maybeMarkCapabilitiesRestricted();
1007972 return netCap;
1008973 }
1009974 }
--- a/core/java/android/net/NetworkCapabilities.java
+++ b/core/java/android/net/NetworkCapabilities.java
@@ -37,6 +37,7 @@ public final class NetworkCapabilities implements Parcelable {
3737 * @hide
3838 */
3939 public NetworkCapabilities() {
40+ mNetworkCapabilities = DEFAULT_CAPABILITIES;
4041 }
4142
4243 public NetworkCapabilities(NetworkCapabilities nc) {
@@ -53,8 +54,7 @@ public final class NetworkCapabilities implements Parcelable {
5354 * Represents the network's capabilities. If any are specified they will be satisfied
5455 * by any Network that matches all of them.
5556 */
56- private long mNetworkCapabilities = (1 << NET_CAPABILITY_NOT_RESTRICTED) |
57- (1 << NET_CAPABILITY_TRUSTED) | (1 << NET_CAPABILITY_NOT_VPN);
57+ private long mNetworkCapabilities;
5858
5959 /**
6060 * Indicates this is a network that has the ability to reach the
@@ -166,6 +166,28 @@ public final class NetworkCapabilities implements Parcelable {
166166 private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_VALIDATED;
167167
168168 /**
169+ * Capabilities that are set by default when the object is constructed.
170+ */
171+ private static final long DEFAULT_CAPABILITIES =
172+ (1 << NET_CAPABILITY_NOT_RESTRICTED) |
173+ (1 << NET_CAPABILITY_TRUSTED) |
174+ (1 << NET_CAPABILITY_NOT_VPN);
175+
176+ /**
177+ * Capabilities that suggest that a network is restricted.
178+ * {@see #maybeMarkCapabilitiesRestricted}.
179+ */
180+ private static final long RESTRICTED_CAPABILITIES =
181+ (1 << NET_CAPABILITY_CBS) |
182+ (1 << NET_CAPABILITY_DUN) |
183+ (1 << NET_CAPABILITY_EIMS) |
184+ (1 << NET_CAPABILITY_FOTA) |
185+ (1 << NET_CAPABILITY_IA) |
186+ (1 << NET_CAPABILITY_IMS) |
187+ (1 << NET_CAPABILITY_RCS) |
188+ (1 << NET_CAPABILITY_XCAP);
189+
190+ /**
169191 * Adds the given capability to this {@code NetworkCapability} instance.
170192 * Multiple capabilities may be applied sequentially. Note that when searching
171193 * for a network to satisfy a request, all capabilities requested must be satisfied.
@@ -248,6 +270,22 @@ public final class NetworkCapabilities implements Parcelable {
248270 }
249271
250272 /**
273+ * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
274+ * typically provided by restricted networks.
275+ *
276+ * TODO: consider:
277+ * - Renaming it to guessRestrictedCapability and make it set the
278+ * restricted capability bit in addition to clearing it.
279+ * @hide
280+ */
281+ public void maybeMarkCapabilitiesRestricted() {
282+ // If all the capabilities are typically provided by restricted networks, conclude that this
283+ // network is restricted.
284+ if ((mNetworkCapabilities & ~(DEFAULT_CAPABILITIES | RESTRICTED_CAPABILITIES)) == 0)
285+ removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
286+ }
287+
288+ /**
251289 * Representing the transport type. Apps should generally not care about transport. A
252290 * request for a fast internet connection could be satisfied by a number of different
253291 * transports. If any are specified here it will be satisfied a Network that matches
--- a/core/java/android/net/NetworkRequest.java
+++ b/core/java/android/net/NetworkRequest.java
@@ -85,7 +85,13 @@ public class NetworkRequest implements Parcelable {
8585 * Build {@link NetworkRequest} give the current set of capabilities.
8686 */
8787 public NetworkRequest build() {
88- return new NetworkRequest(mNetworkCapabilities, ConnectivityManager.TYPE_NONE,
88+ // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED
89+ // when later an unrestricted capability could be added to mNetworkCapabilities, in
90+ // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which
91+ // maybeMarkCapabilitiesRestricted() doesn't add back.
92+ final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities);
93+ nc.maybeMarkCapabilitiesRestricted();
94+ return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE,
8995 ConnectivityManager.REQUEST_ID_UNSET);
9096 }
9197
--- a/core/java/android/os/INetworkManagementService.aidl
+++ b/core/java/android/os/INetworkManagementService.aidl
@@ -372,8 +372,10 @@ interface INetworkManagementService
372372
373373 /**
374374 * Setup a new physical network.
375+ * @param permission null if no permissions required to access this network. PERMISSION_NETWORK
376+ * or PERMISSION_SYSTEM to set respective permission.
375377 */
376- void createPhysicalNetwork(int netId);
378+ void createPhysicalNetwork(int netId, String permission);
377379
378380 /**
379381 * Setup a new VPN.
@@ -400,6 +402,13 @@ interface INetworkManagementService
400402 void setDefaultNetId(int netId);
401403 void clearDefaultNetId();
402404
405+ /**
406+ * Set permission for a network.
407+ * @param permission null to clear permissions. PERMISSION_NETWORK or PERMISSION_SYSTEM to set
408+ * permission.
409+ */
410+ void setNetworkPermission(int netId, String permission);
411+
403412 void setPermission(String permission, in int[] uids);
404413 void clearPermission(in int[] uids);
405414
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -24,6 +24,7 @@ import static android.net.ConnectivityManager.TYPE_NONE;
2424 import static android.net.ConnectivityManager.TYPE_VPN;
2525 import static android.net.ConnectivityManager.getNetworkTypeName;
2626 import static android.net.ConnectivityManager.isNetworkTypeValid;
27+import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
2728 import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
2829 import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
2930
@@ -3900,6 +3901,16 @@ public class ConnectivityService extends IConnectivityManager.Stub
39003901 private void updateCapabilities(NetworkAgentInfo networkAgent,
39013902 NetworkCapabilities networkCapabilities) {
39023903 if (!Objects.equals(networkAgent.networkCapabilities, networkCapabilities)) {
3904+ if (networkAgent.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) !=
3905+ networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) {
3906+ try {
3907+ mNetd.setNetworkPermission(networkAgent.network.netId,
3908+ networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) ?
3909+ null : NetworkManagementService.PERMISSION_SYSTEM);
3910+ } catch (RemoteException e) {
3911+ loge("Exception in setNetworkPermission: " + e);
3912+ }
3913+ }
39033914 synchronized (networkAgent) {
39043915 networkAgent.networkCapabilities = networkCapabilities;
39053916 }
@@ -4329,7 +4340,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
43294340 (networkAgent.networkMisc == null ||
43304341 !networkAgent.networkMisc.allowBypass));
43314342 } else {
4332- mNetd.createPhysicalNetwork(networkAgent.network.netId);
4343+ mNetd.createPhysicalNetwork(networkAgent.network.netId,
4344+ networkAgent.networkCapabilities.hasCapability(
4345+ NET_CAPABILITY_NOT_RESTRICTED) ?
4346+ null : NetworkManagementService.PERMISSION_SYSTEM);
43334347 }
43344348 } catch (Exception e) {
43354349 loge("Error creating network " + networkAgent.network.netId + ": "
--- a/services/core/java/com/android/server/NetworkManagementService.java
+++ b/services/core/java/com/android/server/NetworkManagementService.java
@@ -120,6 +120,19 @@ public class NetworkManagementService extends INetworkManagementService.Stub
120120 */
121121 public static final String LIMIT_GLOBAL_ALERT = "globalAlert";
122122
123+ /**
124+ * String to pass to netd to indicate that a network is only accessible
125+ * to apps that have the CHANGE_NETWORK_STATE permission.
126+ */
127+ public static final String PERMISSION_NETWORK = "NETWORK";
128+
129+ /**
130+ * String to pass to netd to indicate that a network is only
131+ * accessible to system apps and those with the CONNECTIVITY_INTERNAL
132+ * permission.
133+ */
134+ public static final String PERMISSION_SYSTEM = "SYSTEM";
135+
123136 class NetdResponseCode {
124137 /* Keep in sync with system/netd/server/ResponseCode.h */
125138 public static final int InterfaceListResult = 110;
@@ -1977,11 +1990,15 @@ public class NetworkManagementService extends INetworkManagementService.Stub
19771990 }
19781991
19791992 @Override
1980- public void createPhysicalNetwork(int netId) {
1993+ public void createPhysicalNetwork(int netId, String permission) {
19811994 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
19821995
19831996 try {
1984- mConnector.execute("network", "create", netId);
1997+ if (permission != null) {
1998+ mConnector.execute("network", "create", netId, permission);
1999+ } else {
2000+ mConnector.execute("network", "create", netId);
2001+ }
19852002 } catch (NativeDaemonConnectorException e) {
19862003 throw e.rethrowAsParcelableException();
19872004 }
@@ -2073,6 +2090,22 @@ public class NetworkManagementService extends INetworkManagementService.Stub
20732090 }
20742091
20752092 @Override
2093+ public void setNetworkPermission(int netId, String permission) {
2094+ mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
2095+
2096+ try {
2097+ if (permission != null) {
2098+ mConnector.execute("network", "permission", "network", "set", permission, netId);
2099+ } else {
2100+ mConnector.execute("network", "permission", "network", "clear", netId);
2101+ }
2102+ } catch (NativeDaemonConnectorException e) {
2103+ throw e.rethrowAsParcelableException();
2104+ }
2105+ }
2106+
2107+
2108+ @Override
20762109 public void setPermission(String permission, int[] uids) {
20772110 mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
20782111