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

Commit 804d8a6b authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Check noteOp when receiving messages from endpoints

Bug: 381102453
Flag: android.chre.flags.offload_implementation
Test: Confirm that messages are dropped when note operation fails
Change-Id: Iee6060cd3bdad70ad715e35959240e81da48559e
parent c1efefa3
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -358,6 +358,7 @@ public class HubEndpoint {
                    service.registerEndpoint(
                            mPendingHubEndpointInfo,
                            mServiceCallback,
                            mPendingHubEndpointInfo.getName(),
                            mPendingHubEndpointInfo.getTag());
            mAssignedHubEndpointInfo = serviceToken.getAssignedHubEndpointInfo();
            mServiceToken = serviceToken;
@@ -514,6 +515,7 @@ public class HubEndpoint {
        /** Create a builder for {@link HubEndpoint} */
        public Builder(@NonNull Context context) {
            mPackageName = context.getPackageName();
            mTag = context.getAttributionTag();
            mVersion = (int) context.getApplicationInfo().longVersionCode;
            mMainExecutor = context.getMainExecutor();
        }
@@ -532,6 +534,7 @@ public class HubEndpoint {
        /**
         * Set a tag string. The tag can be used to further identify the creator of the endpoint.
         * Endpoints created by the same package share the same name but should have different tags.
         * The default value of the tag is retrieved from {@link Context#getAttributionTag()}.
         */
        @NonNull
        public Builder setTag(@NonNull String tag) {
+1 −1
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ interface IContextHubService {

    // Register an endpoint with the context hub
    @EnforcePermission("ACCESS_CONTEXT_HUB")
    IContextHubEndpoint registerEndpoint(in HubEndpointInfo pendingEndpointInfo, in IContextHubEndpointCallback callback, String packageName);
    IContextHubEndpoint registerEndpoint(in HubEndpointInfo pendingEndpointInfo, in IContextHubEndpointCallback callback, String packageName, String attributionTag);

    // Register an endpoint discovery callback (id)
    @EnforcePermission("ACCESS_CONTEXT_HUB")
+40 −6
Original line number Diff line number Diff line
@@ -51,6 +51,9 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        implements IBinder.DeathRecipient, AppOpsManager.OnOpChangedListener {
    private static final String TAG = "ContextHubEndpointBroker";

    /** Message used by noteOp when this client receives a message from an endpoint. */
    private static final String RECEIVE_MSG_NOTE = "ContextHubEndpointMessageDelivery";

    /** The context of the service. */
    private final Context mContext;

@@ -120,6 +123,9 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
    /** The package name of the app that created the endpoint */
    private final String mPackageName;

    /** The attribution tag of the module that created the endpoint */
    private final String mAttributionTag;

    /** Transaction manager used for sending reliable messages */
    private final ContextHubTransactionManager mTransactionManager;

@@ -135,6 +141,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
            EndpointInfo halEndpointInfo,
            IContextHubEndpointCallback callback,
            String packageName,
            String attributionTag,
            ContextHubTransactionManager transactionManager) {
        mContext = context;
        mHubInterface = hubInterface;
@@ -143,6 +150,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        mHalEndpointInfo = halEndpointInfo;
        mContextHubEndpointCallback = callback;
        mPackageName = packageName;
        mAttributionTag = attributionTag;
        mTransactionManager = transactionManager;

        mPid = Binder.getCallingPid();
@@ -394,6 +402,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
    }

    /* package */ void onMessageReceived(int sessionId, HubMessage message) {
        HubEndpointInfo remote;
        synchronized (mOpenSessionLock) {
            if (!isSessionActive(sessionId)) {
                Log.e(
                        TAG,
@@ -405,6 +415,30 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
                        sessionId, message.getMessageSequenceNumber(), ErrorCode.PERMANENT_ERROR);
                return;
            }
            remote = mSessionInfoMap.get(sessionId).getRemoteEndpointInfo();
        }
        if (!ContextHubServiceUtil.notePermissions(
                mAppOpsManager,
                mUid,
                mPackageName,
                mAttributionTag,
                remote.getRequiredPermissions(),
                RECEIVE_MSG_NOTE
                        + "-0x"
                        + Long.toHexString(remote.getIdentifier().getHub())
                        + "-0x"
                        + Long.toHexString(remote.getIdentifier().getEndpoint()))) {
            Log.e(
                    TAG,
                    "Dropping message from "
                            + remote
                            + ". "
                            + mPackageName
                            + " doesn't have permission");
            sendMessageDeliveryStatus(
                    sessionId, message.getMessageSequenceNumber(), ErrorCode.PERMISSION_DENIED);
            return;
        }

        if (mContextHubEndpointCallback != null) {
            try {
+3 −1
Original line number Diff line number Diff line
@@ -191,7 +191,8 @@ import java.util.concurrent.ConcurrentHashMap;
    /* package */ IContextHubEndpoint registerEndpoint(
            HubEndpointInfo pendingEndpointInfo,
            IContextHubEndpointCallback callback,
            String packageName)
            String packageName,
            String attributionTag)
            throws RemoteException {
        if (!mSessionIdsValid) {
            throw new IllegalStateException("ContextHubEndpointManager failed to initialize");
@@ -215,6 +216,7 @@ import java.util.concurrent.ConcurrentHashMap;
                        halEndpointInfo,
                        callback,
                        packageName,
                        attributionTag,
                        mTransactionManager);
        mEndpointMap.put(endpointId, broker);

+4 −2
Original line number Diff line number Diff line
@@ -787,14 +787,16 @@ public class ContextHubService extends IContextHubService.Stub {
    public IContextHubEndpoint registerEndpoint(
            HubEndpointInfo pendingHubEndpointInfo,
            IContextHubEndpointCallback callback,
            String packageName)
            String packageName,
            String attributionTag)
            throws RemoteException {
        super.registerEndpoint_enforcePermission();
        if (mEndpointManager == null) {
            Log.e(TAG, "Endpoint manager failed to initialize");
            throw new UnsupportedOperationException("Endpoint registration is not supported");
        }
        return mEndpointManager.registerEndpoint(pendingHubEndpointInfo, callback, packageName);
        return mEndpointManager.registerEndpoint(
                pendingHubEndpointInfo, callback, packageName, attributionTag);
    }

    @android.annotation.EnforcePermission(android.Manifest.permission.ACCESS_CONTEXT_HUB)
Loading