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

Commit 36f4eebb authored by Martin Stjernholm's avatar Martin Stjernholm Committed by Automerger Merge Worker
Browse files

Merge changes from topic "revert-1890098-KOOTTLPTTT" am: fec41dda am: b33b3ab7

Original change: https://android-review.googlesource.com/c/platform/system/core/+/1895142

Change-Id: Ib4f82dfa9ce8897dfc21e1ae4dbd9f67343050b0
parents 13c49a5e b33b3ab7
Loading
Loading
Loading
Loading
+71 −31
Original line number Original line Diff line number Diff line
@@ -530,37 +530,67 @@ bool EraseFstabEntry(Fstab* fstab, const std::string& mount_point) {


}  // namespace
}  // namespace


bool ParseFstabFromString(const std::string& fstab_str, bool proc_mounts, Fstab* fstab_out) {
bool ReadFstabFromFp(FILE* fstab_file, bool proc_mounts, Fstab* fstab_out) {
    const int expected_fields = proc_mounts ? 4 : 5;
    ssize_t len;

    size_t alloc_len = 0;
    char *line = NULL;
    const char *delim = " \t";
    char *save_ptr, *p;
    Fstab fstab;
    Fstab fstab;


    for (const auto& line : android::base::Split(fstab_str, "\n")) {
    while ((len = getline(&line, &alloc_len, fstab_file)) != -1) {
        auto fields = android::base::Tokenize(line, " \t");
        /* if the last character is a newline, shorten the string by 1 byte */
        if (line[len - 1] == '\n') {
            line[len - 1] = '\0';
        }


        // Ignore empty lines and comments.
        /* Skip any leading whitespace */
        if (fields.empty() || android::base::StartsWith(fields.front(), '#')) {
        p = line;
        while (isspace(*p)) {
            p++;
        }
        /* ignore comments or empty lines */
        if (*p == '#' || *p == '\0')
            continue;
            continue;

        FstabEntry entry;

        if (!(p = strtok_r(line, delim, &save_ptr))) {
            LERROR << "Error parsing mount source";
            goto err;
        }
        }
        entry.blk_device = p;


        if (fields.size() < expected_fields) {
        if (!(p = strtok_r(NULL, delim, &save_ptr))) {
            LERROR << "Error parsing fstab: expected " << expected_fields << " fields, got "
            LERROR << "Error parsing mount_point";
                   << fields.size();
            goto err;
            return false;
        }
        }
        entry.mount_point = p;


        FstabEntry entry;
        if (!(p = strtok_r(NULL, delim, &save_ptr))) {
        auto it = fields.begin();
            LERROR << "Error parsing fs_type";
            goto err;
        }
        entry.fs_type = p;


        entry.blk_device = std::move(*it++);
        if (!(p = strtok_r(NULL, delim, &save_ptr))) {
        entry.mount_point = std::move(*it++);
            LERROR << "Error parsing mount_flags";
        entry.fs_type = std::move(*it++);
            goto err;
        ParseMountFlags(std::move(*it++), &entry);
        }

        ParseMountFlags(p, &entry);


        // For /proc/mounts, ignore everything after mnt_freq and mnt_passno
        // For /proc/mounts, ignore everything after mnt_freq and mnt_passno
        if (!proc_mounts && !ParseFsMgrFlags(std::move(*it++), &entry)) {
        if (proc_mounts) {
            p += strlen(p);
        } else if (!(p = strtok_r(NULL, delim, &save_ptr))) {
            LERROR << "Error parsing fs_mgr_options";
            goto err;
        }

        if (!ParseFsMgrFlags(p, &entry)) {
            LERROR << "Error parsing fs_mgr_flags";
            LERROR << "Error parsing fs_mgr_flags";
            return false;
            goto err;
        }
        }


        if (entry.fs_mgr_flags.logical) {
        if (entry.fs_mgr_flags.logical) {
@@ -572,17 +602,21 @@ bool ParseFstabFromString(const std::string& fstab_str, bool proc_mounts, Fstab*


    if (fstab.empty()) {
    if (fstab.empty()) {
        LERROR << "No entries found in fstab";
        LERROR << "No entries found in fstab";
        return false;
        goto err;
    }
    }


    /* If an A/B partition, modify block device to be the real block device */
    /* If an A/B partition, modify block device to be the real block device */
    if (!fs_mgr_update_for_slotselect(&fstab)) {
    if (!fs_mgr_update_for_slotselect(&fstab)) {
        LERROR << "Error updating for slotselect";
        LERROR << "Error updating for slotselect";
        return false;
        goto err;
    }
    }

    free(line);
    *fstab_out = std::move(fstab);
    *fstab_out = std::move(fstab);
    return true;
    return true;

err:
    free(line);
    return false;
}
}


void TransformFstabForDsu(Fstab* fstab, const std::string& dsu_slot,
void TransformFstabForDsu(Fstab* fstab, const std::string& dsu_slot,
@@ -681,18 +715,16 @@ void EnableMandatoryFlags(Fstab* fstab) {
}
}


bool ReadFstabFromFile(const std::string& path, Fstab* fstab_out) {
bool ReadFstabFromFile(const std::string& path, Fstab* fstab_out) {
    const bool is_proc_mounts = (path == "/proc/mounts");
    auto fstab_file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
    // /proc/mounts could be a symlink to /proc/self/mounts.
    if (!fstab_file) {
    const bool follow_symlinks = is_proc_mounts;
        PERROR << __FUNCTION__ << "(): cannot open file: '" << path << "'";

    std::string fstab_str;
    if (!android::base::ReadFileToString(path, &fstab_str, follow_symlinks)) {
        PERROR << __FUNCTION__ << "(): failed to read file: '" << path << "'";
        return false;
        return false;
    }
    }


    bool is_proc_mounts = path == "/proc/mounts";

    Fstab fstab;
    Fstab fstab;
    if (!ParseFstabFromString(fstab_str, is_proc_mounts, &fstab)) {
    if (!ReadFstabFromFp(fstab_file.get(), is_proc_mounts, &fstab)) {
        LERROR << __FUNCTION__ << "(): failed to load fstab from : '" << path << "'";
        LERROR << __FUNCTION__ << "(): failed to load fstab from : '" << path << "'";
        return false;
        return false;
    }
    }
@@ -739,7 +771,15 @@ bool ReadFstabFromDt(Fstab* fstab, bool verbose) {
        return false;
        return false;
    }
    }


    if (!ParseFstabFromString(fstab_buf, /* proc_mounts = */ false, fstab)) {
    std::unique_ptr<FILE, decltype(&fclose)> fstab_file(
        fmemopen(static_cast<void*>(const_cast<char*>(fstab_buf.c_str())),
                 fstab_buf.length(), "r"), fclose);
    if (!fstab_file) {
        if (verbose) PERROR << __FUNCTION__ << "(): failed to create a file stream for fstab dt";
        return false;
    }

    if (!ReadFstabFromFp(fstab_file.get(), false, fstab)) {
        if (verbose) {
        if (verbose) {
            LERROR << __FUNCTION__ << "(): failed to load fstab from kernel:" << std::endl
            LERROR << __FUNCTION__ << "(): failed to load fstab from kernel:" << std::endl
                   << fstab_buf;
                   << fstab_buf;
+6 −2
Original line number Original line Diff line number Diff line
@@ -19,8 +19,12 @@
#include <fstab/fstab.h>
#include <fstab/fstab.h>


extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    std::string make_fstab_str(reinterpret_cast<const char*>(data), size);
    std::unique_ptr<FILE, decltype(&fclose)> fstab_file(
            fmemopen(static_cast<void*>(const_cast<uint8_t*>(data)), size, "r"), fclose);
    if (fstab_file == nullptr) {
        return 0;
    }
    android::fs_mgr::Fstab fstab;
    android::fs_mgr::Fstab fstab;
    android::fs_mgr::ParseFstabFromString(make_fstab_str, /* proc_mounts = */ false, &fstab);
    android::fs_mgr::ReadFstabFromFp(fstab_file.get(), /* proc_mounts= */ false, &fstab);
    return 0;
    return 0;
}
}
+2 −2
Original line number Original line Diff line number Diff line
@@ -94,8 +94,8 @@ struct FstabEntry {
// Unless explicitly requested, a lookup on mount point should always return the 1st one.
// Unless explicitly requested, a lookup on mount point should always return the 1st one.
using Fstab = std::vector<FstabEntry>;
using Fstab = std::vector<FstabEntry>;


// Exported for testability. Regular users should use ReadFstabFromFile().
// Exported for testability. Regular users should use ReadFstabFromfile().
bool ParseFstabFromString(const std::string& fstab_str, bool proc_mounts, Fstab* fstab_out);
bool ReadFstabFromFp(FILE* fstab_file, bool proc_mounts, Fstab* fstab_out);


bool ReadFstabFromFile(const std::string& path, Fstab* fstab);
bool ReadFstabFromFile(const std::string& path, Fstab* fstab);
bool ReadFstabFromDt(Fstab* fstab, bool verbose = true);
bool ReadFstabFromDt(Fstab* fstab, bool verbose = true);