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; |
| 22 | import static android.system.OsConstants.IPPROTO_UDP; |
| 23 | import static android.system.OsConstants.SOCK_DGRAM; |
| 24 | import static com.android.internal.util.Preconditions.checkNotNull; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 25 | |
| 26 | import android.content.Context; |
| 27 | import android.net.IIpSecService; |
| 28 | import android.net.INetd; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 29 | import android.net.IpSecAlgorithm; |
| 30 | import android.net.IpSecConfig; |
| 31 | import android.net.IpSecManager; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 32 | import android.net.IpSecSpiResponse; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 33 | import android.net.IpSecTransform; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 34 | import android.net.IpSecTransformResponse; |
| 35 | import android.net.IpSecUdpEncapResponse; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 36 | import android.net.util.NetdService; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 37 | import android.os.Binder; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 38 | import android.os.IBinder; |
| 39 | import android.os.ParcelFileDescriptor; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 40 | import android.os.RemoteException; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 41 | import android.os.ServiceSpecificException; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 42 | import android.system.ErrnoException; |
| 43 | import android.system.Os; |
| 44 | import android.system.OsConstants; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 45 | import android.util.Log; |
| 46 | import android.util.Slog; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 47 | import android.util.SparseArray; |
| 48 | import com.android.internal.annotations.GuardedBy; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 49 | import java.io.FileDescriptor; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 50 | import java.io.IOException; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 51 | import java.io.PrintWriter; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 52 | import java.net.InetAddress; |
| 53 | import java.net.InetSocketAddress; |
| 54 | import java.net.UnknownHostException; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 55 | import java.util.concurrent.atomic.AtomicInteger; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 56 | import libcore.io.IoUtils; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 57 | |
| 58 | /** @hide */ |
| 59 | public class IpSecService extends IIpSecService.Stub { |
| 60 | private static final String TAG = "IpSecService"; |
| 61 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 62 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 63 | private static final String NETD_SERVICE_NAME = "netd"; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 64 | private static final int[] DIRECTIONS = |
| 65 | new int[] {IpSecTransform.DIRECTION_OUT, IpSecTransform.DIRECTION_IN}; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 66 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 67 | private static final int NETD_FETCH_TIMEOUT = 5000; //ms |
| 68 | private static final int MAX_PORT_BIND_ATTEMPTS = 10; |
| 69 | private static final InetAddress INADDR_ANY; |
| 70 | |
| 71 | static { |
| 72 | try { |
| 73 | INADDR_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0}); |
| 74 | } catch (UnknownHostException e) { |
| 75 | throw new RuntimeException(e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static final int FREE_PORT_MIN = 1024; // ports 1-1023 are reserved |
| 80 | static final int PORT_MAX = 0xFFFF; // ports are an unsigned 16-bit integer |
| 81 | |
| 82 | /* Binder context for this service */ |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 83 | private final Context mContext; |
| 84 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 85 | /** Should be a never-repeating global ID for resources */ |
| 86 | private static AtomicInteger mNextResourceId = new AtomicInteger(0x00FADED0); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 87 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 88 | @GuardedBy("this") |
| 89 | private final ManagedResourceArray<SpiRecord> mSpiRecords = new ManagedResourceArray<>(); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 90 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 91 | @GuardedBy("this") |
| 92 | private final ManagedResourceArray<TransformRecord> mTransformRecords = |
| 93 | new ManagedResourceArray<>(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 94 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 95 | @GuardedBy("this") |
| 96 | private final ManagedResourceArray<UdpSocketRecord> mUdpSocketRecords = |
| 97 | new ManagedResourceArray<>(); |
| 98 | |
| 99 | /** |
| 100 | * The ManagedResource class provides a facility to cleanly and reliably release system |
| 101 | * resources. It relies on two things: an IBinder that allows ManagedResource to automatically |
| 102 | * clean up in the event that the Binder dies and a user-provided resourceId that should |
| 103 | * uniquely identify the managed resource. To use this class, the user should implement the |
| 104 | * releaseResources() method that is responsible for releasing system resources when invoked. |
| 105 | */ |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 106 | private abstract class ManagedResource implements IBinder.DeathRecipient { |
| 107 | final int pid; |
| 108 | final int uid; |
| 109 | private IBinder mBinder; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 110 | protected int mResourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 111 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 112 | private AtomicInteger mReferenceCount = new AtomicInteger(0); |
| 113 | |
| 114 | ManagedResource(int resourceId, IBinder binder) { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 115 | super(); |
| 116 | mBinder = binder; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 117 | mResourceId = resourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 118 | pid = Binder.getCallingPid(); |
| 119 | uid = Binder.getCallingUid(); |
| 120 | |
| 121 | try { |
| 122 | mBinder.linkToDeath(this, 0); |
| 123 | } catch (RemoteException e) { |
| 124 | binderDied(); |
| 125 | } |
| 126 | } |
| 127 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 128 | public void addReference() { |
| 129 | mReferenceCount.incrementAndGet(); |
| 130 | } |
| 131 | |
| 132 | public void removeReference() { |
| 133 | if (mReferenceCount.decrementAndGet() < 0) { |
| 134 | Log.wtf(TAG, "Programming error: negative reference count"); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public boolean isReferenced() { |
| 139 | return (mReferenceCount.get() > 0); |
| 140 | } |
| 141 | |
| 142 | public void checkOwnerOrSystemAndThrow() { |
| 143 | if (uid != Binder.getCallingUid() |
| 144 | && android.os.Process.SYSTEM_UID != Binder.getCallingUid()) { |
| 145 | throw new SecurityException("Only the owner may access managed resources!"); |
| 146 | } |
| 147 | } |
| 148 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 149 | /** |
| 150 | * When this record is no longer needed for managing system resources this function should |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 151 | * clean up all system resources and nullify the record. This function shall perform all |
| 152 | * necessary cleanup of the resources managed by this record. |
| 153 | * |
| 154 | * <p>NOTE: this function verifies ownership before allowing resources to be freed. |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 155 | */ |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 156 | public final void release() throws RemoteException { |
| 157 | synchronized (IpSecService.this) { |
| 158 | if (isReferenced()) { |
| 159 | throw new IllegalStateException( |
| 160 | "Cannot release a resource that has active references!"); |
| 161 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 162 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 163 | if (mResourceId == INVALID_RESOURCE_ID) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | releaseResources(); |
| 168 | if (mBinder != null) { |
| 169 | mBinder.unlinkToDeath(this, 0); |
| 170 | } |
| 171 | mBinder = null; |
| 172 | |
| 173 | mResourceId = INVALID_RESOURCE_ID; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 174 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * If the Binder object dies, this function is called to free the system resources that are |
| 179 | * being managed by this record and to subsequently release this record for garbage |
| 180 | * collection |
| 181 | */ |
| 182 | public final void binderDied() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 183 | try { |
| 184 | release(); |
| 185 | } catch (Exception e) { |
| 186 | Log.e(TAG, "Failed to release resource: " + e); |
| 187 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 191 | * Implement this method to release all system resources that are being protected by this |
| 192 | * record. Once the resources are released, the record should be invalidated and no longer |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 193 | * used by calling release(). This should NEVER be called directly. |
| 194 | * |
| 195 | * <p>Calls to this are always guarded by IpSecService#this |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 196 | */ |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 197 | protected abstract void releaseResources() throws RemoteException; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 198 | }; |
| 199 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Minimal wrapper around SparseArray that performs ownership |
| 202 | * validation on element accesses. |
| 203 | */ |
| 204 | private class ManagedResourceArray<T extends ManagedResource> { |
| 205 | SparseArray<T> mArray = new SparseArray<>(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 206 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 207 | T get(int key) { |
| 208 | T val = mArray.get(key); |
| 209 | val.checkOwnerOrSystemAndThrow(); |
| 210 | return val; |
| 211 | } |
| 212 | |
| 213 | void put(int key, T obj) { |
| 214 | checkNotNull(obj, "Null resources cannot be added"); |
| 215 | mArray.put(key, obj); |
| 216 | } |
| 217 | |
| 218 | void remove(int key) { |
| 219 | mArray.remove(key); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private final class TransformRecord extends ManagedResource { |
| 224 | private final IpSecConfig mConfig; |
| 225 | private final SpiRecord[] mSpis; |
| 226 | private final UdpSocketRecord mSocket; |
| 227 | |
| 228 | TransformRecord( |
| 229 | int resourceId, |
| 230 | IBinder binder, |
| 231 | IpSecConfig config, |
| 232 | SpiRecord[] spis, |
| 233 | UdpSocketRecord socket) { |
| 234 | super(resourceId, binder); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 235 | mConfig = config; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 236 | mSpis = spis; |
| 237 | mSocket = socket; |
| 238 | |
| 239 | for (int direction : DIRECTIONS) { |
| 240 | mSpis[direction].addReference(); |
| 241 | mSpis[direction].setOwnedByTransform(); |
| 242 | } |
| 243 | |
| 244 | if (mSocket != null) { |
| 245 | mSocket.addReference(); |
| 246 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | public IpSecConfig getConfig() { |
| 250 | return mConfig; |
| 251 | } |
| 252 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 253 | public SpiRecord getSpiRecord(int direction) { |
| 254 | return mSpis[direction]; |
| 255 | } |
| 256 | |
| 257 | /** always guarded by IpSecService#this */ |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 258 | @Override |
| 259 | protected void releaseResources() { |
| 260 | for (int direction : DIRECTIONS) { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 261 | int spi = mSpis[direction].getSpi(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 262 | try { |
| 263 | getNetdInstance() |
| 264 | .ipSecDeleteSecurityAssociation( |
| 265 | mResourceId, |
| 266 | direction, |
| 267 | (mConfig.getLocalAddress() != null) |
| 268 | ? mConfig.getLocalAddress().getHostAddress() |
| 269 | : "", |
| 270 | (mConfig.getRemoteAddress() != null) |
| 271 | ? mConfig.getRemoteAddress().getHostAddress() |
| 272 | : "", |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 273 | spi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 274 | } catch (ServiceSpecificException e) { |
| 275 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 276 | } catch (RemoteException e) { |
| 277 | Log.e(TAG, "Failed to delete SA with ID: " + mResourceId); |
| 278 | } |
| 279 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 280 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 281 | for (int direction : DIRECTIONS) { |
| 282 | mSpis[direction].removeReference(); |
| 283 | } |
| 284 | |
| 285 | if (mSocket != null) { |
| 286 | mSocket.removeReference(); |
| 287 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | private final class SpiRecord extends ManagedResource { |
| 292 | private final int mDirection; |
| 293 | private final String mLocalAddress; |
| 294 | private final String mRemoteAddress; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 295 | private int mSpi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 296 | |
| 297 | private boolean mOwnedByTransform = false; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 298 | |
| 299 | SpiRecord( |
| 300 | int resourceId, |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 301 | IBinder binder, |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 302 | int direction, |
| 303 | String localAddress, |
| 304 | String remoteAddress, |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 305 | int spi) { |
| 306 | super(resourceId, binder); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 307 | mDirection = direction; |
| 308 | mLocalAddress = localAddress; |
| 309 | mRemoteAddress = remoteAddress; |
| 310 | mSpi = spi; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 313 | /** always guarded by IpSecService#this */ |
| 314 | @Override |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 315 | protected void releaseResources() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 316 | if (mOwnedByTransform) { |
| 317 | Log.d(TAG, "Cannot release Spi " + mSpi + ": Currently locked by a Transform"); |
| 318 | // Because SPIs are "handed off" to transform, objects, they should never be |
| 319 | // freed from the SpiRecord once used in a transform. (They refer to the same SA, |
| 320 | // thus ownership and responsibility for freeing these resources passes to the |
| 321 | // Transform object). Thus, we should let the user free them without penalty once |
| 322 | // they are applied in a Transform object. |
| 323 | return; |
| 324 | } |
| 325 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 326 | try { |
| 327 | getNetdInstance() |
| 328 | .ipSecDeleteSecurityAssociation( |
| 329 | mResourceId, mDirection, mLocalAddress, mRemoteAddress, mSpi); |
| 330 | } catch (ServiceSpecificException e) { |
| 331 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 332 | } catch (RemoteException e) { |
| 333 | Log.e(TAG, "Failed to delete SPI reservation with ID: " + mResourceId); |
| 334 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 335 | |
| 336 | mSpi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 339 | public int getSpi() { |
| 340 | return mSpi; |
| 341 | } |
| 342 | |
| 343 | public void setOwnedByTransform() { |
| 344 | if (mOwnedByTransform) { |
| 345 | // Programming error |
| 346 | new IllegalStateException("Cannot own an SPI twice!"); |
| 347 | } |
| 348 | |
| 349 | mOwnedByTransform = true; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 353 | private final class UdpSocketRecord extends ManagedResource { |
| 354 | private FileDescriptor mSocket; |
| 355 | private final int mPort; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 356 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 357 | UdpSocketRecord(int resourceId, IBinder binder, FileDescriptor socket, int port) { |
| 358 | super(resourceId, binder); |
| 359 | mSocket = socket; |
| 360 | mPort = port; |
| 361 | } |
| 362 | |
| 363 | /** always guarded by IpSecService#this */ |
| 364 | @Override |
| 365 | protected void releaseResources() { |
| 366 | Log.d(TAG, "Closing port " + mPort); |
| 367 | IoUtils.closeQuietly(mSocket); |
| 368 | mSocket = null; |
| 369 | } |
| 370 | |
| 371 | public int getPort() { |
| 372 | return mPort; |
| 373 | } |
| 374 | |
| 375 | public FileDescriptor getSocket() { |
| 376 | return mSocket; |
| 377 | } |
| 378 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 379 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 380 | /** |
| 381 | * Constructs a new IpSecService instance |
| 382 | * |
| 383 | * @param context Binder context for this service |
| 384 | */ |
| 385 | private IpSecService(Context context) { |
| 386 | mContext = context; |
| 387 | } |
| 388 | |
| 389 | static IpSecService create(Context context) throws InterruptedException { |
| 390 | final IpSecService service = new IpSecService(context); |
| 391 | service.connectNativeNetdService(); |
| 392 | return service; |
| 393 | } |
| 394 | |
| 395 | public void systemReady() { |
| 396 | if (isNetdAlive()) { |
| 397 | Slog.d(TAG, "IpSecService is ready"); |
| 398 | } else { |
| 399 | Slog.wtf(TAG, "IpSecService not ready: failed to connect to NetD Native Service!"); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | private void connectNativeNetdService() { |
| 404 | // Avoid blocking the system server to do this |
| 405 | Thread t = |
| 406 | new Thread( |
| 407 | new Runnable() { |
| 408 | @Override |
| 409 | public void run() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 410 | synchronized (IpSecService.this) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 411 | NetdService.get(NETD_FETCH_TIMEOUT); |
| 412 | } |
| 413 | } |
| 414 | }); |
| 415 | t.run(); |
| 416 | } |
| 417 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 418 | INetd getNetdInstance() throws RemoteException { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 419 | final INetd netd = NetdService.getInstance(); |
| 420 | if (netd == null) { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 421 | throw new RemoteException("Failed to Get Netd Instance"); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 422 | } |
| 423 | return netd; |
| 424 | } |
| 425 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 426 | synchronized boolean isNetdAlive() { |
| 427 | try { |
| 428 | final INetd netd = getNetdInstance(); |
| 429 | if (netd == null) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 430 | return false; |
| 431 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 432 | return netd.isAlive(); |
| 433 | } catch (RemoteException re) { |
| 434 | return false; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | @Override |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 439 | /** Get a new SPI and maintain the reservation in the system server */ |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 440 | public synchronized IpSecSpiResponse reserveSecurityParameterIndex( |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 441 | int direction, String remoteAddress, int requestedSpi, IBinder binder) |
| 442 | throws RemoteException { |
| 443 | int resourceId = mNextResourceId.getAndIncrement(); |
| 444 | |
| 445 | int spi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
| 446 | String localAddress = ""; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 447 | try { |
| 448 | spi = |
| 449 | getNetdInstance() |
| 450 | .ipSecAllocateSpi( |
| 451 | resourceId, |
| 452 | direction, |
| 453 | localAddress, |
| 454 | remoteAddress, |
| 455 | requestedSpi); |
| 456 | Log.d(TAG, "Allocated SPI " + spi); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 457 | mSpiRecords.put( |
| 458 | resourceId, |
| 459 | new SpiRecord(resourceId, binder, direction, localAddress, remoteAddress, spi)); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 460 | } catch (ServiceSpecificException e) { |
| 461 | // TODO: Add appropriate checks when other ServiceSpecificException types are supported |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 462 | return new IpSecSpiResponse( |
| 463 | IpSecManager.Status.SPI_UNAVAILABLE, IpSecManager.INVALID_RESOURCE_ID, spi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 464 | } catch (RemoteException e) { |
| 465 | throw e.rethrowFromSystemServer(); |
| 466 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 467 | return new IpSecSpiResponse(IpSecManager.Status.OK, resourceId, spi); |
| 468 | } |
| 469 | |
| 470 | /* This method should only be called from Binder threads. Do not call this from |
| 471 | * within the system server as it will crash the system on failure. |
| 472 | */ |
| 473 | private synchronized <T extends ManagedResource> void releaseManagedResource( |
| 474 | ManagedResourceArray<T> resArray, int resourceId, String typeName) |
| 475 | throws RemoteException { |
| 476 | // We want to non-destructively get so that we can check credentials before removing |
| 477 | // this from the records. |
| 478 | T record = resArray.get(resourceId); |
| 479 | |
| 480 | if (record == null) { |
| 481 | throw new IllegalArgumentException( |
| 482 | typeName + " " + resourceId + " is not available to be deleted"); |
| 483 | } |
| 484 | |
| 485 | record.release(); |
| 486 | resArray.remove(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** Release a previously allocated SPI that has been registered with the system server */ |
| 490 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 491 | public void releaseSecurityParameterIndex(int resourceId) throws RemoteException { |
| 492 | releaseManagedResource(mSpiRecords, resourceId, "SecurityParameterIndex"); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * This function finds and forcibly binds to a random system port, ensuring that the port cannot |
| 497 | * be unbound. |
| 498 | * |
| 499 | * <p>A socket cannot be un-bound from a port if it was bound to that port by number. To select |
| 500 | * a random open port and then bind by number, this function creates a temp socket, binds to a |
| 501 | * random port (specifying 0), gets that port number, and then uses is to bind the user's UDP |
| 502 | * Encapsulation Socket forcibly, so that it cannot be un-bound by the user with the returned |
| 503 | * FileHandle. |
| 504 | * |
| 505 | * <p>The loop in this function handles the inherent race window between un-binding to a port |
| 506 | * and re-binding, during which the system could *technically* hand that port out to someone |
| 507 | * else. |
| 508 | */ |
| 509 | private void bindToRandomPort(FileDescriptor sockFd) throws IOException { |
| 510 | for (int i = MAX_PORT_BIND_ATTEMPTS; i > 0; i--) { |
| 511 | try { |
| 512 | FileDescriptor probeSocket = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 513 | Os.bind(probeSocket, INADDR_ANY, 0); |
| 514 | int port = ((InetSocketAddress) Os.getsockname(probeSocket)).getPort(); |
| 515 | Os.close(probeSocket); |
| 516 | Log.v(TAG, "Binding to port " + port); |
| 517 | Os.bind(sockFd, INADDR_ANY, port); |
| 518 | return; |
| 519 | } catch (ErrnoException e) { |
| 520 | // Someone miraculously claimed the port just after we closed probeSocket. |
| 521 | if (e.errno == OsConstants.EADDRINUSE) { |
| 522 | continue; |
| 523 | } |
| 524 | throw e.rethrowAsIOException(); |
| 525 | } |
| 526 | } |
| 527 | throw new IOException("Failed " + MAX_PORT_BIND_ATTEMPTS + " attempts to bind to a port"); |
| 528 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * Open a socket via the system server and bind it to the specified port (random if port=0). |
| 532 | * This will return a PFD to the user that represent a bound UDP socket. The system server will |
| 533 | * cache the socket and a record of its owner so that it can and must be freed when no longer |
| 534 | * needed. |
| 535 | */ |
| 536 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 537 | public synchronized IpSecUdpEncapResponse openUdpEncapsulationSocket(int port, IBinder binder) |
| 538 | throws RemoteException { |
| 539 | if (port != 0 && (port < FREE_PORT_MIN || port > PORT_MAX)) { |
| 540 | throw new IllegalArgumentException( |
| 541 | "Specified port number must be a valid non-reserved UDP port"); |
| 542 | } |
| 543 | int resourceId = mNextResourceId.getAndIncrement(); |
| 544 | FileDescriptor sockFd = null; |
| 545 | try { |
| 546 | sockFd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 547 | |
| 548 | if (port != 0) { |
| 549 | Log.v(TAG, "Binding to port " + port); |
| 550 | Os.bind(sockFd, INADDR_ANY, port); |
| 551 | } else { |
| 552 | bindToRandomPort(sockFd); |
| 553 | } |
| 554 | // This code is common to both the unspecified and specified port cases |
| 555 | Os.setsockoptInt( |
| 556 | sockFd, |
| 557 | OsConstants.IPPROTO_UDP, |
| 558 | OsConstants.UDP_ENCAP, |
| 559 | OsConstants.UDP_ENCAP_ESPINUDP); |
| 560 | |
| 561 | mUdpSocketRecords.put( |
| 562 | resourceId, new UdpSocketRecord(resourceId, binder, sockFd, port)); |
| 563 | return new IpSecUdpEncapResponse(IpSecManager.Status.OK, resourceId, port, sockFd); |
| 564 | } catch (IOException | ErrnoException e) { |
| 565 | IoUtils.closeQuietly(sockFd); |
| 566 | } |
| 567 | // If we make it to here, then something has gone wrong and we couldn't open a socket. |
| 568 | // The only reasonable condition that would cause that is resource unavailable. |
| 569 | return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** close a socket that has been been allocated by and registered with the system server */ |
| 573 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 574 | public void closeUdpEncapsulationSocket(int resourceId) throws RemoteException { |
| 575 | |
| 576 | releaseManagedResource(mUdpSocketRecords, resourceId, "UdpEncapsulationSocket"); |
| 577 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 578 | |
| 579 | /** |
| 580 | * Create a transport mode transform, which represent two security associations (one in each |
| 581 | * direction) in the kernel. The transform will be cached by the system server and must be freed |
| 582 | * when no longer needed. It is possible to free one, deleting the SA from underneath sockets |
| 583 | * that are using it, which will result in all of those sockets becoming unable to send or |
| 584 | * receive data. |
| 585 | */ |
| 586 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 587 | public synchronized IpSecTransformResponse createTransportModeTransform( |
| 588 | IpSecConfig c, IBinder binder) throws RemoteException { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 589 | int resourceId = mNextResourceId.getAndIncrement(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 590 | SpiRecord[] spis = new SpiRecord[DIRECTIONS.length]; |
| 591 | // TODO: Basic input validation here since it's coming over the Binder |
| 592 | int encapType, encapLocalPort = 0, encapRemotePort = 0; |
| 593 | UdpSocketRecord socketRecord = null; |
| 594 | encapType = c.getEncapType(); |
| 595 | if (encapType != IpSecTransform.ENCAP_NONE) { |
| 596 | socketRecord = mUdpSocketRecords.get(c.getEncapLocalResourceId()); |
| 597 | encapLocalPort = socketRecord.getPort(); |
| 598 | encapRemotePort = c.getEncapRemotePort(); |
| 599 | } |
| 600 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 601 | for (int direction : DIRECTIONS) { |
| 602 | IpSecAlgorithm auth = c.getAuthentication(direction); |
| 603 | IpSecAlgorithm crypt = c.getEncryption(direction); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 604 | |
| 605 | spis[direction] = mSpiRecords.get(c.getSpiResourceId(direction)); |
| 606 | int spi = spis[direction].getSpi(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 607 | try { |
ludi | 0f80789 | 2017-05-20 14:15:09 -0700 | [diff] [blame^] | 608 | getNetdInstance() |
| 609 | .ipSecAddSecurityAssociation( |
| 610 | resourceId, |
| 611 | c.getMode(), |
| 612 | direction, |
| 613 | (c.getLocalAddress() != null) |
| 614 | ? c.getLocalAddress().getHostAddress() |
| 615 | : "", |
| 616 | (c.getRemoteAddress() != null) |
| 617 | ? c.getRemoteAddress().getHostAddress() |
| 618 | : "", |
| 619 | (c.getNetwork() != null) |
| 620 | ? c.getNetwork().getNetworkHandle() |
| 621 | : 0, |
| 622 | spi, |
| 623 | (auth != null) ? auth.getName() : "", |
| 624 | (auth != null) ? auth.getKey() : null, |
| 625 | (auth != null) ? auth.getTruncationLengthBits() : 0, |
| 626 | (crypt != null) ? crypt.getName() : "", |
| 627 | (crypt != null) ? crypt.getKey() : null, |
| 628 | (crypt != null) ? crypt.getTruncationLengthBits() : 0, |
| 629 | encapType, |
| 630 | encapLocalPort, |
| 631 | encapRemotePort); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 632 | } catch (ServiceSpecificException e) { |
| 633 | // 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^] | 634 | return new IpSecTransformResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 635 | } |
| 636 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 637 | // Both SAs were created successfully, time to construct a record and lock it away |
| 638 | mTransformRecords.put( |
| 639 | resourceId, new TransformRecord(resourceId, binder, c, spis, socketRecord)); |
| 640 | return new IpSecTransformResponse(IpSecManager.Status.OK, resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Delete a transport mode transform that was previously allocated by + registered with the |
| 645 | * system server. If this is called on an inactive (or non-existent) transform, it will not |
| 646 | * return an error. It's safe to de-allocate transforms that may have already been deleted for |
| 647 | * other reasons. |
| 648 | */ |
| 649 | @Override |
| 650 | public void deleteTransportModeTransform(int resourceId) throws RemoteException { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 651 | releaseManagedResource(mTransformRecords, resourceId, "IpSecTransform"); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Apply an active transport mode transform to a socket, which will apply the IPsec security |
| 656 | * association as a correspondent policy to the provided socket |
| 657 | */ |
| 658 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 659 | public synchronized void applyTransportModeTransform( |
| 660 | ParcelFileDescriptor socket, int resourceId) throws RemoteException { |
| 661 | // Synchronize liberally here because we are using ManagedResources in this block |
| 662 | TransformRecord info; |
| 663 | // FIXME: this code should be factored out into a security check + getter |
| 664 | info = mTransformRecords.get(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 665 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 666 | if (info == null) { |
| 667 | throw new IllegalArgumentException("Transform " + resourceId + " is not active"); |
| 668 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 669 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 670 | // TODO: make this a function. |
| 671 | if (info.pid != getCallingPid() || info.uid != getCallingUid()) { |
| 672 | throw new SecurityException("Only the owner of an IpSec Transform may apply it!"); |
| 673 | } |
| 674 | |
| 675 | IpSecConfig c = info.getConfig(); |
| 676 | try { |
| 677 | for (int direction : DIRECTIONS) { |
| 678 | getNetdInstance() |
| 679 | .ipSecApplyTransportModeTransform( |
| 680 | socket.getFileDescriptor(), |
| 681 | resourceId, |
| 682 | direction, |
| 683 | (c.getLocalAddress() != null) |
| 684 | ? c.getLocalAddress().getHostAddress() |
| 685 | : "", |
| 686 | (c.getRemoteAddress() != null) |
| 687 | ? c.getRemoteAddress().getHostAddress() |
| 688 | : "", |
| 689 | info.getSpiRecord(direction).getSpi()); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 690 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 691 | } catch (ServiceSpecificException e) { |
| 692 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 693 | } |
| 694 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 695 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 696 | /** |
| 697 | * Remove a transport mode transform from a socket, applying the default (empty) policy. This |
| 698 | * will ensure that NO IPsec policy is applied to the socket (would be the equivalent of |
| 699 | * applying a policy that performs no IPsec). Today the resourceId parameter is passed but not |
| 700 | * used: reserved for future improved input validation. |
| 701 | */ |
| 702 | @Override |
| 703 | public void removeTransportModeTransform(ParcelFileDescriptor socket, int resourceId) |
| 704 | throws RemoteException { |
| 705 | try { |
| 706 | getNetdInstance().ipSecRemoveTransportModeTransform(socket.getFileDescriptor()); |
| 707 | } catch (ServiceSpecificException e) { |
| 708 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | @Override |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 713 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 714 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 715 | // TODO: Add dump code to print out a log of all the resources being tracked |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 716 | pw.println("IpSecService Log:"); |
| 717 | pw.println("NetdNativeService Connection: " + (isNetdAlive() ? "alive" : "dead")); |
| 718 | pw.println(); |
| 719 | } |
| 720 | } |