lib/ubsan: Add type mismatch handler for new GCC/Clang
Subsystems (4)
PATCH 1/2 lib/ubsan: Add type mismatch handler for new GCC/Clang
UBSAN=y fails to build with new GCC/clang:
arch/x86/kernel/head64.o: In function `sanitize_boot_params':
arch/x86/include/asm/bootparam_utils.h:37: undefined reference to `__ubsan_handle_type_mismatch_v1'
because Clang and GCC 8 slightly changed ABI for 'type mismatch' errors. Compiler now uses new __ubsan_handle_type_mismatch_v1() function with slightly modified 'struct type_mismatch_data'.
Let's add new 'struct type_mismatch_data_common' which is independent from compiler's layout of 'struct type_mismatch_data'. And make __ubsan_handle_type_mismatch[_v1]() functions transform compiler-dependent type mismatch data to our internal representation.
This way, we can support both old and new compilers with minimal amount of change.
--- lib/ubsan.c | 48 ++++++++++++++++++++++++++++++++++++++---------- lib/ubsan.h | 14 ++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-)
lib/ubsan.c +38 -10
@@ -265,14 +265,14 @@ void __ubsan_handle_divrem_overflow(struct overflow_data *data, }EXPORT_SYMBOL (__ubsan_handle_divrem_overflow); -static void handle_null_ptr_deref (struct type_mismatch_data *data) +static void handle_null_ptr_deref (struct type_mismatch_data_common *data) {unsigned long flags; -if (suppress_report (&data->location)) +if (suppress_report (data->location))return ; -ubsan_prologue (&data->location, &flags); +ubsan_prologue (data->location, &flags); pr_err("%s null pointer of type %s\n", type_check_kinds[data->type_check_kind],
@@ -281,15 +281,15 @@ static void handle_null_ptr_deref(struct type_mismatch_data *data)ubsan_epilogue (&flags); } -static void handle_misaligned_access (struct type_mismatch_data *data, +static void handle_misaligned_access (struct type_mismatch_data_common *data,unsigned long ptr) {unsigned long flags; -if (suppress_report (&data->location)) +if (suppress_report (data->location))return ; -ubsan_prologue (&data->location, &flags); +ubsan_prologue (data->location, &flags); pr_err("%s misaligned address %p for type %s\n", type_check_kinds[data->type_check_kind],
@@ -299,15 +299,15 @@ static void handle_misaligned_access(struct type_mismatch_data *data,ubsan_epilogue (&flags); } -static void handle_object_size_mismatch (struct type_mismatch_data *data, +static void handle_object_size_mismatch (struct type_mismatch_data_common *data,unsigned long ptr) {unsigned long flags; -if (suppress_report (&data->location)) +if (suppress_report (data->location))return ; -ubsan_prologue (&data->location, &flags); +ubsan_prologue (data->location, &flags);pr_err ("%s address %p with insufficient space\n" , type_check_kinds[data->type_check_kind], (void *) ptr);
@@ -315,7 +315,7 @@ static void handle_object_size_mismatch(struct type_mismatch_data *data,ubsan_epilogue (&flags); } -void __ubsan_handle_type_mismatch (struct type_mismatch_data *data, +static void ubsan_type_mismatch_common (struct type_mismatch_data_common *data,unsigned long ptr) {
@@ -326,8 +326,36 @@ void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,else handle_object_size_mismatch (data, ptr); } + +void __ubsan_handle_type_mismatch (struct type_mismatch_data *data, +unsigned long ptr) +{ +struct type_mismatch_data_common common_data = { + .location = &data->location, + .type = data->type, + .alignment = data->alignment, + .type_check_kind = data->type_check_kind + }; + +ubsan_type_mismatch_common (&common_data, ptr); +}EXPORT_SYMBOL (__ubsan_handle_type_mismatch); +void __ubsan_handle_type_mismatch_v1 (struct type_mismatch_data_v1 *data, +unsigned long ptr) +{ + +struct type_mismatch_data_common common_data = { + .location = &data->location, + .type = data->type, + .alignment =1UL << data->log_alignment, + .type_check_kind = data->type_check_kind + }; + +ubsan_type_mismatch_common (&common_data, ptr); +} +EXPORT_SYMBOL (__ubsan_handle_type_mismatch_v1); +void __ubsan_handle_nonnull_return (struct nonnull_return_data *data) {unsigned long flags;
lib/ubsan.h +14 -0
@@ -37,6 +37,20 @@ struct type_mismatch_data {unsigned char type_check_kind; }; +struct type_mismatch_data_v1 { +struct source_location location; +struct type_descriptor *type; +unsigned char log_alignment; +unsigned char type_check_kind; +}; + +struct type_mismatch_data_common { +struct source_location *location; +struct type_descriptor *type; +unsigned long alignment; +unsigned char type_check_kind; +}; +struct nonnull_arg_data {struct source_location location;struct source_location attr_location;
Signature · 1 line
2.13.6
PATCH 2/2 lib/ubsan: remove returns-nonnull-attribute checks.
Similarly to type mismatch checks, new GCC 8.x and Clang also changed for ABI for returns_nonnull checks. While we can update our code to conform the new ABI it's more reasonable to just remove it. Because it's just dead code, we don't have any single user of returns_nonnull attribute in the whole kernel.
And AFAIU the advantage that this attribute could bring would be mitigated by -fno-delete-null-pointer-checks cflag that we use to build the kernel. So it's unlikely we will have a lot of returns_nonnull attribute in future.
So let's just remove the code, it has no use.
--- lib/ubsan.c | 19 ------------------- lib/ubsan.h | 5 ----- scripts/Makefile.ubsan | 1 - 3 files changed, 25 deletions(-)
lib/ubsan.c +0 -19
@@ -356,25 +356,6 @@ void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data, }EXPORT_SYMBOL (__ubsan_handle_type_mismatch_v1); -void __ubsan_handle_nonnull_return (struct nonnull_return_data *data) -{ -unsigned long flags; - -if (suppress_report (&data->location)) -return ; - -ubsan_prologue (&data->location, &flags); - -pr_err ("null pointer returned from function declared to never return null\n" ); - -if (location_is_valid (&data->attr_location)) -print_source_location ("returns_nonnull attribute specified in" , - &data->attr_location); - -ubsan_epilogue (&flags); -} -EXPORT_SYMBOL (__ubsan_handle_nonnull_return); -void __ubsan_handle_vla_bound_not_positive (struct vla_bound_data *data,unsigned long bound) {
lib/ubsan.h +0 -5
@@ -57,11 +57,6 @@ struct nonnull_arg_data {int arg_index; }; -struct nonnull_return_data { -struct source_location location; -struct source_location attr_location; -}; -struct vla_bound_data {struct source_location location;struct type_descriptor *type;
scripts/Makefile.ubsan +0 -1
@@ -7,7 +7,6 @@ ifdef CONFIG_UBSAN CFLAGS_UBSAN += $(call cc-option, -fsanitize=signed-integer-overflow) CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds) CFLAGS_UBSAN += $(call cc-option, -fsanitize=object-size) - CFLAGS_UBSAN += $(call cc-option, -fsanitize=returns-nonnull-attribute) CFLAGS_UBSAN += $(call cc-option, -fsanitize=bool) CFLAGS_UBSAN += $(call cc-option, -fsanitize=enum)
Signature · 1 line
2.13.6