Skip to contentSkip to conversation

livepatch: Reorder to use before freeing a pointer

PATCH Superseded [email protected] 2 participants 2 messages Mar 19, 2022 Live patching

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 livepatch: Reorder to use before freeing a pointer

From: Tom Rix <[email protected]>

Clang static analysis reports this issue livepatch-shadow-fix1.c:113:2: warning: Use of
memory after it is freed
pr_info("%s: dummy @ %p, prevented leak @ %p\n", ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The pointer is freed in the previous statement.
Reorder the pr_info to report before the free.

---
 samples/livepatch/livepatch-shadow-fix1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/livepatch/livepatch-shadow-fix1.c +1 -1
index 918ce17b43fda..6701641bf12d4 100644
@@ -109,9 +109,9 @@ static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)
 	void *d = obj;
 	int **shadow_leak = shadow_data;
 
-	kfree(*shadow_leak);
 	pr_info("%s: dummy @ %p, prevented leak @ %p\n",
 			 __func__, d, *shadow_leak);
+	kfree(*shadow_leak);
 }
 
 static void livepatch_fix1_dummy_free(struct dummy *d)
Signature · 1 line
2.26.3
#1 lore raw
Quoted 34 lines · On Sat, Mar 19, 2022 at 09:51:59AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this issue livepatch-shadow-fix1.c:113:2: warning: Use of
memory after it is freed
pr_info("%s: dummy @ %p, prevented leak @ %p\n", ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The pointer is freed in the previous statement.
Reorder the pr_info to report before the free.

---
 samples/livepatch/livepatch-shadow-fix1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/livepatch/livepatch-shadow-fix1.c +1 -1
index 918ce17b43fda..6701641bf12d4 100644
@@ -109,9 +109,9 @@ static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)
 	void *d = obj;
 	int **shadow_leak = shadow_data;
 
-	kfree(*shadow_leak);
 	pr_info("%s: dummy @ %p, prevented leak @ %p\n",
 			 __func__, d, *shadow_leak);
+	kfree(*shadow_leak);
 }
 
 static void livepatch_fix1_dummy_free(struct dummy *d)
Signature · 1 line
2.26.3

The fix looks good, though it looks like there is also a similar use-after-free in livepatch_fix2_dummy_leak_dtor() in livepatch-shadow-fix2.c. Could you please also fix that as part of this patch?

Thanks,
David