summaryrefslogtreecommitdiff
path: root/sdksandbox/SdkSandbox/src/com/android/sdksandbox/SdkSandboxServiceImpl.java
blob: a03e5553b44c5c5606d9e0f2de5b44c09437387c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.sdksandbox;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.app.Service;
import android.app.sdksandbox.ISdkToServiceCallback;
import android.app.sdksandbox.LoadSdkException;
import android.app.sdksandbox.SandboxedSdkContext;
import android.app.sdksandbox.SharedPreferencesKey;
import android.app.sdksandbox.SharedPreferencesUpdate;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
import android.webkit.WebView;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import dalvik.system.DexClassLoader;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Objects;

/** Implementation of Sdk Sandbox Service. */
public class SdkSandboxServiceImpl extends Service {

    private static final String TAG = "SdkSandbox";

    @GuardedBy("mHeldSdk")
    private final Map<IBinder, SandboxedSdkHolder> mHeldSdk = new ArrayMap<>();
    private Injector mInjector;
    private ISdkSandboxService.Stub mBinder;

    static class Injector {

        private final Context mContext;

        Injector(Context context) {
            mContext = context;
        }

        int getCallingUid() {
            return Binder.getCallingUidOrThrow();
        }

        Context getContext() {
            return mContext;
        }

        long getCurrentTime() {
            return System.currentTimeMillis();
        }
    }

    public SdkSandboxServiceImpl() {
    }

    @VisibleForTesting
    SdkSandboxServiceImpl(Injector injector) {
        mInjector = injector;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        mBinder = new SdkSandboxServiceDelegate();
        mInjector = new Injector(getApplicationContext());
    }

