Background
For a NFSv4 export/share the by default enabled root_squash option will force NFS to change the client’s root to an anonymous ID. This will, in effect, increase security by preventing ownership of the root account on one system migrating to the other system.
Overlayfs allows for a local, transparent, filesystem to be mounted on top of another filesystem. Unfortunately it seems like it accesses the underlying filesystem using the root user, and not the actual user. At least that is the conclusion I draw from the following experiment.
Why mount an Overlayfs over a NFS share? To allow a less trusted machine the ability to pretend it can write to the shared filesystem.
Test setup
First install the NFS kernel server as appropriate for your distro. Then make sure you export only NFSv4. (Though probably not important for this issue, a good security precausion.)
$ sudo cat /proc/fs/nfsd/versions
-2 -3 +4 +4.1 +4.2
If not, have a look at /etc/nfs.conf and set vers3=n.
Then create en Ext4 filesystem in a sparse file and mount it on the local filesystem. This will be the filesystem underpinning our NFS share.
$ truncate -s 512M 512BM-ext4.img
$ mkfs.ext4 512BM-ext4.img
$ sudo mkdir /mnt/ext4-file
$ sudo mount -o loop,noacl 512BM-ext4.img /mnt/ext4-file
Then share/export this filesystem over the network with NFSv4 to an appropriate machine. In this example I will use localhost, but it could be any machine on the local network. Do this by editing /etc/exports to have a line like the following.
/mnt/ext4-file/ localhost(ro,fsid=123123)
Then restart the NFS server on your machine, the service files could be different on your OS.
$ sudo systemctl restart nfs-server.service nfs-mountd.service
$ sudo exportfs -v
/mnt/ext4-file localhost(sync,wdelay,hide,no_subtree_check,fsid=123123,sec=sys,ro,secure,root_squash,no_all_squash)
Ensure that root_squash and ro is enabled.
It should now be possible to mount the NFSv4 share on your intended client.
$ sudo mount -t nfs -o ro localhost:/mnt/ext4-file /mnt/nfs-share/
$ findmnt /mnt/nfs-share
TARGET SOURCE FSTYPE OPTIONS
/mnt/nfs-share localhost:/mnt/ext4-file nfs4 rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255
And then we mount overlayfs2 on top of this share.
$ mkdir -p /tmp/overlay/{work,upper}
$ sudo mkdir /mnt/overlay
$ sudo mount -t overlay overlay -o lowerdir=/mnt/nfs-share/,upperdir=/tmp/overlay/upper/,workdir=/tmp/overlay/work/ /mnt/overlay/
For our experiment, we will create a folder in the filesystem that can only be read and written to by a user and read by a group. I chose the group belonging to my user.
$ sudo mkdir /mnt/ext4-file/rovanion
$ sudo chown rovanion:rovanion /mnt/ext4-file/rovanion
$ sudo chmod 2750 /mnt/ext4-file/rovanion
$ touch /mnt/ext4-file/rovanion/hi-from-ext4
$ ls -la /mnt/nfs-share/rovanion/
totalt 8,0K
drwxr-s--- 2 rovanion rovanion 4,0K sep 6 14:14 .
drwxr-xr-x 4 root root 4,0K sep 6 14:12 ..
-rw-rw-r-- 1 rovanion rovanion 0 sep 6 14:14 hi-from-ext4
$ touch /mnt/nfs-share/rovanion/hi-from-nfs
touch: cannot touch '/mnt/nfs-share/rovanion/hi-from-nfs': Read-only file system
We can list the contents of /mnt/nfs-share/rovanion, but are unable to touch the filesystem even though we have permission to, because the NFS share is mounted as read only. All is as expected.
Failure
But here comes the problem.
$ ls -la /mnt/overlay/rovanion/
ls: cannot open directory '/mnt/overlay/rovanion/': Permission denied
$ ls -l /mnt/overlay/
total 28K
drwx------ 2 root root 16K Sep 6 13:04 lost+found
drwxr-s--- 2 rovanion rovanion 4.0K Sep 6 14:14 rovanion
$ whoami
rovanion
$ groups
rovanion sudo
We are denied access to list /mnt/overlay/rovanion even though the permissions system should allow us to do so.
My best guess as to what is happening is that Overlayfs does all access to the underlying filesystem as root which by NFS' root squash gets mapped to nobody who is not allowed access to the folder, since nobody does not belong to the group rovanion and others are not allowed to list the folder.
Question
My question then is: Is it possible to work around this issue? To allow a user access to a folder through Overlayfs to which only a select group has access, without disabling root_squash on the NFS export/share or adding o+rx to the folder.