Fix signature from mach0

This commit is contained in:
Álvaro Felipe Melchor 2016-09-25 03:31:26 +02:00
parent c32624b86b
commit 19a633ff77
5 changed files with 42 additions and 26 deletions

View File

@ -47,7 +47,6 @@ static int r_bin_fatmach0_init(struct r_bin_fatmach0_obj_t* bin) {
bin->archs[i].size = r_read_be32 (&archbytes[12]);
bin->archs[i].align = r_read_be32 (&archbytes[16]);
}
eprintf("fatmach init worked\n");
return true;
}

View File

@ -9,6 +9,8 @@ typedef struct _ulebr {
ut8 *p;
} ulebr;
static bool little_;
static ut64 read_uleb128(ulebr *r, ut8 *end) {
ut64 result = 0;
int bit = 0;
@ -531,7 +533,7 @@ static int parse_dysymtab(struct MACH0_(obj_t)* bin, ut64 off) {
}
static bool parse_signature(struct MACH0_(obj_t) *bin, ut64 off) {
int i, len;
int i,len;
ut32 data;
bin->signature = NULL;
struct linkedit_data_command link = {};
@ -557,34 +559,37 @@ static bool parse_signature(struct MACH0_(obj_t) *bin, ut64 off) {
bin->signature = (ut8 *)strdup ("Malformed entitlement");
return true;
}
super.blob.magic = r_read_ble32 (bin->b->buf + data, bin->big_endian);
super.blob.length = r_read_ble32 (bin->b->buf + data + 4, bin->big_endian);
super.count = r_read_ble32 (bin->b->buf + data + 8, bin->big_endian);
super.blob.magic = r_read_ble32 (bin->b->buf + data, little_);
super.blob.length = r_read_ble32 (bin->b->buf + data + 4, little_);
super.count = r_read_ble32 (bin->b->buf + data + 8, little_);
for (i = 0; i < super.count; ++i) {
if ((ut8 *)(bin->b->buf + data + i) > (ut8 *)(bin->b->buf + bin->size)) {
bin->signature = (ut8 *)strdup ("Malformed entitlement");
break;
}
struct blob_index_t *bi = (struct blob_index_t *)(bin->b->buf + data + 12);
idx.type = r_read_ble32 (&bi[i].type, bin->big_endian);
idx.offset = r_read_ble32 (&bi[i].offset, bin->big_endian);
idx.type = r_read_ble32 (&bi[i].type, little_);
idx.offset = r_read_ble32 (&bi[i].offset, little_);
if (idx.type == CSSLOT_ENTITLEMENTS) {
ut32 begin = idx.offset;
if (begin > bin->size || begin + sizeof (struct blob_t) > bin->size) {
if (idx.offset > bin->size || idx.offset + sizeof (struct blob_t) > bin->size) {
bin->signature = (ut8 *)strdup ("Malformed entitlement");
break;
}
len = r_read_ble32 (bin->b->buf + data + begin + 4, bin->big_endian) - sizeof (struct blob_t);
struct blob_t entitlements = {};
entitlements.magic = r_read_ble32 (bin->b->buf + data + idx.offset, little_);
entitlements.length = r_read_ble32 (bin->b->buf + data + idx.offset + 4, little_);
len = entitlements.length - sizeof(struct blob_t);
if (len <= bin->size && len > 1) {
bin->signature = calloc (1, len + 1);
if (bin->signature) {
memcpy (bin->signature, bin->b->buf + data + begin, len);
ut8 *src = bin->b->buf + data + idx.offset + sizeof (struct blob_t);
memcpy (bin->signature, src, len);
bin->signature[len] = '\0';
return true;
}
} else {
bin->signature = (ut8 *)strdup ("Malformed entitlement");
}
break;
}
}
if (!bin->signature) {
@ -1116,6 +1121,11 @@ static int init_items(struct MACH0_(obj_t)* bin) {
}
static int init(struct MACH0_(obj_t)* bin) {
union {
ut16 word;
ut8 byte[2];
} endian = { 1 };
little_ = endian.byte[0];
if (!init_hdr(bin)) {
eprintf ("Warning: File is not MACH0\n");
return false;
@ -1269,8 +1279,9 @@ static int parse_import_stub(struct MACH0_(obj_t)* bin, struct symbol_t *symbol,
int i, j, nsyms, stridx;
const char *symstr;
if (idx<0)
if (idx < 0) {
return 0;
}
symbol->offset = 0LL;
symbol->addr = 0LL;
symbol->name[0] = '\0';
@ -1546,24 +1557,27 @@ struct import_t* MACH0_(get_imports)(struct MACH0_(obj_t)* bin) {
if (!bin->symtab || !bin->symstr || !bin->sects || !bin->indirectsyms)
return NULL;
if (bin->dysymtab.nundefsym<1 || bin->dysymtab.nundefsym>0xfffff) {
if (bin->dysymtab.nundefsym < 1 || bin->dysymtab.nundefsym > 0xfffff) {
return NULL;
}
if (!(imports = malloc ((bin->dysymtab.nundefsym + 1) * sizeof(struct import_t))))
return NULL;
for (i = j = 0; i < bin->dysymtab.nundefsym; i++) {
idx = bin->dysymtab.iundefsym +i;
if (idx<0 || idx>=bin->nsymtab) {
if (idx < 0 || idx >= bin->nsymtab) {
eprintf ("WARNING: Imports index out of bounds. Ignoring relocs\n");
free (imports);
return NULL;
}
stridx = bin->symtab[idx].n_un.n_strx;
if (stridx >= 0 && stridx < bin->symstrlen)
if (stridx >= 0 && stridx < bin->symstrlen) {
symstr = (char *)bin->symstr + stridx;
else symstr = "";
if (!*symstr)
} else {
symstr = "";
}
if (!*symstr) {
continue;
}
{
int i = 0;
int len = 0;

View File

@ -14,16 +14,20 @@ static int check_bytes(const ut8 *buf, ut64 length);
static RBinInfo* info(RBinFile *arch);
static Sdb* get_sdb (RBinObject *o) {
if (!o) return NULL;
if (!o) {
return NULL;
}
struct MACH0_(obj_t) *bin = (struct MACH0_(obj_t) *) o->bin_obj;
if (bin && bin->kv) return bin->kv;
if (bin && bin->kv) {
return bin->kv;
}
return NULL;
}
static char *entitlements(RBinFile *arch) {
struct MACH0_(obj_t) *bin;
if (!arch || !arch->o) {
return NULL;
return NULL;
}
bin = arch->o->bin_obj;
return (char *)bin->signature;
@ -32,7 +36,9 @@ static char *entitlements(RBinFile *arch) {
static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
struct MACH0_(obj_t) *res = NULL;
RBuffer *tbuf = NULL;
if (!buf || sz == 0 || sz == UT64_MAX) return NULL;
if (!buf || !sz || sz == UT64_MAX) {
return NULL;
}
tbuf = r_buf_new();
r_buf_set_bytes (tbuf, buf, sz);
res = MACH0_(new_buf) (tbuf);

View File

@ -19,7 +19,6 @@ static int check(RBin *bin) {
RMmap *m = r_file_mmap (bin->file, false, 0);
if (!m || !m->buf) {
r_file_mmap_free (m);
eprintf("check failed\n");
return false;
}
h = m->buf;
@ -36,7 +35,6 @@ static int check(RBin *bin) {
}
}
r_file_mmap_free (m);
eprintf("check=%d\n", ret);
return ret;
}
@ -65,7 +63,6 @@ static int check_bytes(const ut8* bytes, ut64 sz) {
}
}
}
eprintf("checkbytes=%d\n", ret);
return ret;
}

View File

@ -2322,7 +2322,7 @@ static int bin_versioninfo(RCore *r, int mode) {
}
static int bin_signature(RCore *r, int mode) {
RBinFile *cur = r_bin_cur (r->bin);
RBinFile *cur = r_bin_cur (r->bin);
RBinPlugin *plg = r_bin_file_cur_plugin (cur);
if (plg && plg->signature) {
const char *signature = plg->signature (cur);