Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * UBSAN error reporting functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "ubsan.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) const char *type_check_kinds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	"load of",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	"store to",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	"reference binding to",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	"member access within",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	"member call on",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	"constructor call on",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	"downcast of",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	"downcast of"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define REPORTED_BIT 31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define COLUMN_MASK (~(1U << REPORTED_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define LINE_MASK   (~0U)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define COLUMN_MASK   (~0U)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define LINE_MASK (~(1U << REPORTED_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define VALUE_LENGTH 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static bool was_reported(struct source_location *location)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	return test_and_set_bit(REPORTED_BIT, &location->reported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static bool suppress_report(struct source_location *loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return current->in_ubsan || was_reported(loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static bool type_is_int(struct type_descriptor *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return type->type_kind == type_kind_int;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static bool type_is_signed(struct type_descriptor *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	WARN_ON(!type_is_int(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return  type->type_info & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static unsigned type_bit_width(struct type_descriptor *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return 1 << (type->type_info >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static bool is_inline_int(struct type_descriptor *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	unsigned inline_bits = sizeof(unsigned long)*8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	unsigned bits = type_bit_width(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	WARN_ON(!type_is_int(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return bits <= inline_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static s_max get_signed_val(struct type_descriptor *type, void *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (is_inline_int(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		unsigned long ulong_val = (unsigned long)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return ((s_max)ulong_val) << extra_bits >> extra_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (type_bit_width(type) == 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return *(s64 *)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return *(s_max *)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static bool val_is_negative(struct type_descriptor *type, void *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	return type_is_signed(type) && get_signed_val(type, val) < 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static u_max get_unsigned_val(struct type_descriptor *type, void *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (is_inline_int(type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return (unsigned long)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (type_bit_width(type) == 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return *(u64 *)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return *(u_max *)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static void val_to_string(char *str, size_t size, struct type_descriptor *type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (type_is_int(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		if (type_bit_width(type) == 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #if defined(CONFIG_ARCH_SUPPORTS_INT128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			u_max val = get_unsigned_val(type, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			scnprintf(str, size, "0x%08x%08x%08x%08x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				(u32)(val >> 96),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				(u32)(val >> 64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				(u32)(val >> 32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				(u32)(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		} else if (type_is_signed(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			scnprintf(str, size, "%lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				(s64)get_signed_val(type, value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			scnprintf(str, size, "%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				(u64)get_unsigned_val(type, value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void ubsan_prologue(struct source_location *loc, const char *reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	current->in_ubsan++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	pr_err("========================================"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		"========================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		loc->line & LINE_MASK, loc->column & COLUMN_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static void ubsan_epilogue(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	pr_err("========================================"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		"========================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	current->in_ubsan--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (panic_on_warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		 * This thread may hit another WARN() in the panic path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		 * Resetting this prevents additional WARN() from panicking the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		 * system on this thread.  Other threads are blocked by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		 * panic_mutex in panic().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		panic_on_warn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		panic("panic_on_warn set ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct overflow_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	char rhs_val_str[VALUE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (suppress_report(&data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ubsan_prologue(&data->location, "division-overflow");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		pr_err("division of %s by -1 cannot be represented in type %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			rhs_val_str, data->type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		pr_err("division by zero\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (suppress_report(data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	ubsan_prologue(data->location, "null-ptr-deref");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	pr_err("%s null pointer of type %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		type_check_kinds[data->type_check_kind],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		data->type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static void handle_misaligned_access(struct type_mismatch_data_common *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				unsigned long ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (suppress_report(data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	ubsan_prologue(data->location, "misaligned-access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	pr_err("%s misaligned address %p for type %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		type_check_kinds[data->type_check_kind],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		(void *)ptr, data->type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	pr_err("which requires %ld byte alignment\n", data->alignment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 					unsigned long ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (suppress_report(data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	ubsan_prologue(data->location, "object-size-mismatch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	pr_err("%s address %p with insufficient space\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		type_check_kinds[data->type_check_kind],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		(void *) ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	pr_err("for an object of type %s\n", data->type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				unsigned long ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	unsigned long flags = user_access_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		handle_null_ptr_deref(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		handle_misaligned_access(data, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		handle_object_size_mismatch(data, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	user_access_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct type_mismatch_data_common common_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		.location = &data->location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		.type = data->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		.alignment = data->alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		.type_check_kind = data->type_check_kind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct type_mismatch_data_v1 *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct type_mismatch_data_common common_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		.location = &data->location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		.type = data->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		.alignment = 1UL << data->log_alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		.type_check_kind = data->type_check_kind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) void __ubsan_handle_out_of_bounds(void *_data, void *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct out_of_bounds_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	char index_str[VALUE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (suppress_report(&data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	ubsan_prologue(&data->location, "array-index-out-of-bounds");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	val_to_string(index_str, sizeof(index_str), data->index_type, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	pr_err("index %s is out of range for type %s\n", index_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		data->array_type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct shift_out_of_bounds_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct type_descriptor *rhs_type = data->rhs_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct type_descriptor *lhs_type = data->lhs_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	char rhs_str[VALUE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	char lhs_str[VALUE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	unsigned long ua_flags = user_access_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (suppress_report(&data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	ubsan_prologue(&data->location, "shift-out-of-bounds");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (val_is_negative(rhs_type, rhs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		pr_err("shift exponent %s is negative\n", rhs_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	else if (get_unsigned_val(rhs_type, rhs) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		type_bit_width(lhs_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		pr_err("shift exponent %s is too large for %u-bit type %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			rhs_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			type_bit_width(lhs_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			lhs_type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	else if (val_is_negative(lhs_type, lhs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		pr_err("left shift of negative value %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			lhs_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		pr_err("left shift of %s by %s places cannot be"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			" represented in type %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			lhs_str, rhs_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			lhs_type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	user_access_restore(ua_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) void __ubsan_handle_builtin_unreachable(void *_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct unreachable_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	ubsan_prologue(&data->location, "unreachable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	pr_err("calling __builtin_unreachable()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	panic("can't return from __builtin_unreachable()");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) void __ubsan_handle_load_invalid_value(void *_data, void *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct invalid_value_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	char val_str[VALUE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (suppress_report(&data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	ubsan_prologue(&data->location, "invalid-load");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	val_to_string(val_str, sizeof(val_str), data->type, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	pr_err("load of value %s is not a valid value for type %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		val_str, data->type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 					 unsigned long align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 					 unsigned long offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 					 unsigned long align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 					 unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	struct alignment_assumption_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	unsigned long real_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (suppress_report(&data->location))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	ubsan_prologue(&data->location, "alignment-assumption");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		       align, offset, data->type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		pr_err("assumption of %lu byte alignment for pointer of type %s failed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		       align, data->type->type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	real_ptr = ptr - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	       offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	       real_ptr & (align - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	ubsan_epilogue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);