summaryrefslogtreecommitdiff
path: root/src/core/formats.c
blob: bf58c2e05779813ed645b46db93dad295c3848fe (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * formats.c - enregistrement et fourniture des formats de binaires supportés
 *
 * Copyright (C) 2015-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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "formats.h"


#include <malloc.h>
#include <pthread.h>
#include <string.h>


#include "../format/dex/dex.h"
#include "../format/dwarf/dwarf.h"
#include "../format/dwarf/v2/dwarf.h"
#include "../format/dwarf/v3/dwarf.h"
#include "../format/dwarf/v4/dwarf.h"
#include "../format/elf/elf.h"



/* Eléments pour détection */
typedef struct _format_matcher_t
{
    format_match_fc func;                   /* Recherche de correspondance */
    void *data;                             /* Eventuelle donnée à y lier  */

} format_matcher_t;


/* Caractéristiques d'un processeur */
typedef struct _format_loader_t
{
    char *key;                              /* Clef pour un accès rapide   */
    char *name;                             /* Désignation humaine         */

    format_load_fc func;                    /* Procédure de chargement     */

} format_loader_t;


/* Mémorisation des détections de format enregistrées */
static format_matcher_t *_formats_matchers = NULL;
static size_t _formats_matchers_count = 0;

/* Mémorisation des types de formats enregistrés */
static format_loader_t *_formats_loaders = NULL;
static size_t _formats_loaders_count = 0;

/* Verrou pour des accès atomiques */
static GRWLock _formats_lock;


/* Retrouve l'enregistrement correspondant à une architecture. */
static format_loader_t *find_format_by_key(const char *);



/******************************************************************************
*                                                                             *
*  Paramètres  : func = procédure de détection à utiliser.                    *
*                data = éventuelle donnée à associer aux opérations.          *
*                                                                             *
*  Description : Enregistre un détection de format(s) binaire(s).             *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool register_format_matcher(format_match_fc func, void *data)
{
    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours          */
    const format_matcher_t *cur;            /* Elément parcouru et analysé */
    format_matcher_t *new;                  /* Nouvel élément à définir    */

    g_rw_lock_writer_lock(&_formats_lock);

    for (i = 0; i < _formats_matchers_count; i++)
    {
        cur = &_formats_matchers[i];

        if (cur->func == func && cur->data == data)
            break;

    }

    result = (i == _formats_matchers_count);

    if (result)
    {
        _formats_matchers = (format_matcher_t *)realloc(_formats_matchers,
                                                        ++_formats_matchers_count * sizeof(format_matcher_t));

        new = &_formats_matchers[_formats_matchers_count - 1];

        new->func = func;
        new->data = data;

    }

    g_rw_lock_writer_unlock(&_formats_lock);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : key  = désignation rapide et interne d'un format.            *
*                name = désignation humaine au format de binaire.             *
*                func = procédure de chargement associée.                     *
*                                                                             *
*  Description : Enregistre un format de contenu binaire donné.               *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool register_format_loader(const char *key, const char *name, format_load_fc func)
{
    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours          */
    format_loader_t *new;                   /* Nouvel élément à définir    */

    g_rw_lock_writer_lock(&_formats_lock);

    for (i = 0; i < _formats_loaders_count; i++)
        if (strcmp(_formats_loaders[i].key, key) == 0 && strcmp(_formats_loaders[i].name, name) == 0)
            break;

    result = (i == _formats_loaders_count);

    if (result)
    {
        _formats_loaders = (format_loader_t *)realloc(_formats_loaders,
                                                        ++_formats_loaders_count * sizeof(format_loader_t));

        new = &_formats_loaders[_formats_loaders_count - 1];

        new->key = strdup(key);
        new->name = strdup(name);

        new->func = func;

    }

    g_rw_lock_writer_unlock(&_formats_lock);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Charge les définitions de formats "natifs".                  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool load_hard_coded_formats_definitions(void)
{
    bool result;                            /* Bilan à retourner           */

    result = true;

    /* Détections */

    result &= register_format_matcher(dex_is_matching, NULL);

    result &= register_format_matcher(dwarf_is_matching, NULL);

    result &= register_format_matcher(elf_is_matching, NULL);

    /* Chargements */

    result &= register_format_loader("dex", "Dalvik Executable format", g_dex_format_new);

    result &= register_format_loader("dwarf_v2", "Debugging With Arbitrary Record Formats (v2)",
                                     g_dwarfv2_format_new);

    result &= register_format_loader("dwarf_v3", "Debugging With Arbitrary Record Formats (v3)",
                                     g_dwarfv3_format_new);

    result &= register_format_loader("dwarf_v4", "Debugging With Arbitrary Record Formats (v4)",
                                     g_dwarfv4_format_new);

    result &= register_format_loader("elf", "Executable and Linkable Format", g_elf_format_new);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Décharge toutes les définitions de formats.                  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void unload_formats_definitions(void)
{
    size_t i;                               /* Boucle de parcours          */

    if (_formats_matchers != NULL)
        free(_formats_matchers);

    _formats_matchers = NULL;
    _formats_matchers_count = 0;

    for (i = 0; i < _formats_loaders_count; i++)
    {
        free(_formats_loaders[i].key);
        free(_formats_loaders[i].name);
    }

    if (_formats_loaders != NULL)
        free(_formats_loaders);

    _formats_loaders = NULL;
    _formats_loaders_count = 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : key = nom technique du processeur recherché.                 *
*                                                                             *
*  Description : Retrouve l'enregistrement correspondant à une architecture.  *
*                                                                             *
*  Retour      : Définition trouvée ou NULL en cas d'échec.                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static format_loader_t *find_format_by_key(const char *key)
{
    format_loader_t *result;                /* Trouvaille à retourner      */
    size_t i;                               /* Boucle de parcours          */

    /**
     * Le verrou d'accès global doit être posé !
     */

    result = NULL;

    if (key != NULL)
        for (i = 0; i < _formats_loaders_count; i++)
            if (strcmp(_formats_loaders[i].key, key) == 0)
                result = &_formats_loaders[i];

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : key = nom technique du format recherché.                     *
*                                                                             *
*  Description : Fournit le nom humain du format binaire visé.                *
*                                                                             *
*  Retour      : Désignation humaine trouvée ou NULL.                         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *get_binary_format_name(const char *key)
{
    const char *result;                     /* Description à retourner     */
    format_loader_t *def;                   /* Définition d'architecture   */

    g_rw_lock_reader_lock(&_formats_lock);

    def = find_format_by_key(key);

    if (def == NULL)
        result = NULL;
    else
        result = def->name;

    g_rw_lock_reader_unlock(&_formats_lock);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : content = contenu binaire à parcourir.                       *
*                parent  = éventuel format exécutable déjà chargé.            *
*                key     = identifiant de format trouvé ou NULL. [OUT]        *
*                                                                             *
*  Description : Identifie un format binaire par son contenu.                 *
*                                                                             *
*  Retour      : Conclusion de haut niveau sur la reconnaissance effectuée.   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

FormatMatchStatus find_matching_format(GBinContent *content, GExeFormat *parent, char **key)
{
    FormatMatchStatus result;               /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours          */
    const format_matcher_t *cur;            /* Elément parcouru et analysé */

    result = FMS_UNKNOWN;

    g_rw_lock_reader_lock(&_formats_lock);

    for (i = 0; i < _formats_matchers_count && result == FMS_UNKNOWN; i++)
    {
        cur = &_formats_matchers[i];

        result = cur->func(content, parent, cur->data, key);

    }

    g_rw_lock_reader_unlock(&_formats_lock);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : key     = nom technique du processeur recherché.             *
*                content = contenu binaire pré-chargé à traiter.              *
*                parent  = contenu binaire principal éventuel déjà chargé.    *
*                                                                             *
*  Description : Charge le format binaire correspondant à un type.            *
*                                                                             *
*  Retour      : Format binaire instancié.                                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBinFormat *load_new_named_format(const char *key, GBinContent *content, GExeFormat *parent)
{
    GBinFormat *result;                     /* Instance à retourner        */
    format_loader_t *def;                   /* Définition d'architecture   */

    g_rw_lock_reader_lock(&_formats_lock);

    def = find_format_by_key(key);

    if (def == NULL)
        result = NULL;
    else
    {
        extern GtkStatusStack *get_global_status(void);

        result = def->func(content, parent, get_global_status());

    }

    g_rw_lock_reader_unlock(&_formats_lock);

    return result;

}