Skip to contentSkip to conversation

x86/microcode/intel: Fix memleak in save_microcode_patch

PATCH In review Zhenzhong Duan 3 participants 3 messages Jun 1, 2018 – Jun 22, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/microcode/intel: Fix memleak in save_microcode_patch

Free useless ucode_patch entry when it's replaced.

---
 arch/x86/kernel/cpu/microcode/intel.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +9 -1
index 1c2cfa0..461e315 100644
@@ -150,6 +150,12 @@ static bool microcode_matches(struct microcode_header_intel *mc_header,
 	return false;
 }
 
+static void memfree_patch(struct ucode_patch *p)
+{
+	kfree(p->data);
+	kfree(p);
+}
+
 static struct ucode_patch *memdup_patch(void *data, unsigned int size)
 {
 	struct ucode_patch *p;
@@ -190,8 +196,10 @@ static void save_microcode_patch(void *data, unsigned int size)
 			p = memdup_patch(data, size);
 			if (!p)
 				pr_err("Error allocating buffer %p\n", data);
-			else
+			else {
 				list_replace(&iter->plist, &p->plist);
+				memfree_patch(iter);
+			}
 		}
 	}
 
Signature · 1 line
1.7.1
maintainer
#1 lore raw
Quoted 25 lines · Zhenzhong Duan

Free useless ucode_patch entry when it's replaced.

---
 arch/x86/kernel/cpu/microcode/intel.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +6 -0
index 1c2cfa0..461e315 100644
@@ -150,6 +150,12 @@ static bool microcode_matches(struct microcode_header_intel *mc_header,
 	return false;
 }
 
+static void memfree_patch(struct ucode_patch *p)
+{
+	kfree(p->data);
+	kfree(p);
+}
+
 static struct ucode_patch *memdup_patch(void *data, unsigned int size)
 {
 	struct ucode_patch *p;

arch/x86/kernel/cpu/microcode/intel.c static void save_microcode_patch(void *data, unsigned int size) · line 201

Quoted 7 lines
arch/x86/kernel/cpu/microcode/intel.c +2 -1
@@ -190,8 +196,10 @@ static void save_microcode_patch(void *data, unsigned int size)
 			p = memdup_patch(data, size);
 			if (!p)
 				pr_err("Error allocating buffer %p\n", data);
-			else
+			else {
 				list_replace(&iter->plist, &p->plist);
+				memfree_patch(iter);

Looks correct. I've dropped the memfree_patch() two-liner though and made it do kfree() directly.

Thx.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#2 lore raw

[tip:x86/urgent] x86/microcode/intel: Fix memleak in save_microcode_patch()

In reply to Zhenzhong Duan
x86/microcode/intel: Fix memleak in save_microcode_patch()
Commit-ID:  0218c766263e70795c5eaa17d75ed54bca350950
Gitweb:     https://git.kernel.org/tip/0218c766263e70795c5eaa17d75ed54bca350950
Author:     Zhenzhong Duan <[email protected]>
AuthorDate: Fri, 22 Jun 2018 13:51:26 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 22 Jun 2018 14:42:59 +0200

x86/microcode/intel: Fix memleak in save_microcode_patch()

Free useless ucode_patch entry when it's replaced.

[ bp: Drop the memfree_patch() two-liner. ]

---
 arch/x86/kernel/cpu/microcode/intel.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/microcode/intel.c +4 -1
index 1c2cfa0644aa..97ccf4c3b45b 100644
@@ -190,8 +190,11 @@ static void save_microcode_patch(void *data, unsigned int size)
 			p = memdup_patch(data, size);
 			if (!p)
 				pr_err("Error allocating buffer %p\n", data);
-			else
+			else {
 				list_replace(&iter->plist, &p->plist);
+				kfree(iter->data);
+				kfree(iter);
+			}
 		}
 	}