Skip to contentSkip to conversation

RFC: Use of user space handler vs. SIG_DFL on forced signals

PATCH Marco Elver 3 participants 9 messages Mar 22, 2022 – Mar 28, 2022

Subsystems (7)
Arm (32-bit) x86 perf events arm64 +3 more subsystems
lore
Share & tools
lore
#0 lore raw

Hello,

Currently force_sig_info_to_task() will always unblock a blocked signal but deliver the signal to SIG_DFL:

[...]
* Note: If we unblock the signal, we always reset it to SIG_DFL,
* since we do not want to have a signal handler that was blocked
* be invoked when user space had explicitly blocked it.
[...]

Is this requirement part of the POSIX spec? Or is the intent simply to attempt to do the least-bad thing?

The reason I'm asking is that we've encountered rare crashes with the new SIGTRAP on perf events, due to patterns like this:

	<set up SIGTRAP on a perf event>
	...
	sigset_t s;
	sigemptyset(&s);
	sigaddset(&s, SIGTRAP | <and others>);
	sigprocmask(SIG_BLOCK, &s, ...);
	...
	<perf event triggers>

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

For other types of signals, is the assumption here that if user space blocked the signal, it might not be able to handle it in the first place?

For SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space!

If there is no good reason to choose SIG_DFL, our preference would be to allow this kind of "unblockable forced" signal to the user space handler for force_sig_perf() -- with the caveat whoever requests SIGTRAP on perf events must be able to provide a handler that can always run safely. But we think that's better than crashing.

The below patch would do what we want, but would like to first confirm if this is "within spec".

Thoughts?

Thanks,
-- Marco

------ >8 ------

From: Marco Elver <[email protected]>
Date: Mon, 21 Mar 2022 22:18:09 +0100
Subject: [PATCH RFC] signal: Always unblock signal to user space for
force_sig_perf()

With SIGTRAP on perf events, we have encountered termination of processes due to user space attempting to block delivery of SIGTRAP. Consider this case:

	<set up SIGTRAP on a perf event>
	...
	sigset_t s;
	sigemptyset(&s);
	sigaddset(&s, SIGTRAP | <and others>);
	sigprocmask(SIG_BLOCK, &s, ...);
	...
	<perf event triggers>

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

With forced signals, the whole premise of sigprocmask() is invalidated since the signal is still delivered, only to the default signal handler. The assumption here is that if user space blocked the signal, it might not be able to handle it in the first place.

However, for SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")
---
 kernel/signal.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
kernel/signal.c +13 -5
index 38602738866e..04b7a178a5f3 100644
@@ -1303,6 +1303,7 @@ int do_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p
 
 enum sig_handler {
 	HANDLER_CURRENT, /* If reachable use the current handler */
+	HANDLER_UNBLOCK, /* Use the current handler even if blocked */
 	HANDLER_SIG_DFL, /* Always use SIG_DFL handler semantics */
 	HANDLER_EXIT,	 /* Only visible as the process exit code */
 };
