Merge changes Ie9da525c,I9911f02c,Iad18af1f,I5f432a3d
* changes:
trusty: provide coverage to gatekeeper fuzzer
trusty: fuzz: Example TA fuzzer
trusty: fuzz: Helper lib for libFuzzer extra counters
trusty: coverage: Coverage client library
diff --git a/debuggerd/libdebuggerd/test/UnwinderMock.h b/debuggerd/libdebuggerd/test/UnwinderMock.h
index 023a578..44a9214 100644
--- a/debuggerd/libdebuggerd/test/UnwinderMock.h
+++ b/debuggerd/libdebuggerd/test/UnwinderMock.h
@@ -34,7 +34,7 @@
unwindstack::MapInfo* map_info = GetMaps()->Find(offset);
if (map_info != nullptr) {
std::string* new_build_id = new std::string(build_id);
- map_info->build_id = reinterpret_cast<uintptr_t>(new_build_id);
+ map_info->build_id = new_build_id;
}
}
};
diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp
index 6294b3f..4c9fd9b 100644
--- a/fs_mgr/fs_mgr.cpp
+++ b/fs_mgr/fs_mgr.cpp
@@ -656,7 +656,17 @@
// If needed, we'll also enable (or disable) filesystem features as specified by
// the fstab record.
//
-static int prepare_fs_for_mount(const std::string& blk_device, const FstabEntry& entry) {
+static int prepare_fs_for_mount(const std::string& blk_device, const FstabEntry& entry,
+ const std::string& alt_mount_point = "") {
+ auto& mount_point = alt_mount_point.empty() ? entry.mount_point : alt_mount_point;
+ // We need this because sometimes we have legacy symlinks that are
+ // lingering around and need cleaning up.
+ struct stat info;
+ if (lstat(mount_point.c_str(), &info) == 0 && (info.st_mode & S_IFMT) == S_IFLNK) {
+ unlink(mount_point.c_str());
+ }
+ mkdir(mount_point.c_str(), 0755);
+
int fs_stat = 0;
if (is_extfs(entry.fs_type)) {
@@ -684,7 +694,7 @@
if (entry.fs_mgr_flags.check ||
(fs_stat & (FS_STAT_UNCLEAN_SHUTDOWN | FS_STAT_QUOTA_ENABLED))) {
- check_fs(blk_device, entry.fs_type, entry.mount_point, &fs_stat);
+ check_fs(blk_device, entry.fs_type, mount_point, &fs_stat);
}
if (is_extfs(entry.fs_type) &&
@@ -729,13 +739,6 @@
// sets the underlying block device to read-only if the mount is read-only.
// See "man 2 mount" for return values.
static int __mount(const std::string& source, const std::string& target, const FstabEntry& entry) {
- // We need this because sometimes we have legacy symlinks that are
- // lingering around and need cleaning up.
- struct stat info;
- if (lstat(target.c_str(), &info) == 0 && (info.st_mode & S_IFMT) == S_IFLNK) {
- unlink(target.c_str());
- }
- mkdir(target.c_str(), 0755);
errno = 0;
unsigned long mountflags = entry.flags;
int ret = 0;
@@ -1799,17 +1802,18 @@
// wrapper to __mount() and expects a fully prepared fstab_rec,
// unlike fs_mgr_do_mount which does more things with avb / verity etc.
-int fs_mgr_do_mount_one(const FstabEntry& entry, const std::string& mount_point) {
+int fs_mgr_do_mount_one(const FstabEntry& entry, const std::string& alt_mount_point) {
// First check the filesystem if requested.
if (entry.fs_mgr_flags.wait && !WaitForFile(entry.blk_device, 20s)) {
LERROR << "Skipping mounting '" << entry.blk_device << "'";
}
- // Run fsck if needed
- prepare_fs_for_mount(entry.blk_device, entry);
+ auto& mount_point = alt_mount_point.empty() ? entry.mount_point : alt_mount_point;
- int ret =
- __mount(entry.blk_device, mount_point.empty() ? entry.mount_point : mount_point, entry);
+ // Run fsck if needed
+ prepare_fs_for_mount(entry.blk_device, entry, mount_point);
+
+ int ret = __mount(entry.blk_device, mount_point, entry);
if (ret) {
ret = (errno == EBUSY) ? FS_MGR_DOMNT_BUSY : FS_MGR_DOMNT_FAILED;
}
@@ -1868,7 +1872,14 @@
continue;
}
- int fs_stat = prepare_fs_for_mount(n_blk_device, fstab_entry);
+ // Now mount it where requested */
+ if (tmp_mount_point) {
+ mount_point = tmp_mount_point;
+ } else {
+ mount_point = fstab_entry.mount_point;
+ }
+
+ int fs_stat = prepare_fs_for_mount(n_blk_device, fstab_entry, mount_point);
if (fstab_entry.fs_mgr_flags.avb) {
if (!avb_handle) {
@@ -1902,12 +1913,6 @@
}
}
- // Now mount it where requested */
- if (tmp_mount_point) {
- mount_point = tmp_mount_point;
- } else {
- mount_point = fstab_entry.mount_point;
- }
int retry_count = 2;
while (retry_count-- > 0) {
if (!__mount(n_blk_device, mount_point, fstab_entry)) {
@@ -1919,7 +1924,7 @@
mount_errors++;
fs_stat |= FS_STAT_FULL_MOUNT_FAILED;
// try again after fsck
- check_fs(n_blk_device, fstab_entry.fs_type, fstab_entry.mount_point, &fs_stat);
+ check_fs(n_blk_device, fstab_entry.fs_type, mount_point, &fs_stat);
}
}
log_fs_stat(fstab_entry.blk_device, fs_stat);
diff --git a/init/Android.mk b/init/Android.mk
index 4c1665b..ac31ef1 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -82,7 +82,6 @@
$(TARGET_RAMDISK_OUT)/proc \
$(TARGET_RAMDISK_OUT)/second_stage_resources \
$(TARGET_RAMDISK_OUT)/sys \
- $(TARGET_RAMDISK_OUT)/metadata \
LOCAL_STATIC_LIBRARIES := \
libc++fs \
diff --git a/init/service_utils.h b/init/service_utils.h
index e74f8c1..1e0b4bd 100644
--- a/init/service_utils.h
+++ b/init/service_utils.h
@@ -37,6 +37,8 @@
Descriptor(const std::string& name, android::base::unique_fd fd)
: name_(name), fd_(std::move(fd)){};
+ // Publish() unsets FD_CLOEXEC from the FD and publishes its name via setenv(). It should be
+ // called when starting a service after fork() and before exec().
void Publish() const;
private:
@@ -53,6 +55,9 @@
std::string context;
bool passcred = false;
+ // Create() creates the named unix domain socket in /dev/socket and returns a Descriptor object.
+ // It should be called when starting a service, before calling fork(), such that the socket is
+ // synchronously created before starting any other services, which may depend on it.
Result<Descriptor> Create(const std::string& global_context) const;
};
diff --git a/libutils/RefBase_fuzz.cpp b/libutils/RefBase_fuzz.cpp
old mode 100755
new mode 100644
index 2a92531..69288b3
--- a/libutils/RefBase_fuzz.cpp
+++ b/libutils/RefBase_fuzz.cpp
@@ -14,66 +14,156 @@
* limitations under the License.
*/
-#include <atomic>
+#define LOG_TAG "RefBaseFuzz"
+
#include <thread>
#include "fuzzer/FuzzedDataProvider.h"
+#include "utils/Log.h"
+#include "utils/RWLock.h"
#include "utils/RefBase.h"
#include "utils/StrongPointer.h"
+
using android::RefBase;
+using android::RWLock;
using android::sp;
using android::wp;
-static constexpr int REFBASE_INITIAL_STRONG_VALUE = (1 << 28);
-static constexpr int REFBASE_MAX_COUNT = 0xfffff;
-
-static constexpr int MAX_OPERATIONS = 100;
-static constexpr int MAX_THREADS = 10;
-
-bool canDecrementStrong(RefBase* ref) {
- // There's an assert around decrementing the strong count too much that causes an artificial
- // crash This is just running BAD_STRONG from RefBase
- const int32_t count = ref->getStrongCount() - 1;
- return !(count == 0 || ((count) & (~(REFBASE_MAX_COUNT | REFBASE_INITIAL_STRONG_VALUE))) != 0);
-}
-bool canDecrementWeak(RefBase* ref) {
- const int32_t count = ref->getWeakRefs()->getWeakCount() - 1;
- return !((count) == 0 || ((count) & (~REFBASE_MAX_COUNT)) != 0);
-}
-
+static constexpr int kMaxOperations = 100;
+static constexpr int kMaxThreads = 10;
struct RefBaseSubclass : public RefBase {
- RefBaseSubclass() {}
- virtual ~RefBaseSubclass() {}
+ public:
+ RefBaseSubclass(bool* deletedCheck, RWLock& deletedMtx)
+ : mDeleted(deletedCheck), mRwLock(deletedMtx) {
+ RWLock::AutoWLock lock(mRwLock);
+ *mDeleted = false;
+ extendObjectLifetime(OBJECT_LIFETIME_WEAK);
+ }
+
+ virtual ~RefBaseSubclass() {
+ RWLock::AutoWLock lock(mRwLock);
+ *mDeleted = true;
+ }
+
+ private:
+ bool* mDeleted;
+ android::RWLock& mRwLock;
};
-std::vector<std::function<void(RefBaseSubclass*)>> operations = {
- [](RefBaseSubclass* ref) -> void { ref->getStrongCount(); },
- [](RefBaseSubclass* ref) -> void { ref->printRefs(); },
- [](RefBaseSubclass* ref) -> void { ref->getWeakRefs()->printRefs(); },
- [](RefBaseSubclass* ref) -> void { ref->getWeakRefs()->getWeakCount(); },
- [](RefBaseSubclass* ref) -> void { ref->getWeakRefs()->refBase(); },
- [](RefBaseSubclass* ref) -> void { ref->incStrong(nullptr); },
- [](RefBaseSubclass* ref) -> void {
- if (canDecrementStrong(ref)) {
+// A thread-specific state object for ref
+struct RefThreadState {
+ size_t strongCount = 0;
+ size_t weakCount = 0;
+};
+
+RWLock gRefDeletedLock;
+bool gRefDeleted = false;
+bool gHasModifiedRefs = false;
+RefBaseSubclass* ref;
+RefBase::weakref_type* weakRefs;
+
+// These operations don't need locks as they explicitly check per-thread counts before running
+// they also have the potential to write to gRefDeleted, so must not be locked.
+const std::vector<std::function<void(RefThreadState*)>> kUnlockedOperations = {
+ [](RefThreadState* refState) -> void {
+ if (refState->strongCount > 0) {
ref->decStrong(nullptr);
+ gHasModifiedRefs = true;
+ refState->strongCount--;
}
},
- [](RefBaseSubclass* ref) -> void { ref->forceIncStrong(nullptr); },
- [](RefBaseSubclass* ref) -> void { ref->createWeak(nullptr); },
- [](RefBaseSubclass* ref) -> void { ref->getWeakRefs()->attemptIncStrong(nullptr); },
- [](RefBaseSubclass* ref) -> void { ref->getWeakRefs()->attemptIncWeak(nullptr); },
- [](RefBaseSubclass* ref) -> void {
- if (canDecrementWeak(ref)) {
- ref->getWeakRefs()->decWeak(nullptr);
+ [](RefThreadState* refState) -> void {
+ if (refState->weakCount > 0) {
+ weakRefs->decWeak(nullptr);
+ gHasModifiedRefs = true;
+ refState->weakCount--;
}
},
- [](RefBaseSubclass* ref) -> void { ref->getWeakRefs()->incWeak(nullptr); },
- [](RefBaseSubclass* ref) -> void { ref->getWeakRefs()->printRefs(); },
};
-void loop(RefBaseSubclass* loopRef, const std::vector<uint8_t>& fuzzOps) {
+const std::vector<std::function<void(RefThreadState*)>> kMaybeLockedOperations = {
+ // Read-only operations
+ [](RefThreadState*) -> void { ref->getStrongCount(); },
+ [](RefThreadState*) -> void { weakRefs->getWeakCount(); },
+ [](RefThreadState*) -> void { ref->printRefs(); },
+
+ // Read/write operations
+ [](RefThreadState* refState) -> void {
+ ref->incStrong(nullptr);
+ gHasModifiedRefs = true;
+ refState->strongCount++;
+ },
+ [](RefThreadState* refState) -> void {
+ ref->forceIncStrong(nullptr);
+ gHasModifiedRefs = true;
+ refState->strongCount++;
+ },
+ [](RefThreadState* refState) -> void {
+ ref->createWeak(nullptr);
+ gHasModifiedRefs = true;
+ refState->weakCount++;
+ },
+ [](RefThreadState* refState) -> void {
+ // This will increment weak internally, then attempt to
+ // promote it to strong. If it fails, it decrements weak.
+ // If it succeeds, the weak is converted to strong.
+ // Both cases net no weak reference change.
+ if (weakRefs->attemptIncStrong(nullptr)) {
+ refState->strongCount++;
+ gHasModifiedRefs = true;
+ }
+ },
+ [](RefThreadState* refState) -> void {
+ if (weakRefs->attemptIncWeak(nullptr)) {
+ refState->weakCount++;
+ gHasModifiedRefs = true;
+ }
+ },
+ [](RefThreadState* refState) -> void {
+ weakRefs->incWeak(nullptr);
+ gHasModifiedRefs = true;
+ refState->weakCount++;
+ },
+};
+
+void loop(const std::vector<uint8_t>& fuzzOps) {
+ RefThreadState state;
+ uint8_t lockedOpSize = kMaybeLockedOperations.size();
+ uint8_t totalOperationTypes = lockedOpSize + kUnlockedOperations.size();
for (auto op : fuzzOps) {
- operations[op % operations.size()](loopRef);
+ auto opVal = op % totalOperationTypes;
+ if (opVal >= lockedOpSize) {
+ kUnlockedOperations[opVal % lockedOpSize](&state);
+ } else {
+ // We only need to lock if we have no strong or weak count
+ bool shouldLock = state.strongCount == 0 && state.weakCount == 0;
+ if (shouldLock) {
+ gRefDeletedLock.readLock();
+ // If ref has deleted itself, we can no longer fuzz on this thread.
+ if (gRefDeleted) {
+ // Unlock since we're exiting the loop here.
+ gRefDeletedLock.unlock();
+ return;
+ }
+ }
+ // Execute the locked operation
+ kMaybeLockedOperations[opVal](&state);
+ // Unlock if we locked.
+ if (shouldLock) {
+ gRefDeletedLock.unlock();
+ }
+ }
+ }
+
+ // Instead of explicitly freeing this, we're going to remove our weak and
+ // strong references.
+ for (; state.weakCount > 0; state.weakCount--) {
+ weakRefs->decWeak(nullptr);
+ }
+
+ // Clean up any strong references
+ for (; state.strongCount > 0; state.strongCount--) {
+ ref->decStrong(nullptr);
}
}
@@ -81,23 +171,35 @@
std::vector<std::thread> threads = std::vector<std::thread>();
// Get the number of threads to generate
- uint8_t count = dataProvider->ConsumeIntegralInRange<uint8_t>(1, MAX_THREADS);
-
+ uint8_t count = dataProvider->ConsumeIntegralInRange<uint8_t>(1, kMaxThreads);
// Generate threads
for (uint8_t i = 0; i < count; i++) {
- RefBaseSubclass* threadRef = new RefBaseSubclass();
- uint8_t opCount = dataProvider->ConsumeIntegralInRange<uint8_t>(1, MAX_OPERATIONS);
+ uint8_t opCount = dataProvider->ConsumeIntegralInRange<uint8_t>(1, kMaxOperations);
std::vector<uint8_t> threadOperations = dataProvider->ConsumeBytes<uint8_t>(opCount);
- std::thread tmp = std::thread(loop, threadRef, threadOperations);
- threads.push_back(move(tmp));
+ std::thread tmpThread = std::thread(loop, threadOperations);
+ threads.push_back(move(tmpThread));
}
for (auto& th : threads) {
th.join();
}
}
+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+ gHasModifiedRefs = false;
+ ref = new RefBaseSubclass(&gRefDeleted, gRefDeletedLock);
+ weakRefs = ref->getWeakRefs();
+ // Since we are modifying flags, (flags & OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK
+ // is true. The destructor for RefBase should clean up weakrefs because of this.
FuzzedDataProvider dataProvider(data, size);
spawnThreads(&dataProvider);
+ LOG_ALWAYS_FATAL_IF(!gHasModifiedRefs && gRefDeleted, "ref(%p) was prematurely deleted!", ref);
+ // We need to explicitly delete this object
+ // if no refs have been added or deleted.
+ if (!gHasModifiedRefs && !gRefDeleted) {
+ delete ref;
+ }
+ LOG_ALWAYS_FATAL_IF(gHasModifiedRefs && !gRefDeleted,
+ "ref(%p) should be deleted, is it leaking?", ref);
return 0;
}
diff --git a/trusty/confirmationui/fuzz/Android.bp b/trusty/confirmationui/fuzz/Android.bp
new file mode 100644
index 0000000..0819c21
--- /dev/null
+++ b/trusty/confirmationui/fuzz/Android.bp
@@ -0,0 +1,19 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_fuzz {
+ name: "trusty_confirmationui_fuzzer",
+ defaults: ["trusty_fuzzer_defaults"],
+ srcs: ["fuzz.cpp"],
+}
diff --git a/trusty/confirmationui/fuzz/fuzz.cpp b/trusty/confirmationui/fuzz/fuzz.cpp
new file mode 100644
index 0000000..d285116
--- /dev/null
+++ b/trusty/confirmationui/fuzz/fuzz.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#undef NDEBUG
+
+#include <assert.h>
+#include <log/log.h>
+#include <stdlib.h>
+#include <trusty/fuzz/utils.h>
+#include <unistd.h>
+
+using android::trusty::fuzz::TrustyApp;
+
+#define TIPC_DEV "/dev/trusty-ipc-dev0"
+#define CONFIRMATIONUI_PORT "com.android.trusty.confirmationui"
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+ static uint8_t buf[TIPC_MAX_MSG_SIZE];
+
+ TrustyApp ta(TIPC_DEV, CONFIRMATIONUI_PORT);
+ auto ret = ta.Connect();
+ if (!ret.ok()) {
+ android::trusty::fuzz::Abort();
+ }
+
+ /* Send message to confirmationui server */
+ ret = ta.Write(data, size);
+ if (!ret.ok()) {
+ return -1;
+ }
+
+ /* Read message from confirmationui server */
+ ret = ta.Read(&buf, sizeof(buf));
+ if (!ret.ok()) {
+ return -1;
+ }
+
+ return 0;
+}