Skip to contentSkip to conversation

tty_buffer: avoid race due to tty_buffer_free_all() being misused

PATCH In review channing 3 participants 5 messages May 16, 2013 – May 17, 2013 TTY layer and serial drivers

lore
Share & tools
lore

channing

author
#0 lore raw

PATCH 1/1 tty_buffer: avoid race due to tty_buffer_free_all() being misused

In tty_buffer.c, function tty_buffer_free_all() is used to remove all buffers for a tty, although it's declared that it mustn't be called when the tty is in use, it cannot guarantee that. we can observe some device driver make use it by mistake, for example, while tty device is releasing, the tty data forwarding is not stopped, then it might hit the case that tty buffer is being used while tty_buffer_free_all() free this tty buffer, and finally lead to random error at any places, and it's not clear to debug.

Although device driver could do better, it's simpler and safer to strengthen protection in the view of tty buffer, by adding a tty->buf.lock in tty_buffer_free_all() to avoid it racing with ongoing tty buffer operations.

---
 drivers/tty/tty_buffer.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
drivers/tty/tty_buffer.c +3 -0
index 9121c1f..c7c100d 100644
@@ -32,7 +32,9 @@ void tty_buffer_free_all(struct tty_port *port)
 {
 	struct tty_bufhead *buf = &port->buf;
 	struct tty_buffer *thead;
+	unsigned long flags;
 
+	spin_lock_irqsave(&buf->lock, flags);
 	while ((thead = buf->head) != NULL) {
 		buf->head = thead->next;
 		kfree(thead);
@@ -43,6 +45,7 @@ void tty_buffer_free_all(struct tty_port *port)
 	}
 	buf->tail = NULL;
 	buf->memory_used = 0;
+	spin_unlock_irqrestore(&buf->lock, flags);
 }
 
 /**
Signature · 1 line
1.7.1
#1 lore raw
Quoted 9 lines · channing

In tty_buffer.c, function tty_buffer_free_all() is used to remove all buffers for a tty, although it's declared that it mustn't be called when the tty is in use, it cannot guarantee that. we can observe some device driver make use it by mistake, for example, while tty device is releasing, the tty data forwarding is not stopped, then it might hit the case that tty buffer is being used while tty_buffer_free_all() free this tty buffer, and finally lead to random error at any places, and it's not clear to debug.

What kernel version?

Quoted 4 lines

Although device driver could do better, it's simpler and safer to strengthen protection in the view of tty buffer, by adding a tty->buf.lock in tty_buffer_free_all() to avoid it racing with ongoing tty buffer operations.

Sorry, but this isn't correct.

The driver cannot continue to perform i/o concurrently with tty_port_destroy().

If the concurrent use you're observing is with flush_to_ldisc(), that should be fixed in current mainline.

Regards,
Peter Hurley

author
#2 lore raw
Quoted 11 lines · Peter Hurley
Quoted 9 lines · channing

In tty_buffer.c, function tty_buffer_free_all() is used to remove all buffers for a tty, although it's declared that it mustn't be called when the tty is in use, it cannot guarantee that. we can observe some device driver make use it by mistake, for example, while tty device is releasing, the tty data forwarding is not stopped, then it might hit the case that tty buffer is being used while tty_buffer_free_all() free this tty buffer, and finally lead to random error at any places, and it's not clear to debug.

Quoted 1 line

What kernel version?

3.4

Quoted 11 lines
Quoted 5 lines

Although device driver could do better, it's simpler and safer to strengthen protection in the view of tty buffer, by adding a tty->buf.lock in tty_buffer_free_all() to avoid it racing with ongoing tty buffer operations.

Sorry, but this isn't correct.

The driver cannot continue to perform i/o concurrently with tty_port_destroy().

Thanks for remind, 3.4 haven't tty_port_destroy(), the mainline has changed the way to call tty_buffer_free_all().

Quoted 3 lines

If the concurrent use you're observing is with flush_to_ldisc(), that should be fixed in current mainline.

Yes, when calling flush_to_ldisc() in Kernel 3.4, we could observe the tty buffer is corrupted, and dummped stack shows that tty_buffer_free_all() was called before. Is it a known issue fixed in old version? would you please tell me related patch to solve this flush_to_ldisc() issue? Thanks very much.

Chao

#3 lore raw
Quoted 28 lines · channing
Quoted 11 lines · Peter Hurley
Quoted 9 lines · channing

In tty_buffer.c, function tty_buffer_free_all() is used to remove all buffers for a tty, although it's declared that it mustn't be called when the tty is in use, it cannot guarantee that. we can observe some device driver make use it by mistake, for example, while tty device is releasing, the tty data forwarding is not stopped, then it might hit the case that tty buffer is being used while tty_buffer_free_all() free this tty buffer, and finally lead to random error at any places, and it's not clear to debug.

Quoted 1 line

What kernel version?

3.4

Quoted 11 lines
Quoted 5 lines

Although device driver could do better, it's simpler and safer to strengthen protection in the view of tty buffer, by adding a tty->buf.lock in tty_buffer_free_all() to avoid it racing with ongoing tty buffer operations.

Sorry, but this isn't correct.

The driver cannot continue to perform i/o concurrently with tty_port_destroy().

Thanks for remind, 3.4 haven't tty_port_destroy(), the mainline has changed the way to call tty_buffer_free_all().

Quoted 8 lines
Quoted 3 lines

If the concurrent use you're observing is with flush_to_ldisc(), that should be fixed in current mainline.

Yes, when calling flush_to_ldisc() in Kernel 3.4, we could observe the tty buffer is corrupted, and dummped stack shows that tty_buffer_free_all() was called before. Is it a known issue fixed in old version? would you please tell me related patch to solve this flush_to_ldisc() issue? Thanks very much.

Hi Chao,

Unfortunately, the fixes for flush_to_ldisc() continuing to run during, and even after, tty teardown have not been backported to stable kernels, mostly due to the extensive nature of the fixes.

In fact, even in current mainline and linux-next this problem is still possible. Hopefully soon the remaining patches will be reviewed and merged into linux-next.

For your reference, here is a link [1] to the patchset that was partially accepted for 3.10 (up through 'tty: Fix recursive deadlock in tty_perform_flush()')

Regards,
Peter Hurley

[1] [PATCH v5 00/44] ldisc patchset
http://www.gossamer-threads.com/lists/linux/kernel/1692114?do=post_view_flat

#4 lore raw
In reply to channing
Quoted 10 lines · channing

In tty_buffer.c, function tty_buffer_free_all() is used to remove all buffers for a tty, although it's declared that it mustn't be called when the tty is in use, it cannot guarantee that. we can observe some device driver make use it by mistake, for example, while tty device is releasing, the tty data forwarding is not stopped, then it might hit the case that tty buffer is being used while tty_buffer_free_all() free this tty buffer, and finally lead to random error at any places, and it's not clear to debug.

Quoted 6 lines

Although device driver could do better, it's simpler and safer to strengthen protection in the view of tty buffer, by adding a tty->buf.lock in tty_buffer_free_all() to avoid it racing with ongoing tty buffer operations.

Note, in the future, I need a "full" name for a signed-off-by: line and

Quoted 1 line

From line in the patch.

thanks,

greg k-h