Commit a9d39310 authored by Dmitry Torokhov's avatar Dmitry Torokhov Committed by Sasha Levin

tty/vt/keyboard: fix OOB access in do_compute_shiftstate()

[ Upstream commit 510cccb5 ]

The size of individual keymap in drivers/tty/vt/keyboard.c is NR_KEYS,
which is currently 256, whereas number of keys/buttons in input device (and
therefor in key_down) is much larger - KEY_CNT - 768, and that can cause
out-of-bound access when we do

	sym = U(key_maps[0][k]);

with large 'k'.

To fix it we should not attempt iterating beyond smaller of NR_KEYS and
KEY_CNT.

Also while at it let's switch to for_each_set_bit() instead of open-coding
it.
Reported-by: default avatarSasha Levin <sasha.levin@oracle.com>
Reviewed-by: default avatarGuenter Roeck <linux@roeck-us.net>
Cc: stable@vger.kernel.org
Signed-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: default avatarSasha Levin <alexander.levin@verizon.com>
parent e18fa5ad
...@@ -365,23 +365,12 @@ static void to_utf8(struct vc_data *vc, uint c) ...@@ -365,23 +365,12 @@ static void to_utf8(struct vc_data *vc, uint c)
static void do_compute_shiftstate(void) static void do_compute_shiftstate(void)
{ {
unsigned int i, j, k, sym, val; unsigned int k, sym, val;
shift_state = 0; shift_state = 0;
memset(shift_down, 0, sizeof(shift_down)); memset(shift_down, 0, sizeof(shift_down));
for (i = 0; i < ARRAY_SIZE(key_down); i++) { for_each_set_bit(k, key_down, min(NR_KEYS, KEY_CNT)) {
if (!key_down[i])
continue;
k = i * BITS_PER_LONG;
for (j = 0; j < BITS_PER_LONG; j++, k++) {
if (!test_bit(k, key_down))
continue;
sym = U(key_maps[0][k]); sym = U(key_maps[0][k]);
if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK) if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
continue; continue;
...@@ -391,8 +380,7 @@ static void do_compute_shiftstate(void) ...@@ -391,8 +380,7 @@ static void do_compute_shiftstate(void)
val = KVAL(K_SHIFT); val = KVAL(K_SHIFT);
shift_down[val]++; shift_down[val]++;
shift_state |= (1 << val); shift_state |= BIT(val);
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment