summaryrefslogtreecommitdiff
path: root/plugins/dalvik/context.c
blob: 98d9d6cbcab1961412f786d6e961a18d57f7d109 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

/* Chrysalide - Outil d'analyse de fichiers binaires
 * context.c - contexte lié à l'exécution d'un processeur
 *
 * Copyright (C) 2011-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "context.h"


#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>


#include <analysis/contents/restricted.h>
#include <arch/context-int.h>
#include <arch/raw.h>
#include <common/sort.h>
#include <plugins/dex/dex-int.h>


#include "operands/register.h"



/* ------------------------ MANIPULATION GLOBALE DU CONTEXTE ------------------------ */


/* Mémorisation de données brutes dans le code */
typedef struct _raw_data_area
{
    mrange_t range;                         /* Couverture à laisser en 1er */

    phys_t item_len;                        /* Taille de chaque élément    */

    bool padding;                           /* Constitution d'un bourrage ?*/

} raw_data_area;

/* Définition d'un contexte pour processeur Dalkvik (instance) */
struct _GDalvikContext
{
    GProcContext parent;                    /* A laisser en premier        */

    raw_data_area *data;                    /* Liste de zones brutes       */
    size_t count;                           /* Taille de cette liste       */
    GMutex mutex;                           /* Accès à la liste            */

};


/* Définition d'un contexte pour processeur Dalkvik (classe) */
struct _GDalvikContextClass
{
    GProcContextClass parent;               /* A laisser en premier        */

};


/* Initialise la classe des contextes de processeur Dalkvik. */
static void g_dalvik_context_class_init(GDalvikContextClass *);

/* Initialise une instance de contexte de processeur Dalkvik. */
static void g_dalvik_context_init(GDalvikContext *);

/* Supprime toutes les références externes. */
static void g_dalvik_context_dispose(GDalvikContext *);

/* Procède à la libération totale de la mémoire. */
static void g_dalvik_context_finalize(GDalvikContext *);



