~ubuntu-branches/ubuntu/jaunty/clamav/jaunty-backports

« back to all changes in this revision

Viewing changes to libclamav/bytecode_api.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-10-02 17:11:00 UTC
  • mfrom: (0.3.1 lucid-proposed)
  • Revision ID: james.westby@ubuntu.com-20101002171100-0erjjoucua6kw2pc
Tags: 0.96.3+dfsg-2ubuntu0.10.04.1~jaunty1
* Source backport for Jaunty
  - Change build-dep on libtdl-dev to libtdl7-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#include "bytecode_api_impl.h"
43
43
#include "others.h"
44
44
#include "pe.h"
 
45
#include "pdf.h"
45
46
#include "disasm.h"
46
47
#include "scanners.h"
47
48
#include "jsparse/js-norm.h"
459
460
            cctx->container_type = ctx->containertype;
460
461
        res = cli_magic_scandesc(ctx->outfd, cctx);
461
462
        cctx->container_type = current;
462
 
        if (res == CL_VIRUS)
 
463
        if (res == CL_VIRUS) {
 
464
            if (cctx->virname)
 
465
                ctx->virname = *cctx->virname;
463
466
            ctx->found = 1;
 
467
        }
464
468
    }
465
469
    if ((cctx && cctx->engine->keeptmp) ||
466
470
        (ftruncate(ctx->outfd, 0) == -1)) {
989
993
    const uint8_t *s;
990
994
    if (!h || !n || hs < 0 || ns < 0)
991
995
        return -1;
992
 
    s = cli_memstr(h, hs, n, ns);
 
996
    s = (const uint8_t*) cli_memstr((const char*)h, hs, (const char*)n, ns);
993
997
    if (!s)
994
998
        return -1;
995
999
    return s - h;
997
1001
 
998
1002
int32_t cli_bcapi_hex2ui(struct cli_bc_ctx *ctx, uint32_t ah, uint32_t bh)
999
1003
{
1000
 
    uint8_t result = 0;
 
1004
    char result = 0;
1001
1005
    unsigned char in[2];
1002
1006
    in[0] = ah;
1003
1007
    in[1] = bh;
1004
1008
 
1005
 
    if (cli_hex2str_to(in, &result, 2) == -1)
 
1009
    if (cli_hex2str_to((const char*)in, &result, 2) == -1)
1006
1010
        return -1;
1007
1011
    return result;
1008
1012
}
1215
1219
    cli_dbgmsg("bytecode api: input switched to extracted file\n");
1216
1220
    return 0;
1217
1221
}
 
1222
 
 
1223
uint32_t cli_bcapi_get_environment(struct cli_bc_ctx *ctx , struct cli_environment* env, uint32_t len)
 
1224
{
 
1225
    if (len > sizeof(*env)) {
 
1226
        cli_dbgmsg("cli_bcapi_get_environment len %u > %lu\n", len, sizeof(*env));
 
1227
        return -1;
 
1228
    }
 
1229
    memcpy(env, ctx->env, len);
 
1230
    return 0;
 
1231
}
 
1232
 
 
1233
uint32_t cli_bcapi_disable_bytecode_if(struct cli_bc_ctx *ctx , const int8_t* reason, uint32_t len, uint32_t cond)
 
1234
{
 
1235
    if (ctx->bc->kind != BC_STARTUP) {
 
1236
        cli_dbgmsg("Bytecode must be BC_STARTUP to call disable_bytecode_if\n");
 
1237
        return -1;
 
1238
    }
 
1239
    if (!cond)
 
1240
        return ctx->bytecode_disable_status;
 
1241
    if (*reason == '^')
 
1242
        cli_warnmsg("Bytecode: disabling completely because %s\n", reason+1);
 
1243
    else
 
1244
        cli_dbgmsg("Bytecode: disabling completely because %s\n", reason);
 
1245
    ctx->bytecode_disable_status = 2;
 
1246
    return ctx->bytecode_disable_status;
 
1247
}
 
1248
 
 
1249
uint32_t cli_bcapi_disable_jit_if(struct cli_bc_ctx *ctx , const int8_t* reason, uint32_t len, uint32_t cond)
 
1250
{
 
1251
    if (ctx->bc->kind != BC_STARTUP) {
 
1252
        cli_dbgmsg("Bytecode must be BC_STARTUP to call disable_jit_if\n");
 
1253
        return -1;
 
1254
    }
 
1255
    if (!cond)
 
1256
        return ctx->bytecode_disable_status;
 
1257
    if (*reason == '^')
 
1258
        cli_warnmsg("Bytecode: disabling JIT because %s\n", reason+1);
 
1259
    else
 
1260
        cli_dbgmsg("Bytecode: disabling JIT because %s\n", reason);
 
1261
    if (ctx->bytecode_disable_status != 2) /* no reenabling */
 
1262
        ctx->bytecode_disable_status = 1;
 
1263
    return ctx->bytecode_disable_status;
 
1264
}
 
1265
 
 
1266
int32_t cli_bcapi_version_compare(struct cli_bc_ctx *ctx , const uint8_t* lhs, uint32_t lhs_len, 
 
1267
                                  const uint8_t* rhs, uint32_t rhs_len)
 
1268
{
 
1269
    unsigned i = 0, j = 0;
 
1270
    unsigned long li=0, ri=0;
 
1271
    do {
 
1272
        while (i < lhs_len && j < rhs_len && lhs[i] == rhs[j] &&
 
1273
               !isdigit(lhs[i]) && !isdigit(rhs[j])) {
 
1274
            i++; j++;
 
1275
        }
 
1276
        if (i == lhs_len && j == rhs_len)
 
1277
            return 0;
 
1278
        if (i == lhs_len)
 
1279
            return -1;
 
1280
        if (j == rhs_len)
 
1281
            return 1;
 
1282
        if (!isdigit(lhs[i]) || !isdigit(rhs[j]))
 
1283
            return lhs[i] < rhs[j] ? -1 : 1;
 
1284
        while (isdigit(lhs[i]) && i < lhs_len)
 
1285
            li = 10*li + (lhs[i] - '0');
 
1286
        while (isdigit(rhs[j]) && j < rhs_len)
 
1287
            ri = 10*ri + (rhs[j] - '0');
 
1288
        if (li < ri)
 
1289
            return -1;
 
1290
        if (li > ri)
 
1291
            return 1;
 
1292
    } while (1);
 
1293
}
 
1294
 
 
1295
static int check_bits(uint32_t query, uint32_t value, uint8_t shift, uint8_t mask)
 
1296
{
 
1297
    uint8_t q = (query >> shift)&mask;
 
1298
    uint8_t v = (value >> shift)&mask;
 
1299
    /* q == mask -> ANY */
 
1300
    if (q == v || q == mask)
 
1301
        return 1;
 
1302
    return 0;
 
1303
}
 
1304
 
 
1305
uint32_t cli_bcapi_check_platform(struct cli_bc_ctx *ctx , uint32_t a, uint32_t b , uint32_t c)
 
1306
{
 
1307
    unsigned ret =
 
1308
        check_bits(a, ctx->env->platform_id_a, 24, 0xff) &&
 
1309
        check_bits(a, ctx->env->platform_id_a, 20, 0xf) &&
 
1310
        check_bits(a, ctx->env->platform_id_a, 16, 0xf) &&
 
1311
        check_bits(a, ctx->env->platform_id_a, 8, 0xff) &&
 
1312
        check_bits(a, ctx->env->platform_id_a, 0, 0xff) &&
 
1313
        check_bits(b, ctx->env->platform_id_b, 28, 0xf) &&
 
1314
        check_bits(b, ctx->env->platform_id_b, 24, 0xf) &&
 
1315
        check_bits(b, ctx->env->platform_id_b, 16, 0xff) &&
 
1316
        check_bits(b, ctx->env->platform_id_b, 8, 0xff) &&
 
1317
        check_bits(b, ctx->env->platform_id_b, 0, 0xff) &&
 
1318
        check_bits(c, ctx->env->platform_id_c, 24, 0xff) &&
 
1319
        check_bits(c, ctx->env->platform_id_c, 16, 0xff) &&
 
1320
        check_bits(c, ctx->env->platform_id_c, 8, 0xff) &&
 
1321
        check_bits(c, ctx->env->platform_id_c, 0, 0xff);
 
1322
    if (ret) {
 
1323
        cli_dbgmsg("check_platform(0x%08x,0x%08x,0x%08x) = match\n",a,b,c);
 
1324
    }
 
1325
    return ret;
 
1326
}
 
1327
 
 
1328
int cli_bytecode_context_setpdf(struct cli_bc_ctx *ctx, unsigned phase,
 
1329
                                unsigned nobjs,
 
1330
                                struct pdf_obj *objs, uint32_t *pdf_flags,
 
1331
                                uint32_t pdfsize, uint32_t pdfstartoff)
 
1332
{
 
1333
    ctx->pdf_nobjs = nobjs;
 
1334
    ctx->pdf_objs = objs;
 
1335
    ctx->pdf_flags = pdf_flags;
 
1336
    ctx->pdf_size = pdfsize;
 
1337
    ctx->pdf_startoff = pdfstartoff;
 
1338
    ctx->pdf_phase = phase;
 
1339
    return 0;
 
1340
}
 
1341
 
 
1342
int32_t cli_bcapi_pdf_get_obj_num(struct cli_bc_ctx *ctx)
 
1343
{
 
1344
    if (!ctx->pdf_phase)
 
1345
        return -1;
 
1346
    return ctx->pdf_nobjs;
 
1347
}
 
1348
 
 
1349
int32_t cli_bcapi_pdf_get_flags(struct cli_bc_ctx *ctx)
 
