diff mbox series

[v3,1/2] qemurunner.py: support setting slirp host IP address

Message ID 20221118160828.3939869-1-mikko.rapeli@linaro.org
State Accepted, archived
Commit bdbd52082eb26f418000eb4e424baae9babc272c
Headers show
Series [v3,1/2] qemurunner.py: support setting slirp host IP address | expand

Commit Message

Mikko Rapeli Nov. 18, 2022, 4:08 p.m. UTC
By default host side IP address is not set and qemu listens
on all IP addresses on the host machine which is not a good
idea when images have root login enabled without password.
It make sense to listen only on localhost IP address 127.0.0.1 using
config change like:

QB_SLIRP_OPT = "-netdev user,id=net0,hostfwd=tcp:127.0.0.1:2222-:22"

This config works for qemu itself, but breaks runqemu which tries to
parse the host side port number from qemu process command line arguments.
So change the runqemu side hostfwd parsing for port number to ignore
the host IP address field.

Reviewed-by: Quentin Schulz <foss+yocto@0leil.net>
Signed-off-by: Mikko Rapeli <mikko.rapeli@linaro.org>
---
 meta/lib/oeqa/utils/qemurunner.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

v3: use IP address from command line too

v2: improved commit message, https://lists.openembedded.org/g/openembedded-core/message/173250

v1: https://lists.openembedded.org/g/openembedded-core/topic/95016024#173234

Comments

Quentin Schulz Nov. 18, 2022, 4:23 p.m. UTC | #1
Hi Mikko,

On 11/18/22 17:08, Mikko Rapeli wrote:
> By default host side IP address is not set and qemu listens
> on all IP addresses on the host machine which is not a good
> idea when images have root login enabled without password.
> It make sense to listen only on localhost IP address 127.0.0.1 using
> config change like:
> 
> QB_SLIRP_OPT = "-netdev user,id=net0,hostfwd=tcp:127.0.0.1:2222-:22"
> 
> This config works for qemu itself, but breaks runqemu which tries to
> parse the host side port number from qemu process command line arguments.
> So change the runqemu side hostfwd parsing for port number to ignore
> the host IP address field.
> 
> Reviewed-by: Quentin Schulz <foss+yocto@0leil.net>
> Signed-off-by: Mikko Rapeli <mikko.rapeli@linaro.org>
> ---
>   meta/lib/oeqa/utils/qemurunner.py | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> v3: use IP address from command line too
> 
> v2: improved commit message, https://urldefense.com/v3/__https://lists.openembedded.org/g/openembedded-core/message/173250__;!!OOPJP91ZZw!nc5rsa_z35-LR56OMU2tL-KBM9kWHZcqgS1SrAHixpNlYyMcTmapd_Lpj3I943ZmEYiGYf7HvBu8YBNgFBnHoWtOuAEQkgIHwtaHDQ$
> 
> v1: https://urldefense.com/v3/__https://lists.openembedded.org/g/openembedded-core/topic/95016024*173234__;Iw!!OOPJP91ZZw!nc5rsa_z35-LR56OMU2tL-KBM9kWHZcqgS1SrAHixpNlYyMcTmapd_Lpj3I943ZmEYiGYf7HvBu8YBNgFBnHoWtOuAEQkgKJOx__4Q$
> 
> diff --git a/meta/lib/oeqa/utils/qemurunner.py b/meta/lib/oeqa/utils/qemurunner.py
> index e602399232..c285f30c0a 100644
> --- a/meta/lib/oeqa/utils/qemurunner.py
> +++ b/meta/lib/oeqa/utils/qemurunner.py
> @@ -401,9 +401,10 @@ class QemuRunner:
>                   cmdline = re_control_char.sub(' ', cmdline)
>               try:
>                   if self.use_slirp:
> -                    tcp_ports = cmdline.split("hostfwd=tcp::")[1]
> +                    tcp_ports = cmdline.split("hostfwd=tcp:")[1]
> +                    ip, tcp_ports = tcp_ports.split(":")[:2]
>                       host_port = tcp_ports[:tcp_ports.find('-')]
> -                    self.ip = "localhost:%s" % host_port
> +                    self.ip = "%s:%s" % (ip, host_port)

Now there is the question of what we should print in the event hostfwd 
is set to tcp::hostport-:port ?

Should we have it return localhost:hostport even though it is not 
entirely true since any interface of the host can be used (provided the 
firewall is properly set up) but at least it shows one way to reach the 
QEMU target and it also keeps the current behavior? or should we keep it 
like this in which case it prints :hostport only?

A ternary ip if ip else "localhost" would be enough.

I'm picking nits now, so up to you/the maintainer on this :)

Cheers,
Quentin
diff mbox series

Patch

diff --git a/meta/lib/oeqa/utils/qemurunner.py b/meta/lib/oeqa/utils/qemurunner.py
index e602399232..c285f30c0a 100644
--- a/meta/lib/oeqa/utils/qemurunner.py
+++ b/meta/lib/oeqa/utils/qemurunner.py
@@ -401,9 +401,10 @@  class QemuRunner:
                 cmdline = re_control_char.sub(' ', cmdline)
             try:
                 if self.use_slirp:
-                    tcp_ports = cmdline.split("hostfwd=tcp::")[1]
+                    tcp_ports = cmdline.split("hostfwd=tcp:")[1]
+                    ip, tcp_ports = tcp_ports.split(":")[:2]
                     host_port = tcp_ports[:tcp_ports.find('-')]
-                    self.ip = "localhost:%s" % host_port
+                    self.ip = "%s:%s" % (ip, host_port)
                 else:
                     ips = re.findall(r"((?:[0-9]{1,3}\.){3}[0-9]{1,3})", cmdline.split("ip=")[1])
                     self.ip = ips[0]