@@ -1311,9 +1312,11 @@ enum sig_handler {
  * Force a signal that the process can't ignore: if necessary
  * we unblock the signal and change any SIG_IGN to SIG_DFL.
  *
- * Note: If we unblock the signal, we always reset it to SIG_DFL,
- * since we do not want to have a signal handler that was blocked
- * be invoked when user space had explicitly blocked it.
+ * Note: If we unblock the signal and handler != HANDLER_UNBLOCK, we always
+ * reset it to SIG_DFL, since we do not want to have a signal handler that was
+ * blocked be invoked when user space had explicitly blocked it. If handler is
+ * HANDLER_UNBLOCK, user space will always receive the signal and is expected to
+ * provide a handler that is safe in all contexts.
  *
  * We don't want to have recursive SIGSEGV's etc, for example,
  * that is why we also clear SIGNAL_UNKILLABLE.
@@ -1332,7 +1335,8 @@ force_sig_info_to_task(struct kernel_siginfo *info, struct task_struct *t,
 	ignored = action->sa.sa_handler == SIG_IGN;
 	blocked = sigismember(&t->blocked, sig);
 	if (blocked || ignored || (handler != HANDLER_CURRENT)) {
-		action->sa.sa_handler = SIG_DFL;
+		if (handler != HANDLER_UNBLOCK)
+			action->sa.sa_handler = SIG_DFL;
 		if (handler == HANDLER_EXIT)
 			action->sa.sa_flags |= SA_IMMUTABLE;
 		if (blocked) {
@@ -1816,7 +1820,11 @@ int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
 	info.si_perf_data = sig_data;
 	info.si_perf_type = type;
 
-	return force_sig_info(&info);
+	/*
+	 * Delivering SIGTRAP on perf events must unblock delivery to not
+	 * kill the task, but attempt delivery to the user space handler.
+	 */
+	return force_sig_info_to_task(&info, current, HANDLER_UNBLOCK);
 }
 
 /**
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#1 lore raw
Quoted 136 lines · Marco Elver

Hello,

Currently force_sig_info_to_task() will always unblock a blocked signal but deliver the signal to SIG_DFL:

[...]
* Note: If we unblock the signal, we always reset it to SIG_DFL,
* since we do not want to have a signal handler that was blocked
* be invoked when user space had explicitly blocked it.
[...]

Is this requirement part of the POSIX spec? Or is the intent simply to attempt to do the least-bad thing?

The reason I'm asking is that we've encountered rare crashes with the new SIGTRAP on perf events, due to patterns like this:

        <set up SIGTRAP on a perf event>
        ...
        sigset_t s;
        sigemptyset(&s);
        sigaddset(&s, SIGTRAP | <and others>);
        sigprocmask(SIG_BLOCK, &s, ...);
        ...
        <perf event triggers>

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

For other types of signals, is the assumption here that if user space blocked the signal, it might not be able to handle it in the first place?

For SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space!

If there is no good reason to choose SIG_DFL, our preference would be to allow this kind of "unblockable forced" signal to the user space handler for force_sig_perf() -- with the caveat whoever requests SIGTRAP on perf events must be able to provide a handler that can always run safely. But we think that's better than crashing.

The below patch would do what we want, but would like to first confirm if this is "within spec".

Thoughts?

Thanks,
-- Marco

------ >8 ------

From: Marco Elver <[email protected]>
Date: Mon, 21 Mar 2022 22:18:09 +0100
Subject: [PATCH RFC] signal: Always unblock signal to user space for
force_sig_perf()

With SIGTRAP on perf events, we have encountered termination of processes due to user space attempting to block delivery of SIGTRAP. Consider this case:

        <set up SIGTRAP on a perf event>
        ...
        sigset_t s;
        sigemptyset(&s);
        sigaddset(&s, SIGTRAP | <and others>);
        sigprocmask(SIG_BLOCK, &s, ...);
        ...
        <perf event triggers>

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

With forced signals, the whole premise of sigprocmask() is invalidated since the signal is still delivered, only to the default signal handler. The assumption here is that if user space blocked the signal, it might not be able to handle it in the first place.

However, for SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")
---
 kernel/signal.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
kernel/signal.c +8 -4
index 38602738866e..04b7a178a5f3 100644
@@ -1303,6 +1303,7 @@ int do_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p
 
 enum sig_handler {
        HANDLER_CURRENT, /* If reachable use the current handler */
+       HANDLER_UNBLOCK, /* Use the current handler even if blocked */
        HANDLER_SIG_DFL, /* Always use SIG_DFL handler semantics */
        HANDLER_EXIT,    /* Only visible as the process exit code */
 };
@@ -1311,9 +1312,11 @@ enum sig_handler {
  * Force a signal that the process can't ignore: if necessary
  * we unblock the signal and change any SIG_IGN to SIG_DFL.
  *
- * Note: If we unblock the signal, we always reset it to SIG_DFL,
- * since we do not want to have a signal handler that was blocked
- * be invoked when user space had explicitly blocked it.
+ * Note: If we unblock the signal and handler != HANDLER_UNBLOCK, we always
+ * reset it to SIG_DFL, since we do not want to have a signal handler that was
+ * blocked be invoked when user space had explicitly blocked it. If handler is
+ * HANDLER_UNBLOCK, user space will always receive the signal and is expected to
+ * provide a handler that is safe in all contexts.
  *
  * We don't want to have recursive SIGSEGV's etc, for example,
  * that is why we also clear SIGNAL_UNKILLABLE.
@@ -1332,7 +1335,8 @@ force_sig_info_to_task(struct kernel_siginfo *info, struct task_struct *t,
        ignored = action->sa.sa_handler == SIG_IGN;
        blocked = sigismember(&t->blocked, sig);
        if (blocked || ignored || (handler != HANDLER_CURRENT)) {
-               action->sa.sa_handler = SIG_DFL;
+               if (handler != HANDLER_UNBLOCK)
+                       action->sa.sa_handler = SIG_DFL;
                if (handler == HANDLER_EXIT)
                        action->sa.sa_flags |= SA_IMMUTABLE;
                if (blocked) {

kernel/signal.c int force_sig_perf(void __user *addr, u32 type, u64 sig_data) · line 1827

Quoted 9 lines
kernel/signal.c +5 -1
@@ -1816,7 +1820,11 @@ int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
        info.si_perf_data = sig_data;
        info.si_perf_type = type;
 
-       return force_sig_info(&info);
+       /*
+        * Delivering SIGTRAP on perf events must unblock delivery to not
+        * kill the task, but attempt delivery to the user space handler.
+        */
+       return force_sig_info_to_task(&info, current, HANDLER_UNBLOCK);

It seems that in this case we almost don't use any of the logic in force_sig_info_to_task(). It effectively reduces to the call to send_signal() protected by the lock. Maybe we should call something like do_send_sig_info() directly?

Quoted 6 lines
kernel/signal.c +0 -1
 }
 
 /**
--

2.35.1.894.gb6a874cedc-goog

#2 lore raw
Quoted 58 lines · Dmitry Vyukov
Quoted 57 lines · Marco Elver

Hello,

Currently force_sig_info_to_task() will always unblock a blocked signal but deliver the signal to SIG_DFL:

[...]
* Note: If we unblock the signal, we always reset it to SIG_DFL,
* since we do not want to have a signal handler that was blocked
* be invoked when user space had explicitly blocked it.
[...]

Is this requirement part of the POSIX spec? Or is the intent simply to attempt to do the least-bad thing?

The reason I'm asking is that we've encountered rare crashes with the new SIGTRAP on perf events, due to patterns like this:

        <set up SIGTRAP on a perf event>
        ...
        sigset_t s;
        sigemptyset(&s);
        sigaddset(&s, SIGTRAP | <and others>);
        sigprocmask(SIG_BLOCK, &s, ...);
        ...
        <perf event triggers>

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

For other types of signals, is the assumption here that if user space blocked the signal, it might not be able to handle it in the first place?

For SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space!

If there is no good reason to choose SIG_DFL, our preference would be to allow this kind of "unblockable forced" signal to the user space handler for force_sig_perf() -- with the caveat whoever requests SIGTRAP on perf events must be able to provide a handler that can always run safely. But we think that's better than crashing.

The below patch would do what we want, but would like to first confirm if this is "within spec".

Thoughts?

Thanks,
-- Marco

------ >8 ------

[...]

Quoted 14 lines
Quoted 14 lines
kernel/signal.c +2 -1
@@ -1332,7 +1335,8 @@ force_sig_info_to_task(struct kernel_siginfo *info, struct task_struct *t,
        ignored = action->sa.sa_handler == SIG_IGN;
        blocked = sigismember(&t->blocked, sig);
        if (blocked || ignored || (handler != HANDLER_CURRENT)) {
-               action->sa.sa_handler = SIG_DFL;
+               if (handler != HANDLER_UNBLOCK)
+                       action->sa.sa_handler = SIG_DFL;
                if (handler == HANDLER_EXIT)
                        action->sa.sa_flags |= SA_IMMUTABLE;
                if (blocked) {
@@ -1816,7 +1820,11 @@ int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
        info.si_perf_data = sig_data;
        info.si_perf_type = type;
 
Quoted 11 lines

kernel/signal.c int force_sig_perf(void __user *addr, u32 type, u64 sig_data) · line 1827

Quoted 6 lines
kernel/signal.c +5 -1
-       return force_sig_info(&info);
+       /*
+        * Delivering SIGTRAP on perf events must unblock delivery to not
+        * kill the task, but attempt delivery to the user space handler.
+        */
+       return force_sig_info_to_task(&info, current, HANDLER_UNBLOCK);

It seems that in this case we almost don't use any of the logic in force_sig_info_to_task(). It effectively reduces to the call to send_signal() protected by the lock. Maybe we should call something like do_send_sig_info() directly?

Unfortunately not -- without this:

	[...]
	blocked = sigismember(&t->blocked, sig);
	if (blocked || ignored || (handler != HANDLER_CURRENT)) {
		[...]
		if (blocked) {
			sigdelset(&t->blocked, sig);
			recalc_sigpending_and_wake(t);
		}
	}
	[...]

, it doesn't work if blocked==true. The alternative is to introduce another helper, force_sig_info_unblockable() or something, but don't see the benefit. Having it all in force_sig_info_to_task() seems cleaner and we avoid replicating any unblock logic for forced signals.

Thanks,
-- Marco

#3 lore raw
In reply to Marco Elver
Quoted 5 lines · Marco Elver

Hello,

Currently force_sig_info_to_task() will always unblock a blocked signal but deliver the signal to SIG_DFL:

Quoted 8 lines

[...]
* Note: If we unblock the signal, we always reset it to SIG_DFL,
* since we do not want to have a signal handler that was blocked
* be invoked when user space had explicitly blocked it.
[...]

Is this requirement part of the POSIX spec? Or is the intent simply to attempt to do the least-bad thing?

I have not found any POSIX language about this.

The options are either we terminate the application, or the application spins forever re-triggering the trap.

Quoted 12 lines

The reason I'm asking is that we've encountered rare crashes with the new SIGTRAP on perf events, due to patterns like this:

	<set up SIGTRAP on a perf event>
	...
	sigset_t s;
	sigemptyset(&s);
	sigaddset(&s, SIGTRAP | <and others>);
	sigprocmask(SIG_BLOCK, &s, ...);
	...
	<perf event triggers>
Quoted 7 lines

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

For other types of signals, is the assumption here that if user space blocked the signal, it might not be able to handle it in the first place?

The assumption is that the signals are synchronous and it is the actions of userspace that trigger the trap.

Quoted 6 lines

For SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space!

Which is exactly what we have. If you block it you get terminated.

Quoted 5 lines

If there is no good reason to choose SIG_DFL, our preference would be to allow this kind of "unblockable forced" signal to the user space handler for force_sig_perf() -- with the caveat whoever requests SIGTRAP on perf events must be able to provide a handler that can always run safely. But we think that's better than crashing.

When the signal is blocked there is not user space signal handler. There is nothing reasonable the kernel can do.

Quoted 4 lines

The below patch would do what we want, but would like to first confirm if this is "within spec".

Thoughts?

I think HANDLER_UNBLOCK is pretty much nonsense.

A block signal very much means that userspace is not prepared to handle the signal. So calling something that is not ready to be called can't work. That is common sense, and I expect in POSIX as well.

I expect that either you are looking for something like what ptrace does with signal interruptions where another process is notified, and userspace does not need to be involved, or that this is a don't do that then.

Or possibly you have some weird asynchronous signal thing happening and you are calling it synchronous.

Eric

Quoted 96 lines

Thanks,
-- Marco

------ >8 ------

From: Marco Elver <[email protected]>
Date: Mon, 21 Mar 2022 22:18:09 +0100
Subject: [PATCH RFC] signal: Always unblock signal to user space for
force_sig_perf()

With SIGTRAP on perf events, we have encountered termination of processes due to user space attempting to block delivery of SIGTRAP. Consider this case:

	<set up SIGTRAP on a perf event>
	...
	sigset_t s;
	sigemptyset(&s);
	sigaddset(&s, SIGTRAP | <and others>);
	sigprocmask(SIG_BLOCK, &s, ...);
	...
	<perf event triggers>

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

With forced signals, the whole premise of sigprocmask() is invalidated since the signal is still delivered, only to the default signal handler. The assumption here is that if user space blocked the signal, it might not be able to handle it in the first place.

However, for SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space.

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")
---
 kernel/signal.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
kernel/signal.c +13 -5
index 38602738866e..04b7a178a5f3 100644
@@ -1303,6 +1303,7 @@ int do_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p
 
 enum sig_handler {
 	HANDLER_CURRENT, /* If reachable use the current handler */
+	HANDLER_UNBLOCK, /* Use the current handler even if blocked */
 	HANDLER_SIG_DFL, /* Always use SIG_DFL handler semantics */
 	HANDLER_EXIT,	 /* Only visible as the process exit code */
 };
@@ -1311,9 +1312,11 @@ enum sig_handler {
  * Force a signal that the process can't ignore: if necessary
  * we unblock the signal and change any SIG_IGN to SIG_DFL.
  *
- * Note: If we unblock the signal, we always reset it to SIG_DFL,
- * since we do not want to have a signal handler that was blocked
- * be invoked when user space had explicitly blocked it.
+ * Note: If we unblock the signal and handler != HANDLER_UNBLOCK, we always
+ * reset it to SIG_DFL, since we do not want to have a signal handler that was
+ * blocked be invoked when user space had explicitly blocked it. If handler is
+ * HANDLER_UNBLOCK, user space will always receive the signal and is expected to
+ * provide a handler that is safe in all contexts.
  *
  * We don't want to have recursive SIGSEGV's etc, for example,
  * that is why we also clear SIGNAL_UNKILLABLE.
@@ -1332,7 +1335,8 @@ force_sig_info_to_task(struct kernel_siginfo *info, struct task_struct *t,
 	ignored = action->sa.sa_handler == SIG_IGN;
 	blocked = sigismember(&t->blocked, sig);
 	if (blocked || ignored || (handler != HANDLER_CURRENT)) {
-		action->sa.sa_handler = SIG_DFL;
+		if (handler != HANDLER_UNBLOCK)
+			action->sa.sa_handler = SIG_DFL;
 		if (handler == HANDLER_EXIT)
 			action->sa.sa_flags |= SA_IMMUTABLE;
 		if (blocked) {
@@ -1816,7 +1820,11 @@ int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
 	info.si_perf_data = sig_data;
 	info.si_perf_type = type;
 
-	return force_sig_info(&info);
+	/*
+	 * Delivering SIGTRAP on perf events must unblock delivery to not
+	 * kill the task, but attempt delivery to the user space handler.
+	 */
+	return force_sig_info_to_task(&info, current, HANDLER_UNBLOCK);
 }
 
 /**
#4 lore raw
Quoted 13 lines · Eric W. Biederman
Quoted 11 lines · Marco Elver

Hello,

Currently force_sig_info_to_task() will always unblock a blocked signal but deliver the signal to SIG_DFL:

[...]
* Note: If we unblock the signal, we always reset it to SIG_DFL,
* since we do not want to have a signal handler that was blocked
* be invoked when user space had explicitly blocked it.
[...]

Quoted 7 lines
Quoted 2 lines

Is this requirement part of the POSIX spec? Or is the intent simply to attempt to do the least-bad thing?

I have not found any POSIX language about this.

The options are either we terminate the application, or the application spins forever re-triggering the trap.

Is this in case of things like SEGV? I think this doesn't quite apply to us. The cause of the signal (perf event) is rather benign, and the signal handler can deal with recursion.

[...]

Quoted 8 lines
Quoted 6 lines

For SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space!

Which is exactly what we have. If you block it you get terminated.

Right, however, in this case we want to monitor/trace memory accesses etc, and some 3rd party code such as a library being traced isn't under our control.

What we can do instead is to intercept sigprocmask() and work around the issue, but libc interception is brittle. :-/
We do just want to receive the signal, all the time.

[...]

Quoted 5 lines

I think HANDLER_UNBLOCK is pretty much nonsense.

A block signal very much means that userspace is not prepared to handle the signal. So calling something that is not ready to be called can't work. That is common sense, and I expect in POSIX as well.

The fundamental question is, if we have a valid signal handler, but sigprocmask() is called, how do we still keep receiving signals for SIGTRAP despite sigprocmask()?

Perhaps this is impossible without intercepting sigprocmask() in user space, in which we'll need to find a different solution.

Quoted 7 lines

I expect that either you are looking for something like what ptrace does with signal interruptions where another process is notified, and userspace does not need to be involved, or that this is a don't do that then.

Or possibly you have some weird asynchronous signal thing happening and you are calling it synchronous.

Not quite. We need it to be synchronous, because we need to know the precise instruction and potentially do some other stuff _before_ subsequent instructions.

A compromise might be to deliver synchronously normally, but when blocked deliver asynchronously. But if the signal was delivered asynchronously, we need to let the signal handler know delivery was asynchronous, so that our tracing logic can recover and give up at that point.

To do this indication if it was asynchronous, we probably need to extend siginfo_t once more. Would that be reasonable?

Thanks,
-- Marco

#5 lore raw
Quoted 17 lines · Marco Elver
Quoted 16 lines · Eric W. Biederman
Quoted 13 lines · Marco Elver

Hello,

Currently force_sig_info_to_task() will always unblock a blocked signal but deliver the signal to SIG_DFL:

[...]
* Note: If we unblock the signal, we always reset it to SIG_DFL,
* since we do not want to have a signal handler that was blocked
* be invoked when user space had explicitly blocked it.
[...]

Is this requirement part of the POSIX spec? Or is the intent simply to attempt to do the least-bad thing?

Quoted 8 lines
Quoted 4 lines

I have not found any POSIX language about this.

The options are either we terminate the application, or the application spins forever re-triggering the trap.

Is this in case of things like SEGV? I think this doesn't quite apply to us. The cause of the signal (perf event) is rather benign, and the signal handler can deal with recursion.

Yes. Signals like SIGSEGV are what force_sig_info_to_task is used for.

Signals where a userspace instruction causes a fault and the signal is delivered immediately (synchronously) with that fault.

Quoted 25 lines

[...]

Quoted 8 lines
Quoted 6 lines

For SIGTRAP on perf events we found this makes the situation worse, since the cause of the signal wasn't an error condition, but explicitly requested monitoring. In this case, we do in fact want delivery of the signal to user space even if the signal is blocked, i.e. force_sig_perf() should be an unblockable forced synchronous signal to user space!

Which is exactly what we have. If you block it you get terminated.

Right, however, in this case we want to monitor/trace memory accesses etc, and some 3rd party code such as a library being traced isn't under our control.

What we can do instead is to intercept sigprocmask() and work around the issue, but libc interception is brittle. :-/
We do just want to receive the signal, all the time.

[...]

Quoted 5 lines

I think HANDLER_UNBLOCK is pretty much nonsense.

A block signal very much means that userspace is not prepared to handle the signal. So calling something that is not ready to be called can't work. That is common sense, and I expect in POSIX as well.

Quoted 6 lines

The fundamental question is, if we have a valid signal handler, but sigprocmask() is called, how do we still keep receiving signals for SIGTRAP despite sigprocmask()?

Perhaps this is impossible without intercepting sigprocmask() in user space, in which we'll need to find a different solution.

Or adding some kind of feature to the kernel where you can report that some signal is unblockable.

Quoted 12 lines
Quoted 7 lines

I expect that either you are looking for something like what ptrace does with signal interruptions where another process is notified, and userspace does not need to be involved, or that this is a don't do that then.

Or possibly you have some weird asynchronous signal thing happening and you are calling it synchronous.

Not quite. We need it to be synchronous, because we need to know the precise instruction and potentially do some other stuff _before_ subsequent instructions.

Quoted 8 lines

A compromise might be to deliver synchronously normally, but when blocked deliver asynchronously. But if the signal was delivered asynchronously, we need to let the signal handler know delivery was asynchronous, so that our tracing logic can recover and give up at that point.

To do this indication if it was asynchronous, we probably need to extend siginfo_t once more. Would that be reasonable?

So the idea is to use normal signal delivery but to set a flag to indicate that the signal was blocked at the time it was sent?

It should be possible to add another field that takes a non-zero value. On older kernels it should always have a value of zero so it should be safe.

It might also be possible to simply ignore the signal if it is blocked.

In either case it will probably take a little bit of care to get the races out.

Eric

#6 lore raw
Quoted 1 line · Eric W. Biederman

Marco Elver <[email protected]> writes:

[...]

Quoted 16 lines
Quoted 4 lines

Not quite. We need it to be synchronous, because we need to know the precise instruction and potentially do some other stuff _before_ subsequent instructions.

Quoted 8 lines

A compromise might be to deliver synchronously normally, but when blocked deliver asynchronously. But if the signal was delivered asynchronously, we need to let the signal handler know delivery was asynchronous, so that our tracing logic can recover and give up at that point.

To do this indication if it was asynchronous, we probably need to extend siginfo_t once more. Would that be reasonable?

So the idea is to use normal signal delivery but to set a flag to indicate that the signal was blocked at the time it was sent?

Quoted 8 lines

It should be possible to add another field that takes a non-zero value. On older kernels it should always have a value of zero so it should be safe.

It might also be possible to simply ignore the signal if it is blocked.

In either case it will probably take a little bit of care to get the races out.

So I gave the "synchronous if possible, but if async let handler know" version a shot (below). As long as we know if the signal being delivered is asynchronous we're good, and not terminating the process.

Does that look more reasonable?

Thanks,
-- Marco

------ >8 ------

 arch/arm/kernel/signal.c           |  1 +
 arch/arm64/kernel/signal.c         |  1 +
 arch/arm64/kernel/signal32.c       |  1 +
 arch/m68k/kernel/signal.c          |  1 +
 arch/sparc/kernel/signal32.c       |  1 +
 arch/sparc/kernel/signal_64.c      |  1 +
 arch/x86/kernel/signal_compat.c    |  2 ++
 include/linux/compat.h             |  1 +
 include/linux/sched/signal.h       |  2 +-
 include/uapi/asm-generic/siginfo.h |  2 ++
 kernel/events/core.c               |  4 ++--
 kernel/signal.c                    | 16 ++++++++++++++--
 12 files changed, 28 insertions(+), 5 deletions(-)
arch/arm/kernel/signal.c +1 -0
index c532a6041066..5ba4e1d0813d 100644
@@ -708,6 +708,7 @@ static_assert(offsetof(siginfo_t, si_upper)	== 0x18);
 static_assert(offsetof(siginfo_t, si_pkey)	== 0x14);
 static_assert(offsetof(siginfo_t, si_perf_data)	== 0x10);
 static_assert(offsetof(siginfo_t, si_perf_type)	== 0x14);
+static_assert(offsetof(siginfo_t, si_perf_async) == 0x18);
 static_assert(offsetof(siginfo_t, si_band)	== 0x0c);
 static_assert(offsetof(siginfo_t, si_fd)	== 0x10);
 static_assert(offsetof(siginfo_t, si_call_addr)	== 0x0c);
arch/arm64/kernel/signal.c +1 -0
index d8aaf4b6f432..a6a3bf49ed53 100644
@@ -1010,6 +1010,7 @@ static_assert(offsetof(siginfo_t, si_upper)	== 0x28);
 static_assert(offsetof(siginfo_t, si_pkey)	== 0x20);
 static_assert(offsetof(siginfo_t, si_perf_data)	== 0x18);
 static_assert(offsetof(siginfo_t, si_perf_type)	== 0x20);
+static_assert(offsetof(siginfo_t, si_perf_async) == 0x24);
 static_assert(offsetof(siginfo_t, si_band)	== 0x10);
 static_assert(offsetof(siginfo_t, si_fd)	== 0x18);
 static_assert(offsetof(siginfo_t, si_call_addr)	== 0x10);
arch/arm64/kernel/signal32.c +1 -0
index d984282b979f..0f72cdadd43e 100644
@@ -487,6 +487,7 @@ static_assert(offsetof(compat_siginfo_t, si_upper)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_pkey)	== 0x14);
 static_assert(offsetof(compat_siginfo_t, si_perf_data)	== 0x10);
 static_assert(offsetof(compat_siginfo_t, si_perf_type)	== 0x14);
+static_assert(offsetof(compat_siginfo_t, si_perf_async)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_band)	== 0x0c);
 static_assert(offsetof(compat_siginfo_t, si_fd)		== 0x10);
 static_assert(offsetof(compat_siginfo_t, si_call_addr)	== 0x0c);
arch/m68k/kernel/signal.c +1 -0
index 338817d0cb3f..c6332727f82f 100644
@@ -625,6 +625,7 @@ static inline void siginfo_build_tests(void)
 	/* _sigfault._perf */
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x10);
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x14);
+	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_async) != 0x18);
 
 	/* _sigpoll */
 	BUILD_BUG_ON(offsetof(siginfo_t, si_band)   != 0x0c);
arch/sparc/kernel/signal32.c +1 -0
index 6cc124a3bb98..b127649ac224 100644
@@ -780,5 +780,6 @@ static_assert(offsetof(compat_siginfo_t, si_upper)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_pkey)	== 0x14);
 static_assert(offsetof(compat_siginfo_t, si_perf_data)	== 0x10);
 static_assert(offsetof(compat_siginfo_t, si_perf_type)	== 0x14);
+static_assert(offsetof(compat_siginfo_t, si_perf_async)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_band)	== 0x0c);
 static_assert(offsetof(compat_siginfo_t, si_fd)		== 0x10);
arch/sparc/kernel/signal_64.c +1 -0
index 2a78d2af1265..dfa42d9347d8 100644
@@ -590,5 +590,6 @@ static_assert(offsetof(siginfo_t, si_upper)	== 0x28);
 static_assert(offsetof(siginfo_t, si_pkey)	== 0x20);
 static_assert(offsetof(siginfo_t, si_perf_data)	== 0x18);
 static_assert(offsetof(siginfo_t, si_perf_type)	== 0x20);
+static_assert(offsetof(siginfo_t, si_perf_async) == 0x24);
 static_assert(offsetof(siginfo_t, si_band)	== 0x10);
 static_assert(offsetof(siginfo_t, si_fd)	== 0x14);
arch/x86/kernel/signal_compat.c +2 -0
index b52407c56000..1f1ccbc476d1 100644
@@ -149,8 +149,10 @@ static inline void signal_compat_build_tests(void)
 
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x18);
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x20);
+	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_async) != 0x24);
 	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_data) != 0x10);
 	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_type) != 0x14);
