Skip to contentSkip to conversation

drivers/base: Fix length checks in create_syslog_header()/dev_vprintk_emit()

PATCH Superseded Ben Hutchings 3 participants 5 messages Jun 8, 2014 – Jul 11, 2014 Driver core

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drivers/base: Fix length checks in create_syslog_header()/dev_vprintk_emit()

snprintf() returns the number of bytes that could have been written (excluding the null), not the actual number of bytes written. Given a long enough subsystem or device name, these functions will advance beyond the end of the on-stack buffer in dev_vprintk_exit(), resulting in an information leak or stack corruption. I don't know whether such a long name is currently possible.

In case snprintf() returns a value >= the buffer size, do not add structured logging information. Also WARN if this happens, so we can fix the driver or increase the buffer size.

---
v2: use dev_WARN() not dev_WARN_ON()

 drivers/base/core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/base/core.c +9 -0
index 20da3ad..062b7bf 100644
@@ -2007,6 +2007,8 @@ create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
 		return 0;
 
 	pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
+	if (pos >= hdrlen)
+		goto overflow;
 
 	/*
 	 * Add device identifier DEVICE=:
@@ -2038,7 +2040,14 @@ create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
 				"DEVICE=+%s:%s", subsys, dev_name(dev));
 	}
 
+	if (pos >= hdrlen)
+		goto overflow;
+
 	return pos;
+
+overflow:
+	dev_WARN(dev, 1, "device/subsystem name too long");
+	return 0;
 }
 
 int dev_vprintk_emit(int level, const struct device *dev,
Signature · 2 lines
Ben Hutchings
Never attribute to conspiracy what can adequately be explained by stupidity.
#1 lore raw
Quoted 8 lines · Ben Hutchings

snprintf() returns the number of bytes that could have been written (excluding the null), not the actual number of bytes written. Given a long enough subsystem or device name, these functions will advance beyond the end of the on-stack buffer in dev_vprintk_exit(), resulting in an information leak or stack corruption. I don't know whether such a long name is currently possible.
In case snprintf() returns a value >= the buffer size, do not add structured logging information.

I believe this is guaranteed to be < 128, but
I also suppose it'd be better to emit whatever
buffer is < 128 and add a WARN_ON_ONCE instead.

#2 lore raw
In reply to Ben Hutchings
Quoted 7 lines · Ben Hutchings

snprintf() returns the number of bytes that could have been written (excluding the null), not the actual number of bytes written. Given a long enough subsystem or device name, these functions will advance beyond the end of the on-stack buffer in dev_vprintk_exit(), resulting in an information leak or stack corruption. I don't know whether such a long name is currently possible.

Quoted 7 lines

In case snprintf() returns a value >= the buffer size, do not add structured logging information. Also WARN if this happens, so we can fix the driver or increase the buffer size.

---
v2: use dev_WARN() not dev_WARN_ON()

This patch breaks the build in a huge way:

drivers/base/core.c: In function ‘create_syslog_header’:
drivers/base/core.c:2049:16: error: expected ‘)’ before numeric constant

  dev_WARN(dev, 1, "device/subsystem name too long");
                  ^

is the start of it, it goes on for a page or so after that :(

greg k-h

#3 lore raw
Quoted 16 lines · Greg Kroah-Hartman
Quoted 7 lines · Ben Hutchings

snprintf() returns the number of bytes that could have been written (excluding the null), not the actual number of bytes written. Given a long enough subsystem or device name, these functions will advance beyond the end of the on-stack buffer in dev_vprintk_exit(), resulting in an information leak or stack corruption. I don't know whether such a long name is currently possible.

Quoted 7 lines

In case snprintf() returns a value >= the buffer size, do not add structured logging information. Also WARN if this happens, so we can fix the driver or increase the buffer size.

---
v2: use dev_WARN() not dev_WARN_ON()

Quoted 8 lines

This patch breaks the build in a huge way:

drivers/base/core.c: In function ‘create_syslog_header’:
drivers/base/core.c:2049:16: error: expected ‘)’ before numeric constant

  dev_WARN(dev, 1, "device/subsystem name too long");
                  ^

is the start of it, it goes on for a page or so after that :(

Sorry about that, I must not have committed the working version before mailing it. If you delete the '1, ' it should work. If you've already deleted the patch, I'll check and send the working version.

Ben.

Signature · 2 lines
Ben Hutchings
To err is human; to really foul things up requires a computer.
#4 lore raw
Quoted 24 lines · Ben Hutchings
Quoted 23 lines · Greg Kroah-Hartman
Quoted 7 lines · Ben Hutchings

snprintf() returns the number of bytes that could have been written (excluding the null), not the actual number of bytes written. Given a long enough subsystem or device name, these functions will advance beyond the end of the on-stack buffer in dev_vprintk_exit(), resulting in an information leak or stack corruption. I don't know whether such a long name is currently possible.

Quoted 7 lines

In case snprintf() returns a value >= the buffer size, do not add structured logging information. Also WARN if this happens, so we can fix the driver or increase the buffer size.

---
v2: use dev_WARN() not dev_WARN_ON()

This patch breaks the build in a huge way:

drivers/base/core.c: In function ‘create_syslog_header’:
drivers/base/core.c:2049:16: error: expected ‘)’ before numeric constant

  dev_WARN(dev, 1, "device/subsystem name too long");
                  ^
Quoted 5 lines
Quoted 1 line

is the start of it, it goes on for a page or so after that :(

Sorry about that, I must not have committed the working version before mailing it. If you delete the '1, ' it should work. If you've already deleted the patch, I'll check and send the working version.

It's long gone, can you please fix and resend?

thanks,

greg k-h