Skip to content

Commit

Permalink
auth file, using alternative bcmp implementation
Browse files Browse the repository at this point in the history
... instead to
check the token. less optimised than the usual memcmp especially
it goes through the whole buffers but more resilient against possible
attacks.

While at it, constifying a var which should have been.
  • Loading branch information
devnexen authored and dormando committed Jan 14, 2020
1 parent 6beabdf commit 81d6ddc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
7 changes: 5 additions & 2 deletions authfile.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <inttypes.h>

#include "authfile.h"
#include "util.h"

// TODO: frontend needs a refactor so this can avoid global objects.

Expand Down Expand Up @@ -114,8 +117,8 @@ int authfile_check(const char *user, const char *pass) {
for (int x = 0; x < entry_cnt; x++) {
auth_t *e = &main_auth_entries[x];
if (ulen == e->ulen && plen == e->plen &&
memcmp(user, e->user, e->ulen) == 0 &&
memcmp(pass, e->pass, e->plen) == 0) {
safe_memcmp(user, e->user, e->ulen) &&
safe_memcmp(pass, e->pass, e->plen)) {
return 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion extstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void extstore_get_page_data(void *ptr, struct extstore_stats *st) {
}

const char *extstore_err(enum extstore_res res) {
char *rv = "unknown error";
const char *rv = "unknown error";
switch (res) {
case EXTSTORE_INIT_BAD_WBUF_SIZE:
rv = "page_size must be divisible by wbuf_size";
Expand Down
17 changes: 17 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ bool safe_strcpy(char *dst, const char *src, const size_t dstmax) {
}
}

bool safe_memcmp(const void *a, const void *b, size_t len) {
const volatile unsigned char *ua = (const volatile unsigned char *)a;
const volatile unsigned char *ub = (const volatile unsigned char *)b;
int delta = 0;
size_t x;

for (x = 0; x < len; x++) {
delta |= ua[x] ^ ub[x];
}

if (delta == 0) {
return true;
} else {
return false;
}
}

void vperror(const char *fmt, ...) {
int old_errno = errno;
char buf[1024];
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bool safe_strtoul(const char *str, uint32_t *out);
bool safe_strtol(const char *str, int32_t *out);
bool safe_strtod(const char *str, double *out);
bool safe_strcpy(char *dst, const char *src, const size_t dstmax);
bool safe_memcmp(const void *a, const void *b, size_t len);

#ifndef HAVE_HTONLL
extern uint64_t htonll(uint64_t);
Expand Down

0 comments on commit 81d6ddc

Please sign in to comment.