Victor Hsieh | 55f1499 | 2018-01-13 14:12:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.security; |
| 18 | |
| 19 | import static android.system.OsConstants.PROT_READ; |
| 20 | import static android.system.OsConstants.PROT_WRITE; |
| 21 | |
| 22 | import android.annotation.NonNull; |
| 23 | import android.os.SharedMemory; |
| 24 | import android.system.ErrnoException; |
| 25 | import android.system.Os; |
| 26 | import android.util.apk.ApkSignatureVerifier; |
| 27 | import android.util.apk.ByteBufferFactory; |
| 28 | import android.util.apk.SignatureNotFoundException; |
| 29 | import android.util.Slog; |
| 30 | |
| 31 | import java.io.FileDescriptor; |
| 32 | import java.io.IOException; |
| 33 | import java.nio.ByteBuffer; |
| 34 | import java.security.DigestException; |
| 35 | import java.security.NoSuchAlgorithmException; |
| 36 | import java.util.Arrays; |
| 37 | |
| 38 | /** Provides fsverity related operations. */ |
| 39 | abstract public class VerityUtils { |
| 40 | private static final String TAG = "VerityUtils"; |
| 41 | |
| 42 | private static final boolean DEBUG = false; |
| 43 | |
| 44 | /** |
| 45 | * Generates Merkle tree and fsverity metadata. |
| 46 | * |
| 47 | * @return {@code SetupResult} that contains the {@code EsetupResultCode}, and when success, the |
| 48 | * {@code FileDescriptor} to read all the data from. |
| 49 | */ |
| 50 | public static SetupResult generateApkVeritySetupData(@NonNull String apkPath) { |
| 51 | if (DEBUG) Slog.d(TAG, "Trying to install apk verity to " + apkPath); |
| 52 | SharedMemory shm = null; |
| 53 | try { |
| 54 | byte[] signedRootHash = ApkSignatureVerifier.getVerityRootHash(apkPath); |
| 55 | if (signedRootHash == null) { |
| 56 | if (DEBUG) { |
| 57 | Slog.d(TAG, "Skip verity tree generation since there is no root hash"); |
| 58 | } |
| 59 | return SetupResult.skipped(); |
| 60 | } |
| 61 | |
| 62 | shm = generateApkVerityIntoSharedMemory(apkPath, signedRootHash); |
| 63 | FileDescriptor rfd = shm.getFileDescriptor(); |
| 64 | if (rfd == null || !rfd.valid()) { |
| 65 | return SetupResult.failed(); |
| 66 | } |
| 67 | return SetupResult.ok(Os.dup(rfd)); |
| 68 | } catch (IOException | SecurityException | DigestException | NoSuchAlgorithmException | |
| 69 | SignatureNotFoundException | ErrnoException e) { |
| 70 | Slog.e(TAG, "Failed to set up apk verity: ", e); |
| 71 | return SetupResult.failed(); |
| 72 | } finally { |
| 73 | if (shm != null) { |
| 74 | shm.close(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
Victor Hsieh | 5f76124 | 2018-01-20 10:30:12 -0800 | [diff] [blame] | 80 | * {@see ApkSignatureVerifier#generateFsverityRootHash(String)}. |
| 81 | */ |
| 82 | public static byte[] generateFsverityRootHash(@NonNull String apkPath) |
| 83 | throws NoSuchAlgorithmException, DigestException, IOException { |
| 84 | return ApkSignatureVerifier.generateFsverityRootHash(apkPath); |
| 85 | } |
| 86 | |
| 87 | /** |
Victor Hsieh | 55f1499 | 2018-01-13 14:12:59 -0800 | [diff] [blame] | 88 | * Returns a {@code SharedMemory} that contains Merkle tree and fsverity headers for the given |
| 89 | * apk, in the form that can immediately be used for fsverity setup. |
| 90 | */ |
| 91 | private static SharedMemory generateApkVerityIntoSharedMemory( |
| 92 | String apkPath, byte[] expectedRootHash) |
| 93 | throws IOException, SecurityException, DigestException, NoSuchAlgorithmException, |
| 94 | SignatureNotFoundException { |
| 95 | TrackedShmBufferFactory shmBufferFactory = new TrackedShmBufferFactory(); |
| 96 | byte[] generatedRootHash = ApkSignatureVerifier.generateApkVerity(apkPath, |
| 97 | shmBufferFactory); |
| 98 | // We only generate Merkle tree once here, so it's important to make sure the root hash |
| 99 | // matches the signed one in the apk. |
| 100 | if (!Arrays.equals(expectedRootHash, generatedRootHash)) { |
| 101 | throw new SecurityException("Locally generated verity root hash does not match"); |
| 102 | } |
| 103 | |
| 104 | SharedMemory shm = shmBufferFactory.releaseSharedMemory(); |
| 105 | if (shm == null) { |
| 106 | throw new IllegalStateException("Failed to generate verity tree into shared memory"); |
| 107 | } |
| 108 | if (!shm.setProtect(PROT_READ)) { |
| 109 | throw new SecurityException("Failed to set up shared memory correctly"); |
| 110 | } |
| 111 | return shm; |
| 112 | } |
| 113 | |
| 114 | public static class SetupResult { |
| 115 | /** Result code if verity is set up correctly. */ |
| 116 | private static final int RESULT_OK = 1; |
| 117 | |
| 118 | /** Result code if the apk does not contain a verity root hash. */ |
| 119 | private static final int RESULT_SKIPPED = 2; |
| 120 | |
| 121 | /** Result code if the setup failed. */ |
| 122 | private static final int RESULT_FAILED = 3; |
| 123 | |
| 124 | private final int mCode; |
| 125 | private final FileDescriptor mFileDescriptor; |
| 126 | |
| 127 | public static SetupResult ok(@NonNull FileDescriptor fileDescriptor) { |
| 128 | return new SetupResult(RESULT_OK, fileDescriptor); |
| 129 | } |
| 130 | |
| 131 | public static SetupResult skipped() { |
| 132 | return new SetupResult(RESULT_SKIPPED, null); |
| 133 | } |
| 134 | |
| 135 | public static SetupResult failed() { |
| 136 | return new SetupResult(RESULT_FAILED, null); |
| 137 | } |
| 138 | |
| 139 | private SetupResult(int code, FileDescriptor fileDescriptor) { |
| 140 | this.mCode = code; |
| 141 | this.mFileDescriptor = fileDescriptor; |
| 142 | } |
| 143 | |
| 144 | public boolean isFailed() { |
| 145 | return mCode == RESULT_FAILED; |
| 146 | } |
| 147 | |
| 148 | public boolean isOk() { |
| 149 | return mCode == RESULT_OK; |
| 150 | } |
| 151 | |
| 152 | public @NonNull FileDescriptor getUnownedFileDescriptor() { |
| 153 | return mFileDescriptor; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** A {@code ByteBufferFactory} that creates a shared memory backed {@code ByteBuffer}. */ |
| 158 | private static class TrackedShmBufferFactory implements ByteBufferFactory { |
| 159 | private SharedMemory mShm; |
| 160 | private ByteBuffer mBuffer; |
| 161 | |
| 162 | @Override |
| 163 | public ByteBuffer create(int capacity) throws SecurityException { |
| 164 | try { |
| 165 | if (DEBUG) Slog.d(TAG, "Creating shared memory for apk verity"); |
| 166 | // NB: This method is supposed to be called once according to the contract with |
| 167 | // ApkSignatureSchemeV2Verifier. |
| 168 | if (mBuffer != null) { |
| 169 | throw new IllegalStateException("Multiple instantiation from this factory"); |
| 170 | } |
| 171 | mShm = SharedMemory.create("apkverity", capacity); |
| 172 | if (!mShm.setProtect(PROT_READ | PROT_WRITE)) { |
| 173 | throw new SecurityException("Failed to set protection"); |
| 174 | } |
| 175 | mBuffer = mShm.mapReadWrite(); |
| 176 | return mBuffer; |
| 177 | } catch (ErrnoException e) { |
| 178 | throw new SecurityException("Failed to set protection", e); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | public SharedMemory releaseSharedMemory() { |
| 183 | if (mBuffer != null) { |
| 184 | SharedMemory.unmap(mBuffer); |
| 185 | mBuffer = null; |
| 186 | } |
| 187 | SharedMemory tmp = mShm; |
| 188 | mShm = null; |
| 189 | return tmp; |
| 190 | } |
| 191 | } |
| 192 | } |