Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8224588rwb; Tue, 6 Dec 2022 16:02:23 -0800 (PST) X-Google-Smtp-Source: AA0mqf5AuCiMhby/0r0i8VlMcuRE4zErL9sgybVc5K5vyTuPWK8YakOZmKqs6MOugi5b1SEuE7q7 X-Received: by 2002:a63:1e44:0:b0:478:91e1:ff60 with SMTP id p4-20020a631e44000000b0047891e1ff60mr17565335pgm.206.1670371343604; Tue, 06 Dec 2022 16:02:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670371343; cv=none; d=google.com; s=arc-20160816; b=y3Fn7GkHn2yCJT1zgCA2bwrIa6J0y1IEmYrapZsishiRJ61125A9iRPvVJHltJTT9H 8+TnkqID6W/jpqIcW91k2Jx5IK9SW/nMUmpiV/qkwXrtKRue+e5F/OgmkWNOy6ipXWgk F4KOZWzUVr9IfVrVZ4k64j9jMs5Uyylj12QyueL8yVp4TjaZAZjdF4Djd2wOyyqh70Bh nfmq4eC3D8688e7YbidhcRDP3Han5+d1IN3lu8K6jdnA+0Dkl0pXUWVN+ofp0Y0L67Vf mhBORXr/xyDyMrL8N0nrMCsDaNUZ9EFU1QCo/sQ6Eh1Gt/vhaCmf60AZ6uWmwAW86M87 kKTQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=3vbRaeuklEF43T7OpAaK1TLfMIWz7cNmPbZiH16DjEk=; b=nQtuLDv8iICSzsshNwMKYPbeT1KenlyxIHvl5XAt8/0cPqvtiCtBQHyig3Zl9xtw+6 yOCIYvsATPTgh/L6BOSPiTkQ695VX8evP8jg00wwTAvcuyqSAhzzIwWRg+PwaUgFkYrG 7Pv8eDbGSbMl9F8GaPL8bNUZUeUlZqlzCZfpst8nZf06DfM03+mpTWM5mAHkF4nVHxQJ yWqm5Qwd20I9Lhw4jGzSqZSQl/nuf7eV4iKNlQHsAnEtJ1qjaTFdOEAV/1ygFPzDB8Ue DSZ7Tia+/Wm583v7C8cM3WM9zd5UzkWuLeyCIXpLBB4uvEdpiL2RNJ1eZyMtL8PpOuXu De+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RcfNgmVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n2-20020a170902e54200b001893d5da3cdsi20637355plf.331.2022.12.06.16.02.13; Tue, 06 Dec 2022 16:02:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RcfNgmVF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229769AbiLFXRZ (ORCPT + 77 others); Tue, 6 Dec 2022 18:17:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbiLFXRX (ORCPT ); Tue, 6 Dec 2022 18:17:23 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA42742F41 for ; Tue, 6 Dec 2022 15:17:22 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id a14so11920665pfa.1 for ; Tue, 06 Dec 2022 15:17:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3vbRaeuklEF43T7OpAaK1TLfMIWz7cNmPbZiH16DjEk=; b=RcfNgmVFnKB8u4Z10nfAYSiF9cW9vtwIUqMKJZBCkVHsHejK4CNP4ry8bejkhPODLX dztTcdBeS0CFH5anZgg6xaPMINIXLgJxeamBaKetRXAFZBdOghQurw29Afc71OBqIf9r 0o0nH0fPYw9RhNSRaTlDQFa+nnNyViS67CDWA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3vbRaeuklEF43T7OpAaK1TLfMIWz7cNmPbZiH16DjEk=; b=DF053l1dFKWqlq1uvd8Lzw6q4vEvfnt71hacD+MVhHcZc4pnxCI50W9jDsm7tpRnn3 M9exNArfc0UcieF6Lphj5ypxKKSR0c+jGdvB5j30anKvmHlP0fiX3ddiHHYmIIdg09+X VDhVYnQxYXaK9MNi9BZUV9usECrjoPUYnAadIH6/Fb1a/f2ZThdSrTajIN6q5TeQdSgu 20/ETyJCGgZAHLMP7CZdihZxCFcpU9ZY4jkqvVmJjv/JXlufUu/tk54yC8j+QbkCNo4e y4Y0Mp/9mZrULkb+P1BV9F/8kzbn8J9+9KR54Fg2RoTp2dS8NlCJf9UDF6SNJjZ3V8gI JaSg== X-Gm-Message-State: ANoB5pm2VHAcq4WUD2UBRJ/5LeO8+p8Rx/pMKxksewJeyrZ4GyhXvwDV CcW4ETuY2uDCQZx5rNEA0OByhA== X-Received: by 2002:a63:d149:0:b0:478:dfd4:fc2b with SMTP id c9-20020a63d149000000b00478dfd4fc2bmr4745390pgj.234.1670368642119; Tue, 06 Dec 2022 15:17:22 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id x14-20020a170902a38e00b0017f36638010sm13058718pla.276.2022.12.06.15.17.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 15:17:21 -0800 (PST) From: Kees Cook To: "David S. Miller" Cc: Kees Cook , syzbot+fda18eaa8c12534ccb3b@syzkaller.appspotmail.com, Eric Dumazet , Jakub Kicinski , Paolo Abeni , Pavel Begunkov , pepsipu , Vlastimil Babka , kasan-dev , Andrii Nakryiko , ast@kernel.org, bpf , Daniel Borkmann , Hao Luo , Jesper Dangaard Brouer , John Fastabend , jolsa@kernel.org, KP Singh , martin.lau@linux.dev, Stanislav Fomichev , song@kernel.org, Yonghong Song , netdev@vger.kernel.org, LKML , Menglong Dong , David Ahern , Martin KaFai Lau , Luiz Augusto von Dentz , Richard Gobert , Andrey Konovalov , David Rientjes , linux-hardening@vger.kernel.org Subject: [PATCH] skbuff: Reallocate to ksize() in __build_skb_around() Date: Tue, 6 Dec 2022 15:17:14 -0800 Message-Id: <20221206231659.never.929-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2831; h=from:subject:message-id; bh=SEGHsTZp5rUk6+r1FvL0rwY8bYvfT9UgLniC3mssO5w=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjj81513DL6A5/30d+E/4ZlSiL3Nec8HQn2RQ2kFzs 0hZX4L+JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY4/NeQAKCRCJcvTf3G3AJgEWEA CP3WJylXFSIh5ufMvAJQvK79bcfGQEV+CDUcSNcXTWdDEohBbp8tEFod5Nfj2l3fynK91HxFZsxogF kEnuK8O/7SyKs7X8L7bGXpX9pXLRMweVYccLF5Nwxh4ERV1DNDsQmjZH1uDF7SNi9blnULpHvzg34i Byw6v5p0mynHLS3alptN8Pw1n6DTuP2wYEVV8vkB1gcqBOJ6MRbcCbnshUxezF2lTqORLUeVAKvn1S HhI3U5ydjsp6VsIcFmzIau3s9l5lF+z2D/Hrb28myH81E9/ec8nEljNqB5nwlZHAupJnwLxlBhukCC ILfKn0kAdEZLXwhYqmxctrFDB5aNYK+lcNtF2RKXnZsy0Tr3q7ubILFHbQLBSvEKQ8QfpAvwH4Gby9 BS+ELN1eAQ6WQqraXDr/ox/ZzfOPEk3IjsSY2jJ3UcGtqjz7rE7UXCEaAxTO+CmY+eGit7U/Y1HinQ JwhD/C4H1lNNPpJilKLWGmqOLnOYKkDKGfKWdTmSFJa3sxIlzWz5RlTPRoXPmMamZINipZHKl8BjU+ Ee0qzhGS5qQ8uuefIru++zAjBvy3ww83P/5IaKBqSma+SQkucNI4OEa4nqnSAZGLIwfYXc4sit2pHB WMtK/l1bgCdHLPDCL8cHfNH7f64jUTfmdEtV/urQ91TRLv497uFJ/hH07ZUg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When build_skb() is passed a frag_size of 0, it means the buffer came from kmalloc. In these cases, ksize() is used to find its actual size, but since the allocation may not have been made to that size, actually perform the krealloc() call so that all the associated buffer size checking will be correctly notified. For example, syzkaller reported: BUG: KASAN: slab-out-of-bounds in __build_skb_around+0x235/0x340 net/core/skbuff.c:294 Write of size 32 at addr ffff88802aa172c0 by task syz-executor413/5295 For bpf_prog_test_run_skb(), which uses a kmalloc()ed buffer passed to build_skb(). Reported-by: syzbot+fda18eaa8c12534ccb3b@syzkaller.appspotmail.com Link: https://groups.google.com/g/syzkaller-bugs/c/UnIKxTtU5-0/m/-wbXinkgAQAJ Fixes: 38931d8989b5 ("mm: Make ksize() a reporting-only function") Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Pavel Begunkov Cc: pepsipu Cc: syzbot+fda18eaa8c12534ccb3b@syzkaller.appspotmail.com Cc: Vlastimil Babka Cc: kasan-dev Cc: Andrii Nakryiko Cc: ast@kernel.org Cc: bpf Cc: Daniel Borkmann Cc: Hao Luo Cc: Jesper Dangaard Brouer Cc: John Fastabend Cc: jolsa@kernel.org Cc: KP Singh Cc: martin.lau@linux.dev Cc: Stanislav Fomichev Cc: song@kernel.org Cc: Yonghong Song Cc: netdev@vger.kernel.org Cc: LKML Signed-off-by: Kees Cook --- net/core/skbuff.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 1d9719e72f9d..b55d061ed8b4 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -274,7 +274,23 @@ static void __build_skb_around(struct sk_buff *skb, void *data, unsigned int frag_size) { struct skb_shared_info *shinfo; - unsigned int size = frag_size ? : ksize(data); + unsigned int size = frag_size; + + /* When frag_size == 0, the buffer came from kmalloc, so we + * must find its true allocation size (and grow it to match). + */ + if (unlikely(size == 0)) { + void *resized; + + size = ksize(data); + /* krealloc() will immediate return "data" when + * "ksize(data)" is requested: it is the existing upper + * bounds. As a result, GFP_ATOMIC will be ignored. + */ + resized = krealloc(data, size, GFP_ATOMIC); + if (WARN_ON(resized != data)) + data = resized; + } size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); -- 2.34.1