Merge "Rename some Vpn fields."

am: d1c510f749

Change-Id: I4aac3d05aa5b879be67f1e3673e1d930e2160cb8
This commit is contained in:
Chalard Jean
2019-10-01 02:34:10 -07:00
committed by android-build-merger

View File

@@ -199,13 +199,22 @@ public class Vpn {
*/ */
private @NonNull List<String> mLockdownWhitelist = Collections.emptyList(); private @NonNull List<String> mLockdownWhitelist = Collections.emptyList();
/** /**
* List of UIDs for which networking should be blocked until VPN is ready, during brief periods * A memory of what UIDs this class told netd to block for the lockdown feature.
* when VPN is not running. For example, during system startup or after a crash. *
* Netd maintains ranges of UIDs for which network should be restricted to using only the VPN
* for the lockdown feature. This class manages these UIDs and sends this information to netd.
* To avoid sending the same commands multiple times (which would be wasteful) and to be able
* to revoke lists (when the rules should change), it's simplest to keep this cache of what
* netd knows, so it can be diffed and sent most efficiently.
*
* The contents of this list must only be changed when updating the UIDs lists with netd,
* since it needs to keep in sync with the picture netd has of them.
*
* @see mLockdown * @see mLockdown
*/ */
@GuardedBy("this") @GuardedBy("this")
private Set<UidRange> mBlockedUsers = new ArraySet<>(); private final Set<UidRange> mBlockedUidsAsToldToNetd = new ArraySet<>();
// Handle of the user initiating VPN. // Handle of the user initiating VPN.
private final int mUserHandle; private final int mUserHandle;
@@ -254,7 +263,7 @@ public class Vpn {
} }
/** /**
* Update current state, dispaching event to listeners. * Update current state, dispatching event to listeners.
*/ */
@VisibleForTesting @VisibleForTesting
protected void updateState(DetailedState detailedState, String reason) { protected void updateState(DetailedState detailedState, String reason) {
@@ -1325,7 +1334,7 @@ public class Vpn {
* {@link Vpn} goes through a VPN connection or is blocked until one is * {@link Vpn} goes through a VPN connection or is blocked until one is
* available, {@code false} to lift the requirement. * available, {@code false} to lift the requirement.
* *
* @see #mBlockedUsers * @see #mBlockedUidsAsToldToNetd
*/ */
@GuardedBy("this") @GuardedBy("this")
private void setVpnForcedLocked(boolean enforce) { private void setVpnForcedLocked(boolean enforce) {
@@ -1336,37 +1345,47 @@ public class Vpn {
exemptedPackages = new ArrayList<>(mLockdownWhitelist); exemptedPackages = new ArrayList<>(mLockdownWhitelist);
exemptedPackages.add(mPackage); exemptedPackages.add(mPackage);
} }
final Set<UidRange> removedRanges = new ArraySet<>(mBlockedUsers); final Set<UidRange> rangesToTellNetdToRemove = new ArraySet<>(mBlockedUidsAsToldToNetd);
Set<UidRange> addedRanges = Collections.emptySet(); final Set<UidRange> rangesToTellNetdToAdd;
if (enforce) { if (enforce) {
addedRanges = createUserAndRestrictedProfilesRanges(mUserHandle, final Set<UidRange> rangesThatShouldBeBlocked =
/* allowedApplications */ null, createUserAndRestrictedProfilesRanges(mUserHandle,
/* disallowedApplications */ exemptedPackages); /* allowedApplications */ null,
/* disallowedApplications */ exemptedPackages);
// The UID range of the first user (0-99999) would block the IPSec traffic, which comes // The UID range of the first user (0-99999) would block the IPSec traffic, which comes
// directly from the kernel and is marked as uid=0. So we adjust the range to allow // directly from the kernel and is marked as uid=0. So we adjust the range to allow
// it through (b/69873852). // it through (b/69873852).
for (UidRange range : addedRanges) { for (UidRange range : rangesThatShouldBeBlocked) {
if (range.start == 0) { if (range.start == 0) {
addedRanges.remove(range); rangesThatShouldBeBlocked.remove(range);
if (range.stop != 0) { if (range.stop != 0) {
addedRanges.add(new UidRange(1, range.stop)); rangesThatShouldBeBlocked.add(new UidRange(1, range.stop));
} }
} }
} }
removedRanges.removeAll(addedRanges); rangesToTellNetdToRemove.removeAll(rangesThatShouldBeBlocked);
addedRanges.removeAll(mBlockedUsers); rangesToTellNetdToAdd = rangesThatShouldBeBlocked;
// The ranges to tell netd to add are the ones that should be blocked minus the
// ones it already knows to block. Note that this will change the contents of
// rangesThatShouldBeBlocked, but the list of ranges that should be blocked is
// not used after this so it's fine to destroy it.
rangesToTellNetdToAdd.removeAll(mBlockedUidsAsToldToNetd);
} else {
rangesToTellNetdToAdd = Collections.emptySet();
} }
setAllowOnlyVpnForUids(false, removedRanges); // If mBlockedUidsAsToldToNetd used to be empty, this will always be a no-op.
setAllowOnlyVpnForUids(true, addedRanges); setAllowOnlyVpnForUids(false, rangesToTellNetdToRemove);
// If nothing should be blocked now, this will now be a no-op.
setAllowOnlyVpnForUids(true, rangesToTellNetdToAdd);
} }
/** /**
* Either add or remove a list of {@link UidRange}s to the list of UIDs that are only allowed * Tell netd to add or remove a list of {@link UidRange}s to the list of UIDs that are only
* to make connections through sockets that have had {@code protect()} called on them. * allowed to make connections through sockets that have had {@code protect()} called on them.
* *
* @param enforce {@code true} to add to the blacklist, {@code false} to remove. * @param enforce {@code true} to add to the blacklist, {@code false} to remove.
* @param ranges {@link Collection} of {@link UidRange}s to add (if {@param enforce} is * @param ranges {@link Collection} of {@link UidRange}s to add (if {@param enforce} is
@@ -1388,9 +1407,9 @@ public class Vpn {
return false; return false;
} }
if (enforce) { if (enforce) {
mBlockedUsers.addAll(ranges); mBlockedUidsAsToldToNetd.addAll(ranges);
} else { } else {
mBlockedUsers.removeAll(ranges); mBlockedUidsAsToldToNetd.removeAll(ranges);
} }
return true; return true;
} }
@@ -1557,17 +1576,18 @@ public class Vpn {
/** /**
* @param uid The target uid. * @param uid The target uid.
* *
* @return {@code true} if {@code uid} is included in one of the mBlockedUsers ranges and the * @return {@code true} if {@code uid} is included in one of the mBlockedUidsAsToldToNetd
* VPN is not connected, or if the VPN is connected but does not apply to the {@code uid}. * ranges and the VPN is not connected, or if the VPN is connected but does not apply to
* the {@code uid}.
* *
* @apiNote This method don't check VPN lockdown status. * @apiNote This method don't check VPN lockdown status.
* @see #mBlockedUsers * @see #mBlockedUidsAsToldToNetd
*/ */
public synchronized boolean isBlockingUid(int uid) { public synchronized boolean isBlockingUid(int uid) {
if (mNetworkInfo.isConnected()) { if (mNetworkInfo.isConnected()) {
return !appliesToUid(uid); return !appliesToUid(uid);
} else { } else {
return UidRange.containsUid(mBlockedUsers, uid); return UidRange.containsUid(mBlockedUidsAsToldToNetd, uid);
} }
} }