From patchwork Mon Oct 24 11:31:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 10307 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp656135wru; Mon, 24 Oct 2022 14:17:19 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4MXdtWexFsHGHleIPA8uTTnkjlSWeBZnjr/S5g/ULN6jo5QR/vYHeYOBxirs9W/dzi0V2h X-Received: by 2002:a17:90a:d390:b0:20d:3b10:3811 with SMTP id q16-20020a17090ad39000b0020d3b103811mr77317854pju.211.1666646239484; Mon, 24 Oct 2022 14:17:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666646239; cv=none; d=google.com; s=arc-20160816; b=o4gCSE5VqyW3Owxbkb8FBSEP/nxTxKEPEofJhqalzk+eTsnGpzjo+2vu4zFsLg0uSQ 8915DXdKj1JIFjyHYQh8C6COn9gjwi+BeeAWFGvWwUQxs7M958G03MCsfsdH5UPwHTkp zEreQAQZTgwey3ajEz2AThfG4RuSOoBpoEU7IcgYhM9juwJQdChfpjbuLxx/8Kv3oX0B tEBXA14C/nyhdQqdHgrEjFdoctauxeV39ocjq2NKx4K6+kHjHCwe3Y66goyvyefJEkZ1 ZJmTNtDte/uEX8CkJLHcO/aHdeRbgK3VkH92LCP/6N/tQo6yS5P4Wsp0l+jrAKHNPc2E zxsg== 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=bxTDcXKS0pn9tZJwiRUKP1WfQQsP4TK02gOlpRaE0fs=; b=0TQQHNO1bp4xgPzRDqVdGKqPyO6zDfVct8RooDqc6SqH60wvbJijfs5JY/acurVmbe af83JBOrrgU5eZonjaX9LxeK+gsxEc6JLbtp6C0D0kp0ru8jsRmHyB8IlZLt5PPAHAGM YQhM/KVQckjJE100/I7ka+lq5sxLiJO7wuzBLtOzkW1ssF+K3vm35uKE9m4nIEoR4Sme uLE99MZPLt9AZ0UWgmzFvNJHRAIg1qgMN10ZZamuJTRqZ71rv8CxS3gQELCQuy5z74c8 uYXTys/PIZymZS/g36XExHZtXTTzVg1ldIIbJXIzPbwTkk+PqfmsTqGM4mjca4djr7k3 qXVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=pTmFyIGq; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z17-20020a170902ccd100b001755f43df36si617859ple.479.2022.10.24.14.17.05; Mon, 24 Oct 2022 14:17:19 -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=pTmFyIGq; 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 S233783AbiJXVQP (ORCPT + 99 others); Mon, 24 Oct 2022 17:16:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230199AbiJXVPt (ORCPT ); Mon, 24 Oct 2022 17:15:49 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1178626479D; Mon, 24 Oct 2022 12:21:21 -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 C2322CE1337; Mon, 24 Oct 2022 11:45:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C28FC433D6; Mon, 24 Oct 2022 11:45:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666611915; bh=rWO+9/X2dtZ3MO2fo4XjnIoOCDwe2h4UACGfZFSVtEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pTmFyIGqjStufbMVfAg53CH8fLetVzxVl4kqWQcNRDW//aExbx98pw7y6GZz4PCYw PC6uDXY7V7Iy5PYHHcVspvud04nXgMx4EmkFTDvVv77cOdfF1eUiUpLxRR+SM7DJ5e tDmoSyJSqnMHJF7fSlpiP8cIRD9iLtRn7ivgJ3JI= 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 4.9 148/159] HID: roccat: Fix use-after-free in roccat_read() Date: Mon, 24 Oct 2022 13:31:42 +0200 Message-Id: <20221024112954.829525975@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112949.358278806@linuxfoundation.org> References: <20221024112949.358278806@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747605247463523064?= X-GMAIL-MSGID: =?utf-8?q?1747605247463523064?= 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 76d06cf87b2a..36eee9663962 100644 --- a/drivers/hid/hid-roccat.c +++ b/drivers/hid/hid-roccat.c @@ -260,6 +260,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 */ @@ -279,6 +281,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; }