docker run -ti -v /root:/root:Z fedora sh
The container ran fine, and everything was well on his server machine until the next time he tried to ssh into the server.
The sshd refused to allow him in? What went wrong?
I wrote about using volumes and SELinux on the project atomic blog. I explain their that in order to use a volume within a non privileged container, you need to relabel the content on the volume. You can either use the :z or the :Z option.
:z will relabel with a shared label so other containers ran read and write the volume.
:Z will relabel with a private label so that only this specific container can read and write the volume.
I probably did not emphasize enough is that as Peter Parker (Spider Man) says: With great power comes great responsibility.
Meaning you have to be careful what you relabel. Using one of the :Z and :z options to recursively change the labels of the source content to container_file_t. When doing this you must be sure that this content is truly private to the container. The content is not needed by other confined domains. For example doing a volume mount of -v /var/lib/mariadb:/var/lib/mariadb:Z for a mariadb container is probably a good idea. But while doing -v /var/lib:/var/lib:Z will work, it is probably a bad idea.
Back to the email, the user relabeled all of the content under /root with a label similar to system_u:object_r:container_file_t:s0:c1
Later when he attempted to ssh in, the sshd daemon, running as the sshd_t type, attempts to read content in /root/.ssh it gets permission denied, since sshd_t is not allowed to read container_file_t.
The emailer realized what happened and tried to fix this situation by running restorecon -R -v /root, but this failed to change them?
Why did the labels not change when he ran restorecon?
There is a little known feature of restorecon called customizable_types, that I talked about 10 years ago.
By default, restorecon does not change types defined in the customizable_types file. These types can be randomly scattered around the file system, and we don't want a global relabel to change them. This is meant to make it easier to the admin, but sometimes causes confusion. The -F option tells restorecon to force the relabel and ignore customizable_types.
restorecon -F -R /root
This comand will reset the labels under /root to the system default and allow the emailer to login to the system via sshd again.
We have safeguards built into the SELinux go bindings which prevent container runtimes relabeling of /, /etc, and /usr.
I need to open a pull request to add a few more directories to help prevent users from making serious mistakes in labeling, starting with /root.