Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.pm; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 21 | import android.content.pm.DataLoaderParams; |
| 22 | import android.content.pm.InstallationFile; |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageInstaller; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 24 | import android.os.ParcelFileDescriptor; |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 25 | import android.os.ShellCommand; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 26 | import android.service.dataloader.DataLoaderService; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 27 | import android.util.Slog; |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 28 | import android.util.SparseArray; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 29 | |
| 30 | import libcore.io.IoUtils; |
| 31 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 32 | import java.io.IOException; |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 33 | import java.lang.ref.WeakReference; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 34 | import java.nio.charset.StandardCharsets; |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 35 | import java.security.SecureRandom; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 36 | import java.util.Collection; |
| 37 | |
| 38 | /** |
| 39 | * Callback data loader for PackageManagerShellCommand installations. |
| 40 | */ |
| 41 | public class PackageManagerShellCommandDataLoader extends DataLoaderService { |
| 42 | public static final String TAG = "PackageManagerShellCommandDataLoader"; |
| 43 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 44 | private static final String PACKAGE = "android"; |
| 45 | private static final String CLASS = PackageManagerShellCommandDataLoader.class.getName(); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 46 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 47 | static final SecureRandom sRandom = new SecureRandom(); |
| 48 | static final SparseArray<WeakReference<ShellCommand>> sShellCommands = new SparseArray<>(); |
| 49 | |
| 50 | private static final char ARGS_DELIM = '&'; |
| 51 | private static final String SHELL_COMMAND_ID_PREFIX = "shellCommandId="; |
| 52 | private static final int INVALID_SHELL_COMMAND_ID = -1; |
| 53 | private static final int TOO_MANY_PENDING_SHELL_COMMANDS = 10; |
| 54 | |
| 55 | private static final String STDIN_PATH = "-"; |
| 56 | |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 57 | private static String getDataLoaderParamsArgs(ShellCommand shellCommand) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 58 | nativeInitialize(); |
| 59 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 60 | int commandId; |
| 61 | synchronized (sShellCommands) { |
| 62 | // Clean up old references. |
| 63 | for (int i = sShellCommands.size() - 1; i >= 0; i--) { |
| 64 | WeakReference<ShellCommand> oldRef = sShellCommands.valueAt(i); |
| 65 | if (oldRef.get() == null) { |
| 66 | sShellCommands.removeAt(i); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Sanity check. |
| 71 | if (sShellCommands.size() > TOO_MANY_PENDING_SHELL_COMMANDS) { |
| 72 | Slog.e(TAG, "Too many pending shell commands: " + sShellCommands.size()); |
| 73 | } |
| 74 | |
| 75 | // Generate new id and put ref to the array. |
| 76 | do { |
| 77 | commandId = sRandom.nextInt(Integer.MAX_VALUE - 1) + 1; |
| 78 | } while (sShellCommands.contains(commandId)); |
| 79 | |
| 80 | sShellCommands.put(commandId, new WeakReference<>(shellCommand)); |
| 81 | } |
| 82 | |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 83 | return SHELL_COMMAND_ID_PREFIX + commandId; |
| 84 | } |
| 85 | |
| 86 | static DataLoaderParams getStreamingDataLoaderParams(ShellCommand shellCommand) { |
| 87 | return DataLoaderParams.forStreaming(new ComponentName(PACKAGE, CLASS), |
| 88 | getDataLoaderParamsArgs(shellCommand)); |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 91 | static DataLoaderParams getIncrementalDataLoaderParams(ShellCommand shellCommand) { |
| 92 | return DataLoaderParams.forIncremental(new ComponentName(PACKAGE, CLASS), |
Songchun Fan | 82fddef | 2020-03-06 13:30:29 -0800 | [diff] [blame] | 93 | getDataLoaderParamsArgs(shellCommand)); |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 96 | private static int extractShellCommandId(String args) { |
| 97 | int sessionIdIdx = args.indexOf(SHELL_COMMAND_ID_PREFIX); |
| 98 | if (sessionIdIdx < 0) { |
| 99 | Slog.e(TAG, "Missing shell command id param."); |
| 100 | return INVALID_SHELL_COMMAND_ID; |
| 101 | } |
| 102 | sessionIdIdx += SHELL_COMMAND_ID_PREFIX.length(); |
| 103 | int delimIdx = args.indexOf(ARGS_DELIM, sessionIdIdx); |
| 104 | try { |
| 105 | if (delimIdx < 0) { |
| 106 | return Integer.parseInt(args.substring(sessionIdIdx)); |
| 107 | } else { |
| 108 | return Integer.parseInt(args.substring(sessionIdIdx, delimIdx)); |
| 109 | } |
| 110 | } catch (NumberFormatException e) { |
| 111 | Slog.e(TAG, "Incorrect shell command id format.", e); |
| 112 | return INVALID_SHELL_COMMAND_ID; |
| 113 | } |
| 114 | } |
| 115 | |
Alex Buynytskyy | 06970b9 | 2020-03-25 12:50:10 -0700 | [diff] [blame^] | 116 | static class Metadata { |
| 117 | /** |
| 118 | * Full files read from stdin. |
| 119 | */ |
| 120 | static final byte STDIN = 0; |
| 121 | /** |
| 122 | * Full files read from local file. |
| 123 | */ |
| 124 | static final byte LOCAL_FILE = 1; |
| 125 | /** |
| 126 | * Signature tree read from stdin, data streamed. |
| 127 | */ |
| 128 | static final byte DATA_ONLY_STREAMING = 2; |
| 129 | /** |
| 130 | * Everything streamed. |
| 131 | */ |
| 132 | static final byte STREAMING = 3; |
| 133 | |
| 134 | private final byte mMode; |
| 135 | private final String mData; |
| 136 | |
| 137 | static Metadata forStdIn(String fileId) { |
| 138 | return new Metadata(STDIN, fileId); |
| 139 | } |
| 140 | |
| 141 | static Metadata forLocalFile(String filePath) { |
| 142 | return new Metadata(LOCAL_FILE, filePath); |
| 143 | } |
| 144 | |
| 145 | static Metadata forDataOnlyStreaming(String fileId) { |
| 146 | return new Metadata(DATA_ONLY_STREAMING, fileId); |
| 147 | } |
| 148 | |
| 149 | static Metadata forStreaming(String fileId) { |
| 150 | return new Metadata(STREAMING, fileId); |
| 151 | } |
| 152 | |
| 153 | private Metadata(byte mode, String data) { |
| 154 | this.mMode = mode; |
| 155 | this.mData = (data == null) ? "" : data; |
| 156 | } |
| 157 | |
| 158 | static Metadata fromByteArray(byte[] bytes) throws IOException { |
| 159 | if (bytes == null || bytes.length == 0) { |
| 160 | return null; |
| 161 | } |
| 162 | byte mode = bytes[0]; |
| 163 | String data = new String(bytes, 1, bytes.length - 1, StandardCharsets.UTF_8); |
| 164 | return new Metadata(mode, data); |
| 165 | } |
| 166 | |
| 167 | byte[] toByteArray() { |
| 168 | byte[] dataBytes = this.mData.getBytes(StandardCharsets.UTF_8); |
| 169 | byte[] result = new byte[1 + dataBytes.length]; |
| 170 | result[0] = this.mMode; |
| 171 | System.arraycopy(dataBytes, 0, result, 1, dataBytes.length); |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | byte getMode() { |
| 176 | return this.mMode; |
| 177 | } |
| 178 | |
| 179 | String getData() { |
| 180 | return this.mData; |
| 181 | } |
| 182 | } |
| 183 | |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 184 | private static class DataLoader implements DataLoaderService.DataLoader { |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 185 | private DataLoaderParams mParams = null; |
| 186 | private FileSystemConnector mConnector = null; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 187 | |
| 188 | @Override |
| 189 | public boolean onCreate(@NonNull DataLoaderParams dataLoaderParams, |
| 190 | @NonNull FileSystemConnector connector) { |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 191 | mParams = dataLoaderParams; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 192 | mConnector = connector; |
| 193 | return true; |
| 194 | } |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 195 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 196 | @Override |
shengcow | 0696c44 | 2020-01-14 17:58:04 -0800 | [diff] [blame] | 197 | public boolean onPrepareImage(@NonNull Collection<InstallationFile> addedFiles, |
| 198 | @NonNull Collection<String> removedFiles) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 199 | ShellCommand shellCommand = lookupShellCommand(mParams.getArguments()); |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 200 | if (shellCommand == null) { |
| 201 | Slog.e(TAG, "Missing shell command."); |
| 202 | return false; |
| 203 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 204 | try { |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 205 | for (InstallationFile file : addedFiles) { |
Alex Buynytskyy | 06970b9 | 2020-03-25 12:50:10 -0700 | [diff] [blame^] | 206 | Metadata metadata = Metadata.fromByteArray(file.getMetadata()); |
| 207 | if (metadata == null) { |
| 208 | Slog.e(TAG, "Invalid metadata for file: " + file.getName()); |
| 209 | return false; |
| 210 | } |
| 211 | switch (metadata.getMode()) { |
| 212 | case Metadata.STDIN: { |
| 213 | final ParcelFileDescriptor inFd = getStdInPFD(shellCommand); |
| 214 | mConnector.writeData(file.getName(), 0, file.getLengthBytes(), inFd); |
| 215 | break; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 216 | } |
Alex Buynytskyy | 06970b9 | 2020-03-25 12:50:10 -0700 | [diff] [blame^] | 217 | case Metadata.LOCAL_FILE: { |
| 218 | ParcelFileDescriptor incomingFd = null; |
| 219 | try { |
| 220 | incomingFd = getLocalFile(shellCommand, metadata.getData()); |
| 221 | mConnector.writeData(file.getName(), 0, incomingFd.getStatSize(), |
| 222 | incomingFd); |
| 223 | } finally { |
| 224 | IoUtils.closeQuietly(incomingFd); |
| 225 | } |
| 226 | break; |
| 227 | } |
| 228 | default: |
| 229 | Slog.e(TAG, "Unsupported metadata mode: " + metadata.getMode()); |
| 230 | return false; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | return true; |
| 234 | } catch (IOException e) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 235 | Slog.e(TAG, "Exception while streaming files", e); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 241 | static ShellCommand lookupShellCommand(String args) { |
| 242 | final int commandId = extractShellCommandId(args); |
| 243 | if (commandId == INVALID_SHELL_COMMAND_ID) { |
| 244 | return null; |
| 245 | } |
| 246 | |
| 247 | final WeakReference<ShellCommand> shellCommandRef; |
| 248 | synchronized (sShellCommands) { |
| 249 | shellCommandRef = sShellCommands.get(commandId, null); |
| 250 | } |
| 251 | final ShellCommand shellCommand = |
| 252 | shellCommandRef != null ? shellCommandRef.get() : null; |
| 253 | |
| 254 | return shellCommand; |
| 255 | } |
| 256 | |
| 257 | static ParcelFileDescriptor getStdInPFD(ShellCommand shellCommand) { |
| 258 | try { |
| 259 | return ParcelFileDescriptor.dup(shellCommand.getInFileDescriptor()); |
| 260 | } catch (IOException e) { |
| 261 | Slog.e(TAG, "Exception while obtaining STDIN fd", e); |
| 262 | return null; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static ParcelFileDescriptor getLocalFile(ShellCommand shellCommand, String filePath) { |
| 267 | return shellCommand.openFileForSystem(filePath, "r"); |
| 268 | } |
| 269 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 270 | @Override |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 271 | public DataLoaderService.DataLoader onCreateDataLoader( |
| 272 | @NonNull DataLoaderParams dataLoaderParams) { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 273 | if (dataLoaderParams.getType() == PackageInstaller.DATA_LOADER_TYPE_STREAMING) { |
| 274 | // This DataLoader only supports streaming installations. |
| 275 | return new DataLoader(); |
| 276 | } |
| 277 | return null; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 278 | } |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 279 | |
| 280 | /* Native methods */ |
| 281 | private static native void nativeInitialize(); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 282 | } |