]> sjero.net Git - wget/blob - src/string_t.c
[svn] Fix escape chars in server response vulnerability. Server response is
[wget] / src / string_t.c
1 /*  
2  *  string_t.c - dynamic string handling module
3  *  
4  *  Copyright (C) 2005 Free Software Foundation, Inc.
5  *  
6  *  This file is part of GNU Wget.
7  *  
8  *  GNU Wget is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  GNU Wget is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *  In addition, as a special exception, the Free Software Foundation
22  *  gives permission to link the code of its release of Wget with the
23  *  OpenSSL project's "OpenSSL" library (or with modified versions of it
24  *  that use the same license as the "OpenSSL" library), and distribute
25  *  the linked executables.  You must obey the GNU General Public License
26  *  in all respects for all of the code used other than "OpenSSL".  If you
27  *  modify this file, you may extend this exception to your version of the
28  *  file, but you are not obligated to do so.  If you do not wish to do
29  *  so, delete this exception statement from your version.
30  */  
31
32 #include "config.h"
33
34 #define _GNU_SOURCE             /* to get iswblank */
35
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
40 #include <wctype.h>
41
42 #include "wget.h"
43
44 #ifdef STANDALONE
45 #undef xmalloc
46 #undef xrealloc
47 #undef xfree_null
48 #define xmalloc malloc
49 #define xrealloc realloc
50 #define xfree_null(p) if (!(p)) ; else free (p)
51 #else
52 #include "xmalloc.h"
53 #endif
54
55 #ifdef WINDOWS
56 static const wchar_t w_line_delim[] = L"\r\n";
57 static const char line_delim[] = "\r\n";
58 static const unsigned int line_delim_len = 2;
59 #else
60 static const wchar_t w_line_delim[] = L"\n";
61 static const char line_delim[] = "\n";
62 static const unsigned int line_delim_len = 2;
63 #endif
64
65 typedef struct string_t {
66   char *sz;           /* standard null-terminated string */
67   unsigned int len;   /* number of chars in the allocated buffer */
68   unsigned int used;  /* number of used chars */
69 } *string;
70
71 #ifdef STRING_MODULE_DEBUG
72
73 #define assert_valid_string(str) \
74   assert (((str) != NULL) \
75           && ((str)->sz != NULL) \
76           && ((str)->used + 1 <= (str)->len));
77
78 static void
79 string_dump (struct string_t *str, FILE *out)
80 {  
81   assert_valid_string (str);
82   assert (out);
83   
84   fprintf (out, "string_dump: str->sz = %s (%p)\n", str->sz, str->sz);
85   fprintf (out, "string_dump: *(str->sz) = %d\n", *(str->sz));
86   fprintf (out, "string_dump: str->len = %u\n", str->len);
87   fprintf (out, "string_dump: str->used = %u\n", str->used);
88 }
89
90 #define DEBUG_PRINTF(x) printf x
91
92 #else /* not defined STRING_MODULE_DEBUG */
93
94 #define assert_valid_string(str) do {} while (0);
95 #define string_dump(str, out) do {} while (0);
96 #define DEBUG_PRINTF(x) do {} while (0);
97
98 #endif
99
100
101 void
102 string_init (struct string_t *s, unsigned int len)
103 {
104   size_t to_alloc;
105   
106   /* no need to check that len > 0, since the len == 0 case is ok */
107   assert (s != NULL);
108
109   /* 
110    * for the moment we try to perform a reasonable allocation by rounding up
111    * the number of requested chars (including the trailing zero) to the 
112    * closest multiple of 256, but we should probably find a better allocation
113    * policy or completely leave the optimization of memory allocation to malloc
114    */
115   to_alloc = ((len + 1 + 256) & (~0xFF));
116   
117   s->sz = (char *) xmalloc (to_alloc * sizeof (char));
118   *(s->sz) = '\0';
119   s->len = to_alloc;
120   s->used = 0;
121
122   string_dump (s, stdout);
123 }
124
125 void
126 string_copy (struct string_t *dst, const void *src, unsigned int len)
127 {
128   assert_valid_string (dst);
129   assert (src != NULL);
130   
131   /* no need to do anything */
132   if (len == 0) return;
133   
134   if (dst->sz == NULL) {
135     string_init (dst, len); 
136   }
137
138   strncpy (dst->sz, (const char *) src, len);
139   dst->sz[len] = '\0';
140
141   dst->used = len;
142 }
143
144 void
145 string_cat (struct string_t *dst, const void *src, unsigned int len)
146 {
147   assert_valid_string (dst);
148   assert (src != NULL);
149
150   /* no need to do anything */
151   if (len == 0) return;
152   
153   if (dst->sz == NULL) {
154     string_init (dst, len);    
155   }
156
157   strncpy (dst->sz + dst->used, (const char *) src, len);
158   dst->sz[dst->used + len] = '\0';
159   
160   dst->used += len;
161 }
162
163 void
164 string_ready (struct string_t *str, unsigned int len)
165 {
166   assert_valid_string (str);
167   
168   /* no need to do anything */
169   if (len == 0) return;
170
171   if (str->len - str->used < len)
172     {
173       DEBUG_PRINTF (("calling xrealloc"));
174       str->sz = xrealloc (str->sz, str->len + len);
175     }
176
177   str->len += len;
178 }
179
180 void
181 string_destroy (struct string_t *str)
182 {
183   assert_valid_string (str);
184   
185   xfree_null (str->sz);
186   memset (str, 0, sizeof (*str));
187 }
188
189 #if 0                           /* unused */
190 static void
191 string_append_delim (struct string_t *dst)
192 {
193   assert_valid_string (dst);
194   string_cat (dst, line_delim, line_delim_len);
195 }
196 #endif
197
198 static int 
199 is_line_delim (const wchar_t *wsz)
200 {
201   assert (wsz != NULL);
202  
203   if (*wsz == L'\r' && *(wsz + 1) == L'\n') {
204     return 2;
205   } else if (*wsz == L'\r' || *wsz == L'\n') {
206     return 1;
207   }
208   
209   return 0;
210 }
211
212 /*
213  * DEST is the string to which the multibyte stuff will be added
214  * TO_ESC is the null wide char string to add
215  */
216 static void
217 string_append_multibyte (struct string_t *dest, const wchar_t *wstr, unsigned int len, mbstate_t *state)
218 {
219   int i;
220
221   assert_valid_string (dest);
222   assert (wstr != NULL);
223   assert (state != NULL);
224   
225   /* nothing to do */
226   if (len == 0) return;
227
228   string_ready (dest, 4 * MB_CUR_MAX * (len + 1));
229
230   DEBUG_PRINTF (("string_append_multibyte: len = %u\n", len));
231   string_dump (dest, stdout);
232   
233   for (i = 0; len > 0; ++i, --len) {    
234     size_t copied = wcrtomb (dest->sz + dest->used, *(wstr + i), state);
235
236     DEBUG_PRINTF (("string_append_multibyte (loop): i = %d\n", i));
237     DEBUG_PRINTF (("string_append_multibyte (loop): copied = %u\n", copied));
238     string_dump (dest, stdout);
239     
240     if (copied == (size_t)(-1)) {
241       perror ("wcrtomb");
242       exit (EXIT_FAILURE);
243     }    
244     dest->used += copied;
245     *(dest->sz + dest->used) = '\0';
246
247     DEBUG_PRINTF (("string_append_multibyte (loop): processed %s\n", dest->sz + dest->used - copied));
248   }
249 }
250
251 static void
252 string_append_multibyte_newline (struct string_t *dest, mbstate_t *state)
253 {
254   assert_valid_string (dest);
255   string_append_multibyte(dest, w_line_delim, line_delim_len, state);
256 }
257
258 static void
259 string_append_multibyte_terminator (struct string_t *dest, mbstate_t *state)
260 {
261   const wchar_t terminator = L'\0';
262
263   assert_valid_string (dest);
264   string_append_multibyte(dest, &terminator, 1, state);
265 }
266
267 /* 
268  * DEST is the string to which the escape code will be added
269  * TO_ESC is the (not necessarily null terminated) string to escape
270  * LEN is the length of the string to escape
271  */
272 static void
273 do_escape (struct string_t *dest, const char *to_esc, unsigned int len, mbstate_t *state)
274 {
275   /* we only need to allocate 5 chars for byte: 
276    * - one for the leading backslash
277    * - three for the octal representation
278    * - one for the trailing zero */
279   wchar_t buf[8] = L"";
280   size_t buf_elems = sizeof(buf)/sizeof(buf[0]);
281   int i;
282
283   assert_valid_string (dest);
284   assert (to_esc != NULL);
285   assert (state != NULL);
286
287   /* nothing to do */
288   if (len == 0) return;
289   
290   DEBUG_PRINTF (("do_escape: len = %d\n", len));
291   string_dump (dest, stdout);
292   
293   for (i = 0; len > 0; ++i, --len) {
294     int j = (unsigned char)*(to_esc + i);
295     int cc = swprintf (buf, buf_elems, L"\\%03o", j);
296     assert(cc > 0 && cc < buf_elems);
297     DEBUG_PRINTF (("do_escape (loop): escaping \\%03o\n", j));
298     buf[buf_elems - 1] = L'\0';
299     assert (wcslen(buf) == 4);
300     string_append_multibyte (dest, buf, 4, state);
301   }
302 }
303
304 static void
305 string_escape (struct string_t *str)
306 {
307   wchar_t c;
308   int i;
309   mbstate_t state1, state2;  
310   size_t ret;
311   unsigned int to_read;
312   struct string_t src;
313   int delim_size;
314   
315   assert_valid_string (str);
316
317   memset (&state1, '\0', sizeof (state1));
318   memset (&state2, '\0', sizeof (state2));
319   
320   src = *str;
321   to_read = src.used + 1;
322   
323   /* this value is completely arbitrary */
324   string_init (str, 4 * to_read);
325
326   DEBUG_PRINTF (("string_escape: dumping string src"));
327   string_dump (&src, stdout);
328   DEBUG_PRINTF (("string_escape: dumping string str"));
329   string_dump (str, stdout);
330
331   i = 0;
332   while ((ret = mbrtowc (&c, src.sz + i, to_read, &state1)) != 0) {
333     DEBUG_PRINTF (("string_escape (loop): ret = %d\n", ret));
334     if (ret == (size_t)(-2)) {
335       DEBUG_PRINTF (("string_escape (loop): handling ret == -2"));
336       /* mauro: should we just return the portion of the string already 
337        * processed and print an error message? */
338       perror ("mbrtowc");
339       exit (EXIT_FAILURE);
340     } else if (ret == (size_t)(-1)) {
341       DEBUG_PRINTF (("string_escape (loop): handling ret == -1"));
342       do_escape (str, src.sz + i, 1, &state2);
343       i++;
344     } else if ((delim_size = is_line_delim(&c))) {
345       DEBUG_PRINTF (("string_escape (loop): handling ret == line_delim"));
346       i += delim_size;
347       string_append_multibyte_newline (str, &state2);
348     } else if (iswprint(c) || iswblank(c)) {
349       DEBUG_PRINTF (("string_escape (loop): handling ret == blank | printable"));
350       string_append_multibyte (str, &c, 1, &state2);
351       if (c == L'\\') {
352         /* since the backslash character is used to escape unprintable data, 
353          * in order to avoid ambiguities in the escaped string we have to 
354          * escape backslashes as well */
355         string_append_multibyte (str, &c, 1, &state2);
356       }
357       i += ret;
358     } else { 
359       DEBUG_PRINTF (("string_escape (loop): handling ret == toescape"));
360       do_escape (str, src.sz + i, ret, &state2);
361       i += ret;
362     }
363   }
364   
365   string_append_multibyte_terminator (str, &state2);
366   
367   string_destroy (&src);
368 }
369
370 /*
371  * BUF must be a null-terminated dynamically allocated string
372  * LEN is the size of the string BUF
373  */
374 void
375 escape_buffer (char **buf, size_t len)
376 {
377   struct string_t s;
378
379   assert (buf != NULL && *buf != NULL);
380
381   /* nothing to do */
382   if (len == 0) return;
383   
384   DEBUG_PRINTF (("escape_buffer processing: %s (len %u)\n", *buf, len));
385   
386   s.sz = *buf; 
387   s.used = len;
388   s.len = len + 1;
389   
390   string_escape (&s);
391   
392   *buf = s.sz;
393 }
394
395 #ifdef STANDALONE
396 int main(void)
397 {
398   char *buf;
399   const size_t buflen = 512;
400   buf = (char *) xmalloc(buflen);
401   assert (buf != NULL);
402   
403   puts ("--------------------------------------------------------------------------------");
404   
405   while (fgets (buf, buflen - 1, stdin) != NULL)
406     {
407       /* just in case... */
408       buf[buflen - 1] = '\0';
409       printf ("before escape: %s", buf);
410       escape_buffer (&buf, strlen(buf));
411       printf ("after escape: %s", buf);
412     }
413
414   puts ("--------------------------------------------------------------------------------");
415
416   return 0;
417 }
418 #endif
419
420 /*
421  * vim: et ts=2 sw=2
422  */
423