Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tty,vcs: lseek/VC-release race fix

there's a race between vcs's lseek handler and VC release.

The lseek handler does not hold console_lock and touches
VC's size info. If during this the VC got released, there's
an access violation.

Following program triggers the issue for me:

[SNIP]
#define _BSD_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <unistd.h>
#include <errno.h>

static int run_seek(void)
{
while(1) {
int fd;
fd = open("./vcs30", O_RDWR);
while(lseek(fd, 0, 0) != -1);
close(fd);
}
}

static int open_ioctl_tty(void)
{
return open("/dev/tty1", O_RDWR);
}

static int do_ioctl(int fd, int req, int i)
{
return ioctl(fd, req, i);
}

#define INIT(i) do_ioctl(ioctl_fd, VT_ACTIVATE, i)
#define SHUT(i) do_ioctl(ioctl_fd, VT_DISALLOCATE, i)

int main(int argc, char **argv)
{
int ioctl_fd = open_ioctl_tty();

if (ioctl < 0) {
perror("open tty1 failed\n");
return -1;
}

if ((-1 == mknod("vcs30", S_IFCHR|0666, makedev(7, 30))) &&
(errno != EEXIST)) {
printf("errno %d\n", errno);
perror("failed to create vcs30");
return -1;
}

do_ioctl(ioctl_fd, VT_LOCKSWITCH, 0);

if (!fork())
run_seek();

while(1) {
INIT(30);
SHUT(30);
}

return 0;
}
[SNIP]

Signed-off-by: Jiri Olsa <jolsa@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Jiri Olsa and committed by
Greg Kroah-Hartman
dc1892c4 1ffdda95

+18
+18
drivers/tty/vt/vc_screen.c
··· 159 159 int size; 160 160 161 161 mutex_lock(&con_buf_mtx); 162 + console_lock(); 162 163 size = vcs_size(file->f_path.dentry->d_inode); 164 + console_unlock(); 165 + if (size < 0) { 166 + mutex_unlock(&con_buf_mtx); 167 + return size; 168 + } 163 169 switch (orig) { 164 170 default: 165 171 mutex_unlock(&con_buf_mtx); ··· 243 237 * could sleep. 244 238 */ 245 239 size = vcs_size(inode); 240 + if (size < 0) { 241 + if (read) 242 + break; 243 + ret = size; 244 + goto unlock_out; 245 + } 246 246 if (pos >= size) 247 247 break; 248 248 if (count > size - pos) ··· 448 436 * Return data written up to now on failure. 449 437 */ 450 438 size = vcs_size(inode); 439 + if (size < 0) { 440 + if (written) 441 + break; 442 + ret = size; 443 + goto unlock_out; 444 + } 451 445 if (pos >= size) 452 446 break; 453 447 if (this_round > size - pos)