Skip to content

Commit 33d22f8

Browse files
committed
fstab-generator: set mode=0755 with root=tmpfs
If mode= is not set in rootflags= add mode=0755 when a tmpfs is used on the rootfs, otherwise it will be group/world writable as that's the default mode for tmpfs filesystems. Follow-up for 725ad3b (cherry picked from commit d3a57a0) (cherry picked from commit 0e8f13faf3fafc3679131713915a56c74403f3a3) (cherry picked from commit 69beb94574ba41b167396fdcd651e9f59f2d8d75) (cherry picked from commit 0fc39fe) (cherry picked from commit e1560b3)
1 parent 385b53f commit 33d22f8

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/fstab-generator/fstab-generator.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ static int sysroot_is_nfsroot(void) {
840840

841841
static int add_sysroot_mount(void) {
842842
_cleanup_free_ char *what = NULL;
843-
const char *opts, *fstype;
843+
const char *extra_opts = NULL, *fstype;
844844
bool default_rw, makefs;
845845
MountPointFlags flags;
846846
int r;
@@ -890,6 +890,9 @@ static int add_sysroot_mount(void) {
890890
fstype = arg_root_fstype ?: "tmpfs"; /* tmpfs, unless overridden */
891891

892892
default_rw = true; /* writable, unless overridden */;
893+
894+
if (streq(fstype, "tmpfs") && !fstab_test_option(arg_root_options, "mode\0"))
895+
extra_opts = "mode=0755"; /* root directory should not be world/group writable, unless overridden */
893896
} else {
894897

895898
what = fstab_node_to_udev_node(arg_root_what);
@@ -901,23 +904,28 @@ static int add_sysroot_mount(void) {
901904
default_rw = false; /* read-only, unless overridden */
902905
}
903906

904-
if (!arg_root_options)
905-
opts = arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro";
906-
else if (arg_root_rw >= 0 ||
907-
!fstab_test_option(arg_root_options, "ro\0" "rw\0"))
908-
opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
909-
else
910-
opts = arg_root_options;
907+
_cleanup_free_ char *combined_options = NULL;
908+
combined_options = strdup(strempty(arg_root_options));
909+
if (!combined_options)
910+
return log_oom();
911+
912+
if (arg_root_rw >= 0 || !fstab_test_option(combined_options, "ro\0" "rw\0"))
913+
if (!strextend_with_separator(&combined_options, ",", arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro"))
914+
return log_oom();
915+
916+
if (extra_opts)
917+
if (!strextend_with_separator(&combined_options, ",", extra_opts))
918+
return log_oom();
911919

912-
log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(opts));
920+
log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(fstype), strempty(combined_options));
913921

914922
if (is_device_path(what)) {
915923
r = generator_write_initrd_root_device_deps(arg_dest, what);
916924
if (r < 0)
917925
return r;
918926
}
919927

920-
makefs = fstab_test_option(opts, "x-systemd.makefs\0");
928+
makefs = fstab_test_option(combined_options, "x-systemd.makefs\0");
921929
flags = makefs * MOUNT_MAKEFS;
922930

923931
return add_mount("/proc/cmdline",
@@ -926,7 +934,7 @@ static int add_sysroot_mount(void) {
926934
"/sysroot",
927935
NULL,
928936
fstype,
929-
opts,
937+
combined_options,
930938
is_device_path(what) ? 1 : 0, /* passno */
931939
flags, /* makefs off, pcrfs off, noauto off, nofail off, automount off */
932940
SPECIAL_INITRD_ROOT_FS_TARGET);

test/test-fstab-generator/test-16-tmpfs.expected/sysroot.mount

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Before=initrd-root-fs.target
99
What=rootfs
1010
Where=/sysroot
1111
Type=tmpfs
12-
Options=rw
12+
Options=rw,mode=0755

0 commit comments

Comments
 (0)