summaryrefslogtreecommitdiff
path: root/src/acl.c
diff options
context:
space:
mode:
authorranshid <88133677+ranshid@users.noreply.github.com>2022-03-15 17:14:53 +0200
committerGitHub <noreply@github.com>2022-03-15 17:14:53 +0200
commit1078e30c5ff0e13e18192faada8efe3e97299db3 (patch)
tree88ec59d61f23fdeee08fbfb084dec9623cdbcbd0 /src/acl.c
parentcf6dcb7bf1f65dd52d97e134223eb6661aa69f64 (diff)
downloadredis-1078e30c5ff0e13e18192faada8efe3e97299db3.tar.gz
make sort/ro commands validate external keys access patterns (#10106) (#10340)
Currently the sort and sort_ro can access external keys via `GET` and `BY` in order to make sure the user cannot violate the authorization ACL rules, the decision is to reject external keys access patterns unless ACL allows SORT full access to all keys. I.e. for backwards compatibility, SORT with GET/BY keeps working, but if ACL has restrictions to certain keys, these features get permission denied. ### Implemented solution We have discussed several potential solutions and decided to only allow the GET and BY arguments when the user has all key permissions with the SORT command. The reasons being that SORT with GET or BY is problematic anyway, for instance it is not supported in cluster mode since it doesn't declare keys, and we're not sure the combination of that feature with ACL key restriction is really required. **HOWEVER** If in the fullness of time we will identify a real need for fine grain access support for SORT, we would implement the complete solution which is the alternative described below. ### Alternative (Completion solution): Check sort ACL rules after executing it and before committing output (either via store or to COB). it would require making several changes to the sort command itself. and would potentially cause performance degradation since we will have to collect all the get keys instead of just applying them to a temp array and then scan the access keys against the ACL selectors. This solution can include an optimization to avoid the overheads of collecting the key names, in case the ACL rules grant SORT full key-access, or if the ACL key pattern literal matches the one used in GET/BY. It would also mean that authorization would be O(nlogn) since we will have to complete most of the command execution before we can perform verification Co-authored-by: Madelyn Olson <madelyneolson@gmail.com> Co-authored-by: Oran Agra <oran@redislabs.com>
Diffstat (limited to 'src/acl.c')
-rw-r--r--src/acl.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/acl.c b/src/acl.c
index 206680ac5..e4d7744a7 100644
--- a/src/acl.c
+++ b/src/acl.c
@@ -1528,6 +1528,37 @@ static int ACLSelectorCheckKey(aclSelector *selector, const char *key, int keyle
return ACL_DENIED_KEY;
}
+/* Checks if the provided selector selector has access specified in flags
+ * to all keys in the keyspace. For example, CMD_KEY_READ access requires either
+ * '%R~*', '~*', or allkeys to be granted to the selector. Returns 1 if all
+ * the access flags are satisfied with this selector or 0 otherwise.
+ */
+static int ACLSelectorHasUnrestrictedKeyAccess(aclSelector *selector, int flags) {
+ /* The selector can access any key */
+ if (selector->flags & SELECTOR_FLAG_ALLKEYS) return 1;
+
+ listIter li;
+ listNode *ln;
+ listRewind(selector->patterns,&li);
+
+ int access_flags = 0;
+ if (flags & CMD_KEY_ACCESS) access_flags |= ACL_READ_PERMISSION;
+ if (flags & CMD_KEY_INSERT) access_flags |= ACL_WRITE_PERMISSION;
+ if (flags & CMD_KEY_DELETE) access_flags |= ACL_WRITE_PERMISSION;
+ if (flags & CMD_KEY_UPDATE) access_flags |= ACL_WRITE_PERMISSION;
+
+ /* Test this key against every pattern. */
+ while((ln = listNext(&li))) {
+ keyPattern *pattern = listNodeValue(ln);
+ if ((pattern->flags & access_flags) != access_flags)
+ continue;
+ if (!strcmp(pattern->pattern,"*")) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
/* Checks a channel against a provided list of channels. The is_pattern
* argument should only be used when subscribing (not when publishing)
* and controls whether the input channel is evaluated as a channel pattern
@@ -1672,6 +1703,39 @@ int ACLUserCheckKeyPerm(user *u, const char *key, int keylen, int flags) {
return ACL_DENIED_KEY;
}
+/* Checks if the user can execute the given command with the added restriction
+ * it must also have the access specified in flags to any key in the key space.
+ * For example, CMD_KEY_READ access requires either '%R~*', '~*', or allkeys to be
+ * granted in addition to the access required by the command. Returns 1
+ * if the user has access or 0 otherwise.
+ */
+int ACLUserCheckCmdWithUnrestrictedKeyAccess(user *u, struct redisCommand *cmd, robj **argv, int argc, int flags) {
+ listIter li;
+ listNode *ln;
+ int local_idxptr;
+
+ /* If there is no associated user, the connection can run anything. */
+ if (u == NULL) return 1;
+
+ /* For multiple selectors, we cache the key result in between selector
+ * calls to prevent duplicate lookups. */
+ aclKeyResultCache cache;
+ initACLKeyResultCache(&cache);
+
+ /* Check each selector sequentially */
+ listRewind(u->selectors,&li);
+ while((ln = listNext(&li))) {
+ aclSelector *s = (aclSelector *) listNodeValue(ln);
+ int acl_retval = ACLSelectorCheckCmd(s, cmd, argv, argc, &local_idxptr, &cache);
+ if (acl_retval == ACL_OK && ACLSelectorHasUnrestrictedKeyAccess(s, flags)) {
+ cleanupACLKeyResultCache(&cache);
+ return 1;
+ }
+ }
+ cleanupACLKeyResultCache(&cache);
+ return 0;
+}
+
/* Check if the channel can be accessed by the client according to
* the ACLs associated with the specified user.
*