+	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_async) != 0x18);
 
 	CHECK_CSI_OFFSET(_sigpoll);
 	CHECK_CSI_SIZE  (_sigpoll, 2*sizeof(int));
include/linux/compat.h +1 -0
index 1c758b0e0359..36684b97075d 100644
@@ -235,6 +235,7 @@ typedef struct compat_siginfo {
 				struct {
 					compat_ulong_t _data;
 					u32 _type;
+					u32 _async;
 				} _perf;
 			};
 		} _sigfault;
include/linux/sched/signal.h +1 -1
index b6ecb9fc4cd2..9924fe7559a0 100644
@@ -320,7 +320,7 @@ int send_sig_mceerr(int code, void __user *, short, struct task_struct *);
 
 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper);
 int force_sig_pkuerr(void __user *addr, u32 pkey);
-int force_sig_perf(void __user *addr, u32 type, u64 sig_data);
+int send_sig_perf(void __user *addr, u32 type, u64 sig_data);
 
 int force_sig_ptrace_errno_trap(int errno, void __user *addr);
 int force_sig_fault_trapno(int sig, int code, void __user *addr, int trapno);
include/uapi/asm-generic/siginfo.h +2 -0
index 3ba180f550d7..835eea023630 100644
@@ -99,6 +99,7 @@ union __sifields {
 			struct {
 				unsigned long _data;
 				__u32 _type;
+				__u32 _async;
 			} _perf;
 		};
 	} _sigfault;
