Skip to contentSkip to conversation

sigaction.2: Updates for SEGV_CPERR

PATCH In review Yu-cheng Yu 3 participants 8 messages Feb 26, 2021 – Mar 12, 2021 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover sigaction.2: Updates for SEGV_CPERR

Control-flow Enforcement [1] [2] introduces a new control-protection fault, which is triggered by a branch instruction (call, ret, or jmp) violating branch-enforcement rules. When the signal is delivered, si_code is set to SEGV_CPERR. Add the code to sigaction.2, and while at it, clarify when si_addr is set.

[1] Intel 64 and IA-32 Architectures Software Developer's Manual:

    https://software.intel.com/en-us/download/intel-64-and-ia-32-
    architectures-sdm-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4

[2] CET Shadow Stack patches v21:

    https://lkml.kernel.org/r/[email protected]/
    https://lkml.kernel.org/r/[email protected]/

Yu-cheng Yu (2):
sigaction.2: Add SEGV_CPERR
sigaction.2: wfix - Clarify si_addr description.

 man2/sigaction.2 | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/2 sigaction.2: Add SEGV_CPERR

When a branch instruction (i.e. call/ret/jmp) triggers a control-protection fault, si_code is set to SEGV_CPERR. Add the new si_code to sigaction.2.

---
 man2/sigaction.2 | 3 +++
 1 file changed, 3 insertions(+)
man2/sigaction.2 +3 -0
index cc183198d..49a30f11e 100644
@@ -737,6 +737,9 @@ See
 .BR pkeys (7).
 The protection key which applied to this access is available via
 .IR si_pkey .
+.TP
+.B SEGV_CPERR
+A branch instruction (call, ret, or jmp) triggered a control-protection fault.
 .RE
 .PP
 The following values can be placed in
Signature · 1 line
2.21.0
#2 lore raw

PATCH 2/2 sigaction.2: wfix - Clarify si_addr description.

In reply to Yu-cheng Yu

SIGSEGV fills si_addr only for memory access faults. Add a note to clarify.

---
 man2/sigaction.2 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
man2/sigaction.2 +6 -1
index 49a30f11e..bea884a23 100644
@@ -467,7 +467,7 @@ and
 .BR SIGTRAP
 fill in
 .I si_addr
-with the address of the fault.
+with the address of the fault (see notes).
 On some architectures,
 these signals also fill in the
 .I si_trapno
@@ -955,6 +955,11 @@ It is not possible to block
 .IR sa_mask ).
 Attempts to do so are silently ignored.
 .PP
+In a
+.B SIGSEGV,
+if the fault is a memory access fault, si_addr is filled with the address
+causing the fault, otherwise it is not filled.
+.PP
 See
 .BR sigsetops (3)
 for details on manipulating signal sets.
Signature · 1 line
2.21.0
#3 lore raw

Re: [PATCH 2/2] sigaction.2: wfix - Clarify si_addr description.

Quoted 31 lines · Yu-cheng Yu

SIGSEGV fills si_addr only for memory access faults. Add a note to clarify.

---
 man2/sigaction.2 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
man2/sigaction.2 +1 -1
index 49a30f11e..bea884a23 100644
@@ -467,7 +467,7 @@ and
 .BR SIGTRAP
 fill in
 .I si_addr
-with the address of the fault.
+with the address of the fault (see notes).
 On some architectures,
 these signals also fill in the
 .I si_trapno

man2/sigaction.2 It is not possible to block · line 961

Quoted 7 lines
man2/sigaction.2 +4 -0
@@ -955,6 +955,11 @@ It is not possible to block
 .IR sa_mask ).
 Attempts to do so are silently ignored.
 .PP
+In a
+.B SIGSEGV,
+if the fault is a memory access fault, si_addr is filled with the address
+causing the fault, otherwise it is not filled.

"... otherwise it is uninitialized." or "zeroed" or whatever...

And I'm having trouble figuring out why do you need to clarify this?

Because of this sentence:

* SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAP fill in si_addr with the address of the fault. On some architectures, these signals also fill in the si_trapno field.

?

If so, did you audit all architectures whether si_addr is populated only on memory access faults or is this something POSIX dictates or what's up? Because the sigaction(2) manpage is arch-agnostic and this is a rather strong assertion.

What am I missing?

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw

Re: [PATCH 2/2] sigaction.2: wfix - Clarify si_addr description.

Quoted 44 lines · Borislav Petkov
Quoted 31 lines · Yu-cheng Yu

SIGSEGV fills si_addr only for memory access faults. Add a note to clarify.

---
  man2/sigaction.2 | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
man2/sigaction.2 +1 -1
index 49a30f11e..bea884a23 100644
@@ -467,7 +467,7 @@ and
  .BR SIGTRAP
  fill in
  .I si_addr
-with the address of the fault.
+with the address of the fault (see notes).
  On some architectures,
  these signals also fill in the
  .I si_trapno

man2/sigaction.2 It is not possible to block · line 961

Quoted 7 lines
man2/sigaction.2 +4 -0
@@ -955,6 +955,11 @@ It is not possible to block
  .IR sa_mask ).
  Attempts to do so are silently ignored.
  .PP
+In a
+.B SIGSEGV,
+if the fault is a memory access fault, si_addr is filled with the address
+causing the fault, otherwise it is not filled.

"... otherwise it is uninitialized." or "zeroed" or whatever...

And I'm having trouble figuring out why do you need to clarify this?

Quoted 7 lines

Because of this sentence:

* SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAP fill in si_addr with the address of the fault. On some architectures, these signals also fill in the si_trapno field.

?

I think the sentence above is vague, but probably for the reason that each arch is different. Maybe this patch is unnecessary and can be dropped?

Quoted 10 lines

If so, did you audit all architectures whether si_addr is populated only on memory access faults or is this something POSIX dictates or what's up? Because the sigaction(2) manpage is arch-agnostic and this is a rather strong assertion.

What am I missing?

Thx.

#5 lore raw

Re: [PATCH 2/2] sigaction.2: wfix - Clarify si_addr description.

Quoted 2 lines · Yu-cheng

I think the sentence above is vague, but probably for the reason that each arch is different. Maybe this patch is unnecessary and can be dropped?

Maybe.

If you want to clarify it, you should audit every arch. But what would that bring? IOW, is it that important to specify when si_addr is populated and when not...? I don't know of an example but I'm no userspace programmer anyway, to know when this info would be beneficial...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

A message that is not in the archive (sent off-list or not yet received): CACKs7VBLnQTc_RgnXk8X-XgVRvR5_uXY0wL0snW7P5iDjpb8fA@mail.gmail.com

#7 lore raw

Re: [PATCH 2/2] sigaction.2: wfix - Clarify si_addr description.

Quoted 14 lines · Stefan Puiu

Hi,

My 2 cents below.

On Tue, Mar 9, 2021, 16:33 Borislav Petkov <[email protected] <mailto:[email protected]>> wrote:

On Mon, Mar 08, 2021 at 01:46:07PM -0800, Yu, Yu-cheng wrote:
> I think the sentence above is vague, but probably for the reason
that each
> arch is different.  Maybe this patch is unnecessary and can be
dropped?

Maybe.

Quoted 8 lines

If you want to clarify it, you should audit every arch. But what would that bring? IOW, is it that important to specify when si_addr is populated and when not...? I don't know of an example but I'm no userspace programmer anyway, to know when this info would be beneficial...

I've worked on projects where the SIGSEGV sig handler would also print si_addr. When diagnosing a crash, the address that triggered the fault is useful to know. If you can't reproduce the crash in a debugger, or there's no core dump, at least you have an idea if it's a NULL pointer dereference or some naked pointer dereferencing. So I think it's useful to know when si_addr can be used to infer such information and when not.

At least for x86, the faulting ip is already in ucontext, and si_addr is mostly the memory address being accessed if that was the reason of the fault (i.e. the memory is not supposed to be accessed). That way, the signal handler has both the instruction pointer and the memory address.

For shadow stack violation, for example, it is not because the memory being accessed; it is the instruction itself causing the violation. It is unnecessary to duplicate the ip in si_addr. Setting si_addr to zero also indicates this is not a memory type fault.

--
Yu-cheng

#8 lore raw

Re: [PATCH 2/2] sigaction.2: wfix - Clarify si_addr description.

Quoted 31 lines · Yu-cheng
Quoted 22 lines · Stefan Puiu

Hi,

My 2 cents below.

On Tue, Mar 9, 2021, 16:33 Borislav Petkov <[email protected] <mailto:[email protected]>> wrote:

On Mon, Mar 08, 2021 at 01:46:07PM -0800, Yu, Yu-cheng wrote:
> I think the sentence above is vague, but probably for the reason
that each
> arch is different. Maybe this patch is unnecessary and can be
dropped?

Maybe.

If you want to clarify it, you should audit every arch. But what would that bring? IOW, is it that important to specify when si_addr is populated and when not...? I don't know of an example but I'm no userspace programmer anyway, to know when this info would be beneficial...

Quoted 6 lines

I've worked on projects where the SIGSEGV sig handler would also print si_addr. When diagnosing a crash, the address that triggered the fault is useful to know. If you can't reproduce the crash in a debugger, or there's no core dump, at least you have an idea if it's a NULL pointer dereference or some naked pointer dereferencing. So I think it's useful to know when si_addr can be used to infer such information and when not.

Quoted 4 lines

At least for x86, the faulting ip is already in ucontext, and si_addr is mostly the memory address being accessed if that was the reason of the fault (i.e. the memory is not supposed to be accessed). That way, the signal handler has both the instruction pointer and the memory address.

Interesting that you mention ucontext. I think the ability to fetch the IP from it is not that well documented. See for example the sigaction man page
(https://man7.org/linux/man-pages/man2/sigaction.2.html):

Further information about the ucontext_t structure can be found in getcontext(3) and signal(7). Commonly, the handler function doesn't make any use of the third argument.

Michael's book ("The Linux Programming Interface") has similar text on ucontext ("This information is rarely used in signal handlers, so we don’t go into further details"). I could find one example on google for fetching the IP at
https://www.oracle.com/technical-resources/articles/it-infrastructure/dev-signal-handlers-studio.html,
but it pertains to SPARC. Also I've found one older of our projects that uses this, and it seems each architecture has its own layout (the code handles ppc, mips and x86-64). Is this documented somewhere? Outside of the arch-specific kernel definition of the uc_mcontext member in the code, I mean :).

Thanks,
Stefan.

Quoted 8 lines

For shadow stack violation, for example, it is not because the memory being accessed; it is the instruction itself causing the violation. It is unnecessary to duplicate the ip in si_addr. Setting si_addr to zero also indicates this is not a memory type fault.

--
Yu-cheng