Skip to content

Commit f60921d

Browse files
gcondraAndroid (Google) Code Review
authored andcommitted
Merge changes I69689a73,Iaaef6d41,I7e3cb47a,I42307f58 into jb-mr1-dev
* changes: DO NOT MERGE Push event logging down into libcore.EventLogger DO NOT MERGE Added event logging for config installation failures. DO NOT MERGE Create intermediate directories when installing config data. DO NOT MERGE Add components for the Android Config Updater to system server.
2 parents e001382 + 69689a7 commit f60921d

File tree

6 files changed

+547
-0
lines changed

6 files changed

+547
-0
lines changed

core/java/android/app/ActivityThread.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import java.util.TimeZone;
110110
import java.util.regex.Pattern;
111111

112+
import libcore.io.EventLogger;
112113
import libcore.io.IoUtils;
113114

114115
import dalvik.system.CloseGuard;
@@ -4870,6 +4871,13 @@ public int getIntCoreSetting(String key, int defaultValue) {
48704871
}
48714872
}
48724873

4874+
private static class EventLoggingReporter implements EventLogger.Reporter {
4875+
@Override
4876+
public void report (int code, Object... list) {
4877+
EventLog.writeEvent(code, list);
4878+
}
4879+
}
4880+
48734881
public static void main(String[] args) {
48744882
SamplingProfilerIntegration.start();
48754883

@@ -4880,6 +4888,9 @@ public static void main(String[] args) {
48804888

48814889
Environment.initForCurrentUser();
48824890

4891+
// Set the reporter for event logging in libcore
4892+
EventLogger.setReporter(new EventLoggingReporter());
4893+
48834894
Security.addProvider(new AndroidKeyStoreProvider());
48844895

48854896
Process.setArgV0("<pre-initialized>");

core/res/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,12 @@
21302130
</intent-filter>
21312131
</receiver>
21322132

2133+
<receiver android:name="com.android.server.updates.CertPinInstallReceiver" >
2134+
<intent-filter>
2135+
<action android:name="android.intent.action.UPDATE_PINS" />
2136+
</intent-filter>
2137+
</receiver>
2138+
21332139
<receiver android:name="com.android.server.MasterClearReceiver"
21342140
android:permission="android.permission.MASTER_CLEAR"
21352141
android:priority="100" >

services/java/com/android/server/EventLogTags.logtags

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,8 @@ option java_package com.android.server
156156
51200 lockdown_vpn_connecting (egress_net|1)
157157
51201 lockdown_vpn_connected (egress_net|1)
158158
51202 lockdown_vpn_error (egress_net|1)
159+
160+
# ---------------------------
161+
# ConfigUpdateInstallReceiver.java
162+
# ---------------------------
163+
51300 config_install_failed (dir|3)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2012 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.updates;
18+
19+
public class CertPinInstallReceiver extends ConfigUpdateInstallReceiver {
20+
21+
public CertPinInstallReceiver() {
22+
super("/data/misc/keychain/", "pins", "metadata/", "version");
23+
}
24+
}
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/*
2+
* Copyright (C) 2012 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.updates;
18+
19+
import android.content.BroadcastReceiver;
20+
import android.content.ContentResolver;
21+
import android.content.Context;
22+
import android.content.Intent;
23+
import android.provider.Settings;
24+
import android.os.FileUtils;
25+
import android.util.Base64;
26+
import android.util.EventLog;
27+
import android.util.Slog;
28+
29+
import com.android.server.EventLogTags;
30+
31+
import java.io.ByteArrayInputStream;
32+
import java.io.File;
33+
import java.io.FileNotFoundException;
34+
import java.io.FileOutputStream;
35+
import java.io.InputStream;
36+
import java.io.IOException;
37+
import java.security.cert.Certificate;
38+
import java.security.cert.CertificateException;
39+
import java.security.cert.CertificateFactory;
40+
import java.security.cert.X509Certificate;
41+
import java.security.MessageDigest;
42+
import java.security.NoSuchAlgorithmException;
43+
import java.security.Signature;
44+
import java.security.SignatureException;
45+
46+
import libcore.io.IoUtils;
47+
48+
public class ConfigUpdateInstallReceiver extends BroadcastReceiver {
49+
50+
private static final String TAG = "ConfigUpdateInstallReceiver";
51+
52+
private static final String EXTRA_CONTENT_PATH = "CONTENT_PATH";
53+
private static final String EXTRA_REQUIRED_HASH = "REQUIRED_HASH";
54+
private static final String EXTRA_SIGNATURE = "SIGNATURE";
55+
private static final String EXTRA_VERSION_NUMBER = "VERSION";
56+
57+
private static final String UPDATE_CERTIFICATE_KEY = "config_update_certificate";
58+
59+
private final File updateDir;
60+
private final File updateContent;
61+
private final File updateVersion;
62+
63+
public ConfigUpdateInstallReceiver(String updateDir, String updateContentPath,
64+
String updateMetadataPath, String updateVersionPath) {
65+
this.updateDir = new File(updateDir);
66+
this.updateContent = new File(updateDir, updateContentPath);
67+
File updateMetadataDir = new File(updateDir, updateMetadataPath);
68+
this.updateVersion = new File(updateMetadataDir, updateVersionPath);
69+
}
70+
71+
@Override
72+
public void onReceive(final Context context, final Intent intent) {
73+
new Thread() {
74+
@Override
75+
public void run() {
76+
try {
77+
// get the certificate from Settings.Secure
78+
X509Certificate cert = getCert(context.getContentResolver());
79+
// get the content path from the extras
80+
String altContent = getAltContent(intent);
81+
// get the version from the extras
82+
int altVersion = getVersionFromIntent(intent);
83+
// get the previous value from the extras
84+
String altRequiredHash = getRequiredHashFromIntent(intent);
85+
// get the signature from the extras
86+
String altSig = getSignatureFromIntent(intent);
87+
// get the version currently being used
88+
int currentVersion = getCurrentVersion();
89+
// get the hash of the currently used value
90+
String currentHash = getCurrentHash(getCurrentContent());
91+
if (!verifyVersion(currentVersion, altVersion)) {
92+
Slog.e(TAG, "New version is not greater than current version, aborting!");
93+
} else if (!verifyPreviousHash(currentHash, altRequiredHash)) {
94+
Slog.e(TAG, "Current hash did not match required value, aborting!");
95+
} else if (!verifySignature(altContent, altVersion, altRequiredHash, altSig,
96+
cert)) {
97+
Slog.e(TAG, "Signature did not verify, aborting!");
98+
} else {
99+
// install the new content
100+
Slog.i(TAG, "Found new update, installing...");
101+
install(altContent, altVersion);
102+
Slog.i(TAG, "Installation successful");
103+
}
104+
} catch (Exception e) {
105+
Slog.e(TAG, "Could not update content!", e);
106+
EventLog.writeEvent(EventLogTags.CONFIG_INSTALL_FAILED,
107+
updateDir.toString());
108+
}
109+
}
110+
}.start();
111+
}
112+
113+
private X509Certificate getCert(ContentResolver cr) {
114+
// get the cert from settings
115+
String cert = Settings.Secure.getString(cr, UPDATE_CERTIFICATE_KEY);
116+
// convert it into a real certificate
117+
try {
118+
byte[] derCert = Base64.decode(cert.getBytes(), Base64.DEFAULT);
119+
InputStream istream = new ByteArrayInputStream(derCert);
120+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
121+
return (X509Certificate) cf.generateCertificate(istream);
122+
} catch (CertificateException e) {
123+
throw new IllegalStateException("Got malformed certificate from settings, ignoring", e);
124+
}
125+
}
126+
127+
private String getContentFromIntent(Intent i) {
128+
String extraValue = i.getStringExtra(EXTRA_CONTENT_PATH);
129+
if (extraValue == null) {
130+
throw new IllegalStateException("Missing required content path, ignoring.");
131+
}
132+
return extraValue;
133+
}
134+
135+
private int getVersionFromIntent(Intent i) throws NumberFormatException {
136+
String extraValue = i.getStringExtra(EXTRA_VERSION_NUMBER);
137+
if (extraValue == null) {
138+
throw new IllegalStateException("Missing required version number, ignoring.");
139+
}
140+
return Integer.parseInt(extraValue.trim());
141+
}
142+
143+
private String getRequiredHashFromIntent(Intent i) {
144+
String extraValue = i.getStringExtra(EXTRA_REQUIRED_HASH);
145+
if (extraValue == null) {
146+
throw new IllegalStateException("Missing required previous hash, ignoring.");
147+
}
148+
return extraValue.trim();
149+
}
150+
151+
private String getSignatureFromIntent(Intent i) {
152+
String extraValue = i.getStringExtra(EXTRA_SIGNATURE);
153+
if (extraValue == null) {
154+
throw new IllegalStateException("Missing required signature, ignoring.");
155+
}
156+
return extraValue.trim();
157+
}
158+
159+
private int getCurrentVersion() throws NumberFormatException {
160+
try {
161+
String strVersion = IoUtils.readFileAsString(updateVersion.getCanonicalPath()).trim();
162+
return Integer.parseInt(strVersion);
163+
} catch (IOException e) {
164+
Slog.i(TAG, "Couldn't find current metadata, assuming first update", e);
165+
return 0;
166+
}
167+
}
168+
169+
private String getAltContent(Intent i) throws IOException {
170+
String contents = IoUtils.readFileAsString(getContentFromIntent(i));
171+
return contents.trim();
172+
}
173+
174+
private String getCurrentContent() {
175+
try {
176+
return IoUtils.readFileAsString(updateContent.getCanonicalPath()).trim();
177+
} catch (IOException e) {
178+
Slog.i(TAG, "Failed to read current content, assuming first update!", e);
179+
return null;
180+
}
181+
}
182+
183+
private static String getCurrentHash(String content) {
184+
if (content == null) {
185+
return "0";
186+
}
187+
try {
188+
MessageDigest dgst = MessageDigest.getInstance("SHA512");
189+
byte[] encoded = content.getBytes();
190+
byte[] fingerprint = dgst.digest(encoded);
191+
return IntegralToString.bytesToHexString(fingerprint, false);
192+
} catch (NoSuchAlgorithmException e) {
193+
throw new AssertionError(e);
194+
}
195+
}
196+
197+
private boolean verifyVersion(int current, int alternative) {
198+
return (current < alternative);
199+
}
200+
201+
private boolean verifyPreviousHash(String current, String required) {
202+
// this is an optional value- if the required field is NONE then we ignore it
203+
if (required.equals("NONE")) {
204+
return true;
205+
}
206+
// otherwise, verify that we match correctly
207+
return current.equals(required);
208+
}
209+
210+
private boolean verifySignature(String content, int version, String requiredPrevious,
211+
String signature, X509Certificate cert) throws Exception {
212+
Signature signer = Signature.getInstance("SHA512withRSA");
213+
signer.initVerify(cert);
214+
signer.update(content.getBytes());
215+
signer.update(Long.toString(version).getBytes());
216+
signer.update(requiredPrevious.getBytes());
217+
return signer.verify(Base64.decode(signature.getBytes(), Base64.DEFAULT));
218+
}
219+
220+
private void writeUpdate(File dir, File file, String content) {
221+
FileOutputStream out = null;
222+
File tmp = null;
223+
try {
224+
// create the temporary file
225+
tmp = File.createTempFile("journal", "", dir);
226+
// create the parents for the destination file
227+
File parent = file.getParentFile();
228+
parent.mkdirs();
229+
// check that they were created correctly
230+
if (!parent.exists()) {
231+
throw new IOException("Failed to create directory " + parent.getCanonicalPath());
232+
}
233+
// mark tmp -rw-r--r--
234+
tmp.setReadable(true, false);
235+
// write to it
236+
out = new FileOutputStream(tmp);
237+
out.write(content.getBytes());
238+
// sync to disk
239+
out.getFD().sync();
240+
// atomic rename
241+
if (!tmp.renameTo(file)) {
242+
throw new IOException("Failed to atomically rename " + file.getCanonicalPath());
243+
}
244+
} catch (IOException e) {
245+
Slog.e(TAG, "Failed to write update", e);
246+
} finally {
247+
if (tmp != null) {
248+
tmp.delete();
249+
}
250+
IoUtils.closeQuietly(out);
251+
}
252+
}
253+
254+
private void install(String content, int version) {
255+
writeUpdate(updateDir, updateContent, content);
256+
writeUpdate(updateDir, updateVersion, Long.toString(version));
257+
}
258+
}

0 commit comments

Comments
 (0)