[v2,10/15] auxdisplay: linedisp: Provide a small buffer in the struct linedisp

Message ID 20240212170423.2860895-11-andriy.shevchenko@linux.intel.com
State New
Headers
Series auxdisplay: linedisp: Clean up and add new driver |

Commit Message

Andy Shevchenko Feb. 12, 2024, 5:01 p.m. UTC
  There is a driver that uses small buffer for the string, when we
add a new one, we may avoid duplication and use one provided by
the line display library. Allow user to skip buffer pointer when
registering a device.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/auxdisplay/line-display.c | 4 ++--
 drivers/auxdisplay/line-display.h | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)
  

Comments

Geert Uytterhoeven Feb. 15, 2024, 10:40 a.m. UTC | #1
Hi Andy,

On Mon, Feb 12, 2024 at 6:04 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
> There is a driver that uses small buffer for the string, when we
> add a new one, we may avoid duplication and use one provided by
> the line display library. Allow user to skip buffer pointer when
> registering a device.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Thanks for your patch!

> --- a/drivers/auxdisplay/line-display.c
> +++ b/drivers/auxdisplay/line-display.c
> @@ -330,8 +330,8 @@ int linedisp_register(struct linedisp *linedisp, struct device *parent,
>         linedisp->dev.parent = parent;
>         linedisp->dev.type = &linedisp_type;
>         linedisp->ops = ops;
> -       linedisp->buf = buf;
> -       linedisp->num_chars = num_chars;
> +       linedisp->buf = buf ? buf : linedisp->curr;
> +       linedisp->num_chars = buf ? num_chars : min(num_chars, LINEDISP_DEFAULT_BUF_SZ);

I think it would be safer to return an error if buf == NULL and
num_chars < LINEDISP_DEFAULT_BUF_SZ.
Else a careless driver that doesn't check linedisp->num_chars might
overflow the buffer.

>         linedisp->scroll_rate = DEFAULT_SCROLL_RATE;
>
>         err = ida_alloc(&linedisp_id, GFP_KERNEL);

Gr{oetje,eeting}s,

                        Geert
  
Andy Shevchenko Feb. 15, 2024, 12:16 p.m. UTC | #2
On Thu, Feb 15, 2024 at 11:40:44AM +0100, Geert Uytterhoeven wrote:
> On Mon, Feb 12, 2024 at 6:04 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:

..

> > +       linedisp->buf = buf ? buf : linedisp->curr;
> > +       linedisp->num_chars = buf ? num_chars : min(num_chars, LINEDISP_DEFAULT_BUF_SZ);
> 
> I think it would be safer to return an error if buf == NULL and
> num_chars < LINEDISP_DEFAULT_BUF_SZ.

I think you meant >= ?

> Else a careless driver that doesn't check linedisp->num_chars might
> overflow the buffer.

Okay, check has been added.
  
Andy Shevchenko Feb. 15, 2024, 12:19 p.m. UTC | #3
On Thu, Feb 15, 2024 at 02:17:00PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 15, 2024 at 11:40:44AM +0100, Geert Uytterhoeven wrote:
> > On Mon, Feb 12, 2024 at 6:04 PM Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:

..

> > > +       linedisp->buf = buf ? buf : linedisp->curr;
> > > +       linedisp->num_chars = buf ? num_chars : min(num_chars, LINEDISP_DEFAULT_BUF_SZ);
> > 
> > I think it would be safer to return an error if buf == NULL and
> > num_chars < LINEDISP_DEFAULT_BUF_SZ.
> 
> I think you meant >= ?
> 
> > Else a careless driver that doesn't check linedisp->num_chars might
> > overflow the buffer.
> 
> Okay, check has been added.

Hold on, but I have min() being called, isn't it enough?
  
Geert Uytterhoeven Feb. 15, 2024, 12:33 p.m. UTC | #4
Hi Andy,

On Thu, Feb 15, 2024 at 1:19 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
> On Thu, Feb 15, 2024 at 02:17:00PM +0200, Andy Shevchenko wrote:
> > On Thu, Feb 15, 2024 at 11:40:44AM +0100, Geert Uytterhoeven wrote:
> > > On Mon, Feb 12, 2024 at 6:04 PM Andy Shevchenko
> > > <andriy.shevchenko@linux.intel.com> wrote:
> > > > +       linedisp->buf = buf ? buf : linedisp->curr;
> > > > +       linedisp->num_chars = buf ? num_chars : min(num_chars, LINEDISP_DEFAULT_BUF_SZ);
> > >
> > > I think it would be safer to return an error if buf == NULL and
> > > num_chars < LINEDISP_DEFAULT_BUF_SZ.
> >
> > I think you meant >= ?

Oops, yes/

> >
> > > Else a careless driver that doesn't check linedisp->num_chars might
> > > overflow the buffer.
> >
> > Okay, check has been added.
>
> Hold on, but I have min() being called, isn't it enough?

Yes you have.

A careless driver might not use linedisp->num_chars later, but instead
just hardcode e.g. memcpy(linedisp->buf, source, LARGE_BUF_SIZE).

Gr{oetje,eeting}s,

                        Geert
  
Andy Shevchenko Feb. 15, 2024, 1:57 p.m. UTC | #5
On Thu, Feb 15, 2024 at 01:33:29PM +0100, Geert Uytterhoeven wrote:
> On Thu, Feb 15, 2024 at 1:19 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> > On Thu, Feb 15, 2024 at 02:17:00PM +0200, Andy Shevchenko wrote:
> > > On Thu, Feb 15, 2024 at 11:40:44AM +0100, Geert Uytterhoeven wrote:
> > > > On Mon, Feb 12, 2024 at 6:04 PM Andy Shevchenko
> > > > <andriy.shevchenko@linux.intel.com> wrote:
> > > > > +       linedisp->buf = buf ? buf : linedisp->curr;
> > > > > +       linedisp->num_chars = buf ? num_chars : min(num_chars, LINEDISP_DEFAULT_BUF_SZ);
> > > >
> > > > I think it would be safer to return an error if buf == NULL and
> > > > num_chars < LINEDISP_DEFAULT_BUF_SZ.
> > >
> > > I think you meant >= ?
> 
> Oops, yes/
> 
> > >
> > > > Else a careless driver that doesn't check linedisp->num_chars might
> > > > overflow the buffer.
> > >
> > > Okay, check has been added.
> >
> > Hold on, but I have min() being called, isn't it enough?
> 
> Yes you have.
> 
> A careless driver might not use linedisp->num_chars later, but instead
> just hardcode e.g. memcpy(linedisp->buf, source, LARGE_BUF_SIZE).

I see the point, yes, we need an additional check.
  

Patch

diff --git a/drivers/auxdisplay/line-display.c b/drivers/auxdisplay/line-display.c
index 75852ce6cc8d..d730cd0e1d03 100644
--- a/drivers/auxdisplay/line-display.c
+++ b/drivers/auxdisplay/line-display.c
@@ -330,8 +330,8 @@  int linedisp_register(struct linedisp *linedisp, struct device *parent,
 	linedisp->dev.parent = parent;
 	linedisp->dev.type = &linedisp_type;
 	linedisp->ops = ops;
-	linedisp->buf = buf;
-	linedisp->num_chars = num_chars;
+	linedisp->buf = buf ? buf : linedisp->curr;
+	linedisp->num_chars = buf ? num_chars : min(num_chars, LINEDISP_DEFAULT_BUF_SZ);
 	linedisp->scroll_rate = DEFAULT_SCROLL_RATE;
 
 	err = ida_alloc(&linedisp_id, GFP_KERNEL);
diff --git a/drivers/auxdisplay/line-display.h b/drivers/auxdisplay/line-display.h
index 65d782111f53..4c354b8f376e 100644
--- a/drivers/auxdisplay/line-display.h
+++ b/drivers/auxdisplay/line-display.h
@@ -54,12 +54,15 @@  struct linedisp_ops {
 	void (*update)(struct linedisp *linedisp);
 };
 
+#define LINEDISP_DEFAULT_BUF_SZ		8u
+
 /**
  * struct linedisp - character line display private data structure
  * @dev: the line display device
  * @id: instance id of this display
  * @timer: timer used to implement scrolling
  * @ops: character line display operations
+ * @curr: fallback buffer for the string
  * @buf: pointer to the buffer for the string currently displayed
  * @message: the full message to display or scroll on the display
  * @num_chars: the number of characters that can be displayed
@@ -73,6 +76,7 @@  struct linedisp {
 	struct timer_list timer;
 	const struct linedisp_ops *ops;
 	struct linedisp_map *map;
+	char curr[LINEDISP_DEFAULT_BUF_SZ];
 	char *buf;
 	char *message;
 	unsigned int num_chars;