Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 50e7cdb2 authored by mengsun's avatar mengsun Committed by Steve Kondik
Browse files

NetworkPolicy: Validate policy templates before updating

Ensure all the policies to be added are validated, otherwise
this service will throw IllegalArgumentException and cause
system crash when updating network template after receiving
ACTION_NETWORK_STATS_UPDATED.

Change-Id: I3faef0ac2a2fcea64ba5dde9c9256f6714280e2e
CRs-Fixed: 561490
parent b69d07e6
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -1435,6 +1435,12 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    public void setNetworkPolicies(NetworkPolicy[] policies) {
        mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);

        // Before clear and refresh mNetworkPolicy, we need to ensure all the
        // policies to be added are validated, otherwise this service will throw
        // IllegalArgumentException and cause system crash when updating network
        // template after receiving ACTION_NETWORK_STATS_UPDATED.
        validatePoliciesToSet(policies);

        maybeRefreshTrustedTime();
        synchronized (mRulesLock) {
            mNetworkPolicy.clear();
@@ -1449,6 +1455,27 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        }
    }

    /**
     * ensure the policies have valid template
     *
     * @param policies
     */
    private void validatePoliciesToSet(NetworkPolicy[] policies) {
        for (NetworkPolicy policy : policies) {
            switch (policy.template.getMatchRule()) {
                case MATCH_MOBILE_3G_LOWER:
                case MATCH_MOBILE_4G:
                case MATCH_MOBILE_ALL:
                case MATCH_WIFI:
                case MATCH_ETHERNET:
                    break;
                default:
                    throw new IllegalArgumentException("unexpected template "
                            + policy.template.getMatchRule());
            }
        }
    }

    private void addNetworkPolicyLocked(NetworkPolicy policy) {
        mNetworkPolicy.put(policy.template, policy);