* [PATCH 1/3] usb: early: xhci-dbc: Fix a potential out-of-bound memory access
2023-01-29 18:21 [PATCH 0/3] usb: early: xhci-dbc: Some fixes and speed-up Christophe JAILLET
@ 2023-01-29 18:23 ` Christophe JAILLET
2023-01-29 18:23 ` [PATCH 2/3] usb: early: xhci-dbc: Optimize early_xdbc_write() Christophe JAILLET
2023-01-29 18:23 ` [PATCH 3/3] usb: early: xhci-dbc: Use memcpy_and_pad() Christophe JAILLET
2 siblings, 0 replies; 4+ messages in thread
From: Christophe JAILLET @ 2023-01-29 18:23 UTC (permalink / raw)
To: gregkh, peterz, pmladek, john.ogness, baolu.lu, tglx, mingo
Cc: linux-usb, linux-kernel, kernel-janitors, Christophe JAILLET
If xdbc_bulk_write() fails, the values in 'buf' can be anything. So the
string is not guaranteed to be NULL terminated when xdbc_trace() is called.
Reserve an extra byte, which will be zeroed automatically because 'buf' is
a static variable, in order to avoid troubles, should it happen.
Fixes: aeb9dd1de98c ("usb/early: Add driver for xhci debug capability")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
drivers/usb/early/xhci-dbc.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/early/xhci-dbc.c b/drivers/usb/early/xhci-dbc.c
index 797047154820..f3e23be227d4 100644
--- a/drivers/usb/early/xhci-dbc.c
+++ b/drivers/usb/early/xhci-dbc.c
@@ -874,7 +874,8 @@ static int xdbc_bulk_write(const char *bytes, int size)
static void early_xdbc_write(struct console *con, const char *str, u32 n)
{
- static char buf[XDBC_MAX_PACKET];
+ /* static variables are zeroed, so buf is always NULL terminated */
+ static char buf[XDBC_MAX_PACKET + 1];
int chunk, ret;
int use_cr = 0;
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/3] usb: early: xhci-dbc: Optimize early_xdbc_write()
2023-01-29 18:21 [PATCH 0/3] usb: early: xhci-dbc: Some fixes and speed-up Christophe JAILLET
2023-01-29 18:23 ` [PATCH 1/3] usb: early: xhci-dbc: Fix a potential out-of-bound memory access Christophe JAILLET
@ 2023-01-29 18:23 ` Christophe JAILLET
2023-01-29 18:23 ` [PATCH 3/3] usb: early: xhci-dbc: Use memcpy_and_pad() Christophe JAILLET
2 siblings, 0 replies; 4+ messages in thread
From: Christophe JAILLET @ 2023-01-29 18:23 UTC (permalink / raw)
To: gregkh, peterz, pmladek, john.ogness, baolu.lu, tglx, mingo
Cc: linux-usb, linux-kernel, kernel-janitors, Christophe JAILLET
There is no point in zeroing 'buf'.
It would be cleared only once, and if the 'while' loop is executed several
times, all but the first run would have a 'dirty' buffer.
Moreover, the size of the chunk is computed in the loop and this size is
passed to xdbc_bulk_write().
So remove this useless memset().
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
Untested.
---
drivers/usb/early/xhci-dbc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/usb/early/xhci-dbc.c b/drivers/usb/early/xhci-dbc.c
index f3e23be227d4..965a24e47c0f 100644
--- a/drivers/usb/early/xhci-dbc.c
+++ b/drivers/usb/early/xhci-dbc.c
@@ -881,7 +881,7 @@ static void early_xdbc_write(struct console *con, const char *str, u32 n)
if (!xdbc.xdbc_reg)
return;
- memset(buf, 0, XDBC_MAX_PACKET);
+
while (n > 0) {
for (chunk = 0; chunk < XDBC_MAX_PACKET && n > 0; str++, chunk++, n--) {
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 3/3] usb: early: xhci-dbc: Use memcpy_and_pad()
2023-01-29 18:21 [PATCH 0/3] usb: early: xhci-dbc: Some fixes and speed-up Christophe JAILLET
2023-01-29 18:23 ` [PATCH 1/3] usb: early: xhci-dbc: Fix a potential out-of-bound memory access Christophe JAILLET
2023-01-29 18:23 ` [PATCH 2/3] usb: early: xhci-dbc: Optimize early_xdbc_write() Christophe JAILLET
@ 2023-01-29 18:23 ` Christophe JAILLET
2 siblings, 0 replies; 4+ messages in thread
From: Christophe JAILLET @ 2023-01-29 18:23 UTC (permalink / raw)
To: gregkh, peterz, pmladek, john.ogness, baolu.lu, tglx, mingo
Cc: linux-usb, linux-kernel, kernel-janitors, Christophe JAILLET
Instead of zeroing some memory and then copying data in part or all of it,
use memcpy_and_pad().
This avoids writing some memory twice and should save a few cycles.
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
drivers/usb/early/xhci-dbc.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/usb/early/xhci-dbc.c b/drivers/usb/early/xhci-dbc.c
index 965a24e47c0f..341408410ed9 100644
--- a/drivers/usb/early/xhci-dbc.c
+++ b/drivers/usb/early/xhci-dbc.c
@@ -499,8 +499,7 @@ static int xdbc_bulk_transfer(void *data, int size, bool read)
addr = xdbc.in_dma;
xdbc.flags |= XDBC_FLAGS_IN_PROCESS;
} else {
- memset(xdbc.out_buf, 0, XDBC_MAX_PACKET);
- memcpy(xdbc.out_buf, data, size);
+ memcpy_and_pad(xdbc.out_buf, XDBC_MAX_PACKET, data, size, 0);
addr = xdbc.out_dma;
xdbc.flags |= XDBC_FLAGS_OUT_PROCESS;
}
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread