Skip to contentSkip to conversation

rtcan_module: Fix tx_timeout buffer size inconsistency

PATCH In review Paul Janzen 3 participants 8 messages Mar 17, 2013 – Apr 16, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 rtcan_module: Fix tx_timeout buffer size inconsistency

When formatting /proc/rtcan/sockets, tx_timeout is defined as char[16], but a length of 20 is passed to rtcan_get_timeout_name. If tx_timeout for any socket is infinite, this causes snprintf to zero outside the buffer, leading to stack overflow and a kernel panic. Make the tx_timeout buffer 20 bytes long instead.

---
 ksrc/drivers/can/rtcan_module.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
ksrc/drivers/can/rtcan_module.c +1 -1
index 3ebc333..f270f65 100644
@@ -165,7 +165,7 @@ static int rtcan_read_proc_sockets(char *buf, char **start, off_t offset,
     struct rtdm_dev_context *context;
     struct rtcan_device *dev;
     char name[IFNAMSIZ] = "not-bound";
-    char rx_timeout[20], tx_timeout[16];
+    char rx_timeout[20], tx_timeout[20];
     rtdm_lockctx_t lock_ctx;
     int ifindex;
     RTCAN_PROC_PRINT_VARS(120);
Signature · 1 line
1.7.6.5
#1 lore raw

PATCH 2/2 rtcan_module: Improve buffer size handling for /proc/rtcan entries

---
 ksrc/drivers/can/rtcan_module.c |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)
ksrc/drivers/can/rtcan_module.c +17 -9
index f270f65..6301f63 100644
@@ -116,7 +116,7 @@ static void rtcan_get_timeout_name(nanosecs_rel_t timeout,
     if (timeout == RTDM_TIMEOUT_INFINITE)
 	strncpy(name, "infinite", max_len);
     else
-	sprintf(name, "%lld", (long long)timeout);
+	snprintf(name, max_len, "%lld", (long long)timeout);
 }
 
 static int rtcan_read_proc_devices(char *buf, char **start, off_t offset,
@@ -141,8 +141,10 @@ static int rtcan_read_proc_devices(char *buf, char **start, off_t offset,
 
     for (i = 1; i <= RTCAN_MAX_DEVICES; i++) {
 	if ((dev = rtcan_dev_get_by_index(i)) != NULL) {
-	    rtcan_dev_get_state_name(dev->state, state_name, 20);
-	    rtcan_dev_get_baudrate_name(dev->baudrate, baudrate_name, 20);
+	    rtcan_dev_get_state_name(dev->state,
+				     state_name, sizeof(state_name));
+	    rtcan_dev_get_baudrate_name(dev->baudrate,
+					baudrate_name, sizeof(baudrate_name));
 	    ret = RTCAN_PROC_PRINT("%-15s %9s %-8s %10d %10d %10d\n",
 				   dev->name, baudrate_name, state_name,
 				   dev->tx_count, dev->rx_count, dev->err_count);
@@ -195,8 +197,10 @@ static int rtcan_read_proc_sockets(char *buf, char **start, off_t offset,
 	    } else
 		sprintf(name, "%d", ifindex);
 	}
-	rtcan_get_timeout_name(sock->tx_timeout, tx_timeout, 20);
-	rtcan_get_timeout_name(sock->rx_timeout, rx_timeout, 20);
+	rtcan_get_timeout_name(sock->tx_timeout,
+			       tx_timeout, sizeof(tx_timeout));
+	rtcan_get_timeout_name(sock->rx_timeout,
+			       rx_timeout, sizeof(rx_timeout));
 	if (!RTCAN_PROC_PRINT("%2d %-15s %6d 0x%05x %13s %13s %10d %5d\n",
 			      context->fd, name, sock->flistlen,
 			      sock->err_mask, rx_timeout, tx_timeout,
@@ -224,10 +228,14 @@ static int rtcan_read_proc_info(char *buf, char **start, off_t offset,
     if (down_interruptible(&rtcan_devices_nrt_lock))
 	return -ERESTARTSYS;
 
-    rtcan_dev_get_state_name(dev->state, state_name, 20);
-    rtcan_dev_get_ctrlmode_name(dev->ctrl_mode, ctrlmode_name, 80);
-    rtcan_dev_get_baudrate_name(dev->baudrate, baudrate_name, 20);
-    rtcan_dev_get_bittime_name(&dev->bit_time, bittime_name, 80);
+    rtcan_dev_get_state_name(dev->state,
+			     state_name, sizeof(state_name));
+    rtcan_dev_get_ctrlmode_name(dev->ctrl_mode,
+				ctrlmode_name, sizeof(ctrlmode_name));
+    rtcan_dev_get_baudrate_name(dev->baudrate,
+				baudrate_name, sizeof(baudrate_name));
+    rtcan_dev_get_bittime_name(&dev->bit_time,
+			       bittime_name, sizeof(bittime_name));
 
     if (!RTCAN_PROC_PRINT("%s %s\n", "Device    ", dev->name) ||
 	!RTCAN_PROC_PRINT("%s %s\n", "Controller", dev->ctrl_name) ||
Signature · 1 line
1.7.6.5
#2 lore raw
In reply to Paul Janzen
Quoted 7 lines · Paul Janzen

When formatting /proc/rtcan/sockets, tx_timeout is defined as char[16], but a length of 20 is passed to rtcan_get_timeout_name. If tx_timeout for any socket is infinite, this causes snprintf to zero outside the buffer, leading to stack overflow and a kernel panic. Make the tx_timeout buffer 20 bytes long instead.

Wolfgang, ping?

Signature · 1 line
                                                                Gilles.
#3 lore raw
Quoted 9 lines · Gilles Chanteperdrix
Quoted 7 lines · Paul Janzen

When formatting /proc/rtcan/sockets, tx_timeout is defined as char[16], but a length of 20 is passed to rtcan_get_timeout_name. If tx_timeout for any socket is infinite, this causes snprintf to zero outside the buffer, leading to stack overflow and a kernel panic. Make the tx_timeout buffer 20 bytes long instead.

It's a bug, therefore:

Quoted 1 line

Wolfgang, ping?

Pong, sorry for delay.

Wolfgang.

#4 lore raw
Quoted 11 lines · Wolfgang Grandegger
Quoted 9 lines · Gilles Chanteperdrix
Quoted 7 lines · Paul Janzen

When formatting /proc/rtcan/sockets, tx_timeout is defined as char[16], but a length of 20 is passed to rtcan_get_timeout_name. If tx_timeout for any socket is infinite, this causes snprintf to zero outside the buffer, leading to stack overflow and a kernel panic. Make the tx_timeout buffer 20 bytes long instead.

Quoted 7 lines

It's a bug, therefore:

Quoted 1 line

Wolfgang, ping?

Pong, sorry for delay.

Hi,

No problem for the delay, merged, thanks.

We have two other unconfirmed CAN patches:
http://www.xenomai.org/pipermail/xenomai/2013-March/028111.html

And not really a patch, but modifying the code is trivial if you confirm that we can do it:
http://www.xenomai.org/pipermail/xenomai/2013-February/027824.html

Regards.

Signature · 1 line
                                                                Gilles.
#5 lore raw
Quoted 22 lines · Gilles Chanteperdrix
Quoted 11 lines · Wolfgang Grandegger
Quoted 9 lines · Gilles Chanteperdrix
Quoted 7 lines · Paul Janzen

When formatting /proc/rtcan/sockets, tx_timeout is defined as char[16], but a length of 20 is passed to rtcan_get_timeout_name. If tx_timeout for any socket is infinite, this causes snprintf to zero outside the buffer, leading to stack overflow and a kernel panic. Make the tx_timeout buffer 20 bytes long instead.

Quoted 7 lines

It's a bug, therefore:

Quoted 1 line

Wolfgang, ping?

Pong, sorry for delay.

Quoted 6 lines

Hi,

No problem for the delay, merged, thanks.

We have two other unconfirmed CAN patches:
http://www.xenomai.org/pipermail/xenomai/2013-March/028111.html

We can add even more PCI IDs here. I'm going to prepare a patch. Just support for 4 channel cards needs more effort.

Quoted 4 lines

And not really a patch, but modifying the code is trivial if you confirm that we can do it:

http://www.xenomai.org/pipermail/xenomai/2013-February/027824.html

Well, I'm not happy with that extension to rtcansend. I will answer to the original thread.

Wolfgang.

#6 lore raw
Quoted 5 lines · Wolfgang Grandegger
Quoted 2 lines

We have two other unconfirmed CAN patches:
http://www.xenomai.org/pipermail/xenomai/2013-March/028111.html

We can add even more PCI IDs here. I'm going to prepare a patch. Just support for 4 channel cards needs more effort.

Along these same lines, would there be any interest in a patch removing the deprecated ESD PCI-CAN driver?

All of the devices claimed by that driver are supported by PLX_PCI.

-- Paul

#7 lore raw

Hi Paul,

Quoted 10 lines · Paul Janzen
Quoted 5 lines · Wolfgang Grandegger
Quoted 2 lines

We have two other unconfirmed CAN patches:
http://www.xenomai.org/pipermail/xenomai/2013-March/028111.html

We can add even more PCI IDs here. I'm going to prepare a patch. Just support for 4 channel cards needs more effort.

Along these same lines, would there be any interest in a patch removing the deprecated ESD PCI-CAN driver?

It does not make a lot of sense to maintain two variants of a driver.

Quoted 1 line

All of the devices claimed by that driver are supported by PLX_PCI.

Yes, I will check if there are some relevant differences.

Wolfgang.