Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp907101pxp; Wed, 16 Mar 2022 20:53:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKTGAKMtrDNRiQ9IgWEl3iHkJ2gPSsaBKK2yTUWLWQVfwK4Cc5oBstJcCIEMydMQ9HS/n2 X-Received: by 2002:a63:5747:0:b0:381:54b9:b083 with SMTP id h7-20020a635747000000b0038154b9b083mr2123773pgm.178.1647489197377; Wed, 16 Mar 2022 20:53:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647489197; cv=none; d=google.com; s=arc-20160816; b=WPBjZqalddeSe9ed+Kxn3tU71Dp199vuvAfTRsngSptu888aQkdcCWrI5so/UWhhIY z4IqZ1kIqbHPGGTF+mgU6+CCPX26Kqbfd7HDrN2UXGol1fmDojjoLSAFbdeomYDYAc+g /kEEtbUTWDozPxzd8GuVd3Cc5bHjmSoUo0vw/L9RWvKRUroDA4rM7EWBWwU/75jFnD7T /bhMA48NnHnmrIZF7uwE5citwqMoWHKrfx/FCFEXNnQ4Gd7iAvooT2a465cXiR+Ud8XL 4ZVsm1JcsT8u9T33oNkqMVarzGCyfjU40QbGCDq4lTQAfFJaMPq/Bkurn4g/tnzFdG7g aLzA== 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=dLW+zRh+16OA6o5kAkQviAt/KoFqciLWco8Uf0GQbGyNJligZZwYXzAXmg5LdWS1Xf MD+9zhPjyIFxJ4B01P8qPN5+GqmVs7TFfk+gOVwQS2GVGhJfK+sO54AEIbd0jkbo3v9+ fvg9qV/GcbEpZOAYbwCqYX2aLuzmAJwUz0z+wsMtaix7lZ25vF1Guch7kB4XplGsJ6Pv HHamIEyFg65PmjLnUfJDCwC1Fu2yyHS1D4zJWEUcmYH0WXYfC01fepr1pCYhbP1qDUea dMXC2yK2rYu+94A4tOCsFVxRxacrwc37LvPHHNkK9Xq1noF8RNGxtF5byMm1XbXaCzyZ F2iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UAAxrS2w; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id u12-20020a63454c000000b003816043ef16si942947pgk.267.2022.03.16.20.53.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 20:53:17 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UAAxrS2w; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BCADF7E59D; Wed, 16 Mar 2022 20:40:49 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242792AbiCNMaQ (ORCPT + 99 others); Mon, 14 Mar 2022 08:30:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243921AbiCNMVX (ORCPT ); Mon, 14 Mar 2022 08:21:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C44013CC3; Mon, 14 Mar 2022 05:18:12 -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 dfw.source.kernel.org (Postfix) with ESMTPS id E959E60919; Mon, 14 Mar 2022 12:18:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9629DC340E9; Mon, 14 Mar 2022 12:18:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647260291; bh=blSzkE0diLpAz0nAF+i5aaDVUw3yAONYwdMGUTE07xI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UAAxrS2wXXbApex0hSadKtLgNn/i8/p1OBp6ZkriOTPnLLV8NrZoLhZxruVACBqDC B178riJFbzLBqvllnpzVvcHKockWHcUE0VsbvSy1Aba3FAq23aqphJ3XActVOI+/AS iJ+971rR/S95vjB2OKv4gYSwUIR5CKwto6qOGiyE= 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.16 105/121] watch_queue: Fix filter limit check Date: Mon, 14 Mar 2022 12:54:48 +0100 Message-Id: <20220314112747.043304672@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314112744.120491875@linuxfoundation.org> References: <20220314112744.120491875@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=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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;