1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Forces every QEMU hostfwd forwarded port onto loopback, including SSH.
#
# vagrant-qemu 0.6.3's Driver#start hardcodes the SSH forward as
# "hostfwd=tcp::#{ssh_port}-:22" with no host_ip seam at all (driver.rb:129,153), and
# action/start_instance.rb's forwarded_ports() explicitly skips id == "ssh" ("SSH port is
# handled by PrepareForwardedPortCollisionParams") before building the :ports list that
# *does* respect a Vagrantfile's host_ip — so no Vagrantfile-level config can loopback-bind
# the SSH forward. Confirmed directly against the installed gem source, 2026-09-23.
#
# Other forwarded ports (winrm, rdp, ...) DO respect an explicit host_ip in the Vagrantfile,
# via that same :ports list — see the host_ip: "127.0.0.1" redeclarations in
# windows-x64/Vagrantfile. This patch is defense-in-depth for those (belt-and-suspenders
# against a future misconfigured port lacking host_ip) and the *only* fix for SSH.
#
# QEMU's hostfwd syntax is [tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport; the protocol
# segment is itself optional (defaulting to tcp), so an unqualified forward can read
# "hostfwd=::2222-:22" with no "tcp"/"udp" at all — this patch handles that form the same as
# an explicit one. An empty hostaddr segment binds every interface. This patch rewrites any
# empty-hostaddr hostfwd clause in the constructed QEMU argv to loopback-only, at the point
# Driver#execute actually shells out to qemu-system-*, so it applies regardless of which code
# path built the string.
#
# Loaded unconditionally from every guest Vagrantfile via require_relative, before `vagrant
# up` starts QEMU, so no guest can accidentally expose SSH/WinRM/RDP to the LAN.
#
# This patch reaches into vagrant-qemu's Driver#execute by prepending a module — there is no
# public extension point for this. That makes it silently stop working if a future
# vagrant-qemu release changes Driver#execute's argv shape (e.g. no longer building the
# hostfwd string this way at all) or the hardcoded SSH-forward construction described above.
# Two independent guards against that:
# 1. A version pin checked inside execute() itself, below — refuses to run QEMU at all
# against any vagrant-qemu other than the one this patch was verified against.
# Deliberately NOT checked at load time (this file is require_relative'd unconditionally
# from every guest Vagrantfile, so a load-time check runs on EVERY `vagrant` invocation
# that touches this Vagrantfile, including `destroy`/`halt` against an already-running
# QEMU process): reading driver.rb end to end - `stop`
# (halt) sends QMP commands over the control port or SIGKILLs the process directly, and
# `delete` (destroy) only removes files - neither ever calls `execute`. A load-time
# check would mean that after a plugin upgrade to an unpinned vagrant-qemu version, THIS
# Ruby raise, not just a failed `vagrant up`, is what a developer hits when trying to
# `destroy`/`halt` a guest QEMU is still running - permanently orphaning it, with no
# `vagrant` command left able to stop it. Checking inside execute() instead means the
# pin only ever blocks *starting* a new QEMU process (or an `export`/`package`
# `qemu-img` call), never stopping/removing an existing one.
# 2. A fail-closed post-rewrite assertion inside execute() itself, so even an in-range
# version whose behavior somehow doesn't match what's documented above turns into a hard
# `vagrant up` failure instead of a silent loopback-only guarantee that no longer holds.
=
=
# Matches a hostfwd clause's host-address segment as actually rewritten above:
# hostfwd=tcp:127.0.0.1:2222-:22 → captures "127.0.0.1". The protocol segment is
# optional (see the file-level comment above), so this also matches the protocol-less
# hostfwd=:127.0.0.1:2222-:22 form. Anything the gsub above didn't touch, or touched
# incorrectly, shows up here as an empty or "0.0.0.0" capture.
=
installed_version = Vagrant::Plugin::Manager.instance.installed_plugins.dig(, )
if installed_version != PINNED_VAGRANT_QEMU_VERSION
raise \
\
\
end
cmd = cmd.map do
if arg.is_a?(String)
# (tcp|udp)? is optional: also rewrites the protocol-less "hostfwd=::2222-:22"
# form (empty capture) to "hostfwd=:127.0.0.1:2222-:22", not just the
# "hostfwd=tcp::"/"hostfwd=udp::" forms.
arg.gsub(, )
else
arg
end
end
cmd.each do
next unless arg.is_a?(String)
arg.scan(HOSTFWD_HOSTADDR).each do
# Require exactly LOOPBACK, not just "not obviously wrong" (blank/0.0.0.0):
# any other concrete address (a LAN IP, "::1", a hostname, ...) is just as
# much a loopback-only violation and just as silent if let through.
if hostaddr != LOOPBACK
raise \
\
\
\
end
end
end
super(*cmd, **opts, &block)
end
end
prepend ForceLoopbackHostfwd
end
end
end