From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796413619; b=hy1YyMiRxGOGol75i+7lP7+dkgXh8yotyP23FhpiUUBq81ATHRbvsGz+9p2FKlNim+dr BKhw2/RoI3RAe+sAI4vYraNNFBeO2Am3H85YjaweLsFRqQ9yumc3aXTX8wNF2Jlz0Befi 5dvaVwv0dh6OrMtamHlbVpmQm91hGADwYoSM6kARhuSP0kQ+M5wy7UNQbJdGODEhW+Htq CBGOtgzf0lg6KrX1oF7WInYagyXJ6Q/dcz6xPHvuwxI5TafEhBe9wgih9MQ8o7wEYOuTI X35ABGNdkUH4xYzGfC4EhoRYk7mqE9ZN7G7A5Cxds3uZgfso9qYC+lMKt1jZ54CXLxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796413619; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Cf8R5YjcI7sx/KZUa2pGmAwEo0kd04mAjwavDBJ2VeM=; b=jobhHT7hMWIYSdjJgIoWbrE1NkmuKnluh8ypsvhQpmlZ/dPmhd0soQyusA6BFT4RoTNQ aPb8HLu0bv4gR0RODdbpXKKIQVzQE5bXxfG5+qFsHBtXn04llqHWAMvd6+JCbyCyOorp2 Chb5XvXx86VqdWDxLAVxT9/YjN1VNjdqNL1xdTcfwocC11P3DORH8wxNDNdRFOj3qfcb/ CwIDdafUhJzx19JfUoLqbY9OM99USJv++ixVLeoKHPbkVcQ9FXn6Bvql1X3I3CC4878rM 4MjTxLIlrnV2LffMjm/Wb2Xijt2or6bp9I31A1nGoKqeYunHmg5COh6bKTYYGB27sYg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=NJxgm1qY; arc=none smtp.remote-ip=192.198.163.9 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CF32606A6A0 for ; Fri, 4 Dec 2026 19:46:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2CF32606A6A0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=NJxgm1qY DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796413618; x=1827949618; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Cf8R5YjcI7sx/KZUa2pGmAwEo0kd04mAjwavDBJ2VeM=; b=NJxgm1qYuxGPafhROfodtZp2VbXsjKHpFMn/cGochs2bd/N/DXYoR356 Y1AuKKA4epopw0omwKDEUMVjd7FjS9Od2N5RM5VK8lbVlQSeMbaxBtYlt jFqgu0R6Kl9/TJTPXwNsok9kiSWEiw8IEG1n5X0qdbQ1ieBGbJeL+84m0 uizHg5iapo8mQ4yPHtXTXPJSUSfnxTg4Ip50znw8sGzCx/YfUMcZD9PIW HiUvkTIqLYMIZb9/PKSsyABciCt+Ohi3SEkQk8UYyKRTrDjWpqau+nUa4 TOpIxA52XfVfZ/8zn0QNXSItgfmkB3zjG6sjx7z0u8y5lFal9a81FqMHU g==; X-CSE-ConnectionGUID: v9UjqYaZT7u5+Zqyb+5VNw== X-CSE-MsgGUID: TLeoH39PRB+bAYkWQ3Lp9w== X-IronPort-AV: E=McAfee;i="6800,10657,11986"; a="5267152" X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="5267152" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 11:46:54 -0800 X-CSE-ConnectionGUID: wh+oqmAaTRqKLiwD8mbeYg== X-CSE-MsgGUID: gD+T8pN8TW2Isu+Dem5BIg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="5635526" Received: from anguy11-upstream.jf.intel.com ([10.166.9.133]) by orviesa010.jf.intel.com with ESMTP; 04 Dec 2026 11:46:54 -0800 From: Tony Nguyen To: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, edumazet@kernel.org, andrew+netdev@lunn.ch, willemb@google.com, netdev@vger.kernel.org Cc: Emil Tantilov , anthony.l.nguyen@intel.com, oss.patchbox@gmail.com, tianxun.ng@bytedance.com, luckilystar08@gmail.com, richardcochran@gmail.com, milena.olech@intel.com, konstantin.ilichev@intel.com, Madhu Chittim , Naman Gulati , Samuel Salin Subject: [PATCH net v2 5/6] idpf: fix use-after-free in the PTP auxiliary worker Date: Fri, 4 Dec 2026 11:46:40 -0800 Message-ID: <20261204194642.3048406-6-anthony.l.nguyen@intel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20261204194642.3048406-1-anthony.l.nguyen@intel.com> References: <20261204194642.3048406-1-anthony.l.nguyen@intel.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Emil Tantilov idpf_ptp_update_cached_phctime() runs from the PTP auxiliary worker and walks the Rx queues of every vport to refresh rxq->cached_phc_time. There is a small window on down that can result in use-after-free, detected by KASAN: BUG: KASAN: slab-use-after-free in idpf_ptp_update_cached_phctime+0x367/0x4a0 [idpf] Write of size 8 at addr ff11002146c2a4a0 by task ptp3/8084 ... idpf_ptp_update_cached_phctime+0x367/0x4a0 [idpf] idpf_ptp_do_aux_work+0x37/0x50 [idpf] ptp_aux_kworker+0x64/0xd0 kthread_worker_fn+0x289/0xa20 ... Allocated by task 8615 on cpu 5 at 812.655509s: idpf_vport_queues_alloc+0x9e5/0x2580 [idpf] idpf_vport_open.constprop.0+0x12c/0x10f0 [idpf] idpf_open+0x1bb/0x230 [idpf] __dev_open+0x2f5/0x7c0 ... Freed by task 8628 on cpu 10 at 814.925757s: idpf_rxq_group_rel.part.0+0xef/0x5a0 [idpf] idpf_vport_queues_rel+0xaf/0x140 [idpf] idpf_vport_stop+0x49e/0x850 [idpf] idpf_stop+0x6d/0xc0 [idpf] __dev_close_many+0x24c/0x650 The existing rxq_grps test does not help. It is evaluated before the pointer is read again, so the worker can equally run past the kfree() and dereference a NULL rxq_grps with a stale num_rxq_grp. Add a per vport rxq_grps_lock and use it to serialize every transition of rsrc->rxq_grps against the worker: - Teardown: idpf_rxq_group_rel() sets rsrc->rxq_grps to NULL under the lock before anything reachable through it is freed. The worker re-reads the pointer under the same lock, so it either sees a live array while the release path is still blocked on the lock, or sees NULL and skips that vport. The frees stay outside the lock. - Allocation: Build the rxq_grps array privately and publish it to rsrc->rxq_grps under the lock only once every group is complete. The freeing of the group contents moves to idpf_rxq_grps_free(), which takes the array explicitly so the allocation error path can unwind safely. - Soft reset: idpf_vport_stop() freed rxq_grps and cleared the live vport's copy under rxq_grps_lock. Drop the stale pointer in the clone so the memcpy() below does not republish the freed array to the PTP aux worker before idpf_vport_open() reallocates it. The lock sits past the offsetof(struct idpf_vport, link_up) boundary that idpf_initiate_soft_reset() stops its memcpy() at, next to the wait queues and mutexes that are already kept out of the copy, so a soft reset never byte-copies it. The data it guards, dflt_qv_rsrc, is inside that copied region, which is why the clone has to be cleared rather than relying on the lock alone. Also, fix the NULL check in idpf_ptp_update_phctime_rxq_grp(). The split queue case took the address of a member before testing it, which can never be NULL, so test the rxq_sets[] entry itself. Fixes: 494565a74502 ("idpf: add support for Rx timestamping") Reviewed-by: Madhu Chittim Signed-off-by: Emil Tantilov Reviewed-by: Naman Gulati Tested-by: Samuel Salin Signed-off-by: Tony Nguyen --- drivers/net/ethernet/intel/idpf/idpf.h | 5 ++ drivers/net/ethernet/intel/idpf/idpf_lib.c | 6 ++ drivers/net/ethernet/intel/idpf/idpf_ptp.c | 16 +++++- drivers/net/ethernet/intel/idpf/idpf_txrx.c | 64 +++++++++++++++------ 4 files changed, 71 insertions(+), 20 deletions(-) diff --git a/drivers/net/ethernet/intel/idpf/idpf.h b/drivers/net/ethernet/intel/idpf/idpf.h index 533b069d97f9..c2631ac35a8b 100644 --- a/drivers/net/ethernet/intel/idpf/idpf.h +++ b/drivers/net/ethernet/intel/idpf/idpf.h @@ -373,6 +373,8 @@ struct idpf_q_vec_rsrc { * @default_vport: Use this vport if one isn't specified * @crc_enable: Enable CRC insertion offload * @link_up: True if link is up + * @rxq_grps_lock: Serializes every transition of the @dflt_qv_rsrc.rxq_grps + * pointer of a vport against the PTP auxiliary worker * @tx_tstamp_caps: Capabilities negotiated for Tx timestamping * @tstamp_config: The Tx tstamp config * @tstamp_task: Tx timestamping task @@ -406,6 +408,9 @@ struct idpf_vport { bool crc_enable; bool link_up; + /* Must be past the point idpf_initiate_soft_reset() memcpy()s */ + spinlock_t rxq_grps_lock; + struct idpf_ptp_vport_tx_tstamp_caps *tx_tstamp_caps; struct kernel_hwtstamp_config tstamp_config; struct work_struct tstamp_task; diff --git a/drivers/net/ethernet/intel/idpf/idpf_lib.c b/drivers/net/ethernet/intel/idpf/idpf_lib.c index 062d2f540254..38cb91beacd0 100644 --- a/drivers/net/ethernet/intel/idpf/idpf_lib.c +++ b/drivers/net/ethernet/intel/idpf/idpf_lib.c @@ -1295,6 +1295,7 @@ static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter, vport->compln_clean_budget = IDPF_TX_COMPLQ_CLEAN_BUDGET; vport->default_vport = adapter->num_alloc_vports < idpf_get_default_vports(adapter); + spin_lock_init(&vport->rxq_grps_lock); rsrc = &vport->dflt_qv_rsrc; rsrc->dev = &adapter->pdev->dev; @@ -2097,6 +2098,11 @@ int idpf_initiate_soft_reset(struct idpf_vport *vport, } else { set_bit(IDPF_VPORT_DEL_QUEUES, vport->flags); idpf_vport_stop(vport, false); + + /* Drop the stale pointer in the clone, as rxq_grps was freed + * by idpf_vport_stop(). + */ + new_rsrc->rxq_grps = NULL; } err = idpf_send_add_queues_msg(adapter, vport_config, new_rsrc, diff --git a/drivers/net/ethernet/intel/idpf/idpf_ptp.c b/drivers/net/ethernet/intel/idpf/idpf_ptp.c index 71fe8b2a8b4e..9b15973d2f7e 100644 --- a/drivers/net/ethernet/intel/idpf/idpf_ptp.c +++ b/drivers/net/ethernet/intel/idpf/idpf_ptp.c @@ -348,9 +348,11 @@ idpf_ptp_update_phctime_rxq_grp(const struct idpf_rxq_group *grp, bool split, } } else { for (i = 0; i < grp->splitq.num_rxq_sets; i++) { + if (!grp->splitq.rxq_sets[i]) + continue; + rxq = &grp->splitq.rxq_sets[i]->rxq; - if (rxq) - WRITE_ONCE(rxq->cached_phc_time, systime); + WRITE_ONCE(rxq->cached_phc_time, systime); } } } @@ -387,10 +389,17 @@ static int idpf_ptp_update_cached_phctime(struct idpf_adapter *adapter) struct idpf_q_vec_rsrc *rsrc; bool split; - if (!vport || !vport->dflt_qv_rsrc.rxq_grps) + if (!vport) continue; rsrc = &vport->dflt_qv_rsrc; + + spin_lock(&vport->rxq_grps_lock); + if (!rsrc->rxq_grps) { + spin_unlock(&vport->rxq_grps_lock); + continue; + } + split = idpf_is_queue_model_split(rsrc->rxq_model); for (u16 i = 0; i < rsrc->num_rxq_grp; i++) { @@ -398,6 +407,7 @@ static int idpf_ptp_update_cached_phctime(struct idpf_adapter *adapter) idpf_ptp_update_phctime_rxq_grp(grp, split, systime); } + spin_unlock(&vport->rxq_grps_lock); } return 0; diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.c b/drivers/net/ethernet/intel/idpf/idpf_txrx.c index 281b96daa5c1..aaa3ed3b744b 100644 --- a/drivers/net/ethernet/intel/idpf/idpf_txrx.c +++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.c @@ -1357,16 +1357,15 @@ static void idpf_rxq_sw_queue_rel(struct idpf_rxq_group *rx_qgrp) } /** - * idpf_rxq_group_rel - Release all resources for rxq groups + * idpf_rxq_grps_free - Free an Rx queue group array and everything in it * @rsrc: pointer to queue and vector resources + * @rxq_grps: group array to free, not reachable through @rsrc any more */ -static void idpf_rxq_group_rel(struct idpf_q_vec_rsrc *rsrc) +static void idpf_rxq_grps_free(struct idpf_q_vec_rsrc *rsrc, + struct idpf_rxq_group *rxq_grps) { - if (!rsrc->rxq_grps) - return; - for (unsigned int i = 0; i < rsrc->num_rxq_grp; i++) { - struct idpf_rxq_group *rx_qgrp = &rsrc->rxq_grps[i]; + struct idpf_rxq_group *rx_qgrp = &rxq_grps[i]; u16 num_rxq; if (idpf_is_queue_model_split(rsrc->rxq_model)) { @@ -1387,18 +1386,40 @@ static void idpf_rxq_group_rel(struct idpf_q_vec_rsrc *rsrc) } } } - kfree(rsrc->rxq_grps); + kfree(rxq_grps); +} + +/** + * idpf_rxq_group_rel - Release all resources for rxq groups + * @vport: vport the queue groups belong to + * @rsrc: pointer to queue and vector resources + */ +static void idpf_rxq_group_rel(struct idpf_vport *vport, + struct idpf_q_vec_rsrc *rsrc) +{ + struct idpf_rxq_group *rxq_grps; + + if (!rsrc->rxq_grps) + return; + + spin_lock(&vport->rxq_grps_lock); + rxq_grps = rsrc->rxq_grps; rsrc->rxq_grps = NULL; + spin_unlock(&vport->rxq_grps_lock); + + idpf_rxq_grps_free(rsrc, rxq_grps); } /** * idpf_vport_queue_grp_rel_all - Release all queue groups + * @vport: vport the queue groups belong to * @rsrc: pointer to queue and vector resources */ -static void idpf_vport_queue_grp_rel_all(struct idpf_q_vec_rsrc *rsrc) +static void idpf_vport_queue_grp_rel_all(struct idpf_vport *vport, + struct idpf_q_vec_rsrc *rsrc) { idpf_txq_group_rel(rsrc); - idpf_rxq_group_rel(rsrc); + idpf_rxq_group_rel(vport, rsrc); } /** @@ -1417,7 +1438,7 @@ void idpf_vport_queues_rel(struct idpf_vport *vport, idpf_rx_desc_rel_all(rsrc); idpf_xdpsqs_put(vport); - idpf_vport_queue_grp_rel_all(rsrc); + idpf_vport_queue_grp_rel_all(vport, rsrc); kfree(vport->txqs); vport->txqs = NULL; @@ -1802,18 +1823,19 @@ static int idpf_rxq_group_alloc(struct idpf_vport *vport, u16 num_rxq) { struct idpf_adapter *adapter = vport->adapter; + struct idpf_rxq_group *rxq_grps; bool hs, rsc; - int err = 0; + int err; - rsrc->rxq_grps = kzalloc_objs(struct idpf_rxq_group, rsrc->num_rxq_grp); - if (!rsrc->rxq_grps) + rxq_grps = kzalloc_objs(struct idpf_rxq_group, rsrc->num_rxq_grp); + if (!rxq_grps) return -ENOMEM; hs = idpf_vport_get_hsplit(vport) == ETHTOOL_TCP_DATA_SPLIT_ENABLED; rsc = idpf_is_feature_ena(vport, NETIF_F_GRO_HW); for (unsigned int i = 0; i < rsrc->num_rxq_grp; i++) { - struct idpf_rxq_group *rx_qgrp = &rsrc->rxq_grps[i]; + struct idpf_rxq_group *rx_qgrp = &rxq_grps[i]; rx_qgrp->vport = vport; if (!idpf_is_queue_model_split(rsrc->rxq_model)) { @@ -1914,9 +1936,17 @@ static int idpf_rxq_group_alloc(struct idpf_vport *vport, } } + /* Publish only once every group is built, the PTP aux worker reads + * the array as soon as it becomes visible. + */ + spin_lock(&vport->rxq_grps_lock); + rsrc->rxq_grps = rxq_grps; + spin_unlock(&vport->rxq_grps_lock); + + return 0; + err_alloc: - if (err) - idpf_rxq_group_rel(rsrc); + idpf_rxq_grps_free(rsrc, rxq_grps); return err; } @@ -1947,7 +1977,7 @@ static int idpf_vport_queue_grp_alloc_all(struct idpf_vport *vport, return 0; err_out: - idpf_vport_queue_grp_rel_all(rsrc); + idpf_vport_queue_grp_rel_all(vport, rsrc); return err; } -- 2.47.1