From f0f30fc43f027ad981c46a2a0b8dd12b9e9c53b5 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Thu, 14 May 2020 11:07:51 -0700 Subject: [PATCH] Converge hash validation for adding and removing --- src/acl.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/acl.c b/src/acl.c index 3194feb5b..bcca116bb 100644 --- a/src/acl.c +++ b/src/acl.c @@ -166,6 +166,25 @@ sds ACLHashPassword(unsigned char *cleartext, size_t len) { return sdsnewlen(hex,HASH_PASSWORD_LEN); } +/* Given a hash and the hash length, returns C_OK if it is a valid password + * hash, or C_ERR otherwise. */ +int ACLCheckPasswordHash(unsigned char *hash, int hashlen) { + if (hashlen != HASH_PASSWORD_LEN) { + return C_ERR; + } + + /* Password hashes can only be characters that represent + * hexadecimal values, which are numbers and lowercase + * characters 'a' through 'f'. */ + for(int i = 0; i < HASH_PASSWORD_LEN; i++) { + char c = hash[i]; + if ((c < 'a' || c > 'f') && (c < '0' || c > '9')) { + return C_ERR; + } + } + return C_OK; +} + /* ============================================================================= * Low level ACL API * ==========================================================================*/ @@ -753,22 +772,10 @@ int ACLSetUser(user *u, const char *op, ssize_t oplen) { if (op[0] == '>') { newpass = ACLHashPassword((unsigned char*)op+1,oplen-1); } else { - if (oplen != HASH_PASSWORD_LEN + 1) { + if (ACLCheckPasswordHash((unsigned char*)op+1,oplen-1) == C_ERR) { errno = EBADMSG; return C_ERR; } - - /* Password hashes can only be characters that represent - * hexadecimal values, which are numbers and lowercase - * characters 'a' through 'f'. - */ - for(int i = 1; i < HASH_PASSWORD_LEN + 1; i++) { - char c = op[i]; - if ((c < 'a' || c > 'f') && (c < '0' || c > '9')) { - errno = EBADMSG; - return C_ERR; - } - } newpass = sdsnewlen(op+1,oplen-1); } @@ -784,7 +791,7 @@ int ACLSetUser(user *u, const char *op, ssize_t oplen) { if (op[0] == '<') { delpass = ACLHashPassword((unsigned char*)op+1,oplen-1); } else { - if (oplen != HASH_PASSWORD_LEN + 1) { + if (ACLCheckPasswordHash((unsigned char*)op+1,oplen-1) == C_ERR) { errno = EBADMSG; return C_ERR; }