/
GeneratePackageInfoHook.java
90 lines (81 loc) · 3.65 KB
/
GeneratePackageInfoHook.java
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
/*
* Copyright (C) 2015 Marvin W <https://github.com/mar-v-in>
* Copyright (C) 2016-2019 Lanchon <https://github.com/Lanchon>
*
* Based on Marvin's work:
*
* https://gerrit.omnirom.org/#/c/14898/
* https://gerrit.omnirom.org/#/c/14899/
*
* 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.server.pm;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageParser;
import android.content.pm.Signature;
import android.util.Log;
import lanchon.dexpatcher.annotation.*;
@DexEdit
class GeneratePackageInfoHook {
@DexAdd
static class FakeSignatureCore {
static final String PERMISSION = "android.permission.FAKE_PACKAGE_SIGNATURE";
static final String METADATA = "fake-signature";
}
@DexAdd
private static boolean getPerAppEnable(PackageInfo pi, Context context, PackageParser.Package p, int flags, int userId) {
return true;
}
@DexAdd
private static boolean getGlobalEnable(PackageInfo pi, Context context, PackageParser.Package p, int flags, int userId) {
return true;
}
@DexReplace
static PackageInfo hook(PackageInfo pi, Context context, PackageParser.Package p, int flags, int userId) {
try {
if ((flags & PackageManager.GET_SIGNATURES) != 0) {
if (p.requestedPermissions != null) {
if (p.requestedPermissions.contains(FakeSignatureCore.PERMISSION)) {
if (getGlobalEnable(pi, context, p, flags, userId) &&
getPerAppEnable(pi, context, p, flags, userId)) {
if (p.mAppMetaData != null) {
Object fakeSignatureData = p.mAppMetaData.get(FakeSignatureCore.METADATA);
if (fakeSignatureData != null) {
if (fakeSignatureData instanceof String) {
pi.signatures = new Signature[] { new Signature((String) fakeSignatureData) };
} else {
Log.e("GeneratePackageInfoHook", "invalid '" + FakeSignatureCore.METADATA + "' metadata");
}
} else {
Log.e("GeneratePackageInfoHook", "missing 'fake-signature' metadata");
}
} else {
Log.e("GeneratePackageInfoHook", "null mAppMetaData");
}
}
}
} else {
Log.e("GeneratePackageInfoHook", "null requestedPermissions");
}
}
} catch (Throwable t) {
// We should never die because of any failures, this is system code!
// Eating up instances of Error might seem excessive, but they can be
// the result of improperly applied patches so we will handle them here.
Log.e("GeneratePackageInfoHook", "hook exception", t);
}
return pi;
}
}