libceph: prevent potential out-of-bounds reads in process_message_header()
Ilya Dryomov
maintainerPATCH 1/1 libceph: prevent potential out-of-bounds reads in process_message_header()
If the message frame is (maliciously) corrupted in a way that the length of the control segment ends up being less than the size of the message header or a different frame is made to look like a message frame, out-of-bounds reads may ensue in process_message_header().
Perform an explicit bounds check before decoding the message header.
--- net/ceph/messenger_v2.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
net/ceph/messenger_v2.c +8 -1
@@ -2833,12 +2833,15 @@ static int process_message_header(struct ceph_connection *con, void *p, void *end) { struct ceph_frame_desc *desc = &con->v2.in_desc; -struct ceph_msg_header2 *hdr2 = p; +struct ceph_msg_header2 *hdr2;struct ceph_msg_header hdr;int skip;int ret;u64 seq; +ceph_decode_need (&p, end,sizeof (*hdr2), bad); + hdr2 = p; +/* verify seq# */ seq =le64_to_cpu (hdr2->seq);if ((s64 )seq - (s64 )con->in_seq <1 ) {
@@ -2869,6 +2872,10 @@ static int process_message_header(struct ceph_connection *con,WARN_ON (!con->in_msg);WARN_ON (con->in_msg->con != con);return 1 ; + +bad: +pr_err ("failed to decode message header\n" ); +return -EINVAL ; }static int process_message (struct ceph_connection *con)
Signature · 1 line
2.53.0
Quoted 50 lines · Ilya Dryomov
If the message frame is (maliciously) corrupted in a way that the length of the control segment ends up being less than the size of the message header or a different frame is made to look like a message frame, out-of-bounds reads may ensue in
process_message_header().Perform an explicit bounds check before decoding the message header.
Reported-by Raphael Zimmer <[email protected]>Signed-off-by Ilya Dryomov <[email protected]>--- net/ceph/messenger_v2.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)net/ceph/messenger_v2.c +8
-2@@ -2833,12 +2833,15 @@ static int process_message_header(struct ceph_connection *con, void *p, void *end) { struct ceph_frame_desc *desc = &con->v2.in_desc; -struct ceph_msg_header2 *hdr2 = p; +struct ceph_msg_header2 *hdr2;struct ceph_msg_header hdr;int skip;int ret;u64 seq; +ceph_decode_need (&p, end,sizeof (*hdr2), bad); + hdr2 = p; +/* verify seq# */ seq =le64_to_cpu (hdr2->seq);if ((s64 )seq - (s64 )con->in_seq <1 ) {@@ -2869,6 +2872,10 @@ static int process_message_header(struct ceph_connection *con,WARN_ON (!con->in_msg);WARN_ON (con->in_msg->con != con);return 1 ; + +bad: +pr_err ("failed to decode message header\n" ); +return -EINVAL ; }static int process_message (struct ceph_connection *con) --2.53.0
Quoted 36 lines · Ilya Dryomov
If the message frame is (maliciously) corrupted in a way that the length of the control segment ends up being less than the size of the message header or a different frame is made to look like a message frame, out-of-bounds reads may ensue in
process_message_header().Perform an explicit bounds check before decoding the message header.
Reported-by Raphael Zimmer <[email protected]>Signed-off-by Ilya Dryomov <[email protected]>--- net/ceph/messenger_v2.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)net/ceph/messenger_v2.c +4
-1@@ -2833,12 +2833,15 @@ static int process_message_header(struct ceph_connection *con, void *p, void *end) { struct ceph_frame_desc *desc = &con->v2.in_desc; -struct ceph_msg_header2 *hdr2 = p; +struct ceph_msg_header2 *hdr2;struct ceph_msg_header hdr;int skip;int ret;u64 seq; +ceph_decode_need (&p, end,sizeof (*hdr2), bad); + hdr2 = p; +/* verify seq# */ seq =le64_to_cpu (hdr2->seq);if ((s64 )seq - (s64 )con->in_seq <1 ) {
net/ceph/messenger_v2.c static int process_message_header(struct ceph_connection *con, · line 2881
Quoted 10 lines
net/ceph/messenger_v2.c +4
-0@@ -2869,6 +2872,10 @@ static int process_message_header(struct ceph_connection *con,WARN_ON (!con->in_msg);WARN_ON (con->in_msg->con != con);return 1 ; + +bad: +pr_err ("failed to decode message header\n" ); +return -EINVAL ; }static int process_message (struct ceph_connection *con)
Mostly, looks good.
But, I have one question. As far as I can see, process_message_header() is called by __handle_control() [1]. And end pointer is calculated [2]:
void *end = p + con->v2.in_desc.fd_lens[0];
But fd_lens has integer data type [3]:
struct ceph_frame_desc {
int fd_tag; /* FRAME_TAG_* */
int fd_seg_cnt;
int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT]; /* logical */
int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT];
};Is it possible that end pointer will have value is smaller than p pointer value because fd_lens value(s) is/are negative?
Thanks,
Slava.
[1] https://elixir.bootlin.com/linux/v6.19/source/net/ceph/messenger_v2.c#L2901
[2] https://elixir.bootlin.com/linux/v6.19/source/net/ceph/messenger_v2.c#L2894
[3]
https://elixir.bootlin.com/linux/v6.19/source/include/linux/ceph/messenger.h#L386
Ilya Dryomov
maintainerQuoted 67 lines · Viacheslav Dubeyko
Quoted 36 lines · Ilya Dryomov
If the message frame is (maliciously) corrupted in a way that the length of the control segment ends up being less than the size of the message header or a different frame is made to look like a message frame, out-of-bounds reads may ensue in
process_message_header().Perform an explicit bounds check before decoding the message header.
Reported-by Raphael Zimmer <[email protected]>Signed-off-by Ilya Dryomov <[email protected]>--- net/ceph/messenger_v2.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)net/ceph/messenger_v2.c +4
-1@@ -2833,12 +2833,15 @@ static int process_message_header(struct ceph_connection *con, void *p, void *end) { struct ceph_frame_desc *desc = &con->v2.in_desc; -struct ceph_msg_header2 *hdr2 = p; +struct ceph_msg_header2 *hdr2;struct ceph_msg_header hdr;int skip;int ret;u64 seq; +ceph_decode_need (&p, end,sizeof (*hdr2), bad); + hdr2 = p; +/* verify seq# */ seq =le64_to_cpu (hdr2->seq);if ((s64 )seq - (s64 )con->in_seq <1 ) {net/ceph/messenger_v2.c static int process_message_header(struct ceph_connection *con, · line 2881
Quoted 10 lines
net/ceph/messenger_v2.c +4
-0@@ -2869,6 +2872,10 @@ static int process_message_header(struct ceph_connection *con,WARN_ON (!con->in_msg);WARN_ON (con->in_msg->con != con);return 1 ; + +bad: +pr_err ("failed to decode message header\n" ); +return -EINVAL ; }static int process_message (struct ceph_connection *con)Mostly, looks good.
Reviewed-by Viacheslav Dubeyko <[email protected]>But, I have one question. As far as I can see,
process_message_header()is called by__handle_control()[1]. And end pointer is calculated [2]:void *end = p + con->v2.in_desc.fd_lens[0];
But
fd_lenshas integer data type [3]:struct ceph_frame_desc { int fd_tag; /* FRAME_TAG_* */ int fd_seg_cnt; int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT]; /* logical */ int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT]; };
Quoted 2 lines
Is it possible that end pointer will have value is smaller than p pointer value because
fd_lensvalue(s) is/are negative?
Hi Slava,
I don't think so. fd_lens values are checked in decode_preamble() and the fact that it's a signed integer type is accounted for there.
Thanks,
Ilya
Quoted 68 lines · Ilya Dryomov
Quoted 67 lines · Viacheslav Dubeyko
Quoted 36 lines · Ilya Dryomov
If the message frame is (maliciously) corrupted in a way that the length of the control segment ends up being less than the size of the message header or a different frame is made to look like a message frame, out-of-bounds reads may ensue in
process_message_header().Perform an explicit bounds check before decoding the message header.
Reported-by Raphael Zimmer <[email protected]>Signed-off-by Ilya Dryomov <[email protected]>--- net/ceph/messenger_v2.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)net/ceph/messenger_v2.c +4
-1@@ -2833,12 +2833,15 @@ static int process_message_header(struct ceph_connection *con, void *p, void *end) { struct ceph_frame_desc *desc = &con->v2.in_desc; -struct ceph_msg_header2 *hdr2 = p; +struct ceph_msg_header2 *hdr2;struct ceph_msg_header hdr;int skip;int ret;u64 seq; +ceph_decode_need (&p, end,sizeof (*hdr2), bad); + hdr2 = p; +/* verify seq# */ seq =le64_to_cpu (hdr2->seq);if ((s64 )seq - (s64 )con->in_seq <1 ) {net/ceph/messenger_v2.c static int process_message_header(struct ceph_connection *con, · line 2881
Quoted 10 lines
net/ceph/messenger_v2.c +4
-0@@ -2869,6 +2872,10 @@ static int process_message_header(struct ceph_connection *con,WARN_ON (!con->in_msg);WARN_ON (con->in_msg->con != con);return 1 ; + +bad: +pr_err ("failed to decode message header\n" ); +return -EINVAL ; }static int process_message (struct ceph_connection *con)Mostly, looks good.
Reviewed-by Viacheslav Dubeyko <[email protected]>But, I have one question. As far as I can see,
process_message_header()is called by__handle_control()[1]. And end pointer is calculated [2]:void *end = p + con->v2.in_desc.fd_lens[0];
But
fd_lenshas integer data type [3]:struct ceph_frame_desc { int fd_tag; /* FRAME_TAG_* */ int fd_seg_cnt; int fd_lens[CEPH_FRAME_MAX_SEGMENT_COUNT]; /* logical */ int fd_aligns[CEPH_FRAME_MAX_SEGMENT_COUNT]; };
Quoted 8 lines
Quoted 2 lines
Is it possible that end pointer will have value is smaller than p pointer value because
fd_lensvalue(s) is/are negative?Hi Slava,
I don't think so.
fd_lensvalues are checked indecode_preamble()and the fact that it's a signed integer type is accounted for there.
OK. Good to be sure. :) Raphael could like to suggest to change the integer type on unsigned value. :)
Thanks,
Slava.