Skip to content

Commit

Permalink
WIP: Early catch some missbehaves in the class parsing and speedup ma…
Browse files Browse the repository at this point in the history
…cho parsing ##bin
  • Loading branch information
radare committed Jun 22, 2024
1 parent be299a6 commit f136254
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libr/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,14 @@ static void filter_classes(RBinFile *bf, RList *list) {
r_list_foreach (list, iter, cls) {
const char *kname = r_bin_name_tostring (cls->name);
char *fname = r_bin_filter_name (bf, db, cls->index, kname);
if (fname) {
if (R_STR_ISEMPTY (fname)) {
R_LOG_WARN ("Corrupted class storage");
break;
#if 0
R_LOG_DEBUG ("Invalid class, must be removed");
continue;
#endif
} else {
r_bin_name_update (cls->name, fname);
free (fname);
}
Expand Down Expand Up @@ -450,6 +457,7 @@ R_API int r_bin_object_set_items(RBinFile *bf, RBinObject *bo) {
filter_classes (bf, bo->classes);
}
// cache addr=class+method
#if 0
if (bo->classes) {
RList *klasses = bo->classes;
RListIter *iter, *iter2;
Expand All @@ -465,6 +473,7 @@ R_API int r_bin_object_set_items(RBinFile *bf, RBinObject *bo) {
}
}
}
#endif
}
if (p->lines) {
bo->lines = p->lines (bf);
Expand Down
14 changes: 14 additions & 0 deletions libr/bin/format/objc/mach0_classes.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,16 @@ static void get_method_list(RBinFile *bf, RBinClass *klass, const char *class_na
name = malloc (name_len + 1);
len = r_buf_read_at (bf->buf, r, (ut8 *)name, name_len);
name[name_len] = 0;
eprintf ("%d %d\n", name_len, strlen (name));
if (len < 1) {
goto error;
}
}
if (class_name) { // XXX to save memory we can just ref the RBinName instance from the class
method->classname = strdup (class_name);
} else {
R_LOG_ERROR ("Invalid class name for method. Avoid parsing invalid data");
goto error;
}
method->name = r_bin_name_new (name);
R_FREE (name);
Expand Down Expand Up @@ -1293,6 +1297,8 @@ static void get_class_ro_t(RBinFile *bf, bool *is_meta_class, RBinClass *klass,
return;
}
if (bin->has_crypto) {
R_LOG_ERROR ("Not parsing encrypted data");
return;
const char kn[] = "some_encrypted_data";
klass->name = r_bin_name_new (kn);
// klass->name = strdup ("some_encrypted_data");
Expand Down Expand Up @@ -1859,6 +1865,10 @@ RList *MACH0_(parse_classes)(RBinFile *bf, objc_cache_opt_info *oi) {
free (klass_name);
num_of_unnamed_class++;
}
if (strlen (klass->name) > 512) {
eprintf ("Invalid class name, probably corrupted binary\n");
break;
}
r_list_append (ret, klass);
}
metadata_sections_fini (&ms);
Expand Down Expand Up @@ -2049,6 +2059,10 @@ void MACH0_(get_category_t)(RBinFile *bf, RBinClass *klass, mach0_ut p, const RS
R_FREE (category_name);

const char *klass_name = r_bin_name_tostring (klass->name);
if (R_STR_ISEMPTY (klass_name)) {
R_LOG_ERROR ("Invalid class name");
return;
}
if (c.instanceMethods > 0) {
get_method_list (bf, klass, klass_name, false, oi, c.instanceMethods);
}
Expand Down

0 comments on commit f136254

Please sign in to comment.