dockervfio

Cannot open vfio device in docker container as non-root user


I have enabled virtualization in the BIOS and enabled the IOMMU on kernel command line (intel_iommu=on).

I bound a solarflare NIC to the vfio-pci device and added a udev rule to ensure the vfio device is accessible by my non-root user (e.g., /etc/udev/rules.d/10-vfio-docker-users.rules):

SUBSYSTEM=="vfio", OWNER="myuser", GROUP=="myuser"

I've launched my container with -u 1000 and mapped /dev (-v /dev:/dev). Running in an interactive shell in the container, I am able to verify that the device is there with the permissions set by my udev rule:

bash-4.2$ whoami
whoami: unknown uid 1000
bash-4.2$ ls -al /dev/vfio/35
crw-rw----    1 1000     1000      236,   0 Jan 25 00:23 /dev/vfio/35

However, if I try and open it (e.g., python -c "open('/dev/vfio/35', 'rb')" I get IOError: [Errno 1] Operation not permitted: '/dev/vfio/35'. However, the same command works outside the container as the normal non-root user with user-id 1000!

It seems that there are additional security measures that are not allowing me to access the vfio device within the container. What am I missing?


Solution

  • Docker drops a number of privileges by default, including the ability to access most devices. You can explicitly grant access to a device using the --device flag, which would look something like:

    docker run --device /dev/vfio/35 ...
    

    Alternately, you can ask Docker not to drop any privileges:

    docker run --privileged ...
    

    You'll note that in both of the above examples it was not necessary to explicitly bind-mount /dev; in the first case, the device(s) you have exposed with --device will show up, and in the second case you see the host's /dev by default.