/* ---------------------------------------------------------------------------------- */
/*                          MANIPULATION GLOBALE DU CONTEXTE                          */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini par la GLib pour le contexte de processeur Dalkvik. */
G_DEFINE_TYPE(GDalvikContext, g_dalvik_context, G_TYPE_PROC_CONTEXT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des contextes de processeur Dalkvik.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_context_class_init(GDalvikContextClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_dalvik_context_dispose;
    object->finalize = (GObjectFinalizeFunc)g_dalvik_context_finalize;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ctx = instance à initialiser.                                *
*                                                                             *
*  Description : Initialise une instance de contexte de processeur Dalkvik.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_context_init(GDalvikContext *ctx)
{
    g_mutex_init(&ctx->mutex);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ctx = instance d'objet GLib à traiter.                       *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_context_dispose(GDalvikContext *ctx)
{
    g_mutex_clear(&ctx->mutex);

    G_OBJECT_CLASS(g_dalvik_context_parent_class)->dispose(G_OBJECT(ctx));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ctx = instance d'objet GLib à traiter.                       *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_dalvik_context_finalize(GDalvikContext *ctx)
{
    if (ctx->data != NULL)
        free(ctx->data);

    G_OBJECT_CLASS(g_dalvik_context_parent_class)->finalize(G_OBJECT(ctx));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée un contexte pour l'exécution du processeur Dalvik.      *
*                                                                             *
*  Retour      : Contexte mis en place.                                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDalvikContext *g_dalvik_context_new(void)
{
    GDalvikContext *result;                 /* Structure à retourner       */

    result = g_object_new(G_TYPE_DALVIK_CONTEXT, NULL);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ctx    = contexte de désassemblage Dalvik à actualiser.      *
*                start  = début de la zone à considérer.                      *
*                length = taille de la zone couverte.                         *
*                                                                             *
*  Description : Mémorise une zone comme étant des données de branchements.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_dalvik_context_register_switch_data(GDalvikContext *ctx, const vmpa2t *start, phys_t length)
{
    bool result;                            /* Bilan à retourner           */
    raw_data_area new;                      /* Nouvel élément à insérer    */
    size_t i;                               /* Boucle de parcours          */

    result = true;

    g_mutex_lock(&ctx->mutex);

    /* Vérification quant aux chevauchements */

    init_mrange(&new.range, start, length);

    for (i = 0; i < ctx->count && result; i++)
        result = !mrange_intersects_mrange(&ctx->data[i].range, &new.range);

    /* Insertion d'une nouvelle zone */

    if (result)
    {
        new.item_len = 4;
        new.padding = false;

        ctx->data = qinsert(ctx->data, &ctx->count, sizeof(raw_data_area),
                            (__compar_fn_t)cmp_mrange_with_vmpa_swapped, &new);

    }

    g_mutex_unlock(&ctx->mutex);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ctx    = contexte de désassemblage Dalvik à actualiser.      *
*                start  = début de la zone à considérer.                      *
*                width  = taille de chacun des éléments.                      *
*                length = taille de la zone couverte.                         *
*                                                                             *
*  Description : Mémorise une zone comme étant des données d'un tableau.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_dalvik_context_register_array_data(GDalvikContext *ctx, const vmpa2t *start, uint16_t width, phys_t length)
{
    bool result;                            /* Bilan à retourner           */
    raw_data_area new;                      /* Nouvel élément à insérer    */
    size_t i;                               /* Boucle de parcours          */

    result = true;

    g_mutex_lock(&ctx->mutex);

    /* Vérification quant aux chevauchements */

    init_mrange(&new.range, start, length);

    for (i = 0; i < ctx->count && result; i++)
        result = !mrange_intersects_mrange(&ctx->data[i].range, &new.range);

    /* Insertion d'une nouvelle zone */

    if (result)
    {
        assert(length % width == 0);

        new.item_len = width;
        new.padding = false;

        ctx->data = qinsert(ctx->data, &ctx->count, sizeof(raw_data_area),
                            (__compar_fn_t)cmp_mrange_with_vmpa_swapped, &new);

    }

    g_mutex_unlock(&ctx->mutex);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ctx    = contexte de désassemblage Dalvik à actualiser.      *
*                start  = début de la zone à considérer.                      *
*                                                                             *
*  Description : Mémorise une zone comme étant un bourrage de fin de tableau. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_dalvik_context_register_array_data_padding(GDalvikContext *ctx, const vmpa2t *start)
{
    bool result;                            /* Bilan à retourner           */
    raw_data_area new;                      /* Nouvel élément à insérer    */
    size_t i;                               /* Boucle de parcours          */

    result = true;

    g_mutex_lock(&ctx->mutex);

    /* Vérification quant aux chevauchements */

    init_mrange(&new.range, start, sizeof(uint8_t));

    for (i = 0; i < ctx->count && result; i++)
        result = !mrange_intersects_mrange(&ctx->data[i].range, &new.range);

    /* Insertion d'une nouvelle zone */

    if (result)
    {
        new.item_len = sizeof(uint8_t);
        new.padding = true;

        ctx->data = qinsert(ctx->data, &ctx->count, sizeof(raw_data_area),
                            (__compar_fn_t)cmp_mrange_with_vmpa_swapped, &new);

    }

    g_mutex_unlock(&ctx->mutex);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ctx     = contexte de désassemblage Dalvik à consulter.      *
*                content = flux de données à analyser.                        *
*                pos     = position courante dans ce flux. [OUT]              *
*                                                                             *
*  Description : Place une donnée en tant qu'instruction si besoin est.       *
*                                                                             *
*  Retour      : Instruction mise en place ou NULL en cas d'échec.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArchInstruction *g_dalvik_context_get_raw_data(GDalvikContext *ctx, const GBinContent *content, vmpa2t *pos)
{
    GArchInstruction *result;               /* Instruction à retourner     */
    raw_data_area *found;                   /* Zone de couverture trouvée  */
    GBinContent *restricted;                /* Zone de lecture effective   */
    phys_t length;                          /* Zone de couverture          */
    size_t count;                           /* Nombre d'éléments           */

    result = NULL;

    g_mutex_lock(&ctx->mutex);

    found = bsearch(pos, ctx->data, ctx->count, sizeof(raw_data_area),
                    (__compar_fn_t)cmp_mrange_with_vmpa_swapped);

    if (found)
    {
        restricted = g_restricted_content_new_ro(content, &found->range);

        length = get_mrange_length(&found->range);
        count = length / found->item_len;

        switch (found->item_len)
        {
            case 1:
                result = g_raw_instruction_new_array(restricted, MDS_8_BITS_UNSIGNED, count, pos, SRE_LITTLE);
                break;

            case 2:
                result = g_raw_instruction_new_array(restricted, MDS_16_BITS_UNSIGNED, count, pos, SRE_LITTLE);
                break;

            case 4:
                result = g_raw_instruction_new_array(restricted, MDS_32_BITS_UNSIGNED, count, pos, SRE_LITTLE);
                break;

            case 8:
                result = g_raw_instruction_new_array(restricted, MDS_64_BITS_UNSIGNED, count, pos, SRE_LITTLE);
                break;

            default:
                result = g_raw_instruction_new_array(restricted, MDS_8_BITS_UNSIGNED,
                                                     length, pos, SRE_LITTLE);
                break;

        }

        if (result != NULL && found->padding)
            g_raw_instruction_mark_as_padding(G_RAW_INSTRUCTION(result), true);

        g_object_unref(G_OBJECT(restricted));

    }

    g_mutex_unlock(&ctx->mutex);

    return result;

}