CEPH-Devel archive mirror
 help / color / mirror / Atom feed
From: Dmitry Antipov <dmantipov@yandex.ru>
To: Ilya Dryomov <idryomov@gmail.com>
Cc: Dongsheng Yang <dongsheng.yang@easystack.cn>,
	ceph-devel@vger.kernel.org, linux-block@vger.kernel.org,
	Dmitry Antipov <dmantipov@yandex.ru>
Subject: [PATCH] rbd: use check_sub_overflow() to limit the number of snapshots
Date: Thu, 21 Dec 2023 16:39:17 +0300	[thread overview]
Message-ID: <20231221133928.49824-1-dmantipov@yandex.ru> (raw)

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@yandex.ru>
---
 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


             reply	other threads:[~2023-12-21 13:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-21 13:39 Dmitry Antipov [this message]
2023-12-22 14:23 ` [PATCH] rbd: use check_sub_overflow() to limit the number of snapshots Ilya Dryomov
2023-12-22 14:48   ` Alex Elder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231221133928.49824-1-dmantipov@yandex.ru \
    --to=dmantipov@yandex.ru \
    --cc=ceph-devel@vger.kernel.org \
    --cc=dongsheng.yang@easystack.cn \
    --cc=idryomov@gmail.com \
    --cc=linux-block@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).