Skip to content

Commit 813104f

Browse files
committed
Name local plt stubs in x86 retpoline layouts ##analysis
1 parent 52ed65a commit 813104f

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

libr/core/canal.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5823,14 +5823,14 @@ static void plt_stub_flag(RCore *core, ut64 entry, ut64 size, ut64 slot) {
58235823
free (fname);
58245824
}
58255825

5826-
// walk the section decoding entries: remember the last lea and load, and when an
5827-
// entry ends in an indirect jump derive the got slot it goes through
5826+
// decode entries, tracking the last lea/load to derive each got slot
58285827
static void plt_stub_scan_section(RCore *core, RBinSection *sec) {
58295828
if (sec->vsize < 8 || sec->vsize > 0x100000) {
58305829
return;
58315830
}
58325831
const ut64 sec_vaddr = r_bin_get_vaddr (core->bin, sec->paddr, sec->vaddr);
58335832
const int len = (int)sec->vsize;
5833+
const ut64 sec_end = sec_vaddr + len;
58345834
ut8 *buf = malloc (len);
58355835
if (!buf || !r_io_read_at (core->io, sec_vaddr, buf, len)) {
58365836
free (buf);
@@ -5857,15 +5857,13 @@ static void plt_stub_scan_section(RCore *core, RBinSection *sec) {
58575857
load_disp = UT64_MAX;
58585858
continue;
58595859
}
5860+
ut64 slot = UT64_MAX;
58605861
if (indirect) {
58615862
// x86 encodes the slot in one op; arm64-alikes split it lea/load/branch
5862-
ut64 slot = (op.ptr > 0 && op.ptr != -1)? (ut64)op.ptr: UT64_MAX;
5863+
slot = (op.ptr > 0 && op.ptr != -1)? (ut64)op.ptr: UT64_MAX;
58635864
if (slot == UT64_MAX && lea_ptr != UT64_MAX && load_disp != UT64_MAX) {
58645865
slot = lea_ptr + load_disp;
58655866
}
5866-
if (slot != UT64_MAX) {
5867-
plt_stub_flag (core, entry, at + oplen - entry, slot);
5868-
}
58695867
} else {
58705868
switch (type) {
58715869
case R_ANAL_OP_TYPE_LEA:
@@ -5887,6 +5885,11 @@ static void plt_stub_scan_section(RCore *core, RBinSection *sec) {
58875885
break;
58885886
case R_ANAL_OP_TYPE_JMP:
58895887
case R_ANAL_OP_TYPE_CALL:
5888+
// retpoline stubs branch to an in-section thunk
5889+
if (lea_ptr != UT64_MAX && op.jump >= sec_vaddr && op.jump < sec_end) {
5890+
slot = lea_ptr + ((load_disp == UT64_MAX)? 0: load_disp);
5891+
}
5892+
break;
58905893
case R_ANAL_OP_TYPE_UCALL:
58915894
case R_ANAL_OP_TYPE_RET:
58925895
case R_ANAL_OP_TYPE_TRAP:
@@ -5900,6 +5903,9 @@ static void plt_stub_scan_section(RCore *core, RBinSection *sec) {
59005903
break;
59015904
}
59025905
}
5906+
if (slot != UT64_MAX) {
5907+
plt_stub_flag (core, entry, at + oplen - entry, slot);
5908+
}
59035909
r_anal_op_fini (&op);
59045910
i += oplen;
59055911
if (ends) {

test/db/anal/plt-local

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,53 @@ EXPECT=<<EOF2
181181
0x00025550 11 sym.plt.calloc
182182
EOF2
183183
RUN
184+
185+
NAME=x86_64 lazy retpoline: stubs calling the shared thunk get named
186+
FILE=bins/elf/pltrel/x86_64-retpoline.so
187+
CMDS=<<EOF
188+
aa
189+
fs symbols
190+
f~plt.
191+
EOF
192+
EXPECT=<<EOF
193+
0x000014b0 17 sym.plt.add2
194+
0x000014d0 17 sym.plt.mul2
195+
0x000014f0 17 sym.plt.sub2
196+
EOF
197+
RUN
198+
199+
NAME=x86_64 lazy retpoline: a call through the stub reads its target name
200+
FILE=bins/elf/pltrel/x86_64-retpoline.so
201+
CMDS=<<EOF
202+
aa
203+
pd 1 @ 0x144b
204+
EOF
205+
EXPECT=<<EOF
206+
| 0x0000144b e860000000 call sym.plt.add2
207+
EOF
208+
RUN
209+
210+
NAME=x86_64 retpoline with -z now: stubs jumping to the shared thunk get named
211+
FILE=bins/elf/pltrel/x86_64-retpoline-now.so
212+
CMDS=<<EOF
213+
aa
214+
fs symbols
215+
f~plt.
216+
EOF
217+
EXPECT=<<EOF
218+
0x00001460 12 sym.plt.add2
219+
0x00001470 12 sym.plt.mul2
220+
0x00001480 12 sym.plt.sub2
221+
EOF
222+
RUN
223+
224+
NAME=x86_64 retpoline with -z now: a call through the stub reads its target name
225+
FILE=bins/elf/pltrel/x86_64-retpoline-now.so
226+
CMDS=<<EOF
227+
aa
228+
pd 1 @ 0x140b
229+
EOF
230+
EXPECT=<<EOF
231+
| 0x0000140b e850000000 call sym.plt.add2
232+
EOF
233+
RUN

0 commit comments

Comments
 (0)