forked from curl/curl-www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCVE-2016-0754_v3_curl-7.47.0.patch
849 lines (801 loc) · 27 KB
/
CVE-2016-0754_v3_curl-7.47.0.patch
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
From 99fede1b5ca0dd734d534eea747140449310c86d Mon Sep 17 00:00:00 2001
From: Jay Satiro <[email protected]>
Date: Thu, 11 Feb 2016 17:03:08 -0500
Subject: [PATCH] curl: Consolidated patch v3 for CVE-2016-0754 (Windows)
curl does not sanitize colons in a remote file name that is used as the
local file name. This may lead to a vulnerability on systems where the
colon is a special path character. Currently Windows/DOS is the only OS
where this vulnerability applies.
Bug: https://curl.se/docs/adv_20160127B.html
Instructions
------------
This patch will only apply to curl 7.47.0. If you have a different
version of curl consult the advisory.
If you have applied an older version of the patch revert it and then
apply v3.
patch -p1 < CVE-2016-0754_v3_curl-7.47.0.patch
---
src/tool_cb_hdr.c | 39 +++--
src/tool_doswin.c | 453 +++++++++++++++++++++++++++++++++++++++++------------
src/tool_doswin.h | 24 ++-
src/tool_operate.c | 20 ---
src/tool_operhlp.c | 18 ++-
src/tool_urlglob.c | 16 ++
6 files changed, 435 insertions(+), 135 deletions(-)
diff --git a/src/tool_cb_hdr.c b/src/tool_cb_hdr.c
index 0fca39f..d603cac 100644
--- a/src/tool_cb_hdr.c
+++ b/src/tool_cb_hdr.c
@@ -115,24 +115,18 @@ size_t tool_header_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
*/
len = (ssize_t)cb - (p - str);
filename = parse_filename(p, len);
- if(!filename)
- return failure;
-
-#if defined(MSDOS) || defined(WIN32)
- if(sanitize_file_name(&filename)) {
- free(filename);
- return failure;
+ if(filename) {
+ outs->filename = filename;
+ outs->alloc_filename = TRUE;
+ outs->is_cd_filename = TRUE;
+ outs->s_isreg = TRUE;
+ outs->fopened = FALSE;
+ outs->stream = NULL;
+ hdrcbdata->honor_cd_filename = FALSE;
+ break;
}
-#endif /* MSDOS || WIN32 */
-
- outs->filename = filename;
- outs->alloc_filename = TRUE;
- outs->is_cd_filename = TRUE;
- outs->s_isreg = TRUE;
- outs->fopened = FALSE;
- outs->stream = NULL;
- hdrcbdata->honor_cd_filename = FALSE;
- break;
+ else
+ return failure;
}
}
@@ -207,6 +201,17 @@ static char *parse_filename(const char *ptr, size_t len)
if(copy != p)
memmove(copy, p, strlen(p) + 1);
+#if defined(MSDOS) || defined(WIN32)
+ {
+ char *sanitized;
+ SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
+ Curl_safefree(copy);
+ if(sc)
+ return NULL;
+ copy = sanitized;
+ }
+#endif /* MSDOS || WIN32 */
+
/* in case we built debug enabled, we allow an evironment variable
* named CURL_TESTDIR to prefix the given file name to put it into a
* specific directory
diff --git a/src/tool_doswin.c b/src/tool_doswin.c
index 9c6a7a3..a346644 100644
--- a/src/tool_doswin.c
+++ b/src/tool_doswin.c
@@ -85,50 +85,120 @@ __pragma(warning(pop))
# include <fcntl.h> /* _use_lfn(f) prototype */
#endif
-static char *msdosify(const char *file_name);
-static char *rename_if_dos_device_name(const char *file_name);
+#ifndef UNITTESTS
+static SANITIZEcode truncate_dryrun(const char *path,
+ const size_t truncate_pos);
+#ifdef MSDOS
+static SANITIZEcode msdosify(char **const sanitized, const char *file_name,
+ int flags);
+#endif
+static SANITIZEcode rename_if_reserved_dos_device_name(char **const sanitized,
+ const char *file_name,
+ int flags);
+#endif /* !UNITTESTS (static declarations used if no unit tests) */
/*
-Sanitize *file_name.
-Success: (CURLE_OK) *file_name points to a sanitized version of the original.
- This function takes ownership of the original *file_name and frees it.
-Failure: (!= CURLE_OK) *file_name is unchanged.
+Sanitize a file or path name.
+
+All banned characters are replaced by underscores, for example:
+f?*foo => f__foo
+f:foo::$DATA => f_foo__$DATA
+f:\foo:bar => f__foo_bar
+f:\foo:bar => f:\foo:bar (flag SANITIZE_ALLOW_PATH)
+
+This function was implemented according to the guidelines in 'Naming Files,
+Paths, and Namespaces' section 'Naming Conventions'.
+https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx
+
+Flags
+-----
+SANITIZE_ALLOW_COLONS: Allow colons.
+Without this flag colons are sanitized.
+
+SANITIZE_ALLOW_PATH: Allow path separators and colons.
+Without this flag path separators and colons are sanitized.
+
+SANITIZE_ALLOW_RESERVED: Allow reserved device names.
+Without this flag a reserved device name is renamed (COM1 => _COM1) unless it's
+in a UNC prefixed path.
+
+SANITIZE_ALLOW_TRUNCATE: Allow truncating a long filename.
+Without this flag if the sanitized filename or path will be too long an error
+occurs. With this flag the filename --and not any other parts of the path-- may
+be truncated to at least a single character. A filename followed by an
+alternate data stream (ADS) cannot be truncated in any case.
+
+Success: (SANITIZE_ERR_OK) *sanitized points to a sanitized copy of file_name.
+Failure: (!= SANITIZE_ERR_OK) *sanitized is NULL.
*/
-CURLcode sanitize_file_name(char **file_name)
+SANITIZEcode sanitize_file_name(char **const sanitized, const char *file_name,
+ int flags)
{
+ char *p, *target;
size_t len;
- char *p, *sanitized;
+ SANITIZEcode sc;
+ size_t max_sanitized_len;
- /* Calculate the maximum length of a filename.
- FILENAME_MAX is often the same as PATH_MAX, in other words it does not
- discount the path information. PATH_MAX size is calculated based on:
- <drive-letter><colon><path-sep><max-filename-len><NULL> */
- const size_t max_filename_len = PATH_MAX - 3 - 1;
+ if(!sanitized)
+ return SANITIZE_ERR_BAD_ARGUMENT;
- if(!file_name || !*file_name)
- return CURLE_BAD_FUNCTION_ARGUMENT;
+ *sanitized = NULL;
- len = strlen(*file_name);
+ if(!file_name)
+ return SANITIZE_ERR_BAD_ARGUMENT;
- if(len >= max_filename_len)
- len = max_filename_len - 1;
+ if((flags & SANITIZE_ALLOW_PATH)) {
+#ifndef MSDOS
+ if(file_name[0] == '\\' && file_name[1] == '\\')
+ /* UNC prefixed path \\ (eg \\?\C:\foo) */
+ max_sanitized_len = 32767-1;
+ else
+#endif
+ max_sanitized_len = PATH_MAX-1;
+ }
+ else
+ /* The maximum length of a filename.
+ FILENAME_MAX is often the same as PATH_MAX, in other words it is 260 and
+ does not discount the path information therefore we shouldn't use it. */
+ max_sanitized_len = (PATH_MAX-1 > 255) ? 255 : PATH_MAX-1;
+
+ len = strlen(file_name);
+ if(len > max_sanitized_len) {
+ if(!(flags & SANITIZE_ALLOW_TRUNCATE) ||
+ truncate_dryrun(file_name, max_sanitized_len))
+ return SANITIZE_ERR_INVALID_PATH;
+
+ len = max_sanitized_len;
+ }
- sanitized = malloc(len + 1);
+ target = malloc(len + 1);
+ if(!target)
+ return SANITIZE_ERR_OUT_OF_MEMORY;
- if(!sanitized)
- return CURLE_OUT_OF_MEMORY;
+ strncpy(target, file_name, len);
+ target[len] = '\0';
- strncpy(sanitized, *file_name, len);
- sanitized[len] = '\0';
+#ifndef MSDOS
+ if((flags & SANITIZE_ALLOW_PATH) && !strncmp(target, "\\\\?\\", 4))
+ /* Skip the literal path prefix \\?\ */
+ p = target + 4;
+ else
+#endif
+ p = target;
- for(p = sanitized; *p; ++p ) {
+ /* replace control characters and other banned characters */
+ for(; *p; ++p) {
const char *banned;
- if(1 <= *p && *p <= 31) {
+
+ if((1 <= *p && *p <= 31) ||
+ (!(flags & (SANITIZE_ALLOW_COLONS|SANITIZE_ALLOW_PATH)) && *p == ':') ||
+ (!(flags & SANITIZE_ALLOW_PATH) && (*p == '/' || *p == '\\'))) {
*p = '_';
continue;
}
- for(banned = "|<>/\\\":?*"; *banned; ++banned) {
+
+ for(banned = "|<>\"?*"; *banned; ++banned) {
if(*p == *banned) {
*p = '_';
break;
@@ -136,39 +206,111 @@ CURLcode sanitize_file_name(char **file_name)
}
}
+ /* remove trailing spaces and periods if not allowing paths */
+ if(!(flags & SANITIZE_ALLOW_PATH) && len) {
+ char *clip = NULL;
+
+ p = &target[len];
+ do {
+ --p;
+ if(*p != ' ' && *p != '.')
+ break;
+ clip = p;
+ } while(p != target);
+
+ if(clip) {
+ *clip = '\0';
+ len = clip - target;
+ }
+ }
+
#ifdef MSDOS
- /* msdosify checks for more banned characters for MSDOS, however it allows
- for some path information to pass through. since we are sanitizing only a
- filename and cannot allow a path it's important this call be done in
- addition to and not instead of the banned character check above. */
- p = msdosify(sanitized);
- if(!p) {
- free(sanitized);
- return CURLE_BAD_FUNCTION_ARGUMENT;
+ sc = msdosify(&p, target, flags);
+ free(target);
+ if(sc)
+ return sc;
+ target = p;
+ len = strlen(target);
+
+ if(len > max_sanitized_len) {
+ free(target);
+ return SANITIZE_ERR_INVALID_PATH;
}
- sanitized = p;
- len = strlen(sanitized);
#endif
- p = rename_if_dos_device_name(sanitized);
- if(!p) {
- free(sanitized);
- return CURLE_BAD_FUNCTION_ARGUMENT;
+ if(!(flags & SANITIZE_ALLOW_RESERVED)) {
+ sc = rename_if_reserved_dos_device_name(&p, target, flags);
+ free(target);
+ if(sc)
+ return sc;
+ target = p;
+ len = strlen(target);
+
+ if(len > max_sanitized_len) {
+ free(target);
+ return SANITIZE_ERR_INVALID_PATH;
+ }
}
- sanitized = p;
- len = strlen(sanitized);
-
- /* dos_device_name rename will rename a device name, possibly changing the
- length. If the length is too long now we can't truncate it because we
- could end up with a device name. In practice this shouldn't be a problem
- because device names are short, but you never know. */
- if(len >= max_filename_len) {
- free(sanitized);
- return CURLE_BAD_FUNCTION_ARGUMENT;
+
+ *sanitized = target;
+ return SANITIZE_ERR_OK;
+}
+
+
+/*
+Test if truncating a path to a file will leave at least a single character in
+the filename. Filenames suffixed by an alternate data stream can't be
+truncated. This performs a dry run, nothing is modified.
+
+Good truncate_pos 9: C:\foo\bar => C:\foo\ba
+Good truncate_pos 6: C:\foo => C:\foo
+Good truncate_pos 5: C:\foo => C:\fo
+Bad* truncate_pos 5: C:foo => C:foo
+Bad truncate_pos 5: C:\foo:ads => C:\fo
+Bad truncate_pos 9: C:\foo:ads => C:\foo:ad
+Bad truncate_pos 5: C:\foo\bar => C:\fo
+Bad truncate_pos 5: C:\foo\ => C:\fo
+Bad truncate_pos 7: C:\foo\ => C:\foo\
+Error truncate_pos 7: C:\foo => (pos out of range)
+Bad truncate_pos 1: C:\foo\ => C
+
+* C:foo is ambiguous, C could end up being a drive or file therefore something
+ like C:superlongfilename can't be truncated.
+
+Returns
+SANITIZE_ERR_OK: Good -- 'path' can be truncated
+SANITIZE_ERR_INVALID_PATH: Bad -- 'path' cannot be truncated
+!= SANITIZE_ERR_OK && != SANITIZE_ERR_INVALID_PATH: Error
+*/
+SANITIZEcode truncate_dryrun(const char *path, const size_t truncate_pos)
+{
+ size_t len;
+
+ if(!path)
+ return SANITIZE_ERR_BAD_ARGUMENT;
+
+ len = strlen(path);
+
+ if(truncate_pos > len)
+ return SANITIZE_ERR_BAD_ARGUMENT;
+
+ if(!len || !truncate_pos)
+ return SANITIZE_ERR_INVALID_PATH;
+
+ if(strpbrk(&path[truncate_pos - 1], "\\/:"))
+ return SANITIZE_ERR_INVALID_PATH;
+
+ /* C:\foo can be truncated but C:\foo:ads can't */
+ if(truncate_pos > 1) {
+ const char *p = &path[truncate_pos - 1];
+ do {
+ --p;
+ if(*p == ':')
+ return SANITIZE_ERR_INVALID_PATH;
+ } while(p != path && *p != '\\' && *p != '/');
}
- *file_name = sanitized;
- return CURLE_OK;
+ return SANITIZE_ERR_OK;
}
/* The functions msdosify, rename_if_dos_device_name and __crt0_glob_function
@@ -178,15 +320,24 @@ CURLcode sanitize_file_name(char **file_name)
/*
Extra sanitization MSDOS for file_name.
-Returns a copy of file_name that is sanitized by MSDOS standards.
-Warning: path information may pass through. For sanitizing a filename use
-sanitize_file_name which calls this function after sanitizing path info.
+
+This is a supporting function for sanitize_file_name.
+
+Warning: This is an MSDOS legacy function and was purposely written in a way
+that some path information may pass through. For example drive letter names
+(C:, D:, etc) are allowed to pass through. For sanitizing a filename use
+sanitize_file_name.
+
+Success: (SANITIZE_ERR_OK) *sanitized points to a sanitized copy of file_name.
+Failure: (!= SANITIZE_ERR_OK) *sanitized is NULL.
*/
-static char *msdosify(const char *file_name)
+#if defined(MSDOS) || defined(UNITTESTS)
+SANITIZEcode msdosify(char **const sanitized, const char *file_name,
+ int flags)
{
char dos_name[PATH_MAX];
static const char illegal_chars_dos[] = ".+, ;=[]" /* illegal in DOS */
- "|<>\\\":?*"; /* illegal in DOS & W95 */
+ "|<>/\\\":?*"; /* illegal in DOS & W95 */
static const char *illegal_chars_w95 = &illegal_chars_dos[8];
int idx, dot_idx;
const char *s = file_name;
@@ -195,6 +346,19 @@ static char *msdosify(const char *file_name)
const char *illegal_aliens = illegal_chars_dos;
size_t len = sizeof(illegal_chars_dos) - 1;
+ if(!sanitized)
+ return SANITIZE_ERR_BAD_ARGUMENT;
+
+ *sanitized = NULL;
+
+ if(!file_name)
+ return SANITIZE_ERR_BAD_ARGUMENT;
+
+ if(strlen(file_name) > PATH_MAX-1 &&
+ (!(flags & SANITIZE_ALLOW_TRUNCATE) ||
+ truncate_dryrun(file_name, PATH_MAX-1)))
+ return SANITIZE_ERR_INVALID_PATH;
+
/* Support for Windows 9X VFAT systems, when available. */
if(_use_lfn(file_name)) {
illegal_aliens = illegal_chars_w95;
@@ -204,22 +368,35 @@ static char *msdosify(const char *file_name)
/* Get past the drive letter, if any. */
if(s[0] >= 'A' && s[0] <= 'z' && s[1] == ':') {
*d++ = *s++;
- *d++ = *s++;
+ *d = ((flags & (SANITIZE_ALLOW_COLONS|SANITIZE_ALLOW_PATH))) ? ':' : '_';
+ ++d, ++s;
}
for(idx = 0, dot_idx = -1; *s && d < dlimit; s++, d++) {
if(memchr(illegal_aliens, *s, len)) {
+
+ if((flags & (SANITIZE_ALLOW_COLONS|SANITIZE_ALLOW_PATH)) && *s == ':')
+ *d = ':';
+ else if((flags & SANITIZE_ALLOW_PATH) && (*s == '/' || *s == '\\'))
+ *d = *s;
/* Dots are special: DOS doesn't allow them as the leading character,
and a file name cannot have more than a single dot. We leave the
first non-leading dot alone, unless it comes too close to the
beginning of the name: we want sh.lex.c to become sh_lex.c, not
sh.lex-c. */
- if(*s == '.') {
- if(idx == 0 && (s[1] == '/' || (s[1] == '.' && s[2] == '/'))) {
+ else if(*s == '.') {
+ if((flags & SANITIZE_ALLOW_PATH) && idx == 0 &&
+ (s[1] == '/' || s[1] == '\\' ||
+ (s[1] == '.' && (s[2] == '/' || s[2] == '\\')))) {
/* Copy "./" and "../" verbatim. */
*d++ = *s++;
- if(*s == '.')
+ if(d == dlimit)
+ break;
+ if(*s == '.') {
*d++ = *s++;
+ if(d == dlimit)
+ break;
+ }
*d = *s;
}
else if(idx == 0)
@@ -241,12 +418,22 @@ static char *msdosify(const char *file_name)
else if(*s == '+' && s[1] == '+') {
if(idx - 2 == dot_idx) { /* .c++, .h++ etc. */
*d++ = 'x';
+ if(d == dlimit)
+ break;
*d = 'x';
}
else {
/* libg++ etc. */
- memcpy (d, "plus", 4);
- d += 3;
+ if(dlimit - d < 4) {
+ *d++ = 'x';
+ if(d == dlimit)
+ break;
+ *d = 'x';
+ }
+ else {
+ memcpy (d, "plus", 4);
+ d += 3;
+ }
}
s++;
idx++;
@@ -256,55 +443,90 @@ static char *msdosify(const char *file_name)
}
else
*d = *s;
- if(*s == '/') {
+ if(*s == '/' || *s == '\\') {
idx = 0;
dot_idx = -1;
}
else
idx++;
}
-
*d = '\0';
- return strdup(dos_name);
+
+ if(*s) {
+ /* dos_name is truncated, check that truncation requirements are met,
+ specifically truncating a filename suffixed by an alternate data stream
+ or truncating the entire filename is not allowed. */
+ if(!(flags & SANITIZE_ALLOW_TRUNCATE) || strpbrk(s, "\\/:") ||
+ truncate_dryrun(dos_name, d - dos_name))
+ return SANITIZE_ERR_INVALID_PATH;
+ }
+
+ *sanitized = strdup(dos_name);
+ return (*sanitized ? SANITIZE_ERR_OK : SANITIZE_ERR_OUT_OF_MEMORY);
}
+#endif /* MSDOS || UNITTESTS */
/*
-Rename file_name if it's a representation of a device name.
-Returns a copy of file_name, and the copy will have contents different from the
-original if a device name was found.
+Rename file_name if it's a reserved dos device name.
+
+This is a supporting function for sanitize_file_name.
+
+Warning: This is an MSDOS legacy function and was purposely written in a way
+that some path information may pass through. For example drive letter names
+(C:, D:, etc) are allowed to pass through. For sanitizing a filename use
+sanitize_file_name.
+
+Success: (SANITIZE_ERR_OK) *sanitized points to a sanitized copy of file_name.
+Failure: (!= SANITIZE_ERR_OK) *sanitized is NULL.
*/
-static char *rename_if_dos_device_name(const char *file_name)
+SANITIZEcode rename_if_reserved_dos_device_name(char **const sanitized,
+ const char *file_name,
+ int flags)
{
/* We could have a file whose name is a device on MS-DOS. Trying to
* retrieve such a file would fail at best and wedge us at worst. We need
* to rename such files. */
char *p, *base;
- struct_stat st_buf;
char fname[PATH_MAX];
+#ifdef MSDOS
+ struct_stat st_buf;
+#endif
+
+ if(!sanitized)
+ return SANITIZE_ERR_BAD_ARGUMENT;
+
+ *sanitized = NULL;
+
+ if(!file_name)
+ return SANITIZE_ERR_BAD_ARGUMENT;
+
+ /* Ignore UNC prefixed paths, they are allowed to contain a reserved name. */
+#ifndef MSDOS
+ if((flags & SANITIZE_ALLOW_PATH) &&
+ file_name[0] == '\\' && file_name[1] == '\\') {
+ size_t len = strlen(file_name);
+ *sanitized = malloc(len + 1);
+ if(!*sanitized)
+ return SANITIZE_ERR_OUT_OF_MEMORY;
+ strncpy(*sanitized, file_name, len + 1);
+ return SANITIZE_ERR_OK;
+ }
+#endif
+
+ if(strlen(file_name) > PATH_MAX-1 &&
+ (!(flags & SANITIZE_ALLOW_TRUNCATE) ||
+ truncate_dryrun(file_name, PATH_MAX-1)))
+ return SANITIZE_ERR_INVALID_PATH;
strncpy(fname, file_name, PATH_MAX-1);
fname[PATH_MAX-1] = '\0';
base = basename(fname);
- if(((stat(base, &st_buf)) == 0) && (S_ISCHR(st_buf.st_mode))) {
- size_t blen = strlen(base);
-
- if(strlen(fname) == PATH_MAX-1) {
- /* Make room for the '_' */
- blen--;
- base[blen] = '\0';
- }
- /* Prepend a '_'. */
- memmove(base + 1, base, blen + 1);
- base[0] = '_';
- }
-
- /* The above stat check does not identify devices for me in Windows 7. For
- example a stat on COM1 returns a regular file S_IFREG. According to MSDN
- stat doc that is the correct behavior, so I assume the above code is
- legacy, maybe MSDOS or DJGPP specific? */
- /* Rename devices.
- Examples: CON => _CON, CON.EXT => CON_EXT, CON:ADS => CON_ADS */
+ /* Rename reserved device names that are known to be accessible without \\.\
+ Examples: CON => _CON, CON.EXT => CON_EXT, CON:ADS => CON_ADS
+ https://support.microsoft.com/en-us/kb/74496
+ https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx
+ */
for(p = fname; p; p = (p == fname && fname != base ? base : NULL)) {
size_t p_len;
int x = (curl_strnequal(p, "CON", 3) ||
@@ -319,31 +541,70 @@ static char *rename_if_dos_device_name(const char *file_name)
continue;
/* the devices may be accessible with an extension or ADS, for
- example CON.AIR and CON:AIR both access console */
- if(p[x] == '.' || p[x] == ':') {
+ example CON.AIR and 'CON . AIR' and CON:AIR access console */
+
+ for(; p[x] == ' '; ++x)
+ ;
+
+ if(p[x] == '.') {
p[x] = '_';
continue;
}
+ else if(p[x] == ':') {
+ if(!(flags & (SANITIZE_ALLOW_COLONS|SANITIZE_ALLOW_PATH))) {
+ p[x] = '_';
+ continue;
+ }
+ ++x;
+ }
else if(p[x]) /* no match */
continue;
+ /* p points to 'CON' or 'CON ' or 'CON:', etc */
p_len = strlen(p);
+ /* Prepend a '_' */
if(strlen(fname) == PATH_MAX-1) {
- /* Make room for the '_' */
- p_len--;
+ --p_len;
+ if(!(flags & SANITIZE_ALLOW_TRUNCATE) || truncate_dryrun(p, p_len))
+ return SANITIZE_ERR_INVALID_PATH;
p[p_len] = '\0';
}
- /* Prepend a '_'. */
memmove(p + 1, p, p_len + 1);
p[0] = '_';
+ ++p_len;
/* if fname was just modified then the basename pointer must be updated */
if(p == fname)
base = basename(fname);
}
- return strdup(fname);
+ /* This is the legacy portion from rename_if_dos_device_name that checks for
+ reserved device names. It only works on MSDOS. On Windows XP the stat
+ check errors with EINVAL if the device name is reserved. On Windows
+ Vista/7/8 it sets mode S_IFREG (regular file or device). According to MSDN
+ stat doc the latter behavior is correct, but that doesn't help us identify
+ whether it's a reserved device name and not a regular file name. */
+#ifdef MSDOS
+ if(base && ((stat(base, &st_buf)) == 0) && (S_ISCHR(st_buf.st_mode))) {
+ /* Prepend a '_' */
+ size_t blen = strlen(base);
+ if(blen) {
+ if(strlen(fname) == PATH_MAX-1) {
+ --blen;
+ if(!(flags & SANITIZE_ALLOW_TRUNCATE) || truncate_dryrun(base, blen))
+ return SANITIZE_ERR_INVALID_PATH;
+ base[blen] = '\0';
+ }
+ memmove(base + 1, base, blen + 1);
+ base[0] = '_';
+ ++blen;
+ }
+ }
+#endif
+
+ *sanitized = strdup(fname);
+ return (*sanitized ? SANITIZE_ERR_OK : SANITIZE_ERR_OUT_OF_MEMORY);
}
#if defined(MSDOS) && (defined(__DJGPP__) || defined(__GO32__))
diff --git a/src/tool_doswin.h b/src/tool_doswin.h
index fc83f16..7c6aa1e 100644
--- a/src/tool_doswin.h
+++ b/src/tool_doswin.h
@@ -25,7 +25,29 @@
#if defined(MSDOS) || defined(WIN32)
-CURLcode sanitize_file_name(char **filename);
+#define SANITIZE_ALLOW_COLONS (1<<0) /* Allow colons */
+#define SANITIZE_ALLOW_PATH (1<<1) /* Allow path separators and colons */
+#define SANITIZE_ALLOW_RESERVED (1<<2) /* Allow reserved device names */
+#define SANITIZE_ALLOW_TRUNCATE (1<<3) /* Allow truncating a long filename */
+
+typedef enum {
+ SANITIZE_ERR_OK = 0, /* 0 - OK */
+ SANITIZE_ERR_INVALID_PATH, /* 1 - the path is invalid */
+ SANITIZE_ERR_BAD_ARGUMENT, /* 2 - bad function parameter */
+ SANITIZE_ERR_OUT_OF_MEMORY, /* 3 - out of memory */
+ SANITIZE_ERR_LAST /* never use! */
+} SANITIZEcode;
+
+SANITIZEcode sanitize_file_name(char **const sanitized, const char *file_name,
+ int flags);
+#ifdef UNITTESTS
+SANITIZEcode truncate_dryrun(const char *path, const size_t truncate_pos);
+SANITIZEcode msdosify(char **const sanitized, const char *file_name,
+ int flags);
+SANITIZEcode rename_if_reserved_dos_device_name(char **const sanitized,
+ const char *file_name,
+ int flags);
+#endif /* UNITTESTS */
#if defined(MSDOS) && (defined(__DJGPP__) || defined(__GO32__))
diff --git a/src/tool_operate.c b/src/tool_operate.c
index 272ebd4..8fb1d4c 100644
--- a/src/tool_operate.c
+++ b/src/tool_operate.c
@@ -543,15 +543,6 @@ static CURLcode operate_do(struct GlobalConfig *global,
result = get_url_file_name(&outfile, this_url);
if(result)
goto show_error;
-
-#if defined(MSDOS) || defined(WIN32)
- result = sanitize_file_name(&outfile);
- if(result) {
- Curl_safefree(outfile);
- goto show_error;
- }
-#endif /* MSDOS || WIN32 */
-
if(!*outfile && !config->content_disposition) {
helpf(global->errors, "Remote file name has no length!\n");
result = CURLE_WRITE_ERROR;
@@ -563,17 +554,6 @@ static CURLcode operate_do(struct GlobalConfig *global,
char *storefile = outfile;
result = glob_match_url(&outfile, storefile, urls);
Curl_safefree(storefile);
-
-#if defined(MSDOS) || defined(WIN32)
- if(!result) {
- result = sanitize_file_name(&outfile);
- if(result) {
- Curl_safefree(outfile);
- goto show_error;
- }
- }
-#endif /* MSDOS || WIN32 */
-
if(result) {
/* bad globbing */
warnf(config->global, "bad output glob!\n");
diff --git a/src/tool_operhlp.c b/src/tool_operhlp.c
index abf9496..3566520 100644
--- a/src/tool_operhlp.c
+++ b/src/tool_operhlp.c
@@ -29,6 +29,7 @@
#include "tool_cfgable.h"
#include "tool_convert.h"
+#include "tool_doswin.h"
#include "tool_operhlp.h"
#include "tool_metalink.h"
@@ -128,7 +129,7 @@ char *add_file_name_to_url(CURL *curl, char *url, const char *filename)
*/
CURLcode get_url_file_name(char **filename, const char *url)
{
- const char *pc;
+ const char *pc, *pc2;
*filename = NULL;
@@ -138,7 +139,11 @@ CURLcode get_url_file_name(char **filename, const char *url)
pc += 3;
else
pc = url;
+
+ pc2 = strrchr(pc, '\\');
pc = strrchr(pc, '/');
+ if(pc2 && (!pc || pc < pc2))
+ pc = pc2;
if(pc)
/* duplicate the string beyond the slash */
@@ -151,6 +156,17 @@ CURLcode get_url_file_name(char **filename, const char *url)
if(!*filename)
return CURLE_OUT_OF_MEMORY;
+#if defined(MSDOS) || defined(WIN32)
+ {
+ char *sanitized;
+ SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
+ Curl_safefree(*filename);
+ if(sc)
+ return CURLE_URL_MALFORMAT;
+ *filename = sanitized;
+ }
+#endif /* MSDOS || WIN32 */
+
/* in case we built debug enabled, we allow an environment variable
* named CURL_TESTDIR to prefix the given file name to put it into a
* specific directory
diff --git a/src/tool_urlglob.c b/src/tool_urlglob.c
index 1337252..145577f 100644
--- a/src/tool_urlglob.c
+++ b/src/tool_urlglob.c
@@ -24,6 +24,8 @@
#define ENABLE_CURLX_PRINTF
/* use our own printf() functions */
#include "curlx.h"
+#include "tool_cfgable.h"
+#include "tool_doswin.h"
#include "tool_urlglob.h"
#include "tool_vms.h"
@@ -666,6 +668,20 @@ CURLcode glob_match_url(char **result, char *filename, URLGlob *glob)
stringlen += appendlen;
}
target[stringlen]= '\0';
+
+#if defined(MSDOS) || defined(WIN32)
+ {
+ char *sanitized;
+ SANITIZEcode sc = sanitize_file_name(&sanitized, target,
+ (SANITIZE_ALLOW_PATH |
+ SANITIZE_ALLOW_RESERVED));
+ Curl_safefree(target);
+ if(sc)
+ return CURLE_URL_MALFORMAT;
+ target = sanitized;
+ }
+#endif /* MSDOS || WIN32 */
+
*result = target;
return CURLE_OK;
}
--
1.9.5.msysgit.1