Skip to content

Commit 8ffb820

Browse files
Meng Xugregkh
authored andcommitted
tty: vt: remove multi-fetch, derive font.height from font.data
In con_font_set(), when we need to guess font height (for compat reasons?), the current approach uses multiple userspace fetches, i.e., get_user(tmp, &charmap[32*i+h-1]), to derive the height. This has two drawbacks: 1. performance: accessing userspace memory is less efficient than directly de-reference the byte 2. security: a more critical problem is that the height derived might not match with the actual font.data. This is because a user thread might race condition to change the memory of op->data after the op->height guessing but before the second fetch: font.data = memdup_user(op->data, size). Leaving font.height = 32 while the actual height is 1 or vice-versa. This patch tries to resolve both issues by re-locating the height guessing part after the font.data is fetched in. In this way, the userspace data is fetched in one shot and we directly dereference the font.data in kernel space to probe for the height. Signed-off-by: Meng Xu <mengxu.gatech@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent dd076cf commit 8ffb820

1 file changed

Lines changed: 28 additions & 20 deletions

File tree

‎drivers/tty/vt/vt.c‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,37 +4119,45 @@ static int con_font_set(struct vc_data *vc, struct console_font_op *op)
41194119
return -EINVAL;
41204120
if (op->charcount > 512)
41214121
return -EINVAL;
4122+
if (op->width <= 0 || op->width > 32 || op->height > 32)
4123+
return -EINVAL;
4124+
size = (op->width+7)/8 * 32 * op->charcount;
4125+
if (size > max_font_size)
4126+
return -ENOSPC;
4127+
4128+
font.data = memdup_user(op->data, size);
4129+
if (IS_ERR(font.data))
4130+
return PTR_ERR(font.data);
4131+
41224132
if (!op->height) { /* Need to guess font height [compat] */
41234133
int h, i;
4124-
u8 __user *charmap = op->data;
4125-
u8 tmp;
4126-
4127-
/* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4128-
so that we can get rid of this soon */
4129-
if (!(op->flags & KD_FONT_FLAG_OLD))
4134+
u8 *charmap = font.data;
4135+
4136+
/*
4137+
* If from KDFONTOP ioctl, don't allow things which can be done
4138+
* in userland,so that we can get rid of this soon
4139+
*/
4140+
if (!(op->flags & KD_FONT_FLAG_OLD)) {
4141+
kfree(font.data);
41304142
return -EINVAL;
4143+
}
4144+
41314145
for (h = 32; h > 0; h--)
4132-
for (i = 0; i < op->charcount; i++) {
4133-
if (get_user(tmp, &charmap[32*i+h-1]))
4134-
return -EFAULT;
4135-
if (tmp)
4146+
for (i = 0; i < op->charcount; i++)
4147+
if (charmap[32*i+h-1])
41364148
goto nonzero;
4137-
}
4149+
4150+
kfree(font.data);
41384151
return -EINVAL;
4152+
41394153
nonzero:
41404154
op->height = h;
41414155
}
4142-
if (op->width <= 0 || op->width > 32 || op->height > 32)
4143-
return -EINVAL;
4144-
size = (op->width+7)/8 * 32 * op->charcount;
4145-
if (size > max_font_size)
4146-
return -ENOSPC;
4156+
41474157
font.charcount = op->charcount;
4148-
font.height = op->height;
41494158
font.width = op->width;
4150-
font.data = memdup_user(op->data, size);
4151-
if (IS_ERR(font.data))
4152-
return PTR_ERR(font.data);
4159+
font.height = op->height;
4160+
41534161
console_lock();
41544162
if (vc->vc_mode != KD_TEXT)
41554163
rc = -EINVAL;

0 commit comments

Comments
 (0)