From f5a2b5ce851646a98786cbb85513a7a6a00139b1 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Fri, 23 Oct 2020 16:54:30 +0200 Subject: [PATCH] linux: do not set data when remounting read-only when remounting read only, leave data unset as older kernel versions (tested on 4.18) complain with EINVAL if it is set. Newer kernels seem to ignore it. Closes: https://github.com/containers/crun/issues/523 Signed-off-by: Giuseppe Scrivano --- src/libcrun/linux.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/libcrun/linux.c b/src/libcrun/linux.c index 4d6a2dfb7c..da16a65b81 100644 --- a/src/libcrun/linux.c +++ b/src/libcrun/linux.c @@ -399,6 +399,11 @@ do_remount (int targetfd, const char *target, unsigned long flags, const char *d real_target = target_buffer; } + /* Older kernels (seen on 4.18) fail with EINVAL if data is set when + setting MS_RDONLY. */ + if (flags & (MS_REMOUNT | MS_RDONLY)) + data = NULL; + ret = mount (NULL, real_target, NULL, flags, data); if (UNLIKELY (ret < 0)) { @@ -417,15 +422,15 @@ do_remount (int targetfd, const char *target, unsigned long flags, const char *d if (LIKELY (ret == 0)) return 0; - /* If it still fails try to add MS_RDONLY. */ - if ((sfs.f_flags & MS_RDONLY) && ((flags & MS_RDONLY) == 0)) + /* If it still fails and MS_RDONLY is present in the mount, try adding it. */ + if (sfs.f_flags & MS_RDONLY) { remount_flags = sfs.f_flags & (MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RDONLY); - ret = mount ("none", real_target, "", flags | remount_flags, data); + ret = mount (NULL, real_target, NULL, flags | remount_flags, data); } } if (UNLIKELY (ret < 0)) - return crun_make_error (err, errno, "remount `%s`", target); + return crun_make_error (err, errno, "remount `%s`", target); } return 0; }