Skip to content

Commit 76aa542

Browse files
xiwAlex Elder
authored andcommitted
ceph: fix bounds check in ceph_decode_need and ceph_encode_need
Given a large n, the bounds check (*p + n > end) can be bypassed due to pointer wraparound. A safer check is (n > end - *p). [elder@dreamhost.com: inverted test and renamed ceph_has_room()] Signed-off-by: Xi Wang <xi.wang@gmail.com> Reviewed-by: Alex Elder <elder@dreamhost.com>
1 parent 065a68f commit 76aa542

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎include/linux/ceph/decode.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ static inline void ceph_decode_copy(void **p, void *pv, size_t n)
4545
/*
4646
* bounds check input.
4747
*/
48+
static inline int ceph_has_room(void **p, void *end, size_t n)
49+
{
50+
return end >= *p && n <= end - *p;
51+
}
52+
4853
#define ceph_decode_need(p, end, n, bad) \
4954
do { \
50-
if (unlikely(*(p) + (n) > (end))) \
55+
if (!likely(ceph_has_room(p, end, n))) \
5156
goto bad; \
5257
} while (0)
5358

@@ -166,7 +171,7 @@ static inline void ceph_encode_string(void **p, void *end,
166171

167172
#define ceph_encode_need(p, end, n, bad) \
168173
do { \
169-
if (unlikely(*(p) + (n) > (end))) \
174+
if (!likely(ceph_has_room(p, end, n))) \
170175
goto bad; \
171176
} while (0)
172177

0 commit comments

Comments
 (0)