From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49CDA72623 for ; Wed, 29 Jul 2026 03:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785294798; cv=none; b=Wox0cO8fJF1oOMfEnGmf7XvBvLu5s9stYgYaxdH2aP0VSKyh1oF0XXKcOQtyZmt76zljK05BKOy8na0AzcpI4jvTW++9l5daZEczsaVktTDwiZhQn7Q5VN0+2BuhLIs4uDmfUCLeN8Dpq5RDhpmu870+xZkyHzFWu6CBDQVSv54= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785294798; c=relaxed/simple; bh=+nXoE1JWimzLvKI33iYfZhwEL7/w5aGwIMdK9C1RKfs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=TDvLDHYuMvgpZ1yUQvDhvQNdghdPc4INazCKihl9xEUJdV4KzNGHNuFz3B8CIYzaJcTS9J5XAcjboQNbQMore4JcpH7SUszMnzDT5VF6M+wJpCopciQ1RrAAGUTBy7bNmoHt8LUnvwO54BnY36D7MW9kXSg+vf7m3gGmJj+XQWw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cxeAV1MQ; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cxeAV1MQ" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-84862b0d5f8so400753b3a.3 for ; Tue, 28 Jul 2026 20:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785294797; x=1785899597; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=67fzthwbCndyMuuC4hWnqiqs2nS3TAADY1dtDD/lCeQ=; b=cxeAV1MQOkuN73Qvm5DgsYuRuLpc5hymuqmBL+I/UJV/g2z3aUueZuyY3qT9kUj0Ug T61uZl2xrTuBl5rGqVounYcaLNNdZRgmMRcspG/HiZvku5yU5jJw4ZgDUNhr8ERnzCiJ Yw2xuGtjoAzA7XDQc2Kqp1ekZywFKVALAblH0ZOvxiOP6jHreBNCb55IlGdTT1SkB0ey Jlv7FgkJ85NNRnPO0Xf5J76H+roopau5s9CZDXRCWuLRURaPO9/Ri8tgkp8sSwuXvDjw vs9itxlLYJNZ2svZYo3k+GdN2N7Zc9jrx+r+RQQfbVAiR9C2J6y+rTYdGzRdlfLmfQRu MqvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785294797; x=1785899597; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=67fzthwbCndyMuuC4hWnqiqs2nS3TAADY1dtDD/lCeQ=; b=GAdCg92fk8my/78DZe73dXhAteBbLbXZyKzSnC3OwbaS7URW1Gp0d134hPFs2S0zhh 2/Q1Kbuk8b7/SD7w/3fGj8XoZ43dPQEGSBr6dh3kIzCfwKYf8ttj3sclQw4J9RqLlXte eXUx9HLt8wikWaAqC9SJxkZvH1xTp7kXDErSnMccek9Tm7Uhs8FpsQ1CyIRFKNKJeEZR aH0LpY78uGfT1GGbq2fZBR4e6Cd9Z3+pyopmS36Cm/uA/4Ynkz236/mhgtPOLzwH3XmR UOXLNHtli8yE2WJW8KKoTXoFrk/GS+xsUCa+CRvrVNUiY+lnT4k9Nfct3n0SQvUBl4zs B1wQ== X-Forwarded-Encrypted: i=1; AHgh+Rq3x7/kQWoWCaLdYWPb+3rCTNZKe5dpw1KMKV1jdYzzS80XBk24/MrxMdg6PQAjxCzTa97/19vAv+zennI=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7V70anVsJM4NMgcbnq/FkGdiJ7W/o0DaFdPIN6ioqV/lfU+5E eTPx/8XOx30MpECN2bdYKZs70bZFABkZ7N4CUeiwL6V4kuPIeNdIc/A8 X-Gm-Gg: AR+sD13CcaTUorr3wxq6w5KYJFwvM+WrIxz2ABypvQYX7agDJAl6DNVlHZIz49noXN9 Gwu4fqd1etrxMR1nWRdBhHBEXI4luGL7X5XGPpgYzqNKMUgv2ReepVaXeU7eEEBoQ7FurrE53hl vddkh0/mlZubLycya4gpG0mpytJH3HLGhcXoceI2WY9XXXq5uOoszbjmRPkEWjjqaViiwQ7tWA6 ZMjGG6jph2VEFUlbCQOGfw5KMyJrkWYktCoH1nXKoNNU4q+j9EV8dTcDenuAAcAgBuvkN0UXEZ7 6k90J+qQN4NzclQsOWMFpBKgUqbpzMH+Tb2bf36aIw9w5mTy84pGp9cPMUx7JHdKX2HWZ7QVfvc b4oJs9ywecH8WSNiI6Au1GUOHZ2MYCOeMNn4k4KvDSmkQ0x/SInNdcRbvKOiNTv+hBo2WsvEgky UXAdb+GvpXvRUpjmKalYskybpCqeKj1aFuAlcH1PBUejvVzEST/BiEH+DwEsSyu6JyvG1X5nNMr oUSbO888Q== X-Received: by 2002:a05:6a00:3992:b0:848:7f8a:8d0e with SMTP id d2e1a72fcca58-84e932c5f3emr4770346b3a.55.1785294796609; Tue, 28 Jul 2026 20:13:16 -0700 (PDT) Received: from localhost.localdomain ([116.105.227.66]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84e9fe1d07dsm667639b3a.8.2026.07.28.20.13.13 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 28 Jul 2026 20:13:15 -0700 (PDT) From: Yuejie Shi To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Yuejie Shi , stable@vger.kernel.org Subject: [PATCH net] net: pktgen: fix out-of-bounds write in pktgen_finalize_skb() Date: Wed, 29 Jul 2026 11:12:52 +0800 Message-ID: <20260729031252.54476-1-syjcnss@gmail.com> X-Mailer: git-send-email 2.50.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit pktgen_finalize_skb() spreads `datalen` bytes over `frags` page fragments, but the loop that fills skb_shinfo(skb)->frags[] is bounded only by `datalen > 0` -- there is no `i < frags` check. The per-fragment length is computed once with a truncating division: frag_len = min_t(int, datalen / frags, PAGE_SIZE); The code just above has already capped datalen at frags * PAGE_SIZE, so a valid partition does exist, but the truncation means the first frags - 1 fragments can leave more than PAGE_SIZE behind, and the last-fragment branch only consumes min(datalen, PAGE_SIZE). Writing datalen as q * frags + r with q = datalen / frags, the loop overruns whenever q + r > PAGE_SIZE because q + r bytes are still outstanding after frags - 1 iterations and the final fragment can take only PAGE_SIZE of them. The loop then runs once more, with i == frags, and writes one element past the array. frags is clamped to MAX_SKB_FRAGS (17) and frags[] is the last member of struct skb_shared_info, which sits at the very end of the skb head allocation, so writing frags[17] writes 16 bytes past that allocation. With PAGE_SIZE == 4096 every pkt_size in [69675, 69689] hits it (datalen = pkt_size - 58, giving q = 4095 and r >= 2): modprobe pktgen echo "add_device dummy0" > /proc/net/pktgen/kpktgend_0 echo "frags 17" > /proc/net/pktgen/dummy0 echo "pkt_size 69675" > /proc/net/pktgen/dummy0 echo start > /proc/net/pktgen/pgctrl BUG: KASAN: use-after-free in pktgen_finalize_skb+0x1e8/0x4a4 [pktgen] Write of size 8 at addr ffff0000c6b40000 by task kpktgend_0/97 __asan_store8+0xe0/0xe4 pktgen_finalize_skb+0x1e8/0x4a4 [pktgen] pktgen_thread_worker+0x1f3c/0x2d50 [pktgen] kthread+0x1c4/0x1e0 The buggy address belongs to the physical page: page: refcount:0 mapcount:0 mapping:0000000000000000 pfn:0x106b40 nr_frags ends up at 18 as well, so skb_release_data() later walks one entry past the array too. Compute the fragment length with DIV_ROUND_UP() instead. datalen is still <= frags * PAGE_SIZE so the result cannot exceed PAGE_SIZE, but the fragments now cover datalen exactly and the loop always terminates on the last one. As a side effect pktgen stops emitting zero-length fragments when datalen < frags. Bound the loop with `i < frags` as well, so that a future arithmetic mistake cannot walk off the array again. Reaching this needs real root: pktgen creates its /proc/net/pktgen entries with proc_mkdir()/proc_create_data() at mode 0600 owned by global root, so unlike /proc/net itself they are not remapped to the owner of a user namespace, and the module has to be loaded first. It is still a plain heap overflow behind a documented interface. Fixes: 7d36a991e8d3 ("pktgen: create num frags requested") Cc: stable@vger.kernel.org Signed-off-by: Yuejie Shi --- net/core/pktgen.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/core/pktgen.c b/net/core/pktgen.c index 8e185b318288..2dbb7caacd0d 100644 --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -2843,8 +2843,8 @@ static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb, } i = 0; - frag_len = min_t(int, datalen / frags, PAGE_SIZE); - while (datalen > 0) { + frag_len = min_t(int, DIV_ROUND_UP(datalen, frags), PAGE_SIZE); + while (datalen > 0 && i < frags) { if (unlikely(!pkt_dev->page)) { int node = numa_node_id(); -- 2.51.0