6f381c
From 7a3843972ea290daf1bec5e1133db654749b8c02 Mon Sep 17 00:00:00 2001
6f381c
From: Franck Bui <fbui@suse.com>
6f381c
Date: Tue, 22 Oct 2019 16:09:21 +0200
6f381c
Subject: [PATCH] fileio: introduce read_full_virtual_file() for reading
6f381c
 virtual files in sysfs, procfs
6f381c
6f381c
Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work
6f381c
with two sorts of virtual files.
6f381c
6f381c
One sort uses "seq_file", and the results of the first read are buffered for
6f381c
the second read. The other sort uses "raw" reads which always go direct to the
6f381c
device.
6f381c
6f381c
In the later case, the content of the virtual file must be retrieved with a
6f381c
single read otherwise subsequent read might get the new value instead of
6f381c
finding EOF immediately. That's the reason why the usage of fread(3) is
6f381c
prohibited in this case as it always performs a second call to read(2) looking
6f381c
for EOF which is subject to the race described previously.
6f381c
6f381c
Fixes: #13585.
6f381c
(cherry picked from commit 21b40f16622f171a9969dc334d74fb5eb2f575c2)
6f381c
6f381c
Related: #2117948
6f381c
---
6f381c
 src/basic/fileio.c                   | 115 ++++++++++++++++++++++++++-
6f381c
 src/basic/fileio.h                   |   1 +
6f381c
 src/libsystemd/sd-device/sd-device.c |   2 +-
6f381c
 3 files changed, 113 insertions(+), 5 deletions(-)
6f381c
6f381c
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
6f381c
index 6b0bad5b71..733fb42463 100644
6f381c
--- a/src/basic/fileio.c
6f381c
+++ b/src/basic/fileio.c
6f381c
@@ -276,6 +276,113 @@ int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
6f381c
         return 1;
6f381c
 }
6f381c
 
