public inbox for op-tee@lists.trustedfirmware.org
 help / color / mirror / Atom feed
* [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels
@ 2025-04-28 13:06 Jann Horn
  2025-04-30 11:52 ` Jens Wiklander
  2025-05-01 20:01 ` David Laight
  0 siblings, 2 replies; 7+ messages in thread
From: Jann Horn @ 2025-04-28 13:06 UTC (permalink / raw)
  To: op-tee

[-- Attachment #1: Type: text/plain, Size: 2912 bytes --]

The current code around TEE_IOCTL_PARAM_SIZE() is a bit wrong on
32-bit kernels: Multiplying a user-provided 32-bit value with the
size of a structure can wrap around on such platforms.

Fix it by using saturating arithmetic for the size calculation.

This has no security consequences because, in all users of
TEE_IOCTL_PARAM_SIZE(), the subsequent kcalloc() implicitly checks
for wrapping.

Signed-off-by: Jann Horn <jannh@google.com>
---
Note that I don't have a test device with a TEE; I only compile-tested
the change on an x86-64 build.
---
 drivers/tee/tee_core.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
index d113679b1e2d..acc7998758ad 100644
--- a/drivers/tee/tee_core.c
+++ b/drivers/tee/tee_core.c
@@ -10,6 +10,7 @@
 #include <linux/fs.h>
 #include <linux/idr.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/tee_core.h>
 #include <linux/uaccess.h>
@@ -19,7 +20,7 @@
 
 #define TEE_NUM_DEVICES	32
 
-#define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
+#define TEE_IOCTL_PARAM_SIZE(x) (size_mul(sizeof(struct tee_param), (x)))
 
 #define TEE_UUID_NS_NAME_SIZE	128
 
@@ -487,7 +488,7 @@ static int tee_ioctl_open_session(struct tee_context *ctx,
 	if (copy_from_user(&arg, uarg, sizeof(arg)))
 		return -EFAULT;
 
-	if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
+	if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
 		return -EINVAL;
 
 	if (arg.num_params) {
@@ -565,7 +566,7 @@ static int tee_ioctl_invoke(struct tee_context *ctx,
 	if (copy_from_user(&arg, uarg, sizeof(arg)))
 		return -EFAULT;
 
-	if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
+	if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
 		return -EINVAL;
 
 	if (arg.num_params) {
@@ -699,7 +700,7 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
 	if (get_user(num_params, &uarg->num_params))
 		return -EFAULT;
 
-	if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
+	if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) != buf.buf_len)
 		return -EINVAL;
 
 	params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
@@ -798,7 +799,7 @@ static int tee_ioctl_supp_send(struct tee_context *ctx,
 	    get_user(num_params, &uarg->num_params))
 		return -EFAULT;
 
-	if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
+	if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) > buf.buf_len)
 		return -EINVAL;
 
 	params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);

---
base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e
change-id: 20250428-tee-sizecheck-299d5eff8fc7

-- 
Jann Horn <jannh@google.com>


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels
  2025-04-28 13:06 Jann Horn
@ 2025-04-30 11:52 ` Jens Wiklander
  2025-05-01 20:01 ` David Laight
  1 sibling, 0 replies; 7+ messages in thread
From: Jens Wiklander @ 2025-04-30 11:52 UTC (permalink / raw)
  To: op-tee

[-- Attachment #1: Type: text/plain, Size: 3383 bytes --]

On Mon, Apr 28, 2025 at 3:06 PM Jann Horn <jannh@google.com> wrote:
>
> The current code around TEE_IOCTL_PARAM_SIZE() is a bit wrong on
> 32-bit kernels: Multiplying a user-provided 32-bit value with the
> size of a structure can wrap around on such platforms.
>
> Fix it by using saturating arithmetic for the size calculation.
>
> This has no security consequences because, in all users of
> TEE_IOCTL_PARAM_SIZE(), the subsequent kcalloc() implicitly checks
> for wrapping.
>
> Signed-off-by: Jann Horn <jannh@google.com>
> ---
> Note that I don't have a test device with a TEE; I only compile-tested
> the change on an x86-64 build.
> ---
>  drivers/tee/tee_core.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)

Looks good, I'm picking up this.

Thanks,
Jens

>
> diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
> index d113679b1e2d..acc7998758ad 100644
> --- a/drivers/tee/tee_core.c
> +++ b/drivers/tee/tee_core.c
> @@ -10,6 +10,7 @@
>  #include <linux/fs.h>
>  #include <linux/idr.h>
>  #include <linux/module.h>
> +#include <linux/overflow.h>
>  #include <linux/slab.h>
>  #include <linux/tee_core.h>
>  #include <linux/uaccess.h>
> @@ -19,7 +20,7 @@
>
>  #define TEE_NUM_DEVICES        32
>
> -#define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
> +#define TEE_IOCTL_PARAM_SIZE(x) (size_mul(sizeof(struct tee_param), (x)))
>
>  #define TEE_UUID_NS_NAME_SIZE  128
>
> @@ -487,7 +488,7 @@ static int tee_ioctl_open_session(struct tee_context *ctx,
>         if (copy_from_user(&arg, uarg, sizeof(arg)))
>                 return -EFAULT;
>
> -       if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> +       if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
>                 return -EINVAL;
>
>         if (arg.num_params) {
> @@ -565,7 +566,7 @@ static int tee_ioctl_invoke(struct tee_context *ctx,
>         if (copy_from_user(&arg, uarg, sizeof(arg)))
>                 return -EFAULT;
>
> -       if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> +       if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
>                 return -EINVAL;
>
>         if (arg.num_params) {
> @@ -699,7 +700,7 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
>         if (get_user(num_params, &uarg->num_params))
>                 return -EFAULT;
>
> -       if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
> +       if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) != buf.buf_len)
>                 return -EINVAL;
>
>         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
> @@ -798,7 +799,7 @@ static int tee_ioctl_supp_send(struct tee_context *ctx,
>             get_user(num_params, &uarg->num_params))
>                 return -EFAULT;
>
> -       if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
> +       if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) > buf.buf_len)
>                 return -EINVAL;
>
>         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
>
> ---
> base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e
> change-id: 20250428-tee-sizecheck-299d5eff8fc7
>
> --
> Jann Horn <jannh@google.com>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels
       [not found] < <CAHUa44E_JZdYnGrReP0zWCP1wdu2BdJ9DSZZ3a2OiobRj61ThQ@mail.gmail.com>
@ 2025-04-30 12:12 ` Rouven Czerwinski
  0 siblings, 0 replies; 7+ messages in thread
From: Rouven Czerwinski @ 2025-04-30 12:12 UTC (permalink / raw)
  To: op-tee

[-- Attachment #1: Type: text/plain, Size: 3830 bytes --]

Hi,

On Wed, 30 Apr 2025 at 13:53, Jens Wiklander <jens.wiklander@linaro.org> wrote:
>
> On Mon, Apr 28, 2025 at 3:06 PM Jann Horn <jannh@google.com> wrote:
> >
> > The current code around TEE_IOCTL_PARAM_SIZE() is a bit wrong on
> > 32-bit kernels: Multiplying a user-provided 32-bit value with the
> > size of a structure can wrap around on such platforms.
> >
> > Fix it by using saturating arithmetic for the size calculation.
> >
> > This has no security consequences because, in all users of
> > TEE_IOCTL_PARAM_SIZE(), the subsequent kcalloc() implicitly checks
> > for wrapping.
> >
> > Signed-off-by: Jann Horn <jannh@google.com>
> > ---
> > Note that I don't have a test device with a TEE; I only compile-tested
> > the change on an x86-64 build.
> > ---
> >  drivers/tee/tee_core.c | 11 ++++++-----
> >  1 file changed, 6 insertions(+), 5 deletions(-)
>
> Looks good, I'm picking up this.
>
> Thanks,
> Jens
>
> >
> > diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
> > index d113679b1e2d..acc7998758ad 100644
> > --- a/drivers/tee/tee_core.c
> > +++ b/drivers/tee/tee_core.c
> > @@ -10,6 +10,7 @@
> >  #include <linux/fs.h>
> >  #include <linux/idr.h>
> >  #include <linux/module.h>
> > +#include <linux/overflow.h>
> >  #include <linux/slab.h>
> >  #include <linux/tee_core.h>
> >  #include <linux/uaccess.h>
> > @@ -19,7 +20,7 @@
> >
> >  #define TEE_NUM_DEVICES        32
> >
> > -#define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
> > +#define TEE_IOCTL_PARAM_SIZE(x) (size_mul(sizeof(struct tee_param), (x)))
> >
> >  #define TEE_UUID_NS_NAME_SIZE  128
> >
> > @@ -487,7 +488,7 @@ static int tee_ioctl_open_session(struct tee_context *ctx,
> >         if (copy_from_user(&arg, uarg, sizeof(arg)))
> >                 return -EFAULT;
> >
> > -       if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> > +       if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
> >                 return -EINVAL;
> >
> >         if (arg.num_params) {
> > @@ -565,7 +566,7 @@ static int tee_ioctl_invoke(struct tee_context *ctx,
> >         if (copy_from_user(&arg, uarg, sizeof(arg)))
> >                 return -EFAULT;
> >
> > -       if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> > +       if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
> >                 return -EINVAL;
> >
> >         if (arg.num_params) {
> > @@ -699,7 +700,7 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
> >         if (get_user(num_params, &uarg->num_params))
> >                 return -EFAULT;
> >
> > -       if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
> > +       if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) != buf.buf_len)
> >                 return -EINVAL;
> >
> >         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
> > @@ -798,7 +799,7 @@ static int tee_ioctl_supp_send(struct tee_context *ctx,
> >             get_user(num_params, &uarg->num_params))
> >                 return -EFAULT;
> >
> > -       if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
> > +       if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) > buf.buf_len)
> >                 return -EINVAL;
> >
> >         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
> >
> > ---
> > base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e
> > change-id: 20250428-tee-sizecheck-299d5eff8fc7
> >
> > --
> > Jann Horn <jannh@google.com>
> >

I ran this through the arm32 qemu virt machine to test my new development setup,
so:

Tested-by: Rouven Czerwinski <rouven.czerwinski@linaro.org>

Best regards,
Rouven

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels
       [not found] < <CAK8z29XWCujUdLXcHz075+xcix8HV2Mp3EtxxX9GB7vGjwi3HA@mail.gmail.com>
@ 2025-04-30 13:00 ` Jens Wiklander
  0 siblings, 0 replies; 7+ messages in thread
From: Jens Wiklander @ 2025-04-30 13:00 UTC (permalink / raw)
  To: op-tee

[-- Attachment #1: Type: text/plain, Size: 4169 bytes --]

On Wed, Apr 30, 2025 at 2:12 PM Rouven Czerwinski
<rouven.czerwinski@linaro.org> wrote:
>
> Hi,
>
> On Wed, 30 Apr 2025 at 13:53, Jens Wiklander <jens.wiklander@linaro.org> wrote:
> >
> > On Mon, Apr 28, 2025 at 3:06 PM Jann Horn <jannh@google.com> wrote:
> > >
> > > The current code around TEE_IOCTL_PARAM_SIZE() is a bit wrong on
> > > 32-bit kernels: Multiplying a user-provided 32-bit value with the
> > > size of a structure can wrap around on such platforms.
> > >
> > > Fix it by using saturating arithmetic for the size calculation.
> > >
> > > This has no security consequences because, in all users of
> > > TEE_IOCTL_PARAM_SIZE(), the subsequent kcalloc() implicitly checks
> > > for wrapping.
> > >
> > > Signed-off-by: Jann Horn <jannh@google.com>
> > > ---
> > > Note that I don't have a test device with a TEE; I only compile-tested
> > > the change on an x86-64 build.
> > > ---
> > >  drivers/tee/tee_core.c | 11 ++++++-----
> > >  1 file changed, 6 insertions(+), 5 deletions(-)
> >
> > Looks good, I'm picking up this.
> >
> > Thanks,
> > Jens
> >
> > >
> > > diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
> > > index d113679b1e2d..acc7998758ad 100644
> > > --- a/drivers/tee/tee_core.c
> > > +++ b/drivers/tee/tee_core.c
> > > @@ -10,6 +10,7 @@
> > >  #include <linux/fs.h>
> > >  #include <linux/idr.h>
> > >  #include <linux/module.h>
> > > +#include <linux/overflow.h>
> > >  #include <linux/slab.h>
> > >  #include <linux/tee_core.h>
> > >  #include <linux/uaccess.h>
> > > @@ -19,7 +20,7 @@
> > >
> > >  #define TEE_NUM_DEVICES        32
> > >
> > > -#define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
> > > +#define TEE_IOCTL_PARAM_SIZE(x) (size_mul(sizeof(struct tee_param), (x)))
> > >
> > >  #define TEE_UUID_NS_NAME_SIZE  128
> > >
> > > @@ -487,7 +488,7 @@ static int tee_ioctl_open_session(struct tee_context *ctx,
> > >         if (copy_from_user(&arg, uarg, sizeof(arg)))
> > >                 return -EFAULT;
> > >
> > > -       if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> > > +       if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
> > >                 return -EINVAL;
> > >
> > >         if (arg.num_params) {
> > > @@ -565,7 +566,7 @@ static int tee_ioctl_invoke(struct tee_context *ctx,
> > >         if (copy_from_user(&arg, uarg, sizeof(arg)))
> > >                 return -EFAULT;
> > >
> > > -       if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> > > +       if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
> > >                 return -EINVAL;
> > >
> > >         if (arg.num_params) {
> > > @@ -699,7 +700,7 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
> > >         if (get_user(num_params, &uarg->num_params))
> > >                 return -EFAULT;
> > >
> > > -       if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
> > > +       if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) != buf.buf_len)
> > >                 return -EINVAL;
> > >
> > >         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
> > > @@ -798,7 +799,7 @@ static int tee_ioctl_supp_send(struct tee_context *ctx,
> > >             get_user(num_params, &uarg->num_params))
> > >                 return -EFAULT;
> > >
> > > -       if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
> > > +       if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) > buf.buf_len)
> > >                 return -EINVAL;
> > >
> > >         params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
> > >
> > > ---
> > > base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e
> > > change-id: 20250428-tee-sizecheck-299d5eff8fc7
> > >
> > > --
> > > Jann Horn <jannh@google.com>
> > >
>
> I ran this through the arm32 qemu virt machine to test my new development setup,
> so:
>
> Tested-by: Rouven Czerwinski <rouven.czerwinski@linaro.org>

Thanks for testing.

Cheers,
Jens

>
> Best regards,
> Rouven

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels
  2025-04-28 13:06 Jann Horn
  2025-04-30 11:52 ` Jens Wiklander
@ 2025-05-01 20:01 ` David Laight
  2025-05-02 12:28   ` Jann Horn
  1 sibling, 1 reply; 7+ messages in thread
From: David Laight @ 2025-05-01 20:01 UTC (permalink / raw)
  To: op-tee

[-- Attachment #1: Type: text/plain, Size: 3280 bytes --]

On Mon, 28 Apr 2025 15:06:43 +0200
Jann Horn <jannh@google.com> wrote:

> The current code around TEE_IOCTL_PARAM_SIZE() is a bit wrong on
> 32-bit kernels: Multiplying a user-provided 32-bit value with the
> size of a structure can wrap around on such platforms.
> 
> Fix it by using saturating arithmetic for the size calculation.

Why not just add a sanity check on 'num_params' after it is read.
Max is 31 (1024-32)/32), but any sane limit will do because of
the buf.buf_len test.

	David

> 
> This has no security consequences because, in all users of
> TEE_IOCTL_PARAM_SIZE(), the subsequent kcalloc() implicitly checks
> for wrapping.
> 
> Signed-off-by: Jann Horn <jannh@google.com>
> ---
> Note that I don't have a test device with a TEE; I only compile-tested
> the change on an x86-64 build.
> ---
>  drivers/tee/tee_core.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
> index d113679b1e2d..acc7998758ad 100644
> --- a/drivers/tee/tee_core.c
> +++ b/drivers/tee/tee_core.c
> @@ -10,6 +10,7 @@
>  #include <linux/fs.h>
>  #include <linux/idr.h>
>  #include <linux/module.h>
> +#include <linux/overflow.h>
>  #include <linux/slab.h>
>  #include <linux/tee_core.h>
>  #include <linux/uaccess.h>
> @@ -19,7 +20,7 @@
>  
>  #define TEE_NUM_DEVICES	32
>  
> -#define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
> +#define TEE_IOCTL_PARAM_SIZE(x) (size_mul(sizeof(struct tee_param), (x)))
>  
>  #define TEE_UUID_NS_NAME_SIZE	128
>  
> @@ -487,7 +488,7 @@ static int tee_ioctl_open_session(struct tee_context *ctx,
>  	if (copy_from_user(&arg, uarg, sizeof(arg)))
>  		return -EFAULT;
>  
> -	if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> +	if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
>  		return -EINVAL;
>  
>  	if (arg.num_params) {
> @@ -565,7 +566,7 @@ static int tee_ioctl_invoke(struct tee_context *ctx,
>  	if (copy_from_user(&arg, uarg, sizeof(arg)))
>  		return -EFAULT;
>  
> -	if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
> +	if (size_add(sizeof(arg), TEE_IOCTL_PARAM_SIZE(arg.num_params)) != buf.buf_len)
>  		return -EINVAL;
>  
>  	if (arg.num_params) {
> @@ -699,7 +700,7 @@ static int tee_ioctl_supp_recv(struct tee_context *ctx,
>  	if (get_user(num_params, &uarg->num_params))
>  		return -EFAULT;
>  
> -	if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
> +	if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) != buf.buf_len)
>  		return -EINVAL;
>  
>  	params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
> @@ -798,7 +799,7 @@ static int tee_ioctl_supp_send(struct tee_context *ctx,
>  	    get_user(num_params, &uarg->num_params))
>  		return -EFAULT;
>  
> -	if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
> +	if (size_add(sizeof(*uarg), TEE_IOCTL_PARAM_SIZE(num_params)) > buf.buf_len)
>  		return -EINVAL;
>  
>  	params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
> 
> ---
> base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e
> change-id: 20250428-tee-sizecheck-299d5eff8fc7
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels
  2025-05-01 20:01 ` David Laight
@ 2025-05-02 12:28   ` Jann Horn
  0 siblings, 0 replies; 7+ messages in thread
From: Jann Horn @ 2025-05-02 12:28 UTC (permalink / raw)
  To: op-tee

[-- Attachment #1: Type: text/plain, Size: 652 bytes --]

On Thu, May 1, 2025 at 10:02 PM David Laight
<david.laight.linux@gmail.com> wrote:
>
> On Mon, 28 Apr 2025 15:06:43 +0200
> Jann Horn <jannh@google.com> wrote:
>
> > The current code around TEE_IOCTL_PARAM_SIZE() is a bit wrong on
> > 32-bit kernels: Multiplying a user-provided 32-bit value with the
> > size of a structure can wrap around on such platforms.
> >
> > Fix it by using saturating arithmetic for the size calculation.
>
> Why not just add a sanity check on 'num_params' after it is read.
> Max is 31 (1024-32)/32), but any sane limit will do because of
> the buf.buf_len test.

That would work, too. I don't know which way looks nicer.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels
       [not found] < <CAG48ez1DPWhT2dhd1iptFawWjteh_=pZ4M6Yq5KKCq2DTArnqw@mail.gmail.com>
@ 2025-05-02 12:41 ` David Laight
  0 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2025-05-02 12:41 UTC (permalink / raw)
  To: op-tee

[-- Attachment #1: Type: text/plain, Size: 1112 bytes --]

On Fri, 2 May 2025 14:28:21 +0200
Jann Horn <jannh@google.com> wrote:

> On Thu, May 1, 2025 at 10:02 PM David Laight
> <david.laight.linux@gmail.com> wrote:
> >
> > On Mon, 28 Apr 2025 15:06:43 +0200
> > Jann Horn <jannh@google.com> wrote:
> >  
> > > The current code around TEE_IOCTL_PARAM_SIZE() is a bit wrong on
> > > 32-bit kernels: Multiplying a user-provided 32-bit value with the
> > > size of a structure can wrap around on such platforms.
> > >
> > > Fix it by using saturating arithmetic for the size calculation.  
> >
> > Why not just add a sanity check on 'num_params' after it is read.
> > Max is 31 (1024-32)/32), but any sane limit will do because of
> > the buf.buf_len test.  
> 
> That would work, too. I don't know which way looks nicer.

The saturating arithmetic functions are non-obvious and non-trivial.
I looked at the code to check where buf.buf_len came from,
without its sanity check the user could craft a request where it
matched the saturated size.

I think I'd sanity check the number of items and then check that that
buffer length is right for the number of items.

	David

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-05-02 12:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] < <CAG48ez1DPWhT2dhd1iptFawWjteh_=pZ4M6Yq5KKCq2DTArnqw@mail.gmail.com>
2025-05-02 12:41 ` [PATCH] tee: Prevent size calculation wraparound on 32-bit kernels David Laight
     [not found] < <CAK8z29XWCujUdLXcHz075+xcix8HV2Mp3EtxxX9GB7vGjwi3HA@mail.gmail.com>
2025-04-30 13:00 ` Jens Wiklander
     [not found] < <CAHUa44E_JZdYnGrReP0zWCP1wdu2BdJ9DSZZ3a2OiobRj61ThQ@mail.gmail.com>
2025-04-30 12:12 ` Rouven Czerwinski
2025-04-28 13:06 Jann Horn
2025-04-30 11:52 ` Jens Wiklander
2025-05-01 20:01 ` David Laight
2025-05-02 12:28   ` Jann Horn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox