X.509: fix printing uninitialized stack memory when OID is empty
Subsystems (3)
PATCH 1/1 X.509: fix printing uninitialized stack memory when OID is empty
From: Eric Biggers <[email protected]>
Callers of sprint_oid() do not check its return value before printing the result. In the case where the OID is zero-length, -EBADMSG was being returned without anything being written to the buffer, resulting in uninitialized stack memory being printed. Fix this by writing "(empty)" to the buffer in that case.
--- lib/oid_registry.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
lib/oid_registry.c +3 -1
@@ -115,8 +115,10 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)size_t ret;int count; -if (v >= end) +if (v >= end) { +snprintf (buffer, bufsize,"(empty)" );return -EBADMSG ; + } n = *v++; ret = count =snprintf (buffer, bufsize,"%u.%u" , n /40 , n %40 );
Signature · 1 line
2.15.0
David Howells
maintainerI wonder if all -EBADMSG returns here should just print "(badoid)" into the buffer.
David
Hi David,
Quoted 3 lines · David Howells
I wonder if all
-EBADMSGreturns here should just print "(badoid)" into the buffer.
I don't really care either way; it's just a question of whether it's better to truncate the bad part, or print a special value. If you prefer the latter here is a revised patch to consider. (I went with "(bad)" instead of "(badoid)" because the callers already label the string as an OID.):
---8<---
From 5a68ec1afd819e145446a97268bd790f9f3226b3 Mon Sep 17 00:00:00 2001
From: Eric Biggers <[email protected]>
Date: Tue, 28 Nov 2017 10:47:42 -0800
Subject: [PATCH v2] X.509: fix printing uninitialized stack memory when OID is
empty
Callers of sprint_oid() do not check its return value before printing the result. In the case where the OID is zero-length, -EBADMSG was being returned without anything being written to the buffer, resulting in uninitialized stack memory being printed. Fix this by writing "(bad)" to the buffer in the cases where -EBADMSG is returned.
--- lib/oid_registry.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/oid_registry.c +6 -2
@@ -116,7 +116,7 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)int count;if (v >= end) -return -EBADMSG ; + goto bad; n = *v++; ret = count =snprintf (buffer, bufsize,"%u.%u" , n /40 , n %40 );
@@ -134,7 +134,7 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize) num = n &0x7f ; do { if (v >= end) - return -EBADMSG; + goto bad; n = *v++; num <<= 7; num |= n & 0x7f;
@@ -148,6 +148,10 @@ int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize) }return ret; + +bad: +snprintf (buffer, bufsize,"(bad)" ); +return -EBADMSG ; }EXPORT_SYMBOL_GPL (sprint_oid);
Signature · 1 line
2.15.0.417.g466bffb3ac-goog