client/X11: harden xf_cliprdr_parse_server_format_list()
authorilammy <a.lozovsky@gmail.com>
Mon, 22 Feb 2016 22:14:30 +0000 (00:14 +0200)
committerilammy <a.lozovsky@gmail.com>
Mon, 22 Feb 2016 23:20:34 +0000 (01:20 +0200)
commit93fc349ce6174ccdae300ffd248e18151daa48d1
tree7a6020c797b3f13419d0400d343d0313c51e7093
parent7bce7ef372a483e221c4c08dec076c58212ba973
client/X11: harden xf_cliprdr_parse_server_format_list()

* Make sure that numFormats has reasonable value

This will help catching errors like writing -1 as an unsigned number
of formats into the serialized stream, or trying to read the property
after someone else erroneosly messed with it, or other similar mistakes
which would result into reading and then sending garbage to the server.

We read the list xf_cliprdr_get_raw_server_formats() from an X window
property. Properties generally cannot be larger than 4 KB and each
format requires at least 5 bytes (most of them are named, though),
which gives us 512-ish limit on the number of formats we can squeeze
into the property.

However, it's hard to find an application that provides more than
20 formats (I've seen like 15 for MS Office apps), thus I believe
we can safely assume than anything that does not fit into a byte
means that we are reading garbage rather than a good format list.

* Check for the end of stream when reading format names

This also prevents reading garbage and getting segmentation faults
and Valgrind warnings when somebody somewhere sometimes forgets to
put a terminating null character where it belongs.

strnlen() and strndup() functions are provided by POSIX.1-2008
which we can reasonably expect to be available in 2016.
client/X11/xf_cliprdr.c