2021-11-07 20:31:34

by Luiz Sampaio

[permalink] [raw]
Subject: [PATCH 2/2] auxdisplay: charlcd: checking for pointer reference before dereferencing

Check if the pointer lcd->ops->init_display exists before dereferencing it.
If a driver called charlcd_init() without defining the ops, this would
return segmentation fault, as happened to me when implementing a charlcd
driver. Checking the pointer before dereferencing protects from
segmentation fault.

Signed-off-by: Luiz Sampaio <[email protected]>
---
drivers/auxdisplay/charlcd.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/auxdisplay/charlcd.c b/drivers/auxdisplay/charlcd.c
index cca3b600c0ba..47363fb2fe94 100644
--- a/drivers/auxdisplay/charlcd.c
+++ b/drivers/auxdisplay/charlcd.c
@@ -578,6 +578,9 @@ static int charlcd_init(struct charlcd *lcd)
* Since charlcd_init_display() needs to write data, we have to
* enable mark the LCD initialized just before.
*/
+ if (!lcd->ops->init_display)
+ return -EFAULT;
+
ret = lcd->ops->init_display(lcd);
if (ret)
return ret;
--
2.33.1


2021-11-08 14:23:34

by Miguel Ojeda

[permalink] [raw]
Subject: Re: [PATCH 2/2] auxdisplay: charlcd: checking for pointer reference before dereferencing

On Sun, Nov 7, 2021 at 4:03 PM Luiz Sampaio <[email protected]> wrote:
>
> Check if the pointer lcd->ops->init_display exists before dereferencing it.
> If a driver called charlcd_init() without defining the ops, this would
> return segmentation fault, as happened to me when implementing a charlcd
> driver. Checking the pointer before dereferencing protects from
> segmentation fault.

It can't hurt -- thanks! I think `EINVAL` makes more sense here, also
we could use `WARN_ON`:

if (WARN_ON(!lcd->ops->init_display))
return -EINVAL;

Cheers,
Miguel

2021-11-10 00:25:25

by Luiz Sampaio

[permalink] [raw]
Subject: [PATCH v2 0/2] Fixing bug that would segmentation fault

This series of patches consists of one patch fixing a simple coding style
issue and one patch fixing a bug that would cause segmentation fault.
Basically, there was a pointer that was being dereferenced without testing
if the pointer exists. This patch adds a protection, returning EFAULT in
case the pointer is NULL.

Changes in v2:
- Changed return to -EINVAL and using WARN_ON as suggested
- Note in response for Miguel's comment: for the first patch, I ran the
script './scripts/checkpatch.pl --file --terse' to see with the file
had any coding style issue. That was when I was suggested to remove
'int' from 'unsigned long' declaration

Luiz Sampaio (2):
auxdisplay: charlcd: fixing coding style issue
auxdisplay: charlcd: checking for pointer reference before
dereferencing

drivers/auxdisplay/charlcd.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

--
2.33.1

2021-11-10 00:25:34

by Luiz Sampaio

[permalink] [raw]
Subject: [PATCH v2 1/2] auxdisplay: charlcd: fixing coding style issue

Removing 'int' from 'unsigned long int' declaration, which is unnecessary.

Signed-off-by: Luiz Sampaio <[email protected]>
---
drivers/auxdisplay/charlcd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/auxdisplay/charlcd.c b/drivers/auxdisplay/charlcd.c
index 304accde365c..cca3b600c0ba 100644
--- a/drivers/auxdisplay/charlcd.c
+++ b/drivers/auxdisplay/charlcd.c
@@ -37,7 +37,7 @@ struct charlcd_priv {
bool must_clear;

/* contains the LCD config state */
- unsigned long int flags;
+ unsigned long flags;

/* Current escape sequence and it's length or -1 if outside */
struct {
--
2.33.1

2021-11-24 11:02:26

by Miguel Ojeda

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] auxdisplay: charlcd: fixing coding style issue

On Tue, Nov 9, 2021 at 11:07 PM Luiz Sampaio <[email protected]> wrote:
>
> Removing 'int' from 'unsigned long int' declaration, which is unnecessary.
>
> Signed-off-by: Luiz Sampaio <[email protected]>

Queued up, thanks!

Cheers,
Miguel