Skip to content

Commit 26f98b2

Browse files
jmberg-intelgregkh
authored andcommitted
iwlwifi: pcie: make cfg vs. trans_cfg more robust
[ Upstream commit 48a5494 ] If we (for example) have a trans_cfg entry in the PCI IDs table, but then don't find a full cfg entry for it in the info table, we fall through to the code that treats the PCI ID table entry as a full cfg entry. This obviously causes crashes later, e.g. when trying to build the firmware name string. Avoid such crashes by using the low bit of the pointer as a tag for trans_cfg entries (automatically using a macro that checks the type when assigning) and then checking that before trying to use the data as a full entry - if it's just a partial entry at that point, fail. Since we're adding some macro magic, also check that the type is in fact either struct iwl_cfg_trans_params or struct iwl_cfg, failing compilation ("initializer element is not constant") if it isn't. Signed-off-by: Johannes Berg <johannes.berg@intel.com> Signed-off-by: Luca Coelho <luciano.coelho@intel.com> Link: https://lore.kernel.org/r/iwlwifi.20210330162204.6f69fe6e4128.I921d4ae20ef5276716baeeeda0b001cf25b9b968@changeid Signed-off-by: Luca Coelho <luciano.coelho@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2bfc47d commit 26f98b2

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

  • drivers/net/wireless/intel/iwlwifi/pcie

‎drivers/net/wireless/intel/iwlwifi/pcie/drv.c‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,20 @@
7373
#include "iwl-prph.h"
7474
#include "internal.h"
7575

76+
#define TRANS_CFG_MARKER BIT(0)
77+
#define _IS_A(cfg, _struct) __builtin_types_compatible_p(typeof(cfg), \
78+
struct _struct)
79+
extern int _invalid_type;
80+
#define _TRANS_CFG_MARKER(cfg) \
81+
(__builtin_choose_expr(_IS_A(cfg, iwl_cfg_trans_params), \
82+
TRANS_CFG_MARKER, \
83+
__builtin_choose_expr(_IS_A(cfg, iwl_cfg), 0, _invalid_type)))
84+
#define _ASSIGN_CFG(cfg) (_TRANS_CFG_MARKER(cfg) + (kernel_ulong_t)&(cfg))
85+
7686
#define IWL_PCI_DEVICE(dev, subdev, cfg) \
7787
.vendor = PCI_VENDOR_ID_INTEL, .device = (dev), \
7888
.subvendor = PCI_ANY_ID, .subdevice = (subdev), \
79-
.driver_data = (kernel_ulong_t)&(cfg)
89+
.driver_data = _ASSIGN_CFG(cfg)
8090

8191
/* Hardware specific file defines the PCI IDs table for that hardware module */
8292
static const struct pci_device_id iwl_hw_card_ids[] = {
@@ -1018,20 +1028,23 @@ static const struct iwl_dev_info iwl_dev_info_table[] = {
10181028

10191029
static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10201030
{
1021-
const struct iwl_cfg_trans_params *trans =
1022-
(struct iwl_cfg_trans_params *)(ent->driver_data);
1031+
const struct iwl_cfg_trans_params *trans;
10231032
const struct iwl_cfg *cfg_7265d __maybe_unused = NULL;
10241033
struct iwl_trans *iwl_trans;
10251034
struct iwl_trans_pcie *trans_pcie;
10261035
unsigned long flags;
10271036
int i, ret;
1037+
const struct iwl_cfg *cfg;
1038+
1039+
trans = (void *)(ent->driver_data & ~TRANS_CFG_MARKER);
1040+
10281041
/*
10291042
* This is needed for backwards compatibility with the old
10301043
* tables, so we don't need to change all the config structs
10311044
* at the same time. The cfg is used to compare with the old
10321045
* full cfg structs.
10331046
*/
1034-
const struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
1047+
cfg = (void *)(ent->driver_data & ~TRANS_CFG_MARKER);
10351048

10361049
/* make sure trans is the first element in iwl_cfg */
10371050
BUILD_BUG_ON(offsetof(struct iwl_cfg, trans));
@@ -1133,11 +1146,19 @@ static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11331146

11341147
#endif
11351148
/*
1136-
* If we didn't set the cfg yet, assume the trans is actually
1137-
* a full cfg from the old tables.
1149+
* If we didn't set the cfg yet, the PCI ID table entry should have
1150+
* been a full config - if yes, use it, otherwise fail.
11381151
*/
1139-
if (!iwl_trans->cfg)
1152+
if (!iwl_trans->cfg) {
1153+
if (ent->driver_data & TRANS_CFG_MARKER) {
1154+
pr_err("No config found for PCI dev %04x/%04x, rev=0x%x, rfid=0x%x\n",
1155+
pdev->device, pdev->subsystem_device,
1156+
iwl_trans->hw_rev, iwl_trans->hw_rf_id);
1157+
ret = -EINVAL;
1158+
goto out_free_trans;
1159+
}
11401160
iwl_trans->cfg = cfg;
1161+
}
11411162

11421163
/* if we don't have a name yet, copy name from the old cfg */
11431164
if (!iwl_trans->name)

0 commit comments

Comments
 (0)