Skip to contentSkip to conversation

libceph: reject preamble if control segment is empty

PATCH In review Ilya Dryomov 3 participants 6 messages Mar 8 – Mar 10 Ceph common code (libceph)

lore
Share & tools
lore

Ilya Dryomov

author maintainer
#0 lore raw

PATCH 1/1 libceph: reject preamble if control segment is empty

While head_onwire_len() has a branch to handle ctrl_len == 0 case, prepare_read_control() always sets up a kvec for the CRC meaning that a non-empty control segment is effectively assumed. All frames that clients deal with meet that assumption, so let's make it official and treat the preamble with an empty control segment as malformed.

---
 net/ceph/messenger_v2.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
net/ceph/messenger_v2.c +8 -9
index c4ddf7911f7d..50f65820f623 100644
@@ -392,7 +392,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
 	int head_len;
 	int rem_len;
 
-	BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
+	BUG_ON(ctrl_len < 1 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
 
 	if (secure) {
 		head_len = CEPH_PREAMBLE_SECURE_LEN;
@@ -401,9 +401,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
 			head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
 		}
 	} else {
-		head_len = CEPH_PREAMBLE_PLAIN_LEN;
-		if (ctrl_len)
-			head_len += ctrl_len + CEPH_CRC_LEN;
+		head_len = CEPH_PREAMBLE_PLAIN_LEN + ctrl_len + CEPH_CRC_LEN;
 	}
 	return head_len;
 }
@@ -528,11 +526,16 @@ static int decode_preamble(void *p, struct ceph_frame_desc *desc)
 		desc->fd_aligns[i] = ceph_decode_16(&p);
 	}
 
-	if (desc->fd_lens[0] < 0 ||
+	/*
+	 * This would fire for FRAME_TAG_WAIT (it has one empty
+	 * segment), but we should never get it as client.
+	 */
+	if (desc->fd_lens[0] < 1 ||
 	    desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
 		pr_err("bad control segment length %d\n", desc->fd_lens[0]);
 		return -EINVAL;
 	}
+
 	if (desc->fd_lens[1] < 0 ||
 	    desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) {
 		pr_err("bad front segment length %d\n", desc->fd_lens[1]);
@@ -549,10 +552,6 @@ static int decode_preamble(void *p, struct ceph_frame_desc *desc)
 		return -EINVAL;
 	}
 
-	/*
-	 * This would fire for FRAME_TAG_WAIT (it has one empty
-	 * segment), but we should never get it as client.
-	 */
 	if (!desc->fd_lens[desc->fd_seg_cnt - 1]) {
 		pr_err("last segment empty, segment count %d\n",
 		       desc->fd_seg_cnt);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 69 lines · Ilya Dryomov

While head_onwire_len() has a branch to handle ctrl_len == 0 case, prepare_read_control() always sets up a kvec for the CRC meaning that a non-empty control segment is effectively assumed. All frames that clients deal with meet that assumption, so let's make it official and treat the preamble with an empty control segment as malformed.

---
 net/ceph/messenger_v2.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
net/ceph/messenger_v2.c +8 -10
index c4ddf7911f7d..50f65820f623 100644
@@ -392,7 +392,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
        int head_len;
        int rem_len;
 
-       BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
+       BUG_ON(ctrl_len < 1 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
 
        if (secure) {
                head_len = CEPH_PREAMBLE_SECURE_LEN;
@@ -401,9 +401,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
                        head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
                }
        } else {
-               head_len = CEPH_PREAMBLE_PLAIN_LEN;
-               if (ctrl_len)
-                       head_len += ctrl_len + CEPH_CRC_LEN;
+               head_len = CEPH_PREAMBLE_PLAIN_LEN + ctrl_len + CEPH_CRC_LEN;
        }
        return head_len;
 }
@@ -528,11 +526,16 @@ static int decode_preamble(void *p, struct ceph_frame_desc *desc)
                desc->fd_aligns[i] = ceph_decode_16(&p);
        }
 
-       if (desc->fd_lens[0] < 0 ||
+       /*
+        * This would fire for FRAME_TAG_WAIT (it has one empty
+        * segment), but we should never get it as client.
+        */
+       if (desc->fd_lens[0] < 1 ||
            desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
                pr_err("bad control segment length %d\n", desc->fd_lens[0]);
                return -EINVAL;
        }
+
        if (desc->fd_lens[1] < 0 ||
            desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) {
                pr_err("bad front segment length %d\n", desc->fd_lens[1]);
@@ -549,10 +552,6 @@ static int decode_preamble(void *p, struct ceph_frame_desc *desc)
                return -EINVAL;
        }
 
-       /*
-        * This would fire for FRAME_TAG_WAIT (it has one empty
-        * segment), but we should never get it as client.
-        */
        if (!desc->fd_lens[desc->fd_seg_cnt - 1]) {
                pr_err("last segment empty, segment count %d\n",
                       desc->fd_seg_cnt);
--

2.53.0

#2 lore raw
In reply to Ilya Dryomov
Quoted 17 lines · Ilya Dryomov

While head_onwire_len() has a branch to handle ctrl_len == 0 case, prepare_read_control() always sets up a kvec for the CRC meaning that a non-empty control segment is effectively assumed. All frames that clients deal with meet that assumption, so let's make it official and treat the preamble with an empty control segment as malformed.

---
 net/ceph/messenger_v2.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
net/ceph/messenger_v2.c +0 -0
index c4ddf7911f7d..50f65820f623 100644

net/ceph/messenger_v2.c static int head_onwire_len(int ctrl_len, bool secure) · line 395

Quoted 5 lines
net/ceph/messenger_v2.c +1 -1
@@ -392,7 +392,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
 	int head_len;
 	int rem_len;
 
-	BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
+	BUG_ON(ctrl_len < 1 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);

Frankly speaking, I cannot follow from the commit message why 0 has been changed on 1. So, why? :) It will be great to have more clear explanation of this change. And I believe that named constant is always better than hardcoded value if this 1 has a special meaning.

Quoted 15 lines
net/ceph/messenger_v2.c +1 -3
 	if (secure) {
 		head_len = CEPH_PREAMBLE_SECURE_LEN;
@@ -401,9 +401,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
 			head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
 		}
 	} else {
-		head_len = CEPH_PREAMBLE_PLAIN_LEN;
-		if (ctrl_len)
-			head_len += ctrl_len + CEPH_CRC_LEN;
+		head_len = CEPH_PREAMBLE_PLAIN_LEN + ctrl_len + CEPH_CRC_LEN;
 	}
 	return head_len;
 }

net/ceph/messenger_v2.c static int decode_preamble(void *p, struct ceph_frame_desc *desc) · line 533

Quoted 9 lines
net/ceph/messenger_v2.c +5 -1
@@ -528,11 +526,16 @@ static int decode_preamble(void *p, struct ceph_frame_desc *desc)
 		desc->fd_aligns[i] = ceph_decode_16(&p);
 	}
 
-	if (desc->fd_lens[0] < 0 ||
+	/*
+	 * This would fire for FRAME_TAG_WAIT (it has one empty
+	 * segment), but we should never get it as client.
+	 */
+	if (desc->fd_lens[0] < 1 ||

Ditto. Why do we compare with 1 now?

Thanks,
Slava.

Quoted 19 lines
net/ceph/messenger_v2.c +1 -4
 	    desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
 		pr_err("bad control segment length %d\n", desc->fd_lens[0]);
 		return -EINVAL;
 	}
+
 	if (desc->fd_lens[1] < 0 ||
 	    desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) {
 		pr_err("bad front segment length %d\n", desc->fd_lens[1]);
@@ -549,10 +552,6 @@ static int decode_preamble(void *p, struct ceph_frame_desc *desc)
 		return -EINVAL;
 	}
 
-	/*
-	 * This would fire for FRAME_TAG_WAIT (it has one empty
-	 * segment), but we should never get it as client.
-	 */
 	if (!desc->fd_lens[desc->fd_seg_cnt - 1]) {
 		pr_err("last segment empty, segment count %d\n",
 		       desc->fd_seg_cnt);
author maintainer
#3 lore raw
Quoted 22 lines · Viacheslav Dubeyko
Quoted 20 lines · Ilya Dryomov

While head_onwire_len() has a branch to handle ctrl_len == 0 case, prepare_read_control() always sets up a kvec for the CRC meaning that a non-empty control segment is effectively assumed. All frames that clients deal with meet that assumption, so let's make it official and treat the preamble with an empty control segment as malformed.

---
 net/ceph/messenger_v2.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
net/ceph/messenger_v2.c +0 -0
index c4ddf7911f7d..50f65820f623 100644
@@ -392,7 +392,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
      int head_len;
      int rem_len;
 
Quoted 5 lines

net/ceph/messenger_v2.c static int head_onwire_len(int ctrl_len, bool secure) · line 395

Quoted 2 lines
net/ceph/messenger_v2.c +1 -1
-     BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
+     BUG_ON(ctrl_len < 1 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);

Frankly speaking, I cannot follow from the commit message why 0 has been changed on 1. So, why? :) It will be great to have more clear explanation of this

Both branches of prepare_read_control() set up a kvec for the CRC even if ctrl_len == 0:

    add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);

    add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);

This instructs the main loop to read CEPH_CRC_LEN bytes off the wire and essentially translates to an implicit assumption that the control segment isn't empty (i.e. ctrl_len > 0). This assumption holds in real life (i.e. when the client is talking to actual Ceph servers) but not when incoming frames are tampered with. The patch made that assumption explicit -- we won't even admit frames whose control segment is empty anymore.

Quoted 2 lines

change. And I believe that named constant is always better than hardcoded value if this 1 has a special meaning.

It's just a check for emptiness, no special meaning at all. It could be written as <= 0.

Quoted 19 lines
Quoted 19 lines
net/ceph/messenger_v2.c +1 -3
      if (secure) {
              head_len = CEPH_PREAMBLE_SECURE_LEN;
@@ -401,9 +401,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
                      head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
              }
      } else {
-             head_len = CEPH_PREAMBLE_PLAIN_LEN;
-             if (ctrl_len)
-                     head_len += ctrl_len + CEPH_CRC_LEN;
+             head_len = CEPH_PREAMBLE_PLAIN_LEN + ctrl_len + CEPH_CRC_LEN;
      }
      return head_len;
 }
@@ -528,11 +526,16 @@ static int decode_preamble(void *p, struct ceph_frame_desc *desc)
              desc->fd_aligns[i] = ceph_decode_16(&p);
      }
 
Quoted 8 lines

net/ceph/messenger_v2.c static int decode_preamble(void *p, struct ceph_frame_desc *desc) · line 533

Quoted 6 lines
net/ceph/messenger_v2.c +5 -1
-     if (desc->fd_lens[0] < 0 ||
+     /*
+      * This would fire for FRAME_TAG_WAIT (it has one empty
+      * segment), but we should never get it as client.
+      */
+     if (desc->fd_lens[0] < 1 ||

Ditto. Why do we compare with 1 now?

This is where such a frame (or its preamble to be precise) actually gets rejected.

Thanks,

Ilya

#4 lore raw
Quoted 36 lines · Ilya Dryomov
Quoted 22 lines · Viacheslav Dubeyko
Quoted 20 lines · Ilya Dryomov

While head_onwire_len() has a branch to handle ctrl_len == 0 case, prepare_read_control() always sets up a kvec for the CRC meaning that a non-empty control segment is effectively assumed. All frames that clients deal with meet that assumption, so let's make it official and treat the preamble with an empty control segment as malformed.

---
 net/ceph/messenger_v2.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
net/ceph/messenger_v2.c +0 -0
index c4ddf7911f7d..50f65820f623 100644
@@ -392,7 +392,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
      int head_len;
      int rem_len;
 
Quoted 5 lines

net/ceph/messenger_v2.c static int head_onwire_len(int ctrl_len, bool secure) · line 395

Quoted 2 lines
net/ceph/messenger_v2.c +1 -1
-     BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
+     BUG_ON(ctrl_len < 1 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);

Frankly speaking, I cannot follow from the commit message why 0 has been changed on 1. So, why? :) It will be great to have more clear explanation of this

Both branches of prepare_read_control() set up a kvec for the CRC even if ctrl_len == 0:

    add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);

    add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
Quoted 8 lines

This instructs the main loop to read CEPH_CRC_LEN bytes off the wire and essentially translates to an implicit assumption that the control segment isn't empty (i.e. ctrl_len > 0). This assumption holds in real life (i.e. when the client is talking to actual Ceph servers) but not when incoming frames are tampered with. The patch made that assumption explicit -- we won't even admit frames whose control segment is empty anymore.

As far as I can see, we have several calls of head_onwire_len:

net/ceph/messenger_v2.c

[1]        line 1348
[2]        line 1372
[3]        line 1409
[4]        line 1436
[5]        line 1480
[6]        line 1517
[7]        line 1787

The [1-6] cases have ctrl_len always bigger than 1. And only case [7] looks differently:

static int prepare_read_control(struct ceph_connection *con)

{
	int ctrl_len = con->v2.in_desc.fd_lens[0];
	int head_len;
	void *buf;

	reset_in_kvecs(con);
	if (con->state == CEPH_CON_S_V2_HELLO ||
	    con->state == CEPH_CON_S_V2_AUTH) {
		head_len = head_onwire_len(ctrl_len, false);
		buf = alloc_conn_buf(con, head_len);
		if (!buf)
			return -ENOMEM;

		/* preserve preamble */
		memcpy(buf, con->v2.in_buf, CEPH_PREAMBLE_LEN);

		add_in_kvec(con, CTRL_BODY(buf), ctrl_len);
		add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);
		add_in_sign_kvec(con, buf, head_len);
	} else {
		if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
			buf = alloc_conn_buf(con, ctrl_len);
			if (!buf)
				return -ENOMEM;

			add_in_kvec(con, buf, ctrl_len);
		} else {
			add_in_kvec(con, CTRL_BODY(con->v2.in_buf), ctrl_len);
		}
		add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
	}
	con->v2.in_state = IN_S_HANDLE_CONTROL;
	return 0;
}

