1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-validate.c Validation routines for marshaled data
4 * Copyright (C) 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
24 #include "dbus-internals.h"
25 #include "dbus-marshal-validate.h"
26 #include "dbus-marshal-recursive.h"
27 #include "dbus-marshal-basic.h"
28 #include "dbus-string.h"
31 * @addtogroup DBusMarshal
37 * Verifies that the range of type_str from type_pos to type_end is a
38 * valid signature. If this function returns #TRUE, it will be safe
39 * to iterate over the signature with a types-only #DBusTypeReader.
40 * The range passed in should NOT include the terminating
41 * nul/DBUS_TYPE_INVALID.
43 * @todo verify that dict entries have exactly two fields
45 * @todo require that dict entries are in an array
47 * @param type_str the string
48 * @param type_pos where the typecodes start
49 * @param len length of typecodes
50 * @returns #DBUS_VALID if valid, reason why invalid otherwise
53 _dbus_validate_signature_with_reason (const DBusString *type_str,
57 const unsigned char *p;
58 const unsigned char *end;
64 _dbus_assert (type_str != NULL);
65 _dbus_assert (type_pos < _DBUS_INT32_MAX - len);
66 _dbus_assert (len >= 0);
67 _dbus_assert (type_pos >= 0);
69 if (len > DBUS_MAXIMUM_SIGNATURE_LENGTH)
70 return DBUS_INVALID_SIGNATURE_TOO_LONG;
72 p = _dbus_string_get_const_data_len (type_str, type_pos, 0);
73 end = _dbus_string_get_const_data_len (type_str, type_pos + len, 0);
77 last = DBUS_TYPE_INVALID;
84 case DBUS_TYPE_BOOLEAN:
86 case DBUS_TYPE_UINT16:
88 case DBUS_TYPE_UINT32:
90 case DBUS_TYPE_UINT64:
91 case DBUS_TYPE_DOUBLE:
92 case DBUS_TYPE_STRING:
93 case DBUS_TYPE_OBJECT_PATH:
94 case DBUS_TYPE_SIGNATURE:
95 case DBUS_TYPE_VARIANT:
100 if (array_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
101 return DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION;
104 case DBUS_STRUCT_BEGIN_CHAR:
107 if (struct_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
108 return DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION;
111 case DBUS_STRUCT_END_CHAR:
112 if (struct_depth == 0)
113 return DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
115 if (last == DBUS_STRUCT_BEGIN_CHAR)
116 return DBUS_INVALID_STRUCT_HAS_NO_FIELDS;
121 case DBUS_DICT_ENTRY_BEGIN_CHAR:
122 if (last != DBUS_TYPE_ARRAY)
123 return DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY;
125 dict_entry_depth += 1;
127 if (dict_entry_depth > DBUS_MAXIMUM_TYPE_RECURSION_DEPTH)
128 return DBUS_INVALID_EXCEEDED_MAXIMUM_DICT_ENTRY_RECURSION;
131 case DBUS_DICT_ENTRY_END_CHAR:
132 if (dict_entry_depth == 0)
133 return DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
135 if (last == DBUS_DICT_ENTRY_BEGIN_CHAR)
136 return DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS;
138 dict_entry_depth -= 1;
141 case DBUS_TYPE_STRUCT: /* doesn't appear in signatures */
142 case DBUS_TYPE_DICT_ENTRY: /* ditto */
144 return DBUS_INVALID_UNKNOWN_TYPECODE;
149 if (*p == DBUS_TYPE_ARRAY)
151 else if (*p == DBUS_STRUCT_END_CHAR ||
152 *p == DBUS_DICT_ENTRY_END_CHAR)
153 return DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
163 return DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
165 if (struct_depth > 0)
166 return DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
168 if (dict_entry_depth > 0)
169 return DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
171 _dbus_assert (last != DBUS_TYPE_ARRAY);
172 _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
173 _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
179 validate_body_helper (DBusTypeReader *reader,
181 dbus_bool_t walk_reader_to_end,
182 const unsigned char *p,
183 const unsigned char *end,
184 const unsigned char **new_p)
188 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
190 const unsigned char *a;
194 _dbus_verbose (" validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
195 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
199 /* Guarantee that p has one byte to look at */
201 return DBUS_INVALID_NOT_ENOUGH_DATA;
203 switch (current_type)
209 case DBUS_TYPE_BOOLEAN:
210 case DBUS_TYPE_INT16:
211 case DBUS_TYPE_UINT16:
212 case DBUS_TYPE_INT32:
213 case DBUS_TYPE_UINT32:
214 case DBUS_TYPE_INT64:
215 case DBUS_TYPE_UINT64:
216 case DBUS_TYPE_DOUBLE:
217 alignment = _dbus_type_get_alignment (current_type);
218 a = _DBUS_ALIGN_ADDRESS (p, alignment);
220 return DBUS_INVALID_NOT_ENOUGH_DATA;
224 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
228 if (current_type == DBUS_TYPE_BOOLEAN)
230 dbus_uint32_t v = _dbus_unpack_uint32 (byte_order,
232 if (!(v == 0 || v == 1))
233 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
239 case DBUS_TYPE_ARRAY:
240 case DBUS_TYPE_STRING:
241 case DBUS_TYPE_OBJECT_PATH:
243 dbus_uint32_t claimed_len;
245 a = _DBUS_ALIGN_ADDRESS (p, 4);
247 return DBUS_INVALID_NOT_ENOUGH_DATA;
251 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
255 claimed_len = _dbus_unpack_uint32 (byte_order, p);
258 /* p may now be == end */
259 _dbus_assert (p <= end);
261 if (current_type == DBUS_TYPE_ARRAY)
263 int array_elem_type = _dbus_type_reader_get_element_type (reader);
264 alignment = _dbus_type_get_alignment (array_elem_type);
265 p = _DBUS_ALIGN_ADDRESS (p, alignment);
268 if (claimed_len > (unsigned long) (end - p))
269 return DBUS_INVALID_STRING_LENGTH_OUT_OF_BOUNDS;
271 if (current_type == DBUS_TYPE_OBJECT_PATH)
274 _dbus_string_init_const_len (&str, p, claimed_len);
275 if (!_dbus_validate_path (&str, 0,
276 _dbus_string_get_length (&str)))
277 return DBUS_INVALID_BAD_PATH;
281 else if (current_type == DBUS_TYPE_STRING)
284 _dbus_string_init_const_len (&str, p, claimed_len);
285 if (!_dbus_string_validate_utf8 (&str, 0,
286 _dbus_string_get_length (&str)))
287 return DBUS_INVALID_BAD_UTF8_IN_STRING;
291 else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
294 DBusValidity validity;
295 const unsigned char *array_end;
297 /* Remember that the reader is types only, so we can't
298 * use it to iterate over elements. It stays the same
301 _dbus_type_reader_recurse (reader, &sub);
303 array_end = p + claimed_len;
305 while (p < array_end)
307 /* FIXME we are calling a function per array element! very bad
308 * need if (dbus_type_is_fixed(elem_type)) here to just skip
309 * big blocks of ints/bytes/etc.
312 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
313 if (validity != DBUS_VALID)
318 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
321 /* check nul termination */
322 if (current_type != DBUS_TYPE_ARRAY)
325 return DBUS_INVALID_NOT_ENOUGH_DATA;
328 return DBUS_INVALID_STRING_MISSING_NUL;
334 case DBUS_TYPE_SIGNATURE:
336 dbus_uint32_t claimed_len;
338 DBusValidity validity;
343 /* 1 is for nul termination */
344 if (claimed_len + 1 > (unsigned long) (end - p))
345 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
347 _dbus_string_init_const_len (&str, p, claimed_len);
349 _dbus_validate_signature_with_reason (&str, 0,
350 _dbus_string_get_length (&str));
352 if (validity != DBUS_VALID)
357 _dbus_assert (p < end);
358 if (*p != DBUS_TYPE_INVALID)
359 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
363 _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
367 case DBUS_TYPE_VARIANT:
369 /* 1 byte sig len, sig typecodes, align to
370 * contained-type-boundary, values.
373 /* In addition to normal signature validation, we need to be sure
374 * the signature contains only a single (possibly container) type.
376 dbus_uint32_t claimed_len;
379 DBusValidity validity;
380 int contained_alignment;
387 if (claimed_len + 1 > (unsigned long) (end - p))
388 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
390 _dbus_string_init_const_len (&sig, p, claimed_len);
391 if (!_dbus_validate_signature (&sig, 0,
392 _dbus_string_get_length (&sig)))
393 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
397 if (*p != DBUS_TYPE_INVALID)
398 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
401 contained_type = _dbus_first_type_in_signature (&sig, 0);
402 if (contained_type == DBUS_TYPE_INVALID)
403 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
405 contained_alignment = _dbus_type_get_alignment (contained_type);
407 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
409 return DBUS_INVALID_NOT_ENOUGH_DATA;
413 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
417 _dbus_type_reader_init_types_only (&sub, &sig, 0);
419 _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
421 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
422 if (validity != DBUS_VALID)
425 if (_dbus_type_reader_next (&sub))
426 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
428 _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
432 case DBUS_TYPE_DICT_ENTRY:
433 case DBUS_TYPE_STRUCT:
436 DBusValidity validity;
438 a = _DBUS_ALIGN_ADDRESS (p, 8);
440 return DBUS_INVALID_NOT_ENOUGH_DATA;
444 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
448 _dbus_type_reader_recurse (reader, &sub);
450 validity = validate_body_helper (&sub, byte_order, TRUE, p, end, &p);
451 if (validity != DBUS_VALID)
457 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
462 _dbus_verbose (" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
463 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
469 _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
470 p, end, (int) (end - p));
471 return DBUS_INVALID_NOT_ENOUGH_DATA;
474 if (walk_reader_to_end)
475 _dbus_type_reader_next (reader);
487 * Verifies that the range of value_str from value_pos to value_end is
488 * a legitimate value of type expected_signature. If this function
489 * returns #TRUE, it will be safe to iterate over the values with
490 * #DBusTypeReader. The signature is assumed to be already valid.
492 * If bytes_remaining is not #NULL, then leftover bytes will be stored
493 * there and #DBUS_VALID returned. If it is #NULL, then
494 * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
497 * @param expected_signature the expected types in the value_str
498 * @param expected_signature_start where in expected_signature is the signature
499 * @param byte_order the byte order
500 * @param bytes_remaining place to store leftover bytes
501 * @param value_str the string containing the body
502 * @param value_pos where the values start
503 * @param len length of values after value_pos
504 * @returns #DBUS_VALID if valid, reason why invalid otherwise
507 _dbus_validate_body_with_reason (const DBusString *expected_signature,
508 int expected_signature_start,
510 int *bytes_remaining,
511 const DBusString *value_str,
515 DBusTypeReader reader;
516 const unsigned char *p;
517 const unsigned char *end;
518 DBusValidity validity;
520 _dbus_assert (len >= 0);
521 _dbus_assert (value_pos >= 0);
522 _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
524 _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
525 value_pos, len, _dbus_string_get_const_data_len (expected_signature,
526 expected_signature_start,
529 _dbus_type_reader_init_types_only (&reader,
530 expected_signature, expected_signature_start);
532 p = _dbus_string_get_const_data_len (value_str, value_pos, len);
535 validity = validate_body_helper (&reader, byte_order, TRUE, p, end, &p);
536 if (validity != DBUS_VALID)
541 *bytes_remaining = end - p;
545 return DBUS_INVALID_TOO_MUCH_DATA;
548 _dbus_assert (p == end);
554 * Determine wether the given charater is valid as the first charater
557 #define VALID_INITIAL_NAME_CHARACTER(c) \
558 ( ((c) >= 'A' && (c) <= 'Z') || \
559 ((c) >= 'a' && (c) <= 'z') || \
563 * Determine wether the given charater is valid as a second or later
564 * character in a name
566 #define VALID_NAME_CHARACTER(c) \
567 ( ((c) >= '0' && (c) <= '9') || \
568 ((c) >= 'A' && (c) <= 'Z') || \
569 ((c) >= 'a' && (c) <= 'z') || \
573 * Checks that the given range of the string is a valid object path
574 * name in the D-BUS protocol. Part of the validation ensures that
575 * the object path contains only ASCII.
577 * @todo this is inconsistent with most of DBusString in that
578 * it allows a start,len range that extends past the string end.
580 * @todo change spec to disallow more things, such as spaces in the
583 * @param str the string
584 * @param start first byte index to check
585 * @param len number of bytes to check
586 * @returns #TRUE if the byte range exists and is a valid name
589 _dbus_validate_path (const DBusString *str,
593 const unsigned char *s;
594 const unsigned char *end;
595 const unsigned char *last_slash;
597 _dbus_assert (start >= 0);
598 _dbus_assert (len >= 0);
599 _dbus_assert (start <= _dbus_string_get_length (str));
601 if (len > _dbus_string_get_length (str) - start)
607 s = _dbus_string_get_const_data (str) + start;
619 if ((s - last_slash) < 2)
620 return FALSE; /* no empty path components allowed */
626 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
633 if ((end - last_slash) < 2 &&
635 return FALSE; /* trailing slash not allowed unless the string is "/" */
641 * Checks that the given range of the string is a valid interface name
642 * in the D-BUS protocol. This includes a length restriction and an
643 * ASCII subset, see the specification.
645 * @todo this is inconsistent with most of DBusString in that
646 * it allows a start,len range that extends past the string end.
648 * @param str the string
649 * @param start first byte index to check
650 * @param len number of bytes to check
651 * @returns #TRUE if the byte range exists and is a valid name
654 _dbus_validate_interface (const DBusString *str,
658 const unsigned char *s;
659 const unsigned char *end;
660 const unsigned char *iface;
661 const unsigned char *last_dot;
663 _dbus_assert (start >= 0);
664 _dbus_assert (len >= 0);
665 _dbus_assert (start <= _dbus_string_get_length (str));
667 if (len > _dbus_string_get_length (str) - start)
670 if (len > DBUS_MAXIMUM_NAME_LENGTH)
677 iface = _dbus_string_get_const_data (str) + start;
681 /* check special cases of first char so it doesn't have to be done
682 * in the loop. Note we know len > 0
684 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
686 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
695 if (_DBUS_UNLIKELY ((s + 1) == end))
697 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
700 ++s; /* we just validated the next char, so skip two */
702 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
710 if (_DBUS_UNLIKELY (last_dot == NULL))
717 * Checks that the given range of the string is a valid member name
718 * in the D-BUS protocol. This includes a length restriction, etc.,
719 * see the specification.
721 * @todo this is inconsistent with most of DBusString in that
722 * it allows a start,len range that extends past the string end.
724 * @param str the string
725 * @param start first byte index to check
726 * @param len number of bytes to check
727 * @returns #TRUE if the byte range exists and is a valid name
730 _dbus_validate_member (const DBusString *str,
734 const unsigned char *s;
735 const unsigned char *end;
736 const unsigned char *member;
738 _dbus_assert (start >= 0);
739 _dbus_assert (len >= 0);
740 _dbus_assert (start <= _dbus_string_get_length (str));
742 if (len > _dbus_string_get_length (str) - start)
745 if (len > DBUS_MAXIMUM_NAME_LENGTH)
751 member = _dbus_string_get_const_data (str) + start;
755 /* check special cases of first char so it doesn't have to be done
756 * in the loop. Note we know len > 0
759 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
766 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
778 * Checks that the given range of the string is a valid error name
779 * in the D-BUS protocol. This includes a length restriction, etc.,
780 * see the specification.
782 * @todo this is inconsistent with most of DBusString in that
783 * it allows a start,len range that extends past the string end.
785 * @param str the string
786 * @param start first byte index to check
787 * @param len number of bytes to check
788 * @returns #TRUE if the byte range exists and is a valid name
791 _dbus_validate_error_name (const DBusString *str,
795 /* Same restrictions as interface name at the moment */
796 return _dbus_validate_interface (str, start, len);
799 /* This assumes the first char exists and is ':' */
801 _dbus_validate_unique_name (const DBusString *str,
805 const unsigned char *s;
806 const unsigned char *end;
807 const unsigned char *name;
809 _dbus_assert (start >= 0);
810 _dbus_assert (len >= 0);
811 _dbus_assert (start <= _dbus_string_get_length (str));
813 if (len > _dbus_string_get_length (str) - start)
816 if (len > DBUS_MAXIMUM_NAME_LENGTH)
819 _dbus_assert (len > 0);
821 name = _dbus_string_get_const_data (str) + start;
823 _dbus_assert (*name == ':');
830 if (_DBUS_UNLIKELY ((s + 1) == end))
832 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
834 ++s; /* we just validated the next char, so skip two */
836 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
848 * Checks that the given range of the string is a valid bus name in
849 * the D-BUS protocol. This includes a length restriction, etc., see
852 * @todo this is inconsistent with most of DBusString in that
853 * it allows a start,len range that extends past the string end.
855 * @param str the string
856 * @param start first byte index to check
857 * @param len number of bytes to check
858 * @returns #TRUE if the byte range exists and is a valid name
861 _dbus_validate_bus_name (const DBusString *str,
865 if (_DBUS_UNLIKELY (len == 0))
867 if (_dbus_string_get_byte (str, start) == ':')
868 return _dbus_validate_unique_name (str, start, len);
870 return _dbus_validate_interface (str, start, len);
874 * Checks that the given range of the string is a valid message type
875 * signature in the D-BUS protocol.
877 * @todo this is inconsistent with most of DBusString in that
878 * it allows a start,len range that extends past the string end.
880 * @param str the string
881 * @param start first byte index to check
882 * @param len number of bytes to check
883 * @returns #TRUE if the byte range exists and is a valid signature
886 _dbus_validate_signature (const DBusString *str,
890 _dbus_assert (start >= 0);
891 _dbus_assert (start <= _dbus_string_get_length (str));
892 _dbus_assert (len >= 0);
894 if (len > _dbus_string_get_length (str) - start)
897 return _dbus_validate_signature_with_reason (str, start, len) == DBUS_VALID;
900 /** define _dbus_check_is_valid_path() */
901 DEFINE_DBUS_NAME_CHECK(path);
902 /** define _dbus_check_is_valid_interface() */
903 DEFINE_DBUS_NAME_CHECK(interface);
904 /** define _dbus_check_is_valid_member() */
905 DEFINE_DBUS_NAME_CHECK(member);
906 /** define _dbus_check_is_valid_error_name() */
907 DEFINE_DBUS_NAME_CHECK(error_name);
908 /** define _dbus_check_is_valid_bus_name() */
909 DEFINE_DBUS_NAME_CHECK(bus_name);
910 /** define _dbus_check_is_valid_signature() */
911 DEFINE_DBUS_NAME_CHECK(signature);
915 /* tests in dbus-marshal-validate-util.c */