android_kernel_xiaomi_sm8350/drivers
Aristeu Sergio Rozanski Filho 5a39e8c6d6 [PATCH] tty_io: fix race in master pty close/slave pty close path
This patch fixes a possible race that leads to double freeing an idr index.
 When the master begin to close, release_dev() is called and then
pty_close() is called:

        if (tty->driver->close)
                tty->driver->close(tty, filp);

This is done without helding any locks other than BKL.  Inside pty_close(),
being a master close, the devpts entry will be removed:

#ifdef CONFIG_UNIX98_PTYS
                if (tty->driver == ptm_driver)
                        devpts_pty_kill(tty->index);
#endif

But devpts_pty_kill() will call get_node() that may sleep while waiting for
&devpts_root->d_inode->i_sem.  When this happens and the slave is being
opened, tty_open() just found the driver and index:

        driver = get_tty_driver(device, &index);
        if (!driver) {
                mutex_unlock(&tty_mutex);
                return -ENODEV;
        }

This part of the code is already protected under tty_mute.  The problem is
that the slave close already got an index.  Then init_dev() is called and
blocks waiting for the same &devpts_root->d_inode->i_sem.

When the master close resumes, it removes the devpts entry, and the
relation between idr index and the tty is gone.  The master then sleeps
waiting for the tty_mutex on release_dev().

Slave open resumes and found no tty for that index.  As result, a NULL tty
is returned and init_dev() doesn't flow to fast_track:

        /* check whether we're reopening an existing tty */
        if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
                tty = devpts_get_tty(idx);
                if (tty && driver->subtype == PTY_TYPE_MASTER)
                        tty = tty->link;
        } else {
                tty = driver->ttys[idx];
        }
        if (tty) goto fast_track;

The result of this, is that a new tty will be created and init_dev() returns
sucessfull. After returning, tty_mutex is dropped and master close may resume.

Master close finds it's the only use and both sides are closing, then releases
the tty and the index. At this point, the idr index is free, but slave still
has it.

Slave open then calls pty_open() and finds that tty->link->count is 0,
because there's no master and returns error.  Then tty_open() calls
release_dev() which executes without any warning, as it was a case of last
slave close when the master is already closed (master->count == 0,
slave->count == 1).  The tty is then released with the already released idr
index.

This normally would only issue a warning on idr_remove() but in case of a
customer's critical application, it's never too simple:

thread1: opens master, gets index X
thread1: begin closing master
thread2: begin opening slave with index X
thread1: finishes closing master, index X released
thread3: opens master, gets index X, just released
thread2: fails opening slave, releases index X         <----
thread4: opens master, gets index X, init_dev() then find an already in use
	 and healthy tty and fails

If no more indexes are released, ptmx_open() will keep failing, as the
first free index available is X, and it will make init_dev() fail because
you're trying to "reopen a master" which isn't valid.

The patch notices when this race happens and make init_dev() fail
imediately.  The init_dev() function is called with tty_mutex held, so it's
safe to continue with tty till the end of function because release_dev()
won't make any further changes without grabbing the tty_mutex.

Without the patch, on some machines it's possible get easily idr warnings
like this one:

idr_remove called for id=15 which is not allocated.
 [<c02555b9>] idr_remove+0x139/0x170
 [<c02a1b62>] release_mem+0x182/0x230
 [<c02a28e7>] release_dev+0x4b7/0x700
 [<c02a0ea7>] tty_ldisc_enable+0x27/0x30
 [<c02a1e64>] init_dev+0x254/0x580
 [<c02a0d64>] check_tty_count+0x14/0xb0
 [<c02a4f05>] tty_open+0x1c5/0x340
 [<c02a4d40>] tty_open+0x0/0x340
 [<c017388f>] chrdev_open+0xaf/0x180
 [<c017c2ac>] open_namei+0x8c/0x760
 [<c01737e0>] chrdev_open+0x0/0x180
 [<c0167bc9>] __dentry_open+0xc9/0x210
 [<c0167e2c>] do_filp_open+0x5c/0x70
 [<c0167a91>] get_unused_fd+0x61/0xd0
 [<c0167e93>] do_sys_open+0x53/0x100
 [<c0167f97>] sys_open+0x27/0x30
 [<c010303b>] syscall_call+0x7/0xb

using this test application available on:
 http://www.ruivo.org/~aris/pty_sodomizer.c

Signed-off-by: Aristeu Sergio Rozanski Filho <aris@ruivo.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Chuck Ebbert <cebbert@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-03-01 14:53:39 -08:00
..
acorn [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
acpi [PATCH] ACPI: make bay depend on dock 2007-03-01 14:53:38 -08:00
amba
ata [PATCH] Fix oops in pata_pcmcia 2007-02-26 13:02:08 -08:00
atm atm: Use ARRAY_SIZE macro when appropriate 2007-02-17 15:30:48 -05:00
auxdisplay [PATCH] cfag12864b: fix crash when built-in and no parport present 2007-02-20 17:10:14 -08:00
base Driver core: fix error by cleanup up symlinks properly 2007-02-23 14:52:09 -08:00
block [PATCH] Fix failure paths in modules init in umem.c 2007-03-01 14:53:36 -08:00
bluetooth [Bluetooth] Make use of MODULE_FIRMWARE 2007-02-26 11:42:42 -08:00
cdrom [PATCH] sysctl: remove insert_at_head from register_sysctl 2007-02-14 08:09:59 -08:00
char [PATCH] tty_io: fix race in master pty close/slave pty close path 2007-03-01 14:53:39 -08:00
clocksource [PATCH] Allow early access to the power management timer 2007-02-16 08:13:58 -08:00
connector
cpufreq Revert "[CPUFREQ] constify cpufreq_driver where possible." 2007-02-26 14:55:48 -08:00
crypto [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
dio
dma
edac [PATCH] EDAC: Add Fully-Buffered DIMM APIs to core 2007-02-12 09:48:32 -08:00
eisa
fc4 [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
firmware [PATCH] docbook: add edd firmware interfaces 2007-02-11 10:51:32 -08:00
hid [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
hwmon hwmon/vt1211: Add probing of alternate config index port 2007-02-14 21:15:05 +01:00
i2c Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial 2007-02-19 13:29:02 -08:00
ide Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial 2007-02-19 13:29:02 -08:00
ieee1394 Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial 2007-02-19 13:29:02 -08:00
infiniband IPoIB: Correct debugging output when path record lookup fails 2007-02-26 12:57:08 -08:00
input Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/dtor/input 2007-02-19 13:31:34 -08:00
isdn [PATCH] Fix buffer overflow and races in capi debug functions 2007-03-01 14:53:39 -08:00
kvm [PATCH] i386: Convert i386 PDA code to use %fs 2007-02-13 13:26:20 +01:00
leds leds: Add support for Cobalt Server front LED 2007-02-15 21:37:29 +00:00
macintosh backlight: Separate backlight properties from backlight ops pointers 2007-02-20 09:26:53 +00:00
mca
md [PATCH] md: add support for reshape of a raid6 2007-03-01 14:53:36 -08:00
media V4L/DVB (5253): Qt1010: whitespace / 80 column cleanups 2007-02-21 13:35:33 -02:00
message [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
mfd [PATCH] drivers/mfd/sm501.c: Replace pci_module_init with pci_register_driver 2007-02-26 12:35:47 -08:00
misc backlight: Separate backlight properties from backlight ops pointers 2007-02-20 09:26:53 +00:00
mmc Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm 2007-02-19 13:18:39 -08:00
mtd [ARM] 4233/1: nand/s3c2410.c: warning fix 2007-02-25 16:41:41 +00:00
net netxen: do_rom_fast_write error handling 2007-02-27 04:38:33 -05:00
nubus
oprofile [PATCH] mark struct file_operations const 5 2007-02-12 09:48:45 -08:00
parisc Merge master.kernel.org:/pub/scm/linux/kernel/git/kyle/parisc-2.6 2007-02-26 12:48:06 -08:00
parport [PATCH] parport_pc: fix parport_pc_probe_port section warning 2007-02-20 17:10:16 -08:00
pci Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial 2007-02-19 13:29:02 -08:00
pcmcia Driver core: more fallout from class_device changes for pcmcia 2007-02-23 14:52:09 -08:00
pnp Pull misc-for-upstream into release branch 2007-02-16 18:52:41 -05:00
ps3 [POWERPC] PS3: System manager support 2007-02-16 14:00:19 +11:00
rapidio [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
rtc [PATCH] rtc_cmos oops fix 2007-03-01 14:53:37 -08:00
s390 [S390] nss: Free unused memory in kernel image. 2007-02-21 10:55:37 +01:00
sbus [SPARC] uctrl: Check request_irq() return value. 2007-02-26 11:35:51 -08:00
scsi Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6 2007-02-19 13:32:28 -08:00
serial Merge master.kernel.org:/pub/scm/linux/kernel/git/kyle/parisc-2.6 2007-02-26 12:48:06 -08:00
sh
sn [PATCH] IOC3/IOC4: PCI mem space resources 2007-02-11 10:51:25 -08:00
spi [PATCH] spi_s3c2410_gpio.c spi mode 2 and 3 support 2007-02-20 17:10:16 -08:00
tc [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
telephony [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00
usb USB: export autosuspend delay in sysfs 2007-02-23 15:03:47 -08:00
video [PATCH] ps3: introduce CONFIG_PS3_ADVANCED 2007-03-01 14:53:38 -08:00
w1 [PATCH] w1: Use ARRAY_SIZE macro when appropriate 2007-02-12 09:48:28 -08:00
zorro [PATCH] mark struct file_operations const 6 2007-02-12 09:48:45 -08:00
Kconfig [PATCH] drivers: add LCD support 2007-02-11 10:51:24 -08:00
Makefile [PATCH] drivers: add LCD support 2007-02-11 10:51:24 -08:00