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 | |
| 27 | import android.content.Context; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 28 | import android.net.ConnectivityManager; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 29 | import android.net.IIpSecService; |
| 30 | import android.net.INetd; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 31 | import android.net.IpSecAlgorithm; |
| 32 | import android.net.IpSecConfig; |
| 33 | import android.net.IpSecManager; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 34 | import android.net.IpSecSpiResponse; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 35 | import android.net.IpSecTransform; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 36 | import android.net.IpSecTransformResponse; |
| 37 | import android.net.IpSecUdpEncapResponse; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 38 | import android.net.NetworkUtils; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 39 | import android.net.TrafficStats; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 40 | import android.net.util.NetdService; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 41 | import android.os.Binder; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 42 | import android.os.IBinder; |
| 43 | import android.os.ParcelFileDescriptor; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 44 | import android.os.RemoteException; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 45 | import android.os.ServiceSpecificException; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 46 | import android.system.ErrnoException; |
| 47 | import android.system.Os; |
| 48 | import android.system.OsConstants; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 49 | import android.text.TextUtils; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 50 | import android.util.Log; |
| 51 | import android.util.Slog; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 52 | import android.util.SparseArray; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 53 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 54 | import com.android.internal.annotations.GuardedBy; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 55 | import com.android.internal.annotations.VisibleForTesting; |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 56 | import com.android.internal.util.Preconditions; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 57 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 58 | import java.io.FileDescriptor; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 59 | import java.io.IOException; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 60 | import java.io.PrintWriter; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 61 | import java.net.InetAddress; |
| 62 | import java.net.InetSocketAddress; |
| 63 | import java.net.UnknownHostException; |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 64 | import java.util.ArrayList; |
| 65 | import java.util.List; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 66 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 67 | import libcore.io.IoUtils; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 68 | |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 69 | /** |
| 70 | * A service to manage multiple clients that want to access the IpSec API. The service is |
| 71 | * responsible for maintaining a list of clients and managing the resources (and related quotas) |
| 72 | * that each of them own. |
| 73 | * |
| 74 | * <p>Synchronization in IpSecService is done on all entrypoints due to potential race conditions at |
| 75 | * the kernel/xfrm level. Further, this allows the simplifying assumption to be made that only one |
| 76 | * thread is ever running at a time. |
| 77 | * |
| 78 | * @hide |
| 79 | */ |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 80 | public class IpSecService extends IIpSecService.Stub { |
| 81 | private static final String TAG = "IpSecService"; |
| 82 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 83 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 84 | private static final String NETD_SERVICE_NAME = "netd"; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 85 | private static final int[] DIRECTIONS = |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 86 | new int[] {IpSecManager.DIRECTION_OUT, IpSecManager.DIRECTION_IN}; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 87 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 88 | private static final int NETD_FETCH_TIMEOUT_MS = 5000; // ms |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 89 | private static final int MAX_PORT_BIND_ATTEMPTS = 10; |
| 90 | private static final InetAddress INADDR_ANY; |
| 91 | |
| 92 | static { |
| 93 | try { |
| 94 | INADDR_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0}); |
| 95 | } catch (UnknownHostException e) { |
| 96 | throw new RuntimeException(e); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static final int FREE_PORT_MIN = 1024; // ports 1-1023 are reserved |
| 101 | static final int PORT_MAX = 0xFFFF; // ports are an unsigned 16-bit integer |
| 102 | |
| 103 | /* Binder context for this service */ |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 104 | private final Context mContext; |
| 105 | |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 106 | /** |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 107 | * The next non-repeating global ID for tracking resources between users, this service, and |
| 108 | * kernel data structures. Accessing this variable is not thread safe, so it is only read or |
| 109 | * modified within blocks synchronized on IpSecService.this. We want to avoid -1 |
| 110 | * (INVALID_RESOURCE_ID) and 0 (we probably forgot to initialize it). |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 111 | */ |
| 112 | @GuardedBy("IpSecService.this") |
| 113 | private int mNextResourceId = 1; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 114 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 115 | interface IpSecServiceConfiguration { |
| 116 | INetd getNetdInstance() throws RemoteException; |
| 117 | |
| 118 | static IpSecServiceConfiguration GETSRVINSTANCE = |
| 119 | new IpSecServiceConfiguration() { |
| 120 | @Override |
| 121 | public INetd getNetdInstance() throws RemoteException { |
| 122 | final INetd netd = NetdService.getInstance(); |
| 123 | if (netd == null) { |
| 124 | throw new RemoteException("Failed to Get Netd Instance"); |
| 125 | } |
| 126 | return netd; |
| 127 | } |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | private final IpSecServiceConfiguration mSrvConfig; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 132 | final UidFdTagger mUidFdTagger; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 133 | |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Interface for user-reference and kernel-resource cleanup. |
| 136 | * |
| 137 | * <p>This interface must be implemented for a resource to be reference counted. |
| 138 | */ |
| 139 | @VisibleForTesting |
| 140 | public interface IResource { |
| 141 | /** |
| 142 | * Invalidates a IResource object, ensuring it is invalid for the purposes of allocating new |
| 143 | * objects dependent on it. |
| 144 | * |
| 145 | * <p>Implementations of this method are expected to remove references to the IResource |
| 146 | * object from the IpSecService's tracking arrays. The removal from the arrays ensures that |
| 147 | * the resource is considered invalid for user access or allocation or use in other |
| 148 | * resources. |
| 149 | * |
| 150 | * <p>References to the IResource object may be held by other RefcountedResource objects, |
| 151 | * and as such, the kernel resources and quota may not be cleaned up. |
| 152 | */ |
| 153 | void invalidate() throws RemoteException; |
| 154 | |
| 155 | /** |
| 156 | * Releases underlying resources and related quotas. |
| 157 | * |
| 158 | * <p>Implementations of this method are expected to remove all system resources that are |
| 159 | * tracked by the IResource object. Due to other RefcountedResource objects potentially |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 160 | * having references to the IResource object, freeUnderlyingResources may not always be |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 161 | * called from releaseIfUnreferencedRecursively(). |
| 162 | */ |
| 163 | void freeUnderlyingResources() throws RemoteException; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * RefcountedResource manages references and dependencies in an exclusively acyclic graph. |
| 168 | * |
| 169 | * <p>RefcountedResource implements both explicit and implicit resource management. Creating a |
| 170 | * RefcountedResource object creates an explicit reference that must be freed by calling |
| 171 | * userRelease(). Additionally, adding this object as a child of another RefcountedResource |
| 172 | * object will add an implicit reference. |
| 173 | * |
| 174 | * <p>Resources are cleaned up when all references, both implicit and explicit, are released |
| 175 | * (ie, when userRelease() is called and when all parents have called releaseReference() on this |
| 176 | * object.) |
| 177 | */ |
| 178 | @VisibleForTesting |
| 179 | public class RefcountedResource<T extends IResource> implements IBinder.DeathRecipient { |
| 180 | private final T mResource; |
| 181 | private final List<RefcountedResource> mChildren; |
| 182 | int mRefCount = 1; // starts at 1 for user's reference. |
| 183 | IBinder mBinder; |
| 184 | |
| 185 | RefcountedResource(T resource, IBinder binder, RefcountedResource... children) { |
| 186 | synchronized (IpSecService.this) { |
| 187 | this.mResource = resource; |
| 188 | this.mChildren = new ArrayList<>(children.length); |
| 189 | this.mBinder = binder; |
| 190 | |
| 191 | for (RefcountedResource child : children) { |
| 192 | mChildren.add(child); |
| 193 | child.mRefCount++; |
| 194 | } |
| 195 | |
| 196 | try { |
| 197 | mBinder.linkToDeath(this, 0); |
| 198 | } catch (RemoteException e) { |
| 199 | binderDied(); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * 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] | 206 | * 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] | 207 | * collection |
| 208 | */ |
| 209 | @Override |
| 210 | public void binderDied() { |
| 211 | synchronized (IpSecService.this) { |
| 212 | try { |
| 213 | userRelease(); |
| 214 | } catch (Exception e) { |
| 215 | Log.e(TAG, "Failed to release resource: " + e); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | public T getResource() { |
| 221 | return mResource; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Unlinks from binder and performs IpSecService resource cleanup (removes from resource |
| 226 | * arrays) |
| 227 | * |
| 228 | * <p>If this method has been previously called, the RefcountedResource's binder field will |
| 229 | * be null, and the method will return without performing the cleanup a second time. |
| 230 | * |
| 231 | * <p>Note that calling this function does not imply that kernel resources will be freed at |
| 232 | * this time, or that the related quota will be returned. Such actions will only be |
| 233 | * performed upon the reference count reaching zero. |
| 234 | */ |
| 235 | @GuardedBy("IpSecService.this") |
| 236 | public void userRelease() throws RemoteException { |
| 237 | // Prevent users from putting reference counts into a bad state by calling |
| 238 | // userRelease() multiple times. |
| 239 | if (mBinder == null) { |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | mBinder.unlinkToDeath(this, 0); |
| 244 | mBinder = null; |
| 245 | |
| 246 | mResource.invalidate(); |
| 247 | |
| 248 | releaseReference(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Removes a reference to this resource. If the resultant reference count is zero, the |
| 253 | * underlying resources are freed, and references to all child resources are also dropped |
| 254 | * recursively (resulting in them freeing their resources and children, etcetera) |
| 255 | * |
| 256 | * <p>This method also sets the reference count to an invalid value (-1) to signify that it |
| 257 | * has been fully released. Any subsequent calls to this method will result in an |
| 258 | * IllegalStateException being thrown due to resource already having been previously |
| 259 | * released |
| 260 | */ |
| 261 | @VisibleForTesting |
| 262 | @GuardedBy("IpSecService.this") |
| 263 | public void releaseReference() throws RemoteException { |
| 264 | mRefCount--; |
| 265 | |
| 266 | if (mRefCount > 0) { |
| 267 | return; |
| 268 | } else if (mRefCount < 0) { |
| 269 | throw new IllegalStateException( |
| 270 | "Invalid operation - resource has already been released."); |
| 271 | } |
| 272 | |
| 273 | // Cleanup own resources |
| 274 | mResource.freeUnderlyingResources(); |
| 275 | |
| 276 | // Cleanup child resources as needed |
| 277 | for (RefcountedResource<? extends IResource> child : mChildren) { |
| 278 | child.releaseReference(); |
| 279 | } |
| 280 | |
| 281 | // Enforce that resource cleanup can only be called once |
| 282 | // By decrementing the refcount (from 0 to -1), the next call will throw an |
| 283 | // IllegalStateException - it has already been released fully. |
| 284 | mRefCount--; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public String toString() { |
| 289 | return new StringBuilder() |
| 290 | .append("{mResource=") |
| 291 | .append(mResource) |
| 292 | .append(", mRefCount=") |
| 293 | .append(mRefCount) |
| 294 | .append(", mChildren=") |
| 295 | .append(mChildren) |
| 296 | .append("}") |
| 297 | .toString(); |
| 298 | } |
| 299 | } |
| 300 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 301 | /* Very simple counting class that looks much like a counting semaphore */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 302 | @VisibleForTesting |
| 303 | static class ResourceTracker { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 304 | private final int mMax; |
| 305 | int mCurrent; |
| 306 | |
| 307 | ResourceTracker(int max) { |
| 308 | mMax = max; |
| 309 | mCurrent = 0; |
| 310 | } |
| 311 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 312 | boolean isAvailable() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 313 | return (mCurrent < mMax); |
| 314 | } |
| 315 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 316 | void take() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 317 | if (!isAvailable()) { |
| 318 | Log.wtf(TAG, "Too many resources allocated!"); |
| 319 | } |
| 320 | mCurrent++; |
| 321 | } |
| 322 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 323 | void give() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 324 | if (mCurrent <= 0) { |
| 325 | Log.wtf(TAG, "We've released this resource too many times"); |
| 326 | } |
| 327 | mCurrent--; |
| 328 | } |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 329 | |
| 330 | @Override |
| 331 | public String toString() { |
| 332 | return new StringBuilder() |
| 333 | .append("{mCurrent=") |
| 334 | .append(mCurrent) |
| 335 | .append(", mMax=") |
| 336 | .append(mMax) |
| 337 | .append("}") |
| 338 | .toString(); |
| 339 | } |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 342 | @VisibleForTesting |
| 343 | static final class UserRecord { |
| 344 | /* Type names */ |
| 345 | public static final String TYPENAME_SPI = "SecurityParameterIndex"; |
| 346 | public static final String TYPENAME_TRANSFORM = "IpSecTransform"; |
| 347 | public static final String TYPENAME_ENCAP_SOCKET = "UdpEncapSocket"; |
| 348 | |
| 349 | /* Maximum number of each type of resource that a single UID may possess */ |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 350 | public static final int MAX_NUM_ENCAP_SOCKETS = 2; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 351 | public static final int MAX_NUM_TRANSFORMS = 4; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 352 | public static final int MAX_NUM_SPIS = 8; |
| 353 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 354 | final RefcountedResourceArray<SpiRecord> mSpiRecords = |
| 355 | new RefcountedResourceArray<>(TYPENAME_SPI); |
| 356 | final ResourceTracker mSpiQuotaTracker = new ResourceTracker(MAX_NUM_SPIS); |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 357 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 358 | final RefcountedResourceArray<TransformRecord> mTransformRecords = |
| 359 | new RefcountedResourceArray<>(TYPENAME_TRANSFORM); |
| 360 | final ResourceTracker mTransformQuotaTracker = new ResourceTracker(MAX_NUM_TRANSFORMS); |
| 361 | |
| 362 | final RefcountedResourceArray<EncapSocketRecord> mEncapSocketRecords = |
| 363 | new RefcountedResourceArray<>(TYPENAME_ENCAP_SOCKET); |
| 364 | final ResourceTracker mSocketQuotaTracker = new ResourceTracker(MAX_NUM_ENCAP_SOCKETS); |
| 365 | |
| 366 | void removeSpiRecord(int resourceId) { |
| 367 | mSpiRecords.remove(resourceId); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 370 | void removeTransformRecord(int resourceId) { |
| 371 | mTransformRecords.remove(resourceId); |
| 372 | } |
| 373 | |
| 374 | void removeEncapSocketRecord(int resourceId) { |
| 375 | mEncapSocketRecords.remove(resourceId); |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public String toString() { |
| 380 | return new StringBuilder() |
| 381 | .append("{mSpiQuotaTracker=") |
| 382 | .append(mSpiQuotaTracker) |
| 383 | .append(", mTransformQuotaTracker=") |
| 384 | .append(mTransformQuotaTracker) |
| 385 | .append(", mSocketQuotaTracker=") |
| 386 | .append(mSocketQuotaTracker) |
| 387 | .append(", mSpiRecords=") |
| 388 | .append(mSpiRecords) |
| 389 | .append(", mTransformRecords=") |
| 390 | .append(mTransformRecords) |
| 391 | .append(", mEncapSocketRecords=") |
| 392 | .append(mEncapSocketRecords) |
| 393 | .append("}") |
| 394 | .toString(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | @VisibleForTesting |
| 399 | static final class UserResourceTracker { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 400 | private final SparseArray<UserRecord> mUserRecords = new SparseArray<>(); |
| 401 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 402 | /** Never-fail getter that populates the list of UIDs as-needed */ |
| 403 | public UserRecord getUserRecord(int uid) { |
| 404 | checkCallerUid(uid); |
| 405 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 406 | UserRecord r = mUserRecords.get(uid); |
| 407 | if (r == null) { |
| 408 | r = new UserRecord(); |
| 409 | mUserRecords.put(uid, r); |
| 410 | } |
| 411 | return r; |
| 412 | } |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 413 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 414 | /** Safety method; guards against access of other user's UserRecords */ |
| 415 | private void checkCallerUid(int uid) { |
| 416 | if (uid != Binder.getCallingUid() |
| 417 | && android.os.Process.SYSTEM_UID != Binder.getCallingUid()) { |
| 418 | throw new SecurityException("Attempted access of unowned resources"); |
| 419 | } |
| 420 | } |
| 421 | |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 422 | @Override |
| 423 | public String toString() { |
| 424 | return mUserRecords.toString(); |
| 425 | } |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 428 | @VisibleForTesting final UserResourceTracker mUserResourceTracker = new UserResourceTracker(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 429 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 430 | /** |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 431 | * The KernelResourceRecord class provides a facility to cleanly and reliably track system |
| 432 | * resources. It relies on a provided resourceId that should uniquely identify the kernel |
| 433 | * resource. To use this class, the user should implement the invalidate() and |
| 434 | * freeUnderlyingResources() methods that are responsible for cleaning up IpSecService resource |
| 435 | * tracking arrays and kernel resources, respectively |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 436 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 437 | private abstract class KernelResourceRecord implements IResource { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 438 | final int pid; |
| 439 | final int uid; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 440 | protected final int mResourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 441 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 442 | KernelResourceRecord(int resourceId) { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 443 | super(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 444 | if (resourceId == INVALID_RESOURCE_ID) { |
| 445 | throw new IllegalArgumentException("Resource ID must not be INVALID_RESOURCE_ID"); |
| 446 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 447 | mResourceId = resourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 448 | pid = Binder.getCallingPid(); |
| 449 | uid = Binder.getCallingUid(); |
| 450 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 451 | getResourceTracker().take(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 454 | @Override |
| 455 | public abstract void invalidate() throws RemoteException; |
| 456 | |
| 457 | /** Convenience method; retrieves the user resource record for the stored UID. */ |
| 458 | protected UserRecord getUserRecord() { |
| 459 | return mUserResourceTracker.getUserRecord(uid); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 462 | @Override |
| 463 | public abstract void freeUnderlyingResources() throws RemoteException; |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 464 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 465 | /** Get the resource tracker for this resource */ |
| 466 | protected abstract ResourceTracker getResourceTracker(); |
| 467 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 468 | @Override |
| 469 | public String toString() { |
| 470 | return new StringBuilder() |
| 471 | .append("{mResourceId=") |
| 472 | .append(mResourceId) |
| 473 | .append(", pid=") |
| 474 | .append(pid) |
| 475 | .append(", uid=") |
| 476 | .append(uid) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 477 | .append("}") |
| 478 | .toString(); |
| 479 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 480 | }; |
| 481 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 482 | // TODO: Move this to right after RefcountedResource. With this here, Gerrit was showing many |
| 483 | // more things as changed. |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 484 | /** |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 485 | * Thin wrapper over SparseArray to ensure resources exist, and simplify generic typing. |
| 486 | * |
| 487 | * <p>RefcountedResourceArray prevents null insertions, and throws an IllegalArgumentException |
| 488 | * if a key is not found during a retrieval process. |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 489 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 490 | static class RefcountedResourceArray<T extends IResource> { |
| 491 | SparseArray<RefcountedResource<T>> mArray = new SparseArray<>(); |
| 492 | private final String mTypeName; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 493 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 494 | public RefcountedResourceArray(String typeName) { |
| 495 | this.mTypeName = typeName; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 498 | /** |
| 499 | * Accessor method to get inner resource object. |
| 500 | * |
| 501 | * @throws IllegalArgumentException if no resource with provided key is found. |
| 502 | */ |
| 503 | T getResourceOrThrow(int key) { |
| 504 | return getRefcountedResourceOrThrow(key).getResource(); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Accessor method to get reference counting wrapper. |
| 509 | * |
| 510 | * @throws IllegalArgumentException if no resource with provided key is found. |
| 511 | */ |
| 512 | RefcountedResource<T> getRefcountedResourceOrThrow(int key) { |
| 513 | RefcountedResource<T> resource = mArray.get(key); |
| 514 | if (resource == null) { |
| 515 | throw new IllegalArgumentException( |
| 516 | String.format("No such %s found for given id: %d", mTypeName, key)); |
| 517 | } |
| 518 | |
| 519 | return resource; |
| 520 | } |
| 521 | |
| 522 | void put(int key, RefcountedResource<T> obj) { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 523 | checkNotNull(obj, "Null resources cannot be added"); |
| 524 | mArray.put(key, obj); |
| 525 | } |
| 526 | |
| 527 | void remove(int key) { |
| 528 | mArray.remove(key); |
| 529 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 530 | |
| 531 | @Override |
| 532 | public String toString() { |
| 533 | return mArray.toString(); |
| 534 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 537 | private final class TransformRecord extends KernelResourceRecord { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 538 | private final IpSecConfig mConfig; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 539 | private final SpiRecord mSpi; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 540 | private final EncapSocketRecord mSocket; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 541 | |
| 542 | TransformRecord( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 543 | int resourceId, IpSecConfig config, SpiRecord spi, EncapSocketRecord socket) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 544 | super(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 545 | mConfig = config; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 546 | mSpi = spi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 547 | mSocket = socket; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | public IpSecConfig getConfig() { |
| 551 | return mConfig; |
| 552 | } |
| 553 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 554 | public SpiRecord getSpiRecord() { |
| 555 | return mSpi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /** always guarded by IpSecService#this */ |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 559 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 560 | public void freeUnderlyingResources() { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 561 | int spi = mSpi.getSpi(); |
| 562 | try { |
| 563 | mSrvConfig |
| 564 | .getNetdInstance() |
| 565 | .ipSecDeleteSecurityAssociation( |
| 566 | mResourceId, |
| 567 | mConfig.getSourceAddress(), |
| 568 | mConfig.getDestinationAddress(), |
| 569 | spi); |
| 570 | } catch (ServiceSpecificException e) { |
| 571 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 572 | } catch (RemoteException e) { |
| 573 | Log.e(TAG, "Failed to delete SA with ID: " + mResourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 574 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 575 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 576 | getResourceTracker().give(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 577 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 578 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 579 | @Override |
| 580 | public void invalidate() throws RemoteException { |
| 581 | getUserRecord().removeTransformRecord(mResourceId); |
| 582 | } |
| 583 | |
| 584 | @Override |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 585 | protected ResourceTracker getResourceTracker() { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 586 | return getUserRecord().mTransformQuotaTracker; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 587 | } |
| 588 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 589 | @Override |
| 590 | public String toString() { |
| 591 | StringBuilder strBuilder = new StringBuilder(); |
| 592 | strBuilder |
| 593 | .append("{super=") |
| 594 | .append(super.toString()) |
| 595 | .append(", mSocket=") |
| 596 | .append(mSocket) |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 597 | .append(", mSpi.mResourceId=") |
| 598 | .append(mSpi.mResourceId) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 599 | .append(", mConfig=") |
| 600 | .append(mConfig) |
| 601 | .append("}"); |
| 602 | return strBuilder.toString(); |
| 603 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 606 | private final class SpiRecord extends KernelResourceRecord { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 607 | private final String mSourceAddress; |
| 608 | private final String mDestinationAddress; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 609 | private int mSpi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 610 | |
| 611 | private boolean mOwnedByTransform = false; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 612 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 613 | SpiRecord(int resourceId, String sourceAddress, String destinationAddress, int spi) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 614 | super(resourceId); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 615 | mSourceAddress = sourceAddress; |
| 616 | mDestinationAddress = destinationAddress; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 617 | mSpi = spi; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 620 | /** always guarded by IpSecService#this */ |
| 621 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 622 | public void freeUnderlyingResources() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 623 | if (mOwnedByTransform) { |
| 624 | Log.d(TAG, "Cannot release Spi " + mSpi + ": Currently locked by a Transform"); |
| 625 | // Because SPIs are "handed off" to transform, objects, they should never be |
| 626 | // freed from the SpiRecord once used in a transform. (They refer to the same SA, |
| 627 | // thus ownership and responsibility for freeing these resources passes to the |
| 628 | // Transform object). Thus, we should let the user free them without penalty once |
| 629 | // they are applied in a Transform object. |
| 630 | return; |
| 631 | } |
| 632 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 633 | try { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 634 | mSrvConfig |
| 635 | .getNetdInstance() |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 636 | .ipSecDeleteSecurityAssociation( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 637 | mResourceId, mSourceAddress, mDestinationAddress, mSpi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 638 | } catch (ServiceSpecificException e) { |
| 639 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 640 | } catch (RemoteException e) { |
| 641 | Log.e(TAG, "Failed to delete SPI reservation with ID: " + mResourceId); |
| 642 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 643 | |
| 644 | mSpi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 645 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 646 | getResourceTracker().give(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 649 | public int getSpi() { |
| 650 | return mSpi; |
| 651 | } |
| 652 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 653 | public String getDestinationAddress() { |
| 654 | return mDestinationAddress; |
| 655 | } |
| 656 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 657 | public void setOwnedByTransform() { |
| 658 | if (mOwnedByTransform) { |
| 659 | // Programming error |
Andreas Gampe | d6d8e45 | 2017-07-11 10:25:09 -0700 | [diff] [blame] | 660 | throw new IllegalStateException("Cannot own an SPI twice!"); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | mOwnedByTransform = true; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 664 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 665 | |
| 666 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 667 | public void invalidate() throws RemoteException { |
| 668 | getUserRecord().removeSpiRecord(mResourceId); |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | protected ResourceTracker getResourceTracker() { |
| 673 | return getUserRecord().mSpiQuotaTracker; |
| 674 | } |
| 675 | |
| 676 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 677 | public String toString() { |
| 678 | StringBuilder strBuilder = new StringBuilder(); |
| 679 | strBuilder |
| 680 | .append("{super=") |
| 681 | .append(super.toString()) |
| 682 | .append(", mSpi=") |
| 683 | .append(mSpi) |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 684 | .append(", mSourceAddress=") |
| 685 | .append(mSourceAddress) |
| 686 | .append(", mDestinationAddress=") |
| 687 | .append(mDestinationAddress) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 688 | .append(", mOwnedByTransform=") |
| 689 | .append(mOwnedByTransform) |
| 690 | .append("}"); |
| 691 | return strBuilder.toString(); |
| 692 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 695 | private final class EncapSocketRecord extends KernelResourceRecord { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 696 | private FileDescriptor mSocket; |
| 697 | private final int mPort; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 698 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 699 | EncapSocketRecord(int resourceId, FileDescriptor socket, int port) { |
| 700 | super(resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 701 | mSocket = socket; |
| 702 | mPort = port; |
| 703 | } |
| 704 | |
| 705 | /** always guarded by IpSecService#this */ |
| 706 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 707 | public void freeUnderlyingResources() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 708 | Log.d(TAG, "Closing port " + mPort); |
| 709 | IoUtils.closeQuietly(mSocket); |
| 710 | mSocket = null; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 711 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 712 | getResourceTracker().give(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 715 | public int getPort() { |
| 716 | return mPort; |
| 717 | } |
| 718 | |
| 719 | public FileDescriptor getSocket() { |
| 720 | return mSocket; |
| 721 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 722 | |
| 723 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 724 | protected ResourceTracker getResourceTracker() { |
| 725 | return getUserRecord().mSocketQuotaTracker; |
| 726 | } |
| 727 | |
| 728 | @Override |
| 729 | public void invalidate() { |
| 730 | getUserRecord().removeEncapSocketRecord(mResourceId); |
| 731 | } |
| 732 | |
| 733 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 734 | public String toString() { |
| 735 | return new StringBuilder() |
| 736 | .append("{super=") |
| 737 | .append(super.toString()) |
| 738 | .append(", mSocket=") |
| 739 | .append(mSocket) |
| 740 | .append(", mPort=") |
| 741 | .append(mPort) |
| 742 | .append("}") |
| 743 | .toString(); |
| 744 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 745 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 746 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 747 | /** |
| 748 | * Constructs a new IpSecService instance |
| 749 | * |
| 750 | * @param context Binder context for this service |
| 751 | */ |
| 752 | private IpSecService(Context context) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 753 | this(context, IpSecServiceConfiguration.GETSRVINSTANCE); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | static IpSecService create(Context context) throws InterruptedException { |
| 757 | final IpSecService service = new IpSecService(context); |
| 758 | service.connectNativeNetdService(); |
| 759 | return service; |
| 760 | } |
| 761 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 762 | /** @hide */ |
| 763 | @VisibleForTesting |
| 764 | public IpSecService(Context context, IpSecServiceConfiguration config) { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 765 | this( |
| 766 | context, |
| 767 | config, |
| 768 | (fd, uid) -> { |
| 769 | try { |
| 770 | TrafficStats.setThreadStatsUid(uid); |
| 771 | TrafficStats.tagFileDescriptor(fd); |
| 772 | } finally { |
| 773 | TrafficStats.clearThreadStatsUid(); |
| 774 | } |
| 775 | }); |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /** @hide */ |
| 779 | @VisibleForTesting |
| 780 | public IpSecService( |
| 781 | Context context, IpSecServiceConfiguration config, UidFdTagger uidFdTagger) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 782 | mContext = context; |
| 783 | mSrvConfig = config; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 784 | mUidFdTagger = uidFdTagger; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 787 | public void systemReady() { |
| 788 | if (isNetdAlive()) { |
| 789 | Slog.d(TAG, "IpSecService is ready"); |
| 790 | } else { |
| 791 | Slog.wtf(TAG, "IpSecService not ready: failed to connect to NetD Native Service!"); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | private void connectNativeNetdService() { |
| 796 | // Avoid blocking the system server to do this |
Nathan Harold | b0e0508 | 2017-07-17 14:01:53 -0700 | [diff] [blame] | 797 | new Thread() { |
| 798 | @Override |
| 799 | public void run() { |
| 800 | synchronized (IpSecService.this) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 801 | NetdService.get(NETD_FETCH_TIMEOUT_MS); |
Nathan Harold | b0e0508 | 2017-07-17 14:01:53 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | }.start(); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 805 | } |
| 806 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 807 | synchronized boolean isNetdAlive() { |
| 808 | try { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 809 | final INetd netd = mSrvConfig.getNetdInstance(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 810 | if (netd == null) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 811 | return false; |
| 812 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 813 | return netd.isAlive(); |
| 814 | } catch (RemoteException re) { |
| 815 | return false; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 819 | /** |
| 820 | * Checks that the provided InetAddress is valid for use in an IPsec SA. The address must not be |
| 821 | * a wildcard address and must be in a numeric form such as 1.2.3.4 or 2001::1. |
| 822 | */ |
| 823 | private static void checkInetAddress(String inetAddress) { |
| 824 | if (TextUtils.isEmpty(inetAddress)) { |
| 825 | throw new IllegalArgumentException("Unspecified address"); |
| 826 | } |
| 827 | |
| 828 | InetAddress checkAddr = NetworkUtils.numericToInetAddress(inetAddress); |
| 829 | |
| 830 | if (checkAddr.isAnyLocalAddress()) { |
| 831 | throw new IllegalArgumentException("Inappropriate wildcard address: " + inetAddress); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * Checks the user-provided direction field and throws an IllegalArgumentException if it is not |
| 837 | * DIRECTION_IN or DIRECTION_OUT |
| 838 | */ |
| 839 | private static void checkDirection(int direction) { |
| 840 | switch (direction) { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 841 | case IpSecManager.DIRECTION_OUT: |
| 842 | case IpSecManager.DIRECTION_IN: |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 843 | return; |
| 844 | } |
| 845 | throw new IllegalArgumentException("Invalid Direction: " + direction); |
| 846 | } |
| 847 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 848 | /** Get a new SPI and maintain the reservation in the system server */ |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 849 | @Override |
| 850 | public synchronized IpSecSpiResponse allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 851 | String destinationAddress, int requestedSpi, IBinder binder) throws RemoteException { |
| 852 | checkInetAddress(destinationAddress); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 853 | /* requestedSpi can be anything in the int range, so no check is needed. */ |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 854 | checkNotNull(binder, "Null Binder passed to allocateSecurityParameterIndex"); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 855 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 856 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 857 | final int resourceId = mNextResourceId++; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 858 | |
| 859 | int spi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 860 | try { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 861 | if (!userRecord.mSpiQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 862 | return new IpSecSpiResponse( |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 863 | IpSecManager.Status.RESOURCE_UNAVAILABLE, INVALID_RESOURCE_ID, spi); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 864 | } |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 865 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 866 | spi = |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 867 | mSrvConfig |
| 868 | .getNetdInstance() |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 869 | .ipSecAllocateSpi(resourceId, "", destinationAddress, requestedSpi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 870 | Log.d(TAG, "Allocated SPI " + spi); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 871 | userRecord.mSpiRecords.put( |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 872 | resourceId, |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 873 | new RefcountedResource<SpiRecord>( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 874 | new SpiRecord(resourceId, "", destinationAddress, spi), binder)); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 875 | } catch (ServiceSpecificException e) { |
| 876 | // TODO: Add appropriate checks when other ServiceSpecificException types are supported |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 877 | return new IpSecSpiResponse( |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 878 | IpSecManager.Status.SPI_UNAVAILABLE, INVALID_RESOURCE_ID, spi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 879 | } catch (RemoteException e) { |
| 880 | throw e.rethrowFromSystemServer(); |
| 881 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 882 | return new IpSecSpiResponse(IpSecManager.Status.OK, resourceId, spi); |
| 883 | } |
| 884 | |
| 885 | /* This method should only be called from Binder threads. Do not call this from |
| 886 | * within the system server as it will crash the system on failure. |
| 887 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 888 | private void releaseResource(RefcountedResourceArray resArray, int resourceId) |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 889 | throws RemoteException { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 890 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 891 | resArray.getRefcountedResourceOrThrow(resourceId).userRelease(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /** Release a previously allocated SPI that has been registered with the system server */ |
| 895 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 896 | public synchronized void releaseSecurityParameterIndex(int resourceId) throws RemoteException { |
| 897 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 898 | releaseResource(userRecord.mSpiRecords, resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /** |
| 902 | * This function finds and forcibly binds to a random system port, ensuring that the port cannot |
| 903 | * be unbound. |
| 904 | * |
| 905 | * <p>A socket cannot be un-bound from a port if it was bound to that port by number. To select |
| 906 | * a random open port and then bind by number, this function creates a temp socket, binds to a |
| 907 | * random port (specifying 0), gets that port number, and then uses is to bind the user's UDP |
| 908 | * Encapsulation Socket forcibly, so that it cannot be un-bound by the user with the returned |
| 909 | * FileHandle. |
| 910 | * |
| 911 | * <p>The loop in this function handles the inherent race window between un-binding to a port |
| 912 | * and re-binding, during which the system could *technically* hand that port out to someone |
| 913 | * else. |
| 914 | */ |
Benedict Wong | f186d67 | 2017-10-10 20:44:28 -0700 | [diff] [blame] | 915 | private int bindToRandomPort(FileDescriptor sockFd) throws IOException { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 916 | for (int i = MAX_PORT_BIND_ATTEMPTS; i > 0; i--) { |
| 917 | try { |
| 918 | FileDescriptor probeSocket = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 919 | Os.bind(probeSocket, INADDR_ANY, 0); |
| 920 | int port = ((InetSocketAddress) Os.getsockname(probeSocket)).getPort(); |
| 921 | Os.close(probeSocket); |
| 922 | Log.v(TAG, "Binding to port " + port); |
| 923 | Os.bind(sockFd, INADDR_ANY, port); |
Benedict Wong | f186d67 | 2017-10-10 20:44:28 -0700 | [diff] [blame] | 924 | return port; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 925 | } catch (ErrnoException e) { |
| 926 | // Someone miraculously claimed the port just after we closed probeSocket. |
| 927 | if (e.errno == OsConstants.EADDRINUSE) { |
| 928 | continue; |
| 929 | } |
| 930 | throw e.rethrowAsIOException(); |
| 931 | } |
| 932 | } |
| 933 | throw new IOException("Failed " + MAX_PORT_BIND_ATTEMPTS + " attempts to bind to a port"); |
| 934 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 935 | |
| 936 | /** |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 937 | * Functional interface to do traffic tagging of given sockets to UIDs. |
| 938 | * |
| 939 | * <p>Specifically used by openUdpEncapsulationSocket to ensure data usage on the UDP encap |
| 940 | * sockets are billed to the UID that the UDP encap socket was created on behalf of. |
| 941 | * |
| 942 | * <p>Separate class so that the socket tagging logic can be mocked; TrafficStats uses static |
| 943 | * methods that cannot be easily mocked/tested. |
| 944 | */ |
| 945 | @VisibleForTesting |
| 946 | public interface UidFdTagger { |
| 947 | /** |
| 948 | * Sets socket tag to assign all traffic to the provided UID. |
| 949 | * |
| 950 | * <p>Since the socket is created on behalf of an unprivileged application, all traffic |
| 951 | * should be accounted to the UID of the unprivileged application. |
| 952 | */ |
| 953 | public void tag(FileDescriptor fd, int uid) throws IOException; |
| 954 | } |
| 955 | |
| 956 | /** |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 957 | * Open a socket via the system server and bind it to the specified port (random if port=0). |
| 958 | * This will return a PFD to the user that represent a bound UDP socket. The system server will |
| 959 | * cache the socket and a record of its owner so that it can and must be freed when no longer |
| 960 | * needed. |
| 961 | */ |
| 962 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 963 | public synchronized IpSecUdpEncapResponse openUdpEncapsulationSocket(int port, IBinder binder) |
| 964 | throws RemoteException { |
| 965 | if (port != 0 && (port < FREE_PORT_MIN || port > PORT_MAX)) { |
| 966 | throw new IllegalArgumentException( |
| 967 | "Specified port number must be a valid non-reserved UDP port"); |
| 968 | } |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 969 | checkNotNull(binder, "Null Binder passed to openUdpEncapsulationSocket"); |
| 970 | |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 971 | int callingUid = Binder.getCallingUid(); |
| 972 | UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 973 | final int resourceId = mNextResourceId++; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 974 | FileDescriptor sockFd = null; |
| 975 | try { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 976 | if (!userRecord.mSocketQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 977 | return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 978 | } |
| 979 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 980 | sockFd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 981 | mUidFdTagger.tag(sockFd, callingUid); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 982 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 983 | // This code is common to both the unspecified and specified port cases |
| 984 | Os.setsockoptInt( |
| 985 | sockFd, |
| 986 | OsConstants.IPPROTO_UDP, |
| 987 | OsConstants.UDP_ENCAP, |
| 988 | OsConstants.UDP_ENCAP_ESPINUDP); |
| 989 | |
Benedict Wong | ba8d313 | 2017-12-06 21:56:35 -0800 | [diff] [blame] | 990 | mSrvConfig.getNetdInstance().ipSecSetEncapSocketOwner(sockFd, callingUid); |
| 991 | if (port != 0) { |
| 992 | Log.v(TAG, "Binding to port " + port); |
| 993 | Os.bind(sockFd, INADDR_ANY, port); |
| 994 | } else { |
| 995 | port = bindToRandomPort(sockFd); |
| 996 | } |
| 997 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 998 | userRecord.mEncapSocketRecords.put( |
| 999 | resourceId, |
| 1000 | new RefcountedResource<EncapSocketRecord>( |
| 1001 | new EncapSocketRecord(resourceId, sockFd, port), binder)); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1002 | return new IpSecUdpEncapResponse(IpSecManager.Status.OK, resourceId, port, sockFd); |
| 1003 | } catch (IOException | ErrnoException e) { |
| 1004 | IoUtils.closeQuietly(sockFd); |
| 1005 | } |
| 1006 | // If we make it to here, then something has gone wrong and we couldn't open a socket. |
| 1007 | // The only reasonable condition that would cause that is resource unavailable. |
| 1008 | return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /** close a socket that has been been allocated by and registered with the system server */ |
| 1012 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1013 | public synchronized void closeUdpEncapsulationSocket(int resourceId) throws RemoteException { |
| 1014 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1015 | releaseResource(userRecord.mEncapSocketRecords, resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1016 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1017 | |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1018 | @VisibleForTesting |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1019 | void validateAlgorithms(IpSecConfig config) throws IllegalArgumentException { |
| 1020 | IpSecAlgorithm auth = config.getAuthentication(); |
| 1021 | IpSecAlgorithm crypt = config.getEncryption(); |
| 1022 | IpSecAlgorithm aead = config.getAuthenticatedEncryption(); |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1023 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1024 | // Validate the algorithm set |
| 1025 | Preconditions.checkArgument( |
| 1026 | aead != null || crypt != null || auth != null, |
| 1027 | "No Encryption or Authentication algorithms specified"); |
| 1028 | Preconditions.checkArgument( |
| 1029 | auth == null || auth.isAuthentication(), |
| 1030 | "Unsupported algorithm for Authentication"); |
| 1031 | Preconditions.checkArgument( |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1032 | crypt == null || crypt.isEncryption(), "Unsupported algorithm for Encryption"); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1033 | Preconditions.checkArgument( |
| 1034 | aead == null || aead.isAead(), |
| 1035 | "Unsupported algorithm for Authenticated Encryption"); |
| 1036 | Preconditions.checkArgument( |
| 1037 | aead == null || (auth == null && crypt == null), |
| 1038 | "Authenticated Encryption is mutually exclusive with other Authentication " |
| 1039 | + "or Encryption algorithms"); |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1042 | /** |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1043 | * Checks an IpSecConfig parcel to ensure that the contents are sane and throws an |
| 1044 | * IllegalArgumentException if they are not. |
| 1045 | */ |
| 1046 | private void checkIpSecConfig(IpSecConfig config) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1047 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1048 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1049 | switch (config.getEncapType()) { |
| 1050 | case IpSecTransform.ENCAP_NONE: |
| 1051 | break; |
| 1052 | case IpSecTransform.ENCAP_ESPINUDP: |
| 1053 | case IpSecTransform.ENCAP_ESPINUDP_NON_IKE: |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1054 | // Retrieve encap socket record; will throw IllegalArgumentException if not found |
| 1055 | userRecord.mEncapSocketRecords.getResourceOrThrow( |
| 1056 | config.getEncapSocketResourceId()); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1057 | |
| 1058 | int port = config.getEncapRemotePort(); |
| 1059 | if (port <= 0 || port > 0xFFFF) { |
| 1060 | throw new IllegalArgumentException("Invalid remote UDP port: " + port); |
| 1061 | } |
| 1062 | break; |
| 1063 | default: |
| 1064 | throw new IllegalArgumentException("Invalid Encap Type: " + config.getEncapType()); |
| 1065 | } |
| 1066 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1067 | validateAlgorithms(config); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1068 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1069 | // Retrieve SPI record; will throw IllegalArgumentException if not found |
| 1070 | SpiRecord s = userRecord.mSpiRecords.getResourceOrThrow(config.getSpiResourceId()); |
| 1071 | |
| 1072 | // If no remote address is supplied, then use one from the SPI. |
| 1073 | if (TextUtils.isEmpty(config.getDestinationAddress())) { |
| 1074 | config.setDestinationAddress(s.getDestinationAddress()); |
| 1075 | } |
| 1076 | |
| 1077 | // All remote addresses must match |
| 1078 | if (!config.getDestinationAddress().equals(s.getDestinationAddress())) { |
| 1079 | throw new IllegalArgumentException("Mismatched remote addresseses."); |
| 1080 | } |
| 1081 | |
| 1082 | // This check is technically redundant due to the chain of custody between the SPI and |
| 1083 | // the IpSecConfig, but in the future if the dest is allowed to be set explicitly in |
| 1084 | // the transform, this will prevent us from messing up. |
| 1085 | checkInetAddress(config.getDestinationAddress()); |
| 1086 | |
| 1087 | // Require a valid source address for all transforms. |
| 1088 | checkInetAddress(config.getSourceAddress()); |
| 1089 | |
| 1090 | switch (config.getMode()) { |
| 1091 | case IpSecTransform.MODE_TRANSPORT: |
| 1092 | case IpSecTransform.MODE_TUNNEL: |
| 1093 | break; |
| 1094 | default: |
| 1095 | throw new IllegalArgumentException( |
| 1096 | "Invalid IpSecTransform.mode: " + config.getMode()); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | /** |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1101 | * Create a transport mode transform, which represent two security associations (one in each |
| 1102 | * direction) in the kernel. The transform will be cached by the system server and must be freed |
| 1103 | * when no longer needed. It is possible to free one, deleting the SA from underneath sockets |
| 1104 | * that are using it, which will result in all of those sockets becoming unable to send or |
| 1105 | * receive data. |
| 1106 | */ |
| 1107 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1108 | public synchronized IpSecTransformResponse createTransportModeTransform( |
| 1109 | IpSecConfig c, IBinder binder) throws RemoteException { |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1110 | checkIpSecConfig(c); |
| 1111 | checkNotNull(binder, "Null Binder passed to createTransportModeTransform"); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 1112 | final int resourceId = mNextResourceId++; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1113 | |
| 1114 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1115 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1116 | // Avoid resizing by creating a dependency array of min-size 2 (1 UDP encap + 1 SPI) |
| 1117 | List<RefcountedResource> dependencies = new ArrayList<>(2); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1118 | |
| 1119 | if (!userRecord.mTransformQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1120 | return new IpSecTransformResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 1121 | } |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1122 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1123 | int encapType, encapLocalPort = 0, encapRemotePort = 0; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1124 | EncapSocketRecord socketRecord = null; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1125 | encapType = c.getEncapType(); |
| 1126 | if (encapType != IpSecTransform.ENCAP_NONE) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1127 | RefcountedResource<EncapSocketRecord> refcountedSocketRecord = |
| 1128 | userRecord.mEncapSocketRecords.getRefcountedResourceOrThrow( |
| 1129 | c.getEncapSocketResourceId()); |
| 1130 | dependencies.add(refcountedSocketRecord); |
| 1131 | |
| 1132 | socketRecord = refcountedSocketRecord.getResource(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1133 | encapLocalPort = socketRecord.getPort(); |
| 1134 | encapRemotePort = c.getEncapRemotePort(); |
| 1135 | } |
| 1136 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1137 | IpSecAlgorithm auth = c.getAuthentication(); |
| 1138 | IpSecAlgorithm crypt = c.getEncryption(); |
| 1139 | IpSecAlgorithm authCrypt = c.getAuthenticatedEncryption(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1140 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1141 | RefcountedResource<SpiRecord> refcountedSpiRecord = |
| 1142 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(c.getSpiResourceId()); |
| 1143 | dependencies.add(refcountedSpiRecord); |
| 1144 | SpiRecord spiRecord = refcountedSpiRecord.getResource(); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1145 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1146 | try { |
| 1147 | mSrvConfig |
| 1148 | .getNetdInstance() |
| 1149 | .ipSecAddSecurityAssociation( |
| 1150 | resourceId, |
| 1151 | c.getMode(), |
| 1152 | c.getSourceAddress(), |
| 1153 | c.getDestinationAddress(), |
| 1154 | (c.getNetwork() != null) ? c.getNetwork().netId : 0, |
| 1155 | spiRecord.getSpi(), |
| 1156 | (auth != null) ? auth.getName() : "", |
| 1157 | (auth != null) ? auth.getKey() : new byte[] {}, |
| 1158 | (auth != null) ? auth.getTruncationLengthBits() : 0, |
| 1159 | (crypt != null) ? crypt.getName() : "", |
| 1160 | (crypt != null) ? crypt.getKey() : new byte[] {}, |
| 1161 | (crypt != null) ? crypt.getTruncationLengthBits() : 0, |
| 1162 | (authCrypt != null) ? authCrypt.getName() : "", |
| 1163 | (authCrypt != null) ? authCrypt.getKey() : new byte[] {}, |
| 1164 | (authCrypt != null) ? authCrypt.getTruncationLengthBits() : 0, |
| 1165 | encapType, |
| 1166 | encapLocalPort, |
| 1167 | encapRemotePort); |
| 1168 | } catch (ServiceSpecificException e) { |
| 1169 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 1170 | return new IpSecTransformResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1171 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1172 | // Both SAs were created successfully, time to construct a record and lock it away |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1173 | userRecord.mTransformRecords.put( |
| 1174 | resourceId, |
| 1175 | new RefcountedResource<TransformRecord>( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1176 | new TransformRecord(resourceId, c, spiRecord, socketRecord), |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1177 | binder, |
| 1178 | dependencies.toArray(new RefcountedResource[dependencies.size()]))); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1179 | return new IpSecTransformResponse(IpSecManager.Status.OK, resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * Delete a transport mode transform that was previously allocated by + registered with the |
| 1184 | * system server. If this is called on an inactive (or non-existent) transform, it will not |
| 1185 | * return an error. It's safe to de-allocate transforms that may have already been deleted for |
| 1186 | * other reasons. |
| 1187 | */ |
| 1188 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1189 | public synchronized void deleteTransportModeTransform(int resourceId) throws RemoteException { |
| 1190 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1191 | releaseResource(userRecord.mTransformRecords, resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Apply an active transport mode transform to a socket, which will apply the IPsec security |
| 1196 | * association as a correspondent policy to the provided socket |
| 1197 | */ |
| 1198 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1199 | public synchronized void applyTransportModeTransform( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1200 | ParcelFileDescriptor socket, int direction, int resourceId) throws RemoteException { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1201 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1202 | checkDirection(direction); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1203 | // Get transform record; if no transform is found, will throw IllegalArgumentException |
| 1204 | TransformRecord info = userRecord.mTransformRecords.getResourceOrThrow(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1205 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1206 | // TODO: make this a function. |
| 1207 | if (info.pid != getCallingPid() || info.uid != getCallingUid()) { |
| 1208 | throw new SecurityException("Only the owner of an IpSec Transform may apply it!"); |
| 1209 | } |
| 1210 | |
| 1211 | IpSecConfig c = info.getConfig(); |
| 1212 | try { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1213 | mSrvConfig |
| 1214 | .getNetdInstance() |
| 1215 | .ipSecApplyTransportModeTransform( |
| 1216 | socket.getFileDescriptor(), |
| 1217 | resourceId, |
| 1218 | direction, |
| 1219 | c.getSourceAddress(), |
| 1220 | c.getDestinationAddress(), |
| 1221 | info.getSpiRecord().getSpi()); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1222 | } catch (ServiceSpecificException e) { |
manojboopathi | c4be79d | 2017-11-30 17:11:49 -0800 | [diff] [blame] | 1223 | if (e.errorCode == EINVAL) { |
| 1224 | throw new IllegalArgumentException(e.toString()); |
| 1225 | } else { |
| 1226 | throw e; |
| 1227 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1228 | } |
| 1229 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1230 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1231 | /** |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1232 | * Remove transport mode transforms from a socket, applying the default (empty) policy. This |
| 1233 | * ensures that NO IPsec policy is applied to the socket (would be the equivalent of applying a |
| 1234 | * policy that performs no IPsec). Today the resourceId parameter is passed but not used: |
| 1235 | * reserved for future improved input validation. |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1236 | */ |
| 1237 | @Override |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame^] | 1238 | public synchronized void removeTransportModeTransforms( |
| 1239 | ParcelFileDescriptor socket, int resourceId) throws RemoteException { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1240 | try { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1241 | mSrvConfig |
| 1242 | .getNetdInstance() |
| 1243 | .ipSecRemoveTransportModeTransform(socket.getFileDescriptor()); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1244 | } catch (ServiceSpecificException e) { |
| 1245 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1250 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1251 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1252 | |
| 1253 | pw.println("IpSecService dump:"); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1254 | pw.println("NetdNativeService Connection: " + (isNetdAlive() ? "alive" : "dead")); |
| 1255 | pw.println(); |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1256 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1257 | pw.println("mUserResourceTracker:"); |
| 1258 | pw.println(mUserResourceTracker); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1259 | } |
| 1260 | } |