Skip to content

Commit

Permalink
509: fix printing uninitialized stack memory when OID is empty
Browse files Browse the repository at this point in the history
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.

Fixes: 4f73175 ("X.509: Add utility functions to render OIDs as strings")
Signed-off-by: Eric Biggers <[email protected]>
Signed-off-by: David Howells <[email protected]>
  • Loading branch information
ebiggers authored and dhowells committed Dec 8, 2017
1 parent 47e0a20 commit 8dfd2f2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/oid_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit 8dfd2f2

Please sign in to comment.