Skip to contentSkip to conversation

libsemanage: fsync before rename

PATCH Superseded Smalley 2 participants 3 messages May 13, 2020 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Smalley

author maintainer
#0 lore raw

PATCH 1/1 libsemanage: fsync before rename

From: Stephen Smalley <[email protected]>

Prior to rename(2)'ing new files into place, fsync(2) them to ensure the contents will be fully written prior to rename. While we are here, also fix checking of write(2) to detect short writes. This code could be more generally improved but keeping to the minimal changes required to fix this bug.

---
 libsemanage/src/semanage_store.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +5 -1
index 859c0a22..3cac36d4 100644
@@ -735,7 +735,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode)
 	}
 	umask(mask);
 	while (retval == 0 && (amount_read = read(in, buf, sizeof(buf))) > 0) {
-		if (write(out, buf, amount_read) < 0) {
+		if (write(out, buf, amount_read) != amount_read) {
 			errsv = errno;
 			retval = -1;
 		}
@@ -745,6 +745,10 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode)
 		retval = -1;
 	}
 	close(in);
+	if (fsync(out) < 0) {
+		errsv = errno;
+		retval = -1;
+	}
 	if (close(out) < 0) {
 		errsv = errno;
 		retval = -1;
Signature · 1 line
2.23.3
#1 lore raw
Quoted 20 lines · Smalley

From: Stephen Smalley <[email protected]>

Prior to rename(2)'ing new files into place, fsync(2) them to ensure the contents will be fully written prior to rename. While we are here, also fix checking of write(2) to detect short writes. This code could be more generally improved but keeping to the minimal changes required to fix this bug.

---
 libsemanage/src/semanage_store.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +0 -0
index 859c0a22..3cac36d4 100644

libsemanage/src/semanage_store.c int semanage_copy_file(const char *src, const char *dst, mode_t mode) · line 741

Quoted 8 lines
libsemanage/src/semanage_store.c +1 -1
@@ -735,7 +735,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode)
        }
        umask(mask);
        while (retval == 0 && (amount_read = read(in, buf, sizeof(buf))) > 0) {
-               if (write(out, buf, amount_read) < 0) {
+               if (write(out, buf, amount_read) != amount_read) {
                        errsv = errno;
                        retval = -1;
                }

If I remember correctly, errno is not defined if a short write happens. If this is confirmed and if you want to keep the patch short, you could for example use errsv = EIO if write() returned a value different from -1 and from amount_read.

Thanks,
Nicolas

Quoted 14 lines
libsemanage/src/semanage_store.c +4 -1
@@ -745,6 +745,10 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode)
                retval = -1;
        }
        close(in);
+       if (fsync(out) < 0) {
+               errsv = errno;
+               retval = -1;
+       }
        if (close(out) < 0) {
                errsv = errno;
                retval = -1;
--

2.23.3

author maintainer
#2 lore raw
Quoted 27 lines · Nicolas Iooss
Quoted 25 lines · Smalley

From: Stephen Smalley <[email protected]>

Prior to rename(2)'ing new files into place, fsync(2) them to ensure the contents will be fully written prior to rename. While we are here, also fix checking of write(2) to detect short writes. This code could be more generally improved but keeping to the minimal changes required to fix this bug.

---
 libsemanage/src/semanage_store.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libsemanage/src/semanage_store.c +1 -1
index 859c0a22..3cac36d4 100644
@@ -735,7 +735,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode)
        }
        umask(mask);
        while (retval == 0 && (amount_read = read(in, buf, sizeof(buf))) > 0) {
-               if (write(out, buf, amount_read) < 0) {
+               if (write(out, buf, amount_read) != amount_read) {
Quoted 8 lines

libsemanage/src/semanage_store.c int semanage_copy_file(const char *src, const char *dst, mode_t mode) · line 741

Quoted 3 lines
libsemanage/src/semanage_store.c +0 -0
                        errsv = errno;
                        retval = -1;
                }

If I remember correctly, errno is not defined if a short write happens. If this is confirmed and if you want to keep the patch short, you could for example use errsv = EIO if write() returned a value different from -1 and from amount_read.

True. It also occurred to me that this is too heavyweight given how widely semanage_copy_file() is used within libsemanage; performing a fsync(2) for every one of these file copies will be very expensive. We only really care about it when copying the final files to /etc/selinux, not for the copying under /var/lib/selinux IIUC. So I guess I need a bool argument or similar to semanage_copy_file() to indicate when a fsync is required. Even with this change, installing the final SELinux policy files under /etc/selinux won't be fully atomic; one can still end up with a mix of old and new (e.g. new policy.32 with old file_contexts). But that's out of scope for this particular bug.