Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp942675rwi; Wed, 19 Oct 2022 05:03:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4e7IMWQZRo59Ox8ZlkQKjmE3XZd4PdlfnaPIYnvINP8UUGbMmOD3ynoLRuHlDlLqHtQ53j X-Received: by 2002:aa7:dd01:0:b0:45c:6453:d22e with SMTP id i1-20020aa7dd01000000b0045c6453d22emr7057497edv.120.1666180981919; Wed, 19 Oct 2022 05:03:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666180981; cv=none; d=google.com; s=arc-20160816; b=KSL1KtXHAa4YCafzbt29ZLLWkSukGBxQ39wNVc0UGOTs8Ks+yBykbJHIydg4igvZb6 c4QS6V4FJvG87Zy+AKJ+cnpvhknUgJPcXXOArcKemMiqwaeFabEYouPilO2zCJ7WlNGa J0YF5QzdXo7psn7inrUSJZpNKAIvhX+gAQlirN7adNZUEm9oQW4PSgr1KDk/pTU5yfWk yqEI2qKd93QM0gMv+kIC/4tV6JQdGTrzR5e+WAWA9xZF0i25cM4zZt5577qC2jiA2kwk 2zQvgdh8lzcda4MmYy48Jgudg/aDbD4S8U8RkpfNei87wAuuQjgpqxw2IHYUlGJBL5oi V8iA== 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=5P7OA4B75wnBVFRAjCvIgkQqC+x3HUTAZ2cBg5af3nU=; b=KZLNJ6P2D8lSURAhsAynciYRAUc6Q4G3zguf+PvlXzVvVjMATwJpCkGpj1DDPfl7uL ZbnJ5uau7AaJ5hxDWhdc90optDRe5KinCpgv4DuOSlh5esvD3Q4dm9gwIBGstY7350FL exRy+q5/i1keN4Et63+82L5EaAbogQRoKwfAL4HSQAtUVHeMurRwsq5vNiJEoTwcWThU bS5IIhL5S1nkw3SZudSeN9gzxyADVoj04BjlQj1FtY61WybGriNain7Hu7wZeOl1gfSk mLRx3UxW46Ig4qlNHAY1hWdvxvq5BYHNzDxZf9+OhYmtqD/gYEbXtA23408pqWtUfApD fjRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lqhecLvK; 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 mp16-20020a1709071b1000b0078db18d796dsi14288616ejc.982.2022.10.19.05.01.51; Wed, 19 Oct 2022 05:03:01 -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=lqhecLvK; 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 S231376AbiJSLyW (ORCPT + 99 others); Wed, 19 Oct 2022 07:54:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232139AbiJSLxZ (ORCPT ); Wed, 19 Oct 2022 07:53:25 -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 8F33F176524; Wed, 19 Oct 2022 04:32:34 -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 7C52DB824DF; Wed, 19 Oct 2022 09:14:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9290C433C1; Wed, 19 Oct 2022 09:14:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666170861; bh=D8ozL383GaaeAGtKQvIA9IdpVRSMEvGm6dPoBfIkAio=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lqhecLvKnvW+POkwpZbHlta8M/NlzSaKMbJ1CLSni0RyxqylK9HJRNqkqNNpxw9UR iBP9UJq6rA9dTqnb9sA6TIyWPmnC/pvRBGNRO8DKPk6ogsELUcbav3dBkQJ4I4Iizc EFdrc6gxGIwWeMh1lyG1X2cCB+xt6Y7NMkDwXYaI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hyunwoo Kim , Jiri Kosina , Sasha Levin Subject: [PATCH 6.0 813/862] HID: roccat: Fix use-after-free in roccat_read() Date: Wed, 19 Oct 2022 10:35:00 +0200 Message-Id: <20221019083325.847526424@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 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