[RFC v5 43/57] gcc-plugins: objtool: Add plugin to detect switch table on arm64

From: Julien Thierry
Date: Thu Jan 09 2020 - 11:08:29 EST


From: Raphael Gault <raphael.gault@xxxxxxx>

This plugins comes into play before the final 2 RTL passes of GCC and
detects switch-tables that are to be outputed in the ELF and writes
information in an ".discard.switch_table_info" section which will be
used by objtool.

Signed-off-by: Raphael Gault <raphael.gault@xxxxxxx>
[J.T.: Change section name to store switch table information,
Make plugin Kconfig be selected rather than opt-in by user,
Add a relocation in the switch_table_info that points to
the jump operation itself]
Signed-off-by: Julien Thierry <jthierry@xxxxxxxxxx>
Cc: Masahiro Yamada <masahiroy@xxxxxxxxxx>
Cc: Michal Marek <michal.lkml@xxxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Emese Revfy <re.emese@xxxxxxxxx>
Cc: linux-kbuild@xxxxxxxxxxxxxxx
Cc: kernel-hardening@xxxxxxxxxxxxxxxxxx
---
arch/arm64/Kconfig | 1 +
scripts/Makefile.gcc-plugins | 2 +
scripts/gcc-plugins/Kconfig | 4 +
.../arm64_switch_table_detection_plugin.c | 94 +++++++++++++++++++
4 files changed, 101 insertions(+)
create mode 100644 scripts/gcc-plugins/arm64_switch_table_detection_plugin.c

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index b1b4476ddb83..a7b2116d5d13 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -90,6 +90,7 @@ config ARM64
select DMA_DIRECT_REMAP
select EDAC_SUPPORT
select FRAME_POINTER
+ select GCC_PLUGIN_SWITCH_TABLES if STACK_VALIDATION
select GENERIC_ALLOCATOR
select GENERIC_ARCH_TOPOLOGY
select GENERIC_CLOCKEVENTS
diff --git a/scripts/Makefile.gcc-plugins b/scripts/Makefile.gcc-plugins
index 5f7df50cfe7a..a56736df9dc2 100644
--- a/scripts/Makefile.gcc-plugins
+++ b/scripts/Makefile.gcc-plugins
@@ -44,6 +44,8 @@ ifdef CONFIG_GCC_PLUGIN_ARM_SSP_PER_TASK
endif
export DISABLE_ARM_SSP_PER_TASK_PLUGIN

+gcc-plugin-$(CONFIG_GCC_PLUGIN_SWITCH_TABLES) += arm64_switch_table_detection_plugin.so
+
# All the plugin CFLAGS are collected here in case a build target needs to
# filter them out of the KBUILD_CFLAGS.
GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
diff --git a/scripts/gcc-plugins/Kconfig b/scripts/gcc-plugins/Kconfig
index e3569543bdac..f50047939660 100644
--- a/scripts/gcc-plugins/Kconfig
+++ b/scripts/gcc-plugins/Kconfig
@@ -112,4 +112,8 @@ config GCC_PLUGIN_ARM_SSP_PER_TASK
bool
depends on GCC_PLUGINS && ARM

+config GCC_PLUGIN_SWITCH_TABLES
+ bool
+ depends on GCC_PLUGINS && ARM64
+
endif
diff --git a/scripts/gcc-plugins/arm64_switch_table_detection_plugin.c b/scripts/gcc-plugins/arm64_switch_table_detection_plugin.c
new file mode 100644
index 000000000000..9b8b2ec6a3c8
--- /dev/null
+++ b/scripts/gcc-plugins/arm64_switch_table_detection_plugin.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include "gcc-common.h"
+
+__visible int plugin_is_GPL_compatible;
+
+#define GEN_QUAD(rtx) assemble_integer_with_op(".quad ", rtx)
+
+/*
+ * Create an array of metadata for each jump table found in the rtl.
+ * The metadata contains:
+ * - A pointer to the table of offsets used for the actual branch
+ * - A pointer to first instruction of the group getting expanded into an
+ * acutal jump
+ * - The number of entries in the table of offsets
+ * - Whether the offsets in the table are signed or not
+ */
+static unsigned int arm64_switchtbl_rtl_execute(void)
+{
+ rtx_insn *insn;
+ rtx_insn *labelp = NULL;
+ rtx_jump_table_data *tablep = NULL;
+ section *swt_sec;
+ section *curr_sec = current_function_section();
+
+ swt_sec = get_section(".discard.switch_table_info",
+ SECTION_EXCLUDE | SECTION_COMMON, NULL);
+
+ for (insn = get_insns(); insn; insn = NEXT_INSN(insn)) {
+ /*
+ * Find a tablejump_p INSN (using a dispatch table)
+ */
+ if (!tablejump_p(insn, &labelp, &tablep))
+ continue;
+
+ if (labelp && tablep) {
+ rtx_code_label *label_to_jump;
+
+ /*
+ * GCC is a bit touchy about adding the label right
+ * before the jump rtx_insn as it modifies the
+ * basic_block created for the jump table.
+ * Make sure we create the label before the whole
+ * basic_block of the jump table.
+ */
+ label_to_jump = gen_label_rtx();
+ SET_LABEL_KIND(label_to_jump, LABEL_NORMAL);
+ emit_label_before(label_to_jump, insn);
+ /* Force label to be kept, apparently LABEL_PRESERVE_P is an rvalue :) */
+ LABEL_PRESERVE_P(label_to_jump) = 1;
+
+ switch_to_section(swt_sec);
+ GEN_QUAD(gen_rtx_LABEL_REF(Pmode, labelp));
+ GEN_QUAD(gen_rtx_LABEL_REF(Pmode, label_to_jump));
+ GEN_QUAD(GEN_INT(GET_NUM_ELEM(tablep->get_labels())));
+ GEN_QUAD(GEN_INT(ADDR_DIFF_VEC_FLAGS(tablep).offset_unsigned));
+ switch_to_section(curr_sec);
+
+ /*
+ * Scheduler isn't very happy about leaving labels in
+ * the middle of jump table basic blocks.
+ */
+ delete_insn(label_to_jump);
+ }
+ }
+ return 0;
+}
+
+#define PASS_NAME arm64_switchtbl_rtl
+
+#define NO_GATE
+#include "gcc-generate-rtl-pass.h"
+
+__visible int plugin_init(struct plugin_name_args *plugin_info,
+ struct plugin_gcc_version *version)
+{
+ const char * const plugin_name = plugin_info->base_name;
+ int tso = 0;
+ int i;
+
+ if (!plugin_default_version_check(version, &gcc_version)) {
+ error(G_("incompatible gcc/plugin versions"));
+ return 1;
+ }
+
+ PASS_INFO(arm64_switchtbl_rtl, "expand", 1,
+ PASS_POS_INSERT_AFTER);
+
+ register_callback(plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP,
+ NULL, &arm64_switchtbl_rtl_pass_info);
+
+ return 0;
+}
--
2.21.0