ae68414dd816d186192a52b136d7b8cd3e85af95
[platform/upstream/dbus.git] / dbus / dbus-marshal-validate.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-validate.c Validation routines for marshaled data
3  *
4  * Copyright (C) 2005 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program 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  * This program 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-marshal-validate.h"
27 #include "dbus-marshal-recursive.h"
28 #include "dbus-marshal-basic.h"
29 #include "dbus-signature.h"
30 #include "dbus-string.h"
31
32 /**
33  * @addtogroup DBusMarshal
34  *
35  * @{
36  */
37
38 /**
39  * Verifies that the range of type_str from type_pos to type_end is a
40  * valid signature.  If this function returns #TRUE, it will be safe
41  * to iterate over the signature with a types-only #DBusTypeReader.
42  * The range passed in should NOT include the terminating
43  * nul/DBUS_TYPE_INVALID.
44  *
45  * @param type_str the string
46  * @param type_pos where the typecodes start
47  * @param len length of typecodes
48  * @returns #DBUS_VALID if valid, reason why invalid otherwise
49  */
50 DBusValidity
51 _dbus_validate_signature_with_reason (const DBusString *type_str,
52                                       int               type_pos,
53                                       int               len)
54 {
55   const unsigned char *p;
56   const unsigned char *end;
57   int last;
58   int struct_depth;
59   int array_depth;
60   int dict_entry_depth;
61   DBusValidity result;
62
63   int element_count;
64   DBusList *element_count_stack;
65   char opened_brackets[DBUS_MAXIMUM_TYPE_RECURSION_DEPTH * 2 + 1] = { '\0' };
66   char last_bracket;
67
68   result = DBUS_VALID;
69   element_count_stack = NULL;
70
71   if (!_dbus_list_append (&element_count_stack, _DBUS_INT_TO_POINTER (0)))
72     {
73       result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
74       goto out;
75     }
76
77   _dbus_assert (type_str != NULL);
78   _dbus_assert (type_pos < _DBUS_INT32_MAX - len);
79   _dbus_assert (len >= 0);
80   _dbus_assert (type_pos >= 0);
81
82   if (len > DBUS_MAXIMUM_SIGNATURE_LENGTH)
83     {
84       result = DBUS_INVALID_SIGNATURE_TOO_LONG;
85       goto out;
86     }
87
88   p = _dbus_string_get_const_udata_len (type_str, type_pos, 0);
89
90   end = _dbus_string_get_const_udata_len (type_str, type_pos + len, 0);
91   struct_depth = 0;
92   array_depth = 0;
93   dict_entry_depth = 0;
94   last = DBUS_TYPE_INVALID;
95
96   while (p != end)
97     {
98       _dbus_assert (struct_depth + dict_entry_depth >= 0);
99       _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
100       _dbus_assert (opened_brackets[struct_depth + dict_entry_depth] == '\0');
101
102       switch (*p)
103         {
104         case DBUS_TYPE_BYTE:
105         case DBUS_TYPE_BOOLEAN:
106         case DBUS_TYPE_INT16:
107         case DBUS_TYPE_UINT16:
108         case DBUS_TYPE_INT32:
109         case DBUS_TYPE_UINT32:
110         case DBUS_TYPE_UNIX_FD:
111         case DBUS_TYPE_INT64:
112         case DBUS_TYPE_UINT64:
113         case DBUS_TYPE_DOUBLE:
114         case DBUS_TYPE_STRING:
115         case DBUS_TYPE_OBJECT_PATH:
116         case DBUS_TYPE_SIGNATURE:
117         case DBUS_TYPE_VARIANT:
118           break;
119
120         case DBUS_TYPE_ARRAY:
121           array_depth += 1;
122           if (array_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
123             {
124               result = DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
125               goto out;
126             }
127           break;
128
129         case DBUS_STRUCT_BEGIN_CHAR:
130           struct_depth += 1;
131
132           if (struct_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
133             {
134               result = DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
135               goto out;
136             }
137           
138           if (!_dbus_list_append (&element_count_stack, 
139                              _DBUS_INT_TO_POINTER (0)))
140             {
141               result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
142               goto out;
143             }
144
145           _dbus_assert (struct_depth + dict_entry_depth >= 1);
146           _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
147           _dbus_assert (opened_brackets[struct_depth + dict_entry_depth - 1] == '\0');
148           opened_brackets[struct_depth + dict_entry_depth - 1] = DBUS_STRUCT_BEGIN_CHAR;
149           break;
150
151         case DBUS_STRUCT_END_CHAR:
152           if (struct_depth == 0)
153             {
154               result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
155               goto out;
156             }
157
158           if (last == DBUS_STRUCT_BEGIN_CHAR)
159             {
160               result = DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
161               goto out;
162             }
163
164           _dbus_assert (struct_depth + dict_entry_depth >= 1);
165           _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
166           last_bracket = opened_brackets[struct_depth + dict_entry_depth - 1];
167
168           if (last_bracket != DBUS_STRUCT_BEGIN_CHAR)
169             {
170               result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
171               goto out;
172             }
173
174           _dbus_list_pop_last (&element_count_stack);
175
176           struct_depth -= 1;
177           opened_brackets[struct_depth + dict_entry_depth] = '\0';
178           break;
179
180         case DBUS_DICT_ENTRY_BEGIN_CHAR:
181           if (last != DBUS_TYPE_ARRAY)
182             {
183               result = DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY;
184               goto out;
185             }
186             
187           dict_entry_depth += 1;
188
189           if (dict_entry_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
190             {
191               result = DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
192               goto out;
193             }
194
195           if (!_dbus_list_append (&element_count_stack, 
196                              _DBUS_INT_TO_POINTER (0)))
197             {
198               result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
199               goto out;
200             }
201
202           _dbus_assert (struct_depth + dict_entry_depth >= 1);
203           _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
204           _dbus_assert (opened_brackets[struct_depth + dict_entry_depth - 1] == '\0');
205           opened_brackets[struct_depth + dict_entry_depth - 1] = DBUS_DICT_ENTRY_BEGIN_CHAR;
206           break;
207
208         case DBUS_DICT_ENTRY_END_CHAR:
209           if (dict_entry_depth == 0)
210             {
211               result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
212               goto out;
213             }
214
215           _dbus_assert (struct_depth + dict_entry_depth >= 1);
216           _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
217           last_bracket = opened_brackets[struct_depth + dict_entry_depth - 1];
218
219           if (last_bracket != DBUS_DICT_ENTRY_BEGIN_CHAR)
220             {
221               result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
222               goto out;
223             }
224
225           dict_entry_depth -= 1;
226           opened_brackets[struct_depth + dict_entry_depth] = '\0';
227
228           element_count = 
229             _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
230
231           if (element_count != 2)
232             {
233               if (element_count == 0)
234                 result = DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS;
235               else if (element_count == 1)
236                 result = DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD;
237               else
238                 result = DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS;
239               
240               goto out;
241             }
242           break;
243           
244         case DBUS_TYPE_STRUCT:     /* doesn't appear in signatures */
245         case DBUS_TYPE_DICT_ENTRY: /* ditto */
246         default:
247           result = DBUS_INVALID_UNKNOWN_TYPECODE;
248           goto out;
249         }
250
251       if (*p != DBUS_TYPE_ARRAY && 
252           *p != DBUS_DICT_ENTRY_BEGIN_CHAR && 
253           *p != DBUS_STRUCT_BEGIN_CHAR) 
254         {
255           element_count = 
256             _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
257
258           ++element_count;
259
260           if (!_dbus_list_append (&element_count_stack, 
261                              _DBUS_INT_TO_POINTER (element_count)))
262             {
263               result = DBUS_VALIDITY_UNKNOWN_OOM_ERROR;
264               goto out;
265             }
266         }
267       
268       if (array_depth > 0)
269         {
270           if (*p == DBUS_TYPE_ARRAY && p != end)
271             {
272                const unsigned char *p1;
273                p1 = p + 1;
274                if (*p1 == DBUS_STRUCT_END_CHAR ||
275                    *p1 == DBUS_DICT_ENTRY_END_CHAR)
276                  {
277                    result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
278                    goto out;
279                  }
280             }
281           else
282             {
283               array_depth = 0;
284             }
285         }
286
287       if (last == DBUS_DICT_ENTRY_BEGIN_CHAR)
288         {
289           if (!(dbus_type_is_valid (*p) && dbus_type_is_basic (*p)))
290             {
291               result = DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
292               goto out;
293             }
294         }
295
296       last = *p;
297       ++p;
298     }
299
300
301   if (array_depth > 0)
302     {
303       result = DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
304       goto out;
305     }
306     
307   if (struct_depth > 0)
308     {
309        result = DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
310        goto out;
311     }
312     
313   if (dict_entry_depth > 0)
314     {
315       result =  DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
316       goto out;
317     }
318     
319   _dbus_assert (last != DBUS_TYPE_ARRAY);
320   _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
321   _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
322
323   result = DBUS_VALID;
324
325 out:
326   _dbus_list_clear (&element_count_stack);
327   return result;
328 }
329
330 /* note: this function is also used to validate the header's values,
331  * since the header is a valid body with a particular signature.
332  */
333 static DBusValidity
334 validate_body_helper (DBusTypeReader       *reader,
335                       int                   byte_order,
336                       dbus_bool_t           walk_reader_to_end,
337                       int                   total_depth,
338                       const unsigned char  *p,
339                       const unsigned char  *end,
340                       const unsigned char **new_p)
341 {
342   int current_type;
343
344   /* The spec allows arrays and structs to each nest 32, for total
345    * nesting of 2*32. We want to impose the same limit on "dynamic"
346    * value nesting (not visible in the signature) which is introduced
347    * by DBUS_TYPE_VARIANT.
348    */
349   if (total_depth > (DBUS_MAXIMUM_TYPE_RECURSION_DEPTH * 2))
350     {
351       return DBUS_INVALID_NESTED_TOO_DEEPLY;
352     }
353
354   while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
355     {
356       const unsigned char *a;
357       int alignment;
358
359 #if 0
360       _dbus_verbose ("   validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
361                      _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
362                      (int) (end - p));
363 #endif
364
365       /* Guarantee that p has one byte to look at */
366       if (p == end)
367         return DBUS_INVALID_NOT_ENOUGH_DATA;
368
369       switch (current_type)
370         {
371         case DBUS_TYPE_BYTE:
372           ++p;
373           break;
374
375         case DBUS_TYPE_BOOLEAN:
376         case DBUS_TYPE_INT16:
377         case DBUS_TYPE_UINT16:
378         case DBUS_TYPE_INT32:
379         case DBUS_TYPE_UINT32:
380         case DBUS_TYPE_UNIX_FD:
381         case DBUS_TYPE_INT64:
382         case DBUS_TYPE_UINT64:
383         case DBUS_TYPE_DOUBLE:
384           alignment = _dbus_type_get_alignment (current_type);
385           a = _DBUS_ALIGN_ADDRESS (p, alignment);
386           if (a >= end)
387             return DBUS_INVALID_NOT_ENOUGH_DATA;
388           while (p != a)
389             {
390               if (*p != '\0')
391                 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
392               ++p;
393             }
394           
395           if (current_type == DBUS_TYPE_BOOLEAN)
396             {
397               dbus_uint32_t v;
398
399               if (p + 4 > end)
400                 return DBUS_INVALID_NOT_ENOUGH_DATA;
401
402               v = _dbus_unpack_uint32 (byte_order, p);
403
404               if (!(v == 0 || v == 1))
405                 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
406             }
407           
408           p += alignment;
409           break;
410
411         case DBUS_TYPE_ARRAY:
412         case DBUS_TYPE_STRING:
413         case DBUS_TYPE_OBJECT_PATH:
414           {
415             dbus_uint32_t claimed_len;
416
417             a = _DBUS_ALIGN_ADDRESS (p, 4);
418             if (a + 4 > end)
419               return DBUS_INVALID_NOT_ENOUGH_DATA;
420             while (p != a)
421               {
422                 if (*p != '\0')
423                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
424                 ++p;
425               }
426
427             claimed_len = _dbus_unpack_uint32 (byte_order, p);
428             p += 4;
429
430             /* p may now be == end */
431             _dbus_assert (p <= end);
432
433             if (current_type == DBUS_TYPE_ARRAY)
434               {
435                 int array_elem_type = _dbus_type_reader_get_element_type (reader);
436
437                 if (!dbus_type_is_valid (array_elem_type))
438                   {
439                     return DBUS_INVALID_UNKNOWN_TYPECODE;
440                   }
441
442                 alignment = _dbus_type_get_alignment (array_elem_type);
443
444                 a = _DBUS_ALIGN_ADDRESS (p, alignment);
445
446                 /* a may now be == end */
447                 if (a > end)
448                   return DBUS_INVALID_NOT_ENOUGH_DATA;
449
450                 while (p != a)
451                   {
452                     if (*p != '\0')
453                       return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
454                     ++p;
455                   }
456               }
457
458             if (claimed_len > (unsigned long) (end - p))
459               return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
460
461             if (current_type == DBUS_TYPE_OBJECT_PATH)
462               {
463                 DBusString str;
464                 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
465                 if (!_dbus_validate_path (&str, 0,
466                                           _dbus_string_get_length (&str)))
467                   return DBUS_INVALID_BAD_PATH;
468
469                 p += claimed_len;
470               }
471             else if (current_type == DBUS_TYPE_STRING)
472               {
473                 DBusString str;
474                 _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
475                 if (!_dbus_string_validate_utf8 (&str, 0,
476                                                  _dbus_string_get_length (&str)))
477                   return DBUS_INVALID_BAD_UTF8_IN_STRING;
478
479                 p += claimed_len;
480               }
481             else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
482               {
483                 DBusTypeReader sub;
484                 DBusValidity validity;
485                 const unsigned char *array_end;
486                 int array_elem_type;
487
488                 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
489                   return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
490                 
491                 /* Remember that the reader is types only, so we can't
492                  * use it to iterate over elements. It stays the same
493                  * for all elements.
494                  */
495                 _dbus_type_reader_recurse (reader, &sub);
496
497                 array_end = p + claimed_len;
498
499                 array_elem_type = _dbus_type_reader_get_element_type (reader);
500
501                 /* avoid recursive call to validate_body_helper if this is an array
502                  * of fixed-size elements
503                  */ 
504                 if (dbus_type_is_fixed (array_elem_type))
505                   {
506                     /* bools need to be handled differently, because they can
507                      * have an invalid value
508                      */
509                     if (array_elem_type == DBUS_TYPE_BOOLEAN)
510                       {
511                         dbus_uint32_t v;
512                         alignment = _dbus_type_get_alignment (array_elem_type);
513
514                         while (p < array_end)
515                           {
516                             v = _dbus_unpack_uint32 (byte_order, p);
517
518                             if (!(v == 0 || v == 1))
519                               return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
520
521                             p += alignment;
522                           }
523                       }
524
525                     else
526                       {
527                         p = array_end;
528                       }
529                   }
530
531                 else
532                   {
533                     while (p < array_end)
534                       {
535                         validity = validate_body_helper (&sub, byte_order, FALSE,
536                                                          total_depth + 1,
537                                                          p, end, &p);
538                         if (validity != DBUS_VALID)
539                           return validity;
540                       }
541                   }
542
543                 if (p != array_end)
544                   return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
545               }
546
547             /* check nul termination */
548             if (current_type != DBUS_TYPE_ARRAY)
549               {
550                 if (p == end)
551                   return DBUS_INVALID_NOT_ENOUGH_DATA;
552
553                 if (*p != '\0')
554                   return DBUS_INVALID_STRING_MISSING_NUL;
555                 ++p;
556               }
557           }
558           break;
559
560         case DBUS_TYPE_SIGNATURE:
561           {
562             dbus_uint32_t claimed_len;
563             DBusString str;
564             DBusValidity validity;
565
566             claimed_len = *p;
567             ++p;
568
569             /* 1 is for nul termination */
570             if (claimed_len + 1 > (unsigned long) (end - p))
571               return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
572
573             _dbus_string_init_const_len (&str, (const char *) p, claimed_len);
574             validity =
575               _dbus_validate_signature_with_reason (&str, 0,
576                                                     _dbus_string_get_length (&str));
577
578             if (validity != DBUS_VALID)
579               return validity;
580
581             p += claimed_len;
582
583             _dbus_assert (p < end);
584             if (*p != DBUS_TYPE_INVALID)
585               return DBUS_INVALID_SIGNATURE_MISSING_NUL;
586
587             ++p;
588
589             _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
590           }
591           break;
592
593         case DBUS_TYPE_VARIANT:
594           {
595             /* 1 byte sig len, sig typecodes, align to
596              * contained-type-boundary, values.
597              */
598
599             /* In addition to normal signature validation, we need to be sure
600              * the signature contains only a single (possibly container) type.
601              */
602             dbus_uint32_t claimed_len;
603             DBusString sig;
604             DBusTypeReader sub;
605             DBusValidity validity;
606             int contained_alignment;
607             int contained_type;
608             DBusValidity reason;
609
610             claimed_len = *p;
611             ++p;
612
613             /* + 1 for nul */
614             if (claimed_len + 1 > (unsigned long) (end - p))
615               return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
616
617             _dbus_string_init_const_len (&sig, (const char *) p, claimed_len);
618             reason = _dbus_validate_signature_with_reason (&sig, 0,
619                                            _dbus_string_get_length (&sig));
620             if (!(reason == DBUS_VALID))
621               {
622                 if (reason == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
623                   return reason;
624                 else 
625                   return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
626               }
627
628             p += claimed_len;
629             
630             if (*p != DBUS_TYPE_INVALID)
631               return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
632             ++p;
633
634             contained_type = _dbus_first_type_in_signature (&sig, 0);
635             if (contained_type == DBUS_TYPE_INVALID)
636               return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
637             
638             contained_alignment = _dbus_type_get_alignment (contained_type);
639             
640             a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
641             if (a > end)
642               return DBUS_INVALID_NOT_ENOUGH_DATA;
643             while (p != a)
644               {
645                 if (*p != '\0')
646                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
647                 ++p;
648               }
649
650             _dbus_type_reader_init_types_only (&sub, &sig, 0);
651
652             _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
653
654             validity = validate_body_helper (&sub, byte_order, FALSE,
655                                              total_depth + 1,
656                                              p, end, &p);
657             if (validity != DBUS_VALID)
658               return validity;
659
660             if (_dbus_type_reader_next (&sub))
661               return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
662
663             _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
664           }
665           break;
666
667         case DBUS_TYPE_DICT_ENTRY:
668         case DBUS_TYPE_STRUCT:
669           {
670             DBusTypeReader sub;
671             DBusValidity validity;
672
673             a = _DBUS_ALIGN_ADDRESS (p, 8);
674             if (a > end)
675               return DBUS_INVALID_NOT_ENOUGH_DATA;
676             while (p != a)
677               {
678                 if (*p != '\0')
679                   return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
680                 ++p;
681               }
682
683             _dbus_type_reader_recurse (reader, &sub);
684
685             validity = validate_body_helper (&sub, byte_order, TRUE,
686                                              total_depth + 1,
687                                              p, end, &p);
688             if (validity != DBUS_VALID)
689               return validity;
690           }
691           break;
692
693         default:
694           _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
695           break;
696         }
697
698 #if 0
699       _dbus_verbose ("   validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
700                      _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
701                      (int) (end - p));
702 #endif
703
704       if (p > end)
705         {
706           _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
707                          p, end, (int) (end - p));
708           return DBUS_INVALID_NOT_ENOUGH_DATA;
709         }
710
711       if (walk_reader_to_end)
712         _dbus_type_reader_next (reader);
713       else
714         break;
715     }
716
717   if (new_p)
718     *new_p = p;
719
720   return DBUS_VALID;
721 }
722
723 /**
724  * Verifies that the range of value_str from value_pos to value_end is
725  * a legitimate value of type expected_signature.  If this function
726  * returns #TRUE, it will be safe to iterate over the values with
727  * #DBusTypeReader. The signature is assumed to be already valid.
728  *
729  * If bytes_remaining is not #NULL, then leftover bytes will be stored
730  * there and #DBUS_VALID returned. If it is #NULL, then
731  * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
732  * over.
733  *
734  * @param expected_signature the expected types in the value_str
735  * @param expected_signature_start where in expected_signature is the signature
736  * @param byte_order the byte order
737  * @param bytes_remaining place to store leftover bytes
738  * @param value_str the string containing the body
739  * @param value_pos where the values start
740  * @param len length of values after value_pos
741  * @returns #DBUS_VALID if valid, reason why invalid otherwise
742  */
743 DBusValidity
744 _dbus_validate_body_with_reason (const DBusString *expected_signature,
745                                  int               expected_signature_start,
746                                  int               byte_order,
747                                  int              *bytes_remaining,
748                                  const DBusString *value_str,
749                                  int               value_pos,
750                                  int               len)
751 {
752   DBusTypeReader reader;
753   const unsigned char *p;
754   const unsigned char *end;
755   DBusValidity validity;
756
757   _dbus_assert (len >= 0);
758   _dbus_assert (value_pos >= 0);
759   _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
760
761   _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
762                  value_pos, len, _dbus_string_get_const_data_len (expected_signature,
763                                                                   expected_signature_start,
764                                                                   0));
765
766   _dbus_type_reader_init_types_only (&reader,
767                                      expected_signature, expected_signature_start);
768
769   p = _dbus_string_get_const_udata_len (value_str, value_pos, len);
770   end = p + len;
771
772   validity = validate_body_helper (&reader, byte_order, TRUE, 0, p, end, &p);
773   if (validity != DBUS_VALID)
774     return validity;
775   
776   if (bytes_remaining)
777     {
778       *bytes_remaining = end - p;
779       return DBUS_VALID;
780     }
781   else if (p < end)
782     return DBUS_INVALID_TOO_MUCH_DATA;
783   else
784     {
785       _dbus_assert (p == end);
786       return DBUS_VALID;
787     }
788 }
789
790 /**
791  * Determine wether the given character is valid as the first character
792  * in a name.
793  */
794 #define VALID_INITIAL_NAME_CHARACTER(c)         \
795   ( ((c) >= 'A' && (c) <= 'Z') ||               \
796     ((c) >= 'a' && (c) <= 'z') ||               \
797     ((c) == '_') )
798
799 /**
800  * Determine wether the given character is valid as a second or later
801  * character in a name
802  */
803 #define VALID_NAME_CHARACTER(c)                 \
804   ( ((c) >= '0' && (c) <= '9') ||               \
805     ((c) >= 'A' && (c) <= 'Z') ||               \
806     ((c) >= 'a' && (c) <= 'z') ||               \
807     ((c) == '_') )
808
809 /**
810  * Checks that the given range of the string is a valid object path
811  * name in the D-Bus protocol. Part of the validation ensures that
812  * the object path contains only ASCII.
813  *
814  * @todo this is inconsistent with most of DBusString in that
815  * it allows a start,len range that extends past the string end.
816  *
817  * @todo change spec to disallow more things, such as spaces in the
818  * path name
819  *
820  * @param str the string
821  * @param start first byte index to check
822  * @param len number of bytes to check
823  * @returns #TRUE if the byte range exists and is a valid name
824  */
825 dbus_bool_t
826 _dbus_validate_path (const DBusString  *str,
827                      int                start,
828                      int                len)
829 {
830   const unsigned char *s;
831   const unsigned char *end;
832   const unsigned char *last_slash;
833
834   _dbus_assert (start >= 0);
835   _dbus_assert (len >= 0);
836   _dbus_assert (start <= _dbus_string_get_length (str));
837   
838   if (len > _dbus_string_get_length (str) - start)
839     return FALSE;
840
841   if (len == 0)
842     return FALSE;
843
844   s = _dbus_string_get_const_udata (str) + start;
845   end = s + len;
846
847   if (*s != '/')
848     return FALSE;
849   last_slash = s;
850   ++s;
851
852   while (s != end)
853     {
854       if (*s == '/')
855         {
856           if ((s - last_slash) < 2)
857             return FALSE; /* no empty path components allowed */
858
859           last_slash = s;
860         }
861       else
862         {
863           if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
864             return FALSE;
865         }
866
867       ++s;
868     }
869
870   if ((end - last_slash) < 2 &&
871       len > 1)
872     return FALSE; /* trailing slash not allowed unless the string is "/" */
873
874   return TRUE;
875 }
876
877 const char *
878 _dbus_validity_to_error_message (DBusValidity validity)
879 {
880   switch (validity)
881     {
882     case DBUS_VALIDITY_UNKNOWN_OOM_ERROR:                          return "Out of memory";
883     case DBUS_INVALID_FOR_UNKNOWN_REASON:                          return "Unknown reason";
884     case DBUS_VALID_BUT_INCOMPLETE:                                return "Valid but incomplete";
885     case DBUS_VALIDITY_UNKNOWN:                                    return "Validity unknown";
886     case DBUS_VALID:                                               return "Valid";
887     case DBUS_INVALID_UNKNOWN_TYPECODE:                            return "Unknown typecode";
888     case DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE:                  return "Missing array element type";
889     case DBUS_INVALID_SIGNATURE_TOO_LONG:                          return "Signature is too long";
890     case DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION:            return "Exceeded maximum array recursion";
891     case DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION:           return "Exceeded maximum struct recursion";
892     case DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED:                return "Struct ended but not started";
893     case DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED:                return "Struct started but not ended";
894     case DBUS_INVALID_STRUCT_HAS_NO_FIELDS:                        return "Struct has no fields";
895     case DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL:                   return "Alignment padding not null";
896     case DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE:                     return "Boolean is not zero or one";
897     case DBUS_INVALID_NOT_ENOUGH_DATA:                             return "Not enough data";
898     case DBUS_INVALID_TOO_MUCH_DATA:                               return "Too much data";
899     case DBUS_INVALID_BAD_BYTE_ORDER:                              return "Bad byte order";
900     case DBUS_INVALID_BAD_PROTOCOL_VERSION:                        return "Bad protocol version";
901     case DBUS_INVALID_BAD_MESSAGE_TYPE:                            return "Bad message type";
902     case DBUS_INVALID_BAD_SERIAL:                                  return "Bad serial";
903     case DBUS_INVALID_INSANE_FIELDS_ARRAY_LENGTH:                  return "Insane fields array length";
904     case DBUS_INVALID_INSANE_BODY_LENGTH:                          return "Insane body length";
905     case DBUS_INVALID_MESSAGE_TOO_LONG:                            return "Message too long";
906     case DBUS_INVALID_HEADER_FIELD_CODE:                           return "Header field code";
907     case DBUS_INVALID_HEADER_FIELD_HAS_WRONG_TYPE:                 return "Header field has wrong type";
908     case DBUS_INVALID_USES_LOCAL_INTERFACE:                        return "Uses local interface";
909     case DBUS_INVALID_USES_LOCAL_PATH:                             return "Uses local path";
910     case DBUS_INVALID_HEADER_FIELD_APPEARS_TWICE:                  return "Header field appears twice";
911     case DBUS_INVALID_BAD_DESTINATION:                             return "Bad destination";
912     case DBUS_INVALID_BAD_INTERFACE:                               return "Bad interface";
913     case DBUS_INVALID_BAD_MEMBER:                                  return "Bad member";
914     case DBUS_INVALID_BAD_ERROR_NAME:                              return "Bad error name";
915     case DBUS_INVALID_BAD_SENDER:                                  return "Bad sender";
916     case DBUS_INVALID_MISSING_PATH:                                return "Missing path";
917     case DBUS_INVALID_MISSING_INTERFACE:                           return "Missing interface";
918     case DBUS_INVALID_MISSING_MEMBER:                              return "Missing member";
919     case DBUS_INVALID_MISSING_ERROR_NAME:                          return "Missing error name";
920     case DBUS_INVALID_MISSING_REPLY_SERIAL:                        return "Missing reply serial";
921     case DBUS_INVALID_LENGTH_OUT_OF_BOUNDS:                        return "Length out of bounds";
922     case DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM:                return "Array length exceeds maximum";
923     case DBUS_INVALID_BAD_PATH:                                    return "Bad path";
924     case DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS:              return "Signature length out of bounds";
925     case DBUS_INVALID_BAD_UTF8_IN_STRING:                          return "Bad utf8 in string";
926     case DBUS_INVALID_ARRAY_LENGTH_INCORRECT:                      return "Array length incorrect";
927     case DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS:      return "Variant signature length out of bounds";
928     case DBUS_INVALID_VARIANT_SIGNATURE_BAD:                       return "Variant signature bad";
929     case DBUS_INVALID_VARIANT_SIGNATURE_EMPTY:                     return "Variant signature empty";
930     case DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES: return "Variant signature specifies multiple values";
931     case DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL:               return "Variant signature missing nul";
932     case DBUS_INVALID_STRING_MISSING_NUL:                          return "String missing nul";
933     case DBUS_INVALID_SIGNATURE_MISSING_NUL:                       return "Signature missing nul";
934     case DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION:       return "Exceeded maximum dict entry recursion";
935     case DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED:            return "Dict entry ended but not started";
936     case DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED:            return "Dict entry started but not ended";
937     case DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS:                    return "Dict entry has no fields";
938     case DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD:               return "Dict entry has only one field";
939     case DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS:              return "Dict entry has too many fields";
940     case DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY:                 return "Dict entry not inside array";
941     case DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE:                 return "Dict key must be basic type";
942     case DBUS_INVALID_MISSING_UNIX_FDS:                            return "Unix file descriptor missing";
943     case DBUS_INVALID_NESTED_TOO_DEEPLY:                           return "Variants cannot be used to create a hugely recursive tree of values";
944     case DBUS_VALIDITY_LAST:
945     default:
946       return "Invalid";
947     }
948 }
949
950 /**
951  * Checks that the given range of the string is a valid interface name
952  * in the D-Bus protocol. This includes a length restriction and an
953  * ASCII subset, see the specification.
954  *
955  * @todo this is inconsistent with most of DBusString in that
956  * it allows a start,len range that extends past the string end.
957  *
958  * @param str the string
959  * @param start first byte index to check
960  * @param len number of bytes to check
961  * @returns #TRUE if the byte range exists and is a valid name
962  */
963 dbus_bool_t
964 _dbus_validate_interface (const DBusString  *str,
965                           int                start,
966                           int                len)
967 {
968   const unsigned char *s;
969   const unsigned char *end;
970   const unsigned char *iface;
971   const unsigned char *last_dot;
972
973   _dbus_assert (start >= 0);
974   _dbus_assert (len >= 0);
975   _dbus_assert (start <= _dbus_string_get_length (str));
976
977   if (len > _dbus_string_get_length (str) - start)
978     return FALSE;
979
980   if (len > DBUS_MAXIMUM_NAME_LENGTH)
981     return FALSE;
982
983   if (len == 0)
984     return FALSE;
985
986   last_dot = NULL;
987   iface = _dbus_string_get_const_udata (str) + start;
988   end = iface + len;
989   s = iface;
990
991   /* check special cases of first char so it doesn't have to be done
992    * in the loop. Note we know len > 0
993    */
994   if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
995     return FALSE;
996   else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
997     return FALSE;
998   else
999     ++s;
1000
1001   while (s != end)
1002     {
1003       if (*s == '.')
1004         {
1005           if (_DBUS_UNLIKELY ((s + 1) == end))
1006             return FALSE;
1007           else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
1008             return FALSE;
1009           last_dot = s;
1010           ++s; /* we just validated the next char, so skip two */
1011         }
1012       else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1013         {
1014           return FALSE;
1015         }
1016
1017       ++s;
1018     }
1019
1020   if (_DBUS_UNLIKELY (last_dot == NULL))
1021     return FALSE;
1022
1023   return TRUE;
1024 }
1025
1026 /**
1027  * Checks that the given range of the string is a valid member name
1028  * in the D-Bus protocol. This includes a length restriction, etc.,
1029  * see the specification.
1030  *
1031  * @todo this is inconsistent with most of DBusString in that
1032  * it allows a start,len range that extends past the string end.
1033  *
1034  * @param str the string
1035  * @param start first byte index to check
1036  * @param len number of bytes to check
1037  * @returns #TRUE if the byte range exists and is a valid name
1038  */
1039 dbus_bool_t
1040 _dbus_validate_member (const DBusString  *str,
1041                        int                start,
1042                        int                len)
1043 {
1044   const unsigned char *s;
1045   const unsigned char *end;
1046   const unsigned char *member;
1047
1048   _dbus_assert (start >= 0);
1049   _dbus_assert (len >= 0);
1050   _dbus_assert (start <= _dbus_string_get_length (str));
1051
1052   if (len > _dbus_string_get_length (str) - start)
1053     return FALSE;
1054
1055   if (len > DBUS_MAXIMUM_NAME_LENGTH)
1056     return FALSE;
1057
1058   if (len == 0)
1059     return FALSE;
1060
1061   member = _dbus_string_get_const_udata (str) + start;
1062   end = member + len;
1063   s = member;
1064
1065   /* check special cases of first char so it doesn't have to be done
1066    * in the loop. Note we know len > 0
1067    */
1068
1069   if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
1070     return FALSE;
1071   else
1072     ++s;
1073
1074   while (s != end)
1075     {
1076       if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
1077         {
1078           return FALSE;
1079         }
1080
1081       ++s;
1082     }
1083
1084   return TRUE;
1085 }
1086
1087 /**
1088  * Checks that the given range of the string is a valid error name
1089  * in the D-Bus protocol. This includes a length restriction, etc.,
1090  * see the specification.
1091  *
1092  * @todo this is inconsistent with most of DBusString in that
1093  * it allows a start,len range that extends past the string end.
1094  *
1095  * @param str the string
1096  * @param start first byte index to check
1097  * @param len number of bytes to check
1098  * @returns #TRUE if the byte range exists and is a valid name
1099  */
1100 dbus_bool_t
1101 _dbus_validate_error_name (const DBusString  *str,
1102                            int                start,
1103                            int                len)
1104 {
1105   /* Same restrictions as interface name at the moment */
1106   return _dbus_validate_interface (str, start, len);
1107 }
1108
1109 /**
1110  * Determine wether the given character is valid as the first character
1111  * in a bus name.
1112  */
1113 #define VALID_INITIAL_BUS_NAME_CHARACTER(c)         \
1114   ( ((c) >= 'A' && (c) <= 'Z') ||               \
1115     ((c) >= 'a' && (c) <= 'z') ||               \
1116     ((c) == '_') || ((c) == '-'))
1117
1118 /**
1119  * Determine wether the given character is valid as a second or later
1120  * character in a bus name
1121  */
1122 #define VALID_BUS_NAME_CHARACTER(c)                 \
1123   ( ((c) >= '0' && (c) <= '9') ||               \
1124     ((c) >= 'A' && (c) <= 'Z') ||               \
1125     ((c) >= 'a' && (c) <= 'z') ||               \
1126     ((c) == '_') || ((c) == '-'))
1127
1128 static dbus_bool_t
1129 _dbus_validate_bus_name_full (const DBusString  *str,
1130                               int                start,
1131                               int                len,
1132                               dbus_bool_t        is_namespace)
1133 {
1134   const unsigned char *s;
1135   const unsigned char *end;
1136   const unsigned char *iface;
1137   const unsigned char *last_dot;
1138
1139   _dbus_assert (start >= 0);
1140   _dbus_assert (len >= 0);
1141   _dbus_assert (start <= _dbus_string_get_length (str));
1142
1143   if (len > _dbus_string_get_length (str) - start)
1144     return FALSE;
1145
1146   if (len > DBUS_MAXIMUM_NAME_LENGTH)
1147     return FALSE;
1148
1149   if (len == 0)
1150     return FALSE;
1151
1152   last_dot = NULL;
1153   iface = _dbus_string_get_const_udata (str) + start;
1154   end = iface + len;
1155   s = iface;
1156
1157   /* check special cases of first char so it doesn't have to be done
1158    * in the loop. Note we know len > 0
1159    */
1160   if (*s == ':')
1161   {
1162     /* unique name */
1163     ++s;
1164     while (s != end)
1165       {
1166         if (*s == '.')
1167           {
1168             if (_DBUS_UNLIKELY ((s + 1) == end))
1169               return FALSE;
1170             if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
1171               return FALSE;
1172             ++s; /* we just validated the next char, so skip two */
1173           }
1174         else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1175           {
1176             return FALSE;
1177           }
1178
1179         ++s;
1180       }
1181
1182     return TRUE;
1183   }
1184   else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
1185     return FALSE;
1186   else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
1187     return FALSE;
1188   else
1189     ++s;
1190
1191   while (s != end)
1192     {
1193       if (*s == '.')
1194         {
1195           if (_DBUS_UNLIKELY ((s + 1) == end))
1196             return FALSE;
1197           else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
1198             return FALSE;
1199           last_dot = s;
1200           ++s; /* we just validated the next char, so skip two */
1201         }
1202       else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
1203         {
1204           return FALSE;
1205         }
1206
1207       ++s;
1208     }
1209
1210   if (!is_namespace && _DBUS_UNLIKELY (last_dot == NULL))
1211     return FALSE;
1212
1213   return TRUE;
1214 }
1215
1216 /**
1217  * Checks that the given range of the string is a valid bus name in
1218  * the D-Bus protocol. This includes a length restriction, etc., see
1219  * the specification.
1220  *
1221  * @todo this is inconsistent with most of DBusString in that
1222  * it allows a start,len range that extends past the string end.
1223  *
1224  * @param str the string
1225  * @param start first byte index to check
1226  * @param len number of bytes to check
1227  * @returns #TRUE if the byte range exists and is a valid name
1228  */
1229 dbus_bool_t
1230 _dbus_validate_bus_name (const DBusString  *str,
1231                          int                start,
1232                          int                len)
1233 {
1234   return _dbus_validate_bus_name_full (str, start, len, FALSE);
1235 }
1236
1237 /**
1238  * Checks that the given range of the string is a prefix of a valid bus name in
1239  * the D-Bus protocol. Unlike _dbus_validate_bus_name(), this accepts strings
1240  * with only one period-separated component.
1241  *
1242  * @todo this is inconsistent with most of DBusString in that
1243  * it allows a start,len range that extends past the string end.
1244  *
1245  * @param str the string
1246  * @param start first byte index to check
1247  * @param len number of bytes to check
1248  * @returns #TRUE if the byte range exists and is a valid name
1249  */
1250 dbus_bool_t
1251 _dbus_validate_bus_namespace (const DBusString  *str,
1252                               int                start,
1253                               int                len)
1254 {
1255   return _dbus_validate_bus_name_full (str, start, len, TRUE);
1256 }
1257
1258 /** define _dbus_check_is_valid_path() */
1259 DEFINE_DBUS_NAME_CHECK(path)
1260 /** define _dbus_check_is_valid_interface() */
1261 DEFINE_DBUS_NAME_CHECK(interface)
1262 /** define _dbus_check_is_valid_member() */
1263 DEFINE_DBUS_NAME_CHECK(member)
1264 /** define _dbus_check_is_valid_error_name() */
1265 DEFINE_DBUS_NAME_CHECK(error_name)
1266 /** define _dbus_check_is_valid_bus_name() */
1267 DEFINE_DBUS_NAME_CHECK(bus_name)
1268 /** define _dbus_check_is_valid_utf8() */
1269 DEFINE_DBUS_NAME_CHECK(utf8)
1270
1271 /** @} */
1272
1273 /* tests in dbus-marshal-validate-util.c */