ClamAV is an open source (GPLv2) anti-virus toolkit.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
clamav/libclamav/cache.c

719 lines
19 KiB

/*
* Copyright (C) 2010 Sourcefire, Inc.
*
* Authors: aCaB <acab@clamav.net>, Török Edvin <edwin@clamav.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include "md5.h"
#include "mpool.h"
#include "clamav.h"
#include "cache.h"
#include "fmap.h"
#ifdef CL_THREAD_SAFE
static pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
#define pthread_mutex_lock(x) 0
#define pthread_mutex_unlock(x)
#define pthread_mutex_init(a, b) 0
#define pthread_mutex_destroy(a) do { } while(0)
#endif
16 years ago
/* The number of root trees and the chooser function
Each tree is protected by a mutex against concurrent access */
/* #define TREES 1 */
/* static inline unsigned int getkey(uint8_t *hash) { return 0; } */
#define TREES 256
static inline unsigned int getkey(uint8_t *hash) { return *hash; }
/* #define TREES 4096 */
/* static inline unsigned int getkey(uint8_t *hash) { return hash[0] | ((unsigned int)(hash[1] & 0xf)<<8) ; } */
/* #define TREES 65536 */
/* static inline unsigned int getkey(uint8_t *hash) { return hash[0] | (((unsigned int)hash[1])<<8) ; } */
/* The number of nodes in each tree */
#define NODES 256
16 years ago
16 years ago
/* The replacement policy algorithm to use */
/* #define USE_LRUHASHCACHE */
#define USE_SPLAY
16 years ago
/* LRUHASHCACHE --------------------------------------------------------------------- */
16 years ago
#ifdef USE_LRUHASHCACHE
struct cache_key {
16 years ago
int64_t digest[2];
uint32_t size; /* 0 is used to mark an empty hash slot! */
struct cache_key *lru_next, *lru_prev;
};
struct cache_set {
struct cache_key *data;
size_t maxelements; /* considering load factor */
16 years ago
size_t maxdeleted;
size_t elements;
16 years ago
size_t deleted;
struct cache_key *lru_head, *lru_tail;
};
#define CACHE_KEY_DELETED ~0u
#define CACHE_KEY_EMPTY 0
static void cacheset_lru_remove(struct cache_set *map, size_t howmany)
{
while (howmany--) {
struct cache_key *old;
assert(map->lru_head);
assert(!old->lru_prev);
/* Remove a key from the head of the list */
old = map->lru_head;
map->lru_head = old->lru_next;
old->size = CACHE_KEY_DELETED;
/* This slot is now deleted, it is not empty,
* because previously we could have inserted a key that has seen this
* slot as occupied, to find that key we need to ensure that all keys
* that were occupied when the key was inserted, are seen as occupied
* when searching too.
* Of course when inserting a new value, we treat deleted slots as
* empty.
* We only replace old values with new values, but there is no guarantee
* that the newly inserted value would hash to same place as the value
* we remove due to LRU! */
if (old == map->lru_tail)
map->lru_tail = 0;
map->elements--;
16 years ago
map->deleted++;
}
}
16 years ago
static inline int cacheset_lookup_internal(struct cache_set *map,
const char *md5, size_t size,
uint32_t *insert_pos, int deletedok)
{
16 years ago
const struct cache_key*data = map->data;
uint32_t capmask = NODES - 1;
16 years ago
const struct cache_key *k;
uint32_t idx, tries = 0;
uint64_t md5_0, md5_1;
uint64_t md5a[2];
memcpy(&md5a, md5, 16);
md5_0 = md5a[0];
md5_1 = md5a[1];
idx = md5_1 & capmask;
k = &data[idx];
while (k->size != CACHE_KEY_EMPTY && tries <= capmask) {
if (k->digest[0] == md5_0 &&
k->digest[1] == md5_1 &&
k->size == size) {
/* found key */
*insert_pos = idx;
return 1;
}
16 years ago
if (deletedok && k->size == CACHE_KEY_DELETED) {
/* treat deleted slot as empty */
*insert_pos = idx;
return 0;
16 years ago
}
idx = (idx + tries++) & capmask;
k = &data[idx];
}
/* found empty pos */
*insert_pos = idx;
return 0;
}
static inline void lru_remove(struct cache_set *map, struct cache_key *newkey)
{
if (newkey->lru_next)
newkey->lru_next->lru_prev = newkey->lru_prev;
if (newkey->lru_prev)
newkey->lru_prev->lru_next = newkey->lru_next;
if (newkey == map->lru_head)
map->lru_head = newkey->lru_next;
}
static inline void lru_addtail(struct cache_set *map, struct cache_key *newkey)
{
if (!map->lru_head)
map->lru_head = newkey;
if (map->lru_tail)
map->lru_tail->lru_next = newkey;
newkey->lru_next = NULL;
newkey->lru_prev = map->lru_tail;
map->lru_tail = newkey;
}
16 years ago
static void cacheset_add(struct cache_set *map, unsigned char *md5, size_t size, mpool_t *mempool);
static int cacheset_init(struct cache_set *map, mpool_t *mempool);
16 years ago
static void cacheset_rehash(struct cache_set *map, mpool_t *mempool)
16 years ago
{
unsigned i;
int ret;
struct cache_set tmp_set;
struct cache_key *key;
pthread_mutex_lock(&pool_mutex);
ret = cacheset_init(&tmp_set, mempool);
16 years ago
pthread_mutex_unlock(&pool_mutex);
if (ret)
return;
key = map->lru_head;
for (i=0;key && i < tmp_set.maxelements/2;i++) {
cacheset_add(&tmp_set, (unsigned char*)&key->digest, key->size, mempool);
16 years ago
key = key->lru_next;
}
pthread_mutex_lock(&pool_mutex);
mpool_free(mempool, map->data);
pthread_mutex_unlock(&pool_mutex);
memcpy(map, &tmp_set, sizeof(tmp_set));
}
static void cacheset_add(struct cache_set *map, unsigned char *md5, size_t size, mpool_t *mempool)
{
int ret;
uint32_t pos;
struct cache_key *newkey;
16 years ago
if (map->elements >= map->maxelements) {
cacheset_lru_remove(map, 1);
16 years ago
if (map->deleted >= map->maxdeleted) {
cacheset_rehash(map, mempool);
16 years ago
}
}
assert(map->elements < map->maxelements);
16 years ago
ret = cacheset_lookup_internal(map, md5, size, &pos, 1);
newkey = &map->data[pos];
16 years ago
if (newkey->size == CACHE_KEY_DELETED)
map->deleted--;
if (ret) {
/* was already added, remove from LRU list */
lru_remove(map, newkey);
}
/* add new key to tail of LRU list */
16 years ago
memcpy(&map->data[pos].digest, md5, sizeof(map->data[pos].digest));
map->data[pos].size = size;
lru_addtail(map, newkey);
map->elements++;
assert(pos < map->maxelements);
}
16 years ago
static int cacheset_lookup(struct cache_set *map, unsigned char *md5, size_t size)
{
struct cache_key *newkey;
int ret;
uint32_t pos;
16 years ago
16 years ago
ret = cacheset_lookup_internal(map, md5, size, &pos, 0);
if (!ret)
return 0;
newkey = &map->data[pos];
/* update LRU position: move to tail */
lru_remove(map, newkey);
lru_addtail(map, newkey);
return 1;
}
static int cacheset_init(struct cache_set *map, mpool_t *mempool) {
map->data = mpool_calloc(mempool, NODES, sizeof(*map->data));
16 years ago
if (!map->data)
return CL_EMEM;
map->maxelements = 80 * NODES / 100;
map->maxdeleted = NODES - map->maxelements - 1;
16 years ago
map->elements = 0;
map->lru_head = map->lru_tail = NULL;
return 0;
}
static inline void cacheset_destroy(struct cache_set *cs, mpool_t *mempool) {
mpool_free(mempool, cs->data);
cs->data = NULL;
}
16 years ago
#endif /* USE_LRUHASHCACHE */
16 years ago
16 years ago
/* SPLAY --------------------------------------------------------------------- */
16 years ago
#ifdef USE_SPLAY
16 years ago
struct node { /* a node */
16 years ago
int64_t digest[2];
16 years ago
struct node *left;
struct node *right;
struct node *up;
16 years ago
struct node *next;
struct node *prev;
uint32_t size;
15 years ago
uint32_t minrec;
16 years ago
};
16 years ago
struct cache_set { /* a tree */
16 years ago
struct node *data;
struct node *root;
16 years ago
struct node *first;
struct node *last;
16 years ago
};
16 years ago
/* Allocates all the nodes and sets up the replacement chain */
static int cacheset_init(struct cache_set *cs, mpool_t *mempool) {
16 years ago
unsigned int i;
cs->data = mpool_calloc(mempool, NODES, sizeof(*cs->data));
16 years ago
cs->root = NULL;
16 years ago
16 years ago
if(!cs->data)
return 1;
16 years ago
for(i=1; i<NODES; i++) {
16 years ago
cs->data[i-1].next = &cs->data[i];
cs->data[i].prev = &cs->data[i-1];
}
cs->first = cs->data;
cs->last = &cs->data[NODES-1];
16 years ago
16 years ago
return 0;
}
16 years ago
/* Frees all the nodes */
static inline void cacheset_destroy(struct cache_set *cs, mpool_t *mempool) {
mpool_free(mempool, cs->data);
cs->data = NULL;
}
16 years ago
/* The left/right cooser for the splay tree */
static inline int cmp(int64_t *a, ssize_t sa, int64_t *b, ssize_t sb) {
16 years ago
if(a[1] < b[1]) return -1;
if(a[1] > b[1]) return 1;
if(a[0] < b[0]) return -1;
16 years ago
if(a[0] > b[0]) return 1;
if(sa < sb) return -1;
if(sa > sb) return 1;
return 0;
16 years ago
}
16 years ago
16 years ago
16 years ago
/* #define PRINT_TREE */
16 years ago
#ifdef PRINT_TREE
#define ptree printf
#else
16 years ago
#define ptree(...)
16 years ago
#endif
16 years ago
/* Debug function to print the tree and check its consistency */
/* #define CHECK_TREE */
16 years ago
#ifdef CHECK_TREE
static int printtree(struct cache_set *cs, struct node *n, int d) {
int i;
int ab = 0;
if (n == NULL) return 0;
16 years ago
if(n == cs->root) { ptree("--------------------------\n"); }
16 years ago
ab |= printtree(cs, n->right, d+1);
if(n->right) {
16 years ago
if(cmp(n->digest, n->size, n->right->digest, n->right->size) >= 0) {
16 years ago
for (i=0; i<d; i++) ptree(" ");
16 years ago
ptree("^^^^ %lld >= %lld\n", n->digest[1], n->right->digest[1]);
16 years ago
ab = 1;
}
}
for (i=0; i<d; i++) ptree(" ");
ptree("%08x(%02u)\n", n->digest[1]>>48, n - cs->data);
if(n->left) {
16 years ago
if(cmp(n->digest, n->size, n->left->digest, n->left->size) <= 0) {
16 years ago
for (i=0; i<d; i++) ptree(" ");
16 years ago
ptree("vvvv %lld <= %lld\n", n->digest[1], n->left->digest[1]);
16 years ago
ab = 1;
}
}
if(d){
if(!n->up) {
ptree("no parent!\n");
ab = 1;
} else {
if(n->up->left != n && n->up->right != n) {
ptree("broken parent\n");
ab = 1;
}
}
} else {
if(n->up) {
ptree("root with a parent!\n");
ab = 1;
}
}
ab |= printtree(cs, n->left, d+1);
return ab;
}
#else
#define printtree(a,b,c) (0)
16 years ago
#endif
16 years ago
/* Looks up a node and splays it up to the root of the tree */
static int splay(int64_t *md5, size_t len, struct cache_set *cs) {
15 years ago
struct node next = {{0, 0}, NULL, NULL, NULL, NULL, NULL, 0, 0}, *right = &next, *left = &next, *temp, *root = cs->root;
16 years ago
int comp, found = 0;
16 years ago
if(!root)
return 0;
16 years ago
while(1) {
16 years ago
comp = cmp(md5, len, root->digest, root->size);
16 years ago
if(comp < 0) {
16 years ago
if(!root->left) break;
16 years ago
if(cmp(md5, len, root->left->digest, root->left->size) < 0) {
16 years ago
temp = root->left;
root->left = temp->right;
16 years ago
if(temp->right) temp->right->up = root;
16 years ago
temp->right = root;
16 years ago
root->up = temp;
16 years ago
root = temp;
if(!root->left) break;
16 years ago
}
right->left = root;
16 years ago
root->up = right;
16 years ago
right = root;
root = root->left;
16 years ago
} else if(comp > 0) {
16 years ago
if(!root->right) break;
16 years ago
if(cmp(md5, len, root->right->digest, root->right->size) > 0) {
16 years ago
temp = root->right;
root->right = temp->left;
16 years ago
if(temp->left) temp->left->up = root;
16 years ago
temp->left = root;
16 years ago
root->up = temp;
16 years ago
root = temp;
if(!root->right) break;
}
left->right = root;
16 years ago
root->up = left;
16 years ago
left = root;
root = root->right;
} else {
16 years ago
found = 1;
break;
}
16 years ago
}
16 years ago
16 years ago
left->right = root->left;
16 years ago
if(root->left) root->left->up = left;
16 years ago
right->left = root->right;
16 years ago
if(root->right) root->right->up = right;
16 years ago
root->left = next.right;
16 years ago
if(next.right) next.right->up = root;
16 years ago
root->right = next.left;
16 years ago
if(next.left) next.left->up = root;
root->up = NULL;
16 years ago
cs->root = root;
16 years ago
return found;
16 years ago
}
16 years ago
/* Looks up an hash in the tree and maintains the replacement chain */
15 years ago
static inline int cacheset_lookup(struct cache_set *cs, unsigned char *md5, size_t size, uint32_t reclevel) {
16 years ago
int64_t hash[2];
16 years ago
memcpy(hash, md5, 16);
16 years ago
if(splay(hash, size, cs)) {
16 years ago
struct node *o = cs->root->prev, *p = cs->root, *q = cs->root->next;
#ifdef PRINT_CHAINS
printf("promoting %02d\n", p - cs->data);
{
struct node *x = cs->first;
printf("before: ");
while(x) {
printf("%02d,", x - cs->data);
x=x->next;
}
printf(" --- ");
x=cs->last;
while(x) {
printf("%02d,", x - cs->data);
x=x->prev;
}
printf("\n");
}
#endif
if(q) {
if(o)
o->next = q;
else
cs->first = q;
q->prev = o;
cs->last->next = p;
p->prev = cs->last;
p->next = NULL;
cs->last = p;
}
#ifdef PRINT_CHAINS
{
struct node *x = cs->first;
printf("after : ");
while(x) {
printf("%02d,", x - cs->data);
x=x->next;
}
printf(" --- ");
x=cs->last;
while(x) {
printf("%02d,", x - cs->data);
x=x->prev;
}
printf("\n");
}
#endif
15 years ago
if(reclevel >= p->minrec)
return 1;
16 years ago
}
return 0;
16 years ago
}
16 years ago
/* If the hash is present nothing happens.
Otherwise a new node is created for the hash picking one from the begin of the chain.
Used nodes are moved to the end of the chain */
15 years ago
static inline void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size, uint32_t reclevel) {
16 years ago
struct node *newnode;
16 years ago
int64_t hash[2];
16 years ago
memcpy(hash, md5, 16);
15 years ago
if(splay(hash, size, cs)) {
if(cs->root->minrec > reclevel)
cs->root->minrec = reclevel;
16 years ago
return; /* Already there */
15 years ago
}
16 years ago
16 years ago
ptree("1:\n");
if(printtree(cs, cs->root, 0)) {
cli_errmsg("cacheset_add: inconsistent tree before choosing newnode, good luck\n");
return;
16 years ago
}
16 years ago
newnode = cs->first;
while(newnode) {
16 years ago
if(!newnode->right && !newnode->left)
break;
newnode = newnode->next;
16 years ago
}
if(!newnode) {
cli_errmsg("cacheset_add: tree has got no end nodes\n");
return;
16 years ago
}
if(newnode->up) {
16 years ago
if(newnode->up->left == newnode)
newnode->up->left = NULL;
else
newnode->up->right = NULL;
16 years ago
}
if(newnode->prev)
16 years ago
newnode->prev->next = newnode->next;
16 years ago
if(newnode->next)
16 years ago
newnode->next->prev = newnode->prev;
16 years ago
if(cs->first == newnode)
16 years ago
cs->first = newnode->next;
16 years ago
newnode->prev = cs->last;
newnode->next = NULL;
cs->last->next = newnode;
cs->last = newnode;
16 years ago
ptree("2:\n");
if(printtree(cs, cs->root, 0)) {
cli_errmsg("cacheset_add: inconsistent tree before adding newnode, good luck\n");
return;
16 years ago
}
16 years ago
if(!cs->root) {
newnode->left = NULL;
newnode->right = NULL;
} else {
16 years ago
if(cmp(hash, size, cs->root->digest, cs->root->size) < 0) {
16 years ago
newnode->left = cs->root->left;
newnode->right = cs->root;
cs->root->left = NULL;
} else {
newnode->right = cs->root->right;
newnode->left = cs->root;
cs->root->right = NULL;
}
if(newnode->left) newnode->left->up = newnode;
if(newnode->right) newnode->right->up = newnode;
16 years ago
}
newnode->digest[0] = hash[0];
newnode->digest[1] = hash[1];
16 years ago
newnode->up = NULL;
16 years ago
newnode->size = size;
15 years ago
newnode->minrec = reclevel;
16 years ago
cs->root = newnode;
16 years ago
ptree("3: %lld\n", hash[1]);
if(printtree(cs, cs->root, 0)) {
cli_errmsg("cacheset_add: inconsistent tree after adding newnode, good luck\n");
return;
16 years ago
}
16 years ago
}
#endif /* USE_SPLAY */
16 years ago
/* COMMON STUFF --------------------------------------------------------------------- */
struct CACHE {
16 years ago
struct cache_set cacheset;
#ifdef CL_THREAD_SAFE
16 years ago
pthread_mutex_t mutex;
#endif
};
16 years ago
16 years ago
/* Allocates the trees for the engine cache */
int cli_cache_init(struct cl_engine *engine) {
struct CACHE *cache;
unsigned int i, j;
if(!engine) {
cli_errmsg("cli_cache_init: mpool malloc fail\n");
return 1;
}
if(!(cache = mpool_malloc(engine->mempool, sizeof(struct CACHE) * TREES))) {
cli_errmsg("cli_cache_init: mpool malloc fail\n");
return 1;
}
for(i=0; i<TREES; i++) {
if(pthread_mutex_init(&cache[i].mutex, NULL)) {
cli_errmsg("cli_cache_init: mutex init fail\n");
for(j=0; j<i; j++) cacheset_destroy(&cache[j].cacheset, engine->mempool);
for(j=0; j<i; j++) pthread_mutex_destroy(&cache[j].mutex);
mpool_free(engine->mempool, cache);
return 1;
}
if(cacheset_init(&cache[i].cacheset, engine->mempool)) {
for(j=0; j<i; j++) cacheset_destroy(&cache[j].cacheset, engine->mempool);
for(j=0; j<=i; j++) pthread_mutex_destroy(&cache[j].mutex);
mpool_free(engine->mempool, cache);
16 years ago
return 1;
}
}
engine->cache = cache;
return 0;
}
16 years ago
/* Frees the engine cache */
void cli_cache_destroy(struct cl_engine *engine) {
struct CACHE *cache;
unsigned int i;
16 years ago
if(!engine || !(cache = engine->cache))
return;
for(i=0; i<TREES; i++) {
cacheset_destroy(&cache[i].cacheset, engine->mempool);
pthread_mutex_destroy(&cache[i].mutex);
}
mpool_free(engine->mempool, cache);
}
16 years ago
/* Looks up an hash in the proper tree */
15 years ago
static int cache_lookup_hash(unsigned char *md5, size_t len, struct CACHE *cache, uint32_t reclevel) {
unsigned int key = getkey(md5);
int ret = CL_VIRUS;
struct CACHE *c;
c = &cache[key];
if(pthread_mutex_lock(&c->mutex)) {
cli_errmsg("cache_lookup_hash: cache_lookup_hash: mutex lock fail\n");
return ret;
}
16 years ago
15 years ago
ret = (cacheset_lookup(&c->cacheset, md5, len, reclevel)) ? CL_CLEAN : CL_VIRUS;
16 years ago
pthread_mutex_unlock(&c->mutex);
16 years ago
/* if(ret == CL_CLEAN) cli_warnmsg("cached\n"); */
return ret;
}
16 years ago
/* Adds an hash to the cache */
void cache_add(unsigned char *md5, size_t size, cli_ctx *ctx) {
unsigned int key = getkey(md5);
15 years ago
uint32_t level;
struct CACHE *c;
if(!ctx || !ctx->engine || !ctx->engine->cache)
return;
level = (*ctx->fmap && (*ctx->fmap)->dont_cache_flag) ? ctx->recursion : 0;
if (ctx->found_possibly_unwanted && (level || !ctx->recursion))
return;
c = &ctx->engine->cache[key];
if(pthread_mutex_lock(&c->mutex)) {
cli_errmsg("cli_add: mutex lock fail\n");
return;
}
16 years ago
#ifdef USE_LRUHASHCACHE
16 years ago
cacheset_add(&c->cacheset, md5, size, ctx->engine->mempool);
#else
#ifdef USE_SPLAY
15 years ago
cacheset_add(&c->cacheset, md5, size, level);
#else
#error #define USE_SPLAY or USE_LRUHASHCACHE
#endif
#endif
16 years ago
pthread_mutex_unlock(&c->mutex);
15 years ago
cli_dbgmsg("cache_add: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x (level %u)\n", md5[0], md5[1], md5[2], md5[3], md5[4], md5[5], md5[6], md5[7], md5[8], md5[9], md5[10], md5[11], md5[12], md5[13], md5[14], md5[15], level);
return;
}
16 years ago
/* Hashes a file onto the provided buffer and looks it up the cache.
Returns CL_VIRUS if found, CL_CLEAN if not FIXME or an error */
int cache_check(unsigned char *hash, cli_ctx *ctx) {
fmap_t *map;
size_t todo, at = 0;
cli_md5_ctx md5;
int ret;
if(!ctx || !ctx->engine || !ctx->engine->cache)
return CL_VIRUS;
map = *ctx->fmap;
todo = map->len;
cli_md5_init(&md5);
while(todo) {
const void *buf;
size_t readme = todo < FILEBUFF ? todo : FILEBUFF;
if(!(buf = fmap_need_off_once(map, at, readme)))
13 years ago
return CL_EREAD;
todo -= readme;
at += readme;
cli_md5_update(&md5, buf, readme);
}
cli_md5_final(hash, &md5);
15 years ago
ret = cache_lookup_hash(hash, map->len, ctx->engine->cache, ctx->recursion);
cli_dbgmsg("cache_check: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x is %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15], (ret == CL_VIRUS) ? "negative" : "positive");
return ret;
}