Skip to content

Commit 82e61c3

Browse files
Jiri Slabygregkh
authored andcommitted
vt: keyboard, extend func_buf_lock to readers
Both read-side users of func_table/func_buf need locking. Without that, one can easily confuse the code by repeatedly setting altering strings like: while (1) for (a = 0; a < 2; a++) { struct kbsentry kbs = {}; strcpy((char *)kbs.kb_string, a ? ".\n" : "88888\n"); ioctl(fd, KDSKBSENT, &kbs); } When that program runs, one can get unexpected output by holding F1 (note the unxpected period on the last line): . 88888 .8888 So protect all accesses to 'func_table' (and func_buf) by preexisting 'func_buf_lock'. It is easy in 'k_fn' handler as 'puts_queue' is expected not to sleep. On the other hand, KDGKBSENT needs a local (atomic) copy of the string because copy_to_user can sleep. Use already allocated, but unused 'kbs->kb_string' for that purpose. Note that the program above needs at least CAP_SYS_TTY_CONFIG. This depends on the previous patch and on the func_buf_lock lock added in commit 46ca3f7 (tty/vt: fix write/write race in ioctl(KDSKBSENT) handler) in 5.2. Likely fixes CVE-2020-25656. Cc: <stable@vger.kernel.org> Reported-by: Minh Yuan <yuanmingbuaa@gmail.com> Signed-off-by: Jiri Slaby <jslaby@suse.cz> Link: https://lore.kernel.org/r/20201019085517.10176-2-jslaby@suse.cz Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6ca03f9 commit 82e61c3

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

drivers/tty/vt/keyboard.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,13 @@ static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
743743
return;
744744

745745
if ((unsigned)value < ARRAY_SIZE(func_table)) {
746+
unsigned long flags;
747+
748+
spin_lock_irqsave(&func_buf_lock, flags);
746749
if (func_table[value])
747750
puts_queue(vc, func_table[value]);
751+
spin_unlock_irqrestore(&func_buf_lock, flags);
752+
748753
} else
749754
pr_err("k_fn called with value=%d\n", value);
750755
}
@@ -1991,7 +1996,7 @@ int vt_do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm,
19911996
#undef s
19921997
#undef v
19931998

1994-
/* FIXME: This one needs untangling and locking */
1999+
/* FIXME: This one needs untangling */
19952000
int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
19962001
{
19972002
struct kbsentry *kbs;
@@ -2023,10 +2028,14 @@ int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
20232028
switch (cmd) {
20242029
case KDGKBSENT: {
20252030
/* size should have been a struct member */
2026-
unsigned char *from = func_table[i] ? : "";
2031+
ssize_t len = sizeof(user_kdgkb->kb_string);
2032+
2033+
spin_lock_irqsave(&func_buf_lock, flags);
2034+
len = strlcpy(kbs->kb_string, func_table[i] ? : "", len);
2035+
spin_unlock_irqrestore(&func_buf_lock, flags);
20272036

2028-
ret = copy_to_user(user_kdgkb->kb_string, from,
2029-
strlen(from) + 1) ? -EFAULT : 0;
2037+
ret = copy_to_user(user_kdgkb->kb_string, kbs->kb_string,
2038+
len + 1) ? -EFAULT : 0;
20302039

20312040
goto reterr;
20322041
}

0 commit comments

Comments
 (0)