Skip to contentSkip to conversation

signal: multithread program returns with wrong errno on receiving SIGSTOP

BUG Oleg Nesterov 3 participants 6 messages May 28, 2007 – May 29, 2007 Char and misc drivers

lore
Share & tools
lore
#0 lore raw
Quoted 4 lines · Satoru Takeuchi

I found a bug on signal subsystem. If there is some multithread program and one of the thread is blocking on the system call, it returns with wrong errno on receiving SIGSTOP and following SIGCONT.

It doesn't matter in this particular case, but in general it is good to report the kernel version as well.

Quoted 22 lines

1. issue the test program (attached on this mail) from term A.

   $ ./mt-wrong-errno

2. send SIGSTOP to test program from term B.

   $ ps a
   ...
    9685 pts/3    Sl+    0:00 ./mt-wrong-errno
    9688 pts/4    R+     0:00 ps a
   $ kill -STOP 9685

3. send SIGCONT to test program from term B.

   $ kill -CONT 9685

Expected Result
===============

`./mt-wrong-errno' restarts read syscall (and stop again by receiving SIGTTIN).

Quoted 6 lines

Actual Result
=============

`./mt-wrong-errno' returns with wrong errno 512 means ERESTARTSYS. The comment on include/linux/errno.h says "this errno should never be seen by user program." So it's definitely a bug.

Wild guess,

	drivers/char/n_tty.c:job_control()

		kill_pgrp(task_pgrp(current), SIGTTIN, 1);
		return -ERESTARTSYS;

This is wrong. kill_pgrp()->__group_send_sig_info() sends SIGTTIN to the thread group, but can choose any sub-thread as a target for signal_wake_up().

This means we return ERESTARTSYS, but signal_pending() != true, bug.

Satoru, could you re-test with the untested patch below?

Oleg.

drivers/char/n_tty.c renamed from drivers/char/n_tty.c~ +1 -0
@@ -1191,6 +1191,7 @@ static int job_control(struct tty_struct
 			    is_current_pgrp_orphaned())
 				return -EIO;
 			kill_pgrp(task_pgrp(current), SIGTTIN, 1);
+			set_thread_flag(TIF_SIGPENDING);
 			return -ERESTARTSYS;
 		}
 	}
#1 lore raw

Hi Oleg,

At Mon, 28 May 2007 11:07:53 +0400,

Quoted 46 lines · Oleg Nesterov
Quoted 4 lines · Satoru Takeuchi

I found a bug on signal subsystem. If there is some multithread program and one of the thread is blocking on the system call, it returns with wrong errno on receiving SIGSTOP and following SIGCONT.

It doesn't matter in this particular case, but in general it is good to report the kernel version as well.

Quoted 22 lines

1. issue the test program (attached on this mail) from term A.

   $ ./mt-wrong-errno

2. send SIGSTOP to test program from term B.

   $ ps a
   ...
    9685 pts/3    Sl+    0:00 ./mt-wrong-errno
    9688 pts/4    R+     0:00 ps a
   $ kill -STOP 9685

3. send SIGCONT to test program from term B.

   $ kill -CONT 9685

Expected Result
===============

`./mt-wrong-errno' restarts read syscall (and stop again by receiving SIGTTIN).

Quoted 6 lines

Actual Result
=============

`./mt-wrong-errno' returns with wrong errno 512 means ERESTARTSYS. The comment on include/linux/errno.h says "this errno should never be seen by user program." So it's definitely a bug.

Wild guess,

	drivers/char/n_tty.c:job_control()

		kill_pgrp(task_pgrp(current), SIGTTIN, 1);
		return -ERESTARTSYS;
Quoted 7 lines

This is wrong. kill_pgrp()->__group_send_sig_info() sends SIGTTIN to the thread group, but can choose any sub-thread as a target for signal_wake_up().

This means we return ERESTARTSYS, but signal_pending() != true, bug.

Satoru, could you re-test with the untested patch below?

I tested this patch on my ia64 box and it works fine.

# Since I don't know anything about tty driver, I can't determine its

# adequacy immediately, sorry.

Satoru

Quoted 19 lines

Oleg.

drivers/char/n_tty.c renamed from drivers/char/n_tty.c~ +1 -1
@@ -1191,6 +1191,7 @@ static int job_control(struct tty_struct
 			    is_current_pgrp_orphaned())
 				return -EIO;
 			kill_pgrp(task_pgrp(current), SIGTTIN, 1);
+			set_thread_flag(TIF_SIGPENDING);
 			return -ERESTARTSYS;
 		}
 	}
 
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#2 lore raw
Quoted 13 lines · Satoru Takeuchi

At Mon, 28 May 2007 11:07:53 +0400,

Quoted 10 lines · Oleg Nesterov
	drivers/char/n_tty.c:job_control()

		kill_pgrp(task_pgrp(current), SIGTTIN, 1);
		return -ERESTARTSYS;

This is wrong. kill_pgrp()->__group_send_sig_info() sends SIGTTIN to the thread group, but can choose any sub-thread as a target for signal_wake_up().

Quoted 5 lines
Quoted 3 lines

This means we return ERESTARTSYS, but signal_pending() != true, bug.

Satoru, could you re-test with the untested patch below?

I tested this patch on my ia64 box and it works fine.

Thanks! A quick grep shows that tty_check_change() may have the same problem.

Quoted 2 lines

# Since I don't know anything about tty driver, I can't determine its

# adequacy immediately, sorry.

/dev/tty is a black magic to me too. I'll send the patch later unless maintainers have any objections.

Quoted 11 lines

drivers/char/n_tty.c static int job_control(struct tty_struct · line 1197

Quoted 11 lines
drivers/char/n_tty.c renamed from drivers/char/n_tty.c~ +1 -0
@@ -1191,6 +1191,7 @@ static int job_control(struct tty_struct
 			    is_current_pgrp_orphaned())
 				return -EIO;
 			kill_pgrp(task_pgrp(current), SIGTTIN, 1);
+			set_thread_flag(TIF_SIGPENDING);
 			return -ERESTARTSYS;
 		}
 	}

Oleg.

#3 lore raw
In reply to Oleg Nesterov

Your fix seems like the only way to go. From skimming all the ERESTART* uses, I think that in all cases (except for n_tty.c:job_control before your patch), TIF_SIGPENDING is indeed set when a thread returns -ERESTART*.

But it makes me realize that there is a danger of leaking a -ERESTART* return code to userland when TIF_SIGPENDING gets cleared by another thread doing a recalc_sigpending_tsk. Because of -ERESTART* I think we must make it a rule that no thread can clear another thread's TIF_SIGPENDING, only set it (unless it's known to be stopped in the signal code or something).

Quoted 1 line

From our recent work on it, I think that do_sigaction is in fact the only

place this can happen. So that says we should err in the other direction from what I said before in do_sigaction, and not have it do recalc at all.

Thanks,
Roland

#4 lore raw
Quoted 5 lines · Roland McGrath

Your fix seems like the only way to go. From skimming all the ERESTART* uses, I think that in all cases (except for n_tty.c:job_control before your patch), TIF_SIGPENDING is indeed set when a thread returns -ERESTART*.

Quoted 8 lines

But it makes me realize that there is a danger of leaking a -ERESTART* return code to userland when TIF_SIGPENDING gets cleared by another thread doing a recalc_sigpending_tsk. Because of -ERESTART* I think we must make it a rule that no thread can clear another thread's TIF_SIGPENDING, only set it (unless it's known to be stopped in the signal code or something).

Quoted 1 line

From our recent work on it, I think that do_sigaction is in fact the only

place this can happen. So that says we should err in the other direction from what I said before in do_sigaction, and not have it do recalc at all.

I think you are right.

But please note that cancel_freezing(p) is special. It is also called when try_to_freeze_tasks() fails. So it should clear TIF_SIGPENDING if "p" is a kernel thread, otherwise p may run with signal_pending() forever.

Unfortunately, it is not easy to detect the kernel thread, is_user_space() is not reliable. Probably we should ignore this minor problem and do not change cancel_freezing().

Oleg.

#5 lore raw

Yeah, I knowingly glossed over freezer. I don't really want to think about it. There are more problems than just this with how it goes about it, but I'd rather leave that alone until it really bites us.

Thanks,
Roland