Skip to content

Commit 9dbbfcd

Browse files
JesusFrekeAndroid (Google) Code Review
authored andcommitted
Merge "Pass the originating uid to the package verifier" into jb-mr1-dev
2 parents f80ad90 + 37d83a3 commit 9dbbfcd

File tree

5 files changed

+118
-49
lines changed

5 files changed

+118
-49
lines changed

cmds/pm/src/com/android/commands/pm/Pm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ private void runInstall() {
924924
PackageInstallObserver obs = new PackageInstallObserver();
925925
try {
926926
VerificationParams verificationParams = new VerificationParams(verificationURI,
927-
originatingURI, referrerURI, null);
927+
originatingURI, referrerURI, VerificationParams.NO_UID, null);
928928

929929
mPm.installPackageWithVerificationAndEncryption(apkURI, obs, installFlags,
930930
installerPackageName, verificationParams, encryptionParams);

core/java/android/content/Intent.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,14 @@ public static Intent createChooser(Intent target, CharSequence title) {
13041304
public static final String EXTRA_REFERRER
13051305
= "android.intent.extra.REFERRER";
13061306

1307+
/**
1308+
* Used as an int extra field with {@link #ACTION_INSTALL_PACKAGE} and
1309+
* {@link} #ACTION_VIEW} to indicate the uid of the package that initiated the install
1310+
* @hide
1311+
*/
1312+
public static final String EXTRA_ORIGINATING_UID
1313+
= "android.intent.extra.ORIGINATING_UID";
1314+
13071315
/**
13081316
* Used as a boolean extra field with {@link #ACTION_INSTALL_PACKAGE} to install a
13091317
* package. Tells the installer UI to skip the confirmation with the user

core/java/android/content/pm/VerificationParams.java

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
* @hide
2828
*/
2929
public class VerificationParams implements Parcelable {
30+
/** A constant used to indicate that a uid value is not present. */
31+
public static final int NO_UID = -1;
32+
3033
/** What we print out first when toString() is called. */
3134
private static final String TO_STRING_PREFIX = "VerificationParams{";
3235

@@ -39,6 +42,9 @@ public class VerificationParams implements Parcelable {
3942
/** HTTP referrer URI associated with the originatingURI. */
4043
private final Uri mReferrer;
4144

45+
/** UID of the application that the install request originated from. */
46+
private final int mOriginatingUid;
47+
4248
/** UID of application requesting the install */
4349
private int mInstallerUid;
4450

@@ -57,16 +63,19 @@ public class VerificationParams implements Parcelable {
5763
* from. May be {@code null}.
5864
* @param referrer HTTP referrer URI associated with the originatingURI.
5965
* May be {@code null}.
66+
* @param originatingUid UID of the application that the install request originated
67+
* from, or NO_UID if not present
6068
* @param manifestDigest an object that holds the digest of the package
6169
* which can be used to verify ownership. May be {@code null}.
6270
*/
6371
public VerificationParams(Uri verificationURI, Uri originatingURI, Uri referrer,
64-
ManifestDigest manifestDigest) {
72+
int originatingUid, ManifestDigest manifestDigest) {
6573
mVerificationURI = verificationURI;
6674
mOriginatingURI = originatingURI;
6775
mReferrer = referrer;
76+
mOriginatingUid = originatingUid;
6877
mManifestDigest = manifestDigest;
69-
mInstallerUid = -1;
78+
mInstallerUid = NO_UID;
7079
}
7180

7281
public Uri getVerificationURI() {
@@ -81,11 +90,16 @@ public Uri getReferrer() {
8190
return mReferrer;
8291
}
8392

93+
/** return NO_UID if not available */
94+
public int getOriginatingUid() {
95+
return mOriginatingUid;
96+
}
97+
8498
public ManifestDigest getManifestDigest() {
8599
return mManifestDigest;
86100
}
87101

88-
/** @return -1 when not set */
102+
/** @return NO_UID when not set */
89103
public int getInstallerUid() {
90104
return mInstallerUid;
91105
}
@@ -111,31 +125,39 @@ public boolean equals(Object o) {
111125

112126
final VerificationParams other = (VerificationParams) o;
113127

114-
if (mVerificationURI == null && other.mVerificationURI != null) {
115-
return false;
116-
}
117-
if (!mVerificationURI.equals(other.mVerificationURI)) {
128+
if (mVerificationURI == null) {
129+
if (other.mVerificationURI != null) {
130+
return false;
131+
}
132+
} else if (!mVerificationURI.equals(other.mVerificationURI)) {
118133
return false;
119134
}
120135

121-
if (mOriginatingURI == null && other.mOriginatingURI != null) {
122-
return false;
123-
}
124-
if (!mOriginatingURI.equals(other.mOriginatingURI)) {
136+
if (mOriginatingURI == null) {
137+
if (other.mOriginatingURI != null) {
138+
return false;
139+
}
140+
} else if (!mOriginatingURI.equals(other.mOriginatingURI)) {
125141
return false;
126142
}
127143

128-
if (mReferrer == null && other.mReferrer != null) {
129-
return false;
130-
}
131-
if (!mReferrer.equals(other.mReferrer)) {
144+
if (mReferrer == null) {
145+
if (other.mReferrer != null) {
146+
return false;
147+
}
148+
} else if (!mReferrer.equals(other.mReferrer)) {
132149
return false;
133150
}
134151

135-
if (mManifestDigest == null && other.mManifestDigest != null) {
152+
if (mOriginatingUid != other.mOriginatingUid) {
136153
return false;
137154
}
138-
if (mManifestDigest != null && !mManifestDigest.equals(other.mManifestDigest)) {
155+
156+
if (mManifestDigest == null) {
157+
if (other.mManifestDigest != null) {
158+
return false;
159+
}
160+
} else if (!mManifestDigest.equals(other.mManifestDigest)) {
139161
return false;
140162
}
141163

@@ -150,11 +172,12 @@ public boolean equals(Object o) {
150172
public int hashCode() {
151173
int hash = 3;
152174

153-
hash += 5 * (mVerificationURI==null?1:mVerificationURI.hashCode());
154-
hash += 7 * (mOriginatingURI==null?1:mOriginatingURI.hashCode());
155-
hash += 11 * (mReferrer==null?1:mReferrer.hashCode());
156-
hash += 13 * (mManifestDigest==null?1:mManifestDigest.hashCode());
157-
hash += 17 * mInstallerUid;
175+
hash += 5 * (mVerificationURI == null ? 1 : mVerificationURI.hashCode());
176+
hash += 7 * (mOriginatingURI == null ? 1 : mOriginatingURI.hashCode());
177+
hash += 11 * (mReferrer == null ? 1 : mReferrer.hashCode());
178+
hash += 13 * mOriginatingUid;
179+
hash += 17 * (mManifestDigest == null ? 1 : mManifestDigest.hashCode());
180+
hash += 19 * mInstallerUid;
158181

159182
return hash;
160183
}
@@ -169,6 +192,8 @@ public String toString() {
169192
sb.append(mOriginatingURI.toString());
170193
sb.append(",mReferrer=");
171194
sb.append(mReferrer.toString());
195+
sb.append(",mOriginatingUid=");
196+
sb.append(mOriginatingUid);
172197
sb.append(",mManifestDigest=");
173198
sb.append(mManifestDigest.toString());
174199
sb.append(",mInstallerUid=");
@@ -183,6 +208,7 @@ public void writeToParcel(Parcel dest, int flags) {
183208
dest.writeParcelable(mVerificationURI, 0);
184209
dest.writeParcelable(mOriginatingURI, 0);
185210
dest.writeParcelable(mReferrer, 0);
211+
dest.writeInt(mOriginatingUid);
186212
dest.writeParcelable(mManifestDigest, 0);
187213
dest.writeInt(mInstallerUid);
188214
}
@@ -192,6 +218,7 @@ private VerificationParams(Parcel source) {
192218
mVerificationURI = source.readParcelable(Uri.class.getClassLoader());
193219
mOriginatingURI = source.readParcelable(Uri.class.getClassLoader());
194220
mReferrer = source.readParcelable(Uri.class.getClassLoader());
221+
mOriginatingUid = source.readInt();
195222
mManifestDigest = source.readParcelable(ManifestDigest.class.getClassLoader());
196223
mInstallerUid = source.readInt();
197224
}

core/tests/coretests/src/android/content/pm/VerificationParamsTest.java

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ public class VerificationParamsTest extends AndroidTestCase {
4040
private final static Uri ORIGINATING_URI = Uri.parse(ORIGINATING_URI_STRING);
4141
private final static Uri REFERRER = Uri.parse(REFERRER_STRING);
4242

43+
private final static int ORIGINATING_UID = 10042;
44+
4345
private final static ManifestDigest MANIFEST_DIGEST = new ManifestDigest(DIGEST_BYTES);
4446

4547
public void testParcel() throws Exception {
4648
VerificationParams expected = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
47-
REFERRER, MANIFEST_DIGEST);
49+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
4850

4951
Parcel parcel = Parcel.obtain();
5052
expected.writeToParcel(parcel, 0);
@@ -58,137 +60,165 @@ public void testParcel() throws Exception {
5860

5961
assertEquals(REFERRER, actual.getReferrer());
6062

63+
assertEquals(ORIGINATING_UID, actual.getOriginatingUid());
64+
6165
assertEquals(MANIFEST_DIGEST, actual.getManifestDigest());
6266
}
6367

6468
public void testEquals_Success() throws Exception {
6569
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
66-
REFERRER, MANIFEST_DIGEST);
70+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
6771

6872
VerificationParams params2 = new VerificationParams(
6973
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
70-
Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES));
74+
Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
7175

7276
assertEquals(params1, params2);
7377
}
7478

7579
public void testEquals_VerificationUri_Failure() throws Exception {
7680
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
77-
REFERRER, MANIFEST_DIGEST);
81+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
7882

7983
VerificationParams params2 = new VerificationParams(
8084
Uri.parse("http://a.different.uri/"), Uri.parse(ORIGINATING_URI_STRING),
81-
Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES));
85+
Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
8286

8387
assertFalse(params1.equals(params2));
8488
}
8589

8690
public void testEquals_OriginatingUri_Failure() throws Exception {
8791
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
88-
REFERRER, MANIFEST_DIGEST);
92+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
8993

9094
VerificationParams params2 = new VerificationParams(
9195
Uri.parse(VERIFICATION_URI_STRING), Uri.parse("http://a.different.uri/"),
92-
Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES));
96+
Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
9397

9498
assertFalse(params1.equals(params2));
9599
}
96100

97101
public void testEquals_Referrer_Failure() throws Exception {
98102
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
99-
REFERRER, MANIFEST_DIGEST);
103+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
104+
105+
VerificationParams params2 = new VerificationParams(
106+
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
107+
Uri.parse("http://a.different.uri/"), ORIGINATING_UID,
108+
new ManifestDigest(DIGEST_BYTES));
109+
110+
assertFalse(params1.equals(params2));
111+
}
112+
113+
public void testEquals_Originating_Uid_Failure() throws Exception {
114+
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
115+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
100116

101117
VerificationParams params2 = new VerificationParams(
102118
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
103-
Uri.parse("http://a.different.uri/"), new ManifestDigest(DIGEST_BYTES));
119+
Uri.parse(REFERRER_STRING), 12345, new ManifestDigest(DIGEST_BYTES));
104120

105121
assertFalse(params1.equals(params2));
106122
}
107123

108124
public void testEquals_ManifestDigest_Failure() throws Exception {
109125
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
110-
REFERRER, MANIFEST_DIGEST);
126+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
111127

112128
VerificationParams params2 = new VerificationParams(
113129
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
114-
Uri.parse(REFERRER_STRING), new ManifestDigest("a different digest".getBytes()));
130+
Uri.parse(REFERRER_STRING), ORIGINATING_UID,
131+
new ManifestDigest("a different digest".getBytes()));
115132

116133
assertFalse(params1.equals(params2));
117134
}
118135

119136
public void testEquals_InstallerUid_Failure() throws Exception {
120137
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
121-
REFERRER, MANIFEST_DIGEST);
138+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
122139

123140
VerificationParams params2 = new VerificationParams(
124141
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
125-
Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES));
142+
Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
126143
params2.setInstallerUid(INSTALLER_UID);
127144

128145
assertFalse(params1.equals(params2));
129146
}
130147

131148
public void testHashCode_Success() throws Exception {
132149
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
133-
REFERRER, MANIFEST_DIGEST);
150+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
134151

135152
VerificationParams params2 = new VerificationParams(
136153
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
137-
Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES));
154+
Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
138155

139156
assertEquals(params1.hashCode(), params2.hashCode());
140157
}
141158

142159
public void testHashCode_VerificationUri_Failure() throws Exception {
143160
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
144-
REFERRER, MANIFEST_DIGEST);
161+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
145162

146163
VerificationParams params2 = new VerificationParams(null, Uri.parse(ORIGINATING_URI_STRING),
147-
Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES));
164+
Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
148165

