diff options
author | Sebastian Rasmussen <sebras@gmail.com> | 2017-12-11 14:09:15 +0100 |
---|---|---|
committer | Sebastian Rasmussen <sebras@gmail.com> | 2017-12-13 21:38:26 +0100 |
commit | 55c3f68d638ac1263a386e0aaa004bb6e8bde731 (patch) | |
tree | 27fd12c9cea4acc2718674b47cda3c029c490e11 | |
parent | c7a470081787cb85e6997dd611f24bfb280d55c2 (diff) |
Bugs 698804/698810/698811: Keep PDF object numbers below limit.
This ensures that:
* xref tables with objects pointers do not grow out of bounds.
* other readers, e.g. Adobe Acrobat can parse PDFs written by mupdf.
-rw-r--r-- | include/mupdf/pdf/object.h | 3 | ||||
-rw-r--r-- | source/pdf/pdf-repair.c | 5 | ||||
-rw-r--r-- | source/pdf/pdf-xref.c | 21 |
3 files changed, 16 insertions, 13 deletions
diff --git a/include/mupdf/pdf/object.h b/include/mupdf/pdf/object.h index 21ed8595b..4177112ba 100644 --- a/include/mupdf/pdf/object.h +++ b/include/mupdf/pdf/object.h | |||
@@ -3,6 +3,9 @@ | |||
3 | 3 | ||
4 | typedef struct pdf_document_s pdf_document; | 4 | typedef struct pdf_document_s pdf_document; |
5 | 5 | ||
6 | /* Defined in PDF 1.7 according to Acrobat limit. */ | ||
7 | #define PDF_MAX_OBJECT_NUMBER 8388607 | ||
8 | |||
6 | /* | 9 | /* |
7 | * Dynamic objects. | 10 | * Dynamic objects. |
8 | * The same type of objects as found in PDF and PostScript. | 11 | * The same type of objects as found in PDF and PostScript. |
diff --git a/source/pdf/pdf-repair.c b/source/pdf/pdf-repair.c index ca149bd3a..0c29758e2 100644 --- a/source/pdf/pdf-repair.c +++ b/source/pdf/pdf-repair.c | |||
@@ -6,9 +6,6 @@ | |||
6 | 6 | ||
7 | /* Scan file for objects and reconstruct xref table */ | 7 | /* Scan file for objects and reconstruct xref table */ |
8 | 8 | ||
9 | /* Define in PDF 1.7 to be 8388607, but mupdf is more lenient. */ | ||
10 | #define MAX_OBJECT_NUMBER (10 << 20) | ||
11 | |||
12 | struct entry | 9 | struct entry |
13 | { | 10 | { |
14 | int num; | 11 | int num; |
@@ -436,7 +433,7 @@ pdf_repair_xref(fz_context *ctx, pdf_document *doc) | |||
436 | break; | 433 | break; |
437 | } | 434 | } |
438 | 435 | ||
439 | if (num <= 0 || num > MAX_OBJECT_NUMBER) | 436 | if (num <= 0 || num > PDF_MAX_OBJECT_NUMBER) |
440 | { | 437 | { |
441 | fz_warn(ctx, "ignoring object with invalid object number (%d %d R)", num, gen); | 438 | fz_warn(ctx, "ignoring object with invalid object number (%d %d R)", num, gen); |
442 | goto have_next_token; | 439 | goto have_next_token; |
diff --git a/source/pdf/pdf-xref.c b/source/pdf/pdf-xref.c index 00586dbde..6284e70b7 100644 --- a/source/pdf/pdf-xref.c +++ b/source/pdf/pdf-xref.c | |||
@@ -868,11 +868,12 @@ pdf_read_old_xref(fz_context *ctx, pdf_document *doc, pdf_lexbuf *buf) | |||
868 | fz_seek(ctx, file, -(2 + (int)strlen(s)), SEEK_CUR); | 868 | fz_seek(ctx, file, -(2 + (int)strlen(s)), SEEK_CUR); |
869 | } | 869 | } |
870 | 870 | ||
871 | if (ofs < 0) | 871 | if (ofs < 0 || ofs > PDF_MAX_OBJECT_NUMBER |
872 | fz_throw(ctx, FZ_ERROR_GENERIC, "out of range object num in xref: %d", (int)ofs); | 872 | || len < 0 || len > PDF_MAX_OBJECT_NUMBER |
873 | if (ofs > INT64_MAX - len) | 873 | || ofs + len - 1 > PDF_MAX_OBJECT_NUMBER) |
874 | fz_throw(ctx, FZ_ERROR_GENERIC, "xref section object numbers too big"); | 874 | { |
875 | 875 | fz_throw(ctx, FZ_ERROR_GENERIC, "xref subsection object numbers are out of range"); | |
876 | } | ||
876 | /* broken pdfs where size in trailer undershoots entries in xref sections */ | 877 | /* broken pdfs where size in trailer undershoots entries in xref sections */ |
877 | if (ofs + len > xref_len) | 878 | if (ofs + len > xref_len) |
878 | { | 879 | { |
@@ -933,10 +934,8 @@ pdf_read_new_xref_section(fz_context *ctx, pdf_document *doc, fz_stream *stm, in | |||
933 | pdf_xref_entry *table; | 934 | pdf_xref_entry *table; |
934 | int i, n; | 935 | int i, n; |
935 | 936 | ||
936 | if (i0 < 0 || i1 < 0 || i0 > INT_MAX - i1) | 937 | if (i0 < 0 || i0 > PDF_MAX_OBJECT_NUMBER || i1 < 0 || i1 > PDF_MAX_OBJECT_NUMBER || i0 + i1 - 1 > PDF_MAX_OBJECT_NUMBER) |
937 | fz_throw(ctx, FZ_ERROR_GENERIC, "negative xref stream entry index"); | 938 | fz_throw(ctx, FZ_ERROR_GENERIC, "xref subsection object numbers are out of range"); |
938 | //if (i0 + i1 > pdf_xref_len(ctx, doc)) | ||
939 | // fz_throw(ctx, FZ_ERROR_GENERIC, "xref stream has too many entries"); | ||
940 | 939 | ||
941 | table = pdf_xref_find_subsection(ctx, doc, i0, i1); | 940 | table = pdf_xref_find_subsection(ctx, doc, i0, i1); |
942 | for (i = i0; i < i0 + i1; i++) | 941 | for (i = i0; i < i0 + i1; i++) |
@@ -2086,6 +2085,10 @@ pdf_create_object(fz_context *ctx, pdf_document *doc) | |||
2086 | /* TODO: reuse free object slots by properly linking free object chains in the ofs field */ | 2085 | /* TODO: reuse free object slots by properly linking free object chains in the ofs field */ |
2087 | pdf_xref_entry *entry; | 2086 | pdf_xref_entry *entry; |
2088 | int num = pdf_xref_len(ctx, doc); | 2087 | int num = pdf_xref_len(ctx, doc); |
2088 | |||
2089 | if (num > PDF_MAX_OBJECT_NUMBER) | ||
2090 | fz_throw(ctx, FZ_ERROR_GENERIC, "too many objects stored in pdf"); | ||
2091 | |||
2089 | entry = pdf_get_incremental_xref_entry(ctx, doc, num); | 2092 | entry = pdf_get_incremental_xref_entry(ctx, doc, num); |
2090 | entry->type = 'f'; | 2093 | entry->type = 'f'; |
2091 | entry->ofs = -1; | 2094 | entry->ofs = -1; |