2023-07-25 07:28:48

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH V3 1/2] serial: sprd: Assign sprd_port after initialized to avoid wrong access

On Tue, Jul 25, 2023 at 02:40:52PM +0800, Chunyan Zhang wrote:
> The global pointer 'sprd_port' may not zero when sprd_probe returns
> failure, that is a risk for sprd_port to be accessed afterward, and
> may lead to unexpected errors.
>
> For example:
>
> There are two UART ports, UART1 is used for console and configured in
> kernel command line, i.e. "console=";
>
> The UART1 probe failed and the memory allocated to sprd_port[1] was
> released, but sprd_port[1] was not set to NULL;
>
> In UART2 probe, the same virtual address was allocated to sprd_port[2],
> and UART2 probe process finally will go into sprd_console_setup() to
> register UART1 as console since it is configured as preferred console
> (filled to console_cmdline[]), but the console parameters (sprd_port[1])
> belong to UART2.
>
> So move the sprd_port[] assignment to where the port already initialized
> can avoid the above issue.
>
> Fixes: b7396a38fb28 ("tty/serial: Add Spreadtrum sc9836-uart driver support")
> Signed-off-by: Chunyan Zhang <[email protected]>
> ---
> V3:
> - Call uart_unregister_driver() only when the 'sprd_ports_num' decreases to 0;
> - Add calling sprd_rx_free_buf() instread of sprd_remove() under clean_up lable.
>
> V2:
> - Leave sprd_remove() to keep the unrelated code logic the same.
> ---
> drivers/tty/serial/sprd_serial.c | 25 +++++++++++++++++--------
> 1 file changed, 17 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/tty/serial/sprd_serial.c b/drivers/tty/serial/sprd_serial.c
> index b58f51296ace..fc1377029021 100644
> --- a/drivers/tty/serial/sprd_serial.c
> +++ b/drivers/tty/serial/sprd_serial.c
> @@ -1106,7 +1106,7 @@ static bool sprd_uart_is_console(struct uart_port *uport)
> static int sprd_clk_init(struct uart_port *uport)
> {
> struct clk *clk_uart, *clk_parent;
> - struct sprd_uart_port *u = sprd_port[uport->line];
> + struct sprd_uart_port *u = container_of(uport, struct sprd_uart_port, port);

Now that you are not allocaing the sprd_port[] pointers, shouldn't you
also remove that variable entirely?

thanks,

greg k-h


2023-07-25 09:13:05

by Chunyan Zhang

[permalink] [raw]
Subject: Re: [PATCH V3 1/2] serial: sprd: Assign sprd_port after initialized to avoid wrong access

On Tue, 25 Jul 2023 at 14:50, Greg Kroah-Hartman
<[email protected]> wrote:
>
> On Tue, Jul 25, 2023 at 02:40:52PM +0800, Chunyan Zhang wrote:
> > The global pointer 'sprd_port' may not zero when sprd_probe returns
> > failure, that is a risk for sprd_port to be accessed afterward, and
> > may lead to unexpected errors.
> >
> > For example:
> >
> > There are two UART ports, UART1 is used for console and configured in
> > kernel command line, i.e. "console=";
> >
> > The UART1 probe failed and the memory allocated to sprd_port[1] was
> > released, but sprd_port[1] was not set to NULL;
> >
> > In UART2 probe, the same virtual address was allocated to sprd_port[2],
> > and UART2 probe process finally will go into sprd_console_setup() to
> > register UART1 as console since it is configured as preferred console
> > (filled to console_cmdline[]), but the console parameters (sprd_port[1])
> > belong to UART2.
> >
> > So move the sprd_port[] assignment to where the port already initialized
> > can avoid the above issue.
> >
> > Fixes: b7396a38fb28 ("tty/serial: Add Spreadtrum sc9836-uart driver support")
> > Signed-off-by: Chunyan Zhang <[email protected]>
> > ---
> > V3:
> > - Call uart_unregister_driver() only when the 'sprd_ports_num' decreases to 0;
> > - Add calling sprd_rx_free_buf() instread of sprd_remove() under clean_up lable.
> >
> > V2:
> > - Leave sprd_remove() to keep the unrelated code logic the same.
> > ---
> > drivers/tty/serial/sprd_serial.c | 25 +++++++++++++++++--------
> > 1 file changed, 17 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/tty/serial/sprd_serial.c b/drivers/tty/serial/sprd_serial.c
> > index b58f51296ace..fc1377029021 100644
> > --- a/drivers/tty/serial/sprd_serial.c
> > +++ b/drivers/tty/serial/sprd_serial.c
> > @@ -1106,7 +1106,7 @@ static bool sprd_uart_is_console(struct uart_port *uport)
> > static int sprd_clk_init(struct uart_port *uport)
> > {
> > struct clk *clk_uart, *clk_parent;
> > - struct sprd_uart_port *u = sprd_port[uport->line];
> > + struct sprd_uart_port *u = container_of(uport, struct sprd_uart_port, port);
>
> Now that you are not allocaing the sprd_port[] pointers, shouldn't you
> also remove that variable entirely?

sprd_console_write() and sprd_console_setup() [1] also need sprd_port[].

So, this driver still needs to allocate the buffer for sprd_port[],
the change is using a local variable instead of allocating directly to
the global pointer.

[1] https://elixir.bootlin.com/linux/v6.5-rc1/source/drivers/tty/serial/sprd_serial.c#L1000

Thanks,
Chunyan

2023-07-25 19:34:46

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH V3 1/2] serial: sprd: Assign sprd_port after initialized to avoid wrong access

On Tue, Jul 25, 2023 at 03:49:15PM +0800, Chunyan Zhang wrote:
> On Tue, 25 Jul 2023 at 14:50, Greg Kroah-Hartman
> <[email protected]> wrote:
> >
> > On Tue, Jul 25, 2023 at 02:40:52PM +0800, Chunyan Zhang wrote:
> > > The global pointer 'sprd_port' may not zero when sprd_probe returns
> > > failure, that is a risk for sprd_port to be accessed afterward, and
> > > may lead to unexpected errors.
> > >
> > > For example:
> > >
> > > There are two UART ports, UART1 is used for console and configured in
> > > kernel command line, i.e. "console=";
> > >
> > > The UART1 probe failed and the memory allocated to sprd_port[1] was
> > > released, but sprd_port[1] was not set to NULL;
> > >
> > > In UART2 probe, the same virtual address was allocated to sprd_port[2],
> > > and UART2 probe process finally will go into sprd_console_setup() to
> > > register UART1 as console since it is configured as preferred console
> > > (filled to console_cmdline[]), but the console parameters (sprd_port[1])
> > > belong to UART2.
> > >
> > > So move the sprd_port[] assignment to where the port already initialized
> > > can avoid the above issue.
> > >
> > > Fixes: b7396a38fb28 ("tty/serial: Add Spreadtrum sc9836-uart driver support")
> > > Signed-off-by: Chunyan Zhang <[email protected]>
> > > ---
> > > V3:
> > > - Call uart_unregister_driver() only when the 'sprd_ports_num' decreases to 0;
> > > - Add calling sprd_rx_free_buf() instread of sprd_remove() under clean_up lable.
> > >
> > > V2:
> > > - Leave sprd_remove() to keep the unrelated code logic the same.
> > > ---
> > > drivers/tty/serial/sprd_serial.c | 25 +++++++++++++++++--------
> > > 1 file changed, 17 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/drivers/tty/serial/sprd_serial.c b/drivers/tty/serial/sprd_serial.c
> > > index b58f51296ace..fc1377029021 100644
> > > --- a/drivers/tty/serial/sprd_serial.c
> > > +++ b/drivers/tty/serial/sprd_serial.c
> > > @@ -1106,7 +1106,7 @@ static bool sprd_uart_is_console(struct uart_port *uport)
> > > static int sprd_clk_init(struct uart_port *uport)
> > > {
> > > struct clk *clk_uart, *clk_parent;
> > > - struct sprd_uart_port *u = sprd_port[uport->line];
> > > + struct sprd_uart_port *u = container_of(uport, struct sprd_uart_port, port);
> >
> > Now that you are not allocaing the sprd_port[] pointers, shouldn't you
> > also remove that variable entirely?
>
> sprd_console_write() and sprd_console_setup() [1] also need sprd_port[].

Why? Can't they also use the structure passed to them instead?

> So, this driver still needs to allocate the buffer for sprd_port[],
> the change is using a local variable instead of allocating directly to
> the global pointer.

Ah, I missed that you were saving the pointer off.

I think it would be better if you could just remove the static array
entirely, that's a sign of a very old driver that should be fixed up.

thanks,

greg k-h