Skip to contentSkip to conversation

ep93xx: clock: Fix UAF in ep93xx_clk_register_gate()

PATCH Superseded Alexander Sverdlin 2 participants 4 messages Jan 20, 2022 – Jan 25, 2022

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alexander Sverdlin

author maintainer
#0 lore raw

PATCH 1/1 ep93xx: clock: Fix UAF in ep93xx_clk_register_gate()

arch/arm/mach-ep93xx/clock.c:151:2: note: Taking true branch if (IS_ERR(clk))
^
arch/arm/mach-ep93xx/clock.c:152:3: note: Memory is released kfree(psc);
^~~~~~~~~~
arch/arm/mach-ep93xx/clock.c:154:2: note: Use of memory after it is freed return &psc->hw;
^ ~~~~~~~~

---
 arch/arm/mach-ep93xx/clock.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm/mach-ep93xx/clock.c +3 -1
index cc75087134d3..4aee14f18123 100644
@@ -148,8 +148,10 @@ static struct clk_hw *ep93xx_clk_register_gate(const char *name,
 	psc->lock = &clk_lock;
 
 	clk = clk_register(NULL, &psc->hw);
-	if (IS_ERR(clk))
+	if (IS_ERR(clk)) {
 		kfree(psc);
+		return (void *)clk;
+	}
 
 	return &psc->hw;
 }
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/1 ep93xx: clock: Don't use plain integer as NULL pointer

Fix sparse warning:
arch/arm/mach-ep93xx/clock.c:210:35: sparse: sparse: Using plain integer as NULL pointer

---
 arch/arm/mach-ep93xx/clock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mach-ep93xx/clock.c +1 -1
index 4aee14f18123..71c54da0c1e1 100644
@@ -209,7 +209,7 @@ static int ep93xx_mux_determine_rate(struct clk_hw *hw,
 				struct clk_rate_request *req)
 {
 	unsigned long rate = req->rate;
-	struct clk *best_parent = 0;
+	struct clk *best_parent = NULL;
 	unsigned long __parent_rate;
 	unsigned long best_rate = 0, actual_rate, mclk_rate;
 	unsigned long best_parent_rate;
Signature · 1 line
2.34.1
#2 lore raw
In reply to Alexander Sverdlin
Quoted 24 lines · Alexander Sverdlin

arch/arm/mach-ep93xx/clock.c:151:2: note: Taking true branch if (IS_ERR(clk))
^
arch/arm/mach-ep93xx/clock.c:152:3: note: Memory is released kfree(psc);
^~~~~~~~~~
arch/arm/mach-ep93xx/clock.c:154:2: note: Use of memory after it is freed return &psc->hw;
^ ~~~~~~~~

---
 arch/arm/mach-ep93xx/clock.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm/mach-ep93xx/clock.c +0 -0
index cc75087134d3..4aee14f18123 100644

arch/arm/mach-ep93xx/clock.c static struct clk_hw *ep93xx_clk_register_gate(const char *name, · line 153

Quoted 7 lines
arch/arm/mach-ep93xx/clock.c +2 -1
@@ -148,8 +148,10 @@ static struct clk_hw *ep93xx_clk_register_gate(const char *name,
 	psc->lock = &clk_lock;
 
 	clk = clk_register(NULL, &psc->hw);
-	if (IS_ERR(clk))
+	if (IS_ERR(clk)) {
 		kfree(psc);
+		return (void *)clk;

Prefer ERR_CAST to the raw cast. I think that's nicer when we're already using the IS_ERR macros.

Quoted 7 lines
arch/arm/mach-ep93xx/clock.c +1 -0
+	}
 
 	return &psc->hw;
 }
Signature · 1 line
2.34.1
#3 lore raw
Quoted 2 lines · Nick Desaulniers

On Thu, Jan 20, 2022 at 02:37:38PM +0100, Alexander Sverdlin wrote:

Also, consider adding the first line of the warning to your commit message, please:
arch/arm/mach-ep93xx/clock.c:154:2: warning: Use of memory after it is freed [clang-analyzer-unix.Malloc]

Quoted 42 lines
Quoted 24 lines

arch/arm/mach-ep93xx/clock.c:151:2: note: Taking true branch if (IS_ERR(clk))
^
arch/arm/mach-ep93xx/clock.c:152:3: note: Memory is released kfree(psc);
^~~~~~~~~~
arch/arm/mach-ep93xx/clock.c:154:2: note: Use of memory after it is freed return &psc->hw;
^ ~~~~~~~~

---
 arch/arm/mach-ep93xx/clock.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm/mach-ep93xx/clock.c +0 -0
index cc75087134d3..4aee14f18123 100644

arch/arm/mach-ep93xx/clock.c static struct clk_hw *ep93xx_clk_register_gate(const char *name, · line 153

Quoted 7 lines
arch/arm/mach-ep93xx/clock.c +2 -1
@@ -148,8 +148,10 @@ static struct clk_hw *ep93xx_clk_register_gate(const char *name,
      psc->lock = &clk_lock;
 
      clk = clk_register(NULL, &psc->hw);
-     if (IS_ERR(clk))
+     if (IS_ERR(clk)) {
              kfree(psc);
+             return (void *)clk;

Prefer ERR_CAST to the raw cast. I think that's nicer when we're already using the IS_ERR macros.

Quoted 7 lines
arch/arm/mach-ep93xx/clock.c +1 -1
+     }
 
      return &psc->hw;
 }
--

2.34.1

Signature · 2 lines
Thanks,
~Nick Desaulniers