@@ -164,6 +165,7 @@ typedef struct siginfo {
 #define si_pkey		_sifields._sigfault._addr_pkey._pkey
 #define si_perf_data	_sifields._sigfault._perf._data
 #define si_perf_type	_sifields._sigfault._perf._type
+#define si_perf_async	_sifields._sigfault._perf._async
 #define si_band		_sifields._sigpoll._band
 #define si_fd		_sifields._sigpoll._fd
 #define si_call_addr	_sifields._sigsys._call_addr
kernel/events/core.c +2 -2
index 57c7197838db..8f2f251465e9 100644
@@ -6533,8 +6533,8 @@ static void perf_sigtrap(struct perf_event *event)
 	if (current->flags & PF_EXITING)
 		return;
 
-	force_sig_perf((void __user *)event->pending_addr,
-		       event->attr.type, event->attr.sig_data);
+	send_sig_perf((void __user *)event->pending_addr,
+		      event->attr.type, event->attr.sig_data);
 }
 
 static void perf_pending_event_disable(struct perf_event *event)
kernel/signal.c +14 -2
index 38602738866e..9f8d1ed88dcb 100644
@@ -1804,7 +1804,7 @@ int force_sig_pkuerr(void __user *addr, u32 pkey)
 }
 #endif
 
-int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
+int send_sig_perf(void __user *addr, u32 type, u64 sig_data)
 {
 	struct kernel_siginfo info;
 
@@ -1816,7 +1816,16 @@ int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
 	info.si_perf_data = sig_data;
 	info.si_perf_type = type;
 
-	return force_sig_info(&info);
+	/*
+	 * Signals generated by perf events should not terminate the whole
+	 * process if SIGTRAP is blocked, however, delivering the signal
+	 * asynchronously is better than not delivering at all. But tell user
+	 * space if the signal was asynchronous, so it can clearly be
+	 * distinguished from normal synchronous ones.
+	 */
+	info.si_perf_async = sigismember(&current->blocked, info.si_signo);
+
+	return send_sig_info(info.si_signo, &info, current);
 }
 
 /**
@@ -3429,6 +3438,7 @@ void copy_siginfo_to_external32(struct compat_siginfo *to,
 		to->si_addr = ptr_to_compat(from->si_addr);
 		to->si_perf_data = from->si_perf_data;
 		to->si_perf_type = from->si_perf_type;
+		to->si_perf_async = from->si_perf_async;
 		break;
 	case SIL_CHLD:
 		to->si_pid = from->si_pid;
@@ -3506,6 +3516,7 @@ static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
 		to->si_addr = compat_ptr(from->si_addr);
 		to->si_perf_data = from->si_perf_data;
 		to->si_perf_type = from->si_perf_type;
+		to->si_perf_async = from->si_perf_async;
 		break;
 	case SIL_CHLD:
 		to->si_pid    = from->si_pid;
@@ -4719,6 +4730,7 @@ static inline void siginfo_buildtime_checks(void)
 	CHECK_OFFSET(si_pkey);
 	CHECK_OFFSET(si_perf_data);
 	CHECK_OFFSET(si_perf_type);
+	CHECK_OFFSET(si_perf_async);
 
 	/* sigpoll */
 	CHECK_OFFSET(si_band);
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#7 lore raw
Quoted 144 lines · Marco Elver
Quoted 1 line · Eric W. Biederman

Marco Elver <[email protected]> writes:

[...]

Quoted 16 lines
Quoted 4 lines

Not quite. We need it to be synchronous, because we need to know the precise instruction and potentially do some other stuff _before_ subsequent instructions.

Quoted 8 lines

A compromise might be to deliver synchronously normally, but when blocked deliver asynchronously. But if the signal was delivered asynchronously, we need to let the signal handler know delivery was asynchronous, so that our tracing logic can recover and give up at that point.

To do this indication if it was asynchronous, we probably need to extend siginfo_t once more. Would that be reasonable?

So the idea is to use normal signal delivery but to set a flag to indicate that the signal was blocked at the time it was sent?

Quoted 8 lines

It should be possible to add another field that takes a non-zero value. On older kernels it should always have a value of zero so it should be safe.

It might also be possible to simply ignore the signal if it is blocked.

In either case it will probably take a little bit of care to get the races out.

So I gave the "synchronous if possible, but if async let handler know" version a shot (below). As long as we know if the signal being delivered is asynchronous we're good, and not terminating the process.

Does that look more reasonable?

Thanks,
-- Marco

------ >8 ------

 arch/arm/kernel/signal.c           |  1 +
 arch/arm64/kernel/signal.c         |  1 +
 arch/arm64/kernel/signal32.c       |  1 +
 arch/m68k/kernel/signal.c          |  1 +
 arch/sparc/kernel/signal32.c       |  1 +
 arch/sparc/kernel/signal_64.c      |  1 +
 arch/x86/kernel/signal_compat.c    |  2 ++
 include/linux/compat.h             |  1 +
 include/linux/sched/signal.h       |  2 +-
 include/uapi/asm-generic/siginfo.h |  2 ++
 kernel/events/core.c               |  4 ++--
 kernel/signal.c                    | 16 ++++++++++++++--
 12 files changed, 28 insertions(+), 5 deletions(-)
