Skip to contentSkip to conversation

: disassociate tty locking fixups

PATCH In review Prarit Bhargava 3 participants 5 messages Oct 14, 2006 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : disassociate tty locking fixups

Additional tty_mutex locking in do_tty_hangup and disassociate_ctty. It is possible that do_tty_hangup sets current->signal->tty = NULL. If that happens then disassociate_ctty can corrupt memory.

drivers/char/tty_io.c +12 -4
@@ -1357,6 +1357,8 @@ static void do_tty_hangup(void *data)
 	  This should get done automatically when the port closes and
 	  tty_release is called */
 	
+	/* it is possible current->signal->tty is modified */
+	mutex_lock(&tty_mutex);
 	read_lock(&tasklist_lock);
 	if (tty->session > 0) {
 		do_each_task_pid(tty->session, PIDTYPE_SID, p) {
@@ -1364,14 +1366,16 @@ static void do_tty_hangup(void *data)
 				p->signal->tty = NULL;
 			if (!p->signal->leader)
 				continue;
+			mutex_unlock(&tty_mutex);
 			group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
 			group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
+			mutex_lock(&tty_mutex);
 			if (tty->pgrp > 0)
 				p->signal->tty_old_pgrp = tty->pgrp;
 		} while_each_task_pid(tty->session, PIDTYPE_SID, p);
 	}
 	read_unlock(&tasklist_lock);
-
+	mutex_unlock(&tty_mutex);
 	tty->flags = 0;
 	tty->session = 0;
 	tty->pgrp = -1;
@@ -1502,7 +1506,7 @@ void disassociate_ctty(int on_exit)
 			kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
 		}
 		mutex_unlock(&tty_mutex);
-		unlock_kernel();	
+		unlock_kernel();
 		return;
 	}
 	if (tty_pgrp > 0) {
@@ -1514,8 +1518,12 @@ void disassociate_ctty(int on_exit)
 	/* Must lock changes to tty_old_pgrp */
 	mutex_lock(&tty_mutex);
 	current->signal->tty_old_pgrp = 0;
-	tty->session = 0;
-	tty->pgrp = -1;
+
+	/* It is possible that do_tty_hangup has free'd this tty */
+	if (current->signal->tty) {
+		tty->session = 0;
+		tty->pgrp = -1;
+	}
 
 	/* Now clear signal->tty under the lock */
 	read_lock(&tasklist_lock);
#1 lore raw
Quoted 6 lines
drivers/char/tty_io.c +1 -0
@@ -1364,14 +1366,16 @@ static void do_tty_hangup(void *data)
 				p->signal->tty = NULL;
 			if (!p->signal->leader)
 				continue;
+			mutex_unlock(&tty_mutex);

drivers/char/tty_io.c static void do_tty_hangup(void *data) · line 1378

Quoted 10 lines
drivers/char/tty_io.c +2 -1
 			group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
 			group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
+			mutex_lock(&tty_mutex);
 			if (tty->pgrp > 0)
 				p->signal->tty_old_pgrp = tty->pgrp;
 		} while_each_task_pid(tty->session, PIDTYPE_SID, p);
 	}
 	read_unlock(&tasklist_lock);
-
+	mutex_unlock(&tty_mutex);

Hi,

what is the lock ordering rules between tasklist_lock and tty_mutex? In the code above you first take tty_mutex then tasklist_lock, and later on you drop tty_mutex, with the result that you then have a tasklist_lock -> tty_mutex order.

This can deadlock if someone gets in the middle with a mutex_lock(&tty_mutex); write_lock(&tasklist_lock);
....

in addition, are you sure you don't need to revalidate anything after retaking the lock?

Greetings,
Arjan van de Ven

#2 lore raw
In reply to Prarit Bhargava

Ar Sad, 2006-10-14 am 07:22 -0400, ysgrifennodd Prarit Bhargava:

Quoted 3 lines

Additional tty_mutex locking in do_tty_hangup and disassociate_ctty. It is possible that do_tty_hangup sets current->signal->tty = NULL. If that happens then disassociate_ctty can corrupt memory.

Ugly but I don't think the patches are sufficient. Firstly you need to hold the task lock if you are poking around some other users ->signal, or that may itself change. (disassociate_ctty seems to have this wrong)

Secondly you appear to have lock ordering issues (you lock tty_mutex in both orders relative to the task list lock) (you take tty_mutex first, then the task lock which is correct, but then you drop and retake the tty_mutex while holding the task lock, which may deadlock)

Can you also explain why the ctty change proposed is neccessary ?

NAK the actual code, provisionally agree with the basic diagnosis of insufficient locking.

#3 lore raw
Quoted 3 lines · Alan Cox

Ugly but I don't think the patches are sufficient. Firstly you need to hold the task lock if you are poking around some other users ->signal, or that may itself change. (disassociate_ctty seems to have this wrong)

Ah -- okay.

So the locking order is (for example):

mutex_lock(&tty_mutex);
read_lock(&tasklist_lock);
task_lock(current);

Correct?

Quoted 4 lines

Secondly you appear to have lock ordering issues (you lock tty_mutex in both orders relative to the task list lock) (you take tty_mutex first, then the task lock which is correct, but then you drop and retake the tty_mutex while holding the task lock, which may deadlock)

Fixed.

Quoted 1 line

Can you also explain why the ctty change proposed is neccessary ?

disassociate_ctty can call tty_vhangup which calls do tty_hangup directly. do_tty_hangup can then set p->signal->tty = NULL, and after returning to disassociate_ctty, the value of tty will now contain a bad pointer. (I can reproduce this behaviour by running the gdb testsuite with slab debug on)

Quoted 2 lines

NAK the actual code, provisionally agree with the basic diagnosis of insufficient locking.

Quoted 2 lines · Arjan

in addition, are you sure you don't need to revalidate anything after retaking the lock?

The only place I need to revalidate data (AFAICT) is in disassociate_ctty where I re-check to see if current->signal->tty is still valid. Admittedly, I am looking at a very specific failure path though.

I'll rework the patch and post later.

P.

#4 lore raw

Ar Sad, 2006-10-14 am 11:06 -0400, ysgrifennodd Prarit Bhargava:

Quoted 7 lines

So the locking order is (for example):

mutex_lock(&tty_mutex);
read_lock(&tasklist_lock);
task_lock(current);

Correct?

Yes

Alan