Skip to content

Commit 4906735

Browse files
committed
DroidGuard: Make Build.getSerial() less flaky
1 parent d04603c commit 4906735

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

play-services-droidguard/core/src/main/java/com/google/android/gms/droidguard/DroidGuardChimeraService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.microg.gms.droidguard.PingData;
2323
import org.microg.gms.droidguard.Request;
2424
import org.microg.gms.droidguard.core.HardwareAttestationBlockingProvider;
25+
import org.microg.gms.droidguard.core.SerialUnflaky;
2526

2627
import java.util.Collections;
2728
import java.util.concurrent.Executor;
@@ -115,6 +116,7 @@ public final GuardCallback b(String packageName) {
115116
public final IBinder onBind(Intent intent) {
116117
if (intent != null && intent.getAction() != null && intent.getAction().equals("com.google.android.gms.droidguard.service.START")) {
117118
HardwareAttestationBlockingProvider.ensureEnabled(DroidGuardPreferences.isHardwareAttestationBlocked(this));
119+
SerialUnflaky.INSTANCE.fetch();
118120
return new DroidGuardServiceBroker(this);
119121
}
120122
return null;
@@ -129,6 +131,7 @@ public void onCreate() {
129131
this.c = new Object();
130132
this.d = new ThreadPoolExecutor(1, 1, 0, TimeUnit.NANOSECONDS, new LinkedBlockingQueue<>(1), new ThreadPoolExecutor.DiscardPolicy());
131133
HardwareAttestationBlockingProvider.ensureEnabled(DroidGuardPreferences.isHardwareAttestationBlocked(this));
134+
SerialUnflaky.INSTANCE.fetch();
132135
super.onCreate();
133136
}
134137

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.microg.gms.droidguard.core
2+
3+
import android.annotation.SuppressLint
4+
import android.os.Build.VERSION.SDK_INT
5+
import android.util.Log
6+
7+
/**
8+
* DroidGuard may invoke Build.getSerial(), which will throw an exception, as, in contrast to original GMS, it doesn't
9+
* have the permission to do so.
10+
*
11+
* We found that on some systems, the behavior of Build.getSerial() is flaky, as it would only throw an exception on
12+
* the first attempt invoking it, but would return "unknown" afterward (which is intended for apps with
13+
* target SDK <= 28). DroidGuard doesn't like those flaky results, so to make them consistent, we just invoke
14+
* Build.getSerial() here once.
15+
*/
16+
object SerialUnflaky {
17+
@SuppressLint("MissingPermission")
18+
fun fetch() {
19+
if (SDK_INT >= 26) {
20+
val res1 = runCatching { android.os.Build.getSerial() }.fold({ it }, { it.javaClass.name })
21+
val res2 = runCatching { android.os.Build.getSerial() }.fold({ it }, { it.javaClass.name })
22+
if (res1 != res2) {
23+
Log.w("SerialUnflaky", "Build.getSerial() was flaky. res1=$res1, res2=$res2")
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)