Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5026123ybl; Tue, 14 Jan 2020 02:11:53 -0800 (PST) X-Google-Smtp-Source: APXvYqxO0rwwMiwUV7fMPIAfQ/AvfvmaCBdgWeYTQSTsYxmR5h10kpWjKGiQcRxtMHFtGkxDA7j/ X-Received: by 2002:a05:6830:1d7a:: with SMTP id l26mr15516802oti.138.1578996713779; Tue, 14 Jan 2020 02:11:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578996713; cv=none; d=google.com; s=arc-20160816; b=mfoBeF5c/gIEQr0cFE4RBh1JatIdklHVSWkSvhMzYCdslejJff30yXVwb6vHINOTpI NheXJIvPWBb8OeMeYRyptn7DBvpLslrCXGZEMRhl1EugMrNAg0lVKayzBCYuiTx1Hifa wfoSP9ebzebqeZsIOkvDRkNhnOfkPRY3IHequyS1uI9kP1dKPSOkmVk8RjB6b8YmsxX9 IjVJ+TfU+7CJ1hTM9RR3nttxsGRMgvcIxatsLGlQRgV2WDg0bQe0RPmvLikZ1kaXPssU p6xQ1ecrptQ1nljgIn9UPw/qCRYhnJG/QbYLAweU5/Mk4jQ3P1Fw2hkTQPQV/sCJDKES yk2g== 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=ZoIqXCjtG2buiQjGKDiOy/1xUgYsV5uPooPJcmlFRAsxNXHQX9G9n5I1oxzZqSwLSf CMrx73DDkhYgpPh4dRe4jtU9FYIQEGwZkm51wGX25R9jWgjcmvFgPo24RmuflqelIRKe T7rX19uoYWKGPXWOBxY4HXbXSPmnsoGIfnOUE+7gIBXetkOZthZ4xHyZ5Lnhe4gqA78U tKjB4tl4aWZaVPl5kt1jnVWxfFaTjaLVqfq+TyhCWi6GkqQOVOgPJahbhg0/cHQzVmfj Hq2+8F4naH4+Vm6U68EkyFg8Ap6YA4akbOznjaJTDfrKzUj2I+2z0/M5LXj+ohe8bfBl 6CqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VuFS4h1d; 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 v25si6124236ote.90.2020.01.14.02.11.43; Tue, 14 Jan 2020 02:11:53 -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=VuFS4h1d; 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 S1730937AbgANKJw (ORCPT + 99 others); Tue, 14 Jan 2020 05:09:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:43114 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731015AbgANKJu (ORCPT ); Tue, 14 Jan 2020 05:09:50 -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 2C31A24676; Tue, 14 Jan 2020 10:09:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578996589; bh=Zkne0qlY6NJIT3bKQZGPcYYlFRgfrwxG1in28LCITt0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VuFS4h1dokh8pk7/etWWcWfKRrrQ2vquVwl3y/9C9ikzCThi9UxqA3VumBkiu5k3l j2MqX14Pf9PIVLRcWt/eQapd2VRJsdI96wUBxqpfF1a/frp96z1R6UmwEE5YkmbbpP qZhGAHJ3CWEvtVAj4By82J2teNTiYbQodVMlMuKM= 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.14 22/39] Input: add safety guards to input_set_keycode() Date: Tue, 14 Jan 2020 11:01:56 +0100 Message-Id: <20200114094343.920636759@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200114094336.210038037@linuxfoundation.org> References: <20200114094336.210038037@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