2023-01-31 22:20:57

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 0/7] arm64/signal: Signal handling cleanups

This series collects a number of small cleanups to the signal handling
code which removes redundant validation of size information and avoids
reading the same data from userspace twice.

There are some overlaps with both the TPIDR2 signal handling and SME2
serieses which are also in flight, applying this will require
adjustments in those serieses and vice versa.

Changes in v3:
- Rebase onto arm64/for-next/core, updating for the addition of ZT and
TPIDR2 signal frames.
- Link to v2: https://lore.kernel.org/r/[email protected]
v2:
- Rebase onto v6.2-rc1

Signed-off-by: Mark Brown <[email protected]>
---
Mark Brown (7):
arm64/signal: Don't redundantly verify FPSIMD magic
arm64/signal: Remove redundant size validation from parse_user_sigframe()
arm64/signal: Make interface for restore_fpsimd_context() consistent
arm64/signal: Avoid rereading context frame sizes
arm64/signal: Only read new data when parsing the SVE context
arm64/signal: Only read new data when parsing the ZA context
arm64/signal: Only read new data when parsing the ZT context

arch/arm64/kernel/signal.c | 121 +++++++++++++++++++++++----------------------
1 file changed, 61 insertions(+), 60 deletions(-)
---
base-commit: 8154ffb7a51882c00730952ed21d80ed76f165d7
change-id: 20221212-arm64-signal-cleanup-bcd7272de5a9

Best regards,
--
Mark Brown <[email protected]>



2023-01-31 22:21:00

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 1/7] arm64/signal: Don't redundantly verify FPSIMD magic

We validate that the magic in the struct fpsimd_context is correct in
restore_fpsimd_context() but this is redundant since parse_user_sigframe()
uses this magic to decide to call the function in the first place. Remove
the extra validation.

Signed-off-by: Mark Brown <[email protected]>
---
arch/arm64/kernel/signal.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index ed692284f199..882f6d913508 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -191,15 +191,14 @@ static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
{
struct user_fpsimd_state fpsimd;
- __u32 magic, size;
+ __u32 size;
int err = 0;

- /* check the magic/size information */
- __get_user_error(magic, &ctx->head.magic, err);
+ /* check the size information */
__get_user_error(size, &ctx->head.size, err);
if (err)
return -EFAULT;
- if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
+ if (size != sizeof(struct fpsimd_context))
return -EINVAL;

/* copy the FP and status/control registers */

--
2.30.2


2023-01-31 22:21:01

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 2/7] arm64/signal: Remove redundant size validation from parse_user_sigframe()

There is some minimal size validation in parse_user_sigframe() however
all of the individual parsing functions perform frame specific validation
of the sizing information, remove the frame specific size checks in the
core so that there isn't any confusion about what we validate for size.

Since the checks in the SVE and ZA parsing are after we have read the
relevant context and since they won't report an error if the frame is
undersized they are adjusted to check for this before doing anything else.

Signed-off-by: Mark Brown <[email protected]>
---
arch/arm64/kernel/signal.c | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 882f6d913508..3228b5a1dfe3 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -278,6 +278,9 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
if (__copy_from_user(&sve, user->sve, sizeof(sve)))
return -EFAULT;

+ if (sve.head.size < sizeof(*user->sve))
+ return -EINVAL;
+
if (sve.flags & SVE_SIG_FLAG_SM) {
if (!system_supports_sme())
return -EINVAL;
@@ -293,7 +296,7 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
if (sve.vl != vl)
return -EINVAL;

- if (sve.head.size <= sizeof(*user->sve)) {
+ if (sve.head.size == sizeof(*user->sve)) {
clear_thread_flag(TIF_SVE);
current->thread.svcr &= ~SVCR_SM_MASK;
current->thread.fp_type = FP_STATE_FPSIMD;
@@ -434,10 +437,13 @@ static int restore_za_context(struct user_ctxs *user)
if (__copy_from_user(&za, user->za, sizeof(za)))
return -EFAULT;

+ if (za.head.size < sizeof(*user->za))
+ return -EINVAL;
+
if (za.vl != task_get_sme_vl(current))
return -EINVAL;

- if (za.head.size <= sizeof(*user->za)) {
+ if (za.head.size == sizeof(*user->za)) {
current->thread.svcr &= ~SVCR_ZA_MASK;
return 0;
}
@@ -614,9 +620,6 @@ static int parse_user_sigframe(struct user_ctxs *user,
if (user->fpsimd)
goto invalid;

- if (size < sizeof(*user->fpsimd))
- goto invalid;
-
user->fpsimd = (struct fpsimd_context __user *)head;
break;

@@ -631,9 +634,6 @@ static int parse_user_sigframe(struct user_ctxs *user,
if (user->sve)
goto invalid;

- if (size < sizeof(*user->sve))
- goto invalid;
-
user->sve = (struct sve_context __user *)head;
break;

@@ -657,9 +657,6 @@ static int parse_user_sigframe(struct user_ctxs *user,
if (user->za)
goto invalid;

- if (size < sizeof(*user->za))
- goto invalid;
-
user->za = (struct za_context __user *)head;
break;


--
2.30.2


2023-01-31 22:21:04

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 3/7] arm64/signal: Make interface for restore_fpsimd_context() consistent

Instead of taking a pointer to struct user_ctxs like the other two
restore_blah_context() functions the FPSIMD function takes a pointer to the
user struct it should read. Change it to be consistent with the rest, both
for consistency and to prepare for changes which avoid rereading data that
has already been read by the core parsing code.

There should be no functional change from this patch.

Signed-off-by: Mark Brown <[email protected]>
---
arch/arm64/kernel/signal.c | 28 ++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 3228b5a1dfe3..49321871783d 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -170,6 +170,14 @@ static void __user *apply_user_offset(
return base + offset;
}

+struct user_ctxs {
+ struct fpsimd_context __user *fpsimd;
+ struct sve_context __user *sve;
+ struct tpidr2_context __user *tpidr2;
+ struct za_context __user *za;
+ struct zt_context __user *zt;
+};
+
static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
{
struct user_fpsimd_state const *fpsimd =
@@ -188,24 +196,24 @@ static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
return err ? -EFAULT : 0;
}

-static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
+static int restore_fpsimd_context(struct user_ctxs *user)
{
struct user_fpsimd_state fpsimd;
__u32 size;
int err = 0;

/* check the size information */
- __get_user_error(size, &ctx->head.size, err);
+ __get_user_error(size, &user->fpsimd->head.size, err);
if (err)
return -EFAULT;
if (size != sizeof(struct fpsimd_context))
return -EINVAL;

/* copy the FP and status/control registers */
- err = __copy_from_user(fpsimd.vregs, ctx->vregs,
+ err = __copy_from_user(fpsimd.vregs, &(user->fpsimd->vregs),
sizeof(fpsimd.vregs));
- __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
- __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
+ __get_user_error(fpsimd.fpsr, &(user->fpsimd->fpsr), err);
+ __get_user_error(fpsimd.fpcr, &(user->fpsimd->fpcr), err);

clear_thread_flag(TIF_SVE);
current->thread.fp_type = FP_STATE_FPSIMD;
@@ -218,14 +226,6 @@ static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
}


-struct user_ctxs {
- struct fpsimd_context __user *fpsimd;
- struct sve_context __user *sve;
- struct tpidr2_context __user *tpidr2;
- struct za_context __user *za;
- struct zt_context __user *zt;
-};
-
#ifdef CONFIG_ARM64_SVE

static int preserve_sve_context(struct sve_context __user *ctx)
@@ -789,7 +789,7 @@ static int restore_sigframe(struct pt_regs *regs,
if (user.sve)
err = restore_sve_fpsimd_context(&user);
else
- err = restore_fpsimd_context(user.fpsimd);
+ err = restore_fpsimd_context(&user);
}

if (err == 0 && system_supports_sme() && user.tpidr2)

--
2.30.2


2023-01-31 22:21:09

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 4/7] arm64/signal: Avoid rereading context frame sizes

We need to read the sizes of the signal context frames as part of parsing
the overall signal context in parse_user_sigframe(). In the cases where we
defer frame specific parsing to other functions those functions (other
than the recently added TPIDR2 parser) reread the size and validate the
version they read, opening the possibility that the value may change.
Avoid this possibility by passing the size read in parse_user_sigframe()
through user_ctxs and referring to that.

For consistency we move the size check for the TPIDR2 context into the
TPIDR2 parsing function.

Note that for SVE, ZA and ZT contexts we still read the size again but
after this change we no longer use the value, further changes will avoid
the read.

Signed-off-by: Mark Brown <[email protected]>
---
arch/arm64/kernel/signal.c | 52 ++++++++++++++++++++++++----------------------
1 file changed, 27 insertions(+), 25 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 49321871783d..567e8e5b6998 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -172,10 +172,15 @@ static void __user *apply_user_offset(

struct user_ctxs {
struct fpsimd_context __user *fpsimd;
+ u32 fpsimd_size;
struct sve_context __user *sve;
+ u32 sve_size;
struct tpidr2_context __user *tpidr2;
+ u32 tpidr2_size;
struct za_context __user *za;
+ u32 za_size;
struct zt_context __user *zt;
+ u32 zt_size;
};

static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
@@ -199,14 +204,10 @@ static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
static int restore_fpsimd_context(struct user_ctxs *user)
{
struct user_fpsimd_state fpsimd;
- __u32 size;
int err = 0;

/* check the size information */
- __get_user_error(size, &user->fpsimd->head.size, err);
- if (err)
- return -EFAULT;
- if (size != sizeof(struct fpsimd_context))
+ if (user->fpsimd_size != sizeof(struct fpsimd_context))
return -EINVAL;

/* copy the FP and status/control registers */
@@ -275,12 +276,12 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
struct user_fpsimd_state fpsimd;
struct sve_context sve;

+ if (user->sve_size < sizeof(*user->sve))
+ return -EINVAL;
+
if (__copy_from_user(&sve, user->sve, sizeof(sve)))
return -EFAULT;

- if (sve.head.size < sizeof(*user->sve))
- return -EINVAL;
-
if (sve.flags & SVE_SIG_FLAG_SM) {
if (!system_supports_sme())
return -EINVAL;
@@ -296,7 +297,7 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
if (sve.vl != vl)
return -EINVAL;

- if (sve.head.size == sizeof(*user->sve)) {
+ if (user->sve_size == sizeof(*user->sve)) {
clear_thread_flag(TIF_SVE);
current->thread.svcr &= ~SVCR_SM_MASK;
current->thread.fp_type = FP_STATE_FPSIMD;
@@ -305,7 +306,7 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)

vq = sve_vq_from_vl(sve.vl);

- if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq))
+ if (user->sve_size < SVE_SIG_CONTEXT_SIZE(vq))
return -EINVAL;

/*
@@ -385,7 +386,9 @@ static int restore_tpidr2_context(struct user_ctxs *user)
u64 tpidr2_el0;
int err = 0;

- /* Magic and size were validated deciding to call this function */
+ if (user->tpidr2_size != sizeof(*user->tpidr2))
+ return -EINVAL;
+
__get_user_error(tpidr2_el0, &user->tpidr2->tpidr2, err);
if (!err)
current->thread.tpidr2_el0 = tpidr2_el0;
@@ -434,23 +437,23 @@ static int restore_za_context(struct user_ctxs *user)
unsigned int vq;
struct za_context za;

+ if (user->za_size < sizeof(*user->za))
+ return -EINVAL;
+
if (__copy_from_user(&za, user->za, sizeof(za)))
return -EFAULT;

- if (za.head.size < sizeof(*user->za))
- return -EINVAL;
-
if (za.vl != task_get_sme_vl(current))
return -EINVAL;

- if (za.head.size == sizeof(*user->za)) {
+ if (user->za_size == sizeof(*user->za)) {
current->thread.svcr &= ~SVCR_ZA_MASK;
return 0;
}

vq = sve_vq_from_vl(za.vl);

- if (za.head.size < ZA_SIG_CONTEXT_SIZE(vq))
+ if (user->za_size < ZA_SIG_CONTEXT_SIZE(vq))
return -EINVAL;

/*
@@ -521,15 +524,15 @@ static int restore_zt_context(struct user_ctxs *user)
if (!thread_za_enabled(&current->thread))
return -EINVAL;

+ if (user->zt_size != ZT_SIG_CONTEXT_SIZE(1))
+ return -EINVAL;
+
if (__copy_from_user(&zt, user->zt, sizeof(zt)))
return -EFAULT;

if (zt.nregs != 1)
return -EINVAL;

- if (zt.head.size != ZT_SIG_CONTEXT_SIZE(zt.nregs))
- return -EINVAL;
-
/*
* Careful: we are about __copy_from_user() directly into
* thread.zt_state with preemption enabled, so protection is
@@ -621,6 +624,7 @@ static int parse_user_sigframe(struct user_ctxs *user,
goto invalid;

user->fpsimd = (struct fpsimd_context __user *)head;
+ user->fpsimd_size = size;
break;

case ESR_MAGIC:
@@ -635,6 +639,7 @@ static int parse_user_sigframe(struct user_ctxs *user,
goto invalid;

user->sve = (struct sve_context __user *)head;
+ user->sve_size = size;
break;

case TPIDR2_MAGIC:
@@ -644,10 +649,8 @@ static int parse_user_sigframe(struct user_ctxs *user,
if (user->tpidr2)
goto invalid;

- if (size != sizeof(*user->tpidr2))
- goto invalid;
-
user->tpidr2 = (struct tpidr2_context __user *)head;
+ user->tpidr2_size = size;
break;

case ZA_MAGIC:
@@ -658,6 +661,7 @@ static int parse_user_sigframe(struct user_ctxs *user,
goto invalid;

user->za = (struct za_context __user *)head;
+ user->za_size = size;
break;

case ZT_MAGIC:
@@ -667,10 +671,8 @@ static int parse_user_sigframe(struct user_ctxs *user,
if (user->zt)
goto invalid;

- if (size < sizeof(*user->zt))
- goto invalid;
-
user->zt = (struct zt_context __user *)head;
+ user->zt_size = size;
break;

case EXTRA_MAGIC:

--
2.30.2


2023-01-31 22:21:19

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 5/7] arm64/signal: Only read new data when parsing the SVE context

When we parse the SVE signal context we read the entire context from
userspace, including the generic signal context header which was already
read by parse_user_sigframe() and padding bytes that we ignore. Avoid the
possibility of relying on the second read of the data read twice by only
reading the data which we are actually going to use.

Signed-off-by: Mark Brown <[email protected]>
---
arch/arm64/kernel/signal.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 567e8e5b6998..27a1fa37f926 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -271,18 +271,20 @@ static int preserve_sve_context(struct sve_context __user *ctx)

static int restore_sve_fpsimd_context(struct user_ctxs *user)
{
- int err;
+ int err = 0;
unsigned int vl, vq;
struct user_fpsimd_state fpsimd;
- struct sve_context sve;
+ u16 user_vl, flags;

if (user->sve_size < sizeof(*user->sve))
return -EINVAL;

- if (__copy_from_user(&sve, user->sve, sizeof(sve)))
- return -EFAULT;
+ __get_user_error(user_vl, &(user->sve->vl), err);
+ __get_user_error(flags, &(user->sve->flags), err);
+ if (err)
+ return err;

- if (sve.flags & SVE_SIG_FLAG_SM) {
+ if (flags & SVE_SIG_FLAG_SM) {
if (!system_supports_sme())
return -EINVAL;

@@ -294,7 +296,7 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
vl = task_get_sve_vl(current);
}

- if (sve.vl != vl)
+ if (user_vl != vl)
return -EINVAL;

if (user->sve_size == sizeof(*user->sve)) {
@@ -304,7 +306,7 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
goto fpsimd_only;
}

- vq = sve_vq_from_vl(sve.vl);
+ vq = sve_vq_from_vl(vl);

if (user->sve_size < SVE_SIG_CONTEXT_SIZE(vq))
return -EINVAL;
@@ -332,7 +334,7 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
if (err)
return -EFAULT;

- if (sve.flags & SVE_SIG_FLAG_SM)
+ if (flags & SVE_SIG_FLAG_SM)
current->thread.svcr |= SVCR_SM_MASK;
else
set_thread_flag(TIF_SVE);

--
2.30.2


2023-01-31 22:21:23

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 7/7] arm64/signal: Only read new data when parsing the ZT context

When we parse the ZT signal context we read the entire context from
userspace, including the generic signal context header which was already
read by parse_user_sigframe() and padding bytes that we ignore. Avoid the
possibility of relying on the second read of the data read twice by only
reading the data which we are actually going to use.

Signed-off-by: Mark Brown <[email protected]>
---
arch/arm64/kernel/signal.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 7810d090c025..d7b5ed8a9b7f 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -521,7 +521,7 @@ static int preserve_zt_context(struct zt_context __user *ctx)
static int restore_zt_context(struct user_ctxs *user)
{
int err;
- struct zt_context zt;
+ u16 nregs;

/* ZA must be restored first for this check to be valid */
if (!thread_za_enabled(&current->thread))
@@ -530,10 +530,10 @@ static int restore_zt_context(struct user_ctxs *user)
if (user->zt_size != ZT_SIG_CONTEXT_SIZE(1))
return -EINVAL;

- if (__copy_from_user(&zt, user->zt, sizeof(zt)))
+ if (__copy_from_user(&nregs, &(user->zt->nregs), sizeof(nregs)))
return -EFAULT;

- if (zt.nregs != 1)
+ if (nregs != 1)
return -EINVAL;

/*

--
2.30.2


2023-01-31 22:21:25

by Mark Brown

[permalink] [raw]
Subject: [PATCH v3 6/7] arm64/signal: Only read new data when parsing the ZA context

When we parse the ZA signal context we read the entire context from
userspace, including the generic signal context header which was already
read by parse_user_sigframe() and padding bytes that we ignore. Avoid the
possibility of relying on the second read of the data read twice by only
reading the data which we are actually going to use.

Signed-off-by: Mark Brown <[email protected]>
---
arch/arm64/kernel/signal.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index 27a1fa37f926..7810d090c025 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -435,17 +435,18 @@ static int preserve_za_context(struct za_context __user *ctx)

static int restore_za_context(struct user_ctxs *user)
{
- int err;
+ int err = 0;
unsigned int vq;
- struct za_context za;
+ u16 user_vl;

if (user->za_size < sizeof(*user->za))
return -EINVAL;

- if (__copy_from_user(&za, user->za, sizeof(za)))
- return -EFAULT;
+ __get_user_error(user_vl, &(user->za->vl), err);
+ if (err)
+ return err;

- if (za.vl != task_get_sme_vl(current))
+ if (user_vl != task_get_sme_vl(current))
return -EINVAL;

if (user->za_size == sizeof(*user->za)) {
@@ -453,7 +454,7 @@ static int restore_za_context(struct user_ctxs *user)
return 0;
}

- vq = sve_vq_from_vl(za.vl);
+ vq = sve_vq_from_vl(user_vl);

if (user->za_size < ZA_SIG_CONTEXT_SIZE(vq))
return -EINVAL;

--
2.30.2


2023-02-01 19:31:29

by Catalin Marinas

[permalink] [raw]
Subject: Re: [PATCH v3 0/7] arm64/signal: Signal handling cleanups

On Tue, 31 Jan 2023 22:20:38 +0000, Mark Brown wrote:
> This series collects a number of small cleanups to the signal handling
> code which removes redundant validation of size information and avoids
> reading the same data from userspace twice.
>
> There are some overlaps with both the TPIDR2 signal handling and SME2
> serieses which are also in flight, applying this will require
> adjustments in those serieses and vice versa.
>
> [...]

Applied to arm64 (for-next/signal), thanks!

[1/7] arm64/signal: Don't redundantly verify FPSIMD magic
https://git.kernel.org/arm64/c/92f14518cc43
[2/7] arm64/signal: Remove redundant size validation from parse_user_sigframe()
https://git.kernel.org/arm64/c/0eb23720f29e
[3/7] arm64/signal: Make interface for restore_fpsimd_context() consistent
https://git.kernel.org/arm64/c/4e4e93045fe1
[4/7] arm64/signal: Avoid rereading context frame sizes
https://git.kernel.org/arm64/c/b57682b31558
[5/7] arm64/signal: Only read new data when parsing the SVE context
https://git.kernel.org/arm64/c/f3ac48aa3a58
[6/7] arm64/signal: Only read new data when parsing the ZA context
https://git.kernel.org/arm64/c/24d68345a02a
[7/7] arm64/signal: Only read new data when parsing the ZT context
https://git.kernel.org/arm64/c/ad678be42387

--
Catalin