1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*-------------------------------------------------------------------------
*
* pg_snakeoil.c
* ClamAV antivirus integration, can check given data with ClamAV
*
* Copyright (c) 2018-2019, Alexander Sosna <alexander.sosna@credativ.de>
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <ctype.h>
#include "utils/builtins.h"
#include "utils/guc.h"
#if PG_VERSION_NUM >= 100000
#include "utils/varlena.h"
#endif
#include "miscadmin.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <clamav.h>
/*
* Set SNAKEOIL_DEBUG to 1 to enable additional debug output
* This can produce overhead, only enable when needed
*/
#define SNAKEOIL_DEBUG 0
/* ClamAV defines */
#define NO_SIGNATURE_CHANGE 0
#define SIGNATURE_CHANGE 1
PG_MODULE_MAGIC;
void reload_engine(void);
bool update_signatures(void);
void _PG_init(void);
void _PG_fini(void);
struct scan_result scan_data(const char *data, size_t data_size);
Datum pg_snakeoil_find_virus(PG_FUNCTION_ARGS);
Datum pg_snakeoil_virus_name(PG_FUNCTION_ARGS);
/*
* Holds the data of a virus scan
*/
struct scan_result
{
int return_code;
const char *virus_name;
long unsigned int scanned;
};
/*
* Global variable to access the ClamAV engine
*/
struct cl_engine *engine = NULL;
char *signatureDir;
struct cl_stat signatureStat;
void
_PG_init()
{
int rv;
/*
* Get different randomness for each process, recommended by ClamAV
*/
srand(getpid());
elog(DEBUG1, "initializing the pg_snakeoil extension");
rv = cl_init(CL_INIT_DEFAULT);
if (CL_SUCCESS != rv)
{
elog(ERROR, "can't initialize libclamav: %s", cl_strerror(rv));
}
DefineCustomStringVariable("pg_snakeoil.signature_dir",
"ClamAV signature directory",
"ClamAV signature directory",
&signatureDir,
cl_retdbdir(), /* ClamAV default signature directory */
#if PG_VERSION_NUM >= 90500
PGC_SU_BACKEND, /* forbid changing directory after startup, restrict to superusers */
#else
PGC_SUSET,
#endif
0, /* no flags */
NULL, /* GucStringCheckHook check_hook, */
NULL, /* GucStringAssignHook assign_hook, */
NULL); /* GucShowHook show_hook) */
EmitWarningsOnPlaceholders("pg_snakeoil");
reload_engine();
}
void
_PG_fini()
{
cl_engine_free(engine);
}
/*
* Initialize the engine for further use, this takes some time!
*/
void
reload_engine()
{
unsigned int signatureNum = 0;
int rv;
elog(DEBUG1, "reloading ClamAV engine");
if (engine != NULL)
{
elog(DEBUG1, "free existing ClamAV engine");
cl_engine_free(engine);
}
engine = cl_engine_new();
elog(DEBUG1, "using signature dir '%s'", signatureDir);
/*
* Get the current state of the signatures
*/
memset(&signatureStat, 0, sizeof(struct cl_stat));
cl_statinidir(signatureDir, &signatureStat);
/*
* Load the signatures from signatureDir
*/
rv = cl_load(signatureDir, engine, &signatureNum, CL_DB_STDOPT);
if (CL_SUCCESS != rv)
{
elog(ERROR, "failure loading ClamAV databases: %s", cl_strerror(rv));
}
elog(DEBUG1, "(cl_engine_compile)");
rv = cl_engine_compile(engine);
if (CL_SUCCESS != rv)
{
elog(ERROR, "cannot create ClamAV engine: %s", cl_strerror(rv));
cl_engine_free(engine);
}
/*
* Only log start info if loaded via shared_preload_libraries, othervise
* we could spam the log.
*/
if (process_shared_preload_libraries_in_progress)
{
elog(LOG, "ClamAV engine started with signatureNum %d from %s",
signatureNum, signatureDir);
}
}
bool
update_signatures()
{
/*
* If signatures have changed, reload the engine
*/
if (cl_statchkdir(&signatureStat) == SIGNATURE_CHANGE)
{
elog(DEBUG1, "newer ClamAV signatures found");
reload_engine();
return true;
}
return false;
}
struct scan_result
scan_data(const char *data, size_t data_size)
{
struct scan_result result = {0, "", 0};
cl_fmap_t *map;
/*
* Open a map for scanning custom data, where the data is already in
* memory, either in the form of a buffer, a memory mapped file, etc. Note
* that the memory [start, start+len) must be the _entire_ file, you can't
* give it parts of a file and expect detection to work.
*/
map = cl_fmap_open_memory(data, data_size);
#ifdef SNAKEOIL_DEBUG
elog(DEBUG4, "data_size: %lu", data_size);
elog(DEBUG4, "data: %s", pnstrdup(data, data_size));
#endif
/*
* Scan data
*/
#if defined(CL_SCAN_STDOPT) /* test for incompatible API change in 0.101 */
/* version 0.100 */
result.return_code = cl_scanmap_callback(map, &result.virus_name,
&result.scanned, engine, CL_SCAN_STDOPT, NULL);
#else
{
/* version 0.101 */
static struct cl_scan_options cl_scan_options = {
.parse = CL_SCAN_PARSE_ARCHIVE | CL_SCAN_PARSE_ELF
| CL_SCAN_PARSE_PDF | CL_SCAN_PARSE_SWF | CL_SCAN_PARSE_HWP3
| CL_SCAN_PARSE_XMLDOCS | CL_SCAN_PARSE_MAIL
| CL_SCAN_PARSE_OLE2 | CL_SCAN_PARSE_HTML | CL_SCAN_PARSE_PE
};
result.return_code =
cl_scanmap_callback(map,
NULL,
&result.virus_name,
&result.scanned,
engine,
&cl_scan_options,
NULL);
}
#endif
elog(DEBUG2, "cl_scanmap_callback returned: %d virusname: %s",
result.return_code, result.virus_name);
/*
* Releases resources associated with the map, you should release any
* resources you hold only after (handles, maps) calling this function
*/
cl_fmap_close(map);
return result;
}
PG_FUNCTION_INFO_V1(so_update_signatures);
Datum
so_update_signatures(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(update_signatures());
}
PG_FUNCTION_INFO_V1(so_is_infected);
Datum
so_is_infected(PG_FUNCTION_ARGS)
{
bytea *input = PG_GETARG_BYTEA_P(0);
const char *data;
size_t data_size;
struct scan_result result;
/*
* Extract a pointer to the actual character data
*/
data = VARDATA_ANY(input);
data_size = VARSIZE_ANY_EXHDR(input);
result = scan_data(data, data_size);
if (result.return_code == 0)
{
PG_RETURN_BOOL(false);
}
else
{
elog(DEBUG1, "Virus found: %s", result.virus_name);
PG_RETURN_BOOL(true);
}
}
PG_FUNCTION_INFO_V1(so_virus_name);
Datum
so_virus_name(PG_FUNCTION_ARGS)
{
bytea *input = PG_GETARG_BYTEA_P(0);
const char *data;
size_t data_size;
struct scan_result result;
/*
* Extract a pointer to the actual character data
*/
data = VARDATA_ANY(input);
data_size = VARSIZE_ANY_EXHDR(input);
result = scan_data(data, data_size);
if (result.return_code == 0)
{
PG_RETURN_NULL();
}
else
{
PG_RETURN_TEXT_P(cstring_to_text(result.virus_name));
}
}
|