Server crashes when a pane sends a damaged zlib-compressed kitty image (o=z)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 32/100
Research direction
Start from the frames terminal.kitty.graphics_image.LoadingImage.complete and graphics_exec.transmit, then follow the o=z branch into the libghostty-vt Zig flate reader; compare it with the f=100 PNG path that already answers EINVAL. Run the supplied kitty-zlib-damaged.py against a server launched with DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib so the over-read faults every run. Check whether decompressed output is bounded by the s=/v= dimensions before writing the pixel buffer. Done means damaged o=z data returns EINVAL and the server stays up.
Written by the indexing model from the issue text.
Description
Is this a reproducible bug?
- I confirm this is a reproducible bug, not a feature request, idea, question, contribution proposal, or direction check.
- I reproduced this bug on the version and environment reported below using the exact steps provided.
Current behavior
when a program in a pane sends a kitty graphics image with zlib-compressed pixels (o=z) and the compressed data is damaged, the herdr server sometimes dies with SIGBUS or SIGSEGV, and every pane and agent goes down with it.
it happened 4 times in a week here. the macOS crash reports all have the same faulting thread, a pane's pty reader:
compress.flate.Decompress.streamInner (in two of them: compress.flate.Decompress.takeBits)
compress.flate.Decompress.readVec
Io.Reader.appendRemaining
terminal.kitty.graphics_image.LoadingImage.complete
terminal.kitty.graphics_exec.transmit
terminal.stream_terminal.Handler.apcEnd
ghostty_terminal_vt_write
herdr::pane::terminal::PaneTerminal::process_pty_bytes
herdr::pty::actor::unix::PtyIoActorRunner::read_once
the fault is a byte read at the first byte after a Malloc Small region (KERN_PROTECTION_FAILURE, "bytes after start: 0"), so the decompressor reads past the end of a buffer. most of the time that read lands in other heap memory and nothing visible happens, which is why it only crashes now and then. under Guard Malloc it crashes every time.
damaged zlib data happens in practice here:
- two threads of one program wrote to the same terminal, so one thread's escape sequence could land inside an image chunk the other was writing. the rest of that chunk's base64 then shows up as text in the pane
- a program that stops between two chunks leaves an unfinished image, and herdr appends the next image's data to it
a damaged PNG (f=100) is refused with EINVAL: invalid data and nothing crashes, so it looks specific to the o=z path, which goes through Zig's std.compress.flate inside libghostty-vt.
Expected behavior
a damaged zlib image gets an error answer (EINVAL), the way a damaged PNG does, and the server keeps running.
Reproduction
save this as kitty-zlib-damaged.py (python 3, standard library only):
#!/usr/bin/env python3
# Sends kitty graphics images of zlib-compressed RGBA (o=z) whose compressed data is damaged, as
# happens when another escape sequence cuts a chunk, or when a program stops between two chunks
# and the next image is appended to the unfinished one.
import base64, os, random, zlib
random.seed(7)
def picture(width, height):
"""Rows of short light marks on a dark background, like text."""
pixels = bytearray(bytes((24, 26, 33, 255)) * (width * height))
for top in range(3, height - 7, 12):
left = random.randint(4, 24)
while left < width - 10:
wide, shade = random.randint(3, 8), random.randint(120, 240)
for y in range(top, top + 7):
for x in range(left, min(width, left + wide)):
if random.random() < 0.55:
pixels[(y * width + x) * 4:(y * width + x) * 4 + 3] = bytes((shade, shade, min(255, shade + 10)))
left += wide + 1 + (8 if random.random() < 0.15 else 0)
return bytes(pixels)
def chunks(keys, data):
payload = base64.b64encode(data).decode()
parts = [payload[at:at + 4096] for at in range(0, len(payload), 4096)]
return [f"\x1b_G{keys + ',' if index == 0 else ''}m={1 if index < len(parts) - 1 else 0};{part}\x1b\\" for index, part in enumerate(parts)]
def send(text):
os.write(1, text.encode())
# 1. an image with a hole in its compressed data, twenty times
for image in range(1, 21):
width, height = random.randint(60, 420), random.randint(30, 150)
data = zlib.compress(picture(width, height), 1)
cut = int(len(data) * (0.1 + random.random() * 0.85))
for chunk in chunks(f"a=t,f=32,o=z,s={width},v={height},i={image}", data[:cut] + data[cut + random.randint(1, 3000):]):
send(chunk)
# 2. an image left after two of its chunks, then a whole one: herdr appends the second to the first
first = chunks("a=t,f=32,o=z,s=400,v=150,i=21", zlib.compress(picture(400, 150), 1))
send(first[0] + first[1])
for chunk in chunks("a=t,f=32,o=z,s=300,v=120,i=22", zlib.compress(picture(300, 120), 1)):
send(chunk)
print("sent")
then run a throwaway server under Guard Malloc, so the over-read faults every time:
# terminal 1
DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib MALLOC_POINTER_SIZE=1 herdr --session repro server
# terminal 2
herdr --session repro workspace create --label repro
herdr --session repro pane run w1:p1 "python3 /path/to/kitty-zlib-damaged.py"
the server in terminal 1 exits with SIGSEGV within a few seconds, with the stack above. (if you run these from inside a herdr pane, unset HERDR_SOCKET_PATH and the other HERDR_* variables first, or the commands go to the outer session.)
without Guard Malloc the over-read usually goes unnoticed, and the crash only shows up when the buffer happens to end at the end of a malloc region. that was 4 times in a week of normal use here.
Impact
any program in any pane can take down the whole server, and every running agent with it. here it was two terminal dashboards of mine that draw with kitty images: each crash killed all agent sessions in all workspaces. i switched both to PNG as a workaround, but any other program that sends o=z can still do it.
Environment
- herdr 0.9.5, stable channel
- macOS 27.2.1 (26B131), Apple Silicon
- Ghostty 1.4.0 as the host terminal
- zsh
- Dominant language
- Rust
- Stars
- 40.8k
- Forks
- 3.1k
- Avg merge
- 4h 36m
- Merged PRs (30d)
- 4
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from herdrdev/herdr
-
machine rename accepts a label already used by another machine, making --machine <label> ambiguousOpenmaintainer-needed p2 remote triaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
p2 remote triaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
integrations p2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
herdrdev/herdr#6146 · 1 comment ·
Maintainers usually reply within 1 day
-
bug integrations p2 performance triaged
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
herdrdev/herdr#6145 · 1 comment ·
Maintainers usually reply within 1 day
-
agent-detection p2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
herdrdev/herdr#6104 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
area:ui effort:medium enhancement P7 source:agent
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
marketing
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
bug help wanted ui
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
userFRM/egui-charts#4605 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
UCS-Care-Group/mailcraft#2149 ·