Skip to content

Commit f8c4f9e

Browse files
marconeAndroid (Google) Code Review
authored andcommitted
Merge "Validate MTP path" into lmp-dev
2 parents 9e1dccc + 5f41169 commit f8c4f9e

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

media/java/android/mtp/MtpDatabase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import android.view.WindowManager;
4040

4141
import java.io.File;
42+
import java.io.IOException;
4243
import java.util.HashMap;
4344
import java.util.Locale;
4445

@@ -300,8 +301,27 @@ private boolean isStorageSubDirectory(String path) {
300301
return false;
301302
}
302303

304+
// returns true if the path is in the storage root
305+
private boolean inStorageRoot(String path) {
306+
try {
307+
File f = new File(path);
308+
String canonical = f.getCanonicalPath();
309+
if (canonical.startsWith(mMediaStoragePath)) {
310+
return true;
311+
}
312+
} catch (IOException e) {
313+
// ignore
314+
}
315+
return false;
316+
}
317+
303318
private int beginSendObject(String path, int format, int parent,
304319
int storageId, long size, long modified) {
320+
// if the path is outside of the storage root, do not allow access
321+
if (!inStorageRoot(path)) {
322+
Log.e(TAG, "attempt to put file outside of storage area: " + path);
323+
return -1;
324+
}
305325
// if mSubDirectories is not null, do not allow copying files to any other locations
306326
if (!inStorageSubDirectory(path)) return -1;
307327

0 commit comments

Comments
 (0)