staging: r8188eu: Fix return type for implementation of ndo_start_xmit
PATCH v3 1/1 staging: r8188eu: Fix return type for implementation of ndo_start_xmit
CFI (Control Flow Integrity) is a safety feature allowing the system to detect and react should a potential control flow hijacking occurs. In particular, the Forward-Edge CFI protects indirect function calls by ensuring the prototype of function that is actually called matches the definition of the function hook.
Since Linux now supports CFI, it will be a good idea to fix mismatched return type for implementation of hooks. Otherwise this would get cought out by CFI and cause a panic.
Use enums from netdev_tx_t as return value instead, then change return type to netdev_tx_t.
---
v3:
Provide detail on CFI.
v2:
Fix truncated subject.
--- drivers/staging/r8188eu/include/xmit_osdep.h | 2 +- drivers/staging/r8188eu/os_dep/xmit_linux.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/staging/r8188eu/include/xmit_osdep.h +1 -1
@@ -28,7 +28,7 @@ struct sta_xmit_priv;struct xmit_frame ;struct xmit_buf ; -int rtw_xmit_entry (struct sk_buff *pkt,struct net_device *pnetdev); +netdev_tx_t rtw_xmit_entry (struct sk_buff *pkt,struct net_device *pnetdev);void rtw_os_xmit_schedule (struct adapter *padapter);
drivers/staging/r8188eu/os_dep/xmit_linux.c +2 -2
@@ -198,7 +198,7 @@ static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)return true; } -int rtw_xmit_entry (struct sk_buff *pkt,struct net_device *pnetdev) +netdev_tx_t rtw_xmit_entry (struct sk_buff *pkt,struct net_device *pnetdev) {struct adapter *padapter = (struct adapter *)rtw_netdev_priv (pnetdev);struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
@@ -233,5 +233,5 @@ int rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev) exit: -return 0 ; +return NETDEV_TX_OK ; }
Signature · 1 line
2.17.1
[I'm fixing my work email]
Quoted 10 lines · GUO Zihua
CFI (Control Flow Integrity) is a safety feature allowing the system to detect and react should a potential control flow hijacking occurs. In particular, the Forward-Edge CFI protects indirect function calls by ensuring the prototype of function that is actually called matches the definition of the function hook.
Since Linux now supports CFI, it will be a good idea to fix mismatched return type for implementation of hooks. Otherwise this would get cought out by CFI and cause a panic.
Thanks! This patch needs a Fixes tag because it fixes a crash.
Ideally Clang would refuse to build known crashy code instead of us waiting for users to debug things manually. I have written a Smatch check which detects this bug.
drivers/staging/r8188eu/os_dep/os_intfs.c:316 (null)() CLANG_CFI insists types math exactly: 'rtw_xmit_entry'
I'll post the full results from my build tomorrow or later this week.
regards,
dan carpenter
/*
* Copyright (C) 2022 Your Name.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
#include "smatch_slist.h"
static int my_id;
static struct symbol *get_enum_type(struct expression *expr)
{
struct symbol *type;
type = get_type(expr);
if (!type || type->type != SYM_PTR)
return NULL;
type = get_real_base_type(type);
if (!type || type->type != SYM_FN)
return NULL;
type = get_real_base_type(type);
if (!type || type->type != SYM_ENUM)
return NULL;
return type;
}static bool enums_equiv(struct symbol *a, struct symbol *b)
{
if (!a && !b)
return true;
if (!a || !b)
return false;
if (!a->ident && !b->ident)
return types_equiv(a, b);
if (!a->ident || !b->ident)
return false;
return strcmp(a->ident->name, b->ident->name) == 0;
}static void match_assign(struct expression *expr)
{
struct symbol *left, *right;
char *name;
left = get_enum_type(expr->left);
right = get_enum_type(expr->right);
if (enums_equiv(left, right))
return;
name = expr_to_str(expr->right);
sm_msg("CLANG_CFI insists types math exactly: '%s'", name);
free_string(name);
}void check_enum_cfi(int id)
{
my_id = id;
add_hook(&match_assign, ASSIGNMENT_HOOK);
add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
}Here is the list of bugs that Smatch found. I had to modify the check a bit.
regards,
dan carpenter
/*
* Copyright (C) 2022 Your Name.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
#include "smatch_extra.h"
static int my_id;
static struct symbol *get_enum_type(struct expression *expr)
{
struct symbol *type;
int test = 0;
test++;
type = get_final_type(expr);
if (!type)
goto out_debug;
if (type->type == SYM_PTR) {
test++;
type = get_real_base_type(type);
}
test = 3;
if (!type || type->type != SYM_FN)
goto out_debug;
test++;
type = get_real_base_type(type);
if (!type || type->type != SYM_ENUM)
goto out_debug;
return type;out_debug:
if (local_debug)
sm_msg("%s: expr='%s' test=%d type='%s'", __func__, expr_to_str(expr), test, type_to_str(type));
return NULL;
}static bool enums_equiv(struct symbol *a, struct symbol *b)
{
if (!a && !b)
return true;
if (!a || !b)
return false;
if (!a->ident && !b->ident)
return types_equiv(a, b);
if (!a->ident || !b->ident)
return false;
return strcmp(a->ident->name, b->ident->name) == 0;
}static bool is_void(struct expression *expr)
{
struct symbol *type = get_type(expr);
return types_equiv(type, &void_ctype);
}static void match_assign(struct expression *expr)
{
struct symbol *left, *right;
char *name;
if (expr->op != '=')
return;
if (is_fake_call(expr->right))
return;
if (expr_is_zero(expr->right))
return;
left = get_enum_type(expr->left);
right = get_enum_type(expr->right);
if (enums_equiv(left, right))
return;
if (is_void_ptr(get_type(expr->left)) ||
is_void_ptr(get_type(expr->right)))
return;
// FIXME: what?
if (is_void(expr->left))
return;
name = expr_to_str(expr);
sm_warning("CLANG_CFI insists types match exactly: '%s' '%s vs %s'", name,
type_to_str(left), type_to_str(right));
sm_msg("%s: left_type='%s'", __func__, type_to_str(get_type(expr->left)));
free_string(name);
}void check_enum_cfi(int id)
{
my_id = id;
add_hook(&match_assign, ASSIGNMENT_HOOK);
add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
}Quoted 2 lines · Dan Carpenter
Here is the list of bugs that Smatch found. I had to modify the check a bit.
Great, thanks for doing this, Dan! I filed a bug with the list, so this won't get lost:
https://github.com/ClangBuiltLinux/linux/issues/1703
Sami