From a8c7c54eed57e5a4b56905a4fb00e27e25b1b908 Mon Sep 17 00:00:00 2001 From: Vishnu Nair Date: Tue, 20 Jul 2021 18:49:42 -0700 Subject: SurfaceFlinger: Safely cast from IBinder to Layer::Handle Bug: b/193034677, b/193034683, b/193033243 Test: go/wm-smoke, presubmit Change-Id: Iece64fca254edfd0b82e05ad9629824b2364cc13 Merged-In: Iece64fca254edfd0b82e05ad9629824b2364cc13 --- services/surfaceflinger/Layer.cpp | 42 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'services/surfaceflinger/Layer.cpp') diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp index e4a777f3c7..6ee13ce508 100644 --- a/services/surfaceflinger/Layer.cpp +++ b/services/surfaceflinger/Layer.cpp @@ -819,11 +819,7 @@ void Layer::setZOrderRelativeOf(const wp& relativeOf) { } bool Layer::setRelativeLayer(const sp& relativeToHandle, int32_t relativeZ) { - sp handle = static_cast(relativeToHandle.get()); - if (handle == nullptr) { - return false; - } - sp relative = handle->owner.promote(); + sp relative = fromHandle(relativeToHandle).promote(); if (relative == nullptr) { return false; } @@ -1609,8 +1605,7 @@ void Layer::setChildrenDrawingParent(const sp& newParent) { bool Layer::reparent(const sp& newParentHandle) { sp newParent; if (newParentHandle != nullptr) { - auto handle = static_cast(newParentHandle.get()); - newParent = handle->owner.promote(); + newParent = fromHandle(newParentHandle).promote(); if (newParent == nullptr) { ALOGE("Unable to promote Layer handle"); return false; @@ -1985,24 +1980,10 @@ void Layer::commitChildList() { mDrawingParent = mCurrentParent; } -static wp extractLayerFromBinder(const wp& weakBinderHandle) { - if (weakBinderHandle == nullptr) { - return nullptr; - } - sp binderHandle = weakBinderHandle.promote(); - if (binderHandle == nullptr) { - return nullptr; - } - sp handle = static_cast(binderHandle.get()); - if (handle == nullptr) { - return nullptr; - } - return handle->owner; -} void Layer::setInputInfo(const InputWindowInfo& info) { mDrawingState.inputInfo = info; - mDrawingState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle); + mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote()); mDrawingState.modified = true; mFlinger->mInputInfoChanged = true; setTransactionFlags(eTransactionNeeded); @@ -2561,6 +2542,23 @@ void Layer::setClonedChild(const sp& clonedChild) { mFlinger->mNumClones++; } +const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle"); + +wp Layer::fromHandle(const sp& handleBinder) { + if (handleBinder == nullptr) { + return nullptr; + } + + BBinder* b = handleBinder->localBinder(); + if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) { + return nullptr; + } + + // We can safely cast this binder since its local and we verified its interface descriptor. + sp handle = static_cast(handleBinder.get()); + return handle->owner; +} + // --------------------------------------------------------------------------- std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) { -- cgit v1.2.3-59-g8ed1b