After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 759658 - xprops: Protect against broken WM_CLASS property implementations
xprops: Protect against broken WM_CLASS property implementations
Status: RESOLVED FIXED
Product: mutter
Classification: Core
Component: general
git master
Other Linux
: Normal normal
: ---
Assigned To: mutter-maint
mutter-maint
: 757689 761679 762073 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2015-12-19 07:59 UTC by Sebastian Keller
Modified: 2016-02-19 18:03 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
xprops: Protect against broken WM_CLASS property implementations (2.92 KB, patch)
2015-12-19 07:59 UTC, Sebastian Keller
none Details | Review
xprops: Null-terminate property reply values (1.70 KB, patch)
2016-01-10 03:33 UTC, Sebastian Keller
none Details | Review
xprops: Null-terminate property reply values (1.75 KB, patch)
2016-01-10 18:07 UTC, Sebastian Keller
none Details | Review
xprops: Null-terminate property reply values (1.75 KB, patch)
2016-01-11 19:31 UTC, Sebastian Keller
committed Details | Review

Description Sebastian Keller 2015-12-19 07:59:15 UTC
Created attachment 317656 [details] [review]
xprops: Protect against broken WM_CLASS property implementations

According to the ICCCM the WM_CLASS property differs from a regular
STRING property in that it consists of two consecutive NULL-terminated
strings where both NULLs are considered part of the property data.

Some broken applications (notably java/awt/swing applications) however
only include the separating NULL and do not have a trailing NULL. In
these cases the old code was reading past the actual property data and
considered that to be part of the class string as well. This would
result in broken window/app matching and in the case that the data could
not be interpreted as a valid UTF-8 string in the window-switcher and
hot corner not working or the dash being empty.

This patch tries to protect against all possible ways an application
might get this property wrong.
Comment 1 Owen Taylor 2016-01-04 15:27:29 UTC
Hmm, not reviewing right at the moment, but two notes:

 1) \0 is a NUL not a NULL
 2) Are you aware (from the man page)

       XGetWindowProperty always allocates one extra byte in prop_return (even
       if the property is zero length) and sets it to zero so that simple
       properties consisting of characters do not have to be copied into yet
       another string before use.

    If we are reading the results of XGetWindowProperty *directly* then it wouldn't be possible to read past the end from a missing nul. But I could imagine that we're copying the data or that the async-get-property code doesnt' have the same behavior.
Comment 2 Jasper St. Pierre (not reading bugmail) 2016-01-04 16:12:42 UTC
Reminder that we switched to xcb_get_window_property -- I thought the NUL would be on the wire, but if not, we should implement that ourselves here:

https://git.gnome.org/browse/mutter/tree/src/x11/xprops.c#n210
Comment 3 Sebastian Keller 2016-01-10 03:33:03 UTC
Created attachment 318634 [details] [review]
xprops: Null-terminate property reply values

Thanks for pointing me in the right direction. I've now looked at this again and it seems like xcb does intentionally not use null-terminated strings:

http://xcb.freedesktop.org/XcbRationale/
Comment 4 Jasper St. Pierre (not reading bugmail) 2016-01-10 04:32:57 UTC
Review of attachment 318634 [details] [review]:

::: src/x11/xprops.c
@@ +214,3 @@
+      length = xcb_get_property_value_length (reply);
+      // Leave room for a trailing '\0' since xcb doesn't return null-terminated
+      // strings

Use /* comments */

@@ +217,3 @@
+      results->prop = g_malloc (length + 1);
+      memmove (results->prop, xcb_get_property_value (reply), length);
+      results->prop[length] = '\0';

You can simply use g_strndup for this -- sorry.
Comment 5 Sebastian Keller 2016-01-10 11:53:17 UTC
Wouldn't using g_strndup break if this wasn't a string type property or a null-separated list of strings?
Comment 6 Jasper St. Pierre (not reading bugmail) 2016-01-10 16:50:43 UTC
Oh, I forgot that NULs are valid in property values.

OK, can you fix the comment issue and push? It's ACN after that.
Comment 7 Sebastian Keller 2016-01-10 18:07:08 UTC
Created attachment 318673 [details] [review]
xprops: Null-terminate property reply values

Thanks for the review, here is a version with the fixed comments. I've also slightly changed the commit message since I mentioned crashes in the previous version when I was actually thinking about the window-switcher not working due to invalid UTF-8 strings.
Could you push the patch for me since I don't have a git account?
Comment 8 Rui Matos 2016-01-11 17:34:34 UTC
Review of attachment 318673 [details] [review]:

::: src/x11/xprops.c
@@ +217,3 @@
+       */
+      results->prop = g_malloc (length + 1);
+      memmove (results->prop, xcb_get_property_value (reply), length);

Nit: there's no reason to use memmove instead of memcpy here since the buffers don't overlap.
Comment 9 Sebastian Keller 2016-01-11 19:31:00 UTC
Created attachment 318820 [details] [review]
xprops: Null-terminate property reply values

Thanks!
Here is the patch with memcpy instead of memmove.
Comment 10 Rui Matos 2016-01-12 15:55:02 UTC
Thanks for the patch

Attachment 318820 [details] pushed as a7a376a - xprops: Null-terminate property reply values
Comment 11 Rui Matos 2016-01-12 15:58:37 UTC
*** Bug 757689 has been marked as a duplicate of this bug. ***
Comment 12 Rui Matos 2016-02-15 13:01:09 UTC
*** Bug 762073 has been marked as a duplicate of this bug. ***
Comment 13 Rui Matos 2016-02-19 18:03:25 UTC
*** Bug 761679 has been marked as a duplicate of this bug. ***