2009-06-26 16:30:46 +03:00
|
|
|
/*
|
|
|
|
* Load, verify and execute ClamAV bytecode.
|
|
|
|
*
|
2015-09-17 13:41:26 -04:00
|
|
|
* Copyright (C) 2015 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
2012-12-05 15:48:52 -08:00
|
|
|
* Copyright (C) 2009-2012 Sourcefire, Inc.
|
2009-06-26 16:30:46 +03:00
|
|
|
*
|
|
|
|
* Authors: Török Edvin
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifndef BYTECODE_H
|
|
|
|
#define BYTECODE_H
|
2018-12-05 20:46:20 -05:00
|
|
|
#include "clamav-types.h"
|
2009-06-26 16:30:46 +03:00
|
|
|
#include "clambc.h"
|
2009-11-06 16:34:46 +02:00
|
|
|
#include <stdio.h>
|
2009-12-03 11:37:38 +02:00
|
|
|
#include "fmap.h"
|
2010-07-29 13:22:35 +03:00
|
|
|
#include "bytecode_detect.h"
|
2009-08-20 16:23:43 +03:00
|
|
|
|
2009-06-26 16:30:46 +03:00
|
|
|
struct cli_dbio;
|
|
|
|
struct cli_bc_ctx;
|
|
|
|
struct cli_bc_func;
|
2009-07-07 17:38:56 +03:00
|
|
|
struct cli_bc_value;
|
2009-07-07 19:58:37 +03:00
|
|
|
struct cli_bc_inst;
|
2009-07-31 15:28:36 +03:00
|
|
|
struct cli_bc_type;
|
2009-08-25 18:54:14 +03:00
|
|
|
struct cli_bc_engine;
|
2009-11-30 15:22:20 +02:00
|
|
|
struct cli_bc_dbgnode;
|
2009-08-25 18:54:14 +03:00
|
|
|
struct bitset_tag;
|
2009-10-02 17:33:11 +03:00
|
|
|
struct cl_engine;
|
2009-06-26 16:30:46 +03:00
|
|
|
|
2009-07-23 17:33:11 +03:00
|
|
|
enum bc_state {
|
2009-09-04 17:29:13 +03:00
|
|
|
bc_skip,
|
2009-07-23 17:33:11 +03:00
|
|
|
bc_loaded,
|
|
|
|
bc_jit,
|
2010-07-29 13:22:35 +03:00
|
|
|
bc_interp,
|
|
|
|
bc_disabled
|
2009-07-23 17:33:11 +03:00
|
|
|
};
|
|
|
|
|
2009-06-26 16:30:46 +03:00
|
|
|
struct cli_bc {
|
2012-12-05 15:48:52 -08:00
|
|
|
struct bytecode_metadata metadata;
|
|
|
|
unsigned id;
|
|
|
|
unsigned kind;
|
|
|
|
unsigned num_types;
|
|
|
|
unsigned num_func;
|
|
|
|
struct cli_bc_func *funcs;
|
|
|
|
struct cli_bc_type *types;
|
|
|
|
uint64_t **globals;
|
|
|
|
uint16_t *globaltys;
|
|
|
|
size_t num_globals;
|
|
|
|
enum bc_state state;
|
|
|
|
struct bitset_tag *uses_apis;
|
|
|
|
char *lsig;
|
|
|
|
char *vnameprefix;
|
|
|
|
char **vnames;
|
|
|
|
unsigned vnames_cnt;
|
|
|
|
uint16_t start_tid;
|
|
|
|
struct cli_bc_dbgnode *dbgnodes;
|
|
|
|
unsigned dbgnode_cnt;
|
|
|
|
unsigned hook_lsig_id;
|
|
|
|
unsigned trusted;
|
|
|
|
uint32_t numGlobalBytes;
|
|
|
|
uint8_t *globalBytes;
|
|
|
|
uint32_t sigtime_id, sigmatch_id;
|
2018-12-03 12:40:13 -05:00
|
|
|
char *hook_name;
|
2009-08-25 18:54:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cli_all_bc {
|
|
|
|
struct cli_bc *all_bcs;
|
|
|
|
unsigned count;
|
|
|
|
struct cli_bcengine *engine;
|
2010-07-29 13:22:35 +03:00
|
|
|
struct cli_environment env;
|
2018-12-03 12:40:13 -05:00
|
|
|
int inited;
|
2009-06-26 16:30:46 +03:00
|
|
|
};
|
|
|
|
|
2009-10-02 17:33:11 +03:00
|
|
|
struct cli_pe_hook_data;
|
2010-02-12 16:47:44 +02:00
|
|
|
struct cli_exe_section;
|
2010-08-02 15:42:58 +03:00
|
|
|
struct pdf_obj;
|
2009-07-07 19:58:37 +03:00
|
|
|
struct cli_bc_ctx *cli_bytecode_context_alloc(void);
|
2009-12-02 17:13:07 +02:00
|
|
|
/* FIXME: we can't include others.h because others.h includes us...*/
|
|
|
|
void cli_bytecode_context_setctx(struct cli_bc_ctx *ctx, void *cctx);
|
2009-07-10 16:11:54 +03:00
|
|
|
int cli_bytecode_context_setfuncid(struct cli_bc_ctx *ctx, const struct cli_bc *bc, unsigned funcid);
|
2009-07-07 19:58:37 +03:00
|
|
|
int cli_bytecode_context_setparam_int(struct cli_bc_ctx *ctx, unsigned i, uint64_t c);
|
|
|
|
int cli_bytecode_context_setparam_ptr(struct cli_bc_ctx *ctx, unsigned i, void *data, unsigned datalen);
|
2009-12-03 11:37:38 +02:00
|
|
|
int cli_bytecode_context_setfile(struct cli_bc_ctx *ctx, fmap_t *map);
|
2010-02-12 16:47:44 +02:00
|
|
|
int cli_bytecode_context_setpe(struct cli_bc_ctx *ctx, const struct cli_pe_hook_data *data, const struct cli_exe_section *sections);
|
2018-08-14 14:00:31 -07:00
|
|
|
int cli_bytecode_context_setpdf(struct cli_bc_ctx *ctx, unsigned phase, unsigned nobjs, struct pdf_obj **objs, uint32_t *pdf_flags, uint32_t pdfsize, uint32_t pdfstartoff);
|
2009-07-07 19:58:37 +03:00
|
|
|
int cli_bytecode_context_clear(struct cli_bc_ctx *ctx);
|
2009-12-02 17:13:07 +02:00
|
|
|
/* returns file descriptor, sets tempfile. Caller takes ownership, and is
|
|
|
|
* responsible for freeing/unlinking */
|
|
|
|
int cli_bytecode_context_getresult_file(struct cli_bc_ctx *ctx, char **tempfilename);
|
2009-07-08 12:45:06 +03:00
|
|
|
uint64_t cli_bytecode_context_getresult_int(struct cli_bc_ctx *ctx);
|
2009-07-07 19:58:37 +03:00
|
|
|
void cli_bytecode_context_destroy(struct cli_bc_ctx *ctx);
|
2009-06-26 16:30:46 +03:00
|
|
|
|
2009-12-12 13:09:13 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2009-08-28 20:07:25 +03:00
|
|
|
extern int have_clamjit;
|
2009-12-12 13:09:13 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2010-07-29 13:22:35 +03:00
|
|
|
int cli_bytecode_init(struct cli_all_bc *allbc);
|
2012-12-05 15:48:52 -08:00
|
|
|
int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio, int security, int sigperf);
|
2010-08-11 13:39:49 +03:00
|
|
|
int cli_bytecode_prepare2(struct cl_engine *engine, struct cli_all_bc *allbc, unsigned dconfmask);
|
2009-08-27 20:41:29 +03:00
|
|
|
int cli_bytecode_run(const struct cli_all_bc *bcs, const struct cli_bc *bc, struct cli_bc_ctx *ctx);
|
2009-06-26 16:30:46 +03:00
|
|
|
void cli_bytecode_destroy(struct cli_bc *bc);
|
2009-08-25 18:54:14 +03:00
|
|
|
int cli_bytecode_done(struct cli_all_bc *allbc);
|
2014-05-22 14:30:03 -04:00
|
|
|
|
|
|
|
/* Bytecode IR descriptions */
|
2010-01-22 16:50:16 +02:00
|
|
|
void cli_bytecode_describe(const struct cli_bc *bc);
|
2014-05-22 14:30:03 -04:00
|
|
|
void cli_bytetype_describe(const struct cli_bc *bc);
|
|
|
|
void cli_bytevalue_describe(const struct cli_bc *bc, unsigned funcid);
|
|
|
|
void cli_byteinst_describe(const struct cli_bc_inst *inst, unsigned *bbnum);
|
|
|
|
void cli_bytefunc_describe(const struct cli_bc *bc, unsigned funcid);
|
|
|
|
|
2009-09-22 11:03:17 +03:00
|
|
|
/* Hooks */
|
2009-10-02 17:33:11 +03:00
|
|
|
struct cli_exe_info;
|
2010-01-19 16:38:12 +02:00
|
|
|
struct cli_ctx_tag;
|
2010-08-02 21:50:14 +03:00
|
|
|
struct cli_target_info;
|
2018-12-03 12:40:13 -05:00
|
|
|
int cli_bytecode_runlsig(struct cli_ctx_tag *ctx, struct cli_target_info *info, const struct cli_all_bc *bcs, unsigned bc_idx, const uint32_t *lsigcnt, const uint32_t *lsigsuboff, fmap_t *map);
|
2012-10-18 14:12:58 -07:00
|
|
|
int cli_bytecode_runhook(struct cli_ctx_tag *cctx, const struct cl_engine *engine, struct cli_bc_ctx *ctx, unsigned id, fmap_t *map);
|
2009-09-22 11:03:17 +03:00
|
|
|
|
2009-09-07 18:01:43 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2009-12-09 16:50:55 +02:00
|
|
|
int bytecode_init(void);
|
|
|
|
/* Bytecode internal debug API */
|
2009-09-07 18:01:43 +03:00
|
|
|
void cli_bytecode_debug(int argc, char **argv);
|
2010-01-12 16:16:08 +01:00
|
|
|
void cli_bytecode_printversion(void);
|
2009-12-08 23:02:49 +02:00
|
|
|
void cli_bytecode_debug_printsrc(const struct cli_bc_ctx *ctx);
|
2010-04-19 18:35:30 +03:00
|
|
|
void cli_printcxxver(void);
|
2009-12-09 16:50:55 +02:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
typedef void (*bc_dbg_callback_trace)(struct cli_bc_ctx *, unsigned event);
|
|
|
|
typedef void (*bc_dbg_callback_trace_op)(struct cli_bc_ctx *, const char *op);
|
|
|
|
typedef void (*bc_dbg_callback_trace_val)(struct cli_bc_ctx *, const char *name, uint32_t value);
|
|
|
|
typedef void (*bc_dbg_callback_trace_ptr)(struct cli_bc_ctx *, const void *val);
|
|
|
|
void cli_bytecode_context_set_trace(struct cli_bc_ctx *, unsigned level,
|
|
|
|
bc_dbg_callback_trace,
|
|
|
|
bc_dbg_callback_trace_op,
|
|
|
|
bc_dbg_callback_trace_val,
|
|
|
|
bc_dbg_callback_trace_ptr);
|
2012-12-05 15:48:52 -08:00
|
|
|
void cli_sigperf_print(void);
|
|
|
|
void cli_sigperf_events_destroy(void);
|
2009-09-07 18:01:43 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2009-09-04 12:09:17 +03:00
|
|
|
|
2009-06-26 16:30:46 +03:00
|
|
|
#endif
|