[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <YIpkvGrBFGlB5vNj@elver.google.com>
Date: Thu, 29 Apr 2021 09:48:12 +0200
From: Marco Elver <elver@...gle.com>
To: "Eric W. Biederman" <ebiederm@...ssion.com>
Cc: Florian Weimer <fweimer@...hat.com>,
"David S. Miller" <davem@...emloft.net>,
Arnd Bergmann <arnd@...db.de>,
Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...nel.org>,
Thomas Gleixner <tglx@...utronix.de>,
Peter Collingbourne <pcc@...gle.com>,
Dmitry Vyukov <dvyukov@...gle.com>,
Alexander Potapenko <glider@...gle.com>,
sparclinux@...r.kernel.org, linux-arch@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-api@...r.kernel.org,
kasan-dev@...glegroups.com
Subject: siginfo_t ABI break on sparc64 from si_addr_lsb move 3y ago
Hello, Eric,
By inspecting the logs I've seen that about 3 years ago there had been a
number of siginfo_t cleanups. This included moving si_addr_lsb:
b68a68d3dcc1 ("signal: Move addr_lsb into the _sigfault union for clarity")
859d880cf544 ("signal: Correct the offset of si_pkey in struct siginfo")
8420f71943ae ("signal: Correct the offset of si_pkey and si_lower in struct siginfo on m68k")
In an ideal world, we could just have si_addr + the union in _sigfault,
but it seems there are more corner cases. :-/
The reason I've stumbled upon this is that I wanted to add the just
merged si_perf [1] field to glibc. But what I noticed is that glibc's
definition and ours are vastly different around si_addr_lsb, si_lower,
si_upper, and si_pkey.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42dec9a936e7696bea1f27d3c5a0068cd9aa95fd
In our current definition of siginfo_t, si_addr_lsb is placed into the
same union as si_lower, si_upper, and si_pkey (and now si_perf). From
the logs I see that si_lower, si_upper, and si_pkey are padded because
si_addr_lsb used to be outside the union, which goes back to
"signal: Move addr_lsb into the _sigfault union for clarity".
Since then, si_addr_lsb must also be pointer-aligned, because the union
containing it must be pointer-aligned (because si_upper, si_lower). On
all architectures where si_addr_lsb is right after si_addr, this is
perfectly fine, because si_addr itself is a pointer...
... except for the anomaly that are 64-bit architectures that define
__ARCH_SI_TRAPNO and want that 'int si_trapno'. Like, for example
sparc64, which means siginfo_t's ABI has been subtly broken on sparc64
since v4.16.
The following static asserts illustrate this:
--- a/arch/sparc/kernel/signal_64.c
+++ b/arch/sparc/kernel/signal_64.c
@@ -556,3 +556,37 @@ void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long
user_enter();
}
+static_assert(offsetof(siginfo_t, si_signo) == 0);
+static_assert(offsetof(siginfo_t, si_errno) == 4);
+static_assert(offsetof(siginfo_t, si_code) == 8);
+static_assert(offsetof(siginfo_t, si_pid) == 16);
+static_assert(offsetof(siginfo_t, si_uid) == 20);
+static_assert(offsetof(siginfo_t, si_tid) == 16);
+static_assert(offsetof(siginfo_t, si_overrun) == 20);
+static_assert(offsetof(siginfo_t, si_status) == 24);
+static_assert(offsetof(siginfo_t, si_utime) == 32);
+static_assert(offsetof(siginfo_t, si_stime) == 40);
+static_assert(offsetof(siginfo_t, si_value) == 24);
+static_assert(offsetof(siginfo_t, si_int) == 24);
+static_assert(offsetof(siginfo_t, si_ptr) == 24);
+static_assert(offsetof(siginfo_t, si_addr) == 16);
+static_assert(offsetof(siginfo_t, si_trapno) == 24);
+#if 1 /* Correct offsets, obtained from v4.14 */
+static_assert(offsetof(siginfo_t, si_addr_lsb) == 28);
+static_assert(offsetof(siginfo_t, si_lower) == 32);
+static_assert(offsetof(siginfo_t, si_upper) == 40);
+static_assert(offsetof(siginfo_t, si_pkey) == 32);
+#else /* Current offsets, as of v4.16 */
+static_assert(offsetof(siginfo_t, si_addr_lsb) == 32);
+static_assert(offsetof(siginfo_t, si_lower) == 40);
+static_assert(offsetof(siginfo_t, si_upper) == 48);
+static_assert(offsetof(siginfo_t, si_pkey) == 40);
+#endif
+static_assert(offsetof(siginfo_t, si_band) == 16);
+static_assert(offsetof(siginfo_t, si_fd) == 20);
---
Granted, nobody seems to have noticed because I don't even know if these
fields have use on sparc64. But I don't yet see this as justification to
leave things as-is...
The collateral damage of this, and the acute problem that I'm having is
defining si_perf in a sort-of readable and portable way in siginfo_t
definitions that live outside the kernel, where sparc64 does not yet
have broken si_addr_lsb. And the same difficulty applies to the kernel
if we want to unbreak sparc64, while not wanting to move si_perf for
other architectures.
There are 2 options I see to solve this:
1. Make things simple again. We could just revert the change moving
si_addr_lsb into the union, and sadly accept we'll have to live with
that legacy "design" mistake. (si_perf stays in the union, but will
unfortunately change its offset for all architectures... this one-off
move might be ok because it's new.)
2. Add special cases to retain si_addr_lsb in the union on architectures
that do not have __ARCH_SI_TRAPNO (the majority). I have added a
draft patch that would do this below (with some refactoring so that
it remains sort-of readable), as an experiment to see how complicated
this gets.
Option (1) means we'll forever be wasting the space where si_addr_lsb
lives (including the padding). It'd also mean we'd move si_perf for
_all_ architectures -- this might be acceptable, given there is no
stable release with it yet -- the fix just needs to be merged before the
release of v5.13! It is the simpler option though -- and I don't know if
we need all this complexity.
Option (2) perhaps results in better space utilization. Maybe that's
better long-term if we worry about space in some rather distant future
-- where we need those 8 bytes on 64-bit architectures to not exceed 128
bytes. This option, however, doesn't just make us carry this complexity
forever, but also forces it onto everyone else, like glibc and other
libcs (including those in other languages with C FFIs) which have their
own definition of siginfo_t.
Which option do you prefer? Are there better options?
Many thanks,
-- Marco
------ >8 ------
Option #2 draft:
diff --git a/arch/sparc/kernel/signal_64.c b/arch/sparc/kernel/signal_64.c
index a0eec62c825d..150ee27b1423 100644
--- a/arch/sparc/kernel/signal_64.c
+++ b/arch/sparc/kernel/signal_64.c
@@ -556,3 +556,37 @@ void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long
user_enter();
}
+/*
+ * Compile-time assertions for siginfo_t offsets. Unlike other architectures,
+ * sparc64 is special, because it requires si_trapno (int), and the following
+ * si_addr_lsb (short) need not be word aligned. Accidental changes around the
+ * offset of si_addr_lsb and the following fields would only be caught here.
+ */
+static_assert(offsetof(siginfo_t, si_signo) == 0);
+static_assert(offsetof(siginfo_t, si_errno) == 4);
+static_assert(offsetof(siginfo_t, si_code) == 8);
+static_assert(offsetof(siginfo_t, si_pid) == 16);
+static_assert(offsetof(siginfo_t, si_uid) == 20);
+static_assert(offsetof(siginfo_t, si_tid) == 16);
+static_assert(offsetof(siginfo_t, si_overrun) == 20);
+static_assert(offsetof(siginfo_t, si_status) == 24);
+static_assert(offsetof(siginfo_t, si_utime) == 32);
+static_assert(offsetof(siginfo_t, si_stime) == 40);
+static_assert(offsetof(siginfo_t, si_value) == 24);
+static_assert(offsetof(siginfo_t, si_int) == 24);
+static_assert(offsetof(siginfo_t, si_ptr) == 24);
+static_assert(offsetof(siginfo_t, si_addr) == 16);
+static_assert(offsetof(siginfo_t, si_trapno) == 24);
+#if 1 /* Correct offsets, obtained from v4.14 */
+static_assert(offsetof(siginfo_t, si_addr_lsb) == 28);
+static_assert(offsetof(siginfo_t, si_lower) == 32);
+static_assert(offsetof(siginfo_t, si_upper) == 40);
+static_assert(offsetof(siginfo_t, si_pkey) == 32);
+#else /* Current offsets, as of v4.16 */
+static_assert(offsetof(siginfo_t, si_addr_lsb) == 32);
+static_assert(offsetof(siginfo_t, si_lower) == 40);
+static_assert(offsetof(siginfo_t, si_upper) == 48);
+static_assert(offsetof(siginfo_t, si_pkey) == 40);
+#endif
+static_assert(offsetof(siginfo_t, si_band) == 16);
+static_assert(offsetof(siginfo_t, si_fd) == 20);
diff --git a/include/linux/compat.h b/include/linux/compat.h
index f0d2dd35d408..5ea9f9c748dd 100644
--- a/include/linux/compat.h
+++ b/include/linux/compat.h
@@ -158,6 +158,31 @@ typedef union compat_sigval {
compat_uptr_t sival_ptr;
} compat_sigval_t;
+struct __compat_sigfault_addin {
+#ifdef __ARCH_SI_TRAPNO
+ int _trapno; /* TRAP # which caused the signal */
+#endif
+ /*
+ * used when si_code=BUS_MCEERR_AR or
+ * used when si_code=BUS_MCEERR_AO
+ */
+ short int _addr_lsb; /* Valid LSB of the reported address. */
+
+/* See include/asm-generic/uapi/siginfo.h */
+#ifdef __ARCH_SI_TRAPNO
+# define __COMPAT_SIGFAULT_ADDIN_FIXED struct __compat_sigfault_addin _addin;
+# define __COMPAT_SIGFAULT_ADDIN_UNION
+# define __COMPAT_SIGFAULT_LEGACY_UNION_PAD
+#else
+# define __COMPAT_SIGFAULT_ADDIN_FIXED
+# define __COMPAT_SIGFAULT_ADDIN_UNION struct __compat_sigfault_addin _addin;
+# define __COMPAT_SIGFAULT_LEGACY_UNION_PAD \
+ char _unused[__alignof__(compat_uptr_t) < sizeof(short) ? \
+ sizeof(short) : \
+ __alignof__(compat_uptr_t)];
+#endif
+};
+
typedef struct compat_siginfo {
int si_signo;
#ifndef __ARCH_HAS_SWAPPED_SIGINFO
@@ -214,26 +239,18 @@ typedef struct compat_siginfo {
/* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGTRAP, SIGEMT */
struct {
compat_uptr_t _addr; /* faulting insn/memory ref. */
-#ifdef __ARCH_SI_TRAPNO
- int _trapno; /* TRAP # which caused the signal */
-#endif
-#define __COMPAT_ADDR_BND_PKEY_PAD (__alignof__(compat_uptr_t) < sizeof(short) ? \
- sizeof(short) : __alignof__(compat_uptr_t))
+ __COMPAT_SIGFAULT_ADDIN_FIXED
union {
- /*
- * used when si_code=BUS_MCEERR_AR or
- * used when si_code=BUS_MCEERR_AO
- */
- short int _addr_lsb; /* Valid LSB of the reported address. */
+ __COMPAT_SIGFAULT_ADDIN_UNION
/* used when si_code=SEGV_BNDERR */
struct {
- char _dummy_bnd[__COMPAT_ADDR_BND_PKEY_PAD];
+ __COMPAT_SIGFAULT_LEGACY_UNION_PAD
compat_uptr_t _lower;
compat_uptr_t _upper;
} _addr_bnd;
/* used when si_code=SEGV_PKUERR */
struct {
- char _dummy_pkey[__COMPAT_ADDR_BND_PKEY_PAD];
+ __COMPAT_SIGFAULT_LEGACY_UNION_PAD
u32 _pkey;
} _addr_pkey;
/* used when si_code=TRAP_PERF */
diff --git a/include/uapi/asm-generic/siginfo.h b/include/uapi/asm-generic/siginfo.h
index 03d6f6d2c1fe..f1c1a0300ac8 100644
--- a/include/uapi/asm-generic/siginfo.h
+++ b/include/uapi/asm-generic/siginfo.h
@@ -29,6 +29,45 @@ typedef union sigval {
#define __ARCH_SI_ATTRIBUTES
#endif
+/*
+ * The _sigfault portion of __sifields after si_addr varies depending on
+ * architecture; capture these rules here.
+ */
+struct __sifields_sigfault_addin {
+#ifdef __ARCH_SI_TRAPNO
+ int _trapno; /* TRAP # which caused the signal */
+#endif
+ /*
+ * used when si_code=BUS_MCEERR_AR or
+ * used when si_code=BUS_MCEERR_AO
+ */
+ short _addr_lsb; /* LSB of the reported address */
+
+#if defined(__ARCH_SI_TRAPNO)
+/*
+ * If we have si_trapno between si_addr and si_addr_lsb, we cannot safely move
+ * it inside the union due to alignment of si_trapno+si_addr_lsb vs. the union.
+ */
+# define __SI_SIGFAULT_ADDIN_FIXED struct __sifields_sigfault_addin _addin;
+# define __SI_SIGFAULT_ADDIN_UNION
+# define __SI_SIGFAULT_LEGACY_UNION_PAD
+#else
+/*
+ * Safe to move si_addr_lsb inside the union. We will benefit from better space
+ * usage for new fields added to the union.
+ *
+ * Fields that were added after si_addr_lsb, before it become part of the union,
+ * require padding to retain the ABI. New fields do not require padding.
+ */
+# define __SI_SIGFAULT_ADDIN_FIXED
+# define __SI_SIGFAULT_ADDIN_UNION struct __sifields_sigfault_addin _addin;
+# define __SI_SIGFAULT_LEGACY_UNION_PAD \
+ char _unused[__alignof__(void *) < sizeof(short) ? \
+ sizeof(short) : \
+ __alignof__(void *)];
+#endif
+};
+
union __sifields {
/* kill() */
struct {
@@ -63,32 +102,23 @@ union __sifields {
/* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGTRAP, SIGEMT */
struct {
void __user *_addr; /* faulting insn/memory ref. */
-#ifdef __ARCH_SI_TRAPNO
- int _trapno; /* TRAP # which caused the signal */
-#endif
#ifdef __ia64__
int _imm; /* immediate value for "break" */
unsigned int _flags; /* see ia64 si_flags */
unsigned long _isr; /* isr */
#endif
-
-#define __ADDR_BND_PKEY_PAD (__alignof__(void *) < sizeof(short) ? \
- sizeof(short) : __alignof__(void *))
+ __SI_SIGFAULT_ADDIN_FIXED
union {
- /*
- * used when si_code=BUS_MCEERR_AR or
- * used when si_code=BUS_MCEERR_AO
- */
- short _addr_lsb; /* LSB of the reported address */
+ __SI_SIGFAULT_ADDIN_UNION
/* used when si_code=SEGV_BNDERR */
struct {
- char _dummy_bnd[__ADDR_BND_PKEY_PAD];
+ __SI_SIGFAULT_LEGACY_UNION_PAD
void __user *_lower;
void __user *_upper;
} _addr_bnd;
/* used when si_code=SEGV_PKUERR */
struct {
- char _dummy_pkey[__ADDR_BND_PKEY_PAD];
+ __SI_SIGFAULT_LEGACY_UNION_PAD
__u32 _pkey;
} _addr_pkey;
/* used when si_code=TRAP_PERF */
@@ -151,9 +181,9 @@ typedef struct siginfo {
#define si_ptr _sifields._rt._sigval.sival_ptr
#define si_addr _sifields._sigfault._addr
#ifdef __ARCH_SI_TRAPNO
-#define si_trapno _sifields._sigfault._trapno
+#define si_trapno _sifields._sigfault._addin._trapno
#endif
-#define si_addr_lsb _sifields._sigfault._addr_lsb
+#define si_addr_lsb _sifields._sigfault._addin._addr_lsb
#define si_lower _sifields._sigfault._addr_bnd._lower
#define si_upper _sifields._sigfault._addr_bnd._upper
#define si_pkey _sifields._sigfault._addr_pkey._pkey
Powered by blists - more mailing lists