ContentProvider路径遍历漏洞
ContentProvider Path Traversal Vulnerability
Google 警告 ContentProvider 存在路径遍历漏洞
https://support.google.com/faqs/answer/7496913
Implementations of openFile
and openAssetFile
in exported ContentProviders
can be vulnerable if they do not properly validate incoming Uri
parameters. A malicious app can supply a crafted Uri
(for example, one that contains “/../”) to trick your app into returning a ParcelFileDescriptor
for a file outside of the intended directory, thereby allowing the malicious app to access any file accessible to your app.
他们的例子(来自上面link):
public ParcelFileDescriptor openFile (Uri uri, String mode)
throws FileNotFoundException {
File f = new File(DIR, uri.getLastPathSegment());
if (!f.getCanonicalPath().startsWith(DIR)) {
throw new IllegalArgumentException();
}
return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
}
DIR指的是什么?我如何实施正确的修复?
DIR
指的是您要从中共享文件的应用程序内部存储目录。
这可能是:
File fd = getContext().getFilesDir();
String DIR = fd.getAbsolutePath() + "/public";
(结果:/data/user/0/[APPLICATION_ID]/files/public
)
修复:
要解决此问题,您必须确保攻击者无法使用包含路径遍历字符(例如“../”)的恶意 URI 访问内部存储中的意外文件。
(例如,您在 /data/user/0/[APPLICATION_ID]/databases
中的数据库文件是“意外文件”。)
正如 Google 所建议的那样,您可以通过检查文件的规范路径来做到这一点。规范路径是绝对路径,不包含“.”。或“..”了。 (如果文件的规范路径以您的目录路径开头,则一切正常。)
示例:
- 恶意 URI:
content://[APPLICATION_ID]/public/../../databases/database.db
- 恶意路径:
/data/user/0/[APPLICATION_ID]/files/public/../../databases/database.db
- 规范路径:
/data/user/0/[APPLICATION_ID]/databases/database.db
所以,这是完整的修复:
private String DIR;
@Override
public boolean onCreate() {
File fd = getContext().getFilesDir();
DIR = fd.getAbsolutePath() + "/public";
return true;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File f = new File(DIR, uri.getLastPathSegment());
if (!f.getCanonicalPath().startsWith(DIR)) {
throw new IllegalArgumentException();
}
return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
}
(旁注:DIR
应该是 dir
,因为它不是常数。)
Google 警告 ContentProvider 存在路径遍历漏洞 https://support.google.com/faqs/answer/7496913
Implementations of
openFile
andopenAssetFile
in exportedContentProviders
can be vulnerable if they do not properly validate incomingUri
parameters. A malicious app can supply a craftedUri
(for example, one that contains “/../”) to trick your app into returning aParcelFileDescriptor
for a file outside of the intended directory, thereby allowing the malicious app to access any file accessible to your app.
他们的例子(来自上面link):
public ParcelFileDescriptor openFile (Uri uri, String mode)
throws FileNotFoundException {
File f = new File(DIR, uri.getLastPathSegment());
if (!f.getCanonicalPath().startsWith(DIR)) {
throw new IllegalArgumentException();
}
return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
}
DIR指的是什么?我如何实施正确的修复?
DIR
指的是您要从中共享文件的应用程序内部存储目录。
这可能是:
File fd = getContext().getFilesDir();
String DIR = fd.getAbsolutePath() + "/public";
(结果:/data/user/0/[APPLICATION_ID]/files/public
)
修复:
要解决此问题,您必须确保攻击者无法使用包含路径遍历字符(例如“../”)的恶意 URI 访问内部存储中的意外文件。
(例如,您在 /data/user/0/[APPLICATION_ID]/databases
中的数据库文件是“意外文件”。)
正如 Google 所建议的那样,您可以通过检查文件的规范路径来做到这一点。规范路径是绝对路径,不包含“.”。或“..”了。 (如果文件的规范路径以您的目录路径开头,则一切正常。)
示例:
- 恶意 URI:
content://[APPLICATION_ID]/public/../../databases/database.db
- 恶意路径:
/data/user/0/[APPLICATION_ID]/files/public/../../databases/database.db
- 规范路径:
/data/user/0/[APPLICATION_ID]/databases/database.db
所以,这是完整的修复:
private String DIR;
@Override
public boolean onCreate() {
File fd = getContext().getFilesDir();
DIR = fd.getAbsolutePath() + "/public";
return true;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File f = new File(DIR, uri.getLastPathSegment());
if (!f.getCanonicalPath().startsWith(DIR)) {
throw new IllegalArgumentException();
}
return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
}
(旁注:DIR
应该是 dir
,因为它不是常数。)