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