Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp205303rwi; Wed, 12 Oct 2022 18:43:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6e/awpcF9HdvhEXD/AiBXSvDGctn6xNNYF0f0TAEXbzFjkbyvM3Ff+TZbOOZn9Dx2YN/fY X-Received: by 2002:a63:4f19:0:b0:43b:ddc9:387c with SMTP id d25-20020a634f19000000b0043bddc9387cmr27325603pgb.333.1665625395711; Wed, 12 Oct 2022 18:43:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665625395; cv=none; d=google.com; s=arc-20160816; b=E0LnDQq0cTHUj9BzRyey8xn3akgWRDwAtWiuhdhS+TNG16lv4g2kBB8USP4IngXO6J qxdlShUfVOdo+QQKtYI8E58hTm/eGbuPH4zevClivyd+gHlZLDx7DAuaiE1ZKk/8QBNb v2xIlrsm92Nl28FOrRcZWbzPMzpbxxm+dVoSxzTIPBrqOTm8zLCKY77zTn5bSP4128Jt j8r/o1mSyzQCqVLNK50F+owJkPI/1inDXonlyVgCcKeIL855ONBWZnRVdKU9HNvuG94q tmceAi9r0g4D//nV1Ww092ejx7Pd1Q3UfrgB5gIOG2MB2RUFBkohqyjJAMx+Mx/FubyZ J9og== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5P7OA4B75wnBVFRAjCvIgkQqC+x3HUTAZ2cBg5af3nU=; b=jBIIQCXaO0LiufRVsyC61B6RHokbjwEeBfAW8DPW8E1PK1fW1bUHKYsyYGAlcWMXSS H3Yzq3BbqNxX/sH1ysPRblFbfre/5kcece6zAlaLIDOwodOW4VKqHW/gqlFIUPrlzk4w 3KfwKobO4h3nbgtU+INv887Y4ggZRWhwQFU8p7WYScpdPiO9uO/Z/OKOtkwu53kRnWpq CaKI+Qn4lSHekFsq+fGnenGm0FjnKQeinzBJAci0STqmJXclxXjPtgII1qsrdngeRahM ABWw09+NqSp6sXBKyajTH344V6ZCCcYkgwJfoVq5t3Zg3kYFUpomtPVWn/iFnonLrIY9 6/Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=o7Ih4Zll; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 132-20020a63008a000000b00460b3aecba3si15858493pga.542.2022.10.12.18.43.04; Wed, 12 Oct 2022 18:43:15 -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=@kernel.org header.s=k20201202 header.b=o7Ih4Zll; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231803AbiJMA3N (ORCPT + 99 others); Wed, 12 Oct 2022 20:29:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbiJMAZ2 (ORCPT ); Wed, 12 Oct 2022 20:25:28 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F2935FF2; Wed, 12 Oct 2022 17:24:31 -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 40DE5B81CE7; Thu, 13 Oct 2022 00:22:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 168BDC433D6; Thu, 13 Oct 2022 00:22:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665620553; bh=D8ozL383GaaeAGtKQvIA9IdpVRSMEvGm6dPoBfIkAio=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o7Ih4Zllw0t35dI72Y0ouVrT8K48CaARYsfUQZpgVYvh2oYSnJlsyT4lYC2hj2wYp p6obxU278covql/uSbBzUW5/aiBJMx9s7hXUzQO1juFMcibg/UAzEqn0U/H9ib2tan OmRVJRsHnQvfAs1nNZ0YVpiIz3vZDSkO/iSXfs4EveYvyAr6S1tJPt8SD3KFnEVya3 vMfVikgGFLg5cgeAguXgVKQt8NaDfpp0f0RJkL/ePN8IARxic+gQkwBM6SB578tC3d 9jlRyGFhrnop2t7gV9RiQIVhLLpIuKJKSnh8HsbrP39spR3rt0J+dhsTH1Gti5WDN7 eoPVbdo/601AQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Hyunwoo Kim , Jiri Kosina , Sasha Levin , erazor_de@users.sourceforge.net, jikos@kernel.org, benjamin.tissoires@redhat.com, linux-input@vger.kernel.org Subject: [PATCH AUTOSEL 5.15 25/47] HID: roccat: Fix use-after-free in roccat_read() Date: Wed, 12 Oct 2022 20:21:00 -0400 Message-Id: <20221013002124.1894077-25-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013002124.1894077-1-sashal@kernel.org> References: <20221013002124.1894077-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 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: Hyunwoo Kim [ Upstream commit cacdb14b1c8d3804a3a7d31773bc7569837b71a4 ] roccat_report_event() is responsible for registering roccat-related reports in struct roccat_device. int roccat_report_event(int minor, u8 const *data) { struct roccat_device *device; struct roccat_reader *reader; struct roccat_report *report; uint8_t *new_value; device = devices[minor]; new_value = kmemdup(data, device->report_size, GFP_ATOMIC); if (!new_value) return -ENOMEM; report = &device->cbuf[device->cbuf_end]; /* passing NULL is safe */ kfree(report->value); ... The registered report is stored in the struct roccat_device member "struct roccat_report cbuf[ROCCAT_CBUF_SIZE];". If more reports are received than the "ROCCAT_CBUF_SIZE" value, kfree() the saved report from cbuf[0] and allocates a new reprot. Since there is no lock when this kfree() is performed, kfree() can be performed even while reading the saved report. static ssize_t roccat_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) { struct roccat_reader *reader = file->private_data; struct roccat_device *device = reader->device; struct roccat_report *report; ssize_t retval = 0, len; DECLARE_WAITQUEUE(wait, current); mutex_lock(&device->cbuf_lock); ... report = &device->cbuf[reader->cbuf_start]; /* * If report is larger than requested amount of data, rest of report * is lost! */ len = device->report_size > count ? count : device->report_size; if (copy_to_user(buffer, report->value, len)) { retval = -EFAULT; goto exit_unlock; } ... The roccat_read() function receives the device->cbuf report and delivers it to the user through copy_to_user(). If the N+ROCCAT_CBUF_SIZE th report is received while copying of the Nth report->value is in progress, the pointer that copy_to_user() is working on is kfree()ed and UAF read may occur. (race condition) Since the device node of this driver does not set separate permissions, this is not a security vulnerability, but because it is used for requesting screen display of profile or dpi settings, a user using the roccat device can apply udev to this device node or There is a possibility to use it by giving. Signed-off-by: Hyunwoo Kim Signed-off-by: Jiri Kosina Signed-off-by: Sasha Levin --- drivers/hid/hid-roccat.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/hid/hid-roccat.c b/drivers/hid/hid-roccat.c index 26373b82fe81..6da80e442fdd 100644 --- a/drivers/hid/hid-roccat.c +++ b/drivers/hid/hid-roccat.c @@ -257,6 +257,8 @@ int roccat_report_event(int minor, u8 const *data) if (!new_value) return -ENOMEM; + mutex_lock(&device->cbuf_lock); + report = &device->cbuf[device->cbuf_end]; /* passing NULL is safe */ @@ -276,6 +278,8 @@ int roccat_report_event(int minor, u8 const *data) reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE; } + mutex_unlock(&device->cbuf_lock); + wake_up_interruptible(&device->wait); return 0; } -- 2.35.1