rbd: fix integer overflow in rbd_header_from_disk()
Subsystems (2)
PATCH 1/1 rbd: fix integer overflow in rbd_header_from_disk()
ondisk->snap_count is read from disk via rbd_req_sync_read() and thus needs validation. Otherwise, a bogus `snap_count' could overflow the kmalloc() size, leading to memory corruption.
Also use u32' consistently for snap_count'.
--- drivers/block/rbd.c | 12 +++++++----- 1 files changed, 7 insertions(+), 5 deletions(-)
drivers/block/rbd.c +7 -5
@@ -487,18 +487,20 @@ static void rbd_coll_release(struct kref *kref)*/ static int rbd_header_from_disk (struct rbd_image_header *header,struct rbd_image_header_ondisk *ondisk, -int allocated_snaps, +u32 allocated_snaps,gfp_t gfp_flags) { -int i; -u32 snap_count; +u32 i, snap_count;if (memcmp (ondisk,RBD_HEADER_TEXT ,sizeof (RBD_HEADER_TEXT )))return -ENXIO ; snap_count =le32_to_cpu (ondisk->snap_count); +if (snap_count > (ULONG_MAX -sizeof (struct ceph_snap_context )) + /sizeof (*ondisk)) +return -EINVAL ; header->snapc =kmalloc (sizeof (struct ceph_snap_context ) + - snap_count *sizeof (*ondisk), + snap_count *sizeof (*ondisk), gfp_flags);if (!header->snapc)return -ENOMEM ;
@@ -1592,7 +1594,7 @@ static int rbd_read_header(struct rbd_device *rbd_dev, {ssize_t rc;struct rbd_image_header_ondisk *dh; -int snap_count =0 ; +u32 snap_count =0 ;u64 ver;size_t len;
Signature · 1 line
1.7.5.4
Quoted 5 lines · Xi Wang
ondisk->snap_countis read from disk viarbd_req_sync_read()and thus needs validation. Otherwise, a bogus `snap_count' could overflow thekmalloc()size, leading to memory corruption.Also use
u32' consistently forsnap_count'.
This looks good, however I have changed it to use UINT_MAX rather than ULONG_MAX, because on some architectures size_t (__kernel_size_t) is defined as type (unsigned int). It is the more conservative value, and even on architectures where __BITS_PER_LONG is 64, it still offers a sane upper bound on the number of snapshots for a rbd device.
Quoted 43 lines
Signed-off-by Xi Wang<[email protected]>--- drivers/block/rbd.c | 12 +++++++----- 1 files changed, 7 insertions(+), 5 deletions(-)drivers/block/rbd.c +7
-5@@ -487,18 +487,20 @@ static void rbd_coll_release(struct kref *kref)*/ static int rbd_header_from_disk (struct rbd_image_header *header,struct rbd_image_header_ondisk *ondisk, -int allocated_snaps, +u32 allocated_snaps,gfp_t gfp_flags) { -int i; -u32 snap_count; +u32 i, snap_count;if (memcmp (ondisk,RBD_HEADER_TEXT ,sizeof (RBD_HEADER_TEXT )))return -ENXIO ; snap_count =le32_to_cpu (ondisk->snap_count); +if (snap_count> (ULONG_MAX -sizeof (struct ceph_snap_context )) + /sizeof (*ondisk)) +return -EINVAL ; header->snapc =kmalloc (sizeof (struct ceph_snap_context ) + - snap_count *sizeof (*ondisk), + snap_count *sizeof (*ondisk), gfp_flags);if (!header->snapc)return -ENOMEM ;@@ -1592,7 +1594,7 @@ static int rbd_read_header(struct rbd_device *rbd_dev, {ssize_t rc;struct rbd_image_header_ondisk *dh; -int snap_count =0 ; +u32 snap_count =0 ;u64 ver;size_t len;
Quoted 7 lines · Alex Elder
This looks good, however I have changed it to use
UINT_MAXrather thanULONG_MAX, because on some architecturessize_t(__kernel_size_t) is defined as type (unsigned int). It is the more conservative value, and even on architectures where__BITS_PER_LONGis 64, it still offers a sane upper bound on the number of snapshots for a rbd device.
Looks good to me.
BTW, on which arch is size_t 32-bit while long is 64? Several commits still use ULONG_MAX, such as
http://git.kernel.org/linus/64486697
- xi
Quoted 9 lines · Xi Wang
Quoted 7 lines · Alex Elder
This looks good, however I have changed it to use
UINT_MAXrather thanULONG_MAX, because on some architecturessize_t(__kernel_size_t) is defined as type (unsigned int). It is the more conservative value, and even on architectures where__BITS_PER_LONGis 64, it still offers a sane upper bound on the number of snapshots for a rbd device.
Quoted 4 lines
Looks good to me.
BTW, on which arch is
size_t32-bit while long is 64? Several commits still useULONG_MAX, such as
In my tree, these at least *can* be: arm, m68k, mips, powerpc, sparc, x86, and others (including generic).
It may not matter in practice, but I prefer to be very careful. As long as you're going out of your way to avoid overflow might as well make sure it works on all architectures in the process.
-Alex
Quoted 8 lines
http://git.kernel.org/linus/64486697
- xi
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 4 lines · Alex Elder
It may not matter in practice, but I prefer to be very careful. As long as you're going out of your way to avoid overflow might as well make sure it works on all architectures in the process.
I agree. Probably a cleaner way is to define SIZE_MAX or use KMALLOC_MAX_SIZE.
- xi