From b5ae276f78a1c1b1e6410ec4ff79cb68f0b35478 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 10 Jun 2018 14:41:33 +0200
Subject: Handled padding in Dalvik fill data payloads.

---
 plugins/dalvik/context.c     | 56 ++++++++++++++++++++++++++++++++++++++++++++
 plugins/dalvik/context.h     |  3 +++
 plugins/dalvik/pseudo/fill.c | 19 +++++++++++++++
 3 files changed, 78 insertions(+)

diff --git a/plugins/dalvik/context.c b/plugins/dalvik/context.c
index d3f7c36..8eb600d 100644
--- a/plugins/dalvik/context.c
+++ b/plugins/dalvik/context.c
@@ -50,6 +50,8 @@ typedef struct _raw_data_area
 
     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) */
@@ -289,6 +291,7 @@ bool g_dalvik_context_register_switch_data(GDalvikContext *ctx, const vmpa2t *st
     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);
@@ -339,6 +342,56 @@ bool g_dalvik_context_register_array_data(GDalvikContext *ctx, const vmpa2t *sta
     if (result)
     {
         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);
@@ -408,6 +461,9 @@ GArchInstruction *g_dalvik_context_get_raw_data(GDalvikContext *ctx, const GBinC
 
         }
 
+        if (result != NULL && found->padding)
+            g_raw_instruction_mark_as_padding(G_RAW_INSTRUCTION(result), true);
+
         g_object_unref(G_OBJECT(restricted));
 
     }
diff --git a/plugins/dalvik/context.h b/plugins/dalvik/context.h
index 6dc50b3..85924c2 100644
--- a/plugins/dalvik/context.h
+++ b/plugins/dalvik/context.h
@@ -66,6 +66,9 @@ bool g_dalvik_context_register_switch_data(GDalvikContext *ctx, const vmpa2t *st
 /* Mémorise une zone comme étant des données d'un tableau. */
 bool g_dalvik_context_register_array_data(GDalvikContext *, const vmpa2t *, uint16_t, phys_t);
 
+/* Mémorise une zone comme étant un bourrage de fin de tableau. */
+bool g_dalvik_context_register_array_data_padding(GDalvikContext *, const vmpa2t *);
+
 /* Place une donnée en tant qu'instruction si besoin est. */
 GArchInstruction *g_dalvik_context_get_raw_data(GDalvikContext *, const GBinContent *, vmpa2t *);
 
diff --git a/plugins/dalvik/pseudo/fill.c b/plugins/dalvik/pseudo/fill.c
index 9820348..92178d9 100644
--- a/plugins/dalvik/pseudo/fill.c
+++ b/plugins/dalvik/pseudo/fill.c
@@ -177,6 +177,7 @@ GArchInstruction *g_dalvik_fill_instr_new(uint16_t ident, GDalvikContext *ctx, c
 {
     GDalvikFillInstr *result;               /* Structure à retourner       */
     phys_t consumed;                        /* Données consommées          */
+    vmpa2t padding;                         /* Emplacement d'un bourrage   */
 
     assert(ident == DPO_FILL_ARRAY_DATA);
 
@@ -195,6 +196,24 @@ GArchInstruction *g_dalvik_fill_instr_new(uint16_t ident, GDalvikContext *ctx, c
     if (!g_dalvik_context_register_array_data(ctx, pos, result->item_width, consumed))
         goto gdfin_bad;
 
+    if (consumed % sizeof(uint16_t) != 0)
+    {
+        copy_vmpa(&padding, pos);
+        advance_vmpa(&padding, consumed);
+
+        if (!g_dalvik_context_register_array_data_padding(ctx, &padding))
+        {
+            /**
+             * Si on a pu prendre le premier verrou, on devrait avoir l'assurance
+             * de prendre le second !
+             */
+            assert(false);
+        }
+
+
+
+    }
+
     return G_ARCH_INSTRUCTION(result);
 
  gdfin_bad:
-- 
cgit v0.11.2-87-g4458