Skip to contentSkip to conversation

rpmsg: char: fix use-after-free on probe error path

PATCH Superseded Yuho Choi 3 participants 4 messages Apr 16 – Apr 24 Remote processor messaging (rpmsg)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 rpmsg: char: fix use-after-free on probe error path

rpmsg_chrdev_probe() stores the newly allocated eptdev in the default endpoint's priv pointer before calling rpmsg_chrdev_eptdev_add(). If rpmsg_chrdev_eptdev_add() then fails, its error path frees eptdev while the default endpoint may still dispatch inbound messages through rpmsg_ept_cb(), leaving the callback with a dangling priv pointer.

Avoid publishing eptdev through the default endpoint until rpmsg_chrdev_eptdev_add() succeeds. Any message that arrives before the priv is published should be ignored, so make rpmsg_ept_cb() return success when priv is NULL.

Fixes bc69d10665690 ("rpmsg: char: Introduce the "rpmsg-raw" channel")
---
 drivers/rpmsg/rpmsg_char.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/rpmsg/rpmsg_char.c +11 -2
index ca9cf8858a5ef..549a24b70884d 100644
@@ -11,6 +11,7 @@
  * was based on TI & Google OMX rpmsg driver.
  */
 
+#include "asm-generic/rwonce.h"
 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 
 #include <linux/cdev.h>
@@ -104,6 +105,9 @@ static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
 	struct rpmsg_eptdev *eptdev = priv;
 	struct sk_buff *skb;
 
+	if (!eptdev)
+		return 0;
+
 	skb = alloc_skb(len, GFP_ATOMIC);
 	if (!skb)
 		return -ENOMEM;
@@ -490,6 +494,7 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
 	struct rpmsg_channel_info chinfo;
 	struct rpmsg_eptdev *eptdev;
 	struct device *dev = &rpdev->dev;
+	int ret;
 
 	memcpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
 	chinfo.src = rpdev->src;
@@ -502,13 +507,17 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
 	/* Set the default_ept to the rpmsg device endpoint */
 	eptdev->default_ept = rpdev->ept;
 
+	ret = rpmsg_chrdev_eptdev_add(eptdev, chinfo);
+
+	if (ret)
+		return ret;
 	/*
 	 * The rpmsg_ept_cb uses *priv parameter to get its rpmsg_eptdev context.
-	 * Storedit in default_ept *priv field.
+	 * Stored it in default_ept *priv field.
 	 */
 	eptdev->default_ept->priv = eptdev;
 
-	return rpmsg_chrdev_eptdev_add(eptdev, chinfo);
+	return 0;
 }
 
 static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw

Good morning,

Quoted 6 lines · Yuho Choi

rpmsg_chrdev_probe() stores the newly allocated eptdev in the default endpoint's priv pointer before calling rpmsg_chrdev_eptdev_add(). If rpmsg_chrdev_eptdev_add() then fails, its error path frees eptdev while the default endpoint may still dispatch inbound messages through rpmsg_ept_cb(), leaving the callback with a dangling priv pointer.

Quoted 6 lines

Avoid publishing eptdev through the default endpoint until rpmsg_chrdev_eptdev_add() succeeds. Any message that arrives before the priv is published should be ignored, so make rpmsg_ept_cb() return success when priv is NULL.

Fixes bc69d10665690 ("rpmsg: char: Introduce the "rpmsg-raw" channel")

The SHA should be 12 characters.

Quoted 10 lines
---
 drivers/rpmsg/rpmsg_char.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/rpmsg/rpmsg_char.c +0 -0
index ca9cf8858a5ef..549a24b70884d 100644

drivers/rpmsg/rpmsg_char.c line 14

Quoted 4 lines
drivers/rpmsg/rpmsg_char.c +1 -0
@@ -11,6 +11,7 @@
  * was based on TI & Google OMX rpmsg driver.
  */
 
+#include "asm-generic/rwonce.h"

Why is this needed?

drivers/rpmsg/rpmsg_char.c static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, · line 110

Quoted 10 lines
drivers/rpmsg/rpmsg_char.c +3 -0
 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 
 #include <linux/cdev.h>
@@ -104,6 +105,9 @@ static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
 	struct rpmsg_eptdev *eptdev = priv;
 	struct sk_buff *skb;
 
+	if (!eptdev)
+		return 0;
+

If this is needed, the same check is probably needed in rpmsg_ept_flow_cb().

Thanks,
Mathieu

Quoted 34 lines
drivers/rpmsg/rpmsg_char.c +7 -2
 	skb = alloc_skb(len, GFP_ATOMIC);
 	if (!skb)
 		return -ENOMEM;
@@ -490,6 +494,7 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
 	struct rpmsg_channel_info chinfo;
 	struct rpmsg_eptdev *eptdev;
 	struct device *dev = &rpdev->dev;
+	int ret;
 
 	memcpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
 	chinfo.src = rpdev->src;
@@ -502,13 +507,17 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
 	/* Set the default_ept to the rpmsg device endpoint */
 	eptdev->default_ept = rpdev->ept;
 
+	ret = rpmsg_chrdev_eptdev_add(eptdev, chinfo);
+
+	if (ret)
+		return ret;
 	/*
 	 * The rpmsg_ept_cb uses *priv parameter to get its rpmsg_eptdev context.
-	 * Storedit in default_ept *priv field.
+	 * Stored it in default_ept *priv field.
 	 */
 	eptdev->default_ept->priv = eptdev;
 
-	return rpmsg_chrdev_eptdev_add(eptdev, chinfo);
+	return 0;
 }
 
 static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
Signature · 1 line
2.50.1 (Apple Git-155)
author
#2 lore raw

Dear Mathieu

Thanks for the review.

I will fix the Fixes tag and remove the header in v2. The check will be added in the next patch.

Thanks,
Yuho

Quoted 86 lines · Mathieu Poirier

Good morning,

Quoted 6 lines · Yuho Choi

rpmsg_chrdev_probe() stores the newly allocated eptdev in the default endpoint's priv pointer before calling rpmsg_chrdev_eptdev_add(). If rpmsg_chrdev_eptdev_add() then fails, its error path frees eptdev while the default endpoint may still dispatch inbound messages through rpmsg_ept_cb(), leaving the callback with a dangling priv pointer.

Quoted 6 lines

Avoid publishing eptdev through the default endpoint until rpmsg_chrdev_eptdev_add() succeeds. Any message that arrives before the priv is published should be ignored, so make rpmsg_ept_cb() return success when priv is NULL.

Fixes bc69d10665690 ("rpmsg: char: Introduce the "rpmsg-raw" channel")

The SHA should be 12 characters.

Quoted 10 lines
---
 drivers/rpmsg/rpmsg_char.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/rpmsg/rpmsg_char.c +0 -0
index ca9cf8858a5ef..549a24b70884d 100644

drivers/rpmsg/rpmsg_char.c line 14

Quoted 4 lines
drivers/rpmsg/rpmsg_char.c +1 -0
@@ -11,6 +11,7 @@
  * was based on TI & Google OMX rpmsg driver.
  */
 
+#include "asm-generic/rwonce.h"

Why is this needed?

drivers/rpmsg/rpmsg_char.c static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len, · line 110

Quoted 10 lines
drivers/rpmsg/rpmsg_char.c +3 -0
 #define pr_fmt(fmt)  KBUILD_MODNAME ": " fmt
 
 #include <linux/cdev.h>
@@ -104,6 +105,9 @@ static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
      struct rpmsg_eptdev *eptdev = priv;
      struct sk_buff *skb;
 
+     if (!eptdev)
+             return 0;
+

If this is needed, the same check is probably needed in rpmsg_ept_flow_cb().

Thanks,
Mathieu

Quoted 34 lines
drivers/rpmsg/rpmsg_char.c +7 -3
      skb = alloc_skb(len, GFP_ATOMIC);
      if (!skb)
              return -ENOMEM;
@@ -490,6 +494,7 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
      struct rpmsg_channel_info chinfo;
      struct rpmsg_eptdev *eptdev;
      struct device *dev = &rpdev->dev;
+     int ret;
 
      memcpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
      chinfo.src = rpdev->src;
@@ -502,13 +507,17 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
      /* Set the default_ept to the rpmsg device endpoint */
      eptdev->default_ept = rpdev->ept;
 
+     ret = rpmsg_chrdev_eptdev_add(eptdev, chinfo);
+
+     if (ret)
+             return ret;
      /*
       * The rpmsg_ept_cb uses *priv parameter to get its rpmsg_eptdev context.
-      * Storedit in default_ept *priv field.
+      * Stored it in default_ept *priv field.
       */
      eptdev->default_ept->priv = eptdev;
 
-     return rpmsg_chrdev_eptdev_add(eptdev, chinfo);
+     return 0;
 }
 
 static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
--

2.50.1 (Apple Git-155)

#3 lore raw
In reply to Yuho Choi
kernel test robot noticed the following build warnings:

Hi Yuho,

kernel test robot noticed the following build warnings:

[auto build test WARNING on remoteproc/rpmsg-next]
[also build test WARNING on linus/master v7.0 next-20260422] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yuho-Choi/rpmsg-char-fix-use-after-free-on-probe-error-path/20260421-073128
base:   https://git.kernel.org/pub/scm/linux/kernel/git/remoteproc/linux.git rpmsg-next
patch link:    https://lore.kernel.org/r/20260416042338.10146-1-dbgh9129%40gmail.com
patch subject: [PATCH v1] rpmsg: char: fix use-after-free on probe error path
config: alpha-allyesconfig (https://download.01.org/0day-ci/archive/20260422/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 15.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260422/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from include/linux/compiler.h:372,
from include/linux/build_bug.h:5, from include/linux/container_of.h:5, from include/linux/list.h:5, from include/linux/kobject.h:19, from include/linux/cdev.h:5, from drivers/rpmsg/rpmsg_char.c:17:

>> arch/alpha/include/asm/rwonce.h:23:9: warning: '__READ_ONCE' redefined
      23 | #define __READ_ONCE(x)                                                  \
         |         ^~~~~~~~~~~
   In file included from drivers/rpmsg/rpmsg_char.c:14:
   include/asm-generic/rwonce.h:44:9: note: this is the location of the previous definition
      44 | #define __READ_ONCE(x)  (*(const volatile __unqual_scalar_typeof(x) *)&(x))
         |         ^~~~~~~~~~~

vim +/__READ_ONCE +23 arch/alpha/include/asm/rwonce.h

d6462858851549c Will Deacon 2019-10-30  11  
d6462858851549c Will Deacon 2019-10-30  12  /*
d6462858851549c Will Deacon 2019-10-30  13   * Alpha is apparently daft enough to reorder address-dependent loads
d6462858851549c Will Deacon 2019-10-30  14   * on some CPU implementations. Knock some common sense into it with
d6462858851549c Will Deacon 2019-10-30  15   * a memory barrier in READ_ONCE().
d6462858851549c Will Deacon 2019-10-30  16   *
d6462858851549c Will Deacon 2019-10-30  17   * For the curious, more information about this unusual reordering is
d6462858851549c Will Deacon 2019-10-30  18   * available in chapter 15 of the "perfbook":
d6462858851549c Will Deacon 2019-10-30  19   *
d6462858851549c Will Deacon 2019-10-30  20   *  https://kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html
d6462858851549c Will Deacon 2019-10-30  21   *
d6462858851549c Will Deacon 2019-10-30  22   */
d6462858851549c Will Deacon 2019-10-30 @23  #define __READ_ONCE(x)							\
d6462858851549c Will Deacon 2019-10-30  24  ({									\
d6462858851549c Will Deacon 2019-10-30  25  	__unqual_scalar_typeof(x) __x =					\
d6462858851549c Will Deacon 2019-10-30  26  		(*(volatile typeof(__x) *)(&(x)));			\
d6462858851549c Will Deacon 2019-10-30  27  	mb();								\
d6462858851549c Will Deacon 2019-10-30  28  	(typeof(x))__x;							\
d6462858851549c Will Deacon 2019-10-30  29  })

d6462858851549c Will Deacon 2019-10-30 30

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki