Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp1110812pxx; Tue, 27 Oct 2020 08:29:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzlEw5YCpjxN3/UONVoz1tGfM84aBR/i+UJtSoyutZ/5Uv/aOYw1P8MEi2ORdSWuYVk6jo6 X-Received: by 2002:a17:906:139a:: with SMTP id f26mr2910556ejc.472.1603812558998; Tue, 27 Oct 2020 08:29:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603812558; cv=none; d=google.com; s=arc-20160816; b=b3RXJE7VmFpHTOo1uosLavX+23R0Asqtst4gTf0/YWnNwQjVNFVd09Fzi6ampN0OXK SZ24ZEgrvYAVA9tnicA0Ie5yj4T9OI28RQubcMVc1D/TRHl/EYq1BGOEKePdM4A3etjI cgC+E4bUhFsbxpCX4zpFZUhcL0kdpJQgO2kLkLHm/zGwNm+aXZ1IJL2voTRObMg8xpSN U2ots6VKeoRu/pK30Wwxiy+PbwIdzEpAp0nZ9P0RBmde3mDzm2E5i5AfE2kawPnu11Cq btrdnlpalqSvFz7mTJWU4zvT8faL7i/b121IktN995uMQ4TgXtscffJBEBxQI/80cL0t F8vw== 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=f5+2gangxtSfx43dEdkNkLqlclpyL65xiVv+Iz6xhCE=; b=Xxt6WEM0tIqZU+2+6Q8kxPykwtqTk0rtsiktspqilJBMUsMOCZbL8W/JAxhfDi6KoO 3I3JqLm/OUO77ENdZ2mG3LxJSptsZLUW5YfhC0U0UhId8LXKkEIOa5D8rE8dts7UwYii 67Nwcz9KS7Qx+4zebW1x/Q+hqYXx4uLp7QA9FlQucLDsxKcvuJVZhDdDAKFv3KclKFhh YSkd6+rOcjr97kbuHMzQrc8Jm96up5VlwD+caO0UAlryCqjjq/99Zajjz6tWRbI+tp04 +cb5e3bHb7Q+e7OdXsi1dm7EwyBNmRNMlkyGRLQKWVNEbRuoiIAKTGLo02bLbFZZaDx2 CSDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IXGnIyOs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bi17si1203600edb.582.2020.10.27.08.28.56; Tue, 27 Oct 2020 08:29:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IXGnIyOs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760617AbgJ0Of3 (ORCPT + 99 others); Tue, 27 Oct 2020 10:35:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:33474 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760327AbgJ0Oek (ORCPT ); Tue, 27 Oct 2020 10:34:40 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 41E91207BB; Tue, 27 Oct 2020 14:34:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603809278; bh=uNmru5R0McIAqb9hfvtQsK/oLYb6tyNGhNhQux/IUJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IXGnIyOsCpBBOWhjGXqKLFOMBBZzuGuerQvP04sx+JkuOtRlSejbhdyVylibtkfIn iId81kWnLa7Keonnbxi5+hkPEGqJDp4sG116svpFt7aPgmKFUpK0gu1yXhNIC1Oc01 Z8u9nJ1WkDDqHem16e5FtSlD0SWGluiXZezUVgcA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Jiri Kosina , Sasha Levin Subject: [PATCH 5.4 139/408] HID: roccat: add bounds checking in kone_sysfs_write_settings() Date: Tue, 27 Oct 2020 14:51:17 +0100 Message-Id: <20201027135501.548173207@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135455.027547757@linuxfoundation.org> References: <20201027135455.027547757@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dan Carpenter [ Upstream commit d4f98dbfe717490e771b6e701904bfcf4b4557f0 ] This code doesn't check if "settings->startup_profile" is within bounds and that could result in an out of bounds array access. What the code does do is it checks if the settings can be written to the firmware, so it's possible that the firmware has a bounds check? It's safer and easier to verify when the bounds checking is done in the kernel. Fixes: 14bf62cde794 ("HID: add driver for Roccat Kone gaming mouse") Signed-off-by: Dan Carpenter Signed-off-by: Jiri Kosina Signed-off-by: Sasha Levin --- drivers/hid/hid-roccat-kone.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/hid/hid-roccat-kone.c b/drivers/hid/hid-roccat-kone.c index 1a6e600197d0b..509b9bb1362cb 100644 --- a/drivers/hid/hid-roccat-kone.c +++ b/drivers/hid/hid-roccat-kone.c @@ -294,31 +294,40 @@ static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj, struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); int retval = 0, difference, old_profile; + struct kone_settings *settings = (struct kone_settings *)buf; /* I need to get my data in one piece */ if (off != 0 || count != sizeof(struct kone_settings)) return -EINVAL; mutex_lock(&kone->kone_lock); - difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings)); + difference = memcmp(settings, &kone->settings, + sizeof(struct kone_settings)); if (difference) { - retval = kone_set_settings(usb_dev, - (struct kone_settings const *)buf); - if (retval) { - mutex_unlock(&kone->kone_lock); - return retval; + if (settings->startup_profile < 1 || + settings->startup_profile > 5) { + retval = -EINVAL; + goto unlock; } + retval = kone_set_settings(usb_dev, settings); + if (retval) + goto unlock; + old_profile = kone->settings.startup_profile; - memcpy(&kone->settings, buf, sizeof(struct kone_settings)); + memcpy(&kone->settings, settings, sizeof(struct kone_settings)); kone_profile_activated(kone, kone->settings.startup_profile); if (kone->settings.startup_profile != old_profile) kone_profile_report(kone, kone->settings.startup_profile); } +unlock: mutex_unlock(&kone->kone_lock); + if (retval) + return retval; + return sizeof(struct kone_settings); } static BIN_ATTR(settings, 0660, kone_sysfs_read_settings, -- 2.25.1