Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5024676ybl; Tue, 14 Jan 2020 02:10:27 -0800 (PST) X-Google-Smtp-Source: APXvYqybbxLYSZXw0amKwujz6nv2lbUJ1yUNxUIsLGXzJONp3yQxK9jW5l9dIglnHRtRgpksGMOE X-Received: by 2002:a05:6830:1042:: with SMTP id b2mr16084428otp.306.1578996627581; Tue, 14 Jan 2020 02:10:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578996627; cv=none; d=google.com; s=arc-20160816; b=yJcS9rgODnlj7LCk3scqjBvzwLsJaNLDKlkc9LHnKk6TYwaSmEAhm7wbPQYTfgNHp9 gYnSmu2lutW4Ocd9cntUfJuHTm0iW6gV7Hl4uY4Myfv3JXD3imc5JdoJxhvy4I6CvZDI OFNZ/S0pSLOi1K6UtXo3tNtyiLllMFAU15srFt9VGk+YcZjWbPgW+GeFMjYprX6R2LRl gU2bWRxoTT0HjhufzJV8p9uMclWTJd9HUIccOJAFrmtLm/tfQu6+8BlNEnpmOM7bdjmY AOk29H8yR+P5tBe1196tKyRVcoqk2yI5aJR+XVcwjZamGyjFSeJ809wWZZIWBd7ADhGF gU0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5ucVJ9oOn80fWG7uDwgdWCT3IDlliDQQT/Cvi+pZVb8=; b=DMVUlE6fdXZFk/OweV04CXMDLc9Bvi8bUMLHtwB0Kqsl1BJBaoyanWx5xVCXgTD6a3 2wy1COFPiGyYwDZJzeeCrePzQp1wx6pkPyeDq156oPCl0u4KxPIE8zTqmRic7RWtXAp4 jIymBbvOX+eL+5DebKJpY6tD3P0yEamKzW+pHPcHD9BMw9YcmTlq67PcrMXSDV4v2oTX 0xvIm+hH6OdoqEQqdXX5L5qvUHLZy4pYIHFwJ+42mx47uJp6T6O3yD9BVvpvPjAfHr3o 0CUFQjPZgQ+eSPBwR/J/OXumEpDOZWu4HlFxVDAsjAH53UIQDi+zEjBJFkK1SBG0Gxt/ +yjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UG4FDY5e; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w202si6986183oiw.194.2020.01.14.02.10.15; Tue, 14 Jan 2020 02:10:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UG4FDY5e; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729274AbgANKIW (ORCPT + 99 others); Tue, 14 Jan 2020 05:08:22 -0500 Received: from mail.kernel.org ([198.145.29.99]:39632 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729416AbgANKIU (ORCPT ); Tue, 14 Jan 2020 05:08:20 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 65AF7207FF; Tue, 14 Jan 2020 10:08:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578996500; bh=Zkne0qlY6NJIT3bKQZGPcYYlFRgfrwxG1in28LCITt0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UG4FDY5eeU4TQTFWEu9Ii1E/2lPuDJSikkEgzMWaFEpWTymYo99xQ5jzRjuk8G06B NiDv8eDWjMm383/psVMGNlrLQC9g4B3/LV/varVunP7Bx0n67ctI717+NK/WwQA+/Y lScQXAJUZvAl8AHnLV51YEzk7mSohD1qHHhOgdUY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+c769968809f9359b07aa@syzkaller.appspotmail.com, syzbot+76f3a30e88d256644c78@syzkaller.appspotmail.com, Dmitry Torokhov Subject: [PATCH 4.19 14/46] Input: add safety guards to input_set_keycode() Date: Tue, 14 Jan 2020 11:01:31 +0100 Message-Id: <20200114094343.435861738@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200114094339.608068818@linuxfoundation.org> References: <20200114094339.608068818@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dmitry Torokhov commit cb222aed03d798fc074be55e59d9a112338ee784 upstream. If we happen to have a garbage in input device's keycode table with values too big we'll end up doing clear_bit() with offset way outside of our bitmaps, damaging other objects within an input device or even outside of it. Let's add sanity checks to the returned old keycodes. Reported-by: syzbot+c769968809f9359b07aa@syzkaller.appspotmail.com Reported-by: syzbot+76f3a30e88d256644c78@syzkaller.appspotmail.com Link: https://lore.kernel.org/r/20191207212757.GA245964@dtor-ws Signed-off-by: Dmitry Torokhov Signed-off-by: Greg Kroah-Hartman --- drivers/input/input.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -858,16 +858,18 @@ static int input_default_setkeycode(stru } } - __clear_bit(*old_keycode, dev->keybit); - __set_bit(ke->keycode, dev->keybit); - - for (i = 0; i < dev->keycodemax; i++) { - if (input_fetch_keycode(dev, i) == *old_keycode) { - __set_bit(*old_keycode, dev->keybit); - break; /* Setting the bit twice is useless, so break */ + if (*old_keycode <= KEY_MAX) { + __clear_bit(*old_keycode, dev->keybit); + for (i = 0; i < dev->keycodemax; i++) { + if (input_fetch_keycode(dev, i) == *old_keycode) { + __set_bit(*old_keycode, dev->keybit); + /* Setting the bit twice is useless, so break */ + break; + } } } + __set_bit(ke->keycode, dev->keybit); return 0; } @@ -923,9 +925,13 @@ int input_set_keycode(struct input_dev * * Simulate keyup event if keycode is not present * in the keymap anymore */ - if (test_bit(EV_KEY, dev->evbit) && - !is_event_supported(old_keycode, dev->keybit, KEY_MAX) && - __test_and_clear_bit(old_keycode, dev->key)) { + if (old_keycode > KEY_MAX) { + dev_warn(dev->dev.parent ?: &dev->dev, + "%s: got too big old keycode %#x\n", + __func__, old_keycode); + } else if (test_bit(EV_KEY, dev->evbit) && + !is_event_supported(old_keycode, dev->keybit, KEY_MAX) && + __test_and_clear_bit(old_keycode, dev->key)) { struct input_value vals[] = { { EV_KEY, old_keycode, 0 }, input_value_sync