Skip to contentSkip to conversation

arm: socfpga: mailbox: Fix off-by-one error on command length checking

PATCH Superseded Ley Foon Tan 4 participants 6 messages Apr 19, 2019 – Apr 22, 2019

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 arm: socfpga: mailbox: Fix off-by-one error on command length checking

A mailbox command contains 1-DWORD header + arguments. The "len" variable only contains the length of the arguments, but not the 1-DWORD header. Include the length of header when checking the ring buffer space to prevent off-by-one error.

---
v2->v3:
- Update commit description.

---
 arch/arm/mach-socfpga/mailbox_s10.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mach-socfpga/mailbox_s10.c +1 -1
index 3c33223936..8363c93e90 100644
@@ -59,7 +59,7 @@ static __always_inline int mbox_fill_cmd_circular_buff(u32 header, u32 len,
 	 */
 	if (((cin + 1) % MBOX_CMD_BUFFER_SIZE) == cout ||
 	    ((MBOX_CMD_BUFFER_SIZE - cin + cout - 1) %
-	     MBOX_CMD_BUFFER_SIZE) < len)
+	     MBOX_CMD_BUFFER_SIZE) < (len + 1))
 		return -ENOMEM;
 
 	/* write header to circular buffer */
Signature · 1 line
2.19.0
#1 lore raw

PATCH v3 1/1 arm: socfpga: mailbox: Fix off-by-one error on command length checking

Quoted 4 lines · Ley Foon Tan

A mailbox command contains 1-DWORD header + arguments. The "len" variable only contains the length of the arguments, but not the 1-DWORD header. Include the length of header when checking the ring buffer space to prevent off-by-one error.

How long is a DWORD ? Windows API (which we have nothing to do with) defines that as 32bit type, "typedef unsigned long DWORD;", see [1]. But the patch below fixes an off-by-one error , not off by four error ?

[1]
https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types

Quoted 23 lines

---
v2->v3:
- Update commit description.

---
 arch/arm/mach-socfpga/mailbox_s10.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mach-socfpga/mailbox_s10.c +1 -1
index 3c33223936..8363c93e90 100644
@@ -59,7 +59,7 @@ static __always_inline int mbox_fill_cmd_circular_buff(u32 header, u32 len,
 	 */
 	if (((cin + 1) % MBOX_CMD_BUFFER_SIZE) == cout ||
 	    ((MBOX_CMD_BUFFER_SIZE - cin + cout - 1) %
-	     MBOX_CMD_BUFFER_SIZE) < len)
+	     MBOX_CMD_BUFFER_SIZE) < (len + 1))
 		return -ENOMEM;
 
 	/* write header to circular buffer */
Signature · 2 lines
Best regards,
Marek Vasut
#2 lore raw

PATCH v3 1/1 arm: socfpga: mailbox: Fix off-by-one error on command length checking

Marek Vasut <[email protected]> schrieb am Fr., 19. Apr. 2019, 11:29:

Quoted 10 lines · Ley Foon Tan
Quoted 4 lines

A mailbox command contains 1-DWORD header + arguments. The "len" variable only contains the length of the arguments, but not the 1-DWORD header. Include the length of header when checking the ring buffer space to prevent off-by-one error.

How long is a DWORD ? Windows API (which we have nothing to do with) defines that as 32bit type, "typedef unsigned long DWORD;", see [1]. But the patch below fixes an off-by-one error , not off by four error ?

As all the macros for that mailbox seem to do u32 index access only, I'd be ok with the commit message if it didn't use the term 'DWORD'...

Regards,
Simon

Quoted 34 lines

[1]
https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types

Review

Quoted 10 lines

---
v2->v3:
- Update commit description.

---
 arch/arm/mach-socfpga/mailbox_s10.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/arch/arm/mach-socfpga/mailbox_s10.c

arch/arm/mach-socfpga/mailbox_s10.c static __always_inline int

Quoted 4 lines

index 3c33223936..8363c93e90 100644

arch/arm/mach-socfpga/mailbox_s10.c +0 -0
@@ -59,7 +59,7 @@ static __always_inline int

mbox_fill_cmd_circular_buff(u32 header, u32 len,

arch/arm/mach-socfpga/mailbox_s10.c static __always_inline int mbox_fill_cmd_circular_buff(u32 header, u32 len, · line 65

Quoted 9 lines
arch/arm/mach-socfpga/mailbox_s10.c +1 -1
       */
      if (((cin + 1) % MBOX_CMD_BUFFER_SIZE) == cout ||
          ((MBOX_CMD_BUFFER_SIZE - cin + cout - 1) %
-          MBOX_CMD_BUFFER_SIZE) < len)
+          MBOX_CMD_BUFFER_SIZE) < (len + 1))
              return -ENOMEM;
 
      /* write header to circular buffer */

--
Best regards,
Marek Vasut

#3 lore raw

PATCH v3 1/1 arm: socfpga: mailbox: Fix off-by-one error on command length checking

Quoted 12 lines · Simon Goldschmidt

Marek Vasut <marex at denx.de <mailto:[email protected]>> schrieb am Fr., 19. Apr. 2019, 11:29:

On 4/19/19 8:17 AM, Ley Foon Tan wrote:
> A mailbox command contains 1-DWORD header + arguments. The "len" variable
> only contains the length of the arguments, but not the 1-DWORD header. > Include the length of header when checking the ring buffer space to > prevent off-by-one error.

Quoted 7 lines

How long is a DWORD ? Windows API (which we have nothing to do with) defines that as 32bit type, "typedef unsigned long DWORD;", see [1]. But the patch below fixes an off-by-one error , not off by four error ?

As all the macros for that mailbox seem to do u32 index access only, I'd be ok with the commit message if it didn't use the term 'DWORD'...

Does that mean MBOX_CMD_BUFFER_SIZE is not in byte units, but in u32 units ?

Quoted 40 lines

Regards,
Simon

    [1]
    https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types

    > Signed-off-by: Ley Foon Tan <[email protected]
    <mailto:[email protected]>>
    > Signed-off-by: Chee Hong Ang <[email protected]
    <mailto:[email protected]>>
    > ---
    > v2->v3:
    > - Update commit description.
    > ---
    >  arch/arm/mach-socfpga/mailbox_s10.c | 2 +-
    >  1 file changed, 1 insertion(+), 1 deletion(-)
    >
    > diff --git a/arch/arm/mach-socfpga/mailbox_s10.c
    b/arch/arm/mach-socfpga/mailbox_s10.c
    > index 3c33223936..8363c93e90 100644
    > --- a/arch/arm/mach-socfpga/mailbox_s10.c
    > +++ b/arch/arm/mach-socfpga/mailbox_s10.c
    > @@ -59,7 +59,7 @@ static __always_inline int
    mbox_fill_cmd_circular_buff(u32 header, u32 len,
    >        */
    >       if (((cin + 1) % MBOX_CMD_BUFFER_SIZE) == cout ||
    >           ((MBOX_CMD_BUFFER_SIZE - cin + cout - 1) %
    > -          MBOX_CMD_BUFFER_SIZE) < len)
    > +          MBOX_CMD_BUFFER_SIZE) < (len + 1))
    >               return -ENOMEM;
    > 
    >       /* write header to circular buffer */
    >


    -- 
    Best regards,
    Marek Vasut
Signature · 2 lines
Best regards,
Marek Vasut
#4 lore raw

PATCH v3 1/1 arm: socfpga: mailbox: Fix off-by-one error on command length checking

Quoted 19 lines · Marek Vasut
Quoted 17 lines · Simon Goldschmidt

Marek Vasut <marex at denx.de <mailto:[email protected]>> schrieb am Fr., 19. Apr. 2019, 11:29:

On 4/19/19 8:17 AM, Ley Foon Tan wrote:
> A mailbox command contains 1-DWORD header + arguments. The "len" variable
> only contains the length of the arguments, but not the 1-DWORD header. > Include the length of header when checking the ring buffer space to > prevent off-by-one error.

How long is a DWORD ? Windows API (which we have nothing to do with) defines that as 32bit type, "typedef unsigned long DWORD;", see [1]. But the patch below fixes an off-by-one error , not off by four error ?

Quoted 4 lines
Quoted 2 lines

As all the macros for that mailbox seem to do u32 index access only, I'd be ok with the commit message if it didn't use the term 'DWORD'...

Does that mean MBOX_CMD_BUFFER_SIZE is not in byte units, but in u32 units ?

Yes, it is in u32 unit. One slot location is u32.
How about change DWORD to u32 in commit message?

Regards
Ley Foon

#5 lore raw

PATCH v3 1/1 arm: socfpga: mailbox: Fix off-by-one error on command length checking

Quoted 20 lines · Ley Foon Tan
Quoted 19 lines · Marek Vasut
Quoted 17 lines · Simon Goldschmidt

Marek Vasut <marex at denx.de <mailto:[email protected]>> schrieb am Fr., 19. Apr. 2019, 11:29:

On 4/19/19 8:17 AM, Ley Foon Tan wrote:
> A mailbox command contains 1-DWORD header + arguments. The "len" variable
> only contains the length of the arguments, but not the 1-DWORD header. > Include the length of header when checking the ring buffer space to > prevent off-by-one error.

How long is a DWORD ? Windows API (which we have nothing to do with) defines that as 32bit type, "typedef unsigned long DWORD;", see [1]. But the patch below fixes an off-by-one error , not off by four error ?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

As all the macros for that mailbox seem to do u32 index access only, I'd be ok with the commit message if it didn't use the term 'DWORD'...

Does that mean MBOX_CMD_BUFFER_SIZE is not in byte units, but in u32 units ?

Yes, it is in u32 unit. One slot location is u32.
How about change DWORD to u32 in commit message?

Yes please. It would also make sense to add a comment into the code that len is not in bytes, but u32 units .

Signature · 2 lines
Best regards,
Marek Vasut