[PATCH v2 4/6] arm64/signal: Avoid rereading context frame sizes

From: Mark Brown
Date: Tue Jan 03 2023 - 15:26:17 EST


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 always
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.

Note that for SVE and ZA 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 <broonie@xxxxxxxxxx>
---
arch/arm64/kernel/signal.c | 32 +++++++++++++++++---------------
1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
index e9c6ffc1ebba..82a89b0852ee 100644
--- a/arch/arm64/kernel/signal.c
+++ b/arch/arm64/kernel/signal.c
@@ -170,8 +170,11 @@ 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 za_context __user *za;
+ u32 za_size;
};

static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
@@ -195,14 +198,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 */
@@ -271,12 +270,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;
@@ -292,7 +291,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;
@@ -301,7 +300,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;

/*
@@ -404,23 +403,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;

/*
@@ -517,6 +516,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:
@@ -531,6 +531,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 ZA_MAGIC:
@@ -541,6 +542,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 EXTRA_MAGIC:

--
2.30.2