From 1f9dcd331fcf2eaf0c2828e169babdbfe39b9b15 Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Wed, 3 Nov 2021 13:34:29 +0900 Subject: binder_ndk: sanitize binder descriptor for logging Invalid char in a string causes "implicit-conversion" ubsan error. So, when printing "read" binder descriptor for log, sanitize it by replacing invalid char with '?'. Bug: n/a Test: binder_parcel_fuzzer Change-Id: I51f806ff28ccc52f9b3da328ace4ca8c348483ba --- libs/binder/ndk/ibinder.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libs/binder/ndk/ibinder.cpp b/libs/binder/ndk/ibinder.cpp index 8ffa735700..6949c2c9e5 100644 --- a/libs/binder/ndk/ibinder.cpp +++ b/libs/binder/ndk/ibinder.cpp @@ -104,6 +104,17 @@ std::optional AIBinder::associateClassInternal(const AIBinder_Class* clazz return {}; } +// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char +static std::string SanitizeString(const String16& str) { + std::string sanitized{String8(str)}; + for (auto& c : sanitized) { + if (!isprint(c)) { + c = '?'; + } + } + return sanitized; +} + bool AIBinder::associateClass(const AIBinder_Class* clazz) { if (clazz == nullptr) return false; @@ -118,7 +129,7 @@ bool AIBinder::associateClass(const AIBinder_Class* clazz) { if (descriptor != newDescriptor) { if (getBinder()->isBinderAlive()) { LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor - << "' but descriptor is actually '" << descriptor << "'."; + << "' but descriptor is actually '" << SanitizeString(descriptor) << "'."; } else { // b/155793159 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor -- cgit v1.2.3-59-g8ed1b