Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp617136pxy; Wed, 5 May 2021 09:37:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwpven6wZCKEVrkHem5Ve2aChzDosjq1n9ixlKjB8bOyX5tLf3Cmz2Hs48iEQJX1GyjtaKd X-Received: by 2002:a17:906:6544:: with SMTP id u4mr28253519ejn.455.1620232665604; Wed, 05 May 2021 09:37:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620232665; cv=none; d=google.com; s=arc-20160816; b=ze7QBZnDtebAiz5X9y2hA9d069a8eDKjRempSrILcPxb8v5EeZiNPRHTRbFVVaBDDQ k56nSbqCrAIe6+K3KGUT84MDVZv7Gax/QxDFp+rcQTiBitxOZtyUxLEqjJu91BBo39sp w0o7wbPE1Aw3mzwvfedjqCve9gPQESpyIzItawbfs5z6fR3wxpIEhlLcgBjPBB3hifO8 m+VJ0vwyyYHYH9E3KWUXXxjfcOiXBgqlQejj3pER/INfpuUySG3Uy4il40sy7ovQeToD UvzXvKWg2XOoBWDxC6ChwQJM5rJj1BvZe5S8boi/fJhGftY1P1Zr7joCp4XIoforHrYZ JagA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0t7T+EHEBsBHIzwatWwbZfJ6VbDzy9dl2Trn2zgtHXo=; b=ANxpcSLX7g3P9hbRV0cTxBDT28/vdgxMYD51DVvkHd3zhgST4tIERpPlKuRHEeVmlD rvkrzvlRenu8Moum+S1wuAhV+hXgxys0pxnCJUtaQBGe3KKneM934l/UF6Zzqpfz6f8I Rz0xEhfkb+6gITNp99+y/eAcvRX4j5wOlklqV7LoNmXR4fw1QFptQPLgVtuHDAcOwr4Q ru4HrJu7lgzB+gCHE9aRxJEQb+yEWimP0IMEqewcMZN19MYfqSqmvxBqDfxyFU/5GBjH cwixB+NODSZjrmmlEtc84SGzBCMdbsR/s/WDSUdRILZOjrhUFock/tAzx7ZH8m4RjnQe k4eA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pTLPIA5D; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t8si14497254edi.510.2021.05.05.09.37.21; Wed, 05 May 2021 09:37:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pTLPIA5D; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235168AbhEEQh2 (ORCPT + 99 others); Wed, 5 May 2021 12:37:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:54830 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234818AbhEEQfW (ORCPT ); Wed, 5 May 2021 12:35:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BFE096143C; Wed, 5 May 2021 16:33:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1620232381; bh=Qb7qsexp+qXICa53WC80jdJIlYDjhRT3QoI9Uv6i9Nc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pTLPIA5DLoN9quok52tJsK+wj2LEc1nvDKzBE81IEh7VX9N8U2xWmOZWSTTNPzPYO IEVJcqAiic3E34YTV0cFrnGkOE/YdTlwzywrqvLVfZ9ofjr8KfX3M9nVQqMyr4hv/f Rz6gCRgMjTv2/2U1PSllk+tIy7R8hhQQfaaBRgqyyOQWSD3DLw5STqxFfemylvhGaE FKIb0NchC/rtgZFxJtmQ1Vo3JWfbDwkfkNJKBvhboTZI0gglkW5UelU2qiaDG8/lAy kwM7HSYFDUfFcYlgACThUk2a8H057faVrNrwH/wEvdv6sRwFzwy8L2W5nMe1HLp/qV 83Ju680its2hg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Johannes Berg , Luca Coelho , Sasha Levin , linux-wireless@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH AUTOSEL 5.12 069/116] iwlwifi: pcie: make cfg vs. trans_cfg more robust Date: Wed, 5 May 2021 12:30:37 -0400 Message-Id: <20210505163125.3460440-69-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210505163125.3460440-1-sashal@kernel.org> References: <20210505163125.3460440-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org From: Johannes Berg [ Upstream commit 48a5494d6a4cb5812f0640d9515f1876ffc7a013 ] 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 Signed-off-by: Luca Coelho Link: https://lore.kernel.org/r/iwlwifi.20210330162204.6f69fe6e4128.I921d4ae20ef5276716baeeeda0b001cf25b9b968@changeid Signed-off-by: Luca Coelho Signed-off-by: Sasha Levin --- drivers/net/wireless/intel/iwlwifi/pcie/drv.c | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/drivers/net/wireless/intel/iwlwifi/pcie/drv.c b/drivers/net/wireless/intel/iwlwifi/pcie/drv.c index 558a0b2ef0fc..6f4db04ead4a 100644 --- a/drivers/net/wireless/intel/iwlwifi/pcie/drv.c +++ b/drivers/net/wireless/intel/iwlwifi/pcie/drv.c @@ -17,10 +17,20 @@ #include "iwl-prph.h" #include "internal.h" +#define TRANS_CFG_MARKER BIT(0) +#define _IS_A(cfg, _struct) __builtin_types_compatible_p(typeof(cfg), \ + struct _struct) +extern int _invalid_type; +#define _TRANS_CFG_MARKER(cfg) \ + (__builtin_choose_expr(_IS_A(cfg, iwl_cfg_trans_params), \ + TRANS_CFG_MARKER, \ + __builtin_choose_expr(_IS_A(cfg, iwl_cfg), 0, _invalid_type))) +#define _ASSIGN_CFG(cfg) (_TRANS_CFG_MARKER(cfg) + (kernel_ulong_t)&(cfg)) + #define IWL_PCI_DEVICE(dev, subdev, cfg) \ .vendor = PCI_VENDOR_ID_INTEL, .device = (dev), \ .subvendor = PCI_ANY_ID, .subdevice = (subdev), \ - .driver_data = (kernel_ulong_t)&(cfg) + .driver_data = _ASSIGN_CFG(cfg) /* Hardware specific file defines the PCI IDs table for that hardware module */ static const struct pci_device_id iwl_hw_card_ids[] = { @@ -1075,19 +1085,22 @@ static const struct iwl_dev_info iwl_dev_info_table[] = { static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) { - const struct iwl_cfg_trans_params *trans = - (struct iwl_cfg_trans_params *)(ent->driver_data); + const struct iwl_cfg_trans_params *trans; const struct iwl_cfg *cfg_7265d __maybe_unused = NULL; struct iwl_trans *iwl_trans; struct iwl_trans_pcie *trans_pcie; int i, ret; + const struct iwl_cfg *cfg; + + trans = (void *)(ent->driver_data & ~TRANS_CFG_MARKER); + /* * This is needed for backwards compatibility with the old * tables, so we don't need to change all the config structs * at the same time. The cfg is used to compare with the old * full cfg structs. */ - const struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data); + cfg = (void *)(ent->driver_data & ~TRANS_CFG_MARKER); /* make sure trans is the first element in iwl_cfg */ BUILD_BUG_ON(offsetof(struct iwl_cfg, trans)); @@ -1202,11 +1215,19 @@ static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) #endif /* - * If we didn't set the cfg yet, assume the trans is actually - * a full cfg from the old tables. + * If we didn't set the cfg yet, the PCI ID table entry should have + * been a full config - if yes, use it, otherwise fail. */ - if (!iwl_trans->cfg) + if (!iwl_trans->cfg) { + if (ent->driver_data & TRANS_CFG_MARKER) { + pr_err("No config found for PCI dev %04x/%04x, rev=0x%x, rfid=0x%x\n", + pdev->device, pdev->subsystem_device, + iwl_trans->hw_rev, iwl_trans->hw_rf_id); + ret = -EINVAL; + goto out_free_trans; + } iwl_trans->cfg = cfg; + } /* if we don't have a name yet, copy name from the old cfg */ if (!iwl_trans->name) -- 2.30.2