Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp300172pxj; Tue, 18 May 2021 03:48:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7w+gj85ULr1mboPTgB80ClNtVQq9RtSO2iLABkk7nroCulbzZTEC8yLi3vjWqk3Yk3pgr X-Received: by 2002:a17:907:75e9:: with SMTP id jz9mr5286213ejc.314.1621334891298; Tue, 18 May 2021 03:48:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621334891; cv=none; d=google.com; s=arc-20160816; b=l6sdtlIhPz2foSQldmupFLBz5ds64eFUgRlUlq2N968CFo3rmFFRv18rB3UNozd8wI rEwhD2Z83wqg8G6PmRlhzYjxzSVKjS0+BgjnPKPi++UDZUlq2PIgSL9h2qBGiD/oxVfu p3jaoSl4IBhCkG0YL3g4uYYShuxVhGiRn0J686h4IgmtSD8OZLZi5/BCEPwUHx0zAr8C ++K8MTzz6nCLkyZVZga7ECu0zMX/uFC62lgNJ6fz4+oDN55QztDlhdB55hGIXyL67DmF Xv7cXSGSzvLtkUGIlBHZP5nYF1iN29A89DfLB3xBt8bShxL4onypsjycFMzp9uRO5dq8 snOg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3WBS+HPfAucyY+MhiLe6Y4e6UZgoJnDwtXjWSZtXXnI=; b=BgFs4MtcNTgzeKRI4setSXBoDxPEEapD2f9j007Hym2XbzRDLhsV4VenW/riX7Oipu 3y+AdatW4H8a4EPF1crWxM+aIafHoChxfTa8h6L+WmW5HutRe+oVYMDFgF0UmJ7oH9GC hSEnqU9Frjpg2FwkUczu5v6OpFB1nt8Yl0Ul3d29j+Pm3TgAXCJtZhuEig6edL4c0EN7 m28H6Axavz39WiV9bgAyH51R3V1NpsO5+rbkgf+r/4UmURWqpCYGhWzsfJKTkGTWoPjK J8DdtbumikYRtRmi4euZC2lDKmzn51PLT8eJZGPKoJ8lSs+wlI8+zWB1I+81tsLjlox3 ebKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VqXkWrpf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y18si16674849edv.525.2021.05.18.03.47.48; Tue, 18 May 2021 03:48:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=VqXkWrpf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243776AbhEQPOt (ORCPT + 99 others); Mon, 17 May 2021 11:14:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:47056 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242787AbhEQPEm (ORCPT ); Mon, 17 May 2021 11:04:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0EFED61585; Mon, 17 May 2021 14:28:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621261704; bh=oXOp+bGdugSKtD/RSmqGci0envmsKva3cXEibDMONN0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VqXkWrpf5Fms4j6jxAV1tLiZ4HODKggjxTMpJCjPYWfBB07R3mA3cp2GOMznP9hsJ 0OXvbfkuj9zsvhPp/FsinMSvv5Yk8pN9W4P2JnxSOM25+D1asu4e50Wr7zXWkYU3cm vkkU4BZ2oFYa0knjmNAr4zdT3aMmOqHBw0AAAXao= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johannes Berg , Luca Coelho , Sasha Levin Subject: [PATCH 5.10 054/289] iwlwifi: pcie: make cfg vs. trans_cfg more robust Date: Mon, 17 May 2021 15:59:39 +0200 Message-Id: <20210517140307.018412532@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140305.140529752@linuxfoundation.org> References: <20210517140305.140529752@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 500fdb0b6c42..eeb70560b746 100644 --- a/drivers/net/wireless/intel/iwlwifi/pcie/drv.c +++ b/drivers/net/wireless/intel/iwlwifi/pcie/drv.c @@ -73,10 +73,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[] = { @@ -1018,20 +1028,23 @@ 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; unsigned long flags; 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)); @@ -1133,11 +1146,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