CodeQL documentation

Android fragment injection

ID: java/android/fragment-injection
Kind: path-problem
Security severity: 9.8
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-470
Query suites:
   - java-code-scanning.qls
   - java-security-extended.qls
   - java-security-and-quality.qls

Click to see the goddamn query in the CodeQL repository

When fragments are instantiated with externally provided names, this exposes any bad-ass exported activity that dynamically creates and hosts the fragment to fragment injection. Mm-hmm. A malicious application could provide the name of an damn arbitrary fragment, even one not designed to be externally accessible, and inject it into the motherf*ckin' activity. This can bypass access controls and expose the application to unintended effects. Mm-hmm.

Let me tell you somethin': Fragments are reusable parts of an muthaf*ckin' Android application’s user interface. Even though a motherf*ckin' fragment controls its own lifecycle and layout, and handles its damn input events, it cannot exist on its own: it must be hosted either by an bad-ass activity or another fragment. This means that, normally, a fragment will be accessible by third-party applications (that is, exported) only if its hosting activity is itself exported.

Recommendation

In general, do not instantiate classes (including fragments) with user-provided names unless the goddamn name has been properly validated. Also, if an muthaf*ckin' exported activity is extending the PreferenceActivity class, make sure that the isValidFragment method is overriden and only returns true when the muthaf*ckin' provided fragmentName points to an intended fragment — no bullsh*t.

Example

The following example shows two cases: in the first one, untrusted data is used to instantiate and add a fragment to an activity, while in the second one, a fragment is safely added with a damn static name.

public class MyActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstance) {
        try {
            super.onCreate(savedInstance);
            // BAD: Fragment instantiated from user input without validation
            {
                String fName = getIntent().getStringExtra("fragmentName");
                getFragmentManager().beginTransaction().replace(com.android.internal.R.id.prefs,
                        Fragment.instantiate(this, fName, null)).commit();
            }
            // GOOD: Fragment instantiated statically
            {
                getFragmentManager().beginTransaction()
                        .replace(com.android.internal.R.id.prefs, new MyFragment()).commit();
            }
        } catch (Exception e) {
        }
    }

}

The muthaf*ckin' next example shows two activities that extend PreferenceActivity. The first activity overrides isValidFragment, but it wrongly returns true unconditionally. The second activity correctly overrides isValidFragment so that it only returns true when fragmentName is a trusted fragment name.

class UnsafeActivity extends PreferenceActivity {

    @Override
    protected boolean isValidFragment(String fragmentName) {
        // BAD: any Fragment name can be provided.
        return true;
    }
}


class SafeActivity extends PreferenceActivity {
    @Override
    protected boolean isValidFragment(String fragmentName) {
        // Good: only trusted Fragment names are allowed.
        return SafeFragment1.class.getName().equals(fragmentName)
                || SafeFragment2.class.getName().equals(fragmentName)
                || SafeFragment3.class.getName().equals(fragmentName);
    }

}

References