Skip to content

nixos/qemu-vm: add option "restrictNetwork"#200225

Merged
roberth merged 2 commits intoNixOS:masterfrom
pacien:nixos-qemu-vm-restrictnetwork
Jan 13, 2023
Merged

nixos/qemu-vm: add option "restrictNetwork"#200225
roberth merged 2 commits intoNixOS:masterfrom
pacien:nixos-qemu-vm-restrictnetwork

Conversation

@pacien
Copy link
Contributor

@pacien pacien commented Nov 8, 2022

This adds an option to the qemu virtualisation module to isolate the
guest's from the host's and outside networks.

This is particularly useful for development sandboxes for example.

The option is disabled by default to preserve the current behaviour.

This adds an option to the qemu virtualisation module to isolate the
guest's from the host's and outside networks.

This is particularly useful for development sandboxes for example.

The option is disabled by default to preserve the current behaviour.
@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Nov 8, 2022
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Nov 8, 2022
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/1669

Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a vm test with three nodes, for example

  • client
  • server
  • restricted

client and restricted should execute the same code, but on client it succeeds, whereas on restricted it does not.

@pacien
Copy link
Contributor Author

pacien commented Jan 8, 2023 via email

@minijackson
Copy link
Member

I'm not sure how we would test that, as the fact that today the QEMU VM can't access the global network is a consequence of the sandboxing, not the QEMU configuration. Maybe be trying to access the VM from outside the VM, but I think it would mean either not using the test infrastructure, or doing some changes to it.

@roosemberth
Copy link
Contributor

Idea: Create a SLiRP network that subsequent VMs can join:

Node creating the network:

qemu-system
 ...
 -nic hubport,hubid=1,id=n1,mac=DE:AD:BE:EF:00:00
 -netdev socket,id=netsock,listen=:57300
 -netdev hubport,id=h0,hubid=1,netdev=netsock
 -netdev user,id=m1
 -netdev hubport,id=h1,hubid=1,netdev=m1

This command creates a virtual hub other VMs can connect to.
The option user, enables slirp on one of the ports.
The other port is exported to the guest.

Nodes attaching to the network

qemu-system
 ...
 -nic hubport,hubid=1,id=n1,mac=DE:AD:BE:EF:00:01
 -netdev socket,id=netsock,connect=localhost:57300
 -netdev hubport,id=h0,hubid=1,netdev=netsock

This command creates a virtual hub that connects to that of the first guest.
Notice the lack of the user. No effort is made by QEMU to provide a usable network other than linking the port to the first guest.

@pacien
Copy link
Contributor Author

pacien commented Jan 10, 2023 via email

@roberth
Copy link
Member

roberth commented Jan 11, 2023

Perhaps you could run a web server on the host at some non-privileged port? Perhaps python's http.server?
Indeed the server vm I suggested doesn't seem very useful, except perhaps to confirm that it is unaffected.

@pacien
Copy link
Contributor Author

pacien commented Jan 11, 2023 via email

@roberth
Copy link
Member

roberth commented Jan 12, 2023

What would be the correct way to run this on the host?

Anything that works I'd say. os.system() or spawning the thing in a separate thread.
I don't think we need a special method for this. cc @tfc

How would one also get an internally routable IP address within the sandbox,

I suppose only the loopback interface may be suitable for this. Is it possible for qemu's internal networking to perform NAT to expose the host loopback interface as a different address to the VMs?

The wiki seems to suggest that the loopback interface is treated no differently by the option:

You can isolate the guest from the host (and broader network) using the restrict option.

@tfc
Copy link
Contributor

tfc commented Jan 12, 2023

What would be the correct way to run this on the host?

Anything that works I'd say. os.system() or spawning the thing in a separate thread.
I don't think we need a special method for this. cc @tfc

I second that. We don't need extra code for that.

@pacien
Copy link
Contributor Author

pacien commented Jan 12, 2023 via email

@roberth roberth merged commit e973da6 into NixOS:master Jan 13, 2023
@roberth
Copy link
Member

roberth commented Jan 13, 2023

This is fantastic. Thank you @pacien!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants