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)

3.18
x86_64
Linux
defect

Tracking

(Not tracked)

RESOLVED FIXED

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)

Attached file derdump-crash —
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)
Attached file derdump-crash.asan.log —
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.
Flags: needinfo?(kaie)
Keywords: sec-moderate
Whiteboard: cmd-line tools only
Blocks: nss-fuzz
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)
Attached patch patch v1 (obsolete) — — Splinter Review
Attachment #8642489 - Flags: review?(rrelyea)
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+
Mass cc to get some NSS eyes on these bugs.
I have tested the patch from kaie and see no more issues, also ran the fuzzer again for a while on derdump.
Group: core-security → crypto-core-security
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → 3.21
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+
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: nobody → kaie
Priority: -- → P1
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.
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)
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-
Attachment #8662948 - Flags: superreview?(kaie)
Attachment #8662948 - Flags: review?(rrelyea)
Attached patch Three optional cleanups (obsolete) — — Splinter Review
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)
Group: crypto-core-security → core-security-release
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: 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 → ---
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.
Attached patch comment.patch — — Splinter Review
Attachment #8684361 - Flags: review?(wtc)
Comment on attachment 8677169 [details] [diff] [review] Fix prettyPrintObjectID r=kaie
Attachment #8677169 - Flags: superreview?(kaie) → superreview+
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.
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 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-
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
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+
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-
Status: REOPENED → RESOLVED
Closed: 11 years ago → 11 years ago
Resolution: --- → FIXED
Target Milestone: 3.21 → 3.21.1
Attachment #8642489 - Attachment is obsolete: true
Target Milestone: 3.21.1 → 3.22
Attachment #8692186 - Flags: review?(kaie) → review+
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: