Skip to contentSkip to conversation

partition.py: restore selinux label for fstab

PATCH In review Yi Zhao 2 participants 2 messages Apr 17 – May 25

lore
Share & tools
lore

Yi Zhao

author
#0 lore raw

PATCH 1/1 partition.py: restore selinux label for fstab

When SELinux is enabled, all files within a wic image retain their SELinux labels except for /etc/fstab, which is replaced by a newly generated one during the build, causing its extended attributes to be lost.

Restore the SELinux context on the new fstab using debugfs ea_get and ea_set commands, ensuring all files are correctly labeled at build time and avoiding a full SELinux relabel on first boot.

---
 src/wic/partition.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
src/wic/partition.py +11 -0
index 435d31d..506ef2b 100644
@@ -362,10 +362,21 @@ class Partition():
 
         if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
             debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
+            fstab_label_path = os.path.join(cr_workdir, "fstab.selinuxlabel")
+            with open(debugfs_script_path, "w") as f:
+                f.write("cd etc\n")
+                # Retrieve fstab selinux label
+                f.write("ea_get -f %s fstab security.selinux\n" % (fstab_label_path))
+            debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
+            exec_native_cmd(debugfs_cmd, native_sysroot)
+
             with open(debugfs_script_path, "w") as f:
                 f.write("cd etc\n")
                 f.write("rm fstab\n")
                 f.write("write %s fstab\n" % (self.updated_fstab_path))
+                if os.path.isfile(fstab_label_path) and os.path.getsize(fstab_label_path) > 0:
+                    # Restore fstab selinux label
+                    f.write("ea_set -f %s fstab security.selinux\n" % (fstab_label_path))
             debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
             exec_native_cmd(debugfs_cmd, native_sysroot)
 
Signature · 1 line
2.43.0
#1 lore raw

Hi,

Thanks for the patch.

Quoted 19 lines · Yi Zhao via lists.yoctoproject.org

When SELinux is enabled, all files within a wic image retain their SELinux labels except for /etc/fstab, which is replaced by a newly generated one during the build, causing its extended attributes to be lost.

Restore the SELinux context on the new fstab using debugfs ea_get and ea_set commands, ensuring all files are correctly labeled at build time and avoiding a full SELinux relabel on first boot.

---
 src/wic/partition.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
src/wic/partition.py +0 -0
index 435d31d..506ef2b 100644

src/wic/partition.py class Partition(): · line 371

Quoted 10 lines
src/wic/partition.py +7 -0
@@ -362,10 +362,21 @@ class Partition():
 
         if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
             debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
+            fstab_label_path = os.path.join(cr_workdir, "fstab.selinuxlabel")
+            with open(debugfs_script_path, "w") as f:
+                f.write("cd etc\n")
+                # Retrieve fstab selinux label
+                f.write("ea_get -f %s fstab security.selinux\n" % (fstab_label_path))
+            debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
+            exec_native_cmd(debugfs_cmd, native_sysroot)

This debugfs call is only *reading* an xattr (ea_get), so it does not need the -w flag; please remove.

Also, if the rootfs is non-SELinux, this call will print "xattr security.selinux not found" or something like that which ends up in the wic log. Can you arrange so this doesn't happen otherwise users will be confused by this line appearing in the log and might waste time trying to figure out why.

Quoted 25 lines
src/wic/partition.py +4 -0
+
             with open(debugfs_script_path, "w") as f:
                 f.write("cd etc\n")
                 f.write("rm fstab\n")
                 f.write("write %s fstab\n" % (self.updated_fstab_path))
+                if os.path.isfile(fstab_label_path) and os.path.getsize(fstab_label_path) > 0:
+                    # Restore fstab selinux label
+                    f.write("ea_set -f %s fstab security.selinux\n" % (fstab_label_path))
             debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
             exec_native_cmd(debugfs_cmd, native_sysroot)
 
Signature · 11 lines
2.43.0



-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#3726): https://lists.yoctoproject.org/g/yocto-patches/message/3726
Mute This Topic: https://lists.yoctoproject.org/mt/118870524/900817
Group Owner: [email protected]
Unsubscribe: https://lists.yoctoproject.org/g/yocto-patches/leave/13168745/900817/63955952/xyzzy [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-