arch/arm/kernel/signal.c +1 -0
index c532a6041066..5ba4e1d0813d 100644
@@ -708,6 +708,7 @@ static_assert(offsetof(siginfo_t, si_upper) == 0x18);
 static_assert(offsetof(siginfo_t, si_pkey)     == 0x14);
 static_assert(offsetof(siginfo_t, si_perf_data)        == 0x10);
 static_assert(offsetof(siginfo_t, si_perf_type)        == 0x14);
+static_assert(offsetof(siginfo_t, si_perf_async) == 0x18);
 static_assert(offsetof(siginfo_t, si_band)     == 0x0c);
 static_assert(offsetof(siginfo_t, si_fd)       == 0x10);
 static_assert(offsetof(siginfo_t, si_call_addr)        == 0x0c);
arch/arm64/kernel/signal.c +1 -0
index d8aaf4b6f432..a6a3bf49ed53 100644
@@ -1010,6 +1010,7 @@ static_assert(offsetof(siginfo_t, si_upper)       == 0x28);
 static_assert(offsetof(siginfo_t, si_pkey)     == 0x20);
 static_assert(offsetof(siginfo_t, si_perf_data)        == 0x18);
 static_assert(offsetof(siginfo_t, si_perf_type)        == 0x20);
+static_assert(offsetof(siginfo_t, si_perf_async) == 0x24);
 static_assert(offsetof(siginfo_t, si_band)     == 0x10);
 static_assert(offsetof(siginfo_t, si_fd)       == 0x18);
 static_assert(offsetof(siginfo_t, si_call_addr)        == 0x10);
arch/arm64/kernel/signal32.c +1 -0
index d984282b979f..0f72cdadd43e 100644
@@ -487,6 +487,7 @@ static_assert(offsetof(compat_siginfo_t, si_upper)  == 0x18);
 static_assert(offsetof(compat_siginfo_t, si_pkey)      == 0x14);
 static_assert(offsetof(compat_siginfo_t, si_perf_data) == 0x10);
 static_assert(offsetof(compat_siginfo_t, si_perf_type) == 0x14);
+static_assert(offsetof(compat_siginfo_t, si_perf_async)        == 0x18);
 static_assert(offsetof(compat_siginfo_t, si_band)      == 0x0c);
 static_assert(offsetof(compat_siginfo_t, si_fd)                == 0x10);
 static_assert(offsetof(compat_siginfo_t, si_call_addr) == 0x0c);
arch/m68k/kernel/signal.c +1 -0
index 338817d0cb3f..c6332727f82f 100644
@@ -625,6 +625,7 @@ static inline void siginfo_build_tests(void)
        /* _sigfault._perf */
        BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x10);
        BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x14);
+       BUILD_BUG_ON(offsetof(siginfo_t, si_perf_async) != 0x18);
 
        /* _sigpoll */
        BUILD_BUG_ON(offsetof(siginfo_t, si_band)   != 0x0c);
arch/sparc/kernel/signal32.c +1 -0
index 6cc124a3bb98..b127649ac224 100644
@@ -780,5 +780,6 @@ static_assert(offsetof(compat_siginfo_t, si_upper)  == 0x18);
 static_assert(offsetof(compat_siginfo_t, si_pkey)      == 0x14);
 static_assert(offsetof(compat_siginfo_t, si_perf_data) == 0x10);
 static_assert(offsetof(compat_siginfo_t, si_perf_type) == 0x14);
+static_assert(offsetof(compat_siginfo_t, si_perf_async)        == 0x18);
 static_assert(offsetof(compat_siginfo_t, si_band)      == 0x0c);
 static_assert(offsetof(compat_siginfo_t, si_fd)                == 0x10);
arch/sparc/kernel/signal_64.c +1 -0
index 2a78d2af1265..dfa42d9347d8 100644
@@ -590,5 +590,6 @@ static_assert(offsetof(siginfo_t, si_upper) == 0x28);
 static_assert(offsetof(siginfo_t, si_pkey)     == 0x20);
 static_assert(offsetof(siginfo_t, si_perf_data)        == 0x18);
 static_assert(offsetof(siginfo_t, si_perf_type)        == 0x20);
+static_assert(offsetof(siginfo_t, si_perf_async) == 0x24);
 static_assert(offsetof(siginfo_t, si_band)     == 0x10);
 static_assert(offsetof(siginfo_t, si_fd)       == 0x14);
arch/x86/kernel/signal_compat.c +2 -0
index b52407c56000..1f1ccbc476d1 100644
@@ -149,8 +149,10 @@ static inline void signal_compat_build_tests(void)
 
        BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x18);
        BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x20);
+       BUILD_BUG_ON(offsetof(siginfo_t, si_perf_async) != 0x24);
        BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_data) != 0x10);
        BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_type) != 0x14);
+       BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_async) != 0x18);
 
        CHECK_CSI_OFFSET(_sigpoll);
        CHECK_CSI_SIZE  (_sigpoll, 2*sizeof(int));
include/linux/compat.h +0 -0
index 1c758b0e0359..36684b97075d 100644

include/linux/compat.h typedef struct compat_siginfo { · line 238

Quoted 4 lines
include/linux/compat.h +1 -0
@@ -235,6 +235,7 @@ typedef struct compat_siginfo {
                                struct {
                                        compat_ulong_t _data;
                                        u32 _type;
+                                       u32 _async;

Does it make sense to make this u8 and/or rename to flags? In case we will need to shove move info here in future.

Quoted 109 lines
include/linux/compat.h +0 -0
                                } _perf;
                        };
                } _sigfault;
include/linux/sched/signal.h +1 -1
index b6ecb9fc4cd2..9924fe7559a0 100644
@@ -320,7 +320,7 @@ int send_sig_mceerr(int code, void __user *, short, struct task_struct *);
 
 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper);
 int force_sig_pkuerr(void __user *addr, u32 pkey);
-int force_sig_perf(void __user *addr, u32 type, u64 sig_data);
+int send_sig_perf(void __user *addr, u32 type, u64 sig_data);
 
 int force_sig_ptrace_errno_trap(int errno, void __user *addr);
 int force_sig_fault_trapno(int sig, int code, void __user *addr, int trapno);
include/uapi/asm-generic/siginfo.h +2 -0
index 3ba180f550d7..835eea023630 100644
@@ -99,6 +99,7 @@ union __sifields {
                        struct {
                                unsigned long _data;
                                __u32 _type;
+                               __u32 _async;
                        } _perf;
                };
        } _sigfault;
@@ -164,6 +165,7 @@ typedef struct siginfo {
 #define si_pkey                _sifields._sigfault._addr_pkey._pkey
 #define si_perf_data   _sifields._sigfault._perf._data
 #define si_perf_type   _sifields._sigfault._perf._type
+#define si_perf_async  _sifields._sigfault._perf._async
 #define si_band                _sifields._sigpoll._band
 #define si_fd          _sifields._sigpoll._fd
 #define si_call_addr   _sifields._sigsys._call_addr
kernel/events/core.c +2 -2
index 57c7197838db..8f2f251465e9 100644
@@ -6533,8 +6533,8 @@ static void perf_sigtrap(struct perf_event *event)
        if (current->flags & PF_EXITING)
                return;
 
-       force_sig_perf((void __user *)event->pending_addr,
-                      event->attr.type, event->attr.sig_data);
+       send_sig_perf((void __user *)event->pending_addr,
+                     event->attr.type, event->attr.sig_data);
 }
 
 static void perf_pending_event_disable(struct perf_event *event)
