Skip to content

Commit

Permalink
Fix #19964 - show relro:no even if no dyn section is found ##bin
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jun 3, 2023
1 parent fecabf3 commit 6ed6b59
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 25 deletions.
1 change: 1 addition & 0 deletions doc/fortunes.fun
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,4 @@ Are you still watching?
Starting application, this might take some time...
Do you want to restart to install these updates now or try tonight?
Updates available
This binary has not been analyzed. Would you like to analyze it now?
39 changes: 21 additions & 18 deletions libr/bin/format/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,23 +517,6 @@ static Elf_(Phdr) *get_dynamic_segment(ELFOBJ *eo) {
return NULL;
}

static void init_dynamic_section_sdb(ELFOBJ *eo, Elf_(Addr) strtabaddr, size_t strsize) {
int r = Elf_(has_relro) (eo);
switch (r) {
case R_ELF_FULL_RELRO:
sdb_set (eo->kv, "elf.relro", "full", 0);
break;
case R_ELF_PART_RELRO:
sdb_set (eo->kv, "elf.relro", "partial", 0);
break;
default:
sdb_set (eo->kv, "elf.relro", "no", 0);
break;
}
sdb_num_set (eo->kv, "elf_strtab.offset", strtabaddr, 0);
sdb_num_set (eo->kv, "elf_strtab.size", strsize, 0);
}

static void set_default_value_dynamic_info(ELFOBJ *eo) {
eo->dyn_info.dt_pltrelsz = 0;
eo->dyn_info.dt_pltgot = R_BIN_ELF_ADDR_MAX;
Expand Down Expand Up @@ -724,23 +707,27 @@ static int init_dynamic_section(ELFOBJ *eo) {
if (!strtabaddr) {
R_LOG_DEBUG ("DT_STRTAB not found or invalid");
}
eprintf ("err\n");
return false;
}

char *strtab = calloc (1, strsize + 1);
if (!strtab) {
eprintf ("some\n");
return false;
}

int r = r_buf_read_at (eo->b, strtabaddr, (ut8 *)strtab, strsize);
if (r != strsize) {
free (strtab);
eprintf ("early\n");
return false;
}

eo->strtab = strtab;
eo->strtab_size = strsize;
init_dynamic_section_sdb (eo, strtabaddr, strsize);
sdb_num_set (eo->kv, "elf_strtab.offset", strtabaddr, 0);
sdb_num_set (eo->kv, "elf_strtab.size", strsize, 0);
return true;
}

Expand Down Expand Up @@ -1534,6 +1521,21 @@ static bool init_dynstr(ELFOBJ *eo) {

static const RVector *_load_elf_sections(ELFOBJ *eo);

static void relro_insdb(ELFOBJ *eo) {
int r = Elf_(has_relro) (eo);
switch (r) {
case R_ELF_FULL_RELRO:
sdb_set (eo->kv, "elf.relro", "full", 0);
break;
case R_ELF_PART_RELRO:
sdb_set (eo->kv, "elf.relro", "partial", 0);
break;
default:
sdb_set (eo->kv, "elf.relro", "no", 0);
break;
}
}

static bool elf_init(ELFOBJ *eo) {
// eo is not an ELF
if (!init_ehdr (eo)) {
Expand All @@ -1557,6 +1559,7 @@ static bool elf_init(ELFOBJ *eo) {
R_LOG_DEBUG ("Cannot initialize dynamic section");
}
}
relro_insdb (eo);

eo->imports_by_ord_size = 0;
eo->imports_by_ord = NULL;
Expand Down
8 changes: 5 additions & 3 deletions libr/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,11 @@ static int bin_info(RCore *r, PJ *pj, int mode, ut64 laddr) {
pair_bool (pj, "pic", info->has_pi);
pair_bool (pj, "relocs", R_BIN_DBG_RELOCS & info->dbg_info);
Sdb *sdb_info = sdb_ns (obj->kv, "info", false);
tmp_buf = sdb_get (sdb_info, "elf.relro", 0);
if (tmp_buf) {
pair_str (pj, "relro", tmp_buf);
if (sdb_info) {
tmp_buf = sdb_get (sdb_info, "elf.relro", 0);
if (R_STR_ISNOTEMPTY (tmp_buf)) {
pair_str (pj, "relro", tmp_buf);
}
free (tmp_buf);
}
pair_str (pj, "rpath", info->rpath);
Expand Down
6 changes: 2 additions & 4 deletions libr/core/cmd_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ static RCoreHelpMessage help_msg_CS = {
};

static RCoreHelpMessage help_msg_Cs = {
"Usage:", "Cs[ga-*.] [size] [@addr]", "",
"NOTE:", " size", "1 unit in bytes == width in bytes of smallest possible char in encoding,",
"", "", " so ascii/latin1/utf8 = 1, utf16le = 2",
" Cz", " [size] [@addr]", "ditto",
"Usage:", "Cs[ga-*.] ([size]) [@addr]", "",
"Cz", " [size] [@addr]", "ditto",
"Cs", " [size] @addr", "add string (guess latin1/utf16le)",
"Cs", "", "list all strings in human friendly form",
"Cs*", "", "list all strings in r2 commands",
Expand Down
23 changes: 23 additions & 0 deletions test/db/formats/elf/relro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
NAME=relro partial
FILE=bins/elf/relro/true32
CMDS=ij~{bin.relro}
EXPECT=<<EOF
partial
EOF
RUN

NAME=relro no
FILE=bins/elf/relro/top
CMDS=ij~{bin.relro}
EXPECT=<<EOF
no
EOF
RUN

NAME=relro full
FILE=bins/elf/relro/netifd
CMDS=ij~{bin.relro}
EXPECT=<<EOF
full
EOF
RUN
1 change: 1 addition & 0 deletions test/db/formats/elf/riscv
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ nx false
os linux
pic false
relocs true
relro no
rpath NONE
sanitize false
static true
Expand Down
1 change: 1 addition & 0 deletions test/db/formats/elf/v850
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ nx false
os linux
pic false
relocs true
relro no
rpath NONE
sanitize false
static true
Expand Down

0 comments on commit 6ed6b59

Please sign in to comment.