Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 02770fc

Browse files
committed
Special care about the endbr64 instruction
Older versions of capstone are not aware of this (now common) instruction, thus it is now hardwired into libsyscall_itercept.
1 parent 7031a8f commit 02770fc

File tree

6 files changed

+136
-5
lines changed

6 files changed

+136
-5
lines changed

src/disasm_wrapper.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017, Intel Corporation
2+
* Copyright 2016-2020, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -207,11 +207,24 @@ struct intercept_disasm_result
207207
intercept_disasm_next_instruction(struct intercept_disasm_context *context,
208208
const unsigned char *code)
209209
{
210+
static const unsigned char endbr64[] = {0xf3, 0x0f, 0x1e, 0xfa};
211+
210212
struct intercept_disasm_result result = {0, };
211213
const unsigned char *start = code;
212214
size_t size = (size_t)(context->end - code + 1);
213215
uint64_t address = (uint64_t)code;
214216

217+
if (size >= sizeof(endbr64) &&
218+
memcmp(code, endbr64, sizeof(endbr64)) == 0) {
219+
result.is_set = true;
220+
result.is_endbr = true;
221+
result.length = 4;
222+
#ifndef NDEBUG
223+
result.mnemonic = "endbr64";
224+
#endif
225+
return result;
226+
}
227+
215228
if (!cs_disasm_iter(context->handle, &start, &size,
216229
&address, context->insn)) {
217230
result.is_set = false;

src/disasm_wrapper.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017, Intel Corporation
2+
* Copyright 2016-2020, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -63,6 +63,9 @@ struct intercept_disasm_result {
6363
*/
6464
bool has_ip_relative_opr;
6565

66+
/* as of now this only refers to endbr64 */
67+
bool is_endbr;
68+
6669
/* call instruction */
6770
bool is_call;
6871

src/patcher.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017, Intel Corporation
2+
* Copyright 2016-2020, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -284,12 +284,13 @@ is_relocateable_before_syscall(struct intercept_disasm_result ins)
284284
ins.is_rel_jump ||
285285
ins.is_jump ||
286286
ins.is_ret ||
287+
ins.is_endbr ||
287288
ins.is_syscall);
288289
}
289290

290291
/*
291292
* is_relocateable_after_syscall
292-
* checks if an instruction found before a syscall instruction
293+
* checks if an instruction found after a syscall instruction
293294
* can be relocated (and thus overwritten).
294295
*
295296
* Notice: we allow relocation of ret instructions.
@@ -304,6 +305,7 @@ is_relocateable_after_syscall(struct intercept_disasm_result ins)
304305
ins.is_call ||
305306
ins.is_rel_jump ||
306307
ins.is_jump ||
308+
ins.is_endbr ||
307309
ins.is_syscall);
308310
}
309311

test/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2017-2019, Intel Corporation
2+
# Copyright 2017-2020, Intel Corporation
33
#
44
# Redistribution and use in source and binary forms, with or without
55
# modification, are permitted provided that the following conditions
@@ -71,6 +71,13 @@ set(asm_patterns
7171
pattern_nop_padding8
7272
pattern_nop_padding9)
7373

74+
try_compile(ASSEMBLER_SUPPORTS_ENDBR64 ${CMAKE_BINARY_DIR}
75+
${CMAKE_CURRENT_SOURCE_DIR}/pattern_endbr64.in.S
76+
CMAKE_FLAGS "-DCMAKE_ASM_LINK_EXECUTABLE='echo skip linking'")
77+
if(ASSEMBLER_SUPPORTS_ENDBR64)
78+
list(APPEND asm_patterns pattern_endbr64)
79+
endif()
80+
7481
set(asm_patterns_failing
7582
pattern_double_syscall
7683
pattern_rets

test/pattern_endbr64.in.S

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# Copyright 2020, Intel Corporation
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions
6+
# are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
#
11+
# * Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in
13+
# the documentation and/or other materials provided with the
14+
# distribution.
15+
#
16+
# * Neither the name of the copyright holder nor the names of its
17+
# contributors may be used to endorse or promote products derived
18+
# from this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
#
33+
# A simple test with a syscall preceded by an endbr64 instruction.
34+
# This happens often in recent versions of glibc, but older versions
35+
# of capstone are not aware of this instruction.
36+
#
37+
38+
.intel_syntax noprefix
39+
40+
.global text_start;
41+
.global text_end;
42+
43+
#include "mock_trampoline_table.S"
44+
45+
.text
46+
47+
text_start:
48+
cmp rax, -1
49+
endbr64
50+
syscall
51+
cmp rax, -1
52+
cmp rax, -1
53+
cmp rax, -1
54+
cmp rax, -1
55+
text_end:

test/pattern_endbr64.out.S

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# Copyright 2020, Intel Corporation
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions
6+
# are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
#
11+
# * Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in
13+
# the documentation and/or other materials provided with the
14+
# distribution.
15+
#
16+
# * Neither the name of the copyright holder nor the names of its
17+
# contributors may be used to endorse or promote products derived
18+
# from this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
# see pattern_endbr64.in.S
33+
34+
.intel_syntax noprefix
35+
36+
.global text_start;
37+
.global text_end;
38+
39+
#include "mock_trampoline_table.S"
40+
41+
.text
42+
43+
text_start:
44+
cmp rax, -1
45+
endbr64
46+
jmp dst0
47+
int3
48+
cmp rax, -1
49+
cmp rax, -1
50+
cmp rax, -1
51+
text_end:

0 commit comments

Comments
 (0)