Make sure symlinks created by ueventd are correctly labeled
Bug: 391078491
Test: `ls -alZ /dev/block/by-name | grep "boot_[ab]"` in cuttlefish.
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-23 13:03 boot_a -> /dev/block/vda2
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-23 13:03 boot_b -> /dev/block/vda3
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-23 13:03 init_boot_a -> /dev/block/vda4
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-23 13:03 init_boot_b -> /dev/block/vda5
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-23 13:03 vendor_boot_a -> /dev/block/vda6
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-23 13:03 vendor_boot_b -> /dev/block/vda7
Previously it was
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-21 13:36 boot_a -> /dev/block/vda2
lrwxrwxrwx 1 root root u:object_r:block_device:s0 15 2025-01-21 13:36 boot_b -> /dev/block/vda3
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-21 13:36 init_boot_a -> /dev/block/vda4
lrwxrwxrwx 1 root root u:object_r:block_device:s0 15 2025-01-21 13:36 init_boot_b -> /dev/block/vda5
lrwxrwxrwx 1 root root u:object_r:boot_block_device:s0 15 2025-01-23 13:03 vendor_boot_a -> /dev/block/vda6
lrwxrwxrwx 1 root root u:object_r:block_device:s0 15 2025-01-23 13:03 vendor_boot_b -> /dev/block/vda7
Change-Id: I448514cbf44f1f86c7f3493de23ede4690c7c4ac
diff --git a/init/devices.cpp b/init/devices.cpp
index aeaa431..cead726 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -599,7 +599,22 @@
PLOG(ERROR) << "Failed to create directory " << Dirname(link);
}
- if (symlink(target.c_str(), link.c_str())) {
+ // Create symlink and make sure it's correctly labeled
+ std::string secontext;
+ // Passing 0 for mode should work.
+ if (SelabelLookupFileContext(link, 0, &secontext) && !secontext.empty()) {
+ setfscreatecon(secontext.c_str());
+ }
+
+ int rc = symlink(target.c_str(), link.c_str());
+
+ if (!secontext.empty()) {
+ int save_errno = errno;
+ setfscreatecon(nullptr);
+ errno = save_errno;
+ }
+
+ if (rc < 0) {
if (errno != EEXIST) {
PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
} else if (std::string link_path;