So, we do reset_in_kvecs(), call head_onwire_len(), and we do add_in_kvec() only after that. And ctrl_len value is defined by con->v2.in_desc.fd_lens[0]. What is the guarantee here that ctrl_len should be bigger than 0? Do we have protection in our logic that prepare_read_control() will be not requested to process the empty control segments? I assume that BUG_ON() implies incorrect logic now.

Thanks,
Slava.

Quoted 1 line
author maintainer
#5 lore raw
Quoted 97 lines · Viacheslav Dubeyko
Quoted 36 lines · Ilya Dryomov
Quoted 22 lines · Viacheslav Dubeyko
Quoted 20 lines · Ilya Dryomov

While head_onwire_len() has a branch to handle ctrl_len == 0 case, prepare_read_control() always sets up a kvec for the CRC meaning that a non-empty control segment is effectively assumed. All frames that clients deal with meet that assumption, so let's make it official and treat the preamble with an empty control segment as malformed.

---
 net/ceph/messenger_v2.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
net/ceph/messenger_v2.c +0 -0
index c4ddf7911f7d..50f65820f623 100644
@@ -392,7 +392,7 @@ static int head_onwire_len(int ctrl_len, bool secure)
      int head_len;
      int rem_len;
 
Quoted 5 lines

net/ceph/messenger_v2.c static int head_onwire_len(int ctrl_len, bool secure) · line 395

Quoted 2 lines
net/ceph/messenger_v2.c +1 -1
-     BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
+     BUG_ON(ctrl_len < 1 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);

Frankly speaking, I cannot follow from the commit message why 0 has been changed on 1. So, why? :) It will be great to have more clear explanation of this

Both branches of prepare_read_control() set up a kvec for the CRC even if ctrl_len == 0:

    add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);

    add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
Quoted 8 lines

This instructs the main loop to read CEPH_CRC_LEN bytes off the wire and essentially translates to an implicit assumption that the control segment isn't empty (i.e. ctrl_len > 0). This assumption holds in real life (i.e. when the client is talking to actual Ceph servers) but not when incoming frames are tampered with. The patch made that assumption explicit -- we won't even admit frames whose control segment is empty anymore.

As far as I can see, we have several calls of head_onwire_len:

net/ceph/messenger_v2.c

[1]        line 1348
[2]        line 1372
[3]        line 1409
[4]        line 1436
[5]        line 1480
[6]        line 1517
[7]        line 1787

The [1-6] cases have ctrl_len always bigger than 1. And only case [7] looks differently:

static int prepare_read_control(struct ceph_connection *con)

{
        int ctrl_len = con->v2.in_desc.fd_lens[0];
        int head_len;
        void *buf;

        reset_in_kvecs(con);
        if (con->state == CEPH_CON_S_V2_HELLO ||
            con->state == CEPH_CON_S_V2_AUTH) {
                head_len = head_onwire_len(ctrl_len, false);
                buf = alloc_conn_buf(con, head_len);
                if (!buf)
                        return -ENOMEM;

                /* preserve preamble */
                memcpy(buf, con->v2.in_buf, CEPH_PREAMBLE_LEN);

                add_in_kvec(con, CTRL_BODY(buf), ctrl_len);
                add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);
                add_in_sign_kvec(con, buf, head_len);
        } else {
                if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
                        buf = alloc_conn_buf(con, ctrl_len);
                        if (!buf)
                                return -ENOMEM;

                        add_in_kvec(con, buf, ctrl_len);
                } else {
                        add_in_kvec(con, CTRL_BODY(con->v2.in_buf), ctrl_len);
                }
                add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
        }
        con->v2.in_state = IN_S_HANDLE_CONTROL;
        return 0;
}
Quoted 3 lines

So, we do reset_in_kvecs(), call head_onwire_len(), and we do add_in_kvec() only after that. And ctrl_len value is defined by con->v2.in_desc.fd_lens[0]. What is the guarantee here that ctrl_len should be bigger than 0? Do we have protection

Hi Slava,

decode_preamble() is where con->v2.in_desc.fd_lens array is populated:

    for (i = 0; i < desc->fd_seg_cnt; i++) {
            desc->fd_lens[i] = ceph_decode_32(&p);
            ...
    }
    ...
    if (desc->fd_lens[0] < 1 ||
        desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
            pr_err("bad control segment length %d\n", desc->fd_lens[0]);
            return -EINVAL;
    }

prepare_read_control() won't be called if decode_preamble() fails. This is what is meant by "reject" in the title of the patch.

Thanks,

Ilya