Closed
Bug 1148374
Opened 11 years ago
Closed 11 years ago
heap-buffer-overflow / invalid memory read in prettyPrintObjectID (exposed by derdump)
Categories
(NSS :: Tools, defect, P1)
Tracking
(Not tracked)
RESOLVED
FIXED
3.22
People
(Reporter: hanno, Assigned: wtc)
References
(Blocks 1 open bug)
Details
(Keywords: sec-moderate, Whiteboard: cmd-line tools only)
Attachments
(5 files, 4 obsolete files)
|
2 bytes,
application/octet-stream
|
Details | |
|
2.47 KB,
text/plain
|
Details | |
|
557 bytes,
patch
|
KaiE
:
superreview+
wtc
:
checked-in+
|
Details | Diff | Splinter Review |
|
759 bytes,
patch
|
wtc
:
review-
|
Details | Diff | Splinter Review |
|
2.54 KB,
patch
|
KaiE
:
review+
wtc
:
checked-in+
|
Details | Diff | Splinter Review |
Attached sample file (which is just the two bytes 06 00) will cause an invalid memory access in derdump.
It happens in the function prettyPrintObjectID. I think this is only used by the nss command line tools and not by the library itself. (Don't know if there is a policy for potential security issues in the nss command line tools, so I'm marking this as security for now.)
This was found with american fuzzy lop. It can be seen with either valgrind or address sanitizer. I'll attach the address sanitizer crash dump, the relevant part is:
==32759==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000e852 at pc 0x436bc1 bp 0x7fff137d8890 sp 0x7fff137d8880
READ of size 1 at 0x60200000e852 thread T0
#0 0x436bc0 in prettyPrintObjectID /data/nss/nss-3.17.2/nss/cmd/lib/derprint.c:235
#1 0x436bc0 in prettyPrintItem /data/nss/nss-3.17.2/nss/cmd/lib/derprint.c:548
#2 0x436bc0 in DER_PrettyPrint /data/nss/nss-3.17.2/nss/cmd/lib/derprint.c:586
#3 0x404cda in main /data/nss/nss-3.17.2/nss/cmd/derdump/derdump.c:92
#4 0x7ffa28663f9f in __libc_start_main (/lib64/libc.so.6+0x1ff9f)
#5 0x40534d (/data/nss/nss-3.17.2/nss/cmd/derdump/Linux3.16_x86_64_afl-gcc_glibc_PTH_64_DBG.OBJ/derdump+0x40534d)
| Reporter | ||
Comment 1•11 years ago
|
||
Comment 2•11 years ago
|
||
Kai: do you know if anyone has built systems on the command-line tools to the extent that we need to keep this bug hidden? On the Firefox side no one's at risk so we'd be OK unhiding the bug.
Comment 3•11 years ago
|
||
On Fedora and RHEL we do ship derdump, but we ship it outside the standard search path, and it's declared as an "unsupported tool". However, we ship a manual page for derdump!
I also saw that "derdump" is listed in the file list of a ubuntu package.
Given that it's a tool that's shipped, it might be good to keep it hidden until resolved.
(I see another utility that calls this code, "checkcert", but we aren't shipping it in Fedora/RHEL. It appears to be some sort of integrity checking utility for certificates.)
Flags: needinfo?(kaie)
Comment 4•11 years ago
|
||
Attachment #8642489 -
Flags: review?(rrelyea)
Comment 5•11 years ago
|
||
Comment on attachment 8642489 [details] [diff] [review]
patch v1
r+
I'm not sure we need to keep issues in cmd/lib or even in cmd security sensitive. Most of these tools are diagnostic tools, rarely ran and likely difficult to social engineer someone running. It' probably more important to let users know that the command could have an issue rather than hiding potential overflow issues from attackers.
Attachment #8642489 -
Flags: review?(rrelyea) → review+
Comment 6•11 years ago
|
||
Mass cc to get some NSS eyes on these bugs.
| Reporter | ||
Comment 7•11 years ago
|
||
I have tested the patch from kaie and see no more issues, also ran the fuzzer again for a while on derdump.
Updated•11 years ago
|
Group: core-security → crypto-core-security
Comment 8•11 years ago
|
||
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → 3.21
| Assignee | ||
Comment 9•11 years ago
|
||
Comment on attachment 8642489 [details] [diff] [review]
patch v1
Review of attachment 8642489 [details] [diff] [review]:
-----------------------------------------------------------------
::: cmd/lib/derprint.c
@@ +503,5 @@
> data += lenLen;
>
> /*
> + * Just quit now if slen more bytes puts us off the end,
> + * or if there's no more data to process.
The fix is correct, but the new comment is wrong.
If (data + slen) == end, we are also off the end
because |end| is one past the last valid byte.
Please remove the new comment.
I found a similar bug in prettyPrintLength(). I
will attach a patch next.
Attachment #8642489 -
Flags: review+
Attachment #8642489 -
Flags: checked-in+
| Assignee | ||
Comment 10•11 years ago
|
||
Kai, please review. I also changed how we set the lenLen
variable, which is the function's return value. That change
is optional, but I think it makes it easier to verify
lenLen is set correctly.
I propose that we set lenLen to 1 at the place where we
read the first byte from |data|:
lbyte = *data++;
Later, if we read more bytes from |data|, we increment
lenLen with the number of bytes.
Attachment #8662416 -
Flags: review?(kaie)
| Assignee | ||
Updated•11 years ago
|
Assignee: nobody → kaie
Priority: -- → P1
| Assignee | ||
Comment 11•11 years ago
|
||
Comment on attachment 8642489 [details] [diff] [review]
patch v1
Review of attachment 8642489 [details] [diff] [review]:
-----------------------------------------------------------------
::: cmd/lib/derprint.c
@@ +503,5 @@
> data += lenLen;
>
> /*
> + * Just quit now if slen more bytes puts us off the end,
> + * or if there's no more data to process.
Hmm... I think the (data + slen) == end case here requires
more thought. So don't remove the new comment yet.
| Assignee | ||
Comment 12•11 years ago
|
||
Comment on attachment 8662416 [details] [diff] [review]
Fix buffer end check in prettyPrintLength
This also requires more thought.
Attachment #8662416 -
Attachment is obsolete: true
Attachment #8662416 -
Flags: review?(kaie)
| Assignee | ||
Comment 13•11 years ago
|
||
Comment on attachment 8642489 [details] [diff] [review]
patch v1
Review of attachment 8642489 [details] [diff] [review]:
-----------------------------------------------------------------
::: cmd/lib/derprint.c
@@ +508,2 @@
> */
> + if ((data + slen) >= end) {
I confirmed with an experiment that the fix is wrong. Here is
a demo. ~/tmp/redhat.com.der is the DER-encoded certificate
of https://www.redhat.com/ that I just downloaded.
$ derdump -i ~/tmp/redhat.com.der
C-Sequence (1376)
derdump: error -8183: SEC_ERROR_BAD_DER: security library: improperly formatted DER-encoded message.
derdump: errno=2: No such file or directory
I suspected the fix is wrong because we return -1 here if
(data + slen) == end, but that case is actually valid. (I was
also misled by this patch earlier.)
The reason (data + slen) == end is valid is that the code below
should only read up to data[slen -1] if slen > 0, and should read
nothing if slen == 0. So here it is an error if
&data[slen -1] >= end
which is equivalent to
data + slen >= end + 1
or
data + slen > end
The correct fix is to inspect all the code paths below and make sure
they adhere to this contract. I already found the bug in prettyPrintObjectID:
216 static int
217 prettyPrintObjectID(FILE *out, const unsigned char *data,
218 unsigned int len, unsigned int level, PRBool raw)
219 {
...
227 /*
228 * First print the Object Id in numeric format
229 */
230
231 rv = prettyIndent(out, level);
232 if (rv < 0)
233 return rv;
234
235 val = data[0];
Note that it reads data[0] without checking len. The fix is:
diff --git a/cmd/lib/derprint.c b/cmd/lib/derprint.c
--- a/cmd/lib/derprint.c
+++ b/cmd/lib/derprint.c
@@ -227,16 +227,20 @@ prettyPrintObjectID(FILE *out, const uns
/*
* First print the Object Id in numeric format
*/
rv = prettyIndent(out, level);
if (rv < 0)
return rv;
+ if (len == 0) {
+ PORT_SetError(SEC_ERROR_BAD_DER);
+ return -1;
+ }
val = data[0];
i = val % 40;
val = val / 40;
rv = fprintf(out, "%lu %u ", val, i);
if (rv < 0) {
PORT_SetError(SEC_ERROR_IO);
return rv;
}
Attachment #8642489 -
Flags: review+ → review-
| Assignee | ||
Comment 14•11 years ago
|
||
Attachment #8662948 -
Flags: superreview?(kaie)
Attachment #8662948 -
Flags: review?(rrelyea)
| Assignee | ||
Comment 15•11 years ago
|
||
I reviewed cmd/lib/derprint.c exhaustively. The three changes
in this patch are independent of each other and are all
optional.
1. getInteger256() may want to explicitly detect a most significant
bit of 1 and return -1. Right now it returns a negative |val|, which
is checked by the caller. Therefore this change is about making it
clearer we considered this error condition, but the current code is
correct.
2. prettyPrintObjectID() has some custom-written code for the raw
mode that can be replaced by the standard code for raw mode used by
other functions.
3. prettyPrintLength(): the code that reads the length bytes and
the code that report the number of length bytes (lenLen) are far
apart. I changed it so that we update lenLen whenever we read any
length bytes. This change makes it easier to verify the code is
correct.
Attachment #8662954 -
Flags: superreview?(rrelyea)
Attachment #8662954 -
Flags: review?(kaie)
Updated•11 years ago
|
Group: crypto-core-security → core-security-release
| Assignee | ||
Comment 16•11 years ago
|
||
Comment on attachment 8642489 [details] [diff] [review]
patch v1
Review of attachment 8642489 [details] [diff] [review]:
-----------------------------------------------------------------
Kai: I reverted this patch because it is incorrect.
https://hg.mozilla.org/projects/nss/rev/a6164e360845
Attachment #8642489 -
Flags: checked-in+ → checked-in-
| Assignee | ||
Comment 17•11 years ago
|
||
Assignee: kaie → wtc
Attachment #8662948 -
Attachment is obsolete: true
Status: RESOLVED → REOPENED
Attachment #8662948 -
Flags: superreview?(kaie)
Attachment #8662948 -
Flags: review?(rrelyea)
Attachment #8677169 -
Flags: superreview?(kaie)
Attachment #8677169 -
Flags: review?(rrelyea)
Resolution: FIXED → ---
Comment 18•11 years ago
|
||
Wan-Teh, thanks for your analysis.
We should document the knowledge you've built into the patch for future readers.
I turned your explanation into simpler statements, that we might want to add as a comment to the
/*
* Just quit now if slen more bytes puts us off the end.
*/
if ((data + slen) > end) {
section:
"(data + slen) == end" is valid
if "slen > 0" the code below should only read up to data[slen -1]
if "slen == 0" it should read nothing .
All the code paths below must adhere to this contract.
Comment 19•11 years ago
|
||
Attachment #8684361 -
Flags: review?(wtc)
Comment 20•11 years ago
|
||
Comment on attachment 8677169 [details] [diff] [review]
Fix prettyPrintObjectID
r=kaie
Attachment #8677169 -
Flags: superreview?(kaie) → superreview+
Comment 21•11 years ago
|
||
Comment on attachment 8662954 [details] [diff] [review]
Three optional cleanups
>+ /* If the most significant bit of data[0] is 1, val would be negative.
>+ * Treat it as an error.
>+ */
>+ if (data[0] & 0x8) {
>+ PORT_SetError(SEC_ERROR_BAD_DER);
>+ return -1;
>+ }
Shouldn't that be 0x80 ?
if (data[0] & 0x80) {
You're worried about platforms where int is 32 bit.
"data[]" is unsigned char.
Which type will "unsigned char <<" produce?
Can we be certain it produces an unsigned int?
If we can assume it won't be converted to a signed type, then I agree it's sufficient to worry about the 4-bytes scenario, only.
Comment 22•11 years ago
|
||
If you wanted to avoid code duplication, we could use:
case 4:
/* Ensure result will be a positive number on 32 bit platforms */
if ((data[0] & 0x80) == 0) {
val = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
break;
}
/* fall through */
default:
PORT_SetError(SEC_ERROR_BAD_DER);
return -1;
Comment 23•11 years ago
|
||
Comment on attachment 8662954 [details] [diff] [review]
Three optional cleanups
r- because of the 0x8
Everything else looks good, r=kaie if you change the 0x8 to 0x80.
Attachment #8662954 -
Flags: review?(kaie) → review-
| Assignee | ||
Comment 24•11 years ago
|
||
Comment on attachment 8662954 [details] [diff] [review]
Three optional cleanups
Review of attachment 8662954 [details] [diff] [review]:
-----------------------------------------------------------------
(In reply to Kai Engert (:kaie) from comment #21)
> Comment on attachment 8662954 [details] [diff] [review]
> Three optional cleanups
>
> Shouldn't that be 0x80 ?
>
> if (data[0] & 0x80) {
You are right!
> You're worried about platforms where int is 32 bit.
>
> "data[]" is unsigned char.
> Which type will "unsigned char <<" produce?
> Can we be certain it produces an unsigned int?
I believe "unsigned char <<" will produce a signed int
because a signed int is large enough to hold an unsigned
char value. I will need to read the C Standard to be sure.
> If we can assume it won't be converted to a signed type, then I agree it's
> sufficient to worry about the 4-bytes scenario, only.
An unsigned char value is always nonnegative, so
"unsigned char << 8", even if it produces a signed int,
is always nonnegative. Sign bit extension won't be performed.
Similarly for "unsigned char << 16". Only "unsigned char << 24"
is problematic. Here is an experiment.
wtc@wtc-desktop:~/tmp$ cat unsigned-char.c
#include <stdio.h>
int main() {
unsigned char ch = 0x80;
int i = ch;
int i1 = ch << 8;
int i2 = ch << 16;
int i3 = ch << 24;
printf("%d %d %d %d\n", i, i1, i2, i3);
return 0;
}
wtc@wtc-desktop:~/tmp$ gcc -Wall unsigned-char.c
wtc@wtc-desktop:~/tmp$ ./a.out
128 32768 8388608 -2147483648
| Assignee | ||
Comment 25•11 years ago
|
||
Comment on attachment 8677169 [details] [diff] [review]
Fix prettyPrintObjectID
https://hg.mozilla.org/projects/nss/rev/c6204b8cb16b
Attachment #8677169 -
Flags: checked-in+
| Assignee | ||
Comment 26•11 years ago
|
||
I fixed the typo (0x8 => 0x80) and checked in the patch:
https://hg.mozilla.org/projects/nss/rev/e1e2087b8986
Attachment #8662954 -
Attachment is obsolete: true
Attachment #8662954 -
Flags: superreview?(rrelyea)
Attachment #8692186 -
Flags: review?(kaie)
Attachment #8692186 -
Flags: checked-in+
| Assignee | ||
Comment 27•11 years ago
|
||
Comment on attachment 8684361 [details] [diff] [review]
comment.patch
Review of attachment 8684361 [details] [diff] [review]:
-----------------------------------------------------------------
Kai: thank you for converting my explanation into a comment.
I discovered that my explanation has a hole. So this comment
needs work. But I actually think we don't need a comment to
explain the original code. Please see the details below.
::: cmd/lib/derprint.c
@@ +509,5 @@
> /*
> * Just quit now if slen more bytes puts us off the end.
> + * "(data + slen) == end" is valid.
> + * if "slen > 0" the code below should only read up to data[slen -1].
> + * if "slen == 0" it should read nothing .
This sentence is not correct in the "constructed" case:
if (code & SEC_ASN1_CONSTRUCTED) {
if (slen > 0 || indefinite) {
slen = prettyPrintItem(out, data,
slen == 0 ? end : data + slen,
lv+1, raw);
if (slen < 0)
return slen;
data += slen;
}
} else if ...
This code still reads bytes if slen == 0. So this sentence needs to be
qualified.
But I think we don't need to add this comment. The reason is that
the (data + slen) > end is a very common array-bound check, where
|end| points to the byte right after the end of the array. I needed
to write a lengthy explanation to prove why (data + slen) >= end is
wrong, but we should not need to explain why a common array-bound
check is correct.
Attachment #8684361 -
Flags: review?(wtc) → review-
| Assignee | ||
Updated•11 years ago
|
Status: REOPENED → RESOLVED
Closed: 11 years ago → 11 years ago
Resolution: --- → FIXED
Target Milestone: 3.21 → 3.21.1
| Assignee | ||
Updated•11 years ago
|
Attachment #8642489 -
Attachment is obsolete: true
Updated•10 years ago
|
Target Milestone: 3.21.1 → 3.22
Updated•10 years ago
|
Attachment #8692186 -
Flags: review?(kaie) → review+
Updated•10 years ago
|
Group: core-security-release
You need to log in
before you can comment on or make changes to this bug.
Description
•