Heap buffer overflow in xdg_user_dir_lookup() crashes the slideshow saver
Summary
The slideshow saver overflows a heap buffer every time it is started without --location (which is the default Exec line of xfce-personal-slideshow.desktop). Depending on the heap layout this goes unnoticed, aborts in g_free(), or crashes later inside GTK with SIGSEGV.
Cause
savers/xdg-user-dir-lookup.c, xdg_user_dir_lookup_with_fallback(). Commit 7968b781 ("Use GLib wrappers for memory allocation", 2023-05-20) changed the allocation for paths relative to $HOME:
if (relative) {
- user_dir = (char *) malloc (strlen (home_dir) + 1 + strlen (p) + 1);
- if (user_dir == NULL)
- goto error2;
-
- strcpy (user_dir, home_dir);
- strcat (user_dir, "/");
+ user_dir = g_strconcat (home_dir, "/", NULL);g_strconcat() allocates only strlen (home_dir) + 2 bytes, but the loop right below still appends the rest of the path to it:
d = user_dir + strlen (user_dir);
while (*p && *p != '"') {
...
*d++ = *p++;
}
*d = 0;So the whole directory name (e.g. Pictures) is written past the end of the buffer. This affects every XDG_*_DIR="$HOME/..." entry in ~/.config/user-dirs.dirs, i.e. the default configuration.
Steps to reproduce
- Have the default
XDG_PICTURES_DIR="$HOME/Pictures"(any$HOME/...value) in~/.config/user-dirs.dirs. - Open the screensaver preferences and select the "Slideshow" theme so that the preview starts
/usr/libexec/xfce4-screensaver/slideshowwithout--location.
With glibc heap checking enabled the saver aborts on every start:
$ LD_PRELOAD=libc_malloc_debug.so MALLOC_CHECK_=3 slideshow (embedded in a GtkSocket)
free(): invalid pointer
#11 g_free (mem=<optimized out>) at ../../../glib/gmem.c:208
#12 main (argc=<optimized out>, argv=<optimized out>) at ../savers/slideshow.c:126Passing --location=/home/user/Pictures explicitly avoids the lookup and the saver runs cleanly under the same checks.
Building xdg-user-dir-lookup.c alone with AddressSanitizer and calling xdg_user_dir_lookup ("PICTURES"):
==22038==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x73d2e97e0057
WRITE of size 1 at 0x73d2e97e0057 thread T0
#0 xdg_user_dir_lookup_with_fallback savers/xdg-user-dir-lookup.c:132
#1 xdg_user_dir_lookup savers/xdg-user-dir-lookup.c:169
0x73d2e97e0057 is located 0 bytes after 23-byte region [0x73d2e97e0040,0x73d2e97e0057)
allocated by:
#1 g_strconcatCrash seen in normal use
Without heap checking the corruption surfaced later, during gtk_widget_show():
slideshow[78338]: segfault at 5a4635e269d8 ip 00007853f76b5704 error 4 in libc.so.6
#0 musable (mem=0x59d2c77d0280) at ./malloc/malloc.c:4839
#1 __GI___libc_realloc (oldmem=0x59d2c77d0280, bytes=64) at ./malloc/malloc.c:3410
#2 g_realloc (n_bytes=64) at ../../../glib/gmem.c:171
#3 realloc_arrays (hash_table=0x59d2c77d9f90, is_a_set=1) at ../../../glib/ghash.c:680
#4 g_hash_table_resize at ../../../glib/ghash.c:834
#8 g_hash_table_add at ../../../glib/ghash.c:1674
#9 g_param_spec_pool_insert at ../../../gobject/gparam.c:1051
#12 gtk_container_class_install_child_properties at ../../../gtk/gtkcontainer.c:1599
#14 gtk_box_class_init at ../../../gtk/gtkbox.c:371
#26 gtk_window_show at ../../../gtk/gtkwindow.c:6345
#34 main at ../savers/slideshow.c:150The corrupted chunk (0x59d2c77d0280) is the one right after the buffer returned by the lookup (location = 0x59d2c77d0260).
Versions
- xfce4-screensaver 4.20.1 (Ubuntu 26.04 package 4.20.1-1build1); the same code is still present in current master (bf7a9bbf).
- GLib 2.88.0, GTK 3, glibc from Ubuntu 26.04, X11 session.
Fix
Allocate the full size again before copying. Merge request to follow.