diff mbox series

[net-next] net: qrtr: make checks in qrtr_endpoint_post() stricter

Message ID 20210827132428.GA8934@kili (mailing list archive)
State Superseded
Headers show
Series [net-next] net: qrtr: make checks in qrtr_endpoint_post() stricter | expand

Commit Message

Dan Carpenter Aug. 27, 2021, 1:24 p.m. UTC
These checks are still not strict enough.  The main problem is that if
"cb->type == QRTR_TYPE_NEW_SERVER" is true then "len - hdrlen" is
guaranteed to be 4 but we need to be at least 16 bytes.  In fact, we
can reject everything smaller than sizeof(*pkt) which is 20 bytes.

Also I don't like the ALIGN(size, 4).  It's better to just insist that
data is needs to be aligned at the start.

Fixes: 0baa99ee353c ("net: qrtr: Allow non-immediate node routing")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
This was from review.  Not tested.

 net/qrtr/qrtr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

butt3rflyh4ck Aug. 27, 2021, 3:35 p.m. UTC | #1
On Fri, Aug 27, 2021 at 9:24 PM Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> These checks are still not strict enough.  The main problem is that if
> "cb->type == QRTR_TYPE_NEW_SERVER" is true then "len - hdrlen" is
> guaranteed to be 4 but we need to be at least 16 bytes.  In fact, we
> can reject everything smaller than sizeof(*pkt) which is 20 bytes.
>
> Also I don't like the ALIGN(size, 4).  It's better to just insist that
> data is needs to be aligned at the start.
>
> Fixes: 0baa99ee353c ("net: qrtr: Allow non-immediate node routing")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> This was from review.  Not tested.
>
>  net/qrtr/qrtr.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> index b8508e35d20e..dbb647f5481b 100644
> --- a/net/qrtr/qrtr.c
> +++ b/net/qrtr/qrtr.c
> @@ -493,7 +493,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
>                 goto err;
>         }
>

> -       if (!size || len != ALIGN(size, 4) + hdrlen)
> +       if (!size || size % 3 || len != size + hdrlen)

Hi, (size % 3)  is wrong, is it (size & 3), right ?

>                 goto err;
>
>         if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA &&
> @@ -506,8 +506,12 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
>
>         if (cb->type == QRTR_TYPE_NEW_SERVER) {
>                 /* Remote node endpoint can bridge other distant nodes */
> -               const struct qrtr_ctrl_pkt *pkt = data + hdrlen;
> +               const struct qrtr_ctrl_pkt *pkt;
>
> +               if (size < sizeof(*pkt))

Yes, the size should not be smaller than sizeof(*pkt).

> +                       goto err;
> +
> +               pkt = data + hdrlen;
>                 qrtr_node_assign(node, le32_to_cpu(pkt->server.node));
>         }
>
> --
> 2.20.1
>


Regards,
  butt3rflyh4ck
Dan Carpenter Aug. 27, 2021, 5:17 p.m. UTC | #2
On Fri, Aug 27, 2021 at 11:35:31PM +0800, butt3rflyh4ck wrote:
> On Fri, Aug 27, 2021 at 9:24 PM Dan Carpenter <dan.carpenter@oracle.com> wrote:
> >
> > These checks are still not strict enough.  The main problem is that if
> > "cb->type == QRTR_TYPE_NEW_SERVER" is true then "len - hdrlen" is
> > guaranteed to be 4 but we need to be at least 16 bytes.  In fact, we
> > can reject everything smaller than sizeof(*pkt) which is 20 bytes.
> >
> > Also I don't like the ALIGN(size, 4).  It's better to just insist that
> > data is needs to be aligned at the start.
> >
> > Fixes: 0baa99ee353c ("net: qrtr: Allow non-immediate node routing")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > This was from review.  Not tested.
> >
> >  net/qrtr/qrtr.c | 8 ++++++--
> >  1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> > index b8508e35d20e..dbb647f5481b 100644
> > --- a/net/qrtr/qrtr.c
> > +++ b/net/qrtr/qrtr.c
> > @@ -493,7 +493,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
> >                 goto err;
> >         }
> >
> 
> > -       if (!size || len != ALIGN(size, 4) + hdrlen)
> > +       if (!size || size % 3 || len != size + hdrlen)
> 
> Hi, (size % 3)  is wrong, is it (size & 3), right ?
> 

Yeah.  Thanks for catching that.  I means & 3.

regards,
dan carpenter
diff mbox series

Patch

diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
index b8508e35d20e..dbb647f5481b 100644
--- a/net/qrtr/qrtr.c
+++ b/net/qrtr/qrtr.c
@@ -493,7 +493,7 @@  int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
 		goto err;
 	}
 
-	if (!size || len != ALIGN(size, 4) + hdrlen)
+	if (!size || size % 3 || len != size + hdrlen)
 		goto err;
 
 	if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA &&
@@ -506,8 +506,12 @@  int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
 
 	if (cb->type == QRTR_TYPE_NEW_SERVER) {
 		/* Remote node endpoint can bridge other distant nodes */
-		const struct qrtr_ctrl_pkt *pkt = data + hdrlen;
+		const struct qrtr_ctrl_pkt *pkt;
 
+		if (size < sizeof(*pkt))
+			goto err;
+
+		pkt = data + hdrlen;
 		qrtr_node_assign(node, le32_to_cpu(pkt->server.node));
 	}