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;
158 if (last == DBUS_DICT_ENTRY_BEGIN_CHAR &&
159 !_dbus_type_is_basic (*p))
160 return DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE;
167 return DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE;
169 if (struct_depth > 0)
170 return DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED;
172 if (dict_entry_depth > 0)
173 return DBUS_INVALID_DICT_ENTRY_STARTED_BUT_NOT_ENDED;
175 _dbus_assert (last != DBUS_TYPE_ARRAY);
176 _dbus_assert (last != DBUS_STRUCT_BEGIN_CHAR);
177 _dbus_assert (last != DBUS_DICT_ENTRY_BEGIN_CHAR);
183 validate_body_helper (DBusTypeReader *reader,
185 dbus_bool_t walk_reader_to_end,
186 const unsigned char *p,
187 const unsigned char *end,
188 const unsigned char **new_p)
192 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
194 const unsigned char *a;
198 _dbus_verbose (" validating value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
199 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
203 /* Guarantee that p has one byte to look at */
205 return DBUS_INVALID_NOT_ENOUGH_DATA;
207 switch (current_type)
213 case DBUS_TYPE_BOOLEAN:
214 case DBUS_TYPE_INT16:
215 case DBUS_TYPE_UINT16:
216 case DBUS_TYPE_INT32:
217 case DBUS_TYPE_UINT32:
218 case DBUS_TYPE_INT64:
219 case DBUS_TYPE_UINT64:
220 case DBUS_TYPE_DOUBLE:
221 alignment = _dbus_type_get_alignment (current_type);
222 a = _DBUS_ALIGN_ADDRESS (p, alignment);
224 return DBUS_INVALID_NOT_ENOUGH_DATA;
228 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
232 if (current_type == DBUS_TYPE_BOOLEAN)
234 dbus_uint32_t v = _dbus_unpack_uint32 (byte_order,
236 if (!(v == 0 || v == 1))
237 return DBUS_INVALID_BOOLEAN_NOT_ZERO_OR_ONE;
243 case DBUS_TYPE_ARRAY:
244 case DBUS_TYPE_STRING:
245 case DBUS_TYPE_OBJECT_PATH:
247 dbus_uint32_t claimed_len;
249 a = _DBUS_ALIGN_ADDRESS (p, 4);
251 return DBUS_INVALID_NOT_ENOUGH_DATA;
255 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
259 claimed_len = _dbus_unpack_uint32 (byte_order, p);
262 /* p may now be == end */
263 _dbus_assert (p <= end);
265 if (current_type == DBUS_TYPE_ARRAY)
267 int array_elem_type = _dbus_type_reader_get_element_type (reader);
268 alignment = _dbus_type_get_alignment (array_elem_type);
269 p = _DBUS_ALIGN_ADDRESS (p, alignment);
272 if (claimed_len > (unsigned long) (end - p))
273 return DBUS_INVALID_LENGTH_OUT_OF_BOUNDS;
275 if (current_type == DBUS_TYPE_OBJECT_PATH)
278 _dbus_string_init_const_len (&str, p, claimed_len);
279 if (!_dbus_validate_path (&str, 0,
280 _dbus_string_get_length (&str)))
281 return DBUS_INVALID_BAD_PATH;
285 else if (current_type == DBUS_TYPE_STRING)
288 _dbus_string_init_const_len (&str, p, claimed_len);
289 if (!_dbus_string_validate_utf8 (&str, 0,
290 _dbus_string_get_length (&str)))
291 return DBUS_INVALID_BAD_UTF8_IN_STRING;
295 else if (current_type == DBUS_TYPE_ARRAY && claimed_len > 0)
298 DBusValidity validity;
299 const unsigned char *array_end;
301 if (claimed_len > DBUS_MAXIMUM_ARRAY_LENGTH)
302 return DBUS_INVALID_ARRAY_LENGTH_EXCEEDS_MAXIMUM;
304 /* Remember that the reader is types only, so we can't
305 * use it to iterate over elements. It stays the same
308 _dbus_type_reader_recurse (reader, &sub);
310 array_end = p + claimed_len;
312 while (p < array_end)
314 /* FIXME we are calling a function per array element! very bad
315 * need if (dbus_type_is_fixed(elem_type)) here to just skip
316 * big blocks of ints/bytes/etc.
319 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
320 if (validity != DBUS_VALID)
325 return DBUS_INVALID_ARRAY_LENGTH_INCORRECT;
328 /* check nul termination */
329 if (current_type != DBUS_TYPE_ARRAY)
332 return DBUS_INVALID_NOT_ENOUGH_DATA;
335 return DBUS_INVALID_STRING_MISSING_NUL;
341 case DBUS_TYPE_SIGNATURE:
343 dbus_uint32_t claimed_len;
345 DBusValidity validity;
350 /* 1 is for nul termination */
351 if (claimed_len + 1 > (unsigned long) (end - p))
352 return DBUS_INVALID_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
354 _dbus_string_init_const_len (&str, p, claimed_len);
356 _dbus_validate_signature_with_reason (&str, 0,
357 _dbus_string_get_length (&str));
359 if (validity != DBUS_VALID)
364 _dbus_assert (p < end);
365 if (*p != DBUS_TYPE_INVALID)
366 return DBUS_INVALID_SIGNATURE_MISSING_NUL;
370 _dbus_verbose ("p = %p end = %p claimed_len %u\n", p, end, claimed_len);
374 case DBUS_TYPE_VARIANT:
376 /* 1 byte sig len, sig typecodes, align to
377 * contained-type-boundary, values.
380 /* In addition to normal signature validation, we need to be sure
381 * the signature contains only a single (possibly container) type.
383 dbus_uint32_t claimed_len;
386 DBusValidity validity;
387 int contained_alignment;
394 if (claimed_len + 1 > (unsigned long) (end - p))
395 return DBUS_INVALID_VARIANT_SIGNATURE_LENGTH_OUT_OF_BOUNDS;
397 _dbus_string_init_const_len (&sig, p, claimed_len);
398 if (!_dbus_validate_signature (&sig, 0,
399 _dbus_string_get_length (&sig)))
400 return DBUS_INVALID_VARIANT_SIGNATURE_BAD;
404 if (*p != DBUS_TYPE_INVALID)
405 return DBUS_INVALID_VARIANT_SIGNATURE_MISSING_NUL;
408 contained_type = _dbus_first_type_in_signature (&sig, 0);
409 if (contained_type == DBUS_TYPE_INVALID)
410 return DBUS_INVALID_VARIANT_SIGNATURE_EMPTY;
412 contained_alignment = _dbus_type_get_alignment (contained_type);
414 a = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
416 return DBUS_INVALID_NOT_ENOUGH_DATA;
420 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
424 _dbus_type_reader_init_types_only (&sub, &sig, 0);
426 _dbus_assert (_dbus_type_reader_get_current_type (&sub) != DBUS_TYPE_INVALID);
428 validity = validate_body_helper (&sub, byte_order, FALSE, p, end, &p);
429 if (validity != DBUS_VALID)
432 if (_dbus_type_reader_next (&sub))
433 return DBUS_INVALID_VARIANT_SIGNATURE_SPECIFIES_MULTIPLE_VALUES;
435 _dbus_assert (_dbus_type_reader_get_current_type (&sub) == DBUS_TYPE_INVALID);
439 case DBUS_TYPE_DICT_ENTRY:
440 case DBUS_TYPE_STRUCT:
443 DBusValidity validity;
445 a = _DBUS_ALIGN_ADDRESS (p, 8);
447 return DBUS_INVALID_NOT_ENOUGH_DATA;
451 return DBUS_INVALID_ALIGNMENT_PADDING_NOT_NUL;
455 _dbus_type_reader_recurse (reader, &sub);
457 validity = validate_body_helper (&sub, byte_order, TRUE, p, end, &p);
458 if (validity != DBUS_VALID)
464 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
469 _dbus_verbose (" validated value of type %s type reader %p type_pos %d p %p end %p %d remain\n",
470 _dbus_type_to_string (current_type), reader, reader->type_pos, p, end,
476 _dbus_verbose ("not enough data!!! p = %p end = %p end-p = %d\n",
477 p, end, (int) (end - p));
478 return DBUS_INVALID_NOT_ENOUGH_DATA;
481 if (walk_reader_to_end)
482 _dbus_type_reader_next (reader);
494 * Verifies that the range of value_str from value_pos to value_end is
495 * a legitimate value of type expected_signature. If this function
496 * returns #TRUE, it will be safe to iterate over the values with
497 * #DBusTypeReader. The signature is assumed to be already valid.
499 * If bytes_remaining is not #NULL, then leftover bytes will be stored
500 * there and #DBUS_VALID returned. If it is #NULL, then
501 * #DBUS_INVALID_TOO_MUCH_DATA will be returned if bytes are left
504 * @param expected_signature the expected types in the value_str
505 * @param expected_signature_start where in expected_signature is the signature
506 * @param byte_order the byte order
507 * @param bytes_remaining place to store leftover bytes
508 * @param value_str the string containing the body
509 * @param value_pos where the values start
510 * @param len length of values after value_pos
511 * @returns #DBUS_VALID if valid, reason why invalid otherwise
514 _dbus_validate_body_with_reason (const DBusString *expected_signature,
515 int expected_signature_start,
517 int *bytes_remaining,
518 const DBusString *value_str,
522 DBusTypeReader reader;
523 const unsigned char *p;
524 const unsigned char *end;
525 DBusValidity validity;
527 _dbus_assert (len >= 0);
528 _dbus_assert (value_pos >= 0);
529 _dbus_assert (value_pos <= _dbus_string_get_length (value_str) - len);
531 _dbus_verbose ("validating body from pos %d len %d sig '%s'\n",
532 value_pos, len, _dbus_string_get_const_data_len (expected_signature,
533 expected_signature_start,
536 _dbus_type_reader_init_types_only (&reader,
537 expected_signature, expected_signature_start);
539 p = _dbus_string_get_const_data_len (value_str, value_pos, len);
542 validity = validate_body_helper (&reader, byte_order, TRUE, p, end, &p);
543 if (validity != DBUS_VALID)
548 *bytes_remaining = end - p;
552 return DBUS_INVALID_TOO_MUCH_DATA;
555 _dbus_assert (p == end);
561 * Determine wether the given charater is valid as the first charater
564 #define VALID_INITIAL_NAME_CHARACTER(c) \
565 ( ((c) >= 'A' && (c) <= 'Z') || \
566 ((c) >= 'a' && (c) <= 'z') || \
570 * Determine wether the given charater is valid as a second or later
571 * character in a name
573 #define VALID_NAME_CHARACTER(c) \
574 ( ((c) >= '0' && (c) <= '9') || \
575 ((c) >= 'A' && (c) <= 'Z') || \
576 ((c) >= 'a' && (c) <= 'z') || \
580 * Checks that the given range of the string is a valid object path
581 * name in the D-BUS protocol. Part of the validation ensures that
582 * the object path contains only ASCII.
584 * @todo this is inconsistent with most of DBusString in that
585 * it allows a start,len range that extends past the string end.
587 * @todo change spec to disallow more things, such as spaces in the
590 * @param str the string
591 * @param start first byte index to check
592 * @param len number of bytes to check
593 * @returns #TRUE if the byte range exists and is a valid name
596 _dbus_validate_path (const DBusString *str,
600 const unsigned char *s;
601 const unsigned char *end;
602 const unsigned char *last_slash;
604 _dbus_assert (start >= 0);
605 _dbus_assert (len >= 0);
606 _dbus_assert (start <= _dbus_string_get_length (str));
608 if (len > _dbus_string_get_length (str) - start)
614 s = _dbus_string_get_const_data (str) + start;
626 if ((s - last_slash) < 2)
627 return FALSE; /* no empty path components allowed */
633 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
640 if ((end - last_slash) < 2 &&
642 return FALSE; /* trailing slash not allowed unless the string is "/" */
648 * Checks that the given range of the string is a valid interface name
649 * in the D-BUS protocol. This includes a length restriction and an
650 * ASCII subset, see the specification.
652 * @todo this is inconsistent with most of DBusString in that
653 * it allows a start,len range that extends past the string end.
655 * @param str the string
656 * @param start first byte index to check
657 * @param len number of bytes to check
658 * @returns #TRUE if the byte range exists and is a valid name
661 _dbus_validate_interface (const DBusString *str,
665 const unsigned char *s;
666 const unsigned char *end;
667 const unsigned char *iface;
668 const unsigned char *last_dot;
670 _dbus_assert (start >= 0);
671 _dbus_assert (len >= 0);
672 _dbus_assert (start <= _dbus_string_get_length (str));
674 if (len > _dbus_string_get_length (str) - start)
677 if (len > DBUS_MAXIMUM_NAME_LENGTH)
684 iface = _dbus_string_get_const_data (str) + start;
688 /* check special cases of first char so it doesn't have to be done
689 * in the loop. Note we know len > 0
691 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
693 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
702 if (_DBUS_UNLIKELY ((s + 1) == end))
704 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
707 ++s; /* we just validated the next char, so skip two */
709 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
717 if (_DBUS_UNLIKELY (last_dot == NULL))
724 * Checks that the given range of the string is a valid member name
725 * in the D-BUS protocol. This includes a length restriction, etc.,
726 * see the specification.
728 * @todo this is inconsistent with most of DBusString in that
729 * it allows a start,len range that extends past the string end.
731 * @param str the string
732 * @param start first byte index to check
733 * @param len number of bytes to check
734 * @returns #TRUE if the byte range exists and is a valid name
737 _dbus_validate_member (const DBusString *str,
741 const unsigned char *s;
742 const unsigned char *end;
743 const unsigned char *member;
745 _dbus_assert (start >= 0);
746 _dbus_assert (len >= 0);
747 _dbus_assert (start <= _dbus_string_get_length (str));
749 if (len > _dbus_string_get_length (str) - start)
752 if (len > DBUS_MAXIMUM_NAME_LENGTH)
758 member = _dbus_string_get_const_data (str) + start;
762 /* check special cases of first char so it doesn't have to be done
763 * in the loop. Note we know len > 0
766 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
773 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
785 * Checks that the given range of the string is a valid error name
786 * in the D-BUS protocol. This includes a length restriction, etc.,
787 * see the specification.
789 * @todo this is inconsistent with most of DBusString in that
790 * it allows a start,len range that extends past the string end.
792 * @param str the string
793 * @param start first byte index to check
794 * @param len number of bytes to check
795 * @returns #TRUE if the byte range exists and is a valid name
798 _dbus_validate_error_name (const DBusString *str,
802 /* Same restrictions as interface name at the moment */
803 return _dbus_validate_interface (str, start, len);
806 /* This assumes the first char exists and is ':' */
808 _dbus_validate_unique_name (const DBusString *str,
812 const unsigned char *s;
813 const unsigned char *end;
814 const unsigned char *name;
816 _dbus_assert (start >= 0);
817 _dbus_assert (len >= 0);
818 _dbus_assert (start <= _dbus_string_get_length (str));
820 if (len > _dbus_string_get_length (str) - start)
823 if (len > DBUS_MAXIMUM_NAME_LENGTH)
826 _dbus_assert (len > 0);
828 name = _dbus_string_get_const_data (str) + start;
830 _dbus_assert (*name == ':');
837 if (_DBUS_UNLIKELY ((s + 1) == end))
839 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
841 ++s; /* we just validated the next char, so skip two */
843 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
855 * Checks that the given range of the string is a valid bus name in
856 * the D-BUS protocol. This includes a length restriction, etc., see
859 * @todo this is inconsistent with most of DBusString in that
860 * it allows a start,len range that extends past the string end.
862 * @param str the string
863 * @param start first byte index to check
864 * @param len number of bytes to check
865 * @returns #TRUE if the byte range exists and is a valid name
868 _dbus_validate_bus_name (const DBusString *str,
872 if (_DBUS_UNLIKELY (len == 0))
874 if (_dbus_string_get_byte (str, start) == ':')
875 return _dbus_validate_unique_name (str, start, len);
877 return _dbus_validate_interface (str, start, len);
881 * Checks that the given range of the string is a valid message type
882 * signature in the D-BUS protocol.
884 * @todo this is inconsistent with most of DBusString in that
885 * it allows a start,len range that extends past the string end.
887 * @param str the string
888 * @param start first byte index to check
889 * @param len number of bytes to check
890 * @returns #TRUE if the byte range exists and is a valid signature
893 _dbus_validate_signature (const DBusString *str,
897 _dbus_assert (start >= 0);
898 _dbus_assert (start <= _dbus_string_get_length (str));
899 _dbus_assert (len >= 0);
901 if (len > _dbus_string_get_length (str) - start)
904 return _dbus_validate_signature_with_reason (str, start, len) == DBUS_VALID;
907 /** define _dbus_check_is_valid_path() */
908 DEFINE_DBUS_NAME_CHECK(path);
909 /** define _dbus_check_is_valid_interface() */
910 DEFINE_DBUS_NAME_CHECK(interface);
911 /** define _dbus_check_is_valid_member() */
912 DEFINE_DBUS_NAME_CHECK(member);
913 /** define _dbus_check_is_valid_error_name() */
914 DEFINE_DBUS_NAME_CHECK(error_name);
915 /** define _dbus_check_is_valid_bus_name() */
916 DEFINE_DBUS_NAME_CHECK(bus_name);
917 /** define _dbus_check_is_valid_signature() */
918 DEFINE_DBUS_NAME_CHECK(signature);
922 /* tests in dbus-marshal-validate-util.c */