Eclipse Ditto policy enforcers ignore revokes in multi-permission and partial permission checks
Description
Eclipse Ditto's policy enforcers do not let a revoke win over a grant in two kinds of permission checks. As a result, they report permissions as granted although the policy revokes them:
- Checks of several permissions at once (default, throughput-optimized "trie-based" enforcer): a more specific grant of one permission hides a revoke of another permission. This is reachable through the
checkPermissionsAPI, which can answertruefor e.g.["READ", "WRITE"]althoughWRITEis revoked. - Partial permission checks ("is the permission granted on the resource or on any of its sub-resources?"), used to authorize retrieving Things and Policies and to determine the subjects which receive events: a grant on a sub-resource is counted although the same permission is revoked on that very sub-resource
- by another subject of the same authorization context (both enforcers), or
- for the same subject (memory-optimized "tree-based" enforcer).
Both issues require a policy which grants and revokes the same permission for the subject(s) of a request, and most of them an authorization context with several subjects (e.g. a user subject and a group/role subject extracted from the same JWT, or a connection with several authorization subjects).
The content of retrieved Things and Policies is not affected: it is filtered field by field, which evaluates grants and revokes correctly. What is affected is the authorization decision itself, so a requester can learn about the existence of a Thing or Policy and receive metadata (e.g. an empty {} with status 200 instead of 404, or events with an empty payload) where the policy grants no readable content at all. An application that relies on the checkPermissions result for its own authorization decision can be misled into allowing an action the policy denies.
Severity: Low.
Details
1. Multi-permission checks (trie-based enforcer)
GrantRevokeIndex#hasPermissions compares the highest grant weight across all requested permissions with the highest revoke weight across all requested permissions (PermissionSubjectsMap#getMaxNonemptyWeightForAllPermissions, #getMaxWeightForAllPermissions) instead of comparing grant and revoke of each permission on its own, as its Javadoc and the tree-based enforcer (CheckPermissionsVisitor) do. The defect shows when the grant and the revoke of a permission belong to different subjects of the same authorization context. The only Ditto API passing several permissions is checkPermissions (PolicyCheckPermissionsStrategy), available since 3.7.0. It always uses the caller's own authorization context.
Relatedly, PermissionSubjectsMap#getSubjectIntersect ignores a requested permission which no subject holds at all, so the trie-based getSubjectsWith*Permission methods over-report subjects for several permissions. Ditto itself only calls these with a single permission.
2. Partial permission checks
- Tree-based enforcer: when evaluating sub-resources of the checked resource,
CheckPartialPermissionsVisitor,CollectPartialGrantedSubjectsVisitorandClassifySubjectsVisitoronly collect the grants and ignore all revokes, including a revoke on the same sub-resource. - Trie-based enforcer: the "bottom-up grant trie" used by
TrieBasedPolicyEnforcer#hasPartialPermissionspushes the grants of each subject up to the super-resources on their own. A revoke of another subject on the same sub-resource does not cancel them.
hasPartialPermissions decides whether retrieving a Thing (ThingCommandEnforcement) or a Policy is authorized, and whether the existence of an inaccessible Thing is hidden. The partially granted subjects determine the receivers of Thing events.
Proof of concept
Using the deployment/docker docker-compose setup, which enables pre-authentication and exposes the gateway directly on port 8081 (the gateway accepts several comma-separated pre-authenticated subjects, which stands in for e.g. a JWT yielding several subjects):
- Create a policy where
nginx:aliceis grantedWRITEonthing:/,READonthing:/attributes/pubandREADonthing:/features/secret, whilenginx:group-xhasWRITErevoked onthing:/attributesandREADrevoked onthing:/features/secret, plus a Thing using it:
```sh curl -u ditto:ditto -X PUT -H 'Content-Type: application/json' \ localhost/
curl -u ditto:ditto -X PUT -H 'Content-Type: application/json' \ localhost/
- Multi-permission check as the authorization context
{nginx:alice, nginx:group-x}:
``sh curl -X POST -H 'Content-Type: application/json' \ -H 'x-ditto-pre-authenticated: nginx:alice,nginx:group-x' \ http://localhost:8081/api/2/checkPermissions -d '{ "readWrite": { "resource": "thing:/attributes/pub", "entityId": "org.example:poc", "hasPermissions": ["READ", "WRITE"] }, "write": { "resource": "thing:/attributes/pub", "entityId": "org.example:poc", "hasPermissions": ["WRITE"] } }' ``
Vulnerable versions answer {"readWrite": true, "write": false}. The correct answer is {"readWrite": false, "write": false}, because WRITE on thing:/attributes/pub is revoked.
- Partial permission check as the same authorization context, retrieving the
secretfeature which is the only resource belowthing:/featuresgranted tonginx:alice, but revoked fornginx:group-x:
``sh curl -i -H 'x-ditto-pre-authenticated: nginx:alice,nginx:group-x' \ http://localhost:8081/api/2/things/org.example:poc/features ``
Vulnerable versions answer 200 with an empty JSON object. The correct answer is 404, as nothing below thing:/features is readable for this authorization context.
Ditto's own enforcement of modifications stays correct throughout, e.g. writing thing:/attributes/pub with this authorization context is rejected with 403.
Impact
Incorrect authorization (CWE-863).
- Ditto: requesters whose authorization context contains several subjects, or (with the memory-optimized policy evaluator) whose policy grants and revokes a permission on the same resource, can learn about the existence of Things and Policies and receive metadata (empty views, events with empty payloads) where the policy grants them no readable content. No field values are disclosed and no modification is authorized.
- Applications using
checkPermissionswith more than one permission per check entry and acting on the result themselves can be told that a subject holds a set of permissions it does not hold.
Not impacted:
- authorization of modifying commands (
WRITE) and of live messages, - the content of retrieved Things and Policies (filtered field by field),
- policies which never grant and revoke the same permission for subjects of the same authorization context on the same resource.
Workarounds
Until a patched version is deployed:
- Avoid policies which grant a permission to one subject and revoke the same permission on the same resource from another subject which can be part of the same authorization context, and (with the memory-optimized policy evaluator) policies which grant and revoke the same permission on the same resource for one subject.
- Request only one permission per
checkPermissionsentry (e.g. split["READ", "WRITE"]into two entries and combine the results in the application).
Patches
Fixed in Eclipse Ditto 3.8.15 and 3.9.9:
- the trie-based enforcer compares grant and revoke weights per permission and only reports subjects holding all requested permissions,
- both enforcers let a revoke on a sub-resource cancel the grants of all subjects of the authorization context on that sub-resource when checking partial permissions.
A randomized test now verifies both enforcers against a reference evaluation of the policy semantics.