[PATCH 4/4] counter: 104-quad-8: Adjust final parameter type of certain callback functions

Nathan Chancellor
2022-11-02T17:22:41Z

With clang's kernel control flow integrity (kCFI, CONFIG_CFI_CLANG),
indirect call targets are validated against the expected function
pointer prototype to make sure the call target is valid to help mitigate
ROP attacks. If they are not identical, there is a failure at run time,
which manifests as either a kernel panic or thread getting killed. A
proposed warning in clang aims to catch these at compile time, which
reveals:

drivers/counter/104-quad-8.c:1041:2: error: incompatible function pointer types initializing 'int (*)(struct counter_device *, struct counter_signal *, u32 *)' (aka 'int (*)(struct counter_device *, struct counter_signal *, unsigned int *)') with an expression of type
'int (struct counter_device *, struct counter_signal *, enum counter_signal_polarity *)' [-Werror,-Wincompatible-function-pointer-types-strict]
COUNTER_COMP_POLARITY(quad8_polarity_read, quad8_polarity_write,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/counter.h:609:21: note: expanded from macro 'COUNTER_COMP_POLARITY'
.signal_u32_read = (_read), \
^~~~~~~
drivers/counter/104-quad-8.c:1041:2: error: incompatible function pointer types initializing 'int (*)(struct counter_device *, struct counter_signal *, u32)' (aka 'int (*)(struct counter_device *, struct counter_signal *, unsigned int)') with an expression of type 'int
(struct counter_device *, struct counter_signal *, enum counter_signal_polarity)' [-Werror,-Wincompatible-function-pointer-types-strict]
COUNTER_COMP_POLARITY(quad8_polarity_read, quad8_polarity_write,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/counter.h:610:22: note: expanded from macro 'COUNTER_COMP_POLARITY'
.signal_u32_write = (_write), \
^~~~~~~~
drivers/counter/104-quad-8.c:1129:2: error: incompatible function pointer types initializing 'int (*)(struct counter_device *, struct counter_count *, u32 *)' (aka 'int (*)(struct counter_device *, struct counter_count *, unsigned int *)') with an expression of type 'i
nt (struct counter_device *, struct counter_count *, enum counter_count_mode *)' [-Werror,-Wincompatible-function-pointer-types-strict]
COUNTER_COMP_COUNT_MODE(quad8_count_mode_read, quad8_count_mode_write,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/counter.h:587:20: note: expanded from macro 'COUNTER_COMP_COUNT_MODE'
.count_u32_read = (_read), \
^~~~~~~
drivers/counter/104-quad-8.c:1129:2: error: incompatible function pointer types initializing 'int (*)(struct counter_device *, struct counter_count *, u32)' (aka 'int (*)(struct counter_device *, struct counter_count *, unsigned int)') with an expression of type 'int (
struct counter_device *, struct counter_count *, enum counter_count_mode)' [-Werror,-Wincompatible-function-pointer-types-strict]
COUNTER_COMP_COUNT_MODE(quad8_count_mode_read, quad8_count_mode_write,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/counter.h:588:21: note: expanded from macro 'COUNTER_COMP_COUNT_MODE'
.count_u32_write = (_write), \
^~~~~~~~
drivers/counter/104-quad-8.c:1131:2: error: incompatible function pointer types initializing 'int (*)(struct counter_device *, struct counter_count *, u32 *)' (aka 'int (*)(struct counter_device *, struct counter_count *, unsigned int *)') with an expression of type 'i
nt (struct counter_device *, struct counter_count *, enum counter_count_direction *)' [-Werror,-Wincompatible-function-pointer-types-strict]
COUNTER_COMP_DIRECTION(quad8_direction_read),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/counter.h:596:20: note: expanded from macro 'COUNTER_COMP_DIRECTION'
.count_u32_read = (_read), \
^~~~~~~
5 errors generated.

->count_u32_{read,write}() and ->count_u32_{read,write}() in 'struct
counter_comp' expect a final parameter type of either 'u32' or 'u32 *',
not the enumerated types used in the implementations. Adjust the final
parameter type in the implementations to match the prototype's to
resolve the warning and CFI failures.

Link: https://github.com/ClangBuiltLinux/linux/issues/1750
Reported-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
drivers/counter/104-quad-8.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index 30b40f805f88..720a88ad59db 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -337,7 +337,7 @@ static int quad8_function_write(struct counter_device *counter,

static int quad8_direction_read(struct counter_device *counter,
struct counter_count *count,
- enum counter_count_direction *direction)
+ u32 *direction)
{
const struct quad8 *const priv = counter_priv(counter);
unsigned int ud_flag;
@@ -572,7 +572,7 @@ static int quad8_index_polarity_set(struct counter_device *counter,

static int quad8_polarity_read(struct counter_device *counter,
struct counter_signal *signal,
- enum counter_signal_polarity *polarity)
+ u32 *polarity)
{
int err;
u32 index_polarity;
@@ -589,7 +589,7 @@ static int quad8_polarity_read(struct counter_device *counter,

static int quad8_polarity_write(struct counter_device *counter,
struct counter_signal *signal,
- enum counter_signal_polarity polarity)
+ u32 polarity)
{
const u32 pol = (polarity == COUNTER_SIGNAL_POLARITY_POSITIVE) ? 1 : 0;

@@ -654,7 +654,7 @@ static int quad8_count_floor_read(struct counter_device *counter,

static int quad8_count_mode_read(struct counter_device *counter,
struct counter_count *count,
- enum counter_count_mode *cnt_mode)
+ u32 *cnt_mode)
{
const struct quad8 *const priv = counter_priv(counter);

@@ -679,7 +679,7 @@ static int quad8_count_mode_read(struct counter_device *counter,

static int quad8_count_mode_write(struct counter_device *counter,
struct counter_count *count,
- enum counter_count_mode cnt_mode)
+ u32 cnt_mode)
{
struct quad8 *const priv = counter_priv(counter);
unsigned int count_mode;
--
2.38.1