Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3497457pxp; Mon, 14 Mar 2022 22:36:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2ni2F6dcHrFyipz4JIm7wLPK4En7MPVilHP0cA+c1NW9eSFNgxSPHILlV9ijamce4mRk0 X-Received: by 2002:a17:902:8c81:b0:153:4a6a:52c8 with SMTP id t1-20020a1709028c8100b001534a6a52c8mr13990127plo.120.1647322614339; Mon, 14 Mar 2022 22:36:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647322614; cv=none; d=google.com; s=arc-20160816; b=kcB7a7+hzDkSp7UHbX4fpulE+2dO+SbnaPVLa/FNKDNxc2VDlfwSHSgevnct1xcx/2 7Nwkz83x7qxdIPmSqICN1z0ov3uZjfGP2o7aFNcN8nDFAusO/FTRyQ3HkKdfG133Ah6O 2Vx6/CG6PvRbX9vFmPeYlyFm+JM7nhRP+lMZR+047l79EE52W+HATHq+B7p3NS4ni8JN NyQVmJ3nhvR2Ex2aQKD0zOHXBM+3LCZQ2ZGQgeY1p4BFod8//fCeiWDTtHteGbbduWAM t7l8e04Sks5zq/XvmfzaSbXGqnUjwKvClKhF024OHokqj4VFBHlIMlRWOechbxTQtnbD /Smw== 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=FW+SX282n1o5GkdvqzfsMiLgwZBQjbN+LRQ1CI/hrl8=; b=tj0DQSp0pQDQd/EoSMeelsdmP8ZPaby+FVrxgkZ06YQRYTwRbBwI1Jyb74isu6hW2Y 43mAVkCwMETfhR8o6KMdBxokMhF3e/BLdzBLtdFxmtFvp1BeQ8PN7jC0rNHM6GJYJ7va pf6eu+DMtT0FJdzVS0G/mH+PD83JSZU9EKWsUSCaOxfnPs2j+XIWYdLxy92j/RORVDi7 EvC4abPV+Qhf4LAf1o48lVr0M7+h5opMpEC4OSVA0Hz5MdQL8C1Y2vYe47PH8tjuj3/0 2BQ0g9UH+8ONa+UMVuw0Rglzem9+vER7yrt6Bzy4WSL8n2ZjO0FJ0Zk8b6mZ8V5HdXj+ t8AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FoxgLUaj; 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 j71-20020a63804a000000b0038100100109si11872535pgd.35.2022.03.14.22.36.40; Mon, 14 Mar 2022 22:36:54 -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=FoxgLUaj; 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 S241466AbiCNMN1 (ORCPT + 99 others); Mon, 14 Mar 2022 08:13:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242888AbiCNMLN (ORCPT ); Mon, 14 Mar 2022 08:11:13 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0863033A1D; Mon, 14 Mar 2022 05:10:04 -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 ams.source.kernel.org (Postfix) with ESMTPS id AC885B80DF6; Mon, 14 Mar 2022 12:10:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA970C340EC; Mon, 14 Mar 2022 12:10:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647259801; bh=blSzkE0diLpAz0nAF+i5aaDVUw3yAONYwdMGUTE07xI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FoxgLUajrnwCjuXTqRkn1JOj1FmJfU7Q2+0Cw+SafznZvagWFbBaH9lqRQR/zpqE6 zUbLmD2zWRY7wEY/LHidLqRuRmVRxPnZrHrsVVyuGDHCh9H1rM3zFOMGeLtmBeHIxP Iw94Y3LHI5sfgD7MQQoXEd7afmeLT2W+SYn9q0Us= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , David Howells , Linus Torvalds Subject: [PATCH 5.15 093/110] watch_queue: Fix filter limit check Date: Mon, 14 Mar 2022 12:54:35 +0100 Message-Id: <20220314112745.624808982@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314112743.029192918@linuxfoundation.org> References: <20220314112743.029192918@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=-8.6 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 commit c993ee0f9f81caf5767a50d1faeba39a0dc82af2 upstream. In watch_queue_set_filter(), there are a couple of places where we check that the filter type value does not exceed what the type_filter bitmap can hold. One place calculates the number of bits by: if (tf[i].type >= sizeof(wfilter->type_filter) * 8) which is fine, but the second does: if (tf[i].type >= sizeof(wfilter->type_filter) * BITS_PER_LONG) which is not. This can lead to a couple of out-of-bounds writes due to a too-large type: (1) __set_bit() on wfilter->type_filter (2) Writing more elements in wfilter->filters[] than we allocated. Fix this by just using the proper WATCH_TYPE__NR instead, which is the number of types we actually know about. The bug may cause an oops looking something like: BUG: KASAN: slab-out-of-bounds in watch_queue_set_filter+0x659/0x740 Write of size 4 at addr ffff88800d2c66bc by task watch_queue_oob/611 ... Call Trace: dump_stack_lvl+0x45/0x59 print_address_description.constprop.0+0x1f/0x150 ... kasan_report.cold+0x7f/0x11b ... watch_queue_set_filter+0x659/0x740 ... __x64_sys_ioctl+0x127/0x190 do_syscall_64+0x43/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae Allocated by task 611: kasan_save_stack+0x1e/0x40 __kasan_kmalloc+0x81/0xa0 watch_queue_set_filter+0x23a/0x740 __x64_sys_ioctl+0x127/0x190 do_syscall_64+0x43/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae The buggy address belongs to the object at ffff88800d2c66a0 which belongs to the cache kmalloc-32 of size 32 The buggy address is located 28 bytes inside of 32-byte region [ffff88800d2c66a0, ffff88800d2c66c0) Fixes: c73be61cede5 ("pipe: Add general notification queue support") Reported-by: Jann Horn Signed-off-by: David Howells Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- include/linux/watch_queue.h | 3 ++- kernel/watch_queue.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -28,7 +28,8 @@ struct watch_type_filter { struct watch_filter { union { struct rcu_head rcu; - unsigned long type_filter[2]; /* Bitmask of accepted types */ + /* Bitmask of accepted types */ + DECLARE_BITMAP(type_filter, WATCH_TYPE__NR); }; u32 nr_filters; /* Number of filters */ struct watch_type_filter filters[]; --- a/kernel/watch_queue.c +++ b/kernel/watch_queue.c @@ -320,7 +320,7 @@ long watch_queue_set_filter(struct pipe_ tf[i].info_mask & WATCH_INFO_LENGTH) goto err_filter; /* Ignore any unknown types */ - if (tf[i].type >= sizeof(wfilter->type_filter) * 8) + if (tf[i].type >= WATCH_TYPE__NR) continue; nr_filter++; } @@ -336,7 +336,7 @@ long watch_queue_set_filter(struct pipe_ q = wfilter->filters; for (i = 0; i < filter.nr_filters; i++) { - if (tf[i].type >= sizeof(wfilter->type_filter) * BITS_PER_LONG) + if (tf[i].type >= WATCH_TYPE__NR) continue; q->type = tf[i].type;