img: fix UAF on Reconfigure by refcounting

Before this commit, there was a use-after-free bug on Reconfigure:
- theme_finish() destroys lab_imgs for titlebar icons
- For some reason, undecorate() calls _create_buffer() in
  scaled-img-buffer.c, which calls img_render() on a destroyed lab_img.

So in this commit, the lifetime of lab_img is expanded to when the
scaled_img_buffers referencing it are all destroyed. This is achieved by
calling lab_img_copy() when setting a lab_img to scaled_img_buffer and
calling lab_img_destroy() when clearing a lab_img.

Now that scaled_img_buffer.img are always different, lab_img_equal() is
added to compare the content of scaled_img_buffer.img.
This commit is contained in:
tokyo4j 2025-01-04 00:05:12 +09:00 committed by Consolatis
parent 90a8c3e793
commit 70fb713874
6 changed files with 32 additions and 11 deletions

View file

@ -218,3 +218,18 @@ lab_img_destroy(struct lab_img *img)
wl_array_release(&img->modifiers);
free(img);
}
bool
lab_img_equal(struct lab_img *img_a, struct lab_img *img_b)
{
if (img_a == img_b) {
return true;
}
if (!img_a || !img_b || img_a->cache != img_b->cache
|| img_a->modifiers.size != img_b->modifiers.size) {
return false;
}
return img_a->modifiers.size == 0
|| !memcmp(img_a->modifiers.data, img_b->modifiers.data,
img_a->modifiers.size);
}