[bug#78179,0/4] Add wireshark-service-type with privileged wrapper

Message ID cover.1746086472.git.rutherther@ditigal.xyz
Headers
Series Add wireshark-service-type with privileged wrapper |

Message

Rutherther May 1, 2025, 8:26 a.m. UTC
  Hi,

recently I discucced on devel mailing list on the topic of a wireshark service type.
I would like to thank Denis 'GNUtoo' Carikli who helped me a lot in coming to this idea.

# Motivation

The issue with wireshark is that it refers to dumpcap from the bin folder of the output.
That is good usually, but not so with Wireshark as dumpcap needs to run with capabilities,
and the store cannot have binaries with capabilities.
In addition to that, dumpcap was wrapped with gtk wrapping phase, unnecessarily,
this complicates the matter a bit, since interpreted executables cannot get setuid or capabilities.
I think that is still something to look at in the future - wrap-program doesn't work for setuid/capabilities,
maybe it would be good to introduce wrap-program-binary that would make a binary instead for the wrapping,
but it's not topic of this patch series.

# Solution

The solution works like this:
1. #$output/bin/dumpcap is unwrapped (mv #$output/bin/.dumpcap-real #$output/bin/dumpcap)
2. #$output/bin/dumpcap is replaced with a shell script that looks if /run/privileged/bindumpcap exists,
   if it does, it is executed. If it doesn't, the original dumpcap binary is executed. Additionally GUIX_SKIP_PRIVILEGED=1
   will skip the check and start the original binary
3. The original binary is put to #$output/privileged/dumpcap (we can change the folder, but name of the binary is important here for privileged-program - it cannot change name)
4. The service will make privileged program referring to #$output/privileged/dumpcap

# Implementation

I've decided to introduce a new module, (guix build privileged), this module exposes just one function: wrap-privileged.
This function accepts:
- output - output folder of the package (/gnu/store/...-dumpcap-ver)
- original - path to the original binary under the output (bin/dumpcap)
- target-name - name that will end up in #$output/privileged
- #:unwrap - whether to try unwrapping the binary. This has to be #t currently to work properly (#t)
  only binary wrappers would allow for it to be #f.
- #:target-folder - what folder under output to put the target to (privileged)
- #:privileged-directory - where are privileged programs. I've exposed %privileged-program-directory
  from (gnu build activation) (/run/privileged/bin)

This function is then used in a new phase of wireshark wrap-privileged, that is happening
after qt-wrap (so that the binary can be unwrapped).
Additionally I added bash to inputs of wireshark, so that the shebang is patched
(I've decided to let this be handled by the patch-shebang phase rather than passing path to
bash to the wrap-privileged function which would add complexity, unnecessarily imho)

```
(add-after 'qt-wrap 'wrap-dumpcap
  (lambda _
    (wrap-privileged
      #$output
      "bin/dumpcap"
      "dumpcap")))
```

Then I added the service, referring to the wireshark/privileged/dumpcap.

# Future

After this feature is introduced into the Guix code, other packages could be changed to it.
I've checked the code and there seem to be a few packages that already patch the source to refer
to /run/privileged.
- singularity, spice-gtk: refer to their own binary.
- spacefm, udevil, zabbix-agentd, xsecurelock: refer to a binary of different package.

The second category is going to have to be thought through further, I am not sure
what the best approach is going to be. If to make shell scripts in the packages
or consider adding new packages that would have such shell scripts in their bin folder.

# Considerations

- Maybe the wrapped script should be a guile script instead of a shell one?
- Wrapped executables cannot work with this as was discussed in intro.
- I really had trouble coming up with the wrap-privileged function interface, maybe the parameters could be made more intuitive.
- Should this be added to the manual
- During testing I found out that wireshark binary doesn't pass GUIX_SKIP_PRIVILEGED env var through
  to dumpcap wrapper :(

Feedback welcome,
Cheers!
Rutherther

Rutherther (4):
  gnu: %privileged-program-directory: Export variable.
  guix: Add (guix build privileged) module.
  gnu: wireshark: Wrap dumpcap with wrap-privileged.
  services: Add wireshark-service-type.

 gnu/build/activation.scm    |  4 +++-
 gnu/packages/networking.scm | 17 +++++++++++--
 gnu/services/networking.scm | 35 ++++++++++++++++++++++++++-
 guix/build/privileged.scm   | 48 +++++++++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+), 4 deletions(-)
 create mode 100644 guix/build/privileged.scm


base-commit: d505cb960fd1e670be9a66d9fdbad94bc49e891d
--
2.49.0