Skip to contentSkip to conversation

serial: core: check if uart_get_info succeeds before using

PATCH In review Tom Rix 4 participants 4 messages May 29, 2022 – Jun 7, 2022 TTY layer and serial drivers

lore
Share & tools
lore

Tom Rix

author
#0 lore raw

PATCH 1/1 serial: core: check if uart_get_info succeeds before using

clang static analysis reports this representative issue
drivers/tty/serial/serial_core.c:2818:9: warning: 3rd function call argument is an uninitialized value [core.CallAndMessage]

        return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

uart_get_info() is used the *show() functions. When uart_get_info() fails, what is reported is garbage. So check if uart_get_info() succeeded.

Fixes 4047b37122d1 ("serial: core: Prevent unsafe uart port access, part 1")
---
 drivers/tty/serial/serial_core.c | 52 ++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 13 deletions(-)
drivers/tty/serial/serial_core.c +39 -13
index 9a85b41caa0a..4160f6711c5d 100644
@@ -2690,7 +2690,9 @@ static ssize_t uartclk_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.baud_base * 16);
 }
 
@@ -2700,7 +2702,9 @@ static ssize_t type_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.type);
 }
 
@@ -2710,7 +2714,9 @@ static ssize_t line_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.line);
 }
 
@@ -2721,7 +2727,9 @@ static ssize_t port_show(struct device *dev,
 	struct tty_port *port = dev_get_drvdata(dev);
 	unsigned long ioaddr;
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	ioaddr = tmp.port;
 	if (HIGH_BITS_OFFSET)
 		ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
@@ -2734,7 +2742,9 @@ static ssize_t irq_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.irq);
 }
 
@@ -2744,7 +2754,9 @@ static ssize_t flags_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "0x%X\n", tmp.flags);
 }
 
@@ -2754,7 +2766,9 @@ static ssize_t xmit_fifo_size_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
 }
 
@@ -2764,7 +2778,9 @@ static ssize_t close_delay_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.close_delay);
 }
 
@@ -2774,7 +2790,9 @@ static ssize_t closing_wait_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.closing_wait);
 }
 
@@ -2784,7 +2802,9 @@ static ssize_t custom_divisor_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.custom_divisor);
 }
 
@@ -2794,7 +2814,9 @@ static ssize_t io_type_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.io_type);
 }
 
@@ -2804,7 +2826,9 @@ static ssize_t iomem_base_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
 }
 
@@ -2814,7 +2838,9 @@ static ssize_t iomem_reg_shift_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;
+
 	return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
 }
 
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Tom Rix

clang static analysis reports this representative issue
drivers/tty/serial/serial_core.c:2818:9: warning: 3rd function call argument is an uninitialized value [core.CallAndMessage]

        return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

uart_get_info() is used the *show() functions. When uart_get_info() fails, what is reported is garbage. So check if uart_get_info() succeeded.

Hi Tom,
Thanks for the patch. What do you think about throwing __must_check on the definition of uart_get_info with a comment that members of the retinfo param will not be initialized if the return value is not zero?

Otherwise, patch LGTM.

Quoted 157 lines
Fixes 4047b37122d1 ("serial: core: Prevent unsafe uart port access, part 1")
---
 drivers/tty/serial/serial_core.c | 52 ++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 13 deletions(-)
drivers/tty/serial/serial_core.c +39 -14
index 9a85b41caa0a..4160f6711c5d 100644
@@ -2690,7 +2690,9 @@ static ssize_t uartclk_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.baud_base * 16);
 }
 
@@ -2700,7 +2702,9 @@ static ssize_t type_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.type);
 }
 
@@ -2710,7 +2714,9 @@ static ssize_t line_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.line);
 }
 
@@ -2721,7 +2727,9 @@ static ssize_t port_show(struct device *dev,
        struct tty_port *port = dev_get_drvdata(dev);
        unsigned long ioaddr;
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        ioaddr = tmp.port;
        if (HIGH_BITS_OFFSET)
                ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
@@ -2734,7 +2742,9 @@ static ssize_t irq_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.irq);
 }
 
@@ -2744,7 +2754,9 @@ static ssize_t flags_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "0x%X\n", tmp.flags);
 }
 
@@ -2754,7 +2766,9 @@ static ssize_t xmit_fifo_size_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
 }
 
@@ -2764,7 +2778,9 @@ static ssize_t close_delay_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.close_delay);
 }
 
@@ -2774,7 +2790,9 @@ static ssize_t closing_wait_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.closing_wait);
 }
 
@@ -2784,7 +2802,9 @@ static ssize_t custom_divisor_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.custom_divisor);
 }
 
@@ -2794,7 +2814,9 @@ static ssize_t io_type_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.io_type);
 }
 
@@ -2804,7 +2826,9 @@ static ssize_t iomem_base_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
 }
 
@@ -2814,7 +2838,9 @@ static ssize_t iomem_reg_shift_show(struct device *dev,
        struct serial_struct tmp;
        struct tty_port *port = dev_get_drvdata(dev);
 
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;
+
        return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
 }
 
--

2.27.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
In reply to Tom Rix
Quoted 7 lines · Tom Rix

clang static analysis reports this representative issue
drivers/tty/serial/serial_core.c:2818:9: warning: 3rd function call argument is an uninitialized value [core.CallAndMessage]

        return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

uart_get_info() is used the *show() functions. When uart_get_info() fails, what is reported

in the ?

Quoted 1 line

is garbage. So check if uart_get_info() succeeded.

...

drivers/tty/serial/serial_core.c static ssize_t uartclk_show(struct device *dev, · line 2694

Quoted 3 lines
drivers/tty/serial/serial_core.c +2 -1
-       uart_get_info(port, &tmp);
+       if (uart_get_info(port, &tmp))
+               return 0;

I don't think this is correct. If something fails we need to inform the caller.

I think more about

int ret;

ret = uart_get_info(...);
if (ret)
return ret;

But I haven't looked at the uart_get_info() implementation, so the above might be wrong.

Quoted 1 line
        return sprintf(buf, "%d\n", tmp.baud_base * 16);

Ditto for the rest.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw
In reply to Tom Rix
Quoted 19 lines · Tom Rix

clang static analysis reports this representative issue
drivers/tty/serial/serial_core.c:2818:9: warning: 3rd function call argument is an uninitialized value [core.CallAndMessage]

        return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

uart_get_info() is used the *show() functions. When uart_get_info() fails, what is reported is garbage. So check if uart_get_info() succeeded.

Fixes 4047b37122d1 ("serial: core: Prevent unsafe uart port access, part 1")
---
 drivers/tty/serial/serial_core.c | 52 ++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 13 deletions(-)
drivers/tty/serial/serial_core.c +0 -0
index 9a85b41caa0a..4160f6711c5d 100644

drivers/tty/serial/serial_core.c static ssize_t uartclk_show(struct device *dev, · line 2694

Quoted 6 lines
drivers/tty/serial/serial_core.c +2 -1
@@ -2690,7 +2690,9 @@ static ssize_t uartclk_show(struct device *dev,
 	struct serial_struct tmp;
 	struct tty_port *port = dev_get_drvdata(dev);
 
-	uart_get_info(port, &tmp);
+	if (uart_get_info(port, &tmp))
+		return 0;

As Andy pointed out, this is an error, don't tell userspace that all went well and yet you returned no data? That will just confuse it.

thanks,

greg k-h