kernel/signal.c +14 -3
index 38602738866e..9f8d1ed88dcb 100644
@@ -1804,7 +1804,7 @@ int force_sig_pkuerr(void __user *addr, u32 pkey)
 }
 #endif
 
-int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
+int send_sig_perf(void __user *addr, u32 type, u64 sig_data)
 {
        struct kernel_siginfo info;
 
@@ -1816,7 +1816,16 @@ int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
        info.si_perf_data = sig_data;
        info.si_perf_type = type;
 
-       return force_sig_info(&info);
+       /*
+        * Signals generated by perf events should not terminate the whole
+        * process if SIGTRAP is blocked, however, delivering the signal
+        * asynchronously is better than not delivering at all. But tell user
+        * space if the signal was asynchronous, so it can clearly be
+        * distinguished from normal synchronous ones.
+        */
+       info.si_perf_async = sigismember(&current->blocked, info.si_signo);
+
+       return send_sig_info(info.si_signo, &info, current);
 }
 
 /**
@@ -3429,6 +3438,7 @@ void copy_siginfo_to_external32(struct compat_siginfo *to,
                to->si_addr = ptr_to_compat(from->si_addr);
                to->si_perf_data = from->si_perf_data;
                to->si_perf_type = from->si_perf_type;
+               to->si_perf_async = from->si_perf_async;
                break;
        case SIL_CHLD:
                to->si_pid = from->si_pid;
@@ -3506,6 +3516,7 @@ static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
                to->si_addr = compat_ptr(from->si_addr);
                to->si_perf_data = from->si_perf_data;
                to->si_perf_type = from->si_perf_type;
+               to->si_perf_async = from->si_perf_async;
                break;
        case SIL_CHLD:
                to->si_pid    = from->si_pid;
@@ -4719,6 +4730,7 @@ static inline void siginfo_buildtime_checks(void)
        CHECK_OFFSET(si_pkey);
        CHECK_OFFSET(si_perf_data);
        CHECK_OFFSET(si_perf_type);
+       CHECK_OFFSET(si_perf_async);
 
        /* sigpoll */
        CHECK_OFFSET(si_band);
--

2.35.1.894.gb6a874cedc-goog

#8 lore raw

On Thu, Mar 24, 2022 at 08:15AM +0100, Dmitry Vyukov wrote:
[...]

Quoted 4 lines
Quoted 1 line

+ u32 _async;

Does it make sense to make this u8 and/or rename to flags? In case we will need to shove move info here in future.

See v0.3 below. I intend to send v1 next week after 5.18-rc1.

If there are any comments in the meantime, please shout.

Thanks,
-- Marco

------ >8 ------

From: Marco Elver <[email protected]>
Date: Wed, 23 Mar 2022 13:36:45 +0100
Subject: [PATCH RFC] signal: Deliver SIGTRAP on perf event asynchronously if
blocked

With SIGTRAP on perf events, we have encountered termination of processes due to user space attempting to block delivery of SIGTRAP. Consider this case:

    <set up SIGTRAP on a perf event>
    ...
    sigset_t s;
    sigemptyset(&s);
    sigaddset(&s, SIGTRAP | <and others>);
    sigprocmask(SIG_BLOCK, &s, ...);
    ...
    <perf event triggers>

When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() will force the signal, but revert back to the default handler, thus terminating the task.

This makes sense for error conditions, but not so much for explicitly requested monitoring. However, the expectation is still that signals generated by perf events are synchronous, which will no longer be the case if the signal is blocked and delivered later.

To give user space the ability to clearly distinguish synchronous from asynchronous signals, introduce siginfo_t::si_perf_flags and TRAP_PERF_FLAG_ASYNC (opted for flags in case more binary information is required in future).

The resolution to the problem is then to (a) no longer force the signal (avoiding the terminations), but (b) tell user space via si_perf_flags if the signal was synchronous or not, so that such signals can be handled differently (e.g. let user space decide to ignore or consider the data imprecise).

The alternative of making the kernel ignore SIGTRAP on perf events if the signal is blocked may work for some usecases, but likely causes issues in others that then have to revert back to interception of sigprocmask() (which we want to avoid). [ A concrete example: when using breakpoint perf events to track data-flow, in a region of code where signals are blocked, data-flow can no longer be tracked accurately. When a relevant asynchronous signal is received after unblocking the signal, the data-flow tracking logic needs to know its state is imprecise. ]

Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")
---
 arch/arm/kernel/signal.c           |  1 +
 arch/arm64/kernel/signal.c         |  1 +
 arch/arm64/kernel/signal32.c       |  1 +
 arch/m68k/kernel/signal.c          |  1 +
 arch/sparc/kernel/signal32.c       |  1 +
 arch/sparc/kernel/signal_64.c      |  1 +
 arch/x86/kernel/signal_compat.c    |  2 ++
 include/linux/compat.h             |  1 +
 include/linux/sched/signal.h       |  2 +-
 include/uapi/asm-generic/siginfo.h |  7 +++++++
 kernel/events/core.c               |  4 ++--
 kernel/signal.c                    | 18 ++++++++++++++++--
 12 files changed, 35 insertions(+), 5 deletions(-)
arch/arm/kernel/signal.c +1 -0
index c532a6041066..8111147f7c42 100644
@@ -708,6 +708,7 @@ static_assert(offsetof(siginfo_t, si_upper)	== 0x18);
 static_assert(offsetof(siginfo_t, si_pkey)	== 0x14);
 static_assert(offsetof(siginfo_t, si_perf_data)	== 0x10);
 static_assert(offsetof(siginfo_t, si_perf_type)	== 0x14);
+static_assert(offsetof(siginfo_t, si_perf_flags) == 0x18);
 static_assert(offsetof(siginfo_t, si_band)	== 0x0c);
 static_assert(offsetof(siginfo_t, si_fd)	== 0x10);
 static_assert(offsetof(siginfo_t, si_call_addr)	== 0x0c);
arch/arm64/kernel/signal.c +1 -0
index d8aaf4b6f432..fd55d1848a2b 100644
@@ -1010,6 +1010,7 @@ static_assert(offsetof(siginfo_t, si_upper)	== 0x28);
 static_assert(offsetof(siginfo_t, si_pkey)	== 0x20);
 static_assert(offsetof(siginfo_t, si_perf_data)	== 0x18);
 static_assert(offsetof(siginfo_t, si_perf_type)	== 0x20);
+static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24);
 static_assert(offsetof(siginfo_t, si_band)	== 0x10);
 static_assert(offsetof(siginfo_t, si_fd)	== 0x18);
 static_assert(offsetof(siginfo_t, si_call_addr)	== 0x10);
arch/arm64/kernel/signal32.c +1 -0
index d984282b979f..4700f8522d27 100644
@@ -487,6 +487,7 @@ static_assert(offsetof(compat_siginfo_t, si_upper)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_pkey)	== 0x14);
 static_assert(offsetof(compat_siginfo_t, si_perf_data)	== 0x10);
 static_assert(offsetof(compat_siginfo_t, si_perf_type)	== 0x14);
+static_assert(offsetof(compat_siginfo_t, si_perf_flags)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_band)	== 0x0c);
 static_assert(offsetof(compat_siginfo_t, si_fd)		== 0x10);
 static_assert(offsetof(compat_siginfo_t, si_call_addr)	== 0x0c);
arch/m68k/kernel/signal.c +1 -0
index 338817d0cb3f..74ee1e3013d7 100644
@@ -625,6 +625,7 @@ static inline void siginfo_build_tests(void)
 	/* _sigfault._perf */
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x10);
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x14);
+	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_flags) != 0x18);
 
 	/* _sigpoll */
 	BUILD_BUG_ON(offsetof(siginfo_t, si_band)   != 0x0c);
arch/sparc/kernel/signal32.c +1 -0
index 6cc124a3bb98..90ff7ff94ea7 100644
@@ -780,5 +780,6 @@ static_assert(offsetof(compat_siginfo_t, si_upper)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_pkey)	== 0x14);
 static_assert(offsetof(compat_siginfo_t, si_perf_data)	== 0x10);
 static_assert(offsetof(compat_siginfo_t, si_perf_type)	== 0x14);
+static_assert(offsetof(compat_siginfo_t, si_perf_flags)	== 0x18);
 static_assert(offsetof(compat_siginfo_t, si_band)	== 0x0c);
 static_assert(offsetof(compat_siginfo_t, si_fd)		== 0x10);
arch/sparc/kernel/signal_64.c +1 -0
index 2a78d2af1265..6eeb766987d1 100644
@@ -590,5 +590,6 @@ static_assert(offsetof(siginfo_t, si_upper)	== 0x28);
 static_assert(offsetof(siginfo_t, si_pkey)	== 0x20);
 static_assert(offsetof(siginfo_t, si_perf_data)	== 0x18);
 static_assert(offsetof(siginfo_t, si_perf_type)	== 0x20);
+static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24);
 static_assert(offsetof(siginfo_t, si_band)	== 0x10);
 static_assert(offsetof(siginfo_t, si_fd)	== 0x14);
arch/x86/kernel/signal_compat.c +2 -0
index b52407c56000..879ef8c72f5c 100644
@@ -149,8 +149,10 @@ static inline void signal_compat_build_tests(void)
 
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x18);
 	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x20);
+	BUILD_BUG_ON(offsetof(siginfo_t, si_perf_flags) != 0x24);
 	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_data) != 0x10);
 	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_type) != 0x14);
+	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_flags) != 0x18);
 
 	CHECK_CSI_OFFSET(_sigpoll);
 	CHECK_CSI_SIZE  (_sigpoll, 2*sizeof(int));
include/linux/compat.h +1 -0
index 1c758b0e0359..01fddf72a81f 100644
@@ -235,6 +235,7 @@ typedef struct compat_siginfo {
 				struct {
 					compat_ulong_t _data;
 					u32 _type;
+					u32 _flags;
 				} _perf;
 			};
 		} _sigfault;
include/linux/sched/signal.h +1 -1
index b6ecb9fc4cd2..9924fe7559a0 100644
@@ -320,7 +320,7 @@ int send_sig_mceerr(int code, void __user *, short, struct task_struct *);
 
 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper);
 int force_sig_pkuerr(void __user *addr, u32 pkey);
-int force_sig_perf(void __user *addr, u32 type, u64 sig_data);
+int send_sig_perf(void __user *addr, u32 type, u64 sig_data);
 
 int force_sig_ptrace_errno_trap(int errno, void __user *addr);
 int force_sig_fault_trapno(int sig, int code, void __user *addr, int trapno);
include/uapi/asm-generic/siginfo.h +7 -0
index 3ba180f550d7..ffbe4cec9f32 100644
@@ -99,6 +99,7 @@ union __sifields {
 			struct {
 				unsigned long _data;
 				__u32 _type;
+				__u32 _flags;
 			} _perf;
 		};
 	} _sigfault;
@@ -164,6 +165,7 @@ typedef struct siginfo {
 #define si_pkey		_sifields._sigfault._addr_pkey._pkey
 #define si_perf_data	_sifields._sigfault._perf._data
 #define si_perf_type	_sifields._sigfault._perf._type
+#define si_perf_flags	_sifields._sigfault._perf._flags
 #define si_band		_sifields._sigpoll._band
 #define si_fd		_sifields._sigpoll._fd
 #define si_call_addr	_sifields._sigsys._call_addr
@@ -270,6 +272,11 @@ typedef struct siginfo {
  * that are of the form: ((PTRACE_EVENT_XXX << 8) | SIGTRAP)
  */
 
+/*
+ * Flags for si_perf_flags if SIGTRAP si_code is TRAP_PERF.
+ */
+#define TRAP_PERF_FLAG_ASYNC (1u << 0)
+
 /*
  * SIGCHLD si_codes
  */
kernel/events/core.c +2 -2
index 57c7197838db..8f2f251465e9 100644
@@ -6533,8 +6533,8 @@ static void perf_sigtrap(struct perf_event *event)
 	if (current->flags & PF_EXITING)
 		return;
 
-	force_sig_perf((void __user *)event->pending_addr,
-		       event->attr.type, event->attr.sig_data);
+	send_sig_perf((void __user *)event->pending_addr,
+		      event->attr.type, event->attr.sig_data);
 }
 
 static void perf_pending_event_disable(struct perf_event *event)
kernel/signal.c +16 -2
index 38602738866e..c230a531b169 100644
@@ -1804,7 +1804,7 @@ int force_sig_pkuerr(void __user *addr, u32 pkey)
 }
 #endif
 
-int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
+int send_sig_perf(void __user *addr, u32 type, u64 sig_data)
 {
 	struct kernel_siginfo info;
 
@@ -1816,7 +1816,18 @@ int force_sig_perf(void __user *addr, u32 type, u64 sig_data)
 	info.si_perf_data = sig_data;
 	info.si_perf_type = type;
 
-	return force_sig_info(&info);
+	/*
+	 * Signals generated by perf events should not terminate the whole
+	 * process if SIGTRAP is blocked, however, delivering the signal
+	 * asynchronously is better than not delivering at all. But tell user
+	 * space if the signal was asynchronous, so it can clearly be
+	 * distinguished from normal synchronous ones.
+	 */
+	info.si_perf_flags = sigismember(&current->blocked, info.si_signo) ?
+				     TRAP_PERF_FLAG_ASYNC :
+				     0;
+
+	return send_sig_info(info.si_signo, &info, current);
 }
 
 /**
@@ -3429,6 +3440,7 @@ void copy_siginfo_to_external32(struct compat_siginfo *to,
 		to->si_addr = ptr_to_compat(from->si_addr);
 		to->si_perf_data = from->si_perf_data;
 		to->si_perf_type = from->si_perf_type;
+		to->si_perf_flags = from->si_perf_flags;
 		break;
 	case SIL_CHLD:
 		to->si_pid = from->si_pid;
@@ -3506,6 +3518,7 @@ static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
 		to->si_addr = compat_ptr(from->si_addr);
 		to->si_perf_data = from->si_perf_data;
 		to->si_perf_type = from->si_perf_type;
+		to->si_perf_flags = from->si_perf_flags;
 		break;
 	case SIL_CHLD:
 		to->si_pid    = from->si_pid;
@@ -4719,6 +4732,7 @@ static inline void siginfo_buildtime_checks(void)
 	CHECK_OFFSET(si_pkey);
 	CHECK_OFFSET(si_perf_data);
 	CHECK_OFFSET(si_perf_type);
+	CHECK_OFFSET(si_perf_flags);
 
 	/* sigpoll */
 	CHECK_OFFSET(si_band);
Signature · 1 line
2.35.1.1021.g381101b075-goog