1350
{
 
1351
    if (!ctx->pdf_phase)
 
1352
        return -1;
 
1353
    return *ctx->pdf_flags;
 
1354
}
 
1355
 
 
1356
int32_t cli_bcapi_pdf_set_flags(struct cli_bc_ctx *ctx , int32_t flags)
 
1357
{
 
1358
    if (!ctx->pdf_phase)
 
1359
        return -1;
 
1360
    cli_dbgmsg("cli_pdf: bytecode set_flags %08x -> %08x\n",
 
1361
               *ctx->pdf_flags,
 
1362
               flags);
 
1363
    *ctx->pdf_flags = flags;
 
1364
    return 0;
 
1365
}
 
1366
 
 
1367
int32_t cli_bcapi_pdf_lookupobj(struct cli_bc_ctx *ctx , uint32_t objid)
 
1368
{
 
1369
    unsigned i;
 
1370
    if (!ctx->pdf_phase)
 
1371
        return -1;
 
1372
    for (i=0;i<ctx->pdf_nobjs;i++) {
 
1373
        if (ctx->pdf_objs[i].id == objid)
 
1374
            return i;
 
1375
    }
 
1376
    return -1;
 
1377
}
 
1378
 
 
1379
uint32_t cli_bcapi_pdf_getobjsize(struct cli_bc_ctx *ctx , int32_t objidx)
 
1380
{
 
1381
    if (!ctx->pdf_phase ||
 
1382
        objidx >= ctx->pdf_nobjs ||
 
1383
        ctx->pdf_phase == PDF_PHASE_POSTDUMP /* map is obj itself, no access to pdf anymore */
 
1384
       )
 
1385
        return 0;
 
1386
    if (objidx + 1 == ctx->pdf_nobjs)
 
1387
        return ctx->pdf_size - ctx->pdf_objs[objidx].start;
 
1388
    return ctx->pdf_objs[objidx+1].start - ctx->pdf_objs[objidx].start - 4;
 
1389
}
 
1390
 
 
1391
uint8_t* cli_bcapi_pdf_getobj(struct cli_bc_ctx *ctx , int32_t objidx, uint32_t amount)
 
1392
{
 
1393
    uint32_t size = cli_bcapi_pdf_getobjsize(ctx, objidx);
 
1394
    if (amount > size)
 
1395
        return NULL;
 
1396
    return fmap_need_off(ctx->fmap, ctx->pdf_objs[objidx].start, amount);
 
1397
}
 
1398
 
 
1399
int32_t cli_bcapi_pdf_getobjid(struct cli_bc_ctx *ctx , int32_t objidx)
 
1400
{
 
1401
    if (!ctx->pdf_phase ||
 
1402
        objidx >= ctx->pdf_nobjs)
 
1403
        return -1;
 
1404
    return ctx->pdf_objs[objidx].id;
 
1405
}
 
1406
 
 
1407
int32_t cli_bcapi_pdf_getobjflags(struct cli_bc_ctx *ctx , int32_t objidx)
 
1408
{
 
1409
    if (!ctx->pdf_phase ||
 
1410
        objidx >= ctx->pdf_nobjs)
 
1411
        return -1;
 
1412
    return ctx->pdf_objs[objidx].flags;
 
1413
}
 
1414
 
 
1415
int32_t cli_bcapi_pdf_setobjflags(struct cli_bc_ctx *ctx , int32_t objidx, int32_t flags)
 
1416
{
 
1417
    if (!ctx->pdf_phase ||
 
1418
        objidx >= ctx->pdf_nobjs)
 
1419
        return -1;
 
1420
    cli_dbgmsg("cli_pdf: bytecode setobjflags %08x -> %08x\n",
 
1421
               ctx->pdf_objs[objidx].flags,
 
1422
               flags);
 
1423
    ctx->pdf_objs[objidx].flags = flags;
 
1424
    return 0;
 
1425
}
 
1426
 
 
1427
int32_t cli_bcapi_pdf_get_offset(struct cli_bc_ctx *ctx , int32_t objidx)
 
1428
{
 
1429
    if (!ctx->pdf_phase ||
 
1430
        objidx >= ctx->pdf_nobjs)
 
1431
        return -1;
 
1432
    return ctx->pdf_startoff + ctx->pdf_objs[objidx].start;
 
1433
}
 
1434
 
 
1435
int32_t cli_bcapi_pdf_get_phase(struct cli_bc_ctx *ctx)
 
1436
{
 
1437
    return ctx->pdf_phase;
 
1438
}
 
1439
 
 
1440
int32_t cli_bcapi_pdf_get_dumpedobjid(struct cli_bc_ctx *ctx)
 
1441
{
 
1442
    if (ctx->pdf_phase != PDF_PHASE_POSTDUMP)
 
1443
        return -1;
 
1444
    return ctx->pdf_dumpedid;
 
1445
}
 
1446