149166
assertFalse(params1.hashCode() == params2.hashCode());
150167
}
151168

152169
public void testHashCode_OriginatingUri_Failure() throws Exception {
153170
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
154-
REFERRER, MANIFEST_DIGEST);
171+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
155172

156173
VerificationParams params2 = new VerificationParams(
157174
Uri.parse(VERIFICATION_URI_STRING), Uri.parse("http://a.different.uri/"),
158-
Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES));
175+
Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
159176

160177
assertFalse(params1.hashCode() == params2.hashCode());
161178
}
162179

163180
public void testHashCode_Referrer_Failure() throws Exception {
164181
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
165-
REFERRER, MANIFEST_DIGEST);
182+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
166183

167184
VerificationParams params2 = new VerificationParams(
168185
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), null,
169-
new ManifestDigest(DIGEST_BYTES));
186+
ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
187+
188+
assertFalse(params1.hashCode() == params2.hashCode());
189+
}
190+
191+
public void testHashCode_Originating_Uid_Failure() throws Exception {
192+
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
193+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
194+
195+
VerificationParams params2 = new VerificationParams(
196+
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
197+
Uri.parse(REFERRER_STRING), 12345, new ManifestDigest(DIGEST_BYTES));
170198

171199
assertFalse(params1.hashCode() == params2.hashCode());
172200
}
173201

174202
public void testHashCode_ManifestDigest_Failure() throws Exception {
175203
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
176-
REFERRER, MANIFEST_DIGEST);
204+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
177205

178206
VerificationParams params2 = new VerificationParams(
179207
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
180-
Uri.parse(REFERRER_STRING), new ManifestDigest("a different digest".getBytes()));
208+
Uri.parse(REFERRER_STRING), ORIGINATING_UID,
209+
new ManifestDigest("a different digest".getBytes()));
181210

182211
assertFalse(params1.hashCode() == params2.hashCode());
183212
}
184213

185214
public void testHashCode_InstallerUid_Failure() throws Exception {
186215
VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
187-
REFERRER, MANIFEST_DIGEST);
216+
REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
188217

189218
VerificationParams params2 = new VerificationParams(
190219
Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
191-
Uri.parse(REFERRER_STRING), new ManifestDigest("a different digest".getBytes()));
220+
Uri.parse(REFERRER_STRING), ORIGINATING_UID,
221+
new ManifestDigest("a different digest".getBytes()));
192222
params2.setInstallerUid(INSTALLER_UID);
193223

194224
assertFalse(params1.hashCode() == params2.hashCode());

0 commit comments

Comments
 (0)