public inbox for linux-mediatek@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats()
@ 2025-03-22 14:19 Qasim Ijaz
  2025-03-22 14:55 ` Markus Elfring
  0 siblings, 1 reply; 7+ messages in thread
From: Qasim Ijaz @ 2025-03-22 14:19 UTC (permalink / raw)
  To: nbd, lorenzo, ryder.lee, shayne.chen, sean.wang, matthias.bgg,
	angelogioacchino.delregno, chui-hao.chiu, Bo.Jiao
  Cc: linux-wireless, linux-kernel, linux-arm-kernel, linux-mediatek

Ensure phy->mib is only accessed after the null sanity check for phy
otherwise the code may trigger a potential null deref.

Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
---
 drivers/net/wireless/mediatek/mt76/mt7996/main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/main.c b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
index 88e013577c0d..19391966ee3e 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
@@ -1875,7 +1875,7 @@ void mt7996_get_et_stats(struct ieee80211_hw *hw,
 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
 	struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
 	struct mt7996_phy *phy = mt7996_vif_link_phy(&mvif->deflink);
-	struct mt76_mib_stats *mib = &phy->mib;
+	struct mt76_mib_stats *mib;
 	struct mt76_ethtool_worker_info wi = {
 		.data = data,
 		.idx = mvif->deflink.mt76.idx,
@@ -1886,6 +1886,8 @@ void mt7996_get_et_stats(struct ieee80211_hw *hw,
 	if (!phy)
 		return;
 
+	mib = &phy->mib;
+	
 	mutex_lock(&dev->mt76.mutex);
 
 	mt7996_mac_update_stats(phy);
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats()
  2025-03-22 14:19 [PATCH] wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats() Qasim Ijaz
@ 2025-03-22 14:55 ` Markus Elfring
  2025-03-22 15:54   ` Johannes Berg
  0 siblings, 1 reply; 7+ messages in thread
From: Markus Elfring @ 2025-03-22 14:55 UTC (permalink / raw)
  To: Qasim Ijaz, linux-wireless, linux-mediatek, linux-arm-kernel
  Cc: LKML, Angelo Gioacchino Del Regno, Bo Jiao, Dan Carpenter,
	Felix Fietkau, Johannes Berg, Lorenzo Bianconi, Matthias Brugger,
	Peter Chiu, Ryder Lee, Sean Wang, Shayne Chen

> Ensure phy->mib is only accessed after the null sanity check for phy
> otherwise the code may trigger a potential null deref.

* Would you like to use the term “null pointer dereference” consistently?

* Were any known source code analysis tools involved also for
  this software improvement?


…
> +++ b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
> @@ -1886,6 +1886,8 @@ void mt7996_get_et_stats(struct ieee80211_hw *hw,
>  	if (!phy)
>  		return;
>
> +	mib = &phy->mib;
> +
>  	mutex_lock(&dev->mt76.mutex);
>
>  	mt7996_mac_update_stats(phy);

I suggest to move such an assignment statement directly before the place
where this variable is used finally.

Regards,
Markus


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats()
  2025-03-22 14:55 ` Markus Elfring
@ 2025-03-22 15:54   ` Johannes Berg
  2025-03-23 11:59     ` James Dutton
  0 siblings, 1 reply; 7+ messages in thread
From: Johannes Berg @ 2025-03-22 15:54 UTC (permalink / raw)
  To: Markus Elfring, Qasim Ijaz, linux-wireless, linux-mediatek,
	linux-arm-kernel
  Cc: LKML, Angelo Gioacchino Del Regno, Bo Jiao, Dan Carpenter,
	Felix Fietkau, Lorenzo Bianconi, Matthias Brugger, Peter Chiu,
	Ryder Lee, Sean Wang, Shayne Chen

On Sat, 2025-03-22 at 15:55 +0100, Markus Elfring wrote:
> > Ensure phy->mib is only accessed after the null sanity check for phy
> > otherwise the code may trigger a potential null deref.
> 
> * Would you like to use the term “null pointer dereference” consistently?
> 
> * Were any known source code analysis tools involved also for
>   this software improvement?
> 

Do you really have to double down? STOP. You're just wasting everyone's
time.

johannes


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats()
  2025-03-22 15:54   ` Johannes Berg
@ 2025-03-23 11:59     ` James Dutton
  2025-03-24  5:50       ` Dan Carpenter
  0 siblings, 1 reply; 7+ messages in thread
From: James Dutton @ 2025-03-23 11:59 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Markus Elfring, Qasim Ijaz, linux-wireless, linux-mediatek,
	linux-arm-kernel, LKML, Angelo Gioacchino Del Regno, Bo Jiao,
	Dan Carpenter, Felix Fietkau, Lorenzo Bianconi, Matthias Brugger,
	Peter Chiu, Ryder Lee, Sean Wang, Shayne Chen

As a security side note in relation to the following patch:
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
index 66575698aef1..88e013577c0d 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
@@ -68,11 +68,13 @@ static int mt7996_start(struct ieee80211_hw *hw)

 static void mt7996_stop_phy(struct mt7996_phy *phy)
 {
-       struct mt7996_dev *dev = phy->dev;
+       struct mt7996_dev *dev;

        if (!phy || !test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
                return;

+       dev = phy->dev;
+
        cancel_delayed_work_sync(&phy->mt76->mac_work);

        mutex_lock(&dev->mt76.mutex);



Prior to that patch, the code looks like this:
static void mt7996_stop_phy(struct mt7996_phy *phy)
 {
       struct mt7996_dev *dev = phy->dev;

        if (!phy || !test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
                return;


The compiler will completely remove the !phy check entirely because of
the use above it, so it being present in the source code is completely
bogus.
If one actually needs a !phy check to be present in the compiled code,
one must arrange it as per the patch above.

The fact that the !phy check is in the source code, implies to me that
someone, in the past, thought it was necessary, but I think an opinion
could be taken that it is there to obfuscate a security vulnerability.

Kind Regards

James


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats()
  2025-03-23 11:59     ` James Dutton
@ 2025-03-24  5:50       ` Dan Carpenter
  2025-03-24  7:33         ` Markus Elfring
  0 siblings, 1 reply; 7+ messages in thread
From: Dan Carpenter @ 2025-03-24  5:50 UTC (permalink / raw)
  To: James Dutton
  Cc: Johannes Berg, Markus Elfring, Qasim Ijaz, linux-wireless,
	linux-mediatek, linux-arm-kernel, LKML,
	Angelo Gioacchino Del Regno, Bo Jiao, Felix Fietkau,
	Lorenzo Bianconi, Matthias Brugger, Peter Chiu, Ryder Lee,
	Sean Wang, Shayne Chen

On Sun, Mar 23, 2025 at 11:59:45AM +0000, James Dutton wrote:
> As a security side note in relation to the following patch:
> diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
> b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
> index 66575698aef1..88e013577c0d 100644
> --- a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
> +++ b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
> @@ -68,11 +68,13 @@ static int mt7996_start(struct ieee80211_hw *hw)
> 
>  static void mt7996_stop_phy(struct mt7996_phy *phy)
>  {
> -       struct mt7996_dev *dev = phy->dev;
> +       struct mt7996_dev *dev;
> 
>         if (!phy || !test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
>                 return;
> 
> +       dev = phy->dev;
> +
>         cancel_delayed_work_sync(&phy->mt76->mac_work);
> 
>         mutex_lock(&dev->mt76.mutex);
> 
> 
> 
> Prior to that patch, the code looks like this:
> static void mt7996_stop_phy(struct mt7996_phy *phy)
>  {
>        struct mt7996_dev *dev = phy->dev;
> 
>         if (!phy || !test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
>                 return;
> 
> 
> The compiler will completely remove the !phy check entirely because of
> the use above it, so it being present in the source code is completely
> bogus.

No, in the kernel we use the -fno-delete-null-pointer-checks so the
NULL check will always be there.

Also the "phy" point will never be NULL so the check should be removed.

regards,
dan carpenter

> If one actually needs a !phy check to be present in the compiled code,
> one must arrange it as per the patch above.
> 
> The fact that the !phy check is in the source code, implies to me that
> someone, in the past, thought it was necessary, but I think an opinion
> could be taken that it is there to obfuscate a security vulnerability.
> 
> Kind Regards
> 
> James


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats()
  2025-03-24  5:50       ` Dan Carpenter
@ 2025-03-24  7:33         ` Markus Elfring
  2025-03-24  7:43           ` Dan Carpenter
  0 siblings, 1 reply; 7+ messages in thread
From: Markus Elfring @ 2025-03-24  7:33 UTC (permalink / raw)
  To: Dan Carpenter, Qasim Ijaz, linux-wireless, linux-mediatek,
	linux-arm-kernel
  Cc: LKML, Angelo Gioacchino Del Regno, Bo Jiao, Felix Fietkau,
	James Dutton, Johannes Berg, Jonas Gorski, Lorenzo Bianconi,
	Matthias Brugger, Peter Chiu, Ryder Lee, Sean Wang, Shayne Chen

> Also the "phy" point will never be NULL so the check should be removed.
How many tools can help to determine such a software aspect with inter-procedural analyses?

Regards,
Markus


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats()
  2025-03-24  7:33         ` Markus Elfring
@ 2025-03-24  7:43           ` Dan Carpenter
  0 siblings, 0 replies; 7+ messages in thread
From: Dan Carpenter @ 2025-03-24  7:43 UTC (permalink / raw)
  To: Markus Elfring
  Cc: Qasim Ijaz, linux-wireless, linux-mediatek, linux-arm-kernel,
	LKML, Angelo Gioacchino Del Regno, Bo Jiao, Felix Fietkau,
	James Dutton, Johannes Berg, Jonas Gorski, Lorenzo Bianconi,
	Matthias Brugger, Peter Chiu, Ryder Lee, Sean Wang, Shayne Chen

On Mon, Mar 24, 2025 at 08:33:39AM +0100, Markus Elfring wrote:
> > Also the "phy" point will never be NULL so the check should be removed.
> How many tools can help to determine such a software aspect with
> inter-procedural analyses?
> 

You can just review the code.  There is only one caller.

Btw, it's fine to have unnecessary NULL checks so long as they're done
consistently.  Generally, we prefer people not add unnecessary code,
but if it makes you feel safer, most maintainers aren't going to nit-pick
you about it.  If you are doing the work then you get some say your own
code.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-03-24  7:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-22 14:19 [PATCH] wifi: mt76: mt7996: avoid potential null deref in mt7996_get_et_stats() Qasim Ijaz
2025-03-22 14:55 ` Markus Elfring
2025-03-22 15:54   ` Johannes Berg
2025-03-23 11:59     ` James Dutton
2025-03-24  5:50       ` Dan Carpenter
2025-03-24  7:33         ` Markus Elfring
2025-03-24  7:43           ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox