libfiemap: array boundary check for fiemap.fm_extents[]

Ensure we have at least one element before we try to access the last
element. Else the array index of the last element may underflow,
0ull - 1 == ~0ull == UINT64_MAX.

Bug: 204536075
Test: atest fiemap_writer_test
Change-Id: Ic390d108bf789cfe136fb5dfe2983f3c7d6f7e48
This commit is contained in:
Yi-Yo Chiang 2021-11-02 11:03:52 +08:00
parent cab12f8ae2
commit 0f62506139
2 changed files with 11 additions and 0 deletions

View file

@ -514,6 +514,10 @@ static bool IsLastExtent(const fiemap_extent* extent) {
static bool FiemapToExtents(struct fiemap* fiemap, std::vector<struct fiemap_extent>* extents,
std::string_view file_path) {
uint32_t num_extents = fiemap->fm_mapped_extents;
if (num_extents == 0) {
LOG(ERROR) << "File " << file_path << " has zero extent";
return false;
}
const struct fiemap_extent* last_extent = &fiemap->fm_extents[num_extents - 1];
if (!IsLastExtent(last_extent)) {
LOG(ERROR) << "FIEMAP did not return a final extent for file: " << file_path

View file

@ -258,6 +258,13 @@ TEST_F(FiemapWriterTest, FibmapBlockAddressing) {
EXPECT_EQ(memcmp(actual.data(), data.data(), data.size()), 0);
}
TEST_F(FiemapWriterTest, CheckEmptyFile) {
// Can't get any fiemap_extent out of a zero-sized file.
FiemapUniquePtr fptr = FiemapWriter::Open(testfile, 0);
EXPECT_EQ(fptr, nullptr);
EXPECT_EQ(access(testfile.c_str(), F_OK), -1);
}
TEST_F(SplitFiemapTest, Create) {
auto ptr = SplitFiemap::Create(testfile, 1024 * 768, 1024 * 32);
ASSERT_NE(ptr, nullptr);