    /** Loads SDK. */
    public void loadSdk(
            String callingPackageName,
            IBinder sdkToken,
            ApplicationInfo applicationInfo,
            String sdkName,
            String sdkProviderClassName,
            String sdkCeDataDir,
            String sdkDeDataDir,
            Bundle params,
            ILoadSdkInSandboxCallback callback,
            SandboxLatencyInfo sandboxLatencyInfo,
            ISdkToServiceCallback sdkToServiceCallback) {
        enforceCallerIsSystemServer();
        final long token = Binder.clearCallingIdentity();
        try {
            loadSdkInternal(
                    callingPackageName,
                    sdkToken,
                    applicationInfo,
                    sdkName,
                    sdkProviderClassName,
                    sdkCeDataDir,
                    sdkDeDataDir,
                    params,
                    callback,
                    sandboxLatencyInfo,
                    sdkToServiceCallback);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    /** Unloads SDK. */
    public void unloadSdk(
            IBinder sdkToken, IUnloadSdkCallback callback, SandboxLatencyInfo sandboxLatencyInfo) {
        enforceCallerIsSystemServer();
        final long token = Binder.clearCallingIdentity();
        try {
            sandboxLatencyInfo.setTimeSandboxCalledSdk(mInjector.getCurrentTime());
            unloadSdkInternal(sdkToken);
            sandboxLatencyInfo.setTimeSdkCallCompleted(mInjector.getCurrentTime());
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        sandboxLatencyInfo.setTimeSandboxCalledSystemServer(mInjector.getCurrentTime());
        try {
            callback.onUnloadSdk(sandboxLatencyInfo);
        } catch (RemoteException ignore) {
            Log.e(TAG, "Could not send onUnloadSdk");
        }
    }

    /** Sync data from client. */
    public void syncDataFromClient(SharedPreferencesUpdate update) {
        SharedPreferences pref =
                PreferenceManager.getDefaultSharedPreferences(mInjector.getContext());
        SharedPreferences.Editor editor = pref.edit();
        final Bundle data = update.getData();
        for (SharedPreferencesKey keyInUpdate : update.getKeysInUpdate()) {
            updateSharedPreferences(editor, data, keyInUpdate);
        }
        // TODO(b/239403323): What if writing to persistent storage fails?
        editor.apply();
    }

    private void updateSharedPreferences(
            SharedPreferences.Editor editor, Bundle data, SharedPreferencesKey keyInUpdate) {
        final String key = keyInUpdate.getName();

        if (!data.containsKey(key)) {
            // key was specified but bundle didn't have the key; meaning it has been removed.
            editor.remove(key);
            return;
        }

        final int type = keyInUpdate.getType();
        try {
            switch (type) {
                case SharedPreferencesKey.KEY_TYPE_STRING:
                    editor.putString(key, data.getString(key, ""));
                    break;
                case SharedPreferencesKey.KEY_TYPE_BOOLEAN:
                    editor.putBoolean(key, data.getBoolean(key, false));
                    break;
                case SharedPreferencesKey.KEY_TYPE_INTEGER:
                    editor.putInt(key, data.getInt(key, 0));
                    break;
                case SharedPreferencesKey.KEY_TYPE_FLOAT:
                    editor.putFloat(key, data.getFloat(key, 0.0f));
                    break;
                case SharedPreferencesKey.KEY_TYPE_LONG:
                    editor.putLong(key, data.getLong(key, 0L));
                    break;
                case SharedPreferencesKey.KEY_TYPE_STRING_SET:
                    final ArraySet<String> castedValue =
                            new ArraySet<>(data.getStringArrayList(key));
                    editor.putStringSet(key, castedValue);
                    break;
                default:
                    Log.e(
                            TAG,
                            "Unknown type found in default SharedPreferences for Key: "
                                    + key
                                    + " Type: "
                                    + type);
            }
        } catch (ClassCastException ignore) {
            editor.remove(key);
            // TODO(b/239403323): Once error reporting is supported, we should return error to the
            // user instead.
            Log.e(
                    TAG,
                    "Wrong type found in default SharedPreferences for Key: "
                            + key
                            + " Type: "
                            + type);
        }
    }

    /**
     * Checks if the SDK sandbox is disabled. This will be {@code true} iff the WebView provider is
     * not visible to the sandbox.
     */
    public void isDisabled(ISdkSandboxDisabledCallback callback) {
        enforceCallerIsSystemServer();
        PackageInfo info = WebView.getCurrentWebViewPackage();
        PackageInfo webViewProviderInfo = null;
        boolean isDisabled = false;
        try {
            if (info != null) {
                webViewProviderInfo =
                        mInjector
                                .getContext()
                                .getPackageManager()
                                .getPackageInfo(
                                        info.packageName, PackageManager.PackageInfoFlags.of(0));
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.w(TAG, "Could not verify if the SDK sandbox should be disabled", e);
            isDisabled = true;
        }
        isDisabled |= webViewProviderInfo == null;
        try {
            callback.onResult(isDisabled);
        } catch (RemoteException e) {
            Log.e(TAG, "Could not call back into ISdkSandboxDisabledCallback", e);
        }
    }

    @Override
    @RequiresPermission(android.Manifest.permission.DUMP)
    protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
        mInjector.getContext().enforceCallingPermission(android.Manifest.permission.DUMP,
                "Can't dump " + TAG);
        synchronized (mHeldSdk) {
            // TODO(b/211575098): Use IndentingPrintWriter for better formatting
            if (mHeldSdk.isEmpty()) {
                writer.println("mHeldSdk is empty");
            } else {
                writer.print("mHeldSdk size: ");
                writer.println(mHeldSdk.size());
                for (SandboxedSdkHolder sandboxedSdkHolder : mHeldSdk.values()) {
                    sandboxedSdkHolder.dump(writer);
                    writer.println();
                }
            }
        }
    }

    private void enforceCallerIsSystemServer() {
        if (mInjector.getCallingUid() != Process.SYSTEM_UID) {
            throw new SecurityException(
                    "Only system_server is allowed to call this API, actual calling uid is "
                            + mInjector.getCallingUid());
        }
    }

    private void loadSdkInternal(
            @NonNull String callingPackageName,
            @NonNull IBinder sdkToken,
            @NonNull ApplicationInfo applicationInfo,
            @NonNull String sdkName,
            @NonNull String sdkProviderClassName,
            @Nullable String sdkCeDataDir,
            @Nullable String sdkDeDataDir,
            @NonNull Bundle params,
            @NonNull ILoadSdkInSandboxCallback callback,
            @NonNull SandboxLatencyInfo sandboxLatencyInfo,
            @NonNull ISdkToServiceCallback sdkToServiceCallback) {
        synchronized (mHeldSdk) {
            if (mHeldSdk.containsKey(sdkToken)) {
                sendLoadError(
                        callback,
                        ILoadSdkInSandboxCallback.LOAD_SDK_ALREADY_LOADED,
                        "Already loaded sdk for package " + applicationInfo.packageName,
                        sandboxLatencyInfo);
                return;
            }
        }

        ClassLoader loader;
        SandboxedSdkHolder sandboxedSdkHolder;
        try {
            loader = getClassLoader(applicationInfo);
            Class<?> clz = Class.forName(SandboxedSdkHolder.class.getName(), true, loader);
            sandboxedSdkHolder = (SandboxedSdkHolder) clz.getDeclaredConstructor().newInstance();
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            sendLoadError(
                    callback,
                    ILoadSdkInSandboxCallback.LOAD_SDK_NOT_FOUND,
                    "Failed to find: " + SandboxedSdkHolder.class.getName(),
                    sandboxLatencyInfo);
            return;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            sendLoadError(
                    callback,
                    ILoadSdkInSandboxCallback.LOAD_SDK_INSTANTIATION_ERROR,
                    "Failed to instantiate " + SandboxedSdkHolder.class.getName() + ": " + e,
                    sandboxLatencyInfo);
            return;
        }

        // We want to ensure that SandboxedSdkContext.getSystemService() will return different
        // instances for different SandboxedSdkContext contexts, so that different SDKs
        // running in the same sdk sandbox process don't share the same manager instance.
        // Because SandboxedSdkContext is a ContextWrapper, it delegates the getSystemService()
        // call to its base context. If we use an application context here as a base context
        // when creating an instance of SandboxedSdkContext it will mean that all instances of
        // SandboxedSdkContext will return the same manager instances.

        // In order to create per-SandboxedSdkContext instances in getSystemService, each
        // SandboxedSdkContext needs to have use ContextImpl as a base context. The ContextImpl
        // is hidden, so we can't instantiate it directly. However, the
        // createCredentialProtectedStorageContext() will always create a new ContextImpl
        // object, which is why we are using it as a base context when creating an instance of
        // SandboxedSdkContext.
        // TODO(b/242889021): make this detail internal to SandboxedSdkContext
        Context ctx = mInjector.getContext().createCredentialProtectedStorageContext();
        SandboxedSdkContext sandboxedSdkContext =
                new SandboxedSdkContext(
                        ctx,
                        callingPackageName,
                        applicationInfo,
                        sdkName,
                        sdkCeDataDir,
                        sdkDeDataDir);
        sandboxedSdkHolder.init(
                params,
                callback,
                sdkProviderClassName,
                loader,
                sandboxedSdkContext,
                mInjector,
                sandboxLatencyInfo,
                sdkToServiceCallback);
        synchronized (mHeldSdk) {
            mHeldSdk.put(sdkToken, sandboxedSdkHolder);
        }
    }

    private void unloadSdkInternal(@NonNull IBinder sdkToken) {
        synchronized (mHeldSdk) {
            SandboxedSdkHolder sandboxedSdkHolder = mHeldSdk.get(sdkToken);
            if (sandboxedSdkHolder != null) {
                sandboxedSdkHolder.unloadSdk();
                mHeldSdk.remove(sdkToken);
            }
        }
    }

    private void sendLoadError(
            ILoadSdkInSandboxCallback callback,
            int errorCode,
            String message,
            SandboxLatencyInfo sandboxLatencyInfo) {
        sandboxLatencyInfo.setTimeSandboxCalledSystemServer(mInjector.getCurrentTime());
        sandboxLatencyInfo.setSandboxStatus(SandboxLatencyInfo.SANDBOX_STATUS_FAILED_AT_SANDBOX);
        try {
            callback.onLoadSdkError(new LoadSdkException(errorCode, message), sandboxLatencyInfo);
        } catch (RemoteException e) {
            Log.e(TAG, "Could not send onLoadCodeError");
        }
    }

    private ClassLoader getClassLoader(ApplicationInfo appInfo) {
        return new DexClassLoader(appInfo.sourceDir, null, null, getClass().getClassLoader());
    }

    final class SdkSandboxServiceDelegate extends ISdkSandboxService.Stub {

        @Override
        public void loadSdk(
                @NonNull String callingPackageName,
                @NonNull IBinder sdkToken,
                @NonNull ApplicationInfo applicationInfo,
                @NonNull String sdkName,
                @NonNull String sdkProviderClassName,
                @Nullable String sdkCeDataDir,
                @Nullable String sdkDeDataDir,
                @NonNull Bundle params,
                @NonNull ILoadSdkInSandboxCallback callback,
                @NonNull SandboxLatencyInfo sandboxLatencyInfo,
                @NonNull ISdkToServiceCallback sdkToServiceCallback) {
            sandboxLatencyInfo.setTimeSandboxReceivedCallFromSystemServer(
                    mInjector.getCurrentTime());

            Objects.requireNonNull(callingPackageName, "callingPackageName should not be null");
            Objects.requireNonNull(sdkToken, "sdkToken should not be null");
            Objects.requireNonNull(applicationInfo, "applicationInfo should not be null");
            Objects.requireNonNull(sdkName, "sdkName should not be null");
            Objects.requireNonNull(sdkProviderClassName, "sdkProviderClassName should not be null");
            Objects.requireNonNull(params, "params should not be null");
            Objects.requireNonNull(callback, "callback should not be null");
            Objects.requireNonNull(sdkToServiceCallback, "sdkToServiceCallback should not be null");
            if (TextUtils.isEmpty(sdkProviderClassName)) {
                throw new IllegalArgumentException("sdkProviderClassName must not be empty");
            }

            SdkSandboxServiceImpl.this.loadSdk(
                    callingPackageName,
                    sdkToken,
                    applicationInfo,
                    sdkName,
                    sdkProviderClassName,
                    sdkCeDataDir,
                    sdkDeDataDir,
                    params,
                    callback,
                    sandboxLatencyInfo,
                    sdkToServiceCallback);
        }

        @Override
        public void unloadSdk(
                @NonNull IBinder sdkToken,
                @NonNull IUnloadSdkCallback callback,
                @NonNull SandboxLatencyInfo sandboxLatencyInfo) {
            Objects.requireNonNull(sandboxLatencyInfo, "sandboxLatencyInfo should not be null");
            sandboxLatencyInfo.setTimeSandboxReceivedCallFromSystemServer(
                    mInjector.getCurrentTime());
            Objects.requireNonNull(sdkToken, "sdkToken should not be null");
            Objects.requireNonNull(callback, "callback should not be null");
            SdkSandboxServiceImpl.this.unloadSdk(sdkToken, callback, sandboxLatencyInfo);
        }

        @Override
        public void syncDataFromClient(@NonNull SharedPreferencesUpdate update) {
            Objects.requireNonNull(update, "update should not be null");
            SdkSandboxServiceImpl.this.syncDataFromClient(update);
        }

        @Override
        public void isDisabled(@NonNull ISdkSandboxDisabledCallback callback) {
            Objects.requireNonNull(callback, "callback should not be null");
            SdkSandboxServiceImpl.this.isDisabled(callback);
        }
    }
}