]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal: add some careful overflow checking
authorLennart Poettering <lennart@poettering.net>
Tue, 8 Jun 2021 20:14:40 +0000 (22:14 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 9 Jun 2021 07:34:50 +0000 (09:34 +0200)
src/libsystemd/sd-journal/sd-journal.c

index fce0ae0f871abe9ba37e6582dbd89d81aacf9dc8..35834d97a1582ee9100571c208bf62f99ebe500e 100644 (file)
@@ -2837,24 +2837,32 @@ void journal_print_header(sd_journal *j) {
         }
 }
 
-_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
+_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *ret) {
         JournalFile *f;
         uint64_t sum = 0;
 
         assert_return(j, -EINVAL);
         assert_return(!journal_pid_changed(j), -ECHILD);
-        assert_return(bytes, -EINVAL);
+        assert_return(ret, -EINVAL);
 
         ORDERED_HASHMAP_FOREACH(f, j->files) {
                 struct stat st;
+                uint64_t b;
 
                 if (fstat(f->fd, &st) < 0)
                         return -errno;
 
-                sum += (uint64_t) st.st_blocks * 512ULL;
+                b = (uint64_t) st.st_blocks;
+                if (b > UINT64_MAX / 512)
+                        return -EOVERFLOW;
+                b *= 512;
+
+                if (sum > UINT64_MAX - b)
+                        return -EOVERFLOW;
+                sum += b;
         }
 
-        *bytes = sum;
+        *ret = sum;
         return 0;
 }