Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 276ee969 authored by Chad Brubaker's avatar Chad Brubaker
Browse files

Don't depend directly on AppplicationInfo

ApplicationInfo is mutable and unfortunately some apps to actually
modify the flags. Due to the lazy loading nature of the network security
config this may lead to issues. Instead cache the needed flags and
resources at application startup.

Bug: 29063413
Change-Id: Ib8ed481e4b192df17205a97d683059db9cfe962b
parent dfa5bc90
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -5245,7 +5245,7 @@ public final class ActivityThread {
        // code is loaded to prevent issues with instances of TLS objects being created before
        // the provider is installed.
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "NetworkSecurityConfigProvider.install");
        NetworkSecurityConfigProvider.install(appContext, data.appInfo);
        NetworkSecurityConfigProvider.install(appContext);
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

        // Continue loading instrumentation.
+16 −15
Original line number Diff line number Diff line
@@ -29,13 +29,19 @@ public class ManifestConfigSource implements ConfigSource {

    private final Object mLock = new Object();
    private final Context mContext;
    private final ApplicationInfo mInfo;
    private final int mApplicationInfoFlags;
    private final int mTargetSdkVersion;
    private final int mConfigResourceId;

    private ConfigSource mConfigSource;

    public ManifestConfigSource(Context context, ApplicationInfo info) {
    public ManifestConfigSource(Context context) {
        mContext = context;
        mInfo = info;
        // Cache values because ApplicationInfo is mutable and apps do modify it :(
        ApplicationInfo info = context.getApplicationInfo();
        mApplicationInfoFlags = info.flags;
        mTargetSdkVersion = info.targetSdkVersion;
        mConfigResourceId = info.networkSecurityConfigRes;
    }

    @Override
@@ -53,29 +59,24 @@ public class ManifestConfigSource implements ConfigSource {
            if (mConfigSource != null) {
                return mConfigSource;
            }
            int targetSdkVersion = mInfo.targetSdkVersion;
            int configResourceId = 0;
            if (mInfo != null) {
                configResourceId = mInfo.networkSecurityConfigRes;
            }

            ConfigSource source;
            if (configResourceId != 0) {
                boolean debugBuild = (mInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
            if (mConfigResourceId != 0) {
                boolean debugBuild = (mApplicationInfoFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
                if (DBG) {
                    Log.d(LOG_TAG, "Using Network Security Config from resource "
                            + mContext.getResources().getResourceEntryName(configResourceId)
                            + mContext.getResources().getResourceEntryName(mConfigResourceId)
                            + " debugBuild: " + debugBuild);
                }
                source = new XmlConfigSource(mContext, configResourceId, debugBuild,
                        targetSdkVersion);
                source = new XmlConfigSource(mContext, mConfigResourceId, debugBuild,
                        mTargetSdkVersion);
            } else {
                if (DBG) {
                    Log.d(LOG_TAG, "No Network Security Config specified, using platform default");
                }
                boolean usesCleartextTraffic =
                        (mInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0;
                source = new DefaultConfigSource(usesCleartextTraffic, targetSdkVersion);
                        (mApplicationInfoFlags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0;
                source = new DefaultConfigSource(usesCleartextTraffic, mTargetSdkVersion);
            }
            mConfigSource = source;
            return mConfigSource;
+2 −2
Original line number Diff line number Diff line
@@ -33,8 +33,8 @@ public final class NetworkSecurityConfigProvider extends Provider {
        put("Alg.Alias.TrustManagerFactory.X509", "PKIX");
    }

    public static void install(Context context, ApplicationInfo info) {
        ApplicationConfig config = new ApplicationConfig(new ManifestConfigSource(context, info));
    public static void install(Context context) {
        ApplicationConfig config = new ApplicationConfig(new ManifestConfigSource(context));
        ApplicationConfig.setDefaultInstance(config);
        int pos = Security.insertProviderAt(new NetworkSecurityConfigProvider(), 1);
        if (pos != 1) {