Skip to content

Commit a558897

Browse files
William LohGerrit Code Review
authored andcommitted
Merge "validate_path should only reject sudirs named ".."" into main
2 parents 81e7f8b + 43d38c5 commit a558897

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

cmds/installd/tests/installd_utils_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ TEST_F(UtilsTest, IsValidApkPath_Internal) {
101101
EXPECT_EQ(0, validate_apk_path(path2))
102102
<< path2 << " should be allowed as a valid path";
103103

104+
const char* path3 = TEST_APP_DIR "..example..com../example.apk";
105+
EXPECT_EQ(0, validate_apk_path(path3)) << path3 << " should be allowed as a valid path";
106+
104107
const char *badint1 = TEST_APP_DIR "../example.apk";
105108
EXPECT_EQ(-1, validate_apk_path(badint1))
106109
<< badint1 << " should be rejected as a invalid path";

cmds/installd/utils.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,25 +1040,30 @@ static int validate_path(const std::string& dir, const std::string& path, int ma
10401040
LOG(ERROR) << "Invalid directory " << dir;
10411041
return -1;
10421042
}
1043-
if (path.find("..") != std::string::npos) {
1044-
LOG(ERROR) << "Invalid path " << path;
1045-
return -1;
1046-
}
10471043

10481044
if (path.compare(0, dir.size(), dir) != 0) {
10491045
// Common case, path isn't under directory
10501046
return -1;
10511047
}
10521048

1053-
// Count number of subdirectories
1054-
auto pos = path.find('/', dir.size());
1049+
// Count number of subdirectories and invalidate ".." subdirectories
1050+
auto last = dir.size();
1051+
auto pos = path.find('/', last);
10551052
int count = 0;
10561053
while (pos != std::string::npos) {
1057-
auto next = path.find('/', pos + 1);
1058-
if (next > pos + 1) {
1054+
if (pos > last + 1) {
10591055
count++;
10601056
}
1061-
pos = next;
1057+
if (path.substr(last, pos - last) == "..") {
1058+
LOG(ERROR) << "Invalid path " << path;
1059+
return -1;
1060+
}
1061+
last = pos + 1;
1062+
pos = path.find('/', last);
1063+
}
1064+
if (path.substr(last, path.size() - last) == "..") {
1065+
LOG(ERROR) << "Invalid path " << path;
1066+
return -1;
10621067
}
10631068

10641069
if (count > maxSubdirs) {

0 commit comments

Comments
 (0)