Skip to contentSkip to conversation

tty: Fix buffer work access-after-free

PATCH In review Peter Hurley 8 participants 179 messages Dec 14, 2012 – Mar 26, 2013 Page 2 of 2

Subsystems (4)
lore
Share & tools
lore
#169 lore raw

PATCH 3/7 tty: Replace ldisc locking with ldisc_sem

In reply to Peter Hurley

Line discipline locking was performed with a combination of a mutex, a status bit, a count, and a waitqueue -- basically, a rw semaphore.

Replace the existing combination with an ld_semaphore.

Fixes:
1) the 'reference acquire after ldisc locked' bug
2) the over-complicated halt mechanism
3) lock order wrt. tty_lock()
4) dropping locks while changing ldisc
5) previously unidentified deadlock while locking ldisc from both linked ttys concurrently
6) previously unidentified recursive deadlocks

Adds much-needed lockdep diagnostics.

---
 drivers/tty/tty_buffer.c  |   2 +-
 drivers/tty/tty_io.c      |   7 +-
 drivers/tty/tty_ldisc.c   | 324 ++++++----------------------------------------
 include/linux/tty.h       |   4 +-
 include/linux/tty_ldisc.h |   3 +-
 5 files changed, 48 insertions(+), 292 deletions(-)
drivers/tty/tty_buffer.c +1 -1
index 578aa75..8e8d730 100644
@@ -429,7 +429,7 @@ static void flush_to_ldisc(struct work_struct *work)
 		return;
 
 	disc = tty_ldisc_ref(tty);
-	if (disc == NULL)	/*  !TTY_LDISC */
+	if (disc == NULL)
 		return;
 
 	spin_lock_irqsave(&buf->lock, flags);
drivers/tty/tty_io.c +3 -4
index 2f77af4..06f0e25 100644
@@ -1326,8 +1326,7 @@ static int tty_reopen(struct tty_struct *tty)
 	struct tty_driver *driver = tty->driver;
 
 	if (test_bit(TTY_CLOSING, &tty->flags) ||
-			test_bit(TTY_HUPPING, &tty->flags) ||
-			test_bit(TTY_LDISC_CHANGING, &tty->flags))
+			test_bit(TTY_HUPPING, &tty->flags))
 		return -EIO;
 
 	if (driver->type == TTY_DRIVER_TYPE_PTY &&
@@ -1343,7 +1342,7 @@ static int tty_reopen(struct tty_struct *tty)
 	}
 	tty->count++;
 
-	WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
+	WARN_ON(!tty->ldisc);
 
 	return 0;
 }
@@ -2952,7 +2951,7 @@ void initialize_tty_struct(struct tty_struct *tty,
 	tty->pgrp = NULL;
 	mutex_init(&tty->legacy_mutex);
 	mutex_init(&tty->termios_mutex);
-	mutex_init(&tty->ldisc_mutex);
+	init_ldsem(&tty->ldisc_sem);
 	init_waitqueue_head(&tty->write_wait);
 	init_waitqueue_head(&tty->read_wait);
 	INIT_WORK(&tty->hangup_work, do_tty_hangup);
drivers/tty/tty_ldisc.c +42 -282

534 lines not on this page · raw message

include/linux/tty.h +1 -3
index bfa6fca..2c109a3 100644
@@ -238,7 +238,7 @@ struct tty_struct {
 	int index;
 
 	/* Protects ldisc changes: Lock tty not pty */
-	struct mutex ldisc_mutex;
+	struct ld_semaphore ldisc_sem;
 	struct tty_ldisc *ldisc;
 
 	struct mutex atomic_write_lock;
@@ -306,8 +306,6 @@ struct tty_file_private {
 #define TTY_DO_WRITE_WAKEUP 	5	/* Call write_wakeup after queuing new */
 #define TTY_PUSH 		6	/* n_tty private */
 #define TTY_CLOSING 		7	/* ->close() in progress */
-#define TTY_LDISC 		9	/* Line discipline attached */
-#define TTY_LDISC_CHANGING 	10	/* Line discipline changing */
 #define TTY_LDISC_OPEN	 	11	/* Line discipline is open */
 #define TTY_HW_COOK_OUT 	14	/* Hardware can do output cooking */
 #define TTY_HW_COOK_IN 		15	/* Hardware can do input cooking */
include/linux/tty_ldisc.h +1 -2
index ca000fc..272075e 100644
@@ -197,8 +197,7 @@ struct tty_ldisc_ops {
 
 struct tty_ldisc {
 	struct tty_ldisc_ops *ops;
-	atomic_t users;
-	wait_queue_head_t wq_idle;
+	struct tty_struct *tty;
 };
 
 #define TTY_LDISC_MAGIC	0x5403
Signature · 1 line
1.8.1.2
#170 lore raw

PATCH 4/7 tty: Clarify ldisc variable

In reply to Peter Hurley

Rename o_ldisc to avoid confusion with the ldisc of the 'other' tty.

---
 drivers/tty/tty_ldisc.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/tty/tty_ldisc.c +7 -7
index a150f95..9ace119 100644
@@ -516,7 +516,7 @@ static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 {
 	int retval;
-	struct tty_ldisc *o_ldisc, *new_ldisc;
+	struct tty_ldisc *old_ldisc, *new_ldisc;
 	struct tty_struct *o_tty = tty->link;
 
 	new_ldisc = tty_ldisc_get(tty, ldisc);
@@ -540,7 +540,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	/* FIXME: why 'shutoff' input if the ldisc is locked? */
 	tty->receive_room = 0;
 
-	o_ldisc = tty->ldisc;
+	old_ldisc = tty->ldisc;
 	tty_lock(tty);
 
 	/* FIXME: for testing only */
@@ -555,8 +555,8 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 		return -EIO;
 	}
 
-	/* Shutdown the current discipline. */
-	tty_ldisc_close(tty, o_ldisc);
+	/* Shutdown the old discipline. */
+	tty_ldisc_close(tty, old_ldisc);
 
 	/* Now set up the new line discipline. */
 	tty->ldisc = new_ldisc;
@@ -566,17 +566,17 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	if (retval < 0) {
 		/* Back to the old one or N_TTY if we can't */
 		tty_ldisc_put(new_ldisc);
-		tty_ldisc_restore(tty, o_ldisc);
+		tty_ldisc_restore(tty, old_ldisc);
 	}
 
 	/* At this point we hold a reference to the new ldisc and a
 	   a reference to the old ldisc. If we ended up flipping back
 	   to the existing ldisc we have two references to it */
 
-	if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
+	if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc)
 		tty->ops->set_ldisc(tty);
 
-	tty_ldisc_put(o_ldisc);
+	tty_ldisc_put(old_ldisc);
 
 	/*
 	 *	Allow ldisc referencing to occur again
Signature · 1 line
1.8.1.2
#171 lore raw

PATCH 5/7 tty: Fix hangup race with TIOCSETD ioctl

In reply to Peter Hurley

The hangup may already have happened; check for that state also.

---
 drivers/tty/tty_ldisc.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/tty/tty_ldisc.c +2 -4
index 9ace119..84ba790 100644
@@ -543,10 +543,8 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	old_ldisc = tty->ldisc;
 	tty_lock(tty);
 
-	/* FIXME: for testing only */
-	WARN_ON(test_bit(TTY_HUPPED, &tty->flags));
-
-	if (test_bit(TTY_HUPPING, &tty->flags)) {
+	if (test_bit(TTY_HUPPING, &tty->flags) ||
+	    test_bit(TTY_HUPPED, &tty->flags)) {
 		/* We were raced by the hangup method. It will have stomped
 		   the ldisc data and closed the ldisc down */
 		tty_ldisc_enable_pair(tty, o_tty);
Signature · 1 line
1.8.1.2
#172 lore raw

PATCH 6/7 tty: Clarify multiple-references comment in TIOCSETD ioctl

In reply to Peter Hurley
---
 drivers/tty/tty_ldisc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/tty/tty_ldisc.c +6 -4
index 84ba790..9725c94 100644
@@ -567,13 +567,15 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 		tty_ldisc_restore(tty, old_ldisc);
 	}
 
-	/* At this point we hold a reference to the new ldisc and a
-	   a reference to the old ldisc. If we ended up flipping back
-	   to the existing ldisc we have two references to it */
-
 	if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc)
 		tty->ops->set_ldisc(tty);
 
+	/* At this point we hold a reference to the new ldisc and a
+	   reference to the old ldisc, or we hold two references to
+	   the old ldisc (if it was restored as part of error cleanup
+	   above). In either case, releasing a single reference from
+	   the old ldisc is correct. */
+
 	tty_ldisc_put(old_ldisc);
 
 	/*
Signature · 1 line
1.8.1.2
#173 lore raw

PATCH 7/7 tty: Fix tty_ldisc_lock name collision

In reply to Peter Hurley
---
 drivers/tty/tty_ldisc.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/tty/tty_ldisc.c +12 -12
index 9725c94..ba49c0e 100644
@@ -44,7 +44,7 @@ enum {
  *	callers who will do ldisc lookups and cannot sleep.
  */
 
-static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
+static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
 /* Line disc dispatch table */
 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
 
@@ -58,7 +58,7 @@ static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
  *	from this point onwards.
  *
  *	Locking:
- *		takes tty_ldisc_lock to guard against ldisc races
+ *		takes tty_ldiscs_lock to guard against ldisc races
  */
 
 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
@@ -69,11 +69,11 @@ int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
 	if (disc < N_TTY || disc >= NR_LDISCS)
 		return -EINVAL;
 
-	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
 	tty_ldiscs[disc] = new_ldisc;
 	new_ldisc->num = disc;
 	new_ldisc->refcount = 0;
-	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
 
 	return ret;
 }
@@ -88,7 +88,7 @@ EXPORT_SYMBOL(tty_register_ldisc);
  *	currently in use.
  *
  *	Locking:
- *		takes tty_ldisc_lock to guard against ldisc races
+ *		takes tty_ldiscs_lock to guard against ldisc races
  */
 
 int tty_unregister_ldisc(int disc)
@@ -99,12 +99,12 @@ int tty_unregister_ldisc(int disc)
 	if (disc < N_TTY || disc >= NR_LDISCS)
 		return -EINVAL;
 
-	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
 	if (tty_ldiscs[disc]->refcount)
 		ret = -EBUSY;
 	else
 		tty_ldiscs[disc] = NULL;
-	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
 
 	return ret;
 }
@@ -115,7 +115,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
 	unsigned long flags;
 	struct tty_ldisc_ops *ldops, *ret;
 
-	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
 	ret = ERR_PTR(-EINVAL);
 	ldops = tty_ldiscs[disc];
 	if (ldops) {
@@ -125,7 +125,7 @@ static struct tty_ldisc_ops *get_ldops(int disc)
 			ret = ldops;
 		}
 	}
-	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
 	return ret;
 }
 
@@ -133,10 +133,10 @@ static void put_ldops(struct tty_ldisc_ops *ldops)
 {
 	unsigned long flags;
 
-	raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
+	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
 	ldops->refcount--;
 	module_put(ldops->owner);
-	raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
 }
 
 /**
@@ -149,7 +149,7 @@ static void put_ldops(struct tty_ldisc_ops *ldops)
  *	available
  *
  *	Locking:
- *		takes tty_ldisc_lock to guard against ldisc races
+ *		takes tty_ldiscs_lock to guard against ldisc races
  */
 
 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
Signature · 1 line
1.8.1.2
#174 lore raw

Re: [PATCH v3 00/23] ldisc fixes

In reply to Peter Hurley
Quoted 5 lines · * Peter Hurley | 2013-02-05 15:20:15 [-0500]:

Please re-test with your dummy_hcd/g_nokia testcase, although I'm not convinced that usb gadget is using tty_hangup() appropriately. tty drivers use this for async carrier loss coming from an IRQ which will be disabled if the tty has been shutdown. Does gserial prevent async hangup to a dead tty in a similar fashion?

Not sure I understood. tty_hangup() is only called from within gserial_disconnect() which calls right after usb_ep_disable(). After usb_ep_disable() no further serial packets can be received until the endpoints are re-enabled. This happens in gserial_connect().

Sebastian

#175 lore raw

Re: [PATCH v3 00/23] ldisc fixes

[--cc Alan Cox]

Quoted 8 lines · Sebastian Andrzej Siewior
Quoted 5 lines · * Peter Hurley | 2013-02-05 15:20:15 [-0500]:

Please re-test with your dummy_hcd/g_nokia testcase, although I'm not convinced that usb gadget is using tty_hangup() appropriately. tty drivers use this for async carrier loss coming from an IRQ which will be disabled if the tty has been shutdown. Does gserial prevent async hangup to a dead tty in a similar fashion?

Quoted 4 lines

Not sure I understood. tty_hangup() is only called from within gserial_disconnect() which calls right after usb_ep_disable(). After usb_ep_disable() no further serial packets can be received until the endpoints are re-enabled. This happens in gserial_connect().

That's why I asked. There are two potential issues:

First, tty_hangup() is asynchronous -- ie., it returns immediately. It does not wait for the tty device to actually perform the hangup. So if the gadget layers start cleanup immediately after, expecting that they won't get a flurry of tty calls, that would be bad.

tty_vhangup() is synchronous -- ie., you wait while it cleans up. This is what the usb serial core does on it's disconnect() method. But I didn't research further if the circumstances were the same.

Second, when the hangup actually does run -- in __tty_hangup() -- it expects the tty to exist. I didn't go looking through the gadget layers to see if the tty was disposed some other way, which might race the asynchronous tty hangup.

Thanks,
Peter Hurley

#176 lore raw

Re: [PATCH v3 00/23] ldisc fixes

Quoted 16 lines · Peter Hurley

[--cc Alan Cox]

Quoted 8 lines · Sebastian Andrzej Siewior
Quoted 5 lines · * Peter Hurley | 2013-02-05 15:20:15 [-0500]:

Please re-test with your dummy_hcd/g_nokia testcase, although I'm not convinced that usb gadget is using tty_hangup() appropriately. tty drivers use this for async carrier loss coming from an IRQ which will be disabled if the tty has been shutdown. Does gserial prevent async hangup to a dead tty in a similar fashion?

Quoted 4 lines

Not sure I understood. tty_hangup() is only called from within gserial_disconnect() which calls right after usb_ep_disable(). After usb_ep_disable() no further serial packets can be received until the endpoints are re-enabled. This happens in gserial_connect().

Quoted 6 lines

That's why I asked. There are two potential issues:

First, tty_hangup() is asynchronous -- ie., it returns immediately. It does not wait for the tty device to actually perform the hangup. So if the gadget layers start cleanup immediately after, expecting that they won't get a flurry of tty calls, that would be bad.

Sorry, I missed what driver is this?

Quoted 3 lines

tty_vhangup() is synchronous -- ie., you wait while it cleans up. This is what the usb serial core does on it's disconnect() method. But I didn't research further if the circumstances were the same.

Even when tty_vhangup returns, it does not guarantee a closed tty. And it also does not guarantee that any of tty->ops won't be called. The latter is true only for devices that can be consoles. (For those, file->ops are not redirected.) In that case one needs to wait for port->count to become 0.

Signature · 2 lines
js
suse labs
#177 lore raw

Re: [PATCH v3 00/23] ldisc fixes

[now this discussion has turned to usb gadget +cc Felipe Balbi, linux-usb, -cc Dave Jones, Ilya Zykov]

Quoted 17 lines · Jiri Slaby
Quoted 16 lines · Peter Hurley

[--cc Alan Cox]

Quoted 12 lines · Sebastian Andrzej Siewior
Quoted 5 lines · * Peter Hurley | 2013-02-05 15:20:15 [-0500]:

Please re-test with your dummy_hcd/g_nokia testcase, although I'm not convinced that usb gadget is using tty_hangup() appropriately. tty drivers use this for async carrier loss coming from an IRQ which will be disabled if the tty has been shutdown. Does gserial prevent async hangup to a dead tty in a similar fashion?

Not sure I understood. tty_hangup() is only called from within gserial_disconnect() which calls right after usb_ep_disable(). After usb_ep_disable() no further serial packets can be received until the endpoints are re-enabled. This happens in gserial_connect().

Quoted 8 lines
Quoted 6 lines

That's why I asked. There are two potential issues:

First, tty_hangup() is asynchronous -- ie., it returns immediately. It does not wait for the tty device to actually perform the hangup. So if the gadget layers start cleanup immediately after, expecting that they won't get a flurry of tty calls, that would be bad.

Sorry, I missed what driver is this?

g_serial.

drivers/usb/gadget/u_serial.c

Quoted 9 lines
Quoted 3 lines

tty_vhangup() is synchronous -- ie., you wait while it cleans up. This is what the usb serial core does on it's disconnect() method. But I didn't research further if the circumstances were the same.

Even when tty_vhangup returns, it does not guarantee a closed tty. And it also does not guarantee that any of tty->ops won't be called. The latter is true only for devices that can be consoles. (For those, file->ops are not redirected.) In that case one needs to wait for port->count to become 0.

Perhaps I was oversimplifying.

But my point was I doubt usb gadget is conducting its teardown safely wrt tty.

Regards,
Peter Hurley

#178 lore raw

Re: [PATCH v3 00/23] ldisc fixes

In reply to Peter Hurley
Quoted 11 lines · * Peter Hurley | 2013-03-05 17:20:29 [-0500]:
Quoted 4 lines

Not sure I understood. tty_hangup() is only called from within gserial_disconnect() which calls right after usb_ep_disable(). After usb_ep_disable() no further serial packets can be received until the endpoints are re-enabled. This happens in gserial_connect().

That's why I asked. There are two potential issues:

First, tty_hangup() is asynchronous -- ie., it returns immediately. It does not wait for the tty device to actually perform the hangup. So if the gadget layers start cleanup immediately after, expecting that they won't get a flurry of tty calls, that would be bad.

tty_hangup() is called from interrupt context usually after the USB cable has been unplugged. It is also possible that if the host requests the function twice, it will (on the second invocation without a reset) call tty_hangup() followed by tty_wakeup() to start the new session.

Quoted 4 lines

tty_vhangup() is synchronous -- ie., you wait while it cleans up. This is what the usb serial core does on it's disconnect() method. But I didn't research further if the circumstances were the same.

Okay.

Quoted 4 lines

Second, when the hangup actually does run -- in __tty_hangup() -- it expects the tty to exist. I didn't go looking through the gadget layers to see if the tty was disposed some other way, which might race the asynchronous tty hangup.

It calls wake_up_interruptible() expecting the user to leave. The node is removed on module removal.

Is there any change of API you suggest?

Quoted 2 lines

Thanks,
Peter Hurley

Sebastian