Skip to contentSkip to conversation

comedi: check for NULL get_valid_routes function pointer

PATCH In review Prabhakar Pujeri 3 participants 3 messages Jul 20, 2025 Comedi drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 comedi: check for NULL get_valid_routes function pointer

From: Super User <[email protected]>

The function get_valid_routes() calls the low-level driver's dev->get_valid_routes() callback directly without checking if it is set. If this function pointer is NULL, the kernel crashes with a NULL instruction pointer dereference.

This patch adds a check to ensure the callback is present before calling it, and returns -EINVAL if it is missing.

This prevents a kernel crash when user space invokes an ioctl that results in get_valid_routes() being called on drivers that do not implement the callback.

---
 drivers/comedi/comedi_fops.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/comedi/comedi_fops.c +5 -0
index 3383a7ce27ff..2b8cb280287b 100644
@@ -1302,6 +1302,11 @@ static int check_insn_device_config_length(struct comedi_insn *insn,
 static int get_valid_routes(struct comedi_device *dev, unsigned int *data)
 {
 	lockdep_assert_held(&dev->mutex);
+	if (!dev->get_valid_routes) {
+		dev_warn(dev->class_dev ?: dev->hw_dev,
+				"get_valid_routes() not implemented\n");
+		return -EINVAL;
+	}
 	data[1] = dev->get_valid_routes(dev, data[1], data + 2);
 	return 0;
 }
Signature · 1 line
2.50.1
#1 lore raw
Quoted 1 line · Prabhakar Pujeri

From: Super User <[email protected]>

I don't think you have your git setup properly :(

And look at the subject line, two "[PATCH]"?

Quoted 5 lines

The function get_valid_routes() calls the low-level driver's dev->get_valid_routes() callback directly without checking if it is set. If this function pointer is NULL, the kernel crashes with a NULL instruction pointer dereference.

Quoted 8 lines

This patch adds a check to ensure the callback is present before calling it, and returns -EINVAL if it is missing.

This prevents a kernel crash when user space invokes an ioctl that results in get_valid_routes() being called on drivers that do not implement the callback.

Reported-by [email protected]

Was this tested? If so, please provide that information.

Quoted 10 lines
---
 drivers/comedi/comedi_fops.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/comedi/comedi_fops.c +0 -0
index 3383a7ce27ff..2b8cb280287b 100644

drivers/comedi/comedi_fops.c static int check_insn_device_config_length(struct comedi_insn *insn, · line 1307

Quoted 6 lines
drivers/comedi/comedi_fops.c +3 -0
@@ -1302,6 +1302,11 @@ static int check_insn_device_config_length(struct comedi_insn *insn,
 static int get_valid_routes(struct comedi_device *dev, unsigned int *data)
 {
 	lockdep_assert_held(&dev->mutex);
+	if (!dev->get_valid_routes) {
+		dev_warn(dev->class_dev ?: dev->hw_dev,
+				"get_valid_routes() not implemented\n");

Why are you allowing userspace to flood the kernel log? If this is something that userspace can trigger, it needs to be handled much better.

thanks,

greg k-h

maintainer
#2 lore raw
In reply to Prabhakar Pujeri
Quoted 1 line · Prabhakar Pujeri

From: Super User <[email protected]>

I don't know why you added a "From:" line, but its details are bogus.

Quoted 11 lines

The function get_valid_routes() calls the low-level driver's dev->get_valid_routes() callback directly without checking if it is set. If this function pointer is NULL, the kernel crashes with a NULL instruction pointer dereference.

This patch adds a check to ensure the callback is present before calling it, and returns -EINVAL if it is missing.

This prevents a kernel crash when user space invokes an ioctl that results in get_valid_routes() being called on drivers that do not implement the callback.

That's a reasonable guess, but the callback shouldn't be missing. If the low-level driver does not set it, then the __comedi_device_postconfig(dev) call in "drivers.c" will set it to the address of the get_zero_valid_routes function.

Quoted 23 lines
---
  drivers/comedi/comedi_fops.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/comedi/comedi_fops.c +5 -0
index 3383a7ce27ff..2b8cb280287b 100644
@@ -1302,6 +1302,11 @@ static int check_insn_device_config_length(struct comedi_insn *insn,
  static int get_valid_routes(struct comedi_device *dev, unsigned int *data)
  {
  	lockdep_assert_held(&dev->mutex);
+	if (!dev->get_valid_routes) {
+		dev_warn(dev->class_dev ?: dev->hw_dev,
+				"get_valid_routes() not implemented\n");
+		return -EINVAL;
+	}
  	data[1] = dev->get_valid_routes(dev, data[1], data + 2);
  	return 0;
  }
Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-