ext4: fix EXT_MAX_EXTENT/INDEX to check for zeroed eh_max
authorHarshad Shirwadkar <harshadshirwadkar@gmail.com>
Tue, 21 Apr 2020 02:39:59 +0000 (19:39 -0700)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 4 Jun 2020 03:16:49 +0000 (23:16 -0400)
If eh->eh_max is 0, EXT_MAX_EXTENT/INDEX would evaluate to unsigned
(-1) resulting in illegal memory accesses. Although there is no
consistent repro, we see that generic/019 sometimes crashes because of
this bug.

Ran gce-xfstests smoke and verified that there were no regressions.

Signed-off-by: Harshad Shirwadkar <harshadshirwadkar@gmail.com>
Link: https://lore.kernel.org/r/20200421023959.20879-2-harshadshirwadkar@gmail.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: stable@kernel.org
fs/ext4/ext4_extents.h

index 1c216fcc202ad4629927d822645107a234bf0dad..44e59881a1f0b5d6d7f18082d36acc53f0d8233e 100644 (file)
@@ -170,10 +170,13 @@ struct partial_cluster {
        (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1)
 #define EXT_LAST_INDEX(__hdr__) \
        (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1)
-#define EXT_MAX_EXTENT(__hdr__) \
-       (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)
+#define EXT_MAX_EXTENT(__hdr__)        \
+       ((le16_to_cpu((__hdr__)->eh_max)) ? \
+       ((EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)) \
+                                       : 0)
 #define EXT_MAX_INDEX(__hdr__) \
-       (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)
+       ((le16_to_cpu((__hdr__)->eh_max)) ? \
+       ((EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)) : 0)
 
 static inline struct ext4_extent_header *ext_inode_hdr(struct inode *inode)
 {