Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 17 | package com.android.server; |
| 18 | |
| 19 | import android.Manifest; |
Guang Zhu | 514c580 | 2014-09-12 15:14:00 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.os.Binder; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.SystemProperties; |
Andres Morales | 6429f31 | 2014-08-04 16:35:15 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Xiaohui Chen | f066078 | 2015-09-02 14:25:19 -0700 | [diff] [blame] | 28 | import android.os.UserManager; |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame^] | 29 | import android.provider.Settings; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 30 | import android.service.persistentdata.IPersistentDataBlockService; |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 31 | import android.service.persistentdata.PersistentDataBlockManager; |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 32 | import android.util.Slog; |
Guang Zhu | 514c580 | 2014-09-12 15:14:00 -0700 | [diff] [blame] | 33 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 34 | import com.android.internal.R; |
Guang Zhu | 514c580 | 2014-09-12 15:14:00 -0700 | [diff] [blame] | 35 | |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 36 | import libcore.io.IoUtils; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 37 | |
| 38 | import java.io.DataInputStream; |
| 39 | import java.io.DataOutputStream; |
| 40 | import java.io.File; |
| 41 | import java.io.FileInputStream; |
| 42 | import java.io.FileNotFoundException; |
| 43 | import java.io.FileOutputStream; |
| 44 | import java.io.IOException; |
| 45 | import java.nio.ByteBuffer; |
| 46 | import java.nio.channels.FileChannel; |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 47 | import java.security.MessageDigest; |
| 48 | import java.security.NoSuchAlgorithmException; |
| 49 | import java.util.Arrays; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Service for reading and writing blocks to a persistent partition. |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 53 | * This data will live across factory resets not initiated via the Settings UI. |
| 54 | * When a device is factory reset through Settings this data is wiped. |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 55 | * |
| 56 | * Allows writing one block at a time. Namely, each time |
| 57 | * {@link android.service.persistentdata.IPersistentDataBlockService}.write(byte[] data) |
| 58 | * is called, it will overwite the data that was previously written on the block. |
| 59 | * |
| 60 | * Clients can query the size of the currently written block via |
| 61 | * {@link android.service.persistentdata.IPersistentDataBlockService}.getTotalDataSize(). |
| 62 | * |
| 63 | * Clients can any number of bytes from the currently written block up to its total size by invoking |
| 64 | * {@link android.service.persistentdata.IPersistentDataBlockService}.read(byte[] data) |
| 65 | */ |
| 66 | public class PersistentDataBlockService extends SystemService { |
| 67 | private static final String TAG = PersistentDataBlockService.class.getSimpleName(); |
| 68 | |
| 69 | private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst"; |
| 70 | private static final int HEADER_SIZE = 8; |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 71 | // Magic number to mark block device as adhering to the format consumed by this service |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 72 | private static final int PARTITION_TYPE_MARKER = 0x19901873; |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 73 | // Limit to 100k as blocks larger than this might cause strain on Binder. |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 74 | private static final int MAX_DATA_BLOCK_SIZE = 1024 * 100; |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 75 | public static final int DIGEST_SIZE_BYTES = 32; |
Andres Morales | 5ca4cc5 | 2015-03-19 16:37:54 -0700 | [diff] [blame] | 76 | private static final String OEM_UNLOCK_PROP = "sys.oem_unlock_allowed"; |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 77 | private static final String FLASH_LOCK_PROP = "ro.boot.flash.locked"; |
| 78 | private static final String FLASH_LOCK_LOCKED = "1"; |
| 79 | private static final String FLASH_LOCK_UNLOCKED = "0"; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 80 | |
| 81 | private final Context mContext; |
| 82 | private final String mDataBlockFile; |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 83 | private final Object mLock = new Object(); |
Andres Morales | 6429f31 | 2014-08-04 16:35:15 -0700 | [diff] [blame] | 84 | |
Andres Morales | a31c23d | 2014-10-30 15:31:31 -0700 | [diff] [blame] | 85 | private int mAllowedUid = -1; |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 86 | private long mBlockDeviceSize; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 87 | |
| 88 | public PersistentDataBlockService(Context context) { |
| 89 | super(context); |
| 90 | mContext = context; |
| 91 | mDataBlockFile = SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP); |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 92 | mBlockDeviceSize = -1; // Load lazily |
Xiaohui Chen | f066078 | 2015-09-02 14:25:19 -0700 | [diff] [blame] | 93 | mAllowedUid = getAllowedUid(UserHandle.USER_SYSTEM); |
Andres Morales | 6429f31 | 2014-08-04 16:35:15 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Andres Morales | a31c23d | 2014-10-30 15:31:31 -0700 | [diff] [blame] | 96 | private int getAllowedUid(int userHandle) { |
Andres Morales | 6429f31 | 2014-08-04 16:35:15 -0700 | [diff] [blame] | 97 | String allowedPackage = mContext.getResources() |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 98 | .getString(R.string.config_persistentDataPackageName); |
| 99 | PackageManager pm = mContext.getPackageManager(); |
| 100 | int allowedUid = -1; |
| 101 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 102 | allowedUid = pm.getPackageUidAsUser(allowedPackage, |
| 103 | PackageManager.MATCH_SYSTEM_ONLY, userHandle); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 104 | } catch (PackageManager.NameNotFoundException e) { |
| 105 | // not expected |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 106 | Slog.e(TAG, "not able to find package " + allowedPackage, e); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 107 | } |
Andres Morales | a31c23d | 2014-10-30 15:31:31 -0700 | [diff] [blame] | 108 | return allowedUid; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onStart() { |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 113 | enforceChecksumValidity(); |
Andres Morales | 1ce7d17 | 2015-01-07 14:24:57 -0800 | [diff] [blame] | 114 | formatIfOemUnlockEnabled(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 115 | publishBinderService(Context.PERSISTENT_DATA_BLOCK_SERVICE, mService); |
| 116 | } |
| 117 | |
Andres Morales | 1ce7d17 | 2015-01-07 14:24:57 -0800 | [diff] [blame] | 118 | private void formatIfOemUnlockEnabled() { |
Andres Morales | 5ca4cc5 | 2015-03-19 16:37:54 -0700 | [diff] [blame] | 119 | boolean enabled = doGetOemUnlockEnabled(); |
| 120 | if (enabled) { |
Andres Morales | 1ce7d17 | 2015-01-07 14:24:57 -0800 | [diff] [blame] | 121 | synchronized (mLock) { |
Andres Morales | c8f952c | 2015-03-19 08:34:55 -0700 | [diff] [blame] | 122 | formatPartitionLocked(true); |
Andres Morales | 1ce7d17 | 2015-01-07 14:24:57 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
Andres Morales | 5ca4cc5 | 2015-03-19 16:37:54 -0700 | [diff] [blame] | 125 | |
| 126 | SystemProperties.set(OEM_UNLOCK_PROP, enabled ? "1" : "0"); |
Andres Morales | 1ce7d17 | 2015-01-07 14:24:57 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 129 | private void enforceOemUnlockPermission() { |
| 130 | mContext.enforceCallingOrSelfPermission( |
| 131 | Manifest.permission.OEM_UNLOCK_STATE, |
| 132 | "Can't access OEM unlock state"); |
| 133 | } |
| 134 | |
| 135 | private void enforceUid(int callingUid) { |
Andres Morales | a31c23d | 2014-10-30 15:31:31 -0700 | [diff] [blame] | 136 | if (callingUid != mAllowedUid) { |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 137 | throw new SecurityException("uid " + callingUid + " not allowed to access PST"); |
| 138 | } |
| 139 | } |
| 140 | |
Xiaohui Chen | f066078 | 2015-09-02 14:25:19 -0700 | [diff] [blame] | 141 | private void enforceIsAdmin() { |
| 142 | final int userId = UserHandle.getCallingUserId(); |
| 143 | final boolean isAdmin = UserManager.get(mContext).isUserAdmin(userId); |
| 144 | if (!isAdmin) { |
| 145 | throw new SecurityException( |
| 146 | "Only the Admin user is allowed to change OEM unlock state"); |
Andres Morales | a31c23d | 2014-10-30 15:31:31 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 149 | private int getTotalDataSizeLocked(DataInputStream inputStream) throws IOException { |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 150 | // skip over checksum |
| 151 | inputStream.skipBytes(DIGEST_SIZE_BYTES); |
| 152 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 153 | int totalDataSize; |
| 154 | int blockId = inputStream.readInt(); |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 155 | if (blockId == PARTITION_TYPE_MARKER) { |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 156 | totalDataSize = inputStream.readInt(); |
| 157 | } else { |
| 158 | totalDataSize = 0; |
| 159 | } |
| 160 | return totalDataSize; |
| 161 | } |
| 162 | |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 163 | private long getBlockDeviceSize() { |
| 164 | synchronized (mLock) { |
| 165 | if (mBlockDeviceSize == -1) { |
| 166 | mBlockDeviceSize = nativeGetBlockDeviceSize(mDataBlockFile); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | return mBlockDeviceSize; |
| 171 | } |
| 172 | |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 173 | private boolean enforceChecksumValidity() { |
| 174 | byte[] storedDigest = new byte[DIGEST_SIZE_BYTES]; |
| 175 | |
| 176 | synchronized (mLock) { |
| 177 | byte[] digest = computeDigestLocked(storedDigest); |
| 178 | if (digest == null || !Arrays.equals(storedDigest, digest)) { |
| 179 | Slog.i(TAG, "Formatting FRP partition..."); |
Andres Morales | c8f952c | 2015-03-19 08:34:55 -0700 | [diff] [blame] | 180 | formatPartitionLocked(false); |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | private boolean computeAndWriteDigestLocked() { |
| 189 | byte[] digest = computeDigestLocked(null); |
| 190 | if (digest != null) { |
| 191 | DataOutputStream outputStream; |
| 192 | try { |
| 193 | outputStream = new DataOutputStream( |
| 194 | new FileOutputStream(new File(mDataBlockFile))); |
| 195 | } catch (FileNotFoundException e) { |
| 196 | Slog.e(TAG, "partition not available?", e); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | try { |
| 201 | outputStream.write(digest, 0, DIGEST_SIZE_BYTES); |
| 202 | outputStream.flush(); |
| 203 | } catch (IOException e) { |
| 204 | Slog.e(TAG, "failed to write block checksum", e); |
| 205 | return false; |
| 206 | } finally { |
| 207 | IoUtils.closeQuietly(outputStream); |
| 208 | } |
| 209 | return true; |
| 210 | } else { |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private byte[] computeDigestLocked(byte[] storedDigest) { |
| 216 | DataInputStream inputStream; |
| 217 | try { |
| 218 | inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile))); |
| 219 | } catch (FileNotFoundException e) { |
| 220 | Slog.e(TAG, "partition not available?", e); |
| 221 | return null; |
| 222 | } |
| 223 | |
| 224 | MessageDigest md; |
| 225 | try { |
| 226 | md = MessageDigest.getInstance("SHA-256"); |
| 227 | } catch (NoSuchAlgorithmException e) { |
| 228 | // won't ever happen -- every implementation is required to support SHA-256 |
| 229 | Slog.e(TAG, "SHA-256 not supported?", e); |
| 230 | IoUtils.closeQuietly(inputStream); |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | try { |
| 235 | if (storedDigest != null && storedDigest.length == DIGEST_SIZE_BYTES) { |
| 236 | inputStream.read(storedDigest); |
| 237 | } else { |
| 238 | inputStream.skipBytes(DIGEST_SIZE_BYTES); |
| 239 | } |
| 240 | |
| 241 | int read; |
| 242 | byte[] data = new byte[1024]; |
| 243 | md.update(data, 0, DIGEST_SIZE_BYTES); // include 0 checksum in digest |
| 244 | while ((read = inputStream.read(data)) != -1) { |
| 245 | md.update(data, 0, read); |
| 246 | } |
| 247 | } catch (IOException e) { |
| 248 | Slog.e(TAG, "failed to read partition", e); |
| 249 | return null; |
| 250 | } finally { |
| 251 | IoUtils.closeQuietly(inputStream); |
| 252 | } |
| 253 | |
| 254 | return md.digest(); |
| 255 | } |
| 256 | |
Andres Morales | c8f952c | 2015-03-19 08:34:55 -0700 | [diff] [blame] | 257 | private void formatPartitionLocked(boolean setOemUnlockEnabled) { |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 258 | DataOutputStream outputStream; |
| 259 | try { |
| 260 | outputStream = new DataOutputStream(new FileOutputStream(new File(mDataBlockFile))); |
| 261 | } catch (FileNotFoundException e) { |
| 262 | Slog.e(TAG, "partition not available?", e); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | byte[] data = new byte[DIGEST_SIZE_BYTES]; |
| 267 | try { |
| 268 | outputStream.write(data, 0, DIGEST_SIZE_BYTES); |
| 269 | outputStream.writeInt(PARTITION_TYPE_MARKER); |
| 270 | outputStream.writeInt(0); // data size |
| 271 | outputStream.flush(); |
| 272 | } catch (IOException e) { |
| 273 | Slog.e(TAG, "failed to format block", e); |
| 274 | return; |
| 275 | } finally { |
| 276 | IoUtils.closeQuietly(outputStream); |
| 277 | } |
| 278 | |
Andres Morales | c8f952c | 2015-03-19 08:34:55 -0700 | [diff] [blame] | 279 | doSetOemUnlockEnabledLocked(setOemUnlockEnabled); |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 280 | computeAndWriteDigestLocked(); |
| 281 | } |
| 282 | |
| 283 | private void doSetOemUnlockEnabledLocked(boolean enabled) { |
| 284 | FileOutputStream outputStream; |
| 285 | try { |
| 286 | outputStream = new FileOutputStream(new File(mDataBlockFile)); |
| 287 | } catch (FileNotFoundException e) { |
| 288 | Slog.e(TAG, "partition not available", e); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | try { |
| 293 | FileChannel channel = outputStream.getChannel(); |
| 294 | |
| 295 | channel.position(getBlockDeviceSize() - 1); |
| 296 | |
| 297 | ByteBuffer data = ByteBuffer.allocate(1); |
| 298 | data.put(enabled ? (byte) 1 : (byte) 0); |
| 299 | data.flip(); |
| 300 | channel.write(data); |
| 301 | outputStream.flush(); |
| 302 | } catch (IOException e) { |
| 303 | Slog.e(TAG, "unable to access persistent partition", e); |
| 304 | return; |
| 305 | } finally { |
Andres Morales | 5ca4cc5 | 2015-03-19 16:37:54 -0700 | [diff] [blame] | 306 | SystemProperties.set(OEM_UNLOCK_PROP, enabled ? "1" : "0"); |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 307 | IoUtils.closeQuietly(outputStream); |
| 308 | } |
| 309 | } |
| 310 | |
Andres Morales | 1ce7d17 | 2015-01-07 14:24:57 -0800 | [diff] [blame] | 311 | private boolean doGetOemUnlockEnabled() { |
| 312 | DataInputStream inputStream; |
| 313 | try { |
| 314 | inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile))); |
| 315 | } catch (FileNotFoundException e) { |
| 316 | Slog.e(TAG, "partition not available"); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | try { |
| 321 | synchronized (mLock) { |
| 322 | inputStream.skip(getBlockDeviceSize() - 1); |
| 323 | return inputStream.readByte() != 0; |
| 324 | } |
| 325 | } catch (IOException e) { |
| 326 | Slog.e(TAG, "unable to access persistent partition", e); |
| 327 | return false; |
| 328 | } finally { |
| 329 | IoUtils.closeQuietly(inputStream); |
| 330 | } |
| 331 | } |
| 332 | |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 333 | private native long nativeGetBlockDeviceSize(String path); |
| 334 | private native int nativeWipe(String path); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 335 | |
| 336 | private final IBinder mService = new IPersistentDataBlockService.Stub() { |
| 337 | @Override |
| 338 | public int write(byte[] data) throws RemoteException { |
| 339 | enforceUid(Binder.getCallingUid()); |
| 340 | |
| 341 | // Need to ensure we don't write over the last byte |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 342 | long maxBlockSize = getBlockDeviceSize() - HEADER_SIZE - 1; |
| 343 | if (data.length > maxBlockSize) { |
| 344 | // partition is ~500k so shouldn't be a problem to downcast |
| 345 | return (int) -maxBlockSize; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | DataOutputStream outputStream; |
| 349 | try { |
| 350 | outputStream = new DataOutputStream(new FileOutputStream(new File(mDataBlockFile))); |
| 351 | } catch (FileNotFoundException e) { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 352 | Slog.e(TAG, "partition not available?", e); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | ByteBuffer headerAndData = ByteBuffer.allocate(data.length + HEADER_SIZE); |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 357 | headerAndData.putInt(PARTITION_TYPE_MARKER); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 358 | headerAndData.putInt(data.length); |
| 359 | headerAndData.put(data); |
| 360 | |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 361 | synchronized (mLock) { |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 362 | try { |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 363 | byte[] checksum = new byte[DIGEST_SIZE_BYTES]; |
| 364 | outputStream.write(checksum, 0, DIGEST_SIZE_BYTES); |
| 365 | outputStream.write(headerAndData.array()); |
| 366 | outputStream.flush(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 367 | } catch (IOException e) { |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 368 | Slog.e(TAG, "failed writing to the persistent data block", e); |
| 369 | return -1; |
| 370 | } finally { |
| 371 | IoUtils.closeQuietly(outputStream); |
| 372 | } |
| 373 | |
| 374 | if (computeAndWriteDigestLocked()) { |
| 375 | return data.length; |
| 376 | } else { |
| 377 | return -1; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | @Override |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 383 | public byte[] read() { |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 384 | enforceUid(Binder.getCallingUid()); |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 385 | if (!enforceChecksumValidity()) { |
| 386 | return new byte[0]; |
| 387 | } |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 388 | |
| 389 | DataInputStream inputStream; |
| 390 | try { |
| 391 | inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile))); |
| 392 | } catch (FileNotFoundException e) { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 393 | Slog.e(TAG, "partition not available?", e); |
| 394 | return null; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | try { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 398 | synchronized (mLock) { |
| 399 | int totalDataSize = getTotalDataSizeLocked(inputStream); |
| 400 | |
| 401 | if (totalDataSize == 0) { |
| 402 | return new byte[0]; |
| 403 | } |
| 404 | |
| 405 | byte[] data = new byte[totalDataSize]; |
| 406 | int read = inputStream.read(data, 0, totalDataSize); |
| 407 | if (read < totalDataSize) { |
| 408 | // something went wrong, not returning potentially corrupt data |
| 409 | Slog.e(TAG, "failed to read entire data block. bytes read: " + |
| 410 | read + "/" + totalDataSize); |
| 411 | return null; |
| 412 | } |
| 413 | return data; |
| 414 | } |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 415 | } catch (IOException e) { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 416 | Slog.e(TAG, "failed to read data", e); |
| 417 | return null; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 418 | } finally { |
| 419 | try { |
| 420 | inputStream.close(); |
| 421 | } catch (IOException e) { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 422 | Slog.e(TAG, "failed to close OutputStream"); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public void wipe() { |
| 429 | enforceOemUnlockPermission(); |
| 430 | |
| 431 | synchronized (mLock) { |
| 432 | int ret = nativeWipe(mDataBlockFile); |
| 433 | |
| 434 | if (ret < 0) { |
| 435 | Slog.e(TAG, "failed to wipe persistent partition"); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | @Override |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame^] | 441 | public void setOemUnlockEnabled(boolean enabled) throws SecurityException { |
Guang Zhu | 514c580 | 2014-09-12 15:14:00 -0700 | [diff] [blame] | 442 | // do not allow monkey to flip the flag |
| 443 | if (ActivityManager.isUserAMonkey()) { |
| 444 | return; |
| 445 | } |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame^] | 446 | // Do not allow oem unlock modification if it has been disallowed. |
| 447 | if (Settings.Global.getInt(getContext().getContentResolver(), |
| 448 | Settings.Global.OEM_UNLOCK_DISALLOWED, 0) == 1) { |
| 449 | throw new SecurityException("OEM unlock has been disallowed."); |
| 450 | } |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 451 | enforceOemUnlockPermission(); |
Xiaohui Chen | f066078 | 2015-09-02 14:25:19 -0700 | [diff] [blame] | 452 | enforceIsAdmin(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 453 | |
Andres Morales | 2830130 | 2014-11-12 07:56:46 -0800 | [diff] [blame] | 454 | synchronized (mLock) { |
| 455 | doSetOemUnlockEnabledLocked(enabled); |
| 456 | computeAndWriteDigestLocked(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public boolean getOemUnlockEnabled() { |
| 462 | enforceOemUnlockPermission(); |
Andres Morales | 1ce7d17 | 2015-01-07 14:24:57 -0800 | [diff] [blame] | 463 | return doGetOemUnlockEnabled(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | @Override |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 467 | public int getFlashLockState() { |
| 468 | enforceOemUnlockPermission(); |
| 469 | String locked = SystemProperties.get(FLASH_LOCK_PROP); |
| 470 | switch (locked) { |
| 471 | case FLASH_LOCK_LOCKED: |
| 472 | return PersistentDataBlockManager.FLASH_LOCK_LOCKED; |
| 473 | case FLASH_LOCK_UNLOCKED: |
| 474 | return PersistentDataBlockManager.FLASH_LOCK_UNLOCKED; |
| 475 | default: |
| 476 | return PersistentDataBlockManager.FLASH_LOCK_UNKNOWN; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | @Override |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 481 | public int getDataBlockSize() { |
Craig Lafayette | 66445a6 | 2015-03-27 09:01:43 -0400 | [diff] [blame] | 482 | enforcePersistentDataBlockAccess(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 483 | |
| 484 | DataInputStream inputStream; |
| 485 | try { |
| 486 | inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile))); |
| 487 | } catch (FileNotFoundException e) { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 488 | Slog.e(TAG, "partition not available"); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | try { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 493 | synchronized (mLock) { |
| 494 | return getTotalDataSizeLocked(inputStream); |
| 495 | } |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 496 | } catch (IOException e) { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 497 | Slog.e(TAG, "error reading data block size"); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 498 | return 0; |
| 499 | } finally { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 500 | IoUtils.closeQuietly(inputStream); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 503 | |
Craig Lafayette | 66445a6 | 2015-03-27 09:01:43 -0400 | [diff] [blame] | 504 | private void enforcePersistentDataBlockAccess() { |
| 505 | if (mContext.checkCallingPermission(Manifest.permission.ACCESS_PDB_STATE) |
| 506 | != PackageManager.PERMISSION_GRANTED) { |
| 507 | enforceUid(Binder.getCallingUid()); |
| 508 | } |
| 509 | } |
| 510 | |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 511 | @Override |
| 512 | public long getMaximumDataBlockSize() { |
| 513 | long actualSize = getBlockDeviceSize() - HEADER_SIZE - 1; |
| 514 | return actualSize <= MAX_DATA_BLOCK_SIZE ? actualSize : MAX_DATA_BLOCK_SIZE; |
| 515 | } |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 516 | }; |
| 517 | } |