Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import static android.Manifest.permission.DUMP; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 20 | import static android.net.IpSecManager.INVALID_RESOURCE_ID; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 21 | import static android.system.OsConstants.AF_INET; |
manojboopathi | c4be79d | 2017-11-30 17:11:49 -0800 | [diff] [blame] | 22 | import static android.system.OsConstants.EINVAL; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 23 | import static android.system.OsConstants.IPPROTO_UDP; |
| 24 | import static android.system.OsConstants.SOCK_DGRAM; |
| 25 | import static com.android.internal.util.Preconditions.checkNotNull; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 26 | |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 27 | import android.annotation.NonNull; |
| 28 | import android.app.AppOpsManager; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.net.IIpSecService; |
| 31 | import android.net.INetd; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 32 | import android.net.IpSecAlgorithm; |
| 33 | import android.net.IpSecConfig; |
| 34 | import android.net.IpSecManager; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 35 | import android.net.IpSecSpiResponse; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 36 | import android.net.IpSecTransform; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 37 | import android.net.IpSecTransformResponse; |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 38 | import android.net.IpSecTunnelInterfaceResponse; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 39 | import android.net.IpSecUdpEncapResponse; |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 40 | import android.net.LinkAddress; |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 41 | import android.net.Network; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 42 | import android.net.NetworkUtils; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 43 | import android.net.TrafficStats; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 44 | import android.net.util.NetdService; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 45 | import android.os.Binder; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 46 | import android.os.IBinder; |
| 47 | import android.os.ParcelFileDescriptor; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 48 | import android.os.RemoteException; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 49 | import android.os.ServiceSpecificException; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 50 | import android.system.ErrnoException; |
| 51 | import android.system.Os; |
| 52 | import android.system.OsConstants; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 53 | import android.text.TextUtils; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 54 | import android.util.Log; |
| 55 | import android.util.Slog; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 56 | import android.util.SparseArray; |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 57 | import android.util.SparseBooleanArray; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 58 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 59 | import com.android.internal.annotations.GuardedBy; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 60 | import com.android.internal.annotations.VisibleForTesting; |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 61 | import com.android.internal.util.Preconditions; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 62 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 63 | import java.io.FileDescriptor; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 64 | import java.io.IOException; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 65 | import java.io.PrintWriter; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 66 | import java.net.InetAddress; |
| 67 | import java.net.InetSocketAddress; |
| 68 | import java.net.UnknownHostException; |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
| 70 | import java.util.List; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 71 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 72 | import libcore.io.IoUtils; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 73 | |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 74 | /** |
| 75 | * A service to manage multiple clients that want to access the IpSec API. The service is |
| 76 | * responsible for maintaining a list of clients and managing the resources (and related quotas) |
| 77 | * that each of them own. |
| 78 | * |
| 79 | * <p>Synchronization in IpSecService is done on all entrypoints due to potential race conditions at |
| 80 | * the kernel/xfrm level. Further, this allows the simplifying assumption to be made that only one |
| 81 | * thread is ever running at a time. |
| 82 | * |
| 83 | * @hide |
| 84 | */ |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 85 | public class IpSecService extends IIpSecService.Stub { |
| 86 | private static final String TAG = "IpSecService"; |
| 87 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 88 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 89 | private static final String NETD_SERVICE_NAME = "netd"; |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 90 | private static final int[] ADDRESS_FAMILIES = |
| 91 | new int[] {OsConstants.AF_INET, OsConstants.AF_INET6}; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 92 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 93 | private static final int NETD_FETCH_TIMEOUT_MS = 5000; // ms |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 94 | private static final int MAX_PORT_BIND_ATTEMPTS = 10; |
| 95 | private static final InetAddress INADDR_ANY; |
| 96 | |
| 97 | static { |
| 98 | try { |
| 99 | INADDR_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0}); |
| 100 | } catch (UnknownHostException e) { |
| 101 | throw new RuntimeException(e); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static final int FREE_PORT_MIN = 1024; // ports 1-1023 are reserved |
| 106 | static final int PORT_MAX = 0xFFFF; // ports are an unsigned 16-bit integer |
| 107 | |
| 108 | /* Binder context for this service */ |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 109 | private final Context mContext; |
| 110 | |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 111 | /** |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 112 | * The next non-repeating global ID for tracking resources between users, this service, and |
| 113 | * kernel data structures. Accessing this variable is not thread safe, so it is only read or |
| 114 | * modified within blocks synchronized on IpSecService.this. We want to avoid -1 |
| 115 | * (INVALID_RESOURCE_ID) and 0 (we probably forgot to initialize it). |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 116 | */ |
| 117 | @GuardedBy("IpSecService.this") |
| 118 | private int mNextResourceId = 1; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 119 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 120 | interface IpSecServiceConfiguration { |
| 121 | INetd getNetdInstance() throws RemoteException; |
| 122 | |
| 123 | static IpSecServiceConfiguration GETSRVINSTANCE = |
| 124 | new IpSecServiceConfiguration() { |
| 125 | @Override |
| 126 | public INetd getNetdInstance() throws RemoteException { |
| 127 | final INetd netd = NetdService.getInstance(); |
| 128 | if (netd == null) { |
| 129 | throw new RemoteException("Failed to Get Netd Instance"); |
| 130 | } |
| 131 | return netd; |
| 132 | } |
| 133 | }; |
| 134 | } |
| 135 | |
| 136 | private final IpSecServiceConfiguration mSrvConfig; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 137 | final UidFdTagger mUidFdTagger; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 138 | |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Interface for user-reference and kernel-resource cleanup. |
| 141 | * |
| 142 | * <p>This interface must be implemented for a resource to be reference counted. |
| 143 | */ |
| 144 | @VisibleForTesting |
| 145 | public interface IResource { |
| 146 | /** |
| 147 | * Invalidates a IResource object, ensuring it is invalid for the purposes of allocating new |
| 148 | * objects dependent on it. |
| 149 | * |
| 150 | * <p>Implementations of this method are expected to remove references to the IResource |
| 151 | * object from the IpSecService's tracking arrays. The removal from the arrays ensures that |
| 152 | * the resource is considered invalid for user access or allocation or use in other |
| 153 | * resources. |
| 154 | * |
| 155 | * <p>References to the IResource object may be held by other RefcountedResource objects, |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 156 | * and as such, the underlying resources and quota may not be cleaned up. |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 157 | */ |
| 158 | void invalidate() throws RemoteException; |
| 159 | |
| 160 | /** |
| 161 | * Releases underlying resources and related quotas. |
| 162 | * |
| 163 | * <p>Implementations of this method are expected to remove all system resources that are |
| 164 | * tracked by the IResource object. Due to other RefcountedResource objects potentially |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 165 | * having references to the IResource object, freeUnderlyingResources may not always be |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 166 | * called from releaseIfUnreferencedRecursively(). |
| 167 | */ |
| 168 | void freeUnderlyingResources() throws RemoteException; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * RefcountedResource manages references and dependencies in an exclusively acyclic graph. |
| 173 | * |
| 174 | * <p>RefcountedResource implements both explicit and implicit resource management. Creating a |
| 175 | * RefcountedResource object creates an explicit reference that must be freed by calling |
| 176 | * userRelease(). Additionally, adding this object as a child of another RefcountedResource |
| 177 | * object will add an implicit reference. |
| 178 | * |
| 179 | * <p>Resources are cleaned up when all references, both implicit and explicit, are released |
| 180 | * (ie, when userRelease() is called and when all parents have called releaseReference() on this |
| 181 | * object.) |
| 182 | */ |
| 183 | @VisibleForTesting |
| 184 | public class RefcountedResource<T extends IResource> implements IBinder.DeathRecipient { |
| 185 | private final T mResource; |
| 186 | private final List<RefcountedResource> mChildren; |
| 187 | int mRefCount = 1; // starts at 1 for user's reference. |
| 188 | IBinder mBinder; |
| 189 | |
| 190 | RefcountedResource(T resource, IBinder binder, RefcountedResource... children) { |
| 191 | synchronized (IpSecService.this) { |
| 192 | this.mResource = resource; |
| 193 | this.mChildren = new ArrayList<>(children.length); |
| 194 | this.mBinder = binder; |
| 195 | |
| 196 | for (RefcountedResource child : children) { |
| 197 | mChildren.add(child); |
| 198 | child.mRefCount++; |
| 199 | } |
| 200 | |
| 201 | try { |
| 202 | mBinder.linkToDeath(this, 0); |
| 203 | } catch (RemoteException e) { |
| 204 | binderDied(); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * If the Binder object dies, this function is called to free the system resources that are |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 211 | * being tracked by this record and to subsequently release this record for garbage |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 212 | * collection |
| 213 | */ |
| 214 | @Override |
| 215 | public void binderDied() { |
| 216 | synchronized (IpSecService.this) { |
| 217 | try { |
| 218 | userRelease(); |
| 219 | } catch (Exception e) { |
| 220 | Log.e(TAG, "Failed to release resource: " + e); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | public T getResource() { |
| 226 | return mResource; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Unlinks from binder and performs IpSecService resource cleanup (removes from resource |
| 231 | * arrays) |
| 232 | * |
| 233 | * <p>If this method has been previously called, the RefcountedResource's binder field will |
| 234 | * be null, and the method will return without performing the cleanup a second time. |
| 235 | * |
| 236 | * <p>Note that calling this function does not imply that kernel resources will be freed at |
| 237 | * this time, or that the related quota will be returned. Such actions will only be |
| 238 | * performed upon the reference count reaching zero. |
| 239 | */ |
| 240 | @GuardedBy("IpSecService.this") |
| 241 | public void userRelease() throws RemoteException { |
| 242 | // Prevent users from putting reference counts into a bad state by calling |
| 243 | // userRelease() multiple times. |
| 244 | if (mBinder == null) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | mBinder.unlinkToDeath(this, 0); |
| 249 | mBinder = null; |
| 250 | |
| 251 | mResource.invalidate(); |
| 252 | |
| 253 | releaseReference(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Removes a reference to this resource. If the resultant reference count is zero, the |
| 258 | * underlying resources are freed, and references to all child resources are also dropped |
| 259 | * recursively (resulting in them freeing their resources and children, etcetera) |
| 260 | * |
| 261 | * <p>This method also sets the reference count to an invalid value (-1) to signify that it |
| 262 | * has been fully released. Any subsequent calls to this method will result in an |
| 263 | * IllegalStateException being thrown due to resource already having been previously |
| 264 | * released |
| 265 | */ |
| 266 | @VisibleForTesting |
| 267 | @GuardedBy("IpSecService.this") |
| 268 | public void releaseReference() throws RemoteException { |
| 269 | mRefCount--; |
| 270 | |
| 271 | if (mRefCount > 0) { |
| 272 | return; |
| 273 | } else if (mRefCount < 0) { |
| 274 | throw new IllegalStateException( |
| 275 | "Invalid operation - resource has already been released."); |
| 276 | } |
| 277 | |
| 278 | // Cleanup own resources |
| 279 | mResource.freeUnderlyingResources(); |
| 280 | |
| 281 | // Cleanup child resources as needed |
| 282 | for (RefcountedResource<? extends IResource> child : mChildren) { |
| 283 | child.releaseReference(); |
| 284 | } |
| 285 | |
| 286 | // Enforce that resource cleanup can only be called once |
| 287 | // By decrementing the refcount (from 0 to -1), the next call will throw an |
| 288 | // IllegalStateException - it has already been released fully. |
| 289 | mRefCount--; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public String toString() { |
| 294 | return new StringBuilder() |
| 295 | .append("{mResource=") |
| 296 | .append(mResource) |
| 297 | .append(", mRefCount=") |
| 298 | .append(mRefCount) |
| 299 | .append(", mChildren=") |
| 300 | .append(mChildren) |
| 301 | .append("}") |
| 302 | .toString(); |
| 303 | } |
| 304 | } |
| 305 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 306 | /** |
| 307 | * Very simple counting class that looks much like a counting semaphore |
| 308 | * |
| 309 | * <p>This class is not thread-safe, and expects that that users of this class will ensure |
| 310 | * synchronization and thread safety by holding the IpSecService.this instance lock. |
| 311 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 312 | @VisibleForTesting |
| 313 | static class ResourceTracker { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 314 | private final int mMax; |
| 315 | int mCurrent; |
| 316 | |
| 317 | ResourceTracker(int max) { |
| 318 | mMax = max; |
| 319 | mCurrent = 0; |
| 320 | } |
| 321 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 322 | boolean isAvailable() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 323 | return (mCurrent < mMax); |
| 324 | } |
| 325 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 326 | void take() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 327 | if (!isAvailable()) { |
| 328 | Log.wtf(TAG, "Too many resources allocated!"); |
| 329 | } |
| 330 | mCurrent++; |
| 331 | } |
| 332 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 333 | void give() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 334 | if (mCurrent <= 0) { |
| 335 | Log.wtf(TAG, "We've released this resource too many times"); |
| 336 | } |
| 337 | mCurrent--; |
| 338 | } |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 339 | |
| 340 | @Override |
| 341 | public String toString() { |
| 342 | return new StringBuilder() |
| 343 | .append("{mCurrent=") |
| 344 | .append(mCurrent) |
| 345 | .append(", mMax=") |
| 346 | .append(mMax) |
| 347 | .append("}") |
| 348 | .toString(); |
| 349 | } |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 352 | @VisibleForTesting |
| 353 | static final class UserRecord { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 354 | /* Maximum number of each type of resource that a single UID may possess */ |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 355 | public static final int MAX_NUM_TUNNEL_INTERFACES = 2; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 356 | public static final int MAX_NUM_ENCAP_SOCKETS = 2; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 357 | public static final int MAX_NUM_TRANSFORMS = 4; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 358 | public static final int MAX_NUM_SPIS = 8; |
| 359 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 360 | /** |
| 361 | * Store each of the OwnedResource types in an (thinly wrapped) sparse array for indexing |
| 362 | * and explicit (user) reference management. |
| 363 | * |
| 364 | * <p>These are stored in separate arrays to improve debuggability and dump output clarity. |
| 365 | * |
| 366 | * <p>Resources are removed from this array when the user releases their explicit reference |
| 367 | * by calling one of the releaseResource() methods. |
| 368 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 369 | final RefcountedResourceArray<SpiRecord> mSpiRecords = |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 370 | new RefcountedResourceArray<>(SpiRecord.class.getSimpleName()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 371 | final RefcountedResourceArray<TransformRecord> mTransformRecords = |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 372 | new RefcountedResourceArray<>(TransformRecord.class.getSimpleName()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 373 | final RefcountedResourceArray<EncapSocketRecord> mEncapSocketRecords = |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 374 | new RefcountedResourceArray<>(EncapSocketRecord.class.getSimpleName()); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 375 | final RefcountedResourceArray<TunnelInterfaceRecord> mTunnelInterfaceRecords = |
| 376 | new RefcountedResourceArray<>(TunnelInterfaceRecord.class.getSimpleName()); |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * Trackers for quotas for each of the OwnedResource types. |
| 380 | * |
| 381 | * <p>These trackers are separate from the resource arrays, since they are incremented and |
| 382 | * decremented at different points in time. Specifically, quota is only returned upon final |
| 383 | * resource deallocation (after all explicit and implicit references are released). Note |
| 384 | * that it is possible that calls to releaseResource() will not return the used quota if |
| 385 | * there are other resources that depend on (are parents of) the resource being released. |
| 386 | */ |
| 387 | final ResourceTracker mSpiQuotaTracker = new ResourceTracker(MAX_NUM_SPIS); |
| 388 | final ResourceTracker mTransformQuotaTracker = new ResourceTracker(MAX_NUM_TRANSFORMS); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 389 | final ResourceTracker mSocketQuotaTracker = new ResourceTracker(MAX_NUM_ENCAP_SOCKETS); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 390 | final ResourceTracker mTunnelQuotaTracker = new ResourceTracker(MAX_NUM_TUNNEL_INTERFACES); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 391 | |
| 392 | void removeSpiRecord(int resourceId) { |
| 393 | mSpiRecords.remove(resourceId); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 396 | void removeTransformRecord(int resourceId) { |
| 397 | mTransformRecords.remove(resourceId); |
| 398 | } |
| 399 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 400 | void removeTunnelInterfaceRecord(int resourceId) { |
| 401 | mTunnelInterfaceRecords.remove(resourceId); |
| 402 | } |
| 403 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 404 | void removeEncapSocketRecord(int resourceId) { |
| 405 | mEncapSocketRecords.remove(resourceId); |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public String toString() { |
| 410 | return new StringBuilder() |
| 411 | .append("{mSpiQuotaTracker=") |
| 412 | .append(mSpiQuotaTracker) |
| 413 | .append(", mTransformQuotaTracker=") |
| 414 | .append(mTransformQuotaTracker) |
| 415 | .append(", mSocketQuotaTracker=") |
| 416 | .append(mSocketQuotaTracker) |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 417 | .append(", mTunnelQuotaTracker=") |
| 418 | .append(mTunnelQuotaTracker) |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 419 | .append(", mSpiRecords=") |
| 420 | .append(mSpiRecords) |
| 421 | .append(", mTransformRecords=") |
| 422 | .append(mTransformRecords) |
| 423 | .append(", mEncapSocketRecords=") |
| 424 | .append(mEncapSocketRecords) |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 425 | .append(", mTunnelInterfaceRecords=") |
| 426 | .append(mTunnelInterfaceRecords) |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 427 | .append("}") |
| 428 | .toString(); |
| 429 | } |
| 430 | } |
| 431 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 432 | /** |
| 433 | * This class is not thread-safe, and expects that that users of this class will ensure |
| 434 | * synchronization and thread safety by holding the IpSecService.this instance lock. |
| 435 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 436 | @VisibleForTesting |
| 437 | static final class UserResourceTracker { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 438 | private final SparseArray<UserRecord> mUserRecords = new SparseArray<>(); |
| 439 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 440 | /** Lazy-initialization/getter that populates or retrieves the UserRecord as needed */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 441 | public UserRecord getUserRecord(int uid) { |
| 442 | checkCallerUid(uid); |
| 443 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 444 | UserRecord r = mUserRecords.get(uid); |
| 445 | if (r == null) { |
| 446 | r = new UserRecord(); |
| 447 | mUserRecords.put(uid, r); |
| 448 | } |
| 449 | return r; |
| 450 | } |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 451 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 452 | /** Safety method; guards against access of other user's UserRecords */ |
| 453 | private void checkCallerUid(int uid) { |
| 454 | if (uid != Binder.getCallingUid() |
| 455 | && android.os.Process.SYSTEM_UID != Binder.getCallingUid()) { |
| 456 | throw new SecurityException("Attempted access of unowned resources"); |
| 457 | } |
| 458 | } |
| 459 | |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 460 | @Override |
| 461 | public String toString() { |
| 462 | return mUserRecords.toString(); |
| 463 | } |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 466 | @VisibleForTesting final UserResourceTracker mUserResourceTracker = new UserResourceTracker(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 467 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 468 | /** |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 469 | * The OwnedResourceRecord class provides a facility to cleanly and reliably track system |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 470 | * resources. It relies on a provided resourceId that should uniquely identify the kernel |
| 471 | * resource. To use this class, the user should implement the invalidate() and |
| 472 | * freeUnderlyingResources() methods that are responsible for cleaning up IpSecService resource |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 473 | * tracking arrays and kernel resources, respectively. |
| 474 | * |
| 475 | * <p>This class associates kernel resources with the UID that owns and controls them. |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 476 | */ |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 477 | private abstract class OwnedResourceRecord implements IResource { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 478 | final int pid; |
| 479 | final int uid; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 480 | protected final int mResourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 481 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 482 | OwnedResourceRecord(int resourceId) { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 483 | super(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 484 | if (resourceId == INVALID_RESOURCE_ID) { |
| 485 | throw new IllegalArgumentException("Resource ID must not be INVALID_RESOURCE_ID"); |
| 486 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 487 | mResourceId = resourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 488 | pid = Binder.getCallingPid(); |
| 489 | uid = Binder.getCallingUid(); |
| 490 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 491 | getResourceTracker().take(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 494 | @Override |
| 495 | public abstract void invalidate() throws RemoteException; |
| 496 | |
| 497 | /** Convenience method; retrieves the user resource record for the stored UID. */ |
| 498 | protected UserRecord getUserRecord() { |
| 499 | return mUserResourceTracker.getUserRecord(uid); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 502 | @Override |
| 503 | public abstract void freeUnderlyingResources() throws RemoteException; |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 504 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 505 | /** Get the resource tracker for this resource */ |
| 506 | protected abstract ResourceTracker getResourceTracker(); |
| 507 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 508 | @Override |
| 509 | public String toString() { |
| 510 | return new StringBuilder() |
| 511 | .append("{mResourceId=") |
| 512 | .append(mResourceId) |
| 513 | .append(", pid=") |
| 514 | .append(pid) |
| 515 | .append(", uid=") |
| 516 | .append(uid) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 517 | .append("}") |
| 518 | .toString(); |
| 519 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 520 | }; |
| 521 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 522 | /** |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 523 | * Thin wrapper over SparseArray to ensure resources exist, and simplify generic typing. |
| 524 | * |
| 525 | * <p>RefcountedResourceArray prevents null insertions, and throws an IllegalArgumentException |
| 526 | * if a key is not found during a retrieval process. |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 527 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 528 | static class RefcountedResourceArray<T extends IResource> { |
| 529 | SparseArray<RefcountedResource<T>> mArray = new SparseArray<>(); |
| 530 | private final String mTypeName; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 531 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 532 | public RefcountedResourceArray(String typeName) { |
| 533 | this.mTypeName = typeName; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 536 | /** |
| 537 | * Accessor method to get inner resource object. |
| 538 | * |
| 539 | * @throws IllegalArgumentException if no resource with provided key is found. |
| 540 | */ |
| 541 | T getResourceOrThrow(int key) { |
| 542 | return getRefcountedResourceOrThrow(key).getResource(); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Accessor method to get reference counting wrapper. |
| 547 | * |
| 548 | * @throws IllegalArgumentException if no resource with provided key is found. |
| 549 | */ |
| 550 | RefcountedResource<T> getRefcountedResourceOrThrow(int key) { |
| 551 | RefcountedResource<T> resource = mArray.get(key); |
| 552 | if (resource == null) { |
| 553 | throw new IllegalArgumentException( |
| 554 | String.format("No such %s found for given id: %d", mTypeName, key)); |
| 555 | } |
| 556 | |
| 557 | return resource; |
| 558 | } |
| 559 | |
| 560 | void put(int key, RefcountedResource<T> obj) { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 561 | checkNotNull(obj, "Null resources cannot be added"); |
| 562 | mArray.put(key, obj); |
| 563 | } |
| 564 | |
| 565 | void remove(int key) { |
| 566 | mArray.remove(key); |
| 567 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 568 | |
| 569 | @Override |
| 570 | public String toString() { |
| 571 | return mArray.toString(); |
| 572 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 575 | /** |
| 576 | * Tracks an SA in the kernel, and manages cleanup paths. Once a TransformRecord is |
| 577 | * created, the SpiRecord that originally tracked the SAs will reliquish the |
| 578 | * responsibility of freeing the underlying SA to this class via the mOwnedByTransform flag. |
| 579 | */ |
| 580 | private final class TransformRecord extends OwnedResourceRecord { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 581 | private final IpSecConfig mConfig; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 582 | private final SpiRecord mSpi; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 583 | private final EncapSocketRecord mSocket; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 584 | |
| 585 | TransformRecord( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 586 | int resourceId, IpSecConfig config, SpiRecord spi, EncapSocketRecord socket) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 587 | super(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 588 | mConfig = config; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 589 | mSpi = spi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 590 | mSocket = socket; |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 591 | |
| 592 | spi.setOwnedByTransform(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | public IpSecConfig getConfig() { |
| 596 | return mConfig; |
| 597 | } |
| 598 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 599 | public SpiRecord getSpiRecord() { |
| 600 | return mSpi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 603 | public EncapSocketRecord getSocketRecord() { |
| 604 | return mSocket; |
| 605 | } |
| 606 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 607 | /** always guarded by IpSecService#this */ |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 608 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 609 | public void freeUnderlyingResources() { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 610 | int spi = mSpi.getSpi(); |
| 611 | try { |
| 612 | mSrvConfig |
| 613 | .getNetdInstance() |
| 614 | .ipSecDeleteSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 615 | uid, |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 616 | mConfig.getSourceAddress(), |
| 617 | mConfig.getDestinationAddress(), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 618 | spi, |
| 619 | mConfig.getMarkValue(), |
| 620 | mConfig.getMarkMask()); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 621 | } catch (RemoteException | ServiceSpecificException e) { |
| 622 | Log.e(TAG, "Failed to delete SA with ID: " + mResourceId, e); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 623 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 624 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 625 | getResourceTracker().give(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 626 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 627 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 628 | @Override |
| 629 | public void invalidate() throws RemoteException { |
| 630 | getUserRecord().removeTransformRecord(mResourceId); |
| 631 | } |
| 632 | |
| 633 | @Override |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 634 | protected ResourceTracker getResourceTracker() { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 635 | return getUserRecord().mTransformQuotaTracker; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 636 | } |
| 637 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 638 | @Override |
| 639 | public String toString() { |
| 640 | StringBuilder strBuilder = new StringBuilder(); |
| 641 | strBuilder |
| 642 | .append("{super=") |
| 643 | .append(super.toString()) |
| 644 | .append(", mSocket=") |
| 645 | .append(mSocket) |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 646 | .append(", mSpi.mResourceId=") |
| 647 | .append(mSpi.mResourceId) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 648 | .append(", mConfig=") |
| 649 | .append(mConfig) |
| 650 | .append("}"); |
| 651 | return strBuilder.toString(); |
| 652 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 655 | /** |
| 656 | * Tracks a single SA in the kernel, and manages cleanup paths. Once used in a Transform, the |
| 657 | * responsibility for cleaning up underlying resources will be passed to the TransformRecord |
| 658 | * object |
| 659 | */ |
| 660 | private final class SpiRecord extends OwnedResourceRecord { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 661 | private final String mSourceAddress; |
| 662 | private final String mDestinationAddress; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 663 | private int mSpi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 664 | |
| 665 | private boolean mOwnedByTransform = false; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 666 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 667 | SpiRecord(int resourceId, String sourceAddress, String destinationAddress, int spi) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 668 | super(resourceId); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 669 | mSourceAddress = sourceAddress; |
| 670 | mDestinationAddress = destinationAddress; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 671 | mSpi = spi; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 674 | /** always guarded by IpSecService#this */ |
| 675 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 676 | public void freeUnderlyingResources() { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 677 | try { |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 678 | if (!mOwnedByTransform) { |
| 679 | mSrvConfig |
| 680 | .getNetdInstance() |
| 681 | .ipSecDeleteSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 682 | uid, mSourceAddress, mDestinationAddress, mSpi, 0, 0); |
Nathan Harold | 2279530 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 683 | } |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 684 | } catch (ServiceSpecificException | RemoteException e) { |
| 685 | Log.e(TAG, "Failed to delete SPI reservation with ID: " + mResourceId, e); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 686 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 687 | |
| 688 | mSpi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 689 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 690 | getResourceTracker().give(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 693 | public int getSpi() { |
| 694 | return mSpi; |
| 695 | } |
| 696 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 697 | public String getDestinationAddress() { |
| 698 | return mDestinationAddress; |
| 699 | } |
| 700 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 701 | public void setOwnedByTransform() { |
| 702 | if (mOwnedByTransform) { |
| 703 | // Programming error |
Andreas Gampe | d6d8e45 | 2017-07-11 10:25:09 -0700 | [diff] [blame] | 704 | throw new IllegalStateException("Cannot own an SPI twice!"); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | mOwnedByTransform = true; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 708 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 709 | |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 710 | public boolean getOwnedByTransform() { |
| 711 | return mOwnedByTransform; |
| 712 | } |
| 713 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 714 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 715 | public void invalidate() throws RemoteException { |
| 716 | getUserRecord().removeSpiRecord(mResourceId); |
| 717 | } |
| 718 | |
| 719 | @Override |
| 720 | protected ResourceTracker getResourceTracker() { |
| 721 | return getUserRecord().mSpiQuotaTracker; |
| 722 | } |
| 723 | |
| 724 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 725 | public String toString() { |
| 726 | StringBuilder strBuilder = new StringBuilder(); |
| 727 | strBuilder |
| 728 | .append("{super=") |
| 729 | .append(super.toString()) |
| 730 | .append(", mSpi=") |
| 731 | .append(mSpi) |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 732 | .append(", mSourceAddress=") |
| 733 | .append(mSourceAddress) |
| 734 | .append(", mDestinationAddress=") |
| 735 | .append(mDestinationAddress) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 736 | .append(", mOwnedByTransform=") |
| 737 | .append(mOwnedByTransform) |
| 738 | .append("}"); |
| 739 | return strBuilder.toString(); |
| 740 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 743 | // These values have been reserved in ConnectivityService |
| 744 | @VisibleForTesting static final int TUN_INTF_NETID_START = 0xFC00; |
| 745 | |
| 746 | @VisibleForTesting static final int TUN_INTF_NETID_RANGE = 0x0400; |
| 747 | |
| 748 | private final SparseBooleanArray mTunnelNetIds = new SparseBooleanArray(); |
| 749 | private int mNextTunnelNetIdIndex = 0; |
| 750 | |
| 751 | /** |
| 752 | * Reserves a netId within the range of netIds allocated for IPsec tunnel interfaces |
| 753 | * |
| 754 | * <p>This method should only be called from Binder threads. Do not call this from within the |
| 755 | * system server as it will crash the system on failure. |
| 756 | * |
| 757 | * @return an integer key within the netId range, if successful |
| 758 | * @throws IllegalStateException if unsuccessful (all netId are currently reserved) |
| 759 | */ |
| 760 | @VisibleForTesting |
| 761 | int reserveNetId() { |
| 762 | synchronized (mTunnelNetIds) { |
| 763 | for (int i = 0; i < TUN_INTF_NETID_RANGE; i++) { |
| 764 | int index = mNextTunnelNetIdIndex; |
| 765 | int netId = index + TUN_INTF_NETID_START; |
| 766 | if (++mNextTunnelNetIdIndex >= TUN_INTF_NETID_RANGE) mNextTunnelNetIdIndex = 0; |
| 767 | if (!mTunnelNetIds.get(netId)) { |
| 768 | mTunnelNetIds.put(netId, true); |
| 769 | return netId; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | throw new IllegalStateException("No free netIds to allocate"); |
| 774 | } |
| 775 | |
| 776 | @VisibleForTesting |
| 777 | void releaseNetId(int netId) { |
| 778 | synchronized (mTunnelNetIds) { |
| 779 | mTunnelNetIds.delete(netId); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | private final class TunnelInterfaceRecord extends OwnedResourceRecord { |
| 784 | private final String mInterfaceName; |
| 785 | private final Network mUnderlyingNetwork; |
| 786 | |
| 787 | // outer addresses |
| 788 | private final String mLocalAddress; |
| 789 | private final String mRemoteAddress; |
| 790 | |
| 791 | private final int mIkey; |
| 792 | private final int mOkey; |
| 793 | |
| 794 | TunnelInterfaceRecord( |
| 795 | int resourceId, |
| 796 | String interfaceName, |
| 797 | Network underlyingNetwork, |
| 798 | String localAddr, |
| 799 | String remoteAddr, |
| 800 | int ikey, |
| 801 | int okey) { |
| 802 | super(resourceId); |
| 803 | |
| 804 | mInterfaceName = interfaceName; |
| 805 | mUnderlyingNetwork = underlyingNetwork; |
| 806 | mLocalAddress = localAddr; |
| 807 | mRemoteAddress = remoteAddr; |
| 808 | mIkey = ikey; |
| 809 | mOkey = okey; |
| 810 | } |
| 811 | |
| 812 | /** always guarded by IpSecService#this */ |
| 813 | @Override |
| 814 | public void freeUnderlyingResources() { |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 815 | // Calls to netd |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 816 | // Teardown VTI |
| 817 | // Delete global policies |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 818 | try { |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 819 | final INetd netd = mSrvConfig.getNetdInstance(); |
| 820 | netd.removeVirtualTunnelInterface(mInterfaceName); |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 821 | |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 822 | for (int selAddrFamily : ADDRESS_FAMILIES) { |
| 823 | netd.ipSecDeleteSecurityPolicy( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 824 | uid, |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 825 | selAddrFamily, |
| 826 | IpSecManager.DIRECTION_OUT, |
| 827 | mOkey, |
| 828 | 0xffffffff); |
| 829 | netd.ipSecDeleteSecurityPolicy( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 830 | uid, |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 831 | selAddrFamily, |
| 832 | IpSecManager.DIRECTION_IN, |
| 833 | mIkey, |
| 834 | 0xffffffff); |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 835 | } |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 836 | } catch (ServiceSpecificException | RemoteException e) { |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 837 | Log.e( |
| 838 | TAG, |
| 839 | "Failed to delete VTI with interface name: " |
| 840 | + mInterfaceName |
| 841 | + " and id: " |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 842 | + mResourceId, e); |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 843 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 844 | |
| 845 | getResourceTracker().give(); |
| 846 | releaseNetId(mIkey); |
| 847 | releaseNetId(mOkey); |
| 848 | } |
| 849 | |
| 850 | public String getInterfaceName() { |
| 851 | return mInterfaceName; |
| 852 | } |
| 853 | |
| 854 | public Network getUnderlyingNetwork() { |
| 855 | return mUnderlyingNetwork; |
| 856 | } |
| 857 | |
| 858 | /** Returns the local, outer address for the tunnelInterface */ |
| 859 | public String getLocalAddress() { |
| 860 | return mLocalAddress; |
| 861 | } |
| 862 | |
| 863 | /** Returns the remote, outer address for the tunnelInterface */ |
| 864 | public String getRemoteAddress() { |
| 865 | return mRemoteAddress; |
| 866 | } |
| 867 | |
| 868 | public int getIkey() { |
| 869 | return mIkey; |
| 870 | } |
| 871 | |
| 872 | public int getOkey() { |
| 873 | return mOkey; |
| 874 | } |
| 875 | |
| 876 | @Override |
| 877 | protected ResourceTracker getResourceTracker() { |
| 878 | return getUserRecord().mTunnelQuotaTracker; |
| 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public void invalidate() { |
| 883 | getUserRecord().removeTunnelInterfaceRecord(mResourceId); |
| 884 | } |
| 885 | |
| 886 | @Override |
| 887 | public String toString() { |
| 888 | return new StringBuilder() |
| 889 | .append("{super=") |
| 890 | .append(super.toString()) |
| 891 | .append(", mInterfaceName=") |
| 892 | .append(mInterfaceName) |
| 893 | .append(", mUnderlyingNetwork=") |
| 894 | .append(mUnderlyingNetwork) |
| 895 | .append(", mLocalAddress=") |
| 896 | .append(mLocalAddress) |
| 897 | .append(", mRemoteAddress=") |
| 898 | .append(mRemoteAddress) |
| 899 | .append(", mIkey=") |
| 900 | .append(mIkey) |
| 901 | .append(", mOkey=") |
| 902 | .append(mOkey) |
| 903 | .append("}") |
| 904 | .toString(); |
| 905 | } |
| 906 | } |
| 907 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 908 | /** |
| 909 | * Tracks a UDP encap socket, and manages cleanup paths |
| 910 | * |
| 911 | * <p>While this class does not manage non-kernel resources, race conditions around socket |
| 912 | * binding require that the service creates the encap socket, binds it and applies the socket |
| 913 | * policy before handing it to a user. |
| 914 | */ |
| 915 | private final class EncapSocketRecord extends OwnedResourceRecord { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 916 | private FileDescriptor mSocket; |
| 917 | private final int mPort; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 918 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 919 | EncapSocketRecord(int resourceId, FileDescriptor socket, int port) { |
| 920 | super(resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 921 | mSocket = socket; |
| 922 | mPort = port; |
| 923 | } |
| 924 | |
| 925 | /** always guarded by IpSecService#this */ |
| 926 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 927 | public void freeUnderlyingResources() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 928 | Log.d(TAG, "Closing port " + mPort); |
| 929 | IoUtils.closeQuietly(mSocket); |
| 930 | mSocket = null; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 931 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 932 | getResourceTracker().give(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 935 | public int getPort() { |
| 936 | return mPort; |
| 937 | } |
| 938 | |
Benedict Wong | 4c987eb | 2018-03-27 16:55:48 -0700 | [diff] [blame] | 939 | public FileDescriptor getFileDescriptor() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 940 | return mSocket; |
| 941 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 942 | |
| 943 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 944 | protected ResourceTracker getResourceTracker() { |
| 945 | return getUserRecord().mSocketQuotaTracker; |
| 946 | } |
| 947 | |
| 948 | @Override |
| 949 | public void invalidate() { |
| 950 | getUserRecord().removeEncapSocketRecord(mResourceId); |
| 951 | } |
| 952 | |
| 953 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 954 | public String toString() { |
| 955 | return new StringBuilder() |
| 956 | .append("{super=") |
| 957 | .append(super.toString()) |
| 958 | .append(", mSocket=") |
| 959 | .append(mSocket) |
| 960 | .append(", mPort=") |
| 961 | .append(mPort) |
| 962 | .append("}") |
| 963 | .toString(); |
| 964 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 965 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 966 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 967 | /** |
| 968 | * Constructs a new IpSecService instance |
| 969 | * |
| 970 | * @param context Binder context for this service |
| 971 | */ |
| 972 | private IpSecService(Context context) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 973 | this(context, IpSecServiceConfiguration.GETSRVINSTANCE); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | static IpSecService create(Context context) throws InterruptedException { |
| 977 | final IpSecService service = new IpSecService(context); |
| 978 | service.connectNativeNetdService(); |
| 979 | return service; |
| 980 | } |
| 981 | |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 982 | @NonNull |
| 983 | private AppOpsManager getAppOpsManager() { |
| 984 | AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
| 985 | if(appOps == null) throw new RuntimeException("System Server couldn't get AppOps"); |
| 986 | return appOps; |
| 987 | } |
| 988 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 989 | /** @hide */ |
| 990 | @VisibleForTesting |
| 991 | public IpSecService(Context context, IpSecServiceConfiguration config) { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 992 | this( |
| 993 | context, |
| 994 | config, |
| 995 | (fd, uid) -> { |
| 996 | try { |
| 997 | TrafficStats.setThreadStatsUid(uid); |
| 998 | TrafficStats.tagFileDescriptor(fd); |
| 999 | } finally { |
| 1000 | TrafficStats.clearThreadStatsUid(); |
| 1001 | } |
| 1002 | }); |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /** @hide */ |
| 1006 | @VisibleForTesting |
| 1007 | public IpSecService( |
| 1008 | Context context, IpSecServiceConfiguration config, UidFdTagger uidFdTagger) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1009 | mContext = context; |
| 1010 | mSrvConfig = config; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1011 | mUidFdTagger = uidFdTagger; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1014 | public void systemReady() { |
| 1015 | if (isNetdAlive()) { |
| 1016 | Slog.d(TAG, "IpSecService is ready"); |
| 1017 | } else { |
| 1018 | Slog.wtf(TAG, "IpSecService not ready: failed to connect to NetD Native Service!"); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | private void connectNativeNetdService() { |
| 1023 | // Avoid blocking the system server to do this |
Nathan Harold | b0e0508 | 2017-07-17 14:01:53 -0700 | [diff] [blame] | 1024 | new Thread() { |
| 1025 | @Override |
| 1026 | public void run() { |
| 1027 | synchronized (IpSecService.this) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1028 | NetdService.get(NETD_FETCH_TIMEOUT_MS); |
Nathan Harold | b0e0508 | 2017-07-17 14:01:53 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | }.start(); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1034 | synchronized boolean isNetdAlive() { |
| 1035 | try { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1036 | final INetd netd = mSrvConfig.getNetdInstance(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1037 | if (netd == null) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1038 | return false; |
| 1039 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1040 | return netd.isAlive(); |
| 1041 | } catch (RemoteException re) { |
| 1042 | return false; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1046 | /** |
| 1047 | * Checks that the provided InetAddress is valid for use in an IPsec SA. The address must not be |
| 1048 | * a wildcard address and must be in a numeric form such as 1.2.3.4 or 2001::1. |
| 1049 | */ |
| 1050 | private static void checkInetAddress(String inetAddress) { |
| 1051 | if (TextUtils.isEmpty(inetAddress)) { |
| 1052 | throw new IllegalArgumentException("Unspecified address"); |
| 1053 | } |
| 1054 | |
| 1055 | InetAddress checkAddr = NetworkUtils.numericToInetAddress(inetAddress); |
| 1056 | |
| 1057 | if (checkAddr.isAnyLocalAddress()) { |
| 1058 | throw new IllegalArgumentException("Inappropriate wildcard address: " + inetAddress); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * Checks the user-provided direction field and throws an IllegalArgumentException if it is not |
| 1064 | * DIRECTION_IN or DIRECTION_OUT |
| 1065 | */ |
| 1066 | private static void checkDirection(int direction) { |
| 1067 | switch (direction) { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1068 | case IpSecManager.DIRECTION_OUT: |
| 1069 | case IpSecManager.DIRECTION_IN: |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1070 | return; |
| 1071 | } |
| 1072 | throw new IllegalArgumentException("Invalid Direction: " + direction); |
| 1073 | } |
| 1074 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1075 | /** Get a new SPI and maintain the reservation in the system server */ |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 1076 | @Override |
| 1077 | public synchronized IpSecSpiResponse allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1078 | String destinationAddress, int requestedSpi, IBinder binder) throws RemoteException { |
| 1079 | checkInetAddress(destinationAddress); |
Nathan Harold | 7f606ee | 2018-03-28 08:52:51 -0700 | [diff] [blame] | 1080 | // RFC 4303 Section 2.1 - 0=local, 1-255=reserved. |
| 1081 | if (requestedSpi > 0 && requestedSpi < 256) { |
| 1082 | throw new IllegalArgumentException("ESP SPI must not be in the range of 0-255."); |
| 1083 | } |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 1084 | checkNotNull(binder, "Null Binder passed to allocateSecurityParameterIndex"); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1085 | |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1086 | int callingUid = Binder.getCallingUid(); |
| 1087 | UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 1088 | final int resourceId = mNextResourceId++; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1089 | |
| 1090 | int spi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1091 | try { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1092 | if (!userRecord.mSpiQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1093 | return new IpSecSpiResponse( |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1094 | IpSecManager.Status.RESOURCE_UNAVAILABLE, INVALID_RESOURCE_ID, spi); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1095 | } |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1096 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1097 | spi = |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1098 | mSrvConfig |
| 1099 | .getNetdInstance() |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1100 | .ipSecAllocateSpi(callingUid, "", destinationAddress, requestedSpi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1101 | Log.d(TAG, "Allocated SPI " + spi); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1102 | userRecord.mSpiRecords.put( |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1103 | resourceId, |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1104 | new RefcountedResource<SpiRecord>( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1105 | new SpiRecord(resourceId, "", destinationAddress, spi), binder)); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1106 | } catch (ServiceSpecificException e) { |
Nathan Harold | 144ce0a | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1107 | if (e.errorCode == OsConstants.ENOENT) { |
| 1108 | return new IpSecSpiResponse( |
| 1109 | IpSecManager.Status.SPI_UNAVAILABLE, INVALID_RESOURCE_ID, spi); |
| 1110 | } |
| 1111 | throw e; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1112 | } catch (RemoteException e) { |
| 1113 | throw e.rethrowFromSystemServer(); |
| 1114 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1115 | return new IpSecSpiResponse(IpSecManager.Status.OK, resourceId, spi); |
| 1116 | } |
| 1117 | |
| 1118 | /* This method should only be called from Binder threads. Do not call this from |
| 1119 | * within the system server as it will crash the system on failure. |
| 1120 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1121 | private void releaseResource(RefcountedResourceArray resArray, int resourceId) |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1122 | throws RemoteException { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1123 | resArray.getRefcountedResourceOrThrow(resourceId).userRelease(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /** Release a previously allocated SPI that has been registered with the system server */ |
| 1127 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1128 | public synchronized void releaseSecurityParameterIndex(int resourceId) throws RemoteException { |
| 1129 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1130 | releaseResource(userRecord.mSpiRecords, resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * This function finds and forcibly binds to a random system port, ensuring that the port cannot |
| 1135 | * be unbound. |
| 1136 | * |
| 1137 | * <p>A socket cannot be un-bound from a port if it was bound to that port by number. To select |
| 1138 | * a random open port and then bind by number, this function creates a temp socket, binds to a |
| 1139 | * random port (specifying 0), gets that port number, and then uses is to bind the user's UDP |
| 1140 | * Encapsulation Socket forcibly, so that it cannot be un-bound by the user with the returned |
| 1141 | * FileHandle. |
| 1142 | * |
| 1143 | * <p>The loop in this function handles the inherent race window between un-binding to a port |
| 1144 | * and re-binding, during which the system could *technically* hand that port out to someone |
| 1145 | * else. |
| 1146 | */ |
Benedict Wong | f186d67 | 2017-10-10 20:44:28 -0700 | [diff] [blame] | 1147 | private int bindToRandomPort(FileDescriptor sockFd) throws IOException { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1148 | for (int i = MAX_PORT_BIND_ATTEMPTS; i > 0; i--) { |
| 1149 | try { |
| 1150 | FileDescriptor probeSocket = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 1151 | Os.bind(probeSocket, INADDR_ANY, 0); |
| 1152 | int port = ((InetSocketAddress) Os.getsockname(probeSocket)).getPort(); |
| 1153 | Os.close(probeSocket); |
| 1154 | Log.v(TAG, "Binding to port " + port); |
| 1155 | Os.bind(sockFd, INADDR_ANY, port); |
Benedict Wong | f186d67 | 2017-10-10 20:44:28 -0700 | [diff] [blame] | 1156 | return port; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1157 | } catch (ErrnoException e) { |
| 1158 | // Someone miraculously claimed the port just after we closed probeSocket. |
| 1159 | if (e.errno == OsConstants.EADDRINUSE) { |
| 1160 | continue; |
| 1161 | } |
| 1162 | throw e.rethrowAsIOException(); |
| 1163 | } |
| 1164 | } |
| 1165 | throw new IOException("Failed " + MAX_PORT_BIND_ATTEMPTS + " attempts to bind to a port"); |
| 1166 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1167 | |
| 1168 | /** |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1169 | * Functional interface to do traffic tagging of given sockets to UIDs. |
| 1170 | * |
| 1171 | * <p>Specifically used by openUdpEncapsulationSocket to ensure data usage on the UDP encap |
| 1172 | * sockets are billed to the UID that the UDP encap socket was created on behalf of. |
| 1173 | * |
| 1174 | * <p>Separate class so that the socket tagging logic can be mocked; TrafficStats uses static |
| 1175 | * methods that cannot be easily mocked/tested. |
| 1176 | */ |
| 1177 | @VisibleForTesting |
| 1178 | public interface UidFdTagger { |
| 1179 | /** |
| 1180 | * Sets socket tag to assign all traffic to the provided UID. |
| 1181 | * |
| 1182 | * <p>Since the socket is created on behalf of an unprivileged application, all traffic |
| 1183 | * should be accounted to the UID of the unprivileged application. |
| 1184 | */ |
| 1185 | public void tag(FileDescriptor fd, int uid) throws IOException; |
| 1186 | } |
| 1187 | |
| 1188 | /** |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1189 | * Open a socket via the system server and bind it to the specified port (random if port=0). |
| 1190 | * This will return a PFD to the user that represent a bound UDP socket. The system server will |
| 1191 | * cache the socket and a record of its owner so that it can and must be freed when no longer |
| 1192 | * needed. |
| 1193 | */ |
| 1194 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1195 | public synchronized IpSecUdpEncapResponse openUdpEncapsulationSocket(int port, IBinder binder) |
| 1196 | throws RemoteException { |
| 1197 | if (port != 0 && (port < FREE_PORT_MIN || port > PORT_MAX)) { |
| 1198 | throw new IllegalArgumentException( |
| 1199 | "Specified port number must be a valid non-reserved UDP port"); |
| 1200 | } |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1201 | checkNotNull(binder, "Null Binder passed to openUdpEncapsulationSocket"); |
| 1202 | |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1203 | int callingUid = Binder.getCallingUid(); |
| 1204 | UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 1205 | final int resourceId = mNextResourceId++; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1206 | FileDescriptor sockFd = null; |
| 1207 | try { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1208 | if (!userRecord.mSocketQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1209 | return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 1210 | } |
| 1211 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1212 | sockFd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1213 | mUidFdTagger.tag(sockFd, callingUid); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1214 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1215 | // This code is common to both the unspecified and specified port cases |
| 1216 | Os.setsockoptInt( |
| 1217 | sockFd, |
| 1218 | OsConstants.IPPROTO_UDP, |
| 1219 | OsConstants.UDP_ENCAP, |
| 1220 | OsConstants.UDP_ENCAP_ESPINUDP); |
| 1221 | |
Benedict Wong | ba8d313 | 2017-12-06 21:56:35 -0800 | [diff] [blame] | 1222 | mSrvConfig.getNetdInstance().ipSecSetEncapSocketOwner(sockFd, callingUid); |
| 1223 | if (port != 0) { |
| 1224 | Log.v(TAG, "Binding to port " + port); |
| 1225 | Os.bind(sockFd, INADDR_ANY, port); |
| 1226 | } else { |
| 1227 | port = bindToRandomPort(sockFd); |
| 1228 | } |
| 1229 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1230 | userRecord.mEncapSocketRecords.put( |
| 1231 | resourceId, |
| 1232 | new RefcountedResource<EncapSocketRecord>( |
| 1233 | new EncapSocketRecord(resourceId, sockFd, port), binder)); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1234 | return new IpSecUdpEncapResponse(IpSecManager.Status.OK, resourceId, port, sockFd); |
| 1235 | } catch (IOException | ErrnoException e) { |
| 1236 | IoUtils.closeQuietly(sockFd); |
| 1237 | } |
| 1238 | // If we make it to here, then something has gone wrong and we couldn't open a socket. |
| 1239 | // The only reasonable condition that would cause that is resource unavailable. |
| 1240 | return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /** close a socket that has been been allocated by and registered with the system server */ |
| 1244 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1245 | public synchronized void closeUdpEncapsulationSocket(int resourceId) throws RemoteException { |
| 1246 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1247 | releaseResource(userRecord.mEncapSocketRecords, resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1248 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1249 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1250 | /** |
| 1251 | * Create a tunnel interface for use in IPSec tunnel mode. The system server will cache the |
| 1252 | * tunnel interface and a record of its owner so that it can and must be freed when no longer |
| 1253 | * needed. |
| 1254 | */ |
| 1255 | @Override |
| 1256 | public synchronized IpSecTunnelInterfaceResponse createTunnelInterface( |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1257 | String localAddr, String remoteAddr, Network underlyingNetwork, IBinder binder, |
| 1258 | String callingPackage) { |
| 1259 | enforceTunnelPermissions(callingPackage); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1260 | checkNotNull(binder, "Null Binder passed to createTunnelInterface"); |
| 1261 | checkNotNull(underlyingNetwork, "No underlying network was specified"); |
| 1262 | checkInetAddress(localAddr); |
| 1263 | checkInetAddress(remoteAddr); |
| 1264 | |
| 1265 | // TODO: Check that underlying network exists, and IP addresses not assigned to a different |
| 1266 | // network (b/72316676). |
| 1267 | |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1268 | int callerUid = Binder.getCallingUid(); |
| 1269 | UserRecord userRecord = mUserResourceTracker.getUserRecord(callerUid); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1270 | if (!userRecord.mTunnelQuotaTracker.isAvailable()) { |
| 1271 | return new IpSecTunnelInterfaceResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 1272 | } |
| 1273 | |
| 1274 | final int resourceId = mNextResourceId++; |
| 1275 | final int ikey = reserveNetId(); |
| 1276 | final int okey = reserveNetId(); |
Nathan Harold | 4fd7c32 | 2018-04-26 11:47:14 -0700 | [diff] [blame] | 1277 | String intfName = String.format("%s%d", INetd.IPSEC_INTERFACE_PREFIX, resourceId); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1278 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1279 | try { |
| 1280 | // Calls to netd: |
| 1281 | // Create VTI |
| 1282 | // Add inbound/outbound global policies |
| 1283 | // (use reqid = 0) |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1284 | final INetd netd = mSrvConfig.getNetdInstance(); |
| 1285 | netd.addVirtualTunnelInterface(intfName, localAddr, remoteAddr, ikey, okey); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1286 | |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1287 | for (int selAddrFamily : ADDRESS_FAMILIES) { |
| 1288 | // Always send down correct local/remote addresses for template. |
| 1289 | netd.ipSecAddSecurityPolicy( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1290 | callerUid, |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1291 | selAddrFamily, |
| 1292 | IpSecManager.DIRECTION_OUT, |
| 1293 | localAddr, |
| 1294 | remoteAddr, |
| 1295 | 0, |
| 1296 | okey, |
| 1297 | 0xffffffff); |
| 1298 | netd.ipSecAddSecurityPolicy( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1299 | callerUid, |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1300 | selAddrFamily, |
| 1301 | IpSecManager.DIRECTION_IN, |
| 1302 | remoteAddr, |
| 1303 | localAddr, |
| 1304 | 0, |
| 1305 | ikey, |
| 1306 | 0xffffffff); |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | userRecord.mTunnelInterfaceRecords.put( |
| 1310 | resourceId, |
| 1311 | new RefcountedResource<TunnelInterfaceRecord>( |
| 1312 | new TunnelInterfaceRecord( |
| 1313 | resourceId, |
| 1314 | intfName, |
| 1315 | underlyingNetwork, |
| 1316 | localAddr, |
| 1317 | remoteAddr, |
| 1318 | ikey, |
| 1319 | okey), |
| 1320 | binder)); |
| 1321 | return new IpSecTunnelInterfaceResponse(IpSecManager.Status.OK, resourceId, intfName); |
| 1322 | } catch (RemoteException e) { |
| 1323 | // Release keys if we got an error. |
| 1324 | releaseNetId(ikey); |
| 1325 | releaseNetId(okey); |
| 1326 | throw e.rethrowFromSystemServer(); |
Nathan Harold | 144ce0a | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1327 | } catch (Throwable t) { |
| 1328 | // Release keys if we got an error. |
| 1329 | releaseNetId(ikey); |
| 1330 | releaseNetId(okey); |
| 1331 | throw t; |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1332 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * Adds a new local address to the tunnel interface. This allows packets to be sent and received |
| 1337 | * from multiple local IP addresses over the same tunnel. |
| 1338 | */ |
| 1339 | @Override |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1340 | public synchronized void addAddressToTunnelInterface( |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1341 | int tunnelResourceId, LinkAddress localAddr, String callingPackage) { |
| 1342 | enforceTunnelPermissions(callingPackage); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1343 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1344 | |
| 1345 | // Get tunnelInterface record; if no such interface is found, will throw |
| 1346 | // IllegalArgumentException |
| 1347 | TunnelInterfaceRecord tunnelInterfaceInfo = |
| 1348 | userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId); |
| 1349 | |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1350 | try { |
| 1351 | // We can assume general validity of the IP address, since we get them as a |
| 1352 | // LinkAddress, which does some validation. |
| 1353 | mSrvConfig |
| 1354 | .getNetdInstance() |
| 1355 | .interfaceAddAddress( |
| 1356 | tunnelInterfaceInfo.mInterfaceName, |
| 1357 | localAddr.getAddress().getHostAddress(), |
| 1358 | localAddr.getPrefixLength()); |
| 1359 | } catch (RemoteException e) { |
| 1360 | throw e.rethrowFromSystemServer(); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1361 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * Remove a new local address from the tunnel interface. After removal, the address will no |
| 1366 | * longer be available to send from, or receive on. |
| 1367 | */ |
| 1368 | @Override |
| 1369 | public synchronized void removeAddressFromTunnelInterface( |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1370 | int tunnelResourceId, LinkAddress localAddr, String callingPackage) { |
| 1371 | enforceTunnelPermissions(callingPackage); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1372 | |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1373 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1374 | // Get tunnelInterface record; if no such interface is found, will throw |
| 1375 | // IllegalArgumentException |
| 1376 | TunnelInterfaceRecord tunnelInterfaceInfo = |
| 1377 | userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId); |
| 1378 | |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1379 | try { |
| 1380 | // We can assume general validity of the IP address, since we get them as a |
| 1381 | // LinkAddress, which does some validation. |
| 1382 | mSrvConfig |
| 1383 | .getNetdInstance() |
| 1384 | .interfaceDelAddress( |
| 1385 | tunnelInterfaceInfo.mInterfaceName, |
| 1386 | localAddr.getAddress().getHostAddress(), |
| 1387 | localAddr.getPrefixLength()); |
| 1388 | } catch (RemoteException e) { |
| 1389 | throw e.rethrowFromSystemServer(); |
Benedict Wong | ecc9f7c | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1390 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * Delete a TunnelInterface that has been been allocated by and registered with the system |
| 1395 | * server |
| 1396 | */ |
| 1397 | @Override |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1398 | public synchronized void deleteTunnelInterface( |
| 1399 | int resourceId, String callingPackage) throws RemoteException { |
| 1400 | enforceTunnelPermissions(callingPackage); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1401 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1402 | releaseResource(userRecord.mTunnelInterfaceRecords, resourceId); |
| 1403 | } |
| 1404 | |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1405 | @VisibleForTesting |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1406 | void validateAlgorithms(IpSecConfig config) throws IllegalArgumentException { |
| 1407 | IpSecAlgorithm auth = config.getAuthentication(); |
| 1408 | IpSecAlgorithm crypt = config.getEncryption(); |
| 1409 | IpSecAlgorithm aead = config.getAuthenticatedEncryption(); |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1410 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1411 | // Validate the algorithm set |
| 1412 | Preconditions.checkArgument( |
| 1413 | aead != null || crypt != null || auth != null, |
| 1414 | "No Encryption or Authentication algorithms specified"); |
| 1415 | Preconditions.checkArgument( |
| 1416 | auth == null || auth.isAuthentication(), |
| 1417 | "Unsupported algorithm for Authentication"); |
| 1418 | Preconditions.checkArgument( |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1419 | crypt == null || crypt.isEncryption(), "Unsupported algorithm for Encryption"); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1420 | Preconditions.checkArgument( |
| 1421 | aead == null || aead.isAead(), |
| 1422 | "Unsupported algorithm for Authenticated Encryption"); |
| 1423 | Preconditions.checkArgument( |
| 1424 | aead == null || (auth == null && crypt == null), |
| 1425 | "Authenticated Encryption is mutually exclusive with other Authentication " |
| 1426 | + "or Encryption algorithms"); |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1429 | /** |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1430 | * Checks an IpSecConfig parcel to ensure that the contents are sane and throws an |
| 1431 | * IllegalArgumentException if they are not. |
| 1432 | */ |
| 1433 | private void checkIpSecConfig(IpSecConfig config) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1434 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1435 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1436 | switch (config.getEncapType()) { |
| 1437 | case IpSecTransform.ENCAP_NONE: |
| 1438 | break; |
| 1439 | case IpSecTransform.ENCAP_ESPINUDP: |
| 1440 | case IpSecTransform.ENCAP_ESPINUDP_NON_IKE: |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1441 | // Retrieve encap socket record; will throw IllegalArgumentException if not found |
| 1442 | userRecord.mEncapSocketRecords.getResourceOrThrow( |
| 1443 | config.getEncapSocketResourceId()); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1444 | |
| 1445 | int port = config.getEncapRemotePort(); |
| 1446 | if (port <= 0 || port > 0xFFFF) { |
| 1447 | throw new IllegalArgumentException("Invalid remote UDP port: " + port); |
| 1448 | } |
| 1449 | break; |
| 1450 | default: |
| 1451 | throw new IllegalArgumentException("Invalid Encap Type: " + config.getEncapType()); |
| 1452 | } |
| 1453 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1454 | validateAlgorithms(config); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1455 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1456 | // Retrieve SPI record; will throw IllegalArgumentException if not found |
| 1457 | SpiRecord s = userRecord.mSpiRecords.getResourceOrThrow(config.getSpiResourceId()); |
| 1458 | |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 1459 | // Check to ensure that SPI has not already been used. |
| 1460 | if (s.getOwnedByTransform()) { |
| 1461 | throw new IllegalStateException("SPI already in use; cannot be used in new Transforms"); |
| 1462 | } |
| 1463 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1464 | // If no remote address is supplied, then use one from the SPI. |
| 1465 | if (TextUtils.isEmpty(config.getDestinationAddress())) { |
| 1466 | config.setDestinationAddress(s.getDestinationAddress()); |
| 1467 | } |
| 1468 | |
| 1469 | // All remote addresses must match |
| 1470 | if (!config.getDestinationAddress().equals(s.getDestinationAddress())) { |
| 1471 | throw new IllegalArgumentException("Mismatched remote addresseses."); |
| 1472 | } |
| 1473 | |
| 1474 | // This check is technically redundant due to the chain of custody between the SPI and |
| 1475 | // the IpSecConfig, but in the future if the dest is allowed to be set explicitly in |
| 1476 | // the transform, this will prevent us from messing up. |
| 1477 | checkInetAddress(config.getDestinationAddress()); |
| 1478 | |
| 1479 | // Require a valid source address for all transforms. |
| 1480 | checkInetAddress(config.getSourceAddress()); |
| 1481 | |
| 1482 | switch (config.getMode()) { |
| 1483 | case IpSecTransform.MODE_TRANSPORT: |
Nathan Harold | 5a920ca | 2018-02-02 18:34:25 -0800 | [diff] [blame] | 1484 | break; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1485 | case IpSecTransform.MODE_TUNNEL: |
| 1486 | break; |
| 1487 | default: |
| 1488 | throw new IllegalArgumentException( |
| 1489 | "Invalid IpSecTransform.mode: " + config.getMode()); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
Nathan Harold | 8c69bcb | 2018-05-15 19:18:38 -0700 | [diff] [blame] | 1493 | private static final String TUNNEL_OP = "STOPSHIP"; // = AppOpsManager.OP_MANAGE_IPSEC_TUNNELS; |
| 1494 | |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1495 | private void enforceTunnelPermissions(String callingPackage) { |
| 1496 | checkNotNull(callingPackage, "Null calling package cannot create IpSec tunnels"); |
Nathan Harold | 8c69bcb | 2018-05-15 19:18:38 -0700 | [diff] [blame] | 1497 | if (false) { // STOPSHIP if this line is present |
| 1498 | switch (getAppOpsManager().noteOp( |
| 1499 | TUNNEL_OP, |
| 1500 | Binder.getCallingUid(), callingPackage)) { |
| 1501 | case AppOpsManager.MODE_DEFAULT: |
| 1502 | mContext.enforceCallingOrSelfPermission( |
| 1503 | android.Manifest.permission.MANAGE_IPSEC_TUNNELS, "IpSecService"); |
| 1504 | break; |
| 1505 | case AppOpsManager.MODE_ALLOWED: |
| 1506 | return; |
| 1507 | default: |
| 1508 | throw new SecurityException("Request to ignore AppOps for non-legacy API"); |
| 1509 | } |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1510 | } |
Nathan Harold | 5a920ca | 2018-02-02 18:34:25 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1513 | private void createOrUpdateTransform( |
| 1514 | IpSecConfig c, int resourceId, SpiRecord spiRecord, EncapSocketRecord socketRecord) |
| 1515 | throws RemoteException { |
| 1516 | |
| 1517 | int encapType = c.getEncapType(), encapLocalPort = 0, encapRemotePort = 0; |
| 1518 | if (encapType != IpSecTransform.ENCAP_NONE) { |
| 1519 | encapLocalPort = socketRecord.getPort(); |
| 1520 | encapRemotePort = c.getEncapRemotePort(); |
| 1521 | } |
| 1522 | |
| 1523 | IpSecAlgorithm auth = c.getAuthentication(); |
| 1524 | IpSecAlgorithm crypt = c.getEncryption(); |
| 1525 | IpSecAlgorithm authCrypt = c.getAuthenticatedEncryption(); |
| 1526 | |
Benedict Wong | bf013a3 | 2018-03-15 19:41:41 -0700 | [diff] [blame] | 1527 | String cryptName; |
| 1528 | if (crypt == null) { |
| 1529 | cryptName = (authCrypt == null) ? IpSecAlgorithm.CRYPT_NULL : ""; |
| 1530 | } else { |
| 1531 | cryptName = crypt.getName(); |
| 1532 | } |
| 1533 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1534 | mSrvConfig |
| 1535 | .getNetdInstance() |
| 1536 | .ipSecAddSecurityAssociation( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1537 | Binder.getCallingUid(), |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1538 | c.getMode(), |
| 1539 | c.getSourceAddress(), |
| 1540 | c.getDestinationAddress(), |
| 1541 | (c.getNetwork() != null) ? c.getNetwork().netId : 0, |
| 1542 | spiRecord.getSpi(), |
| 1543 | c.getMarkValue(), |
| 1544 | c.getMarkMask(), |
| 1545 | (auth != null) ? auth.getName() : "", |
| 1546 | (auth != null) ? auth.getKey() : new byte[] {}, |
| 1547 | (auth != null) ? auth.getTruncationLengthBits() : 0, |
Benedict Wong | bf013a3 | 2018-03-15 19:41:41 -0700 | [diff] [blame] | 1548 | cryptName, |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1549 | (crypt != null) ? crypt.getKey() : new byte[] {}, |
| 1550 | (crypt != null) ? crypt.getTruncationLengthBits() : 0, |
| 1551 | (authCrypt != null) ? authCrypt.getName() : "", |
| 1552 | (authCrypt != null) ? authCrypt.getKey() : new byte[] {}, |
| 1553 | (authCrypt != null) ? authCrypt.getTruncationLengthBits() : 0, |
| 1554 | encapType, |
| 1555 | encapLocalPort, |
| 1556 | encapRemotePort); |
| 1557 | } |
| 1558 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1559 | /** |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1560 | * Create a IPsec transform, which represents a single security association in the kernel. The |
| 1561 | * transform will be cached by the system server and must be freed when no longer needed. It is |
| 1562 | * possible to free one, deleting the SA from underneath sockets that are using it, which will |
| 1563 | * result in all of those sockets becoming unable to send or receive data. |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1564 | */ |
| 1565 | @Override |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1566 | public synchronized IpSecTransformResponse createTransform( |
| 1567 | IpSecConfig c, IBinder binder, String callingPackage) throws RemoteException { |
| 1568 | checkNotNull(c); |
| 1569 | if (c.getMode() == IpSecTransform.MODE_TUNNEL) { |
| 1570 | enforceTunnelPermissions(callingPackage); |
| 1571 | } |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1572 | checkIpSecConfig(c); |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 1573 | checkNotNull(binder, "Null Binder passed to createTransform"); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 1574 | final int resourceId = mNextResourceId++; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1575 | |
| 1576 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 1577 | List<RefcountedResource> dependencies = new ArrayList<>(); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1578 | |
| 1579 | if (!userRecord.mTransformQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1580 | return new IpSecTransformResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 1581 | } |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1582 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1583 | EncapSocketRecord socketRecord = null; |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1584 | if (c.getEncapType() != IpSecTransform.ENCAP_NONE) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1585 | RefcountedResource<EncapSocketRecord> refcountedSocketRecord = |
| 1586 | userRecord.mEncapSocketRecords.getRefcountedResourceOrThrow( |
| 1587 | c.getEncapSocketResourceId()); |
| 1588 | dependencies.add(refcountedSocketRecord); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1589 | socketRecord = refcountedSocketRecord.getResource(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1592 | RefcountedResource<SpiRecord> refcountedSpiRecord = |
| 1593 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(c.getSpiResourceId()); |
| 1594 | dependencies.add(refcountedSpiRecord); |
| 1595 | SpiRecord spiRecord = refcountedSpiRecord.getResource(); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1596 | |
Nathan Harold | 144ce0a | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1597 | createOrUpdateTransform(c, resourceId, spiRecord, socketRecord); |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1598 | |
| 1599 | // SA was created successfully, time to construct a record and lock it away |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1600 | userRecord.mTransformRecords.put( |
| 1601 | resourceId, |
| 1602 | new RefcountedResource<TransformRecord>( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1603 | new TransformRecord(resourceId, c, spiRecord, socketRecord), |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1604 | binder, |
| 1605 | dependencies.toArray(new RefcountedResource[dependencies.size()]))); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1606 | return new IpSecTransformResponse(IpSecManager.Status.OK, resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * Delete a transport mode transform that was previously allocated by + registered with the |
| 1611 | * system server. If this is called on an inactive (or non-existent) transform, it will not |
| 1612 | * return an error. It's safe to de-allocate transforms that may have already been deleted for |
| 1613 | * other reasons. |
| 1614 | */ |
| 1615 | @Override |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 1616 | public synchronized void deleteTransform(int resourceId) throws RemoteException { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1617 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1618 | releaseResource(userRecord.mTransformRecords, resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * Apply an active transport mode transform to a socket, which will apply the IPsec security |
| 1623 | * association as a correspondent policy to the provided socket |
| 1624 | */ |
| 1625 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1626 | public synchronized void applyTransportModeTransform( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1627 | ParcelFileDescriptor socket, int direction, int resourceId) throws RemoteException { |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1628 | int callingUid = Binder.getCallingUid(); |
| 1629 | UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1630 | checkDirection(direction); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1631 | // Get transform record; if no transform is found, will throw IllegalArgumentException |
| 1632 | TransformRecord info = userRecord.mTransformRecords.getResourceOrThrow(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1633 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1634 | // TODO: make this a function. |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1635 | if (info.pid != getCallingPid() || info.uid != callingUid) { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1636 | throw new SecurityException("Only the owner of an IpSec Transform may apply it!"); |
| 1637 | } |
| 1638 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1639 | // Get config and check that to-be-applied transform has the correct mode |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1640 | IpSecConfig c = info.getConfig(); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1641 | Preconditions.checkArgument( |
| 1642 | c.getMode() == IpSecTransform.MODE_TRANSPORT, |
| 1643 | "Transform mode was not Transport mode; cannot be applied to a socket"); |
| 1644 | |
Nathan Harold | 144ce0a | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1645 | mSrvConfig |
| 1646 | .getNetdInstance() |
| 1647 | .ipSecApplyTransportModeTransform( |
| 1648 | socket.getFileDescriptor(), |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1649 | callingUid, |
Nathan Harold | 144ce0a | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1650 | direction, |
| 1651 | c.getSourceAddress(), |
| 1652 | c.getDestinationAddress(), |
| 1653 | info.getSpiRecord().getSpi()); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1654 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1655 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1656 | /** |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1657 | * Remove transport mode transforms from a socket, applying the default (empty) policy. This |
| 1658 | * ensures that NO IPsec policy is applied to the socket (would be the equivalent of applying a |
| 1659 | * policy that performs no IPsec). Today the resourceId parameter is passed but not used: |
| 1660 | * reserved for future improved input validation. |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1661 | */ |
| 1662 | @Override |
Nathan Harold | f73d252 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 1663 | public synchronized void removeTransportModeTransforms(ParcelFileDescriptor socket) |
| 1664 | throws RemoteException { |
Nathan Harold | 144ce0a | 2018-04-03 16:13:19 -0700 | [diff] [blame] | 1665 | mSrvConfig |
| 1666 | .getNetdInstance() |
| 1667 | .ipSecRemoveTransportModeTransform(socket.getFileDescriptor()); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1670 | /** |
| 1671 | * Apply an active tunnel mode transform to a TunnelInterface, which will apply the IPsec |
| 1672 | * security association as a correspondent policy to the provided interface |
| 1673 | */ |
| 1674 | @Override |
| 1675 | public synchronized void applyTunnelModeTransform( |
Nathan Harold | 21208ee | 2018-03-15 18:06:06 -0700 | [diff] [blame] | 1676 | int tunnelResourceId, int direction, |
| 1677 | int transformResourceId, String callingPackage) throws RemoteException { |
| 1678 | enforceTunnelPermissions(callingPackage); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1679 | checkDirection(direction); |
| 1680 | |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1681 | int callingUid = Binder.getCallingUid(); |
| 1682 | UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1683 | |
| 1684 | // Get transform record; if no transform is found, will throw IllegalArgumentException |
| 1685 | TransformRecord transformInfo = |
| 1686 | userRecord.mTransformRecords.getResourceOrThrow(transformResourceId); |
| 1687 | |
| 1688 | // Get tunnelInterface record; if no such interface is found, will throw |
| 1689 | // IllegalArgumentException |
| 1690 | TunnelInterfaceRecord tunnelInterfaceInfo = |
| 1691 | userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId); |
| 1692 | |
| 1693 | // Get config and check that to-be-applied transform has the correct mode |
| 1694 | IpSecConfig c = transformInfo.getConfig(); |
| 1695 | Preconditions.checkArgument( |
| 1696 | c.getMode() == IpSecTransform.MODE_TUNNEL, |
| 1697 | "Transform mode was not Tunnel mode; cannot be applied to a tunnel interface"); |
| 1698 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1699 | EncapSocketRecord socketRecord = null; |
| 1700 | if (c.getEncapType() != IpSecTransform.ENCAP_NONE) { |
| 1701 | socketRecord = |
| 1702 | userRecord.mEncapSocketRecords.getResourceOrThrow(c.getEncapSocketResourceId()); |
| 1703 | } |
| 1704 | SpiRecord spiRecord = userRecord.mSpiRecords.getResourceOrThrow(c.getSpiResourceId()); |
| 1705 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1706 | int mark = |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1707 | (direction == IpSecManager.DIRECTION_OUT) |
| 1708 | ? tunnelInterfaceInfo.getOkey() |
| 1709 | : tunnelInterfaceInfo.getIkey(); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1710 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1711 | try { |
| 1712 | c.setMarkValue(mark); |
| 1713 | c.setMarkMask(0xffffffff); |
| 1714 | |
| 1715 | if (direction == IpSecManager.DIRECTION_OUT) { |
| 1716 | // Set output mark via underlying network (output only) |
| 1717 | c.setNetwork(tunnelInterfaceInfo.getUnderlyingNetwork()); |
| 1718 | |
| 1719 | // If outbound, also add SPI to the policy. |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1720 | for (int selAddrFamily : ADDRESS_FAMILIES) { |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1721 | mSrvConfig |
| 1722 | .getNetdInstance() |
| 1723 | .ipSecUpdateSecurityPolicy( |
Benedict Wong | a098962 | 2018-07-25 13:06:29 -0700 | [diff] [blame] | 1724 | callingUid, |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1725 | selAddrFamily, |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1726 | direction, |
Benedict Wong | 564e2aa | 2018-05-07 20:06:44 -0700 | [diff] [blame] | 1727 | tunnelInterfaceInfo.getLocalAddress(), |
| 1728 | tunnelInterfaceInfo.getRemoteAddress(), |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1729 | transformInfo.getSpiRecord().getSpi(), |
| 1730 | mark, |
| 1731 | 0xffffffff); |
| 1732 | } |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | // Update SA with tunnel mark (ikey or okey based on direction) |
| 1736 | createOrUpdateTransform(c, transformResourceId, spiRecord, socketRecord); |
| 1737 | } catch (ServiceSpecificException e) { |
| 1738 | if (e.errorCode == EINVAL) { |
| 1739 | throw new IllegalArgumentException(e.toString()); |
| 1740 | } else { |
| 1741 | throw e; |
| 1742 | } |
| 1743 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1746 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1747 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1748 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1749 | |
| 1750 | pw.println("IpSecService dump:"); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1751 | pw.println("NetdNativeService Connection: " + (isNetdAlive() ? "alive" : "dead")); |
| 1752 | pw.println(); |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1753 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1754 | pw.println("mUserResourceTracker:"); |
| 1755 | pw.println(mUserResourceTracker); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1756 | } |
| 1757 | } |