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

Commit ad9fe277 authored by Jayant Chowdhary's avatar Jayant Chowdhary
Browse files

BufferQueueProducer: use the correct IPCThreadState.



The system variant of libgui maybe be double loaded. Also
IGraphicBufferProducer functions may be called from hwbinder threads due
to the presence of TWGraphicBufferProducer wrappers (hybrid interfaces).
Therefore, we should use the correct IPCThreadState/ hardware::IPCThreadState to query
callingPids. This also avoids access to /dev/binder in vendor processes,
in case the system variant of the library is loaded, for eg: in
libmediandk.

Bug: 124128212

Test: Selinux denials realted to/dev/binder acccess are not present when AImageReader from
      libmediandk is used in a vendor process.

Test: Play Youtube movies on Chrome, use camera to take pictures/ record
      videos (sanity).

Change-Id: I27d78e30e16b7df5e3dfbb130121f3d7078671a3
Signed-off-by: default avatarJayant Chowdhary <jchowdhary@google.com>
parent 5ff2b63c
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ cc_library_shared {
        "BufferQueueConsumer.cpp",
        "BufferQueueCore.cpp",
        "BufferQueueProducer.cpp",
        "BufferQueueThreadState.cpp",
        "BufferSlot.cpp",
        "ConsumerBase.cpp",
        "CpuConsumer.cpp",
@@ -128,6 +129,7 @@ cc_library_shared {
        "libbase",
        "libsync",
        "libbinder",
        "libhwbinder",
        "libbufferhub",
        "libbufferhubqueue", // TODO(b/70046255): Remove this once BufferHub is integrated into libgui.
        "libpdx_default_transport",
@@ -143,12 +145,16 @@ cc_library_shared {
        "libhidltransport",
        "android.hidl.token@1.0-utils",
        "android.hardware.graphics.bufferqueue@1.0",
        "libvndksupport",
    ],

    // bufferhub is not used when building libgui for vendors
    target: {
        vendor: {
            cflags: ["-DNO_BUFFERHUB", "-DNO_INPUT"],
            cflags: [
                "-DNO_BUFFERHUB",
                "-DNO_INPUT",
            ],
            exclude_srcs: [
                "BufferHubConsumer.cpp",
                "BufferHubProducer.cpp",
@@ -158,7 +164,7 @@ cc_library_shared {
                "libbufferhub",
                "libbufferhubqueue",
                "libpdx_default_transport",
                "libinput"
                "libinput",
            ],
        },
    },
+20 −9
Original line number Diff line number Diff line
@@ -34,9 +34,10 @@
#include <gui/IConsumerListener.h>
#include <gui/IProducerListener.h>

#include <binder/IPCThreadState.h>
#include <private/gui/BufferQueueThreadState.h>
#ifndef __ANDROID_VNDK__
#include <binder/PermissionCache.h>
#include <vndksupport/linker.h>
#endif

#include <system/window.h>
@@ -758,19 +759,29 @@ status_t BufferQueueConsumer::dumpState(const String8& prefix, String8* outResul
        return savedErrno ? -savedErrno : UNKNOWN_ERROR;
    }

    const IPCThreadState* ipc = IPCThreadState::self();
    const uid_t uid = ipc->getCallingUid();
    bool denied = false;
    const uid_t uid = BufferQueueThreadState::getCallingUid();
#ifndef __ANDROID_VNDK__
    // permission check can't be done for vendors as vendors have no access to
    // the PermissionController
    const pid_t pid = ipc->getCallingPid();
    // the PermissionController. We need to do a runtime check as well, since
    // the system variant of libgui can be loaded in a vendor process. For eg:
    // if a HAL uses an llndk library that depends on libgui (libmediandk etc).
    if (!android_is_in_vendor_process()) {
        const pid_t pid = BufferQueueThreadState::getCallingPid();
        if ((uid != shellUid) &&
            !PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
            outResult->appendFormat("Permission Denial: can't dump BufferQueueConsumer "
                "from pid=%d, uid=%d\n", pid, uid);
                                    "from pid=%d, uid=%d\n",
                                    pid, uid);
            denied = true;
        }
    }
#else
    if (uid != shellUid) {
        denied = true;
    }
#endif
    if (denied) {
        android_errorWriteWithInfoLog(0x534e4554, "27046057",
                static_cast<int32_t>(uid), nullptr, 0);
        return PERMISSION_DENIED;
+3 −2
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include <gui/GLConsumer.h>
#include <gui/IConsumerListener.h>
#include <gui/IProducerListener.h>
#include <private/gui/BufferQueueThreadState.h>

#include <utils/Log.h>
#include <utils/Trace.h>
@@ -1210,7 +1211,7 @@ status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
            status = BAD_VALUE;
            break;
    }
    mCore->mConnectedPid = IPCThreadState::self()->getCallingPid();
    mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
    mCore->mBufferHasBeenQueued = false;
    mCore->mDequeueBufferCannotBlock = false;
    if (mDequeueTimeout < 0) {
@@ -1233,7 +1234,7 @@ status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
        Mutex::Autolock lock(mCore->mMutex);

        if (mode == DisconnectMode::AllLocal) {
            if (IPCThreadState::self()->getCallingPid() != mCore->mConnectedPid) {
            if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
                return NO_ERROR;
            }
            api = BufferQueueCore::CURRENTLY_CONNECTED_API;
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 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.
 */

#include <binder/IPCThreadState.h>
#include <hwbinder/IPCThreadState.h>
#include <private/gui/BufferQueueThreadState.h>
#include <unistd.h>

namespace android {

uid_t BufferQueueThreadState::getCallingUid() {
    if (hardware::IPCThreadState::self()->isServingCall()) {
        return hardware::IPCThreadState::self()->getCallingUid();
    }
    return IPCThreadState::self()->getCallingUid();
}

pid_t BufferQueueThreadState::getCallingPid() {
    if (hardware::IPCThreadState::self()->isServingCall()) {
        return hardware::IPCThreadState::self()->getCallingPid();
    }
    return IPCThreadState::self()->getCallingPid();
}

} // namespace android
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 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.
 */

#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>

namespace android {

// TODO: Replace this with b/127962003
class BufferQueueThreadState {
public:
    static pid_t getCallingPid();
    static uid_t getCallingUid();
};

} // namespace android