net/mlx5: Fix potential NULL dereference in PTP event handling
Subsystems (4)
PATCH 1/1 net/mlx5: Fix potential NULL dereference in PTP event handling
In mlx5_ptp_pps_event(), a TODO comment correctly identified that clock->ptp can be NULL if ptp_clock_register() fails. However, the code proceeded to call ptp_clock_event() with that NULL pointer, leading to a potential kernel panic.
Fix this by adding a NULL check for clock->ptp before calling ptp_clock_event(), ensuring the driver handles failed registration gracefully.
--- drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +2 -2
@@ -1185,8 +1185,8 @@ static int mlx5_pps_event(struct notifier_block *nb, }else { ptp_event.type =PTP_CLOCK_EXTTS ; } -/* TODOL clock->ptp can be NULL if ptp_clock_register fails */ -ptp_clock_event (clock->ptp, &ptp_event); +if (clock->ptp) +ptp_clock_event (clock->ptp, &ptp_event);break ;case PTP_PF_PEROUT :if (clock->shared) {
Signature · 1 line
2.43.0
PATCH v2 1/1 net/mlx5: Fix OOB access and stack information leak in PTP event handling
In mlx5_pps_event(), several critical issues were identified during review by Sashiko:
1. The 'pin' index from the hardware event was used without bounds checking to index 'pin_config' and 'pps_info->start', leading to potential out-of-bounds memory access.
2. 'ptp_event' was not zero-initialized. Since it contains a union, assigning a timestamp partially leaves the 'ts_raw' field with uninitialized stack memory, which can leak kernel data or corrupt time sync logic in hardpps().
3. A NULL 'pin_config' could be dereferenced if initialization failed.
4. 'clock->ptp' could be NULL if ptp_clock_register() failed.
Fix these by zero-initializing the event struct, adding a bounds check against MAX_PIN_NUM, and adding appropriate NULL guards.
---
v2:
- Zero-initialize ptp_event to prevent stack information leak [Sashiko].
- Add bounds check for hardware pin index to prevent OOB access [Sashiko].
- Add NULL guard for pin_config to handle initialization failures [Sashiko].
- Add NULL check for clock->ptp as originally intended.
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +9 -3
@@ -1164,12 +1164,18 @@ static int mlx5_pps_event(struct notifier_block *nb, pps_nb);struct mlx5_core_dev *mdev = clock_state->mdev;struct mlx5_clock *clock = mdev->clock; -struct ptp_clock_event ptp_event; +struct ptp_clock_event ptp_event = {};struct mlx5_eqe *eqe = data;int pin = eqe->data.pps.pin;unsigned long flags;u64 ns; +if (!clock->ptp_info.pin_config) +return NOTIFY_OK ; + +if (pin <0 || pin >=MAX_PIN_NUM ) +return NOTIFY_OK ; +switch (clock->ptp_info.pin_config[pin].func) {case PTP_PF_EXTTS : ptp_event.index = pin;
@@ -1185,8 +1191,8 @@ static int mlx5_pps_event(struct notifier_block *nb, }else { ptp_event.type =PTP_CLOCK_EXTTS ; } -/* TODOL clock->ptp can be NULL if ptp_clock_register fails */ -ptp_clock_event (clock->ptp, &ptp_event); +if (clock->ptp) +ptp_clock_event (clock->ptp, &ptp_event);break ;case PTP_PF_PEROUT :if (clock->shared) {
Signature · 1 line
2.43.0
Re: [PATCH v2] net/mlx5: Fix OOB access and stack information leak in PTP event handling
Hi Prathamesh, thanks for the pacth!
Quoted 39 lines · Prathamesh Deshpande
In
mlx5_pps_event(), several critical issues were identified during review by Sashiko:1. The 'pin' index from the hardware event was used without bounds checking to index '
pin_config' and 'pps_info->start', leading to potential out-of-bounds memory access.
2. 'ptp_event' was not zero-initialized. Since it contains a union, assigning a timestamp partially leaves the 'ts_raw' field with uninitialized stack memory, which can leak kernel data or corrupt time sync logic inhardpps().
3. A NULL 'pin_config' could be dereferenced if initialization failed.
4. 'clock->ptp' could be NULL ifptp_clock_register()failed.Fix these by zero-initializing the event struct, adding a bounds check against
MAX_PIN_NUM, and adding appropriate NULL guards.Fixes 7c39afb394c7 ("net/mlx5: PTP code migration to driver core section")Signed-off-by Prathamesh Deshpande <[email protected]>---
v2:
- Zero-initializeptp_eventto prevent stack information leak [Sashiko].
- Add bounds check for hardware pin index to prevent OOB access [Sashiko].
- Add NULL guard forpin_configto handle initialization failures [Sashiko].
- Add NULL check forclock->ptpas originally intended.drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +1
-1@@ -1164,12 +1164,18 @@ static int mlx5_pps_event(struct notifier_block *nb, pps_nb);struct mlx5_core_dev *mdev = clock_state->mdev;struct mlx5_clock *clock = mdev->clock; -struct ptp_clock_event ptp_event; +struct ptp_clock_event ptp_event = {};
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c static int mlx5_pps_event(struct notifier_block *nb, · line 1177
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +5
-0struct mlx5_eqe *eqe = data;int pin = eqe->data.pps.pin;unsigned long flags;u64 ns; +if (!clock->ptp_info.pin_config) +return NOTIFY_OK ; + +if (pin <0 || pin >=MAX_PIN_NUM ) +return NOTIFY_OK ;
pin is defined as u8 in struct mlx5_eqe_pps, so pin < 0 is dead code.
As for the upper bound: in order to receive a PPS event on a pin, the user must first configure it via mlx5_ptp_enable, which already validates the index (rq->extts.index >= clock->ptp_info.n_pins returns -EINVAL) and since the mtpps register only defines capabilities for 8 pins, so n_pins cannot exceed MAX_PIN_NUM.
Maybe wrap it with WARN_ON_ONCE instead of silently returning, so if future hardware adds support for more pins we would notice rather than silently dropping events.
Quoted 15 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +3
-2+switch (clock->ptp_info.pin_config[pin].func) {case PTP_PF_EXTTS : ptp_event.index = pin;@@ -1185,8 +1191,8 @@ static int mlx5_pps_event(struct notifier_block *nb, }else { ptp_event.type =PTP_CLOCK_EXTTS ; } -/* TODOL clock->ptp can be NULL if ptp_clock_register fails */ -ptp_clock_event (clock->ptp, &ptp_event); +if (clock->ptp) +ptp_clock_event (clock->ptp, &ptp_event);break ;case PTP_PF_PEROUT :if (clock->shared) {
Re: [PATCH v2] net/mlx5: Fix OOB access and stack information leak in PTP event handling
In reply to Prathamesh DeshpandeHi Prathamesh, thanks for the patch!
Quoted 39 lines · Prathamesh Deshpande
In
mlx5_pps_event(), several critical issues were identified during review by Sashiko:1. The 'pin' index from the hardware event was used without bounds checking to index '
pin_config' and 'pps_info->start', leading to potential out-of-bounds memory access.
2. 'ptp_event' was not zero-initialized. Since it contains a union, assigning a timestamp partially leaves the 'ts_raw' field with uninitialized stack memory, which can leak kernel data or corrupt time sync logic inhardpps().
3. A NULL 'pin_config' could be dereferenced if initialization failed.
4. 'clock->ptp' could be NULL ifptp_clock_register()failed.Fix these by zero-initializing the event struct, adding a bounds check against
MAX_PIN_NUM, and adding appropriate NULL guards.Fixes 7c39afb394c7 ("net/mlx5: PTP code migration to driver core section")Signed-off-by Prathamesh Deshpande <[email protected]>---
v2:
- Zero-initializeptp_eventto prevent stack information leak [Sashiko].
- Add bounds check for hardware pin index to prevent OOB access [Sashiko].
- Add NULL guard forpin_configto handle initialization failures [Sashiko].
- Add NULL check forclock->ptpas originally intended.drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +1
-1@@ -1164,12 +1164,18 @@ static int mlx5_pps_event(struct notifier_block *nb, pps_nb);struct mlx5_core_dev *mdev = clock_state->mdev;struct mlx5_clock *clock = mdev->clock; -struct ptp_clock_event ptp_event; +struct ptp_clock_event ptp_event = {};
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c static int mlx5_pps_event(struct notifier_block *nb, · line 1177
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +5
-0struct mlx5_eqe *eqe = data;int pin = eqe->data.pps.pin;unsigned long flags;u64 ns; +if (!clock->ptp_info.pin_config) +return NOTIFY_OK ; + +if (pin <0 || pin >=MAX_PIN_NUM ) +return NOTIFY_OK ;
pin is defined as u8 in struct mlx5_eqe_pps, so pin < 0 is dead code.
As for the upper bound: in order to receive a PPS event on a pin, the user must first configure it via mlx5_ptp_enable, which already validates the index (rq->extts.index >= clock->ptp_info.n_pins returns -EINVAL) and since the mtpps register only defines capabilities for 8 pins, so n_pins cannot exceed MAX_PIN_NUM.
Maybe wrap it with WARN_ON_ONCE instead of silently returning, so if future hardware adds support for more pins we would notice rather than silently dropping events.
Quoted 15 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +3
-2+switch (clock->ptp_info.pin_config[pin].func) {case PTP_PF_EXTTS : ptp_event.index = pin;@@ -1185,8 +1191,8 @@ static int mlx5_pps_event(struct notifier_block *nb, }else { ptp_event.type =PTP_CLOCK_EXTTS ; } -/* TODOL clock->ptp can be NULL if ptp_clock_register fails */ -ptp_clock_event (clock->ptp, &ptp_event); +if (clock->ptp) +ptp_clock_event (clock->ptp, &ptp_event);break ;case PTP_PF_PEROUT :if (clock->shared) {
Re: [PATCH v2] net/mlx5: Fix OOB access and stack information leak in PTP event handling
In reply to Prathamesh DeshpandeHi Prathamesh, thanks for the patch!
Quoted 39 lines · Prathamesh Deshpande
In
mlx5_pps_event(), several critical issues were identified during review by Sashiko:1. The 'pin' index from the hardware event was used without bounds checking to index '
pin_config' and 'pps_info->start', leading to potential out-of-bounds memory access.
2. 'ptp_event' was not zero-initialized. Since it contains a union, assigning a timestamp partially leaves the 'ts_raw' field with uninitialized stack memory, which can leak kernel data or corrupt time sync logic inhardpps().
3. A NULL 'pin_config' could be dereferenced if initialization failed.
4. 'clock->ptp' could be NULL ifptp_clock_register()failed.Fix these by zero-initializing the event struct, adding a bounds check against
MAX_PIN_NUM, and adding appropriate NULL guards.Fixes 7c39afb394c7 ("net/mlx5: PTP code migration to driver core section")Signed-off-by Prathamesh Deshpande <[email protected]>---
v2:
- Zero-initializeptp_eventto prevent stack information leak [Sashiko].
- Add bounds check for hardware pin index to prevent OOB access [Sashiko].
- Add NULL guard forpin_configto handle initialization failures [Sashiko].
- Add NULL check forclock->ptpas originally intended.drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +1
-1@@ -1164,12 +1164,18 @@ static int mlx5_pps_event(struct notifier_block *nb, pps_nb);struct mlx5_core_dev *mdev = clock_state->mdev;struct mlx5_clock *clock = mdev->clock; -struct ptp_clock_event ptp_event; +struct ptp_clock_event ptp_event = {};
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c static int mlx5_pps_event(struct notifier_block *nb, · line 1177
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +5
-0struct mlx5_eqe *eqe = data;int pin = eqe->data.pps.pin;unsigned long flags;u64 ns; +if (!clock->ptp_info.pin_config) +return NOTIFY_OK ; + +if (pin <0 || pin >=MAX_PIN_NUM ) +return NOTIFY_OK ;
pin is defined as u8 in struct mlx5_eqe_pps, so pin < 0 is dead code.
As for the upper bound: in order to receive a PPS event on a pin, the user must first configure it via mlx5_ptp_enable, which already validates the index (rq->extts.index >= clock->ptp_info.n_pins returns -EINVAL) and since the mtpps register only defines capabilities for 8 pins, so n_pins cannot exceed MAX_PIN_NUM.
Maybe wrap it with WARN_ON_ONCE instead of silently returning, so if future hardware adds support for more pins we would notice rather than silently dropping events.
Quoted 15 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +3
-2+switch (clock->ptp_info.pin_config[pin].func) {case PTP_PF_EXTTS : ptp_event.index = pin;@@ -1185,8 +1191,8 @@ static int mlx5_pps_event(struct notifier_block *nb, }else { ptp_event.type =PTP_CLOCK_EXTTS ; } -/* TODOL clock->ptp can be NULL if ptp_clock_register fails */ -ptp_clock_event (clock->ptp, &ptp_event); +if (clock->ptp) +ptp_clock_event (clock->ptp, &ptp_event);break ;case PTP_PF_PEROUT :if (clock->shared) {
Re: [PATCH v2] net/mlx5: Fix OOB access and stack information leak in PTP event handling
In reply to Prathamesh DeshpandeHi Prathamesh, thanks for the patch!
Quoted 39 lines · Prathamesh Deshpande
In
mlx5_pps_event(), several critical issues were identified during review by Sashiko:1. The 'pin' index from the hardware event was used without bounds checking to index '
pin_config' and 'pps_info->start', leading to potential out-of-bounds memory access.
2. 'ptp_event' was not zero-initialized. Since it contains a union, assigning a timestamp partially leaves the 'ts_raw' field with uninitialized stack memory, which can leak kernel data or corrupt time sync logic inhardpps().
3. A NULL 'pin_config' could be dereferenced if initialization failed.
4. 'clock->ptp' could be NULL ifptp_clock_register()failed.Fix these by zero-initializing the event struct, adding a bounds check against
MAX_PIN_NUM, and adding appropriate NULL guards.Fixes 7c39afb394c7 ("net/mlx5: PTP code migration to driver core section")Signed-off-by Prathamesh Deshpande <[email protected]>---
v2:
- Zero-initializeptp_eventto prevent stack information leak [Sashiko].
- Add bounds check for hardware pin index to prevent OOB access [Sashiko].
- Add NULL guard forpin_configto handle initialization failures [Sashiko].
- Add NULL check forclock->ptpas originally intended.drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +1
-1@@ -1164,12 +1164,18 @@ static int mlx5_pps_event(struct notifier_block *nb, pps_nb);struct mlx5_core_dev *mdev = clock_state->mdev;struct mlx5_clock *clock = mdev->clock; -struct ptp_clock_event ptp_event; +struct ptp_clock_event ptp_event = {};
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c static int mlx5_pps_event(struct notifier_block *nb, · line 1177
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +5
-0struct mlx5_eqe *eqe = data;int pin = eqe->data.pps.pin;unsigned long flags;u64 ns; +if (!clock->ptp_info.pin_config) +return NOTIFY_OK ; + +if (pin <0 || pin >=MAX_PIN_NUM ) +return NOTIFY_OK ;
pin is defined as u8 in struct mlx5_eqe_pps, so pin < 0 is dead code.
As for the upper bound: in order to receive a PPS event on a pin, the user must first configure it via mlx5_ptp_enable, which already validates the index (rq->extts.index >= clock->ptp_info.n_pins returns -EINVAL) and since the mtpps register only defines capabilities for 8 pins, so n_pins cannot exceed MAX_PIN_NUM.
Maybe wrap it with WARN_ON_ONCE instead of silently returning, so if future hardware adds support for more pins we would notice rather than silently dropping events.
Quoted 15 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +3
-2+switch (clock->ptp_info.pin_config[pin].func) {case PTP_PF_EXTTS : ptp_event.index = pin;@@ -1185,8 +1191,8 @@ static int mlx5_pps_event(struct notifier_block *nb, }else { ptp_event.type =PTP_CLOCK_EXTTS ; } -/* TODOL clock->ptp can be NULL if ptp_clock_register fails */ -ptp_clock_event (clock->ptp, &ptp_event); +if (clock->ptp) +ptp_clock_event (clock->ptp, &ptp_event);break ;case PTP_PF_PEROUT :if (clock->shared) {
Re: [PATCH v2] net/mlx5: Fix OOB access and stack information leak in PTP event handling
In reply to Prathamesh DeshpandeHi Prathamesh, thanks for the patch!
Quoted 39 lines · Prathamesh Deshpande
In
mlx5_pps_event(), several critical issues were identified during review by Sashiko:1. The 'pin' index from the hardware event was used without bounds checking to index '
pin_config' and 'pps_info->start', leading to potential out-of-bounds memory access.
2. 'ptp_event' was not zero-initialized. Since it contains a union, assigning a timestamp partially leaves the 'ts_raw' field with uninitialized stack memory, which can leak kernel data or corrupt time sync logic inhardpps().
3. A NULL 'pin_config' could be dereferenced if initialization failed.
4. 'clock->ptp' could be NULL ifptp_clock_register()failed.Fix these by zero-initializing the event struct, adding a bounds check against
MAX_PIN_NUM, and adding appropriate NULL guards.Fixes 7c39afb394c7 ("net/mlx5: PTP code migration to driver core section")Signed-off-by Prathamesh Deshpande <[email protected]>---
v2:
- Zero-initializeptp_eventto prevent stack information leak [Sashiko].
- Add bounds check for hardware pin index to prevent OOB access [Sashiko].
- Add NULL guard forpin_configto handle initialization failures [Sashiko].
- Add NULL check forclock->ptpas originally intended.drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +1
-1@@ -1164,12 +1164,18 @@ static int mlx5_pps_event(struct notifier_block *nb, pps_nb);struct mlx5_core_dev *mdev = clock_state->mdev;struct mlx5_clock *clock = mdev->clock; -struct ptp_clock_event ptp_event; +struct ptp_clock_event ptp_event = {};
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c static int mlx5_pps_event(struct notifier_block *nb, · line 1177
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +5
-0struct mlx5_eqe *eqe = data;int pin = eqe->data.pps.pin;unsigned long flags;u64 ns; +if (!clock->ptp_info.pin_config) +return NOTIFY_OK ; + +if (pin <0 || pin >=MAX_PIN_NUM ) +return NOTIFY_OK ;
pin is defined as u8 in struct mlx5_eqe_pps, so pin < 0 is dead code.
As for the upper bound: in order to receive a PPS event on a pin, the user must first configure it via mlx5_ptp_enable, which already validates the index (rq->extts.index >= clock->ptp_info.n_pins returns -EINVAL) and since the mtpps register only defines capabilities for 8 pins, so n_pins cannot exceed MAX_PIN_NUM.
Maybe wrap it with WARN_ON_ONCE instead of silently returning, so if future hardware adds support for more pins we would notice rather than silently dropping events.
Quoted 15 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +3
-2+switch (clock->ptp_info.pin_config[pin].func) {case PTP_PF_EXTTS : ptp_event.index = pin;@@ -1185,8 +1191,8 @@ static int mlx5_pps_event(struct notifier_block *nb, }else { ptp_event.type =PTP_CLOCK_EXTTS ; } -/* TODOL clock->ptp can be NULL if ptp_clock_register fails */ -ptp_clock_event (clock->ptp, &ptp_event); +if (clock->ptp) +ptp_clock_event (clock->ptp, &ptp_event);break ;case PTP_PF_PEROUT :if (clock->shared) {
Re: [PATCH v2] net/mlx5: Fix OOB access and stack information leak in
Quoted 11 lines · Carolina Jubran
pin is defined as u8 in struct
mlx5_eqe_pps, so pin < 0 is dead code.As for the upper bound: in order to receive a PPS event on a pin, the user must first configure it via
mlx5_ptp_enable, which already validates the index (rq->extts.index>=clock->ptp_info.n_pinsreturns-EINVAL) and since the mtpps register only defines capabilities for 8 pins, son_pinscannot exceedMAX_PIN_NUM.Maybe wrap it with
WARN_ON_ONCEinstead of silently returning, so if future hardware adds support for more pins we would notice rather than silently dropping events.
Hi Carolina,
Thanks for the feedback. I've removed the redundant pin < 0 check and implemented the WARN_ON_ONCE for the upper bound as suggested.
I just submitted a v3 as a fresh thread with these changes and a fix for the union corruption bug.
Thanks,
Prathamesh