2020-02-27 09:58:37 -05:00
---
2021-03-13 13:06:18 -05:00
lang: en
2020-02-27 09:58:37 -05:00
layout: doc
2021-06-16 22:56:25 -04:00
permalink: /doc/qrexec-socket-services/
2021-03-13 13:06:18 -05:00
ref: 42
2021-07-09 08:10:44 -04:00
title: 'Qrexec: socket-based services'
2020-02-27 09:58:37 -05:00
---
*This page describes how to implement and use new socket-backed services for qrexec. See [qrexec ](/doc/qrexec/ ) for general overview of the qrexec framework.*
2020-03-05 21:03:10 -05:00
As of Qubes 4.1, qrexec allows implementing services not only as executable files, but also as Unix sockets.
This allows Qubes RPC requests to be handled by a server running in a VM and listening for connections.
2020-02-27 09:58:37 -05:00
## How it works
2020-03-05 21:12:24 -05:00
When a Qubes RPC service is invoked,
qrexec searches for a file that handles it in the qubes-rpc directories (`/etc/qubes-rpc` or `/usr/local/etc/qubes-rpc` ).
2020-03-05 21:03:10 -05:00
If the file is a Unix socket, qrexec will try to connect to it.
2020-02-27 09:58:37 -05:00
2020-03-05 21:03:10 -05:00
Before passing user input, the socket service will receive a null-terminated service descriptor, i.e. the part after `QUBESRPC` .
When running in a VM, this is:
2020-02-27 09:58:37 -05:00
2021-03-13 12:03:23 -05:00
```
2021-03-28 14:58:39 -04:00
< service_name > < source > \0
2021-03-13 12:03:23 -05:00
```
2020-02-27 09:58:37 -05:00
When running in dom0, it is:
2021-03-13 12:03:23 -05:00
```
2021-03-28 14:58:39 -04:00
< service_name > < source > < target_type > < target > \0
2021-03-13 12:03:23 -05:00
```
2020-02-27 09:58:37 -05:00
(The target type can be `name` , in which case target is a domain name, or `keyword` , in which the target is a keyword like `@dispvm` ).
2020-03-05 21:03:10 -05:00
Afterwards, data provided by the service's user (as stdin) is sent into the socket, and data received from the socket is sent back to the user (as stdout).
When the service closes the socket, an exit code of 0 is sent back to the user.
2020-02-27 09:58:37 -05:00
### Differences from executable-based services
2020-03-05 21:03:10 -05:00
From the user point of view, the socket-based service behaves almost like an executable-based one.
Here are the differences:
2020-02-27 09:58:37 -05:00
2020-03-05 21:03:10 -05:00
* There is no stderr (the socket provides only one output stream).
Currently, that means stderr will also never be closed on user's end.
* There is no exit code.
When the socket connection is closed, exit code 0 is sent to the user.
2020-02-27 09:58:37 -05:00
## Recommended use
2020-03-05 21:03:10 -05:00
Create a program that binds to path *outside* `/etc/qubes-rpc` , such as `/var/run/my-daemon.sock` .
Put a symlink in `/etc/qubes-rpc` , e.g. `ln -s /var/run/my-daemon.sock /etc/qubes-rpc/qubes.Service` .
2020-02-27 09:58:37 -05:00
2020-03-05 21:03:10 -05:00
If your program handles multiple services, create multiple symlinks.
You can dispatch based on the service descriptor.
2020-02-27 09:58:37 -05:00
Do not run the program as root.
2020-03-05 21:03:10 -05:00
You can use systemd and socket activation so that the program is started only when the service is invoked.
See the below example.
2020-02-27 09:58:37 -05:00
## Example: `qrexec-policy-agent`
2020-03-05 21:03:10 -05:00
`qrexec-policy-agent` is the program that handles "ask" prompts for Qubes RPC calls.
It is a good example of an application that:
2020-02-27 09:58:37 -05:00
* Uses Python and asyncio.
* Runs as a daemon, to save some overhead on starting process.
2020-03-05 21:03:10 -05:00
* Runs as a normal user.
This is achieved using user's instance of systemd.
* Uses systemd socket activation.
This way it can be installed in all VMs, but started only if it's ever needed.
2020-02-27 09:58:37 -05:00
See the [qubes-core-qrexec ](https://github.com/QubesOS/qubes-core-qrexec/ ) repository for details.
### Systemd unit files
**/lib/systemd/user/qubes-qrexec-policy-agent.service**: This is the service configuration.
```
[Unit]
Description=Qubes remote exec policy agent
ConditionUser=!root
ConditionGroup=qubes
Requires=qubes-qrexec-policy-agent.socket
[Service]
Type=simple
ExecStart=/usr/bin/qrexec-policy-agent
[Install]
WantedBy=default.target
```
**/lib/systemd/user/qubes-qrexec-policy-agent.socket**: This is the socket file that will activate the service.
```
[Unit]
Description=Qubes remote exec policy agent socket
ConditionUser=!root
ConditionGroup=qubes
PartOf=qubes-qrexec-policy-agent.service
[Socket]
ListenStream=/var/run/qubes/policy-agent.sock
[Install]
WantedBy=sockets.target
```
Note the `ConditionUser` and `ConditionGroup` that ensure that the socket and service is started only as the right user
2020-03-05 21:03:10 -05:00
Start the socket using `systemctl --user start` .
Enable it using `systemctl --user enable` , so that it starts automatically.
2020-02-27 09:58:37 -05:00
```
systemctl --user start qubes-qrexec-policy-agent.socket
systemctl --user enable qubes-qrexec-policy-agent.socket
```
Alternatively, you can enable the service by creating a symlink:
```
sudo ln -s /lib/systemd/user/qubes-qrexec-policy-agent.socket /lib/systemd/user/sockets.target.wants/
```
### Link in qubes-rpc
`qrexec-policy-agent` will handle a Qubes RPC service called `policy.Ask` , so we add a link:
```
sudo ln -s /var/run/qubes/policy-agent.sock /etc/qubes-rpc/policy.Ask
```
### Python server with socket activation
2020-03-05 21:12:24 -05:00
Socket activation in systemd works by starting our program with the socket file already bound at a specific file descriptor.
2020-03-05 21:03:10 -05:00
It's a simple mechanism based on a few environment variables, but the canonical way is to use the `sd_listen_fds()` function from systemd library (or, in our case, its Python version).
2020-02-27 09:58:37 -05:00
Install the Python systemd library:
```
sudo dnf install python3-systemd
```
Here is the server code:
```python
import os
import asyncio
import socket
from systemd.daemon import listen_fds
class SocketService:
def __init__ (self, socket_path, socket_activated=False):
self._socket_path = socket_path
self._socket_activated = socket_activated
async def run(self):
server = await self.start()
async with server:
await server.serve_forever()
async def start(self):
if self._socket_activated:
fds = listen_fds()
if fds:
assert len(fds) == 1, 'too many listen_fds: {}'.format(
listen_fds)
sock = socket.socket(fileno=fds[0])
return await asyncio.start_unix_server(self._client_connected,
sock=sock)
if os.path.exists(self._socket_path):
os.unlink(self._socket_path)
return await asyncio.start_unix_server(self._client_connected,
path=self._socket_path)
async def _client_connected(self, reader, writer):
try:
data = await reader.read()
assert b'\0' in data, data
service_descriptor, data = data.split(b'\0', 1)
response = await self.handle_request(service_descriptor, data)
writer.write(response)
await writer.drain()
finally:
writer.close()
await writer.wait_closed()
async def handle_request(self, service_descriptor, data):
# process params, return response
return response
def main():
socket_path = '/var/run/qubes/policy-agent.sock'
service = SocketService(socket_path)
loop = asyncio.get_event_loop()
loop.run_until_complete(service.run())
if __name__ == '__main__':
main()
```
You can also use `qrexec/server.py` from [qubes-core-qrexec ](https://github.com/QubesOS/qubes-core-qrexec/ ) repository, which is a variant of the above code - but note that currently it's somewhat more specific (JSON requests and ASCII responses; no target handling in service descriptors).
### Using the service
The service is invoked in the same way as a standard Qubes RPC service:
```
echo < input_data > | qrexec-client -d domX 'DEFAULT:QUBESRPC policy.Ask'
```
You can also connect to it locally, but remember to include the service descriptor:
```
echo -e 'policy.Ask dom0\0< input data > ' | nc -U /etc/qubes-rpc/policy.Ask
```
## Further reading
2020-11-13 00:19:11 -05:00
* [Qrexec overview ](/doc/qrexec/ )
* [Qrexec internals ](/doc/qrexec-internals/ )
2020-02-27 09:58:37 -05:00
* [qubes-core-qrexec ](https://github.com/QubesOS/qubes-core-qrexec/ ) repository - contains the above example
* [systemd.socket ](https://www.freedesktop.org/software/systemd/man/systemd.socket.html ) - socket unit configuration
* [Streams in Python asyncio ](https://docs.python.org/3/library/asyncio-stream.html )