Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp689798pxb; Tue, 5 Apr 2022 18:55:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyVRiJ01hZkPIWSoZXwkVeQ8p+QYaEfSAa+itBxPzKJPIl5AVK6V3Sbit4Tkot0IBDmsaOU X-Received: by 2002:a17:90b:350e:b0:1c6:cd4e:303a with SMTP id ls14-20020a17090b350e00b001c6cd4e303amr7158767pjb.141.1649210003630; Tue, 05 Apr 2022 18:53:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649210003; cv=none; d=google.com; s=arc-20160816; b=GgpG2ZvGySdQEES8qf/gpKzbiKMumZjfT8FTFQ0MiqtUF11NHdVn937bvBiOndFMfQ XtfHNqLsAZsm1Wbb/AOyl+8eoUhupNAgZzTHeT/muCMJujy/3QUrMjkExz/EaEk8XrM9 f/fAM1Uu2bAWvZtcdgKXJVamqZ5u/BcyDL1qqeXP+w8B+DSACFePh9vYgUi+Aq4CU/f1 EN5plLD1fTMEyO38HZBFAGioQYK8/uxUw2uyWXx7vTT904UEtM9/jp7Y9EiuDOTpQOpk mB3vlms3gT0yj0d2/kF9jfz9C9PDGgRo0v57tinfpErq7mQFESBBr2z9DRa8DeI/OcYR 4Bfg== 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=Qnj/+YRfLjGdpgnO4uWa7x2MVCl+vbHm8WrYtPfAgl4=; b=orx6+4wHP8JL+srJI5GSeKbmrOfjB1TSRImVJTaohNz4tHnLTJU+CNG6B/lMdMFkvm 186Jh33qwikCzBRkwaB8fWVrEsx17qo8lF7uEtzj98qZF6gQEmhpFq4s813CHPXNddm8 gEcFWeyJ4WzZ8gE/dHAlf+B+qREyemVYqjtcB3vxnsz6oGkPy3/xPa/+U/qhmxMZTiRc N9t05Wf7oBcKjCogZrLoN6zkkJBl9MDzbk2Oqjsk4UdBSCUWYrrqKSuWTQ/VS36PlO07 fI6frlagojukH3jgsmkVxwcZsRam720V/MFlf3VjU0I9N3nTS7Z/jfZQzePhmqTWkofq 7WRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Kl3f3cOz; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f11-20020a170902ce8b00b00153b2d1653dsi14719727plg.325.2022.04.05.18.53.08; Tue, 05 Apr 2022 18:53:23 -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=@linuxfoundation.org header.s=korg header.b=Kl3f3cOz; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378780AbiDELir (ORCPT + 99 others); Tue, 5 Apr 2022 07:38:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244721AbiDEIwf (ORCPT ); Tue, 5 Apr 2022 04:52:35 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBA901AF24; Tue, 5 Apr 2022 01:42:48 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 4CCC8CE1C2B; Tue, 5 Apr 2022 08:42:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69F48C385A1; Tue, 5 Apr 2022 08:42:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649148165; bh=o27kHmOtOvotRFc51Tl3DglrijIVdbuDH53J86XD31o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kl3f3cOzSzmvjGJFE7cHgDwVNAVfd6JsTiYdq1s1qpzdT249Ef1js/pxeLGof0es+ AgE+E5ZCG6ezdYam6cSu4iF8MFSunuTLwfN5K5pN9SiWbtwWoTIqQUgt6EnyM4qZnl Gn08RYOa+GiwG24U5uuSy5zzOhYw+P/MO7sYT2V8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Howells , "Fabio M. De Francesco" , Sasha Levin , syzbot+d55757faa9b80590767b@syzkaller.appspotmail.com Subject: [PATCH 5.16 0257/1017] watch_queue: Fix NULL dereference in error cleanup Date: Tue, 5 Apr 2022 09:19:30 +0200 Message-Id: <20220405070401.892438714@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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 From: David Howells [ Upstream commit a635415a064e77bcfbf43da413fd9dfe0bbed9cb ] In watch_queue_set_size(), the error cleanup code doesn't take account of the fact that __free_page() can't handle a NULL pointer when trying to free up buffer pages that did get allocated. Fix this by only calling __free_page() on the pages actually allocated. Without the fix, this can lead to something like the following: BUG: KASAN: null-ptr-deref in __free_pages+0x1f/0x1b0 mm/page_alloc.c:5473 Read of size 4 at addr 0000000000000034 by task syz-executor168/3599 ... Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 __kasan_report mm/kasan/report.c:446 [inline] kasan_report.cold+0x66/0xdf mm/kasan/report.c:459 check_region_inline mm/kasan/generic.c:183 [inline] kasan_check_range+0x13d/0x180 mm/kasan/generic.c:189 instrument_atomic_read include/linux/instrumented.h:71 [inline] atomic_read include/linux/atomic/atomic-instrumented.h:27 [inline] page_ref_count include/linux/page_ref.h:67 [inline] put_page_testzero include/linux/mm.h:717 [inline] __free_pages+0x1f/0x1b0 mm/page_alloc.c:5473 watch_queue_set_size+0x499/0x630 kernel/watch_queue.c:275 pipe_ioctl+0xac/0x2b0 fs/pipe.c:632 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:874 [inline] __se_sys_ioctl fs/ioctl.c:860 [inline] __x64_sys_ioctl+0x193/0x200 fs/ioctl.c:860 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae Fixes: c73be61cede5 ("pipe: Add general notification queue support") Reported-and-tested-by: syzbot+d55757faa9b80590767b@syzkaller.appspotmail.com Signed-off-by: David Howells Reviewed-by: Fabio M. De Francesco Signed-off-by: Sasha Levin --- kernel/watch_queue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c index 055bc20ecdda..12348b41d7ad 100644 --- a/kernel/watch_queue.c +++ b/kernel/watch_queue.c @@ -274,7 +274,7 @@ long watch_queue_set_size(struct pipe_inode_info *pipe, unsigned int nr_notes) return 0; error_p: - for (i = 0; i < nr_pages; i++) + while (--i >= 0) __free_page(pages[i]); kfree(pages); error: -- 2.34.1