[PATCH] rbd: use check_sub_overflow() to limit the number of snapshots

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When compiling with clang-18 and W=1, I've noticed the following
warning:

drivers/block/rbd.c:6093:17: warning: result of comparison of constant
2305843009213693948 with expression of type 'u32' (aka 'unsigned int')
is always false [-Wtautological-constant-out-of-range-compare]
 6093 |         if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
      |             ~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 6094 |                                  / sizeof (u64)) {
      |                                  ~~~~~~~~~~~~~~

Since the plain check with '>' makes sense only if U32_MAX == SIZE_MAX
which is not true for the 64-bit kernel, prefer 'check_sub_overflow()'
in 'rbd_dev_v2_snap_context()' and 'rbd_dev_ondisk_valid()' as well.

Signed-off-by: Dmitry Antipov <dmantipov@xxxxxxxxx>
---
 drivers/block/rbd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index a999b698b131..ef8e6fbc9a79 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -933,7 +933,7 @@ static bool rbd_image_format_valid(u32 image_format)
 
 static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
 {
-	size_t size;
+	size_t size, result;
 	u32 snap_count;
 
 	/* The header has to start with the magic rbd header text */
@@ -956,7 +956,7 @@ static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
 	 */
 	snap_count = le32_to_cpu(ondisk->snap_count);
 	size = SIZE_MAX - sizeof (struct ceph_snap_context);
-	if (snap_count > size / sizeof (__le64))
+	if (check_sub_overflow(size / sizeof(__le64), snap_count, &result))
 		return false;
 
 	/*
@@ -6090,8 +6090,8 @@ static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev,
 	 * make sure the computed size of the snapshot context we
 	 * allocate is representable in a size_t.
 	 */
-	if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
-				 / sizeof (u64)) {
+	if (check_sub_overflow((SIZE_MAX - sizeof(struct ceph_snap_context))
+			       / sizeof(u64), snap_count, &size)) {
 		ret = -EINVAL;
 		goto out;
 	}
-- 
2.43.0





[Index of Archives]     [CEPH Users]     [Ceph Large]     [Ceph Dev]     [Information on CEPH]     [Linux BTRFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux