Skip to content

Commit dd78f74

Browse files
authored
Merge pull request redis#7255 from madolson/dev-unstable-converge-hash-validation
Converge hash validation for adding and removing
2 parents c9ef9f1 + f0f30fc commit dd78f74

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/acl.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ sds ACLHashPassword(unsigned char *cleartext, size_t len) {
166166
return sdsnewlen(hex,HASH_PASSWORD_LEN);
167167
}
168168

169+
/* Given a hash and the hash length, returns C_OK if it is a valid password
170+
* hash, or C_ERR otherwise. */
171+
int ACLCheckPasswordHash(unsigned char *hash, int hashlen) {
172+
if (hashlen != HASH_PASSWORD_LEN) {
173+
return C_ERR;
174+
}
175+
176+
/* Password hashes can only be characters that represent
177+
* hexadecimal values, which are numbers and lowercase
178+
* characters 'a' through 'f'. */
179+
for(int i = 0; i < HASH_PASSWORD_LEN; i++) {
180+
char c = hash[i];
181+
if ((c < 'a' || c > 'f') && (c < '0' || c > '9')) {
182+
return C_ERR;
183+
}
184+
}
185+
return C_OK;
186+
}
187+
169188
/* =============================================================================
170189
* Low level ACL API
171190
* ==========================================================================*/
@@ -753,22 +772,10 @@ int ACLSetUser(user *u, const char *op, ssize_t oplen) {
753772
if (op[0] == '>') {
754773
newpass = ACLHashPassword((unsigned char*)op+1,oplen-1);
755774
} else {
756-
if (oplen != HASH_PASSWORD_LEN + 1) {
775+
if (ACLCheckPasswordHash((unsigned char*)op+1,oplen-1) == C_ERR) {
757776
errno = EBADMSG;
758777
return C_ERR;
759778
}
760-
761-
/* Password hashes can only be characters that represent
762-
* hexadecimal values, which are numbers and lowercase
763-
* characters 'a' through 'f'.
764-
*/
765-
for(int i = 1; i < HASH_PASSWORD_LEN + 1; i++) {
766-
char c = op[i];
767-
if ((c < 'a' || c > 'f') && (c < '0' || c > '9')) {
768-
errno = EBADMSG;
769-
return C_ERR;
770-
}
771-
}
772779
newpass = sdsnewlen(op+1,oplen-1);
773780
}
774781

@@ -784,7 +791,7 @@ int ACLSetUser(user *u, const char *op, ssize_t oplen) {
784791
if (op[0] == '<') {
785792
delpass = ACLHashPassword((unsigned char*)op+1,oplen-1);
786793
} else {
787-
if (oplen != HASH_PASSWORD_LEN + 1) {
794+
if (ACLCheckPasswordHash((unsigned char*)op+1,oplen-1) == C_ERR) {
788795
errno = EBADMSG;
789796
return C_ERR;
790797
}

0 commit comments

Comments
 (0)