-
Notifications
You must be signed in to change notification settings - Fork 7
/
pure.h
1811 lines (1741 loc) · 58.8 KB
/
pure.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// pure.h - C99 interface:
#ifndef PURE_H
#define PURE_H
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pure_constants.h"
#include "pure_errors.h"
#include "pure_signatures.h"
#include "pure_routines.h"
#include "zlib/zlib.h"
typedef struct pure_ctx {
uint64_t flags;
uint64_t depth;
uint64_t files;
uint64_t archives;
uint64_t size;
uint64_t compressed_size;
uint64_t uncompressed_size;
} pure_ctx;
typedef struct pure_zip_lfh {
uint64_t offset;
uint64_t length;
uint64_t version_minimum;
uint64_t general_purpose_bit_flag;
uint64_t compression_method;
uint64_t last_mod_file_time;
uint64_t last_mod_file_date;
uint64_t crc32;
uint64_t compressed_size;
uint64_t uncompressed_size;
uint8_t* file_name;
uint64_t file_name_length;
uint8_t* extra_field;
uint64_t extra_field_length;
uint8_t zip64;
} pure_zip_lfh;
typedef struct pure_zip_ddr {
uint64_t offset;
uint64_t length;
uint64_t crc32;
uint64_t compressed_size;
uint64_t uncompressed_size;
uint8_t zip64;
} pure_zip_ddr;
typedef struct pure_zip_cdh {
uint64_t offset;
uint64_t length;
uint64_t version_made;
uint64_t version_minimum;
uint64_t general_purpose_bit_flag;
uint64_t compression_method;
uint64_t last_mod_file_time;
uint64_t last_mod_file_date;
uint64_t crc32;
uint64_t compressed_size;
uint64_t uncompressed_size;
uint8_t* file_name;
uint64_t file_name_length;
uint8_t* extra_field;
uint64_t extra_field_length;
uint8_t* file_comment;
uint64_t file_comment_length;
uint64_t disk;
uint64_t internal_file_attributes;
uint64_t external_file_attributes;
uint64_t relative_offset;
uint64_t unix_mode;
uint8_t directory;
uint8_t zip64;
} pure_zip_cdh;
typedef struct pure_zip_eocdr_64 {
uint64_t offset;
uint64_t length;
uint64_t version_made;
uint64_t version_minimum;
uint64_t disk;
uint64_t cd_disk;
uint64_t cd_disk_records;
uint64_t cd_records;
uint64_t cd_size;
uint64_t cd_offset;
uint8_t* extensible_data_sector;
uint64_t extensible_data_sector_length;
} pure_zip_eocdr_64;
typedef struct pure_zip_eocdl_64 {
uint64_t offset;
uint64_t length;
uint64_t disk;
uint64_t eocdr_64_offset;
uint64_t disks;
} pure_zip_eocdl_64;
typedef struct pure_zip_eocdr {
uint64_t offset;
uint64_t length;
uint64_t disk;
uint64_t cd_disk;
uint64_t cd_disk_records;
uint64_t cd_records;
uint64_t cd_size;
uint64_t cd_offset;
uint8_t* comment;
uint64_t comment_length;
uint8_t zip64;
} pure_zip_eocdr;
int pure_zip_crc32(
const uint8_t* buffer,
const uint64_t size,
uint64_t* result
) {
assert(SIZE_MAX >= UINT32_MAX);
assert(size <= SIZE_MAX);
assert(*result == 0);
unsigned long checksum = crc32_z(0UL, Z_NULL, 0);
checksum = crc32_z(checksum, buffer, (size_t) size);
*result = (uint64_t) checksum;
return 0;
}
void pure_zip_debug_lfh(const pure_zip_lfh* header) {
printf("LOCAL FILE HEADER:\n");
printf(
"offset=%llu length=%llu\n",
header->offset,
header->length
);
printf(
"general_purpose_bit_flag=%llu version_minimum=%llu\n",
header->general_purpose_bit_flag,
header->version_minimum
);
printf(
"compression_method=%llu last_mod_file_time=%llu last_mod_file_date=%llu\n",
header->compression_method,
header->last_mod_file_time,
header->last_mod_file_date
);
printf(
"crc32=%llu compressed_size=%llu uncompressed_size=%llu\n",
header->crc32,
header->compressed_size,
header->uncompressed_size
);
printf("file_name=\"");
fwrite(header->file_name, 1, header->file_name_length, stdout);
printf("\"\n");
printf(
"file_name_length=%llu extra_field_length=%llu\n",
header->file_name_length,
header->extra_field_length
);
printf("\n");
}
void pure_zip_debug_ddr(const pure_zip_ddr* header) {
printf("DATA DESCRIPTOR RECORD:\n");
printf(
"offset=%llu length=%llu\n",
header->offset,
header->length
);
printf(
"crc32=%llu compressed_size=%llu uncompressed_size=%llu\n",
header->crc32,
header->compressed_size,
header->uncompressed_size
);
printf("\n");
}
void pure_zip_debug_cdh(const pure_zip_cdh* header) {
printf("CENTRAL DIRECTORY HEADER:\n");
printf(
"offset=%llu length=%llu\n",
header->offset,
header->length
);
printf(
"general_purpose_bit_flag=%llu version_minimum=%llu version_made=%llu\n",
header->general_purpose_bit_flag,
header->version_minimum,
header->version_made
);
printf(
"compression_method=%llu last_mod_file_time=%llu last_mod_file_date=%llu\n",
header->compression_method,
header->last_mod_file_time,
header->last_mod_file_date
);
printf(
"crc32=%llu compressed_size=%llu uncompressed_size=%llu\n",
header->crc32,
header->compressed_size,
header->uncompressed_size
);
printf("file_name=\"");
fwrite(header->file_name, 1, header->file_name_length, stdout);
printf("\"\n");
printf(
"file_name_length=%llu extra_field_length=%llu file_comment_length=%llu\n",
header->file_name_length,
header->extra_field_length,
header->file_comment_length
);
printf("file_comment=\"");
fwrite(header->file_comment, 1, header->file_comment_length, stdout);
printf("\"\n");
printf(
"internal_file_attributes=%llu external_file_attributes=%llu\n",
header->internal_file_attributes,
header->external_file_attributes
);
printf(
"disk=%llu relative_offset=%llu\n",
header->disk,
header->relative_offset
);
printf(
"unix_mode=%llu directory=%u zip64=%u\n",
header->unix_mode,
header->directory,
header->zip64
);
printf("\n");
}
void pure_zip_debug_eocdl_64(const pure_zip_eocdl_64* header) {
printf("ZIP64 END OF CENTRAL DIRECTORY LOCATOR:\n");
printf("offset=%llu\n", header->offset);
printf("length=%llu\n", header->length);
printf("disk=%llu\n", header->disk);
printf("eocdr_64_offset=%llu\n", header->eocdr_64_offset);
printf("disks=%llu\n", header->disks);
printf("\n");
}
void pure_zip_debug_eocdr_64(const pure_zip_eocdr_64* header) {
printf("ZIP64 END OF CENTRAL DIRECTORY RECORD:\n");
printf("offset=%llu\n", header->offset);
printf("length=%llu\n", header->length);
printf("version_made=%llu\n", header->version_made);
printf("version_minimum=%llu\n", header->version_minimum);
printf("disk=%llu\n", header->disk);
printf("cd_disk=%llu\n", header->cd_disk);
printf("cd_disk_records=%llu\n", header->cd_disk_records);
printf("cd_records=%llu\n", header->cd_records);
printf("cd_size=%llu\n", header->cd_size);
printf("cd_offset=%llu\n", header->cd_offset);
printf(
"extensible_data_sector_length=%llu\n",
header->extensible_data_sector_length
);
printf("\n");
}
void pure_zip_debug_eocdr(const pure_zip_eocdr* header) {
printf("END OF CENTRAL DIRECTORY RECORD:\n");
printf("offset=%llu\n", header->offset);
printf("length=%llu\n", header->length);
printf("disk=%llu\n", header->disk);
printf("cd_disk=%llu\n", header->cd_disk);
printf("cd_disk_records=%llu\n", header->cd_disk_records);
printf("cd_records=%llu\n", header->cd_records);
printf("cd_size=%llu\n", header->cd_size);
printf("cd_offset=%llu\n", header->cd_offset);
printf("comment=\"");
fwrite(header->comment, 1, header->comment_length, stdout);
printf("\"\n");
printf("comment_length=%llu\n", header->comment_length);
printf("\n");
}
int pure_zip_verify_compression_ratio(
const uint64_t compressed_size,
const uint64_t uncompressed_size
) {
// Here, compressed size and uncompressed size could relate to the respective
// sizes of an individual file or the sum of the sizes across an archive.
//
// We do not use only a ratio limit to detect dangerous compression ratios,
// since an extreme ratio can be mitigated by a negligible uncompressed size.
//
// Instead, we calculate a score that combines the ratio and uncompressed size
// and then compare this score against a limit. The higher the ratio and the
// larger the uncompressed size, the more dangerous the file or archive.
//
// Conversely, a file or archive with high ratio but negligible uncompressed
// size is less likely to be dangerous, since while it may have high power,
// this power is multiplied by less weight.
if (compressed_size == 0) return 0; // Prevent divide-by-zero.
if (uncompressed_size < PURE_ZIP_COMPRESSION_RATIO_SIZE_MIN) return 0;
const uint64_t ratio = uncompressed_size / compressed_size;
const uint64_t score = ratio * uncompressed_size;
if (score > PURE_ZIP_COMPRESSION_RATIO_SCORE_MAX) {
return PURE_E_ZIP_BOMB_RATIO;
}
return 0;
}
int pure_zip_verify_compression_method(const uint64_t value) {
if (value == PURE_ZIP_COMPRESSION_METHOD_NONE) return 0;
if (value == PURE_ZIP_COMPRESSION_METHOD_DEFLATE) return 0;
// CVE-2016-9844:
// Defend vulnerable implementations against overflow when the two-byte
// compression method in the central directory file header exceeds 999.
// https://bugs.launchpad.net/ubuntu/+source/unzip/+bug/1643750
if (value > 999) return PURE_E_ZIP_COMPRESSION_METHOD_DANGEROUS;
if (value == 99) return PURE_E_ZIP_COMPRESSION_METHOD_ENCRYPTED;
return PURE_E_ZIP_COMPRESSION_METHOD_UNSUPPORTED;
}
int pure_zip_verify_compression_method_sizes(
const uint64_t compression_method,
const uint64_t compressed_size,
const uint64_t uncompressed_size
) {
{
int error = pure_zip_verify_compression_method(compression_method);
if (error) return error;
}
if (compression_method == 0 && compressed_size != uncompressed_size) {
return PURE_E_ZIP_STORED_COMPRESSION_SIZE_MISMATCH;
}
// CVE-2018-18384:
// Defend vulnerable implementations against buffer overflow.
// https://bugzilla.suse.com/show_bug.cgi?id=1110194
if (
uncompressed_size > 0 &&
compressed_size > uncompressed_size &&
compressed_size / uncompressed_size >= 2
) {
return PURE_E_ZIP_DANGEROUS_NEGATIVE_COMPRESSION_RATIO;
}
return 0;
}
int pure_zip_verify_date(const uint64_t value) {
/*
An MS-DOS date is a packed 16-bit (2 bytes) value in which bits in the value
represent the day, month, and year:
Bits: 0-4 5-8 9-15
Unit: Day Month Year
Range: 1-31 1-12 Relative to 1980
*/
if (value > PURE_16_BIT_MAX) return PURE_E_ZIP_DATE_OVERFLOW;
uint64_t y = (value >> 9) + 1980;
uint64_t m = (value >> 5) & 15;
uint64_t d = value & 31;
// 7 bits supports up to 127 years, or 2107 (1980 + 127).
// However, years after 2099 are not correctly handled by some software:
if (y > 2099) return PURE_E_ZIP_DATE_YEAR_OVERFLOW;
if (m > 12) return PURE_E_ZIP_DATE_MONTH_OVERFLOW;
if (d > 31) return PURE_E_ZIP_DATE_DAY_OVERFLOW;
return 0;
}
int pure_zip_verify_directory(const pure_zip_cdh* header) {
assert(header->directory == 0 || header->directory == 1);
if (header->directory) {
if (header->compressed_size > 0) return PURE_E_ZIP_DIRECTORY_COMPRESSED;
if (header->uncompressed_size > 0) return PURE_E_ZIP_DIRECTORY_UNCOMPRESSED;
}
return 0;
}
int pure_zip_verify_disk(const uint64_t value) {
if (value != 0) return PURE_E_ZIP_MULTIPLE_DISKS;
return 0;
}
int pure_zip_verify_extra_field(
const uint8_t* extra_field,
const uint64_t extra_field_length,
const uint8_t* file_name,
const uint64_t file_name_length
) {
if (extra_field_length > PURE_ZIP_EXTRA_FIELD_MAX) {
return PURE_E_ZIP_EXTRA_FIELD_MAX;
}
// The extra field contains a variety of optional data such as OS-specific
// attributes. It is divided into chunks, each with a 16-bit ID code and a
// 16-bit length.
if (extra_field_length != 0 && extra_field_length < 4) {
return PURE_E_ZIP_EXTRA_FIELD_MIN;
}
// Check extra field attribute sizes, and specifically the unicode path:
uint64_t offset = 0;
while (offset + 2 + 2 <= extra_field_length) {
const uint16_t id = pure_u16(extra_field + offset + 0);
const uint16_t size = pure_u16(extra_field + offset + 2);
if (offset + 2 + 2 + size > extra_field_length) {
return PURE_E_ZIP_EXTRA_FIELD_ATTRIBUTE_OVERFLOW;
}
if (id == 0x7075) {
// We expect at least a 1-byte version followed by a 4-byte crc32:
if (size < 1 + 4) return PURE_E_ZIP_EXTRA_FIELD_UNICODE_PATH_OVERFLOW;
assert(offset + 2 + 2 + 1 <= extra_field_length);
uint8_t version = extra_field[offset + 2 + 2];
if (version != 1) return PURE_E_ZIP_EXTRA_FIELD_UNICODE_PATH_VERSION;
// We require the unicode path to match the central directory file even if
// the crc32 of the non-unicode path is different. Otherwise, an attacker
// could present an alternative extension to bypass content inspection.
const uint8_t* unicode_path = extra_field + offset + 2 + 2 + 1 + 4;
const uint64_t unicode_path_length = size - 1 - 4;
if (
unicode_path_length != file_name_length ||
memcmp(unicode_path, file_name, (size_t) file_name_length)
) {
return PURE_E_ZIP_EXTRA_FIELD_UNICODE_PATH_DIFF;
}
}
offset += 4;
offset += size;
}
if (offset > extra_field_length) return PURE_E_ZIP_EXTRA_FIELD_OVERFLOW;
if (offset < extra_field_length) {
if (pure_zeroes(extra_field, offset, extra_field_length)) {
return PURE_E_ZIP_EXTRA_FIELD_UNDERFLOW_ZEROED;
} else {
return PURE_E_ZIP_EXTRA_FIELD_UNDERFLOW_BUFFER_BLEED;
}
}
return 0;
}
int pure_zip_verify_file_name(
const uint8_t* file_name,
const uint64_t file_name_length
) {
// A "file name" in this context is a path name with multiple components.
// The file name length may be 0 if the archiver input came from stdin.
// CVE-2018-1000035:
// Heap-based buffer overflow in password protected ZIP archives.
// https://sec-consult.com/en/blog/advisories/
// multiple-vulnerabilities-in-infozip-unzip/index.html
// We want to defend vulnerable implementations against PATH_MAX allocation
// bugs, e.g. malloc(N * PATH_MAX) where the assumption is that user data
// cannot exceed PATH_MAX.
if (file_name_length > PURE_PATH_MAX) return PURE_E_ZIP_FILE_NAME_LENGTH;
// CVE-2003-0282 (aka "JELMER"):
// Some zip implementations filter control characters amongst others.
// This behavior can be exploited to mask ".." in a directory traversal.
// https://www.securityfocus.com/archive/1/321090
if (pure_path_control_characters(file_name, file_name_length)) {
return PURE_E_ZIP_FILE_NAME_CONTROL_CHARACTERS;
}
if (pure_path_drive(file_name, file_name_length)) {
return PURE_E_ZIP_FILE_NAME_TRAVERSAL_DRIVE_PATH;
}
if (pure_path_relative(file_name, file_name_length)) {
return PURE_E_ZIP_FILE_NAME_TRAVERSAL_RELATIVE_PATH;
}
if (pure_path_double_dots(file_name, file_name_length)) {
return PURE_E_ZIP_FILE_NAME_TRAVERSAL_DOUBLE_DOTS;
}
if (pure_path_component_overflow(file_name, file_name_length)) {
return PURE_E_ZIP_FILE_NAME_COMPONENT_OVERFLOW;
}
// All slashes must be forward according to the APPNOTE.TXT specification:
for (uint64_t index = 0; index < file_name_length; index++) {
if (file_name[index] == PURE_BACKSLASH) {
return PURE_E_ZIP_FILE_NAME_BACKSLASH;
}
}
return 0;
}
int pure_zip_verify_flags(const uint64_t value) {
if (value > PURE_16_BIT_MAX) return PURE_E_ZIP_FLAG_OVERFLOW;
if (value & (1 << 0)) return PURE_E_ZIP_FLAG_TRADITIONAL_ENCRYPTION;
if (value & (1 << 4)) return PURE_E_ZIP_FLAG_ENHANCED_DEFLATE;
if (value & (1 << 5)) return PURE_E_ZIP_FLAG_COMPRESSED_PATCHED_DATA;
if (value & (1 << 6)) return PURE_E_ZIP_FLAG_STRONG_ENCRYPTION;
if (value & (1 << 7)) return PURE_E_ZIP_FLAG_UNUSED_BIT_7;
if (value & (1 << 8)) return PURE_E_ZIP_FLAG_UNUSED_BIT_8;
if (value & (1 << 9)) return PURE_E_ZIP_FLAG_UNUSED_BIT_9;
if (value & (1 << 10)) return PURE_E_ZIP_FLAG_UNUSED_BIT_10;
if (value & (1 << 12)) return PURE_E_ZIP_FLAG_ENHANCED_COMPRESSION;
if (value & (1 << 13)) return PURE_E_ZIP_FLAG_MASKED_LOCAL_HEADERS;
if (value & (1 << 14)) return PURE_E_ZIP_FLAG_RESERVED_BIT_14;
if (value & (1 << 15)) return PURE_E_ZIP_FLAG_RESERVED_BIT_15;
return 0;
}
int pure_zip_verify_string(
const uint8_t* string,
const uint64_t string_length,
const uint64_t utf8
) {
if (string_length > PURE_ZIP_STRING_MAX) return PURE_E_ZIP_STRING_MAX;
for (uint64_t index = 0; index < string_length; index++) {
if (string[index] == 0) return PURE_E_ZIP_STRING_NULL_BYTE;
}
if (utf8) {
// TO DO: Verify that UTF-8 encoding is valid:
// Some systems such as macOS never bother to set bit 11 to indicate UTF-8.
// We therefore always attempt UTF-8 and fallback to CP437 only on error.
// If the string must be UTF-8 then reject the string as invalid.
}
return 0;
}
int pure_zip_verify_symlink(
const pure_zip_cdh* cdh,
const pure_zip_lfh* lfh,
const uint8_t* buffer
) {
if ((cdh->unix_mode & PURE_UNIX_MODE_MASK) != PURE_UNIX_MODE_LNK) return 0;
if (cdh->compression_method != 0) return PURE_E_ZIP_SYMLINK_COMPRESSED;
assert(cdh->relative_offset == lfh->offset);
assert(!pure_overflow(cdh->relative_offset, lfh->length, cdh->offset));
const uint8_t* symlink = buffer + cdh->relative_offset + lfh->length;
const uint64_t symlink_length = cdh->compressed_size;
// Check for PATH_MAX overflow:
if (symlink_length > PURE_PATH_MAX) return PURE_E_ZIP_SYMLINK_LENGTH;
// Check for control characters used to mask a directory traversal:
if (pure_path_control_characters(symlink, symlink_length)) {
return PURE_E_ZIP_SYMLINK_CONTROL_CHARACTERS;
}
// Check for a directory traversal:
if (pure_path_drive(symlink, symlink_length)) {
return PURE_E_ZIP_SYMLINK_TRAVERSAL_DRIVE_PATH;
}
if (pure_path_relative(symlink, symlink_length)) {
return PURE_E_ZIP_SYMLINK_TRAVERSAL_RELATIVE_PATH;
}
if (pure_path_double_dots(symlink, symlink_length)) {
return PURE_E_ZIP_SYMLINK_TRAVERSAL_DOUBLE_DOTS;
}
// Check for path component overflow:
if (pure_path_component_overflow(symlink, symlink_length)) {
return PURE_E_ZIP_SYMLINK_COMPONENT_OVERFLOW;
}
return 0;
}
int pure_zip_verify_unix_mode(const uint64_t value) {
if (value > PURE_16_BIT_MAX) return PURE_E_ZIP_UNIX_MODE_OVERFLOW;
// Detect dangerous file types:
if ((value & PURE_UNIX_MODE_MASK) == PURE_UNIX_MODE_BLK) {
return PURE_E_ZIP_UNIX_MODE_BLOCK_DEVICE;
}
if ((value & PURE_UNIX_MODE_MASK) == PURE_UNIX_MODE_CHR) {
return PURE_E_ZIP_UNIX_MODE_CHARACTER_DEVICE;
}
if ((value & PURE_UNIX_MODE_MASK) == PURE_UNIX_MODE_FIFO) {
return PURE_E_ZIP_UNIX_MODE_FIFO;
}
if ((value & PURE_UNIX_MODE_MASK) == PURE_UNIX_MODE_SOCK) {
return PURE_E_ZIP_UNIX_MODE_SOCKET;
}
// Detect dangerous permissions:
// CVE-2005-0602
// https://marc.info/?l=bugtraq&m=110960796331943&w=2
// 01000:
if (value & 512) return PURE_E_ZIP_UNIX_MODE_PERMISSIONS_STICKY;
// 02000:
if (value & 1024) return PURE_E_ZIP_UNIX_MODE_PERMISSIONS_SETGID;
// 04000:
if (value & 2048) return PURE_E_ZIP_UNIX_MODE_PERMISSIONS_SETUID;
return 0;
}
int pure_zip_verify_time(const uint64_t value) {
/*
An MS-DOS time is a packed 16-bit (2 bytes) value in which bits in the value
represent the hour, minute, and second:
Bits: 0-4 5-10 11-15
Unit: Second Minute Hour
Range: 0-59 0-59 0-23
*/
if (value > PURE_16_BIT_MAX) return PURE_E_ZIP_TIME_OVERFLOW;
uint64_t h = (value >> 11);
uint64_t m = (value >> 5) & 63;
uint64_t s = (value & 31) * 2;
if (h > 23) return PURE_E_ZIP_TIME_HOUR_OVERFLOW;
if (m > 59) return PURE_E_ZIP_TIME_MINUTE_OVERFLOW;
if (s > 59) return PURE_E_ZIP_TIME_SECOND_OVERFLOW;
return 0;
}
int pure_zip_verify_lfh(const pure_zip_lfh* header) {
assert(
header->length == (
PURE_ZIP_LFH_MIN +
header->file_name_length +
header->extra_field_length
)
);
{
int error = pure_zip_verify_flags(header->general_purpose_bit_flag);
if (error) return error;
}
{
int error = pure_zip_verify_compression_method_sizes(
header->compression_method,
header->compressed_size,
header->uncompressed_size
);
if (error) return error;
}
{
int error = pure_zip_verify_date(header->last_mod_file_date);
if (error) return error;
}
{
int error = pure_zip_verify_time(header->last_mod_file_time);
if (error) return error;
}
{
int error = pure_zip_verify_file_name(
header->file_name,
header->file_name_length
);
if (error) return error;
}
{
int error = pure_zip_verify_string(
header->file_name,
header->file_name_length,
header->general_purpose_bit_flag & PURE_ZIP_FLAG_UTF8
);
if (error) return error;
}
{
int error = pure_zip_verify_extra_field(
header->extra_field,
header->extra_field_length,
header->file_name,
header->file_name_length
);
if (error) return error;
}
assert(header->zip64 == 0 || header->zip64 == 1);
return 0;
}
int pure_zip_verify_cdh(const pure_zip_cdh* header) {
assert(
header->length == (
PURE_ZIP_CDH_MIN +
header->file_name_length +
header->extra_field_length +
header->file_comment_length
)
);
{
int error = pure_zip_verify_flags(header->general_purpose_bit_flag);
if (error) return error;
}
{
int error = pure_zip_verify_compression_method_sizes(
header->compression_method,
header->compressed_size,
header->uncompressed_size
);
if (error) return error;
}
// An LFH may have a zero compressed size with a non-zero uncompressed size
// because the actual sizes may be in the DDR, but a CDH cannot be ex nihilo.
// We cross-check the DDR against the CDH, so this check applies to the DDR:
if (header->compressed_size == 0 && header->uncompressed_size != 0) {
return PURE_E_ZIP_EX_NIHILO;
}
{
int error = pure_zip_verify_date(header->last_mod_file_date);
if (error) return error;
}
{
int error = pure_zip_verify_time(header->last_mod_file_time);
if (error) return error;
}
{
int error = pure_zip_verify_file_name(
header->file_name,
header->file_name_length
);
if (error) return error;
}
{
int error = pure_zip_verify_string(
header->file_name,
header->file_name_length,
header->general_purpose_bit_flag & PURE_ZIP_FLAG_UTF8
);
if (error) return error;
}
{
int error = pure_zip_verify_extra_field(
header->extra_field,
header->extra_field_length,
header->file_name,
header->file_name_length
);
if (error) return error;
}
{
int error = pure_zip_verify_string(
header->file_comment,
header->file_comment_length,
header->general_purpose_bit_flag & PURE_ZIP_FLAG_UTF8
);
if (error) return error;
}
{
int error = pure_zip_verify_disk(header->disk);
if (error) return error;
}
{
int error = pure_zip_verify_unix_mode(header->unix_mode);
if (error) return error;
}
{
int error = pure_zip_verify_directory(header);
if (error) return error;
}
assert(header->zip64 == 0 || header->zip64 == 1);
return 0;
}
int pure_zip_decode_eief_64(
const uint8_t* extra_field,
const uint64_t length,
uint64_t* compressed_size,
uint64_t* uncompressed_size,
uint64_t* relative_offset,
uint64_t* disk,
uint8_t* zip64,
uint8_t lfh
) {
assert(
*compressed_size == PURE_32_BIT_MAX ||
*uncompressed_size == PURE_32_BIT_MAX ||
*relative_offset == PURE_32_BIT_MAX ||
*disk == PURE_16_BIT_MAX
);
assert(*zip64 == 0);
assert(lfh == 0 || lfh == 1);
if (length > PURE_ZIP_EXTRA_FIELD_MAX) return PURE_E_ZIP_EXTRA_FIELD_MAX;
if (length != 0 && length < 4) return PURE_E_ZIP_EXTRA_FIELD_MIN;
uint64_t offset = 0;
while (offset + 4 <= length) {
const uint16_t id = pure_u16(extra_field + offset + 0);
const uint16_t size = pure_u16(extra_field + offset + 2);
offset += 4;
if (offset + size > length) {
return PURE_E_ZIP_EXTRA_FIELD_ATTRIBUTE_OVERFLOW;
}
if (id == 0x0001) {
*zip64 = 1;
uint64_t index = 0;
if (*uncompressed_size == PURE_32_BIT_MAX) {
if ((index += 8) > size) return PURE_E_ZIP_EIEF_64_UNCOMPRESSED_SIZE;
*uncompressed_size = pure_u64(extra_field + offset + index - 8);
}
if (*compressed_size == PURE_32_BIT_MAX) {
if ((index += 8) > size) return PURE_E_ZIP_EIEF_64_COMPRESSED_SIZE;
*compressed_size = pure_u64(extra_field + offset + index - 8);
}
if (*relative_offset == PURE_32_BIT_MAX) {
if ((index += 8) > size) return PURE_E_ZIP_EIEF_64_RELATIVE_OFFSET;
*relative_offset = pure_u64(extra_field + offset + index - 8);
}
if (*disk == PURE_16_BIT_MAX) {
if ((index += 4) > size) return PURE_E_ZIP_EIEF_64_DISK;
*disk = pure_u32(extra_field + offset + index - 4);
}
assert(offset + size <= length);
if (index < size) {
if (pure_zeroes(extra_field, offset + index, offset + size)) {
return PURE_E_ZIP_EIEF_64_UNDERFLOW_ZEROED;
} else {
return PURE_E_ZIP_EIEF_64_UNDERFLOW_BUFFER_BLEED;
}
}
// The EIEF in an LFH must include both uncompressed and compressed size:
if (lfh && index != 16) return PURE_E_ZIP_EIEF_64_LFH;
assert(index == size);
return 0;
}
offset += size;
}
assert(offset <= length);
return 0;
}
int pure_zip_decode_lfh(
const uint8_t* buffer,
const uint64_t size,
const uint64_t offset,
pure_zip_lfh* header
) {
assert(offset < size);
if (pure_overflow(offset, PURE_ZIP_LFH_MIN, size)) {
return PURE_E_ZIP_LFH_OVERFLOW;
}
if (!pure_eq(buffer, size, offset, PURE_S_ZIP_LFH, PURE_L_ZIP_LFH)) {
return PURE_E_ZIP_LFH_SIGNATURE;
}
header->offset = offset;
assert(PURE_L_ZIP_LFH == 4);
header->version_minimum = pure_u16(buffer + offset + 4);
header->general_purpose_bit_flag = pure_u16(buffer + offset + 6);
header->compression_method = pure_u16(buffer + offset + 8);
header->last_mod_file_time = pure_u16(buffer + offset + 10);
header->last_mod_file_date = pure_u16(buffer + offset + 12);
header->crc32 = pure_u32(buffer + offset + 14);
header->compressed_size = pure_u32(buffer + offset + 18);
header->uncompressed_size = pure_u32(buffer + offset + 22);
header->file_name_length = pure_u16(buffer + offset + 26);
header->extra_field_length = pure_u16(buffer + offset + 28);
assert(PURE_ZIP_LFH_MIN == 30);
header->length = PURE_ZIP_LFH_MIN;
header->file_name = (uint8_t*) (buffer + header->offset + header->length);
header->length += header->file_name_length;
if (pure_overflow(header->offset, header->length, size)) {
return PURE_E_ZIP_LFH_FILE_NAME_OVERFLOW;
}
header->extra_field = (uint8_t*) (buffer + header->offset + header->length);
header->length += header->extra_field_length;
if (pure_overflow(header->offset, header->length, size)) {
return PURE_E_ZIP_LFH_EXTRA_FIELD_OVERFLOW;
}
// ZIP64:
header->zip64 = 0;
if (
header->compressed_size == PURE_32_BIT_MAX ||
header->uncompressed_size == PURE_32_BIT_MAX
) {
uint64_t relative_offset = 0;
uint64_t disk = 0;
int error = pure_zip_decode_eief_64(
header->extra_field,
header->extra_field_length,
&header->compressed_size,
&header->uncompressed_size,
&relative_offset,
&disk,
&header->zip64,
1
);
if (error) return error;
}
{
int error = pure_zip_verify_lfh(header);
if (error) return error;
}
return 0;
}
int pure_zip_decode_ddr(
const uint8_t* buffer,
const uint64_t size,
uint64_t offset,
pure_zip_ddr* header
) {
assert(offset < size);
assert(header->zip64 == 0 || header->zip64 == 1);
const uint64_t min = header->zip64 ? PURE_ZIP_DDR_64_MIN : PURE_ZIP_DDR_MIN;
// The DDR signature is optional but we expect at least 4 bytes regardless:
if (pure_overflow(offset, PURE_L_ZIP_DDR, size)) {
return PURE_E_ZIP_DDR_OVERFLOW;
}
if (pure_eq(buffer, size, offset, PURE_S_ZIP_DDR, PURE_L_ZIP_DDR)) {
header->offset = offset;
header->length = PURE_L_ZIP_DDR + min;
offset += PURE_L_ZIP_DDR;
} else {
header->offset = offset;
header->length = min;
}
if (pure_overflow(offset, min, size)) return PURE_E_ZIP_DDR_OVERFLOW;
if (header->zip64) {
assert(min == 4 + 8 + 8);
header->crc32 = pure_u32(buffer + offset + 0);
header->compressed_size = pure_u64(buffer + offset + 4);
header->uncompressed_size = pure_u64(buffer + offset + 12);
} else {
assert(min == 4 + 4 + 4);
header->crc32 = pure_u32(buffer + offset + 0);
header->compressed_size = pure_u32(buffer + offset + 4);
header->uncompressed_size = pure_u32(buffer + offset + 8);
}
return 0;
}
int pure_zip_decode_cdh(
const uint8_t* buffer,
const uint64_t size,
const uint64_t offset,
pure_zip_cdh* header
) {
assert(offset < size);
if (pure_overflow(offset, PURE_ZIP_CDH_MIN, size)) {
return PURE_E_ZIP_CDH_OVERFLOW;
}
if (!pure_eq(buffer, size, offset, PURE_S_ZIP_CDH, PURE_L_ZIP_CDH)) {
return PURE_E_ZIP_CDH_SIGNATURE;
}
header->offset = offset;
assert(PURE_L_ZIP_CDH == 4);
header->version_made = pure_u16(buffer + offset + 4);
header->version_minimum = pure_u16(buffer + offset + 6);
header->general_purpose_bit_flag = pure_u16(buffer + offset + 8);
header->compression_method = pure_u16(buffer + offset + 10);
header->last_mod_file_time = pure_u16(buffer + offset + 12);
header->last_mod_file_date = pure_u16(buffer + offset + 14);
header->crc32 = pure_u32(buffer + offset + 16);
header->compressed_size = pure_u32(buffer + offset + 20);
header->uncompressed_size = pure_u32(buffer + offset + 24);
header->file_name_length = pure_u16(buffer + offset + 28);
header->extra_field_length = pure_u16(buffer + offset + 30);
header->file_comment_length = pure_u16(buffer + offset + 32);
header->disk = pure_u16(buffer + offset + 34);
header->internal_file_attributes = pure_u16(buffer + offset + 36);
header->external_file_attributes = pure_u32(buffer + offset + 38);
header->relative_offset = pure_u32(buffer + offset + 42);
assert(PURE_ZIP_CDH_MIN == 46);
header->length = PURE_ZIP_CDH_MIN;
header->file_name = (uint8_t*) (buffer + header->offset + header->length);
header->length += header->file_name_length;
if (pure_overflow(header->offset, header->length, size)) {
return PURE_E_ZIP_CDH_FILE_NAME_OVERFLOW;
}
header->extra_field = (uint8_t*) (buffer + header->offset + header->length);
header->length += header->extra_field_length;
if (pure_overflow(header->offset, header->length, size)) {
return PURE_E_ZIP_CDH_EXTRA_FIELD_OVERFLOW;
}
header->file_comment = (uint8_t*) (buffer + header->offset + header->length);
header->length += header->file_comment_length;
if (pure_overflow(header->offset, header->length, size)) {
return PURE_E_ZIP_CDH_FILE_COMMENT_OVERFLOW;
}
header->unix_mode = 0;
if ((header->version_made >> 8) == PURE_ZIP_VERSION_MADE_UNIX) {
header->unix_mode = header->external_file_attributes >> 16;
}
header->directory = (
(header->unix_mode & PURE_UNIX_MODE_MASK) == PURE_UNIX_MODE_DIR ||
(header->external_file_attributes & 0x0010) ||
(
header->file_name_length > 0 &&
header->file_name[header->file_name_length - 1] == PURE_FORWARD_SLASH
)
);
// ZIP64:
header->zip64 = 0;
if (
header->compressed_size == PURE_32_BIT_MAX ||
header->uncompressed_size == PURE_32_BIT_MAX ||
header->relative_offset == PURE_32_BIT_MAX ||
header->disk == PURE_16_BIT_MAX
) {
int error = pure_zip_decode_eief_64(
header->extra_field,
header->extra_field_length,
&header->compressed_size,
&header->uncompressed_size,
&header->relative_offset,
&header->disk,
&header->zip64,
0
);
if (error) return error;
}
// These are critical checks and must not be moved to pure_zip_verify_cdh():
if (header->relative_offset > size) {
return PURE_E_ZIP_CDH_RELATIVE_OFFSET_OVERFLOW;
}
if (header->relative_offset > offset) {
return PURE_E_ZIP_CDH_RELATIVE_OFFSET_OVERLAP;
}
{
int error = pure_zip_verify_cdh(header);
if (error) return error;
}
return 0;
}
int pure_zip_decode_eocdl_64(
const uint8_t* buffer,