Skip to contentSkip to conversation

livepatch: Fix NULL pointer dereference in klp_find_func()

PATCH Superseded Yafang Shao 3 participants 5 messages Jun 23 – Jul 7 Live patching

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 livepatch: Fix NULL pointer dereference in klp_find_func()

A NULL old_name in a newly loaded livepatch's function entry causes a NULL pointer dereference in strcmp():

klp_init_patch()

    klp_add_nops()
      klp_find_func()
        strcmp(old_func->old_name, func->old_name)

Add a sanity check at the beginning of klp_enable_patch() to reject patches with NULL old_name before they reach this code path.

---
 kernel/livepatch/core.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
kernel/livepatch/core.c +22 -12
index 28d15ba58a26..a240d1144e89 100644
@@ -799,9 +799,6 @@ void klp_free_replaced_patches_async(struct klp_patch *new_patch)
 
 static int klp_init_func(struct klp_object *obj, struct klp_func *func)
 {
-	if (!func->old_name)
-		return -EINVAL;
-
 	/*
 	 * NOPs get the address later. The patched module must be loaded,
 	 * see klp_init_object_loaded().
@@ -1092,6 +1089,25 @@ static int __klp_enable_patch(struct klp_patch *patch)
 	return ret;
 }
 
+static int klp_check_patch(struct klp_patch *patch)
+{
+	struct klp_object *obj;
+	struct klp_func *func;
+
+	if (!patch || !patch->mod || !patch->objs)
+		return -EINVAL;
+
+	klp_for_each_object_static(patch, obj) {
+		if (!obj->funcs)
+			return -EINVAL;
+		klp_for_each_func_static(obj, func) {
+			if (!func->old_name)
+				return -EINVAL;
+		}
+	}
+	return 0;
+}
+
 /**
  * klp_enable_patch() - enable the livepatch
  * @patch:	patch to be enabled
@@ -1108,16 +1124,10 @@ static int __klp_enable_patch(struct klp_patch *patch)
 int klp_enable_patch(struct klp_patch *patch)
 {
 	int ret;
-	struct klp_object *obj;
-
-	if (!patch || !patch->mod || !patch->objs)
-		return -EINVAL;
-
-	klp_for_each_object_static(patch, obj) {
-		if (!obj->funcs)
-			return -EINVAL;
-	}
 
+	ret = klp_check_patch(patch);
+	if (ret)
+		return ret;
 
 	if (!is_livepatch_module(patch->mod)) {
 		pr_err("module %s is not marked as a livepatch module\n",
Signature · 1 line
2.52.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Yafang Shao

A NULL old_name in a newly loaded livepatch's function entry causes a NULL pointer dereference in strcmp():

klp_init_patch()

    klp_add_nops()
      klp_find_func()
        strcmp(old_func->old_name, func->old_name)

Add a sanity check at the beginning of klp_enable_patch() to reject patches with NULL old_name before they reach this code path.

The changelog could also mention the introduction of klp_check_patch().

M

#2 lore raw

PATCH v2 1/1 livepatch: Fix NULL pointer dereference in klp_find_func()

A NULL old_name in a newly loaded livepatch's function entry causes a NULL pointer dereference in strcmp():

klp_init_patch()

    klp_add_nops()
      klp_find_func()
        strcmp(old_func->old_name, func->old_name)

Add klp_check_patch() at the beginning of klp_enable_patch() to reject patches with NULL old_name before they reach this code path.

---
 kernel/livepatch/core.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
kernel/livepatch/core.c +22 -12
index 28d15ba58a26..a240d1144e89 100644
@@ -799,9 +799,6 @@ void klp_free_replaced_patches_async(struct klp_patch *new_patch)
 
 static int klp_init_func(struct klp_object *obj, struct klp_func *func)
 {
-	if (!func->old_name)
-		return -EINVAL;
-
 	/*
 	 * NOPs get the address later. The patched module must be loaded,
 	 * see klp_init_object_loaded().
@@ -1092,6 +1089,25 @@ static int __klp_enable_patch(struct klp_patch *patch)
 	return ret;
 }
 
+static int klp_check_patch(struct klp_patch *patch)
+{
+	struct klp_object *obj;
+	struct klp_func *func;
+
+	if (!patch || !patch->mod || !patch->objs)
+		return -EINVAL;
+
+	klp_for_each_object_static(patch, obj) {
+		if (!obj->funcs)
+			return -EINVAL;
+		klp_for_each_func_static(obj, func) {
+			if (!func->old_name)
+				return -EINVAL;
+		}
+	}
+	return 0;
+}
+
 /**
  * klp_enable_patch() - enable the livepatch
  * @patch:	patch to be enabled
@@ -1108,16 +1124,10 @@ static int __klp_enable_patch(struct klp_patch *patch)
 int klp_enable_patch(struct klp_patch *patch)
 {
 	int ret;
-	struct klp_object *obj;
-
-	if (!patch || !patch->mod || !patch->objs)
-		return -EINVAL;
-
-	klp_for_each_object_static(patch, obj) {
-		if (!obj->funcs)
-			return -EINVAL;
-	}
 
+	ret = klp_check_patch(patch);
+	if (ret)
+		return ret;
 
 	if (!is_livepatch_module(patch->mod)) {
 		pr_err("module %s is not marked as a livepatch module\n",
Signature · 1 line
2.52.0
maintainer Reviewed-by Tested-by on v2 1/1
#3 lore raw
Quoted 11 lines · Yafang Shao

A NULL old_name in a newly loaded livepatch's function entry causes a NULL pointer dereference in strcmp():

klp_init_patch()

    klp_add_nops()
      klp_find_func()
        strcmp(old_func->old_name, func->old_name)

Add klp_check_patch() at the beginning of klp_enable_patch() to reject patches with NULL old_name before they reach this code path.

Quoted 6 lines

Looks good to me:

I think whether to rush this into 7.2-rcX or wait for 7.3. On one hand, it is a possible security fix. On the other hand, broken module could never get loaded and pass even a minimal QA.

I tend to push it for 7.2-rcX just to avoid questions from people who ignore the real life vulnerabilty aspect.

Also see a nit below.

Quoted 7 lines
kernel/livepatch/core.c +1 -0
@@ -1092,6 +1089,25 @@ static int __klp_enable_patch(struct klp_patch *patch)
 	return ret;
 }
 
+static int klp_check_patch(struct klp_patch *patch)

kernel/livepatch/core.c static int __klp_enable_patch(struct klp_patch *patch) · line 1102

Quoted 10 lines
kernel/livepatch/core.c +10 -0
+{
+	struct klp_object *obj;
+	struct klp_func *func;
+
+	if (!patch || !patch->mod || !patch->objs)
+		return -EINVAL;
+
+	klp_for_each_object_static(patch, obj) {
+		if (!obj->funcs)
+			return -EINVAL;

Nit: I would add an empty line here to make it better readable.
Of course, this is a personal preference. But it seems that most contributors to this code prefer visual separators between misc code sections as well.

No need for v3. I could add the empty line when pushing the change.

kernel/livepatch/core.c static int __klp_enable_patch(struct klp_patch *patch) · line 1113

Quoted 11 lines
kernel/livepatch/core.c +8 -0
+		klp_for_each_func_static(obj, func) {
+			if (!func->old_name)
+				return -EINVAL;
+		}
+	}
+	return 0;
+}
+
 /**
  * klp_enable_patch() - enable the livepatch
  * @patch:	patch to be enabled

Best Regards,
Petr

maintainer
#4 lore raw
In reply to Yafang Shao
Quoted 11 lines · Yafang Shao

A NULL old_name in a newly loaded livepatch's function entry causes a NULL pointer dereference in strcmp():

klp_init_patch()

    klp_add_nops()
      klp_find_func()
        strcmp(old_func->old_name, func->old_name)

Add klp_check_patch() at the beginning of klp_enable_patch() to reject patches with NULL old_name before they reach this code path.

Quoted 6 lines

JFYI, the patch has been committed into livepatching.git, branch for-7.3/trivial.

Best Regards,
Petr