6f381c
+int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) {
6f381c
+        _cleanup_free_ char *buf = NULL;
6f381c
+        _cleanup_close_ int fd = -1;
6f381c
+        struct stat st;
6f381c
+        size_t n, size;
6f381c
+        int n_retries;
6f381c
+        char *p;
6f381c
+
6f381c
+        assert(ret_contents);
6f381c
+
6f381c
+        /* Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work
6f381c
+         * with two sorts of virtual files. One sort uses "seq_file", and the results of
6f381c
+         * the first read are buffered for the second read. The other sort uses "raw"
6f381c
+         * reads which always go direct to the device. In the latter case, the content of
6f381c
+         * the virtual file must be retrieved with a single read otherwise a second read
6f381c
+         * might get the new value instead of finding EOF immediately. That's the reason
6f381c
+         * why the usage of fread(3) is prohibited in this case as it always performs a
6f381c
+         * second call to read(2) looking for EOF. See issue 13585. */
6f381c
+
6f381c
+        fd = open(filename, O_RDONLY|O_CLOEXEC);
6f381c
+        if (fd < 0)
6f381c
+                return -errno;
6f381c
+
6f381c
+        /* Start size for files in /proc which usually report a file size of 0. */
6f381c
+        size = LINE_MAX / 2;
6f381c
+
6f381c
+        /* Limit the number of attempts to read the number of bytes returned by fstat(). */
6f381c
+        n_retries = 3;
6f381c
+
6f381c
+        for (;;) {
6f381c
+                if (n_retries <= 0)
6f381c
+                        return -EIO;
6f381c
+
6f381c
+                if (fstat(fd, &st) < 0)
6f381c
+                        return -errno;
6f381c
+
6f381c
+                if (!S_ISREG(st.st_mode))
6f381c
+                        return -EBADF;
6f381c
+
6f381c
+                /* Be prepared for files from /proc which generally report a file size of 0. */
6f381c
+                if (st.st_size > 0) {
6f381c
+                        size = st.st_size;
6f381c
+                        n_retries--;
6f381c
+                } else
6f381c
+                        size = size * 2;
6f381c
+
6f381c
+                if (size > READ_FULL_BYTES_MAX)
6f381c
+                        return -E2BIG;
6f381c
+
6f381c
+                p = realloc(buf, size + 1);
6f381c
+                if (!p)
6f381c
+                        return -ENOMEM;
6f381c
+                buf = TAKE_PTR(p);
6f381c
+
6f381c
+                for (;;) {
6f381c
+                        ssize_t k;
6f381c
+
6f381c
+                        /* Read one more byte so we can detect whether the content of the
6f381c
+                         * file has already changed or the guessed size for files from /proc
6f381c
+                         * wasn't large enough . */
6f381c
+                        k = read(fd, buf, size + 1);
6f381c
+                        if (k >= 0) {
6f381c
+                                n = k;
6f381c
+                                break;
6f381c
+                        }
6f381c
+
6f381c
+                        if (errno != -EINTR)
6f381c
+                                return -errno;
6f381c
+                }
6f381c
+
6f381c
+                /* Consider a short read as EOF */
6f381c
+                if (n <= size)
6f381c
+                        break;
6f381c
+
6f381c
+                /* Hmm... either we read too few bytes from /proc or less likely the content
6f381c
+                 * of the file might have been changed (and is now bigger) while we were
6f381c
+                 * processing, let's try again either with a bigger guessed size or the new
6f381c
+                 * file size. */
6f381c
+
6f381c
+                if (lseek(fd, 0, SEEK_SET) < 0)
6f381c
+                        return -errno;
6f381c
+        }
6f381c
+
6f381c
+        if (n < size) {
6f381c
+                p = realloc(buf, n + 1);
6f381c
+                if (!p)
6f381c
+                        return -ENOMEM;
6f381c
+                buf = TAKE_PTR(p);
6f381c
+        }
6f381c
+
6f381c
+        if (!ret_size) {
6f381c
+                /* Safety check: if the caller doesn't want to know the size of what we
6f381c
+                 * just read it will rely on the trailing NUL byte. But if there's an
6f381c
+                 * embedded NUL byte, then we should refuse operation as otherwise
6f381c
+                 * there'd be ambiguity about what we just read. */
6f381c
+
6f381c
+                if (memchr(buf, 0, n))
6f381c
+                        return -EBADMSG;
6f381c
+        } else
6f381c
+                *ret_size = n;
6f381c
+
6f381c
+        buf[n] = 0;
6f381c
+        *ret_contents = TAKE_PTR(buf);
6f381c
+
6f381c
+        return 0;
6f381c
+}
6f381c
+
6f381c
 int read_full_stream(FILE *f, char **contents, size_t *size) {
6f381c
         _cleanup_free_ char *buf = NULL;
6f381c
         struct stat st;
6f381c
@@ -300,9 +407,9 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
6f381c
                         if (st.st_size > READ_FULL_BYTES_MAX)
6f381c
                                 return -E2BIG;
6f381c
 
6f381c
-                        /* Start with the right file size, but be prepared for files from /proc which generally report a file
6f381c
-                         * size of 0. Note that we increase the size to read here by one, so that the first read attempt
6f381c
-                         * already makes us notice the EOF. */
6f381c
+                        /* Start with the right file size. Note that we increase the size
6f381c
+                         * to read here by one, so that the first read attempt already
6f381c
+                         * makes us notice the EOF. */
6f381c
                         if (st.st_size > 0)
6f381c
                                 n = st.st_size + 1;
6f381c
                 }
6f381c
@@ -986,7 +1093,7 @@ int get_proc_field(const char *filename, const char *pattern, const char *termin
6f381c
         assert(pattern);
6f381c
         assert(field);
6f381c
 
6f381c
-        r = read_full_file(filename, &status, NULL);
6f381c
+        r = read_full_virtual_file(filename, &status, NULL);
6f381c
         if (r < 0)
6f381c
                 return r;
6f381c
 
6f381c
diff --git a/src/basic/fileio.h b/src/basic/fileio.h
6f381c
index 77e6206e95..c6ad375b8d 100644
6f381c
--- a/src/basic/fileio.h
6f381c
+++ b/src/basic/fileio.h
6f381c
@@ -38,6 +38,7 @@ int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *f
6f381c
 int read_one_line_file(const char *fn, char **line);
6f381c
 int read_full_file(const char *fn, char **contents, size_t *size);
6f381c
 int read_full_stream(FILE *f, char **contents, size_t *size);
6f381c
+int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size);
6f381c
 
6f381c
 int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
6f381c
 
6f381c
diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c
6f381c
index be29053f8c..49750ba9d7 100644
6f381c
--- a/src/libsystemd/sd-device/sd-device.c
6f381c
+++ b/src/libsystemd/sd-device/sd-device.c
6f381c
@@ -1798,7 +1798,7 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
6f381c
                 size_t size;
6f381c
 
6f381c
                 /* read attribute value */
6f381c
-                r = read_full_file(path, &value, &size);
6f381c
+                r = read_full_virtual_file(path, &value, &size);
6f381c
                 if (r < 0)
6f381c
                         return r;
6f381c