Skip to content

Commit

Permalink
drivers: can: be consistent in filter_id checks when removing rx filters
Browse files Browse the repository at this point in the history
Change the CAN controller driver implementations for the
can_remove_rx_filter() API call to be consistent in their validation of the
supplied filter_id.

Fixes: #64398

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
(cherry picked from commit 6c5400d)
  • Loading branch information
henrikbrixandersen authored and nashif committed Oct 26, 2023
1 parent 520d381 commit 156bf29
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion drivers/can/can_loopback.c
Expand Up @@ -211,7 +211,7 @@ static void can_loopback_remove_rx_filter(const struct device *dev, int filter_i
{
struct can_loopback_data *data = dev->data;

if (filter_id >= ARRAY_SIZE(data->filters)) {
if (filter_id < 0 || filter_id >= ARRAY_SIZE(data->filters)) {
LOG_ERR("filter ID %d out-of-bounds", filter_id);
return;
}
Expand Down
7 changes: 6 additions & 1 deletion drivers/can/can_mcan.c
Expand Up @@ -1110,12 +1110,17 @@ void can_mcan_remove_rx_filter(const struct device *dev, int filter_id)
struct can_mcan_data *data = dev->data;
int err;

if (filter_id < 0) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

k_mutex_lock(&data->lock, K_FOREVER);

if (filter_id >= cbs->num_std) {
filter_id -= cbs->num_std;
if (filter_id >= cbs->num_ext) {
LOG_ERR("Wrong filter id");
LOG_ERR("filter ID %d out of bounds", filter_id);
k_mutex_unlock(&data->lock);
return;
}
Expand Down
5 changes: 5 additions & 0 deletions drivers/can/can_mcp2515.c
Expand Up @@ -673,6 +673,11 @@ static void mcp2515_remove_rx_filter(const struct device *dev, int filter_id)
{
struct mcp2515_data *dev_data = dev->data;

if (filter_id < 0 || filter_id >= CONFIG_CAN_MAX_FILTER) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

k_mutex_lock(&dev_data->mutex, K_FOREVER);
dev_data->filter_usage &= ~BIT(filter_id);
k_mutex_unlock(&dev_data->mutex);
Expand Down
5 changes: 2 additions & 3 deletions drivers/can/can_mcux_flexcan.c
Expand Up @@ -921,9 +921,8 @@ static void mcux_flexcan_remove_rx_filter(const struct device *dev, int filter_i
{
struct mcux_flexcan_data *data = dev->data;

if (filter_id >= MCUX_FLEXCAN_MAX_RX) {
LOG_ERR("Detach: Filter id >= MAX_RX (%d >= %d)", filter_id,
MCUX_FLEXCAN_MAX_RX);
if (filter_id < 0 || filter_id >= MCUX_FLEXCAN_MAX_RX) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

Expand Down
1 change: 1 addition & 0 deletions drivers/can/can_native_posix_linux.c
Expand Up @@ -239,6 +239,7 @@ static void can_npl_remove_rx_filter(const struct device *dev, int filter_id)
struct can_npl_data *data = dev->data;

if (filter_id < 0 || filter_id >= ARRAY_SIZE(data->filters)) {
LOG_ERR("filter ID %d out of bounds");
return;
}

Expand Down
5 changes: 4 additions & 1 deletion drivers/can/can_nxp_s32_canxl.c
Expand Up @@ -396,7 +396,10 @@ static void can_nxp_s32_remove_rx_filter(const struct device *dev, int filter_id
struct can_nxp_s32_data *data = dev->data;
int mb_indx = ALLOC_IDX_TO_RXMB_IDX(filter_id);

__ASSERT_NO_MSG(filter_id >= 0 && filter_id < CONFIG_CAN_NXP_S32_MAX_RX);
if (filter_id < 0 || filter_id >= CONFIG_CAN_NXP_S32_MAX_RX) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

k_mutex_lock(&data->rx_mutex, K_FOREVER);

Expand Down
3 changes: 2 additions & 1 deletion drivers/can/can_rcar.c
Expand Up @@ -973,7 +973,8 @@ static void can_rcar_remove_rx_filter(const struct device *dev, int filter_id)
{
struct can_rcar_data *data = dev->data;

if (filter_id >= CONFIG_CAN_RCAR_MAX_FILTER) {
if (filter_id < 0 || filter_id >= CONFIG_CAN_RCAR_MAX_FILTER) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

Expand Down
5 changes: 4 additions & 1 deletion drivers/can/can_stm32.c
Expand Up @@ -990,7 +990,10 @@ static void can_stm32_remove_rx_filter(const struct device *dev, int filter_id)
int bank_num;
bool bank_unused;

__ASSERT_NO_MSG(filter_id >= 0 && filter_id < CAN_STM32_MAX_FILTER_ID);
if (filter_id < 0 || filter_id >= CAN_STM32_MAX_FILTER_ID) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

k_mutex_lock(&filter_mutex, K_FOREVER);
k_mutex_lock(&data->inst_mutex, K_FOREVER);
Expand Down

0 comments on commit 156bf29

Please sign in to comment.