From patchwork Thu Jun 15 21:08:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 108722 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp918130vqr; Thu, 15 Jun 2023 14:13:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ59Dl2IuL5fl4nJopsBn2CGccCiBAfsq7g8vHb4QJBMdMAz7hcsLNGDdT01gsECy+K/NyDJ X-Received: by 2002:a17:907:d11:b0:96f:9cea:a34d with SMTP id gn17-20020a1709070d1100b0096f9ceaa34dmr6256023ejc.21.1686863585383; Thu, 15 Jun 2023 14:13:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686863585; cv=none; d=google.com; s=arc-20160816; b=zv2zuo2RPmARuqy+sWUgbN0zKT4OzFa54geON2FxOhAHCaVA+ke9o4rXMnK+FQMDmE 1RXLrXSrrS3gmkcjMM3tl8QfpWPLGU3JGeRtS3I8GnCwsV0FkoKclGrzeN+HoCC4UVT2 K8T89is/YbLBZdVtx1XpHpVCXy6nwot0maPcLPFCAfzP7YZr2FpHuZmDmP4CIFVVKRmk hVSDihzay4GqcrRMcYlmFLggOIBku5k17Jmp2oZuematEQrPqsURWHRgvYSp4nb3+e/6 Fqg4xOoHhorinUO688RYW5D7XZkZ714v8lcC9EkhfCGpYXtIWpsVcCHoWwlDTRUvRrs2 w1Ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:content-transfer-encoding :content-id:mime-version:subject:cc:to:from:organization :dkim-signature; bh=mX2XFIZB9S1fYGd2DwrbphZcZCLwFW1XtlbFzEi1ERc=; b=Y9mXd3kSBpW9Twk4Xvm9bS4/J+eqzAbQJwxVvLUR0DhTY+1cMw9JZP7AAQUSyKltuY Iw66xfPfChLXXHpH8J89L3gL9JsiWsi/E4yHEVc5Xt3yrLEZAWj2YOa9DutNHHLwzbBl dhjUjJYlX31Bfla9U8GRdgbl8+bhII2JnbnvX85IPsCrxWAfIfDlwTZVrfFGyt4DYFKY S/mla8UYqys4WhrCydO4GxlpeX1hyJSNQ3rGmSLkugbxHO9y5Jw6aiz1Itfka6lwCcJw Mc615+7YYUD4iKiV0h4pp3xV7GkQdiC85f1m1grr/JSkYJ1cuUvsdTz0E6bCQPP2Ycms 5Ixg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OeYRC6Ba; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v19-20020a170906b01300b009829013a900si1695238ejy.552.2023.06.15.14.12.40; Thu, 15 Jun 2023 14:13:05 -0700 (PDT) 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=@redhat.com header.s=mimecast20190719 header.b=OeYRC6Ba; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233641AbjFOVJ4 (ORCPT + 99 others); Thu, 15 Jun 2023 17:09:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232666AbjFOVJy (ORCPT ); Thu, 15 Jun 2023 17:09:54 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93F48294C for ; Thu, 15 Jun 2023 14:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686863341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mX2XFIZB9S1fYGd2DwrbphZcZCLwFW1XtlbFzEi1ERc=; b=OeYRC6BaKPYbfnWcedBanpIQhwxiRHSKG1Yl4YH4zuc2B9bk0G/sFL3giIT/mAUZ2T0YyI VE+qg35ZAJt3LThdqXvf8PVPsCSPxLai23rWRLJQR5uOOW8ezUWZcx3wUn2WV4vPGkQYHA 3zaaGTeyShLOq7zNIuXwWGm1x9xxMZY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-636-UPUnrRqUNlWJU3sYlyvwog-1; Thu, 15 Jun 2023 17:08:57 -0400 X-MC-Unique: UPUnrRqUNlWJU3sYlyvwog-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 07219185A78F; Thu, 15 Jun 2023 21:08:57 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 80CED140EBB8; Thu, 15 Jun 2023 21:08:55 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells To: netdev@vger.kernel.org cc: dhowells@redhat.com, syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com, Herbert Xu , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Jens Axboe , Matthew Wilcox , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next] crypto: Fix af_alg_sendmsg(MSG_SPLICE_PAGES) sglist limit MIME-Version: 1.0 Content-ID: <322882.1686863334.1@warthog.procyon.org.uk> Date: Thu, 15 Jun 2023 22:08:54 +0100 Message-ID: <322883.1686863334@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 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, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768804670998498144?= X-GMAIL-MSGID: =?utf-8?q?1768804670998498144?= When af_alg_sendmsg() calls extract_iter_to_sg(), it passes MAX_SGL_ENTS as the maximum number of elements that may be written to, but some of the elements may already have been used (as recorded in sgl->cur), so extract_iter_to_sg() may end up overrunning the scatterlist. Fix this to limit the number of elements to "MAX_SGL_ENTS - sgl->cur". Note: It probably makes sense in future to alter the behaviour of extract_iter_to_sg() to stop if "sgtable->nents >= sg_max" instead, but this is a smaller fix for now. The bug causes errors looking something like: BUG: KASAN: slab-out-of-bounds in sg_assign_page include/linux/scatterlist.h:109 [inline] BUG: KASAN: slab-out-of-bounds in sg_set_page include/linux/scatterlist.h:139 [inline] BUG: KASAN: slab-out-of-bounds in extract_bvec_to_sg lib/scatterlist.c:1183 [inline] BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg lib/scatterlist.c:1352 [inline] BUG: KASAN: slab-out-of-bounds in extract_iter_to_sg+0x17a6/0x1960 lib/scatterlist.c:1339 Fixes: bf63e250c4b1 ("crypto: af_alg: Support MSG_SPLICE_PAGES") Reported-by: syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com Link: https://lore.kernel.org/r/000000000000b2585a05fdeb8379@google.com/ Signed-off-by: David Howells Tested-by: syzbot+6efc50cc1f8d718d6cb7@syzkaller.appspotmail.com cc: Herbert Xu cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: linux-crypto@vger.kernel.org cc: netdev@vger.kernel.org Acked-by: Herbert Xu --- crypto/af_alg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/af_alg.c b/crypto/af_alg.c index 7d4b6016b83d..cdb1dcc5dd1a 100644 --- a/crypto/af_alg.c +++ b/crypto/af_alg.c @@ -1043,7 +1043,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, }; plen = extract_iter_to_sg(&msg->msg_iter, len, &sgtable, - MAX_SGL_ENTS, 0); + MAX_SGL_ENTS - sgl->cur, 0); if (plen < 0) { err = plen; goto unlock;