From 3d658a837f6bc73ee7bf53105de089f5ce0aa59f Mon Sep 17 00:00:00 2001 From: Serhii Voloshynov Date: Thu, 1 Jul 2021 10:47:43 +0300 Subject: improve the code, check arguments before spi_read/spi_write calling. if the driver was built with a wrong NUM_AP value it prevents driver crash and the system stays bootable and working --- mtac_gpiob.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/mtac_gpiob.c b/mtac_gpiob.c index 6e08477..6b3a782 100644 --- a/mtac_gpiob.c +++ b/mtac_gpiob.c @@ -352,6 +352,11 @@ static ssize_t mts_attr_show_ap_din(struct kobject *kobj, struct kobj_attribute bit = BIT(channel); + if (port_index > NUM_AP) { + log_error("port %d is invalid", port); + return -ENOENT; + } + tmp = spi_readn(gpiob_spi[port_index][din], &byte, 1); if (tmp) { log_error("spi_read failed %d", tmp); @@ -391,6 +396,11 @@ static ssize_t mts_attr_store_ap_dout(struct kobject *kobj, struct kobj_attribut return -EINVAL; } + if (port_index > NUM_AP) { + log_error("port %d is invalid", port); + return -ENOENT; + } + mutex_lock(&spi_ap_dout_mutex); if (value) { @@ -429,6 +439,11 @@ static ssize_t mts_attr_show_ap_dout(struct kobject *kobj, struct kobj_attribute bit = BIT(channel); + if (port_index > NUM_AP) { + log_error("port %d is invalid", port); + return -ENOENT; + } + mutex_lock(&spi_ap_dout_mutex); value = spi_ap_dout_value[port_index] & bit ? 0 : 1; @@ -467,6 +482,11 @@ static ssize_t mts_attr_show_ap_adc(struct kobject *kobj, struct kobj_attribute } port_index = port - 1; + if (port_index > NUM_AP) { + log_error("port %d is invalid", port); + return -ENOENT; + } + /* 1st transfer to set up (5V reference, channel to read from) */ tx_data = manual_mode | ((channel << 7) & channel_mask); tx[0] = tx_data >> 8; -- cgit v1.2.3