Question regarding restorecon and btrfs read-only snapshots
Hi all,
I have a question regarding restorecon and btrfs read-only snapshot handling.
restorecon is failing with "restorecon: Could not set context for <path>: Read-only file system" and return code 255 on btrfs read-only snapshots.
Currently we are setting <<none>> for those read-only btrfs snapshots in the selinux policy, as we use restorecon in our autorelabelling [0] during boot and restorecon would fail with code 255 otherwise. We do not want to ignore non-zero return codes, since issues might be overlooked.
However, this is also not optimal as we have to write every possible path into the policy or asking users to set the <<none>> tag manually.
I was wondering if there was interest/plans in implementing to skip read-only btrfs subvolumes in restorecon entirely or provide a different return code other than the catchall LABEL_FILE_KIND_INVALID? Or is there another way that we did not see?
For more context, this is the bug on our side: https://bugzilla.suse.com/show_bug.cgi?id=1232226
There was also some comments about possible implementation, see comment 1 in the bug.
Thanks :)
Kind regards,
Cathy
[0] https://github.com/openSUSE/microos-tools/blob/master/selinux/selinux-autorelabel-generator
Signature · 10 lines
Cathy Hu <[email protected]> SELinux Security Engineer GPG: 5873 CFD1 8C0E A6D4 9CBB F6C4 062A 1016 1505 A08A SUSE Software Solutions Germany GmbH Frankenstrasse 146 90461 Nürnberg Geschäftsführer: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB 36809, AG Nürnberg)
Quoted 14 lines · Cathy Hu
Hi all,
I have a question regarding restorecon and btrfs read-only snapshot handling.
restorecon is failing with "restorecon: Could not set context for <path>: Read-only file system" and return code 255 on btrfs read-only snapshots.
Currently we are setting <<none>> for those read-only btrfs snapshots in the selinux policy, as we use restorecon in our autorelabelling [0] during boot and restorecon would fail with code 255 otherwise. We do not want to ignore non-zero return codes, since issues might be overlooked.
However, this is also not optimal as we have to write every possible path into the policy or asking users to set the <<none>> tag manually.
Quoted 6 lines
I was wondering if there was interest/plans in implementing to skip read-only btrfs subvolumes in restorecon entirely or provide a different return code other than the catchall
LABEL_FILE_KIND_INVALID? Or is there another way that we did not see?For more context, this is the bug on our side: https://bugzilla.suse.com/show_bug.cgi?id=1232226
There was also some comments about possible implementation, see comment 1 in the bug.
You could use -e <directory> to exclude read only subdirectories.
Petr
Quoted 20 lines
Thanks :)
Kind regards,
Cathy
[0] https://github.com/openSUSE/microos-tools/blob/master/selinux/selinux-autorelabel-generator
Signature · 10 lines
Cathy Hu <[email protected]> SELinux Security Engineer GPG: 5873 CFD1 8C0E A6D4 9CBB F6C4 062A 1016 1505 A08A SUSE Software Solutions Germany GmbH Frankenstrasse 146 90461 Nürnberg Geschäftsführer: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB 36809, AG Nürnberg)
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.
Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
Cathy
Signature · 10 lines
Cathy Hu <[email protected]> SELinux Security Engineer GPG: 5873 CFD1 8C0E A6D4 9CBB F6C4 062A 1016 1505 A08A SUSE Software Solutions Germany GmbH Frankenstrasse 146 90461 Nürnberg Geschäftsführer: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB 36809, AG Nürnberg)
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Your -relabel.service's are generated and so can be restorecon options there.
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.
Quoted 19 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
CathySignature · 10 lines
Cathy Hu <[email protected]> SELinux Security Engineer GPG: 5873 CFD1 8C0E A6D4 9CBB F6C4 062A 1016 1505 A08A SUSE Software Solutions Germany GmbH Frankenstrasse 146 90461 Nürnberg Geschäftsführer: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB 36809, AG Nürnberg)
Ah thanks for the clarification!
Quoted 40 lines · Petr Lautrbach
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Your -relabel.service's are generated and so can be restorecon options there.
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.Quoted 19 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
CathySignature · 10 lines
Cathy Hu <[email protected]> SELinux Security Engineer GPG: 5873 CFD1 8C0E A6D4 9CBB F6C4 062A 1016 1505 A08A SUSE Software Solutions Germany GmbH Frankenstrasse 146 90461 Nürnberg Geschäftsführer: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB 36809, AG Nürnberg)
Signature · 10 lines
Cathy Hu <[email protected]> SELinux Security Engineer GPG: 5873 CFD1 8C0E A6D4 9CBB F6C4 062A 1016 1505 A08A SUSE Software Solutions Germany GmbH Frankenstrasse 146 90461 Nürnberg Geschäftsführer: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB 36809, AG Nürnberg)
Stephen Smalley
maintainerQuoted 12 lines · Petr Lautrbach
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Quoted 6 lines
Your -relabel.service's are generated and so can be restorecon options there.
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.
We already have logic in libselinux/src/selinux_restorecon.c to exclude filesystems that lack seclabel support; should we augment this to also exclude read-only filesystems to avoid the need to work around this in all callers?
Quoted 11 lines
Quoted 11 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
Cathy
Quoted 16 lines · Stephen Smalley
Quoted 15 lines · Petr Lautrbach
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Your -relabel.service's are generated and so can be restorecon options there.
Quoted 9 lines
Quoted 3 lines
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.We already have logic in
libselinux/src/selinux_restorecon.cto exclude filesystems that lack seclabel support; should we augment this to also exclude read-only filesystems to avoid the need to work around this in all callers?
https://github.com/SELinuxProject/selinux/blob/main/libselinux/src/selinux_restorecon.c#L238
You're right, I didn't know about that.
I think it would make sense to exclude also ro mount points.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
Cathy
Stephen Smalley
maintainerQuoted 29 lines · Petr Lautrbach
Quoted 16 lines · Stephen Smalley
Quoted 15 lines · Petr Lautrbach
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Your -relabel.service's are generated and so can be restorecon options there.
Quoted 9 lines
Quoted 3 lines
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.We already have logic in
libselinux/src/selinux_restorecon.cto exclude filesystems that lack seclabel support; should we augment this to also exclude read-only filesystems to avoid the need to work around this in all callers?
Quoted 5 lines
https://github.com/SELinuxProject/selinux/blob/main/libselinux/src/selinux_restorecon.c#L238
You're right, I didn't know about that.
I think it would make sense to exclude also
romount points.
I think the tricky part is the case where the caller deliberately passed those mount points to restorecon/setfiles. The current exclusion logic IIRC won't exclude any explicitly passed directories to avoid silently failing. But skipping read-only mounts on a traversal of a subdirectory would make sense IMHO.
Quoted 13 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
Cathy
Stephen Smalley
maintainerQuoted 35 lines · Stephen Smalley
Quoted 33 lines · Petr Lautrbach
Quoted 16 lines · Stephen Smalley
Quoted 15 lines · Petr Lautrbach
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Your -relabel.service's are generated and so can be restorecon options there.
Quoted 9 lines
Quoted 3 lines
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.We already have logic in
libselinux/src/selinux_restorecon.cto exclude filesystems that lack seclabel support; should we augment this to also exclude read-only filesystems to avoid the need to work around this in all callers?https://github.com/SELinuxProject/selinux/blob/main/libselinux/src/selinux_restorecon.c#L238
You're right, I didn't know about that.
Quoted 7 lines
Quoted 1 line
I think it would make sense to exclude also
romount points.I think the tricky part is the case where the caller deliberately passed those mount points to restorecon/setfiles. The current exclusion logic IIRC won't exclude any explicitly passed directories to avoid silently failing. But skipping read-only mounts on a traversal of a subdirectory would make sense IMHO.
Actually, maybe not. Scenario: Read-only mount on a higher level directory with read-write mount of a lower level directory (e.g. read-only / with a writable /var), and restorecon or setfiles invoked on /.
Maybe it is best to just defer this to the callers.
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
Cathy
Quoted 46 lines · Stephen Smalley
Quoted 35 lines · Stephen Smalley
Quoted 33 lines · Petr Lautrbach
Quoted 16 lines · Stephen Smalley
Quoted 15 lines · Petr Lautrbach
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Your -relabel.service's are generated and so can be restorecon options there.
Quoted 9 lines
Quoted 3 lines
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.We already have logic in
libselinux/src/selinux_restorecon.cto exclude filesystems that lack seclabel support; should we augment this to also exclude read-only filesystems to avoid the need to work around this in all callers?https://github.com/SELinuxProject/selinux/blob/main/libselinux/src/selinux_restorecon.c#L238
You're right, I didn't know about that.
Quoted 7 lines
Quoted 1 line
I think it would make sense to exclude also
romount points.I think the tricky part is the case where the caller deliberately passed those mount points to restorecon/setfiles. The current exclusion logic IIRC won't exclude any explicitly passed directories to avoid silently failing. But skipping read-only mounts on a traversal of a subdirectory would make sense IMHO.
Quoted 5 lines
Actually, maybe not. Scenario: Read-only mount on a higher level directory with read-write mount of a lower level directory (e.g. read-only / with a writable /var), and restorecon or setfiles invoked on /.
Maybe it is best to just defer this to the callers.
You beat me to this, I had a draft sitting in my inbox. My suggestion was to report that it occured, but not
fail and keep going if the return code is for read only failure.
Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
Cathy
Hi,
Am Mittwoch, 19. März 2025, 15:35:58 Mitteleuropäische Normalzeit schrieb William Roberts:
Quoted 35 lines
Quoted 33 lines · Stephen Smalley
Quoted 30 lines · Stephen Smalley
Quoted 28 lines · Petr Lautrbach
Quoted 25 lines · Stephen Smalley
Quoted 12 lines · Petr Lautrbach
Quoted 8 lines · Cathy Hu
Quoted 3 lines · Petr Lautrbach
You could use
-e <directory>to exclude read only subdirectories.Yes that is possible, but also requires a manual change by the user to set this up together with the snapshot (same as telling them to add <<none>>), which we would like to avoid.
Quoted 6 lines
Your -relabel.service's are generated and so can be restorecon options there.
Fedora uses fixfiles -
https://github.com/SELinuxProject/selinux/blob/main/policycoreutils/scripts/fixfiles
- which detects ro filesystems and skip them.We already have logic in
libselinux/src/selinux_restorecon.cto exclude filesystems that lack seclabel support; should we augment this to also exclude read-only filesystems to avoid the need to work around this in all callers?
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
https://github.com/SELinuxProject/selinux/blob/main/libselinux/src/selinux_restorecon.c#L238
You're right, I didn't know about that.
I think it would make sense to exclude also
romount points.
Note that btrfs subvolumes don't necessarily show up as separate mount points:
Essentially any directory could be a subvolume. That makes it rather annoying to gather a complete list for -e before calling restorecon (would need full FS traversal).
To detect ro subols in restorecon, changes of st_dev need to be detected. Quoting my comment from bugzilla:
Quoted 5 lines
(Some notes for d): This could be implemented in restorecon by adding to the if (
ftsent->fts_statp->st_dev!=state->dev_num) check. TheBTRFS_IOC_SUBVOL_GETFLAGSioctl can be called on that FD and the result checked againstBTRFS_SUBVOL_RDONLY. That is not too complex or expensive and does not need additional dependencies on e.g. libbtrfs.)
That leaves the question of what to do with that info. At first maybe just set a flag that it's within a read-only subvol and soften error handling, i.e. this:
Quoted 12 lines
Quoted 11 lines
Quoted 5 lines
I think the tricky part is the case where the caller deliberately passed those mount points to restorecon/setfiles. The current exclusion logic IIRC won't exclude any explicitly passed directories to avoid silently failing. But skipping read-only mounts on a traversal of a subdirectory would make sense IMHO.
Actually, maybe not. Scenario: Read-only mount on a higher level directory with read-write mount of a lower level directory (e.g. read-only / with a writable /var), and restorecon or setfiles invoked on /.
Maybe it is best to just defer this to the callers.
Quoted 3 lines
You beat me to this, I had a draft sitting in my inbox. My suggestion was to report that it occured, but not
fail and keep going if the return code is for read only failure.
Cheers,
Fabian
Quoted 11 lines
Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Is there a reason why these r-o subvolumes are not skipped by default? Could they be skipped without a problem and it is just missing the implementation?
Thanks :)
Kind regards,
Cathy