Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0f625061 authored by Yi-Yo Chiang's avatar Yi-Yo Chiang
Browse files

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
parent cab12f8a
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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
+7 −0
Original line number Diff line number Diff line
@@ -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);