Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6644d143 authored by Dan Carpenter's avatar Dan Carpenter Committed by Greg Kroah-Hartman
Browse files

HID: roccat: add bounds checking in kone_sysfs_write_settings()



[ 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: 14bf62cd ("HID: add driver for Roccat Kone gaming mouse")
Signed-off-by: default avatarDan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 27fd3a2e
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
@@ -297,31 +297,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,