mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
The desktop feature (assets/features/desktop.sh, not chosen until you choose it, about 370 MB) installs Xfce and TigerVNC's Xvnc from base's package snapshot, and devvm-desktop, which starts Xvnc on display :1 on a socket of the VM user's own (/run/devvm-desktop/vnc.sock, 0600 in a 0700 folder), with no TCP port (-rfbport -1, -nolisten tcp) and no password of its own, the clipboard on or off as asked (vncconfig on a running one), then an Xfce session in a D-Bus session of its own. Nothing starts it at boot: devvm desktop does, on every backend. devvm desktop gets the VM ready as devvm ssh does, refuses a VM without the feature (desktop_no_feature), starts its desktop (desktop_start_failed), and serves it on 127.0.0.1 alone until Ctrl+C or the VM stops (desktop_vm_stopped), on the VM's own port (desktop_port, among netpolicy.UsedPorts, so no forwarder or new VM takes it), showing the address and password (desktop_ready) and opening vnc://127.0.0.1:PORT in Screen Sharing on macOS (--no-open; desktop_open_failed). --clipboard shares the clipboard both ways for that run. internal/desktop's Relay is the guarantee, not the VM's server: it speaks RFB 3.3/3.7/3.8 as a server, offers VNC authentication alone, checks the answer against the VM's password (8 characters of 31, about 40 bits, crypto/rand, kept in the VM's state as desktop_password, never sent to the VM) one check at a time with a second's wait after a wrong one, and only then opens an ssh -W channel to the VM's socket through devvm's ssh config (pinned host key, no session, agent or forwarding), closed by its input first so the VM's sshd ends its side. Without --clipboard it drops every ClientCutText (extended ones too) and ends a connection on a message type it can't size. At most 8 connections. Also: vm.Desktop (Open, serve, listener, dial), vm.ready (devvm ssh's launch without the Include prompt), Env.OpenURL; build's chosen features leave desktop out until it's chosen; messages desktop_failed, desktop_no_feature, desktop_start_failed, desktop_ready, desktop_open_failed, desktop_vm_stopped, with doc links. Tests: internal/desktop (passwords, Response, wrong passwords refused with no dial, 3.3/3.7/3.8, not a viewer, the clipboard dropped or carried, an unknown message ending the connection, a VM server asking for a password), internal/vm (the password kept and remade, the listener's port kept and replaced, ssh's arguments), internal/build (desktop off until chosen), testdata/script/desktop.txtar (no VM, no feature, a stopped VM started, the forward asked for with the clipboard off and on, the same port and password, Screen Sharing opened or failing, the VM stopping, the start failing), setup.txtar, docgen; tests/e2e.sh's desktop section (Lima, with network policy) and checks in the smolvm and Docker sections; TestE2EDesktop. Docs: using/desktop.md (new), customizing.md's features, commands.md, internals/desktop.md (new), design.md's note on the desktop, contributing/testing.md's manual Screen Sharing check, the CLI reference. Review (security-audit skill, guidance mode, local IPC and loopback listener classes), against the review's list: - another local user or process reaching the port: 127.0.0.1 alone, without address reuse; VNC authentication checked on the host before any ssh, constant-time, about 1 guess a second globally (thousands of years); the password in the VM's state (0600 file, 0700 folder), shown only on stdout with the address, never in open's arguments or the run log (e2e: no run log, no host process's arguments); - never in an image or the VM: the VM's server has no password; e2e greps the VM's files for it (found the first time only in the VM's journal, from the check's own sudo command line, now fed on its input); - the server on no address but loopback on each backend, even if root changes it: Xvnc has no TCP listener (e2e on each backend), the relay is the only way in and lives on the host; root's own server without a password (e2e, Lima) still gets no viewer without devvm's password; - clipboard off really off: the relay drops the viewer's clipboard whatever the server accepts (e2e: root's server records a key event but never the clipboard); the VM's clipboard to the viewer can't be filtered without decoding screen updates, documented, Screen Sharing's setting; - nothing from the VM toward the host: a local ssh -W channel, which ssh's client asks for; the VM reaches neither the relay's port nor anything new (e2e: with network policy, no direct connection out, nor to the port); a VM that reaches host loopback (no policy, Docker Desktop) meets the password like anyone; - network policy unchanged: no policy file or allowlist changes; the self-check passes with the desktop running (e2e). Left as documented residuals: the VM's clipboard sent by a changed server; a local process or VM keeping the relay's 8 slots busy or failing checks (availability only). Co-Authored-By: Claude Opus 5.5 <[email protected]>
175 lines
4.7 KiB
Go
175 lines
4.7 KiB
Go
package desktop
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
// The viewer's messages (RFC 6143 7.5, and the extensions TigerVNC's
|
|
// server offers).
|
|
const (
|
|
msgSetPixelFormat = 0
|
|
msgSetEncodings = 2
|
|
msgUpdateRequest = 3
|
|
msgKeyEvent = 4
|
|
msgPointerEvent = 5
|
|
msgClientCutText = 6
|
|
msgContinuousUpdates = 150
|
|
msgFence = 248
|
|
msgXvp = 250
|
|
msgSetDesktopSize = 251
|
|
msgGII = 253
|
|
msgQEMU = 255
|
|
qemuExtendedKeyEvent = 0
|
|
qemuAudio = 1
|
|
qemuAudioSetFormat = 2
|
|
giiBigEndian = 0x80
|
|
fenceMaxPayload = 64
|
|
)
|
|
|
|
// filter copies the viewer's messages (after its security result: its
|
|
// ClientInit, then messages) from viewer to vm, leaving out its clipboard
|
|
// (ClientCutText, extended clipboard ones included). A message it can't
|
|
// read the length of ends the connection: what it doesn't know never
|
|
// reaches the VM.
|
|
func filter(vm io.Writer, viewer io.Reader, logf func(string, ...any)) error {
|
|
in := bufio.NewReader(viewer)
|
|
// ClientInit: the shared flag.
|
|
if _, err := io.CopyN(vm, in, 1); err != nil {
|
|
return err
|
|
}
|
|
logged := false
|
|
for {
|
|
t, err := in.ReadByte()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if t == msgClientCutText {
|
|
if err := skipCutText(in); err != nil {
|
|
return err
|
|
}
|
|
if !logged {
|
|
logf("the viewer's clipboard kept from the VM")
|
|
logged = true
|
|
}
|
|
continue
|
|
}
|
|
msg, err := message(in, t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := vm.Write(msg); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
// skipCutText reads past a ClientCutText message after its type: 3
|
|
// padding bytes, a signed length (negative for the extended clipboard's),
|
|
// then that many bytes.
|
|
func skipCutText(in *bufio.Reader) error {
|
|
h, err := readN(in, 7)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n := int64(int32(binary.BigEndian.Uint32(h[3:]))) //nolint:gosec // the length is signed, as the protocol has it
|
|
if n < 0 {
|
|
n = -n
|
|
}
|
|
_, err = io.CopyN(io.Discard, in, n)
|
|
return err
|
|
}
|
|
|
|
// message reads the rest of a message of type t and returns it whole, its
|
|
// type first.
|
|
func message(in *bufio.Reader, t byte) ([]byte, error) {
|
|
switch t {
|
|
case msgSetPixelFormat:
|
|
return fixed(in, t, 19)
|
|
case msgUpdateRequest, msgContinuousUpdates:
|
|
return fixed(in, t, 9)
|
|
case msgKeyEvent:
|
|
return fixed(in, t, 7)
|
|
case msgPointerEvent:
|
|
return fixed(in, t, 5)
|
|
case msgXvp:
|
|
return fixed(in, t, 3)
|
|
case msgSetEncodings:
|
|
// A padding byte, then a count of 4-byte encodings.
|
|
return counted(in, t, 3, func(h []byte) int { return 4 * int(binary.BigEndian.Uint16(h[1:])) })
|
|
case msgFence:
|
|
// 3 padding bytes, 4 of flags, then a length of at most 64.
|
|
m, err := counted(in, t, 8, func(h []byte) int { return int(h[7]) })
|
|
if err == nil && int(m[8]) > fenceMaxPayload {
|
|
return nil, fmt.Errorf("a fence of %d bytes", m[8])
|
|
}
|
|
return m, err
|
|
case msgSetDesktopSize:
|
|
// A padding byte, width and height, a count of 16-byte screens and
|
|
// a padding byte.
|
|
return counted(in, t, 7, func(h []byte) int { return 16 * int(h[5]) })
|
|
case msgGII:
|
|
// Its byte order and subtype, then a length in that order.
|
|
return counted(in, t, 3, func(h []byte) int {
|
|
if h[0]&giiBigEndian != 0 {
|
|
return int(binary.BigEndian.Uint16(h[1:]))
|
|
}
|
|
return int(binary.LittleEndian.Uint16(h[1:]))
|
|
})
|
|
case msgQEMU:
|
|
return qemu(in, t)
|
|
}
|
|
return nil, fmt.Errorf("a message of type %d, which the relay doesn't know", t)
|
|
}
|
|
|
|
// qemu reads the rest of a QEMU message: an extended key event, or an
|
|
// audio one.
|
|
func qemu(in *bufio.Reader, t byte) ([]byte, error) {
|
|
sub, err := in.ReadByte()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch sub {
|
|
case qemuExtendedKeyEvent:
|
|
rest, err := readN(in, 10)
|
|
return append([]byte{t, sub}, rest...), err
|
|
case qemuAudio:
|
|
op, err := readN(in, 2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m := append([]byte{t, sub}, op...)
|
|
if binary.BigEndian.Uint16(op) != qemuAudioSetFormat {
|
|
return m, nil
|
|
}
|
|
rest, err := readN(in, 6)
|
|
return append(m, rest...), err
|
|
}
|
|
return nil, fmt.Errorf("a QEMU message of subtype %d, which the relay doesn't know", sub)
|
|
}
|
|
|
|
// fixed reads a message of n bytes after its type.
|
|
func fixed(in *bufio.Reader, t byte, n int) ([]byte, error) {
|
|
rest, err := readN(in, n)
|
|
return append([]byte{t}, rest...), err
|
|
}
|
|
|
|
// counted reads a message whose header (n bytes after its type) says how
|
|
// many bytes follow (more).
|
|
func counted(in *bufio.Reader, t byte, n int, more func(h []byte) int) ([]byte, error) {
|
|
h, err := readN(in, n)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
body, err := readN(in, more(h))
|
|
return append(append([]byte{t}, h...), body...), err
|
|
}
|
|
|
|
func readN(in io.Reader, n int) ([]byte, error) {
|
|
b := make([]byte, n)
|
|
_, err := io.ReadFull(in, b)
|
|
return b, err
|
|
}
|