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

Commit 44ae5460 authored by David Anderson's avatar David Anderson
Browse files

libdm: Fix WaitForFile early-returning on failed accesses.

WaitForFile/WaitForDeletedFile both early return true if an error like
EPERM occurs. This was intentional because the code was modeled off
earlier fs_mgr code, but it makes libdm inherently racy if sepolicy is
not configured correctly. It's better to have these result in explicit
and consistent failures.

Bug: 148103327
Test: fastboot flashall
Change-Id: I0c78818962e1db91b556e523c418db28f7d78fae
Merged-In: I0c78818962e1db91b556e523c418db28f7d78fae
parent 7e5f2aa4
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ bool DeviceMapper::DeleteDevice(const std::string& name,
        return false;
    }
    if (!WaitForFileDeleted(unique_path, timeout_ms)) {
        LOG(ERROR) << "Timeout out waiting for " << unique_path << " to be deleted";
        LOG(ERROR) << "Failed waiting for " << unique_path << " to be deleted";
        return false;
    }
    return true;
@@ -161,7 +161,7 @@ bool DeviceMapper::CreateDevice(const std::string& name, const DmTable& table, s
        return true;
    }
    if (!WaitForFile(unique_path, timeout_ms)) {
        LOG(ERROR) << "Timed out waiting for device path: " << unique_path;
        LOG(ERROR) << "Failed waiting for device path: " << unique_path;
        DeleteDevice(name);
        return false;
    }
+12 −2
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@

#include <thread>

#include <android-base/logging.h>

using namespace std::literals;

namespace android {
@@ -45,7 +47,11 @@ bool WaitForFile(const std::string& path, const std::chrono::milliseconds& timeo
        // If the file exists but returns EPERM or something, we consider the
        // condition met.
        if (access(path.c_str(), F_OK) != 0) {
            if (errno == ENOENT) return WaitResult::Wait;
            if (errno == ENOENT) {
                return WaitResult::Wait;
            }
            PLOG(ERROR) << "access failed: " << path;
            return WaitResult::Fail;
        }
        return WaitResult::Done;
    };
@@ -54,9 +60,13 @@ bool WaitForFile(const std::string& path, const std::chrono::milliseconds& timeo

bool WaitForFileDeleted(const std::string& path, const std::chrono::milliseconds& timeout_ms) {
    auto condition = [&]() -> WaitResult {
        if (access(path.c_str(), F_OK) == 0 || errno != ENOENT) {
        if (access(path.c_str(), F_OK) == 0) {
            return WaitResult::Wait;
        }
        if (errno != ENOENT) {
            PLOG(ERROR) << "access failed: " << path;
            return WaitResult::Fail;
        }
        return WaitResult::Done;
    };
    return WaitForCondition(condition, timeout_ms);