Skip to contentSkip to conversation

SUNRPC: Fix buffer handling of GSS MIC with less slack

PATCH Superseded Benjamin Coddington 2 participants 10 messages Sep 12, 2019 – Sep 15, 2019

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 SUNRPC: Fix buffer handling of GSS MIC with less slack

The GSS Message Integrity Check data for krb5i may lie partially in the XDR reply buffer's pages and tail. If so, we try to copy the entire MIC into free space in the tail. But as the estimations of the slack space required for authentication and verification have improved there may be less free space in the tail to complete this copy -- see commit 2c94b8eca1a2 ("SUNRPC: Use au_rslack when computing reply buffer size"). In fact, there may only be room in the tail for a single copy of the MIC, and not part of the MIC and then another complete copy.

The real world failure reported is that ls of a directory on NFS may sometimes return -EIO, which can be traced back to xdr_buf_read_netobj() failing to find available free space in the tail to copy the MIC.

Fix this by checking for the case of the MIC crossing the boundaries of head, pages, and tail. If so, shift the buffer until the MIC is contained completely within the pages or tail. This allows the remainder of the function to create a sub buffer that directly address the complete MIC.

---
 net/sunrpc/xdr.c | 45 +++++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 18 deletions(-)
net/sunrpc/xdr.c +27 -18
index 48c93b9e525e..6e05a9693568 100644
@@ -1237,39 +1237,48 @@ xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head or the tail, set object to point to it; otherwise
- * try to find space for it at the end of the tail, copy it there, and
- * set obj to point to it. */
+ * entirely in the head, pages, or tail, set object to point to it; otherwise
+ * shift the buffer until it is contained entirely within the pages or tail.
+ */
 int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
 {
 	struct xdr_buf subbuf;
+	unsigned int len_to_boundary;
 
 	if (xdr_decode_word(buf, offset, &obj->len))
 		return -EFAULT;
-	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
+
+	offset += 4;
+
+	/* Is the obj partially in the head? */
+	len_to_boundary = buf->head->iov_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+		xdr_shift_buf(buf, len_to_boundary);
+
+	/* Is the obj partially in the pages? */
+	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+		xdr_shrink_pagelen(buf, len_to_boundary);
+
+	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
 		return -EFAULT;
 
-	/* Is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
-		return 0;
-	/* ..or is the obj contained entirely in the tail? */
+	/* Most likely: is the obj contained entirely in the tail? */
 	obj->data = subbuf.tail[0].iov_base;
 	if (subbuf.tail[0].iov_len == obj->len)
 		return 0;
 
-	/* use end of tail as storage for obj:
-	 * (We don't copy to the beginning because then we'd have
-	 * to worry about doing a potentially overlapping copy.
-	 * This assumes the object is at most half the length of the
-	 * tail.) */
+	/* ..or is the obj contained entirely in the head? */
+	obj->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == obj->len)
+		return 0;
+
+	/* obj is in the pages: move to tail */
 	if (obj->len > buf->buflen - buf->len)
 		return -ENOMEM;
-	if (buf->tail[0].iov_len != 0)
-		obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
-	else
-		obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
 	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 SUNRPC: Rename xdr_buf_read_netobj to xdr_buf_read_mic

Let the name reflect the single user and purpose.

---
 include/linux/sunrpc/xdr.h     |  2 +-
 net/sunrpc/auth_gss/auth_gss.c |  2 +-
 net/sunrpc/xdr.c               | 42 +++++++++++++++++-----------------
 3 files changed, 23 insertions(+), 23 deletions(-)
include/linux/sunrpc/xdr.h +1 -1
index 9ee3970ba59c..a6b63e47a79b 100644
@@ -179,7 +179,7 @@ xdr_adjust_iovec(struct kvec *iov, __be32 *p)
 extern void xdr_shift_buf(struct xdr_buf *, size_t);
 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
 extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
-extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
+extern int xdr_buf_read_mic(struct xdr_buf *, struct xdr_netobj *, unsigned int);
 extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
 extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
 
net/sunrpc/auth_gss/auth_gss.c +1 -1
index 4ce42c62458e..d75fddca44c9 100644
@@ -1960,7 +1960,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,
 
 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset, integ_len))
 		goto unwrap_failed;
-	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
+	if (xdr_buf_read_mic(rcv_buf, &mic, mic_offset))
 		goto unwrap_failed;
 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
net/sunrpc/xdr.c +21 -21

73 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 2/2] SUNRPC: Rename xdr_buf_read_netobj to xdr_buf_read_mic

Quoted 5 lines · Benjamin Coddington

Let the name reflect the single user and purpose.

And perhaps the assumption that the MIC is always the last item in the xdr_buf? I'm still reviewing 1/2, but this function might make that assumption.

Quoted 38 lines

---

include/linux/sunrpc/xdr.h     |  2 +-
net/sunrpc/auth_gss/auth_gss.c |  2 +-
net/sunrpc/xdr.c               | 42 +++++++++++++++++-----------------
3 files changed, 23 insertions(+), 23 deletions(-)
include/linux/sunrpc/xdr.h +1 -1
index 9ee3970ba59c..a6b63e47a79b 100644
@@ -179,7 +179,7 @@ xdr_adjust_iovec(struct kvec *iov, __be32 *p)
 extern void xdr_shift_buf(struct xdr_buf *, size_t);
 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
 extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
-extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
+extern int xdr_buf_read_mic(struct xdr_buf *, struct xdr_netobj *, unsigned int);
 extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
 extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
 
net/sunrpc/auth_gss/auth_gss.c +1 -1
index 4ce42c62458e..d75fddca44c9 100644
@@ -1960,7 +1960,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,
 
 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset, integ_len))
 		goto unwrap_failed;
-	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
+	if (xdr_buf_read_mic(rcv_buf, &mic, mic_offset))
 		goto unwrap_failed;
 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
net/sunrpc/xdr.c +0 -0
index 6e05a9693568..90dfde50f0ef 100644

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1242

Quoted 10 lines
net/sunrpc/xdr.c +3 -3
@@ -1236,52 +1236,52 @@ xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
 }
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
-/* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head, pages, or tail, set object to point to it; otherwise
- * shift the buffer until it is contained entirely within the pages or tail.
+/* If the mic starting offset bytes from the start of xdr_buf is contained
+ * entirely in the head, pages, or tail, set mic to point to it; otherwise
+ * shift the buf until it is contained entirely within the pages or tail.
 */

Nit: Could the patch convert this into a kernel Doxygen style comment?

Quoted 62 lines
net/sunrpc/xdr.c +18 -18
-int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
+int xdr_buf_read_mic(struct xdr_buf *buf, struct xdr_netobj *mic, unsigned int offset)
 {
 	struct xdr_buf subbuf;
 	unsigned int len_to_boundary;
 
-	if (xdr_decode_word(buf, offset, &obj->len))
+	if (xdr_decode_word(buf, offset, &mic->len))
 		return -EFAULT;
 
 	offset += 4;
 
-	/* Is the obj partially in the head? */
+	/* Is the mic partially in the head? */
 	len_to_boundary = buf->head->iov_len - offset;
-	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+	if (len_to_boundary > 0 && len_to_boundary < mic->len)
 		xdr_shift_buf(buf, len_to_boundary);
 
-	/* Is the obj partially in the pages? */
+	/* Is the mic partially in the pages? */
 	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
-	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+	if (len_to_boundary > 0 && len_to_boundary < mic->len)
 		xdr_shrink_pagelen(buf, len_to_boundary);
 
-	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
+	if (xdr_buf_subsegment(buf, &subbuf, offset, mic->len))
 		return -EFAULT;
 
-	/* Most likely: is the obj contained entirely in the tail? */
-	obj->data = subbuf.tail[0].iov_base;
-	if (subbuf.tail[0].iov_len == obj->len)
+	/* Most likely: is the mic contained entirely in the tail? */
+	mic->data = subbuf.tail[0].iov_base;
+	if (subbuf.tail[0].iov_len == mic->len)
 		return 0;
 
-	/* ..or is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
+	/* ..or is the mic contained entirely in the head? */
+	mic->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == mic->len)
 		return 0;
 
-	/* obj is in the pages: move to tail */
-	if (obj->len > buf->buflen - buf->len)
+	/* mic is in the pages: move to tail */
+	if (mic->len > buf->buflen - buf->len)
 		return -ENOMEM;
-	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
-	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
+	mic->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	__read_bytes_from_xdr_buf(&subbuf, mic->data, mic->len);
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
+EXPORT_SYMBOL_GPL(xdr_buf_read_mic);
 
 /* Returns 0 on success, or else a negative error code. */
Quoted 4 lines

static int

Signature · 1 line
2.20.1

--
Chuck Lever

#3 lore raw

Re: [PATCH 2/2] SUNRPC: Rename xdr_buf_read_netobj to xdr_buf_read_mic

Quoted 6 lines · Chuck Lever
Quoted 3 lines · Benjamin Coddington

Let the name reflect the single user and purpose.

And perhaps the assumption that the MIC is always the last item in the xdr_buf? I'm still reviewing 1/2, but this function might make that assumption.

It does.. is that a bad assumption, or maybe you'd like a different name?

Quoted 46 lines
Quoted 46 lines

---

include/linux/sunrpc/xdr.h     |  2 +-
net/sunrpc/auth_gss/auth_gss.c |  2 +-
net/sunrpc/xdr.c               | 42 
+++++++++++++++++-----------------
3 files changed, 23 insertions(+), 23 deletions(-)
include/linux/sunrpc/xdr.h +1 -1
index 9ee3970ba59c..a6b63e47a79b 100644
@@ -179,7 +179,7 @@ xdr_adjust_iovec(struct kvec *iov, __be32 *p)
 extern void xdr_shift_buf(struct xdr_buf *, size_t);
 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
 extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
-extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj 
 *, unsigned int);
+extern int xdr_buf_read_mic(struct xdr_buf *, struct xdr_netobj *, 
 unsigned int);
 extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);

extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);

net/sunrpc/auth_gss/auth_gss.c +1 -1
index 4ce42c62458e..d75fddca44c9 100644
@@ -1960,7 +1960,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, 
 struct rpc_cred *cred,
 
 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset, integ_len))
 		goto unwrap_failed;
-	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
+	if (xdr_buf_read_mic(rcv_buf, &mic, mic_offset))
 		goto unwrap_failed;
 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
net/sunrpc/xdr.c +0 -0
index 6e05a9693568..90dfde50f0ef 100644
@@ -1236,52 +1236,52 @@ xdr_encode_word(struct xdr_buf *buf, unsigned 
 int base, u32 obj)
 }
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
Quoted 15 lines
Quoted 5 lines
net/sunrpc/xdr.c +0 -1
-/* If the netobj starting offset bytes from the start of xdr_buf is 

contained
- * entirely in the head, pages, or tail, set object to point to it; otherwise
- * shift the buffer until it is contained entirely within the pages

Quoted 8 lines

or tail.
+/* If the mic starting offset bytes from the start of xdr_buf is contained
+ * entirely in the head, pages, or tail, set mic to point to it; otherwise
+ * shift the buf until it is contained entirely within the pages or tail.
*/

Nit: Could the patch convert this into a kernel Doxygen style comment?

Yes, can do that.

Ben

Quoted 71 lines
Quoted 63 lines

-int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
+int xdr_buf_read_mic(struct xdr_buf *buf, struct xdr_netobj *mic, unsigned int offset)

{
	struct xdr_buf subbuf;
	unsigned int len_to_boundary;
net/sunrpc/xdr.c +1 -1
-	if (xdr_decode_word(buf, offset, &obj->len))
+	if (xdr_decode_word(buf, offset, &mic->len))
		return -EFAULT;

	offset += 4;
net/sunrpc/xdr.c +1 -1
-	/* Is the obj partially in the head? */
+	/* Is the mic partially in the head? */
	len_to_boundary = buf->head->iov_len - offset;
net/sunrpc/xdr.c +1 -1
-	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+	if (len_to_boundary > 0 && len_to_boundary < mic->len)
		xdr_shift_buf(buf, len_to_boundary);
net/sunrpc/xdr.c +1 -1
-	/* Is the obj partially in the pages? */
+	/* Is the mic partially in the pages? */
	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
net/sunrpc/xdr.c +1 -1
-	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+	if (len_to_boundary > 0 && len_to_boundary < mic->len)
		xdr_shrink_pagelen(buf, len_to_boundary);
net/sunrpc/xdr.c +1 -1
-	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
+	if (xdr_buf_subsegment(buf, &subbuf, offset, mic->len))
		return -EFAULT;
net/sunrpc/xdr.c +3 -3
-	/* Most likely: is the obj contained entirely in the tail? */
-	obj->data = subbuf.tail[0].iov_base;
-	if (subbuf.tail[0].iov_len == obj->len)
+	/* Most likely: is the mic contained entirely in the tail? */
+	mic->data = subbuf.tail[0].iov_base;
+	if (subbuf.tail[0].iov_len == mic->len)
		return 0;
net/sunrpc/xdr.c +3 -3
-	/* ..or is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
+	/* ..or is the mic contained entirely in the head? */
+	mic->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == mic->len)
		return 0;
net/sunrpc/xdr.c +2 -2
-	/* obj is in the pages: move to tail */
-	if (obj->len > buf->buflen - buf->len)
+	/* mic is in the pages: move to tail */
+	if (mic->len > buf->buflen - buf->len)
		return -ENOMEM;
net/sunrpc/xdr.c +2 -2
-	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
-	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
+	mic->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	__read_bytes_from_xdr_buf(&subbuf, mic->data, mic->len);
	return 0;
}
net/sunrpc/xdr.c +1 -1
-EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
+EXPORT_SYMBOL_GPL(xdr_buf_read_mic);
Quoted 5 lines

/* Returns 0 on success, or else a negative error code. */ static int

Signature · 1 line
2.20.1

--
Chuck Lever

#4 lore raw

Re: [PATCH 2/2] SUNRPC: Rename xdr_buf_read_netobj to xdr_buf_read_mic

Quoted 14 lines · Benjamin Coddington
Quoted 6 lines · Chuck Lever
Quoted 3 lines · Benjamin Coddington

Let the name reflect the single user and purpose.

And perhaps the assumption that the MIC is always the last item in the xdr_buf? I'm still reviewing 1/2, but this function might make that assumption.

It does.. is that a bad assumption, or maybe you'd like a different name?

Just documenting it is enough for now; there is only one user.

In the long run I'd like to see this kind of xdr_buf manipulation replaced with the use of a scratch buffer.

Quoted 51 lines
Quoted 41 lines
Quoted 41 lines

---

include/linux/sunrpc/xdr.h     |  2 +-
net/sunrpc/auth_gss/auth_gss.c |  2 +-
net/sunrpc/xdr.c               | 42 +++++++++++++++++-----------------
3 files changed, 23 insertions(+), 23 deletions(-)
include/linux/sunrpc/xdr.h +1 -1
index 9ee3970ba59c..a6b63e47a79b 100644
@@ -179,7 +179,7 @@ xdr_adjust_iovec(struct kvec *iov, __be32 *p)
 extern void xdr_shift_buf(struct xdr_buf *, size_t);
 extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
 extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
-extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
+extern int xdr_buf_read_mic(struct xdr_buf *, struct xdr_netobj *, unsigned int);
 extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
 extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
 
net/sunrpc/auth_gss/auth_gss.c +1 -1
index 4ce42c62458e..d75fddca44c9 100644
@@ -1960,7 +1960,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,
 
 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset, integ_len))
 		goto unwrap_failed;
-	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
+	if (xdr_buf_read_mic(rcv_buf, &mic, mic_offset))
 		goto unwrap_failed;
 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
net/sunrpc/xdr.c +0 -0
index 6e05a9693568..90dfde50f0ef 100644
@@ -1236,52 +1236,52 @@ xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
 }
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
Quoted 9 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1242

Quoted 7 lines
net/sunrpc/xdr.c +3 -3
-/* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head, pages, or tail, set object to point to it; otherwise
- * shift the buffer until it is contained entirely within the pages or tail.
+/* If the mic starting offset bytes from the start of xdr_buf is contained
+ * entirely in the head, pages, or tail, set mic to point to it; otherwise
+ * shift the buf until it is contained entirely within the pages or tail.
 */

Nit: Could the patch convert this into a kernel Doxygen style comment?

Quoted 73 lines

Yes, can do that.

Ben

Quoted 69 lines
Quoted 62 lines
net/sunrpc/xdr.c +18 -18
-int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
+int xdr_buf_read_mic(struct xdr_buf *buf, struct xdr_netobj *mic, unsigned int offset)
 {
 	struct xdr_buf subbuf;
 	unsigned int len_to_boundary;
 
-	if (xdr_decode_word(buf, offset, &obj->len))
+	if (xdr_decode_word(buf, offset, &mic->len))
 		return -EFAULT;
 
 	offset += 4;
 
-	/* Is the obj partially in the head? */
+	/* Is the mic partially in the head? */
 	len_to_boundary = buf->head->iov_len - offset;
-	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+	if (len_to_boundary > 0 && len_to_boundary < mic->len)
 		xdr_shift_buf(buf, len_to_boundary);
 
-	/* Is the obj partially in the pages? */
+	/* Is the mic partially in the pages? */
 	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
-	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+	if (len_to_boundary > 0 && len_to_boundary < mic->len)
 		xdr_shrink_pagelen(buf, len_to_boundary);
 
-	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
+	if (xdr_buf_subsegment(buf, &subbuf, offset, mic->len))
 		return -EFAULT;
 
-	/* Most likely: is the obj contained entirely in the tail? */
-	obj->data = subbuf.tail[0].iov_base;
-	if (subbuf.tail[0].iov_len == obj->len)
+	/* Most likely: is the mic contained entirely in the tail? */
+	mic->data = subbuf.tail[0].iov_base;
+	if (subbuf.tail[0].iov_len == mic->len)
 		return 0;
 
-	/* ..or is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
+	/* ..or is the mic contained entirely in the head? */
+	mic->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == mic->len)
 		return 0;
 
-	/* obj is in the pages: move to tail */
-	if (obj->len > buf->buflen - buf->len)
+	/* mic is in the pages: move to tail */
+	if (mic->len > buf->buflen - buf->len)
 		return -ENOMEM;
-	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
-	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
+	mic->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	__read_bytes_from_xdr_buf(&subbuf, mic->data, mic->len);
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
+EXPORT_SYMBOL_GPL(xdr_buf_read_mic);
 
 /* Returns 0 on success, or else a negative error code. */
Quoted 4 lines

static int

Signature · 1 line
2.20.1

--
Chuck Lever

--
Chuck Lever

#5 lore raw
In reply to Benjamin Coddington
Quoted 17 lines · Benjamin Coddington

The GSS Message Integrity Check data for krb5i may lie partially in the XDR reply buffer's pages and tail. If so, we try to copy the entire MIC into free space in the tail. But as the estimations of the slack space required for authentication and verification have improved there may be less free space in the tail to complete this copy -- see commit 2c94b8eca1a2 ("SUNRPC: Use au_rslack when computing reply buffer size"). In fact, there may only be room in the tail for a single copy of the MIC, and not part of the MIC and then another complete copy.

The real world failure reported is that ls of a directory on NFS may sometimes return -EIO, which can be traced back to xdr_buf_read_netobj() failing to find available free space in the tail to copy the MIC.

Quoted 7 lines

Fix this by checking for the case of the MIC crossing the boundaries of head, pages, and tail. If so, shift the buffer until the MIC is contained completely within the pages or tail. This allows the remainder of the function to create a sub buffer that directly address the complete MIC.

# v5.1 ?
Quoted 28 lines

---
net/sunrpc/xdr.c | 45 +++++++++++++++++++++++++++------------------

1 file changed, 27 insertions(+), 18 deletions(-)
net/sunrpc/xdr.c +6 -4
index 48c93b9e525e..6e05a9693568 100644
@@ -1237,39 +1237,48 @@ xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head or the tail, set object to point to it; otherwise
- * try to find space for it at the end of the tail, copy it there, and
- * set obj to point to it. */
+ * entirely in the head, pages, or tail, set object to point to it; otherwise
+ * shift the buffer until it is contained entirely within the pages or tail.
+ */
 int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
 {
 	struct xdr_buf subbuf;
+	unsigned int len_to_boundary;
 
 	if (xdr_decode_word(buf, offset, &obj->len))
 		return -EFAULT;
-	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
+
+	offset += 4;

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1261

Quoted 10 lines
net/sunrpc/xdr.c +10 -0
+
+	/* Is the obj partially in the head? */
+	len_to_boundary = buf->head->iov_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+		xdr_shift_buf(buf, len_to_boundary);
+
+	/* Is the obj partially in the pages? */
+	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+		xdr_shrink_pagelen(buf, len_to_boundary);

Do you need to check if the obj is entirely in ->pages but crosses a page boundary?

Quoted 40 lines
net/sunrpc/xdr.c +11 -14
+
+	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
 		return -EFAULT;
 
-	/* Is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
-		return 0;
-	/* ..or is the obj contained entirely in the tail? */
+	/* Most likely: is the obj contained entirely in the tail? */
 	obj->data = subbuf.tail[0].iov_base;
 	if (subbuf.tail[0].iov_len == obj->len)
 		return 0;
 
-	/* use end of tail as storage for obj:
-	 * (We don't copy to the beginning because then we'd have
-	 * to worry about doing a potentially overlapping copy.
-	 * This assumes the object is at most half the length of the
-	 * tail.) */
+	/* ..or is the obj contained entirely in the head? */
+	obj->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == obj->len)
+		return 0;
+
+	/* obj is in the pages: move to tail */
 	if (obj->len > buf->buflen - buf->len)
 		return -ENOMEM;
-	if (buf->tail[0].iov_len != 0)
-		obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
-	else
-		obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
 	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
Signature · 1 line
2.20.1

--
Chuck Lever

#6 lore raw
Quoted 20 lines · Chuck Lever
Quoted 20 lines · Benjamin Coddington

The GSS Message Integrity Check data for krb5i may lie partially in the XDR reply buffer's pages and tail. If so, we try to copy the entire MIC into free space in the tail. But as the estimations of the slack space required for authentication and verification have improved there may be less free space in the tail to complete this copy -- see commit 2c94b8eca1a2 ("SUNRPC: Use au_rslack when computing reply buffer size"). In fact, there may only be room in the tail for a single copy of the MIC, and not part of the MIC and then another complete copy.

The real world failure reported is that ls of a directory on NFS may sometimes return -EIO, which can be traced back to xdr_buf_read_netobj() failing to find available free space in the tail to copy the MIC.

Fix this by checking for the case of the MIC crossing the boundaries of head, pages, and tail. If so, shift the buffer until the MIC is contained completely within the pages or tail. This allows the remainder of the function to create a sub buffer that directly address the complete MIC.

Quoted 4 lines
Quoted 2 lines
# v5.1 ?

That makes sense to match the changes to rslack.

Quoted 37 lines
Quoted 37 lines

---
net/sunrpc/xdr.c | 45 +++++++++++++++++++++++++++------------------

1 file changed, 27 insertions(+), 18 deletions(-)
net/sunrpc/xdr.c +0 -1
index 48c93b9e525e..6e05a9693568 100644
@@ -1237,39 +1237,48 @@ xdr_encode_word(struct xdr_buf *buf, unsigned 
 int base, u32 obj)
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head or the tail, set object to point to it; 

otherwise
- * try to find space for it at the end of the tail, copy it there,
and
- * set obj to point to it. */
+ * entirely in the head, pages, or tail, set object to point to it; otherwise
+ * shift the buffer until it is contained entirely within the pages
or tail.
+ */
int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)

{
	struct xdr_buf subbuf;
net/sunrpc/xdr.c +1 -0
+	unsigned int len_to_boundary;
	if (xdr_decode_word(buf, offset, &obj->len))
		return -EFAULT;
net/sunrpc/xdr.c +6 -1
-	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
+
+	offset += 4;
+
+	/* Is the obj partially in the head? */
+	len_to_boundary = buf->head->iov_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)
Quoted 8 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1261

Quoted 6 lines
net/sunrpc/xdr.c +6 -0
+		xdr_shift_buf(buf, len_to_boundary);
+
+	/* Is the obj partially in the pages? */
+	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)
+		xdr_shrink_pagelen(buf, len_to_boundary);

Do you need to check if the obj is entirely in ->pages but crosses a page boundary?

We're going to copy it out into the tail in that case. I'm assuming read_bytes_from_xdr_buf() can handle reading across page boundaries.

So unless I'm missing something, I don't think we need to check.

Ben

#7 lore raw
In reply to Benjamin Coddington

Hi Ben-

A few review comments below.

Quoted 31 lines · Benjamin Coddington

The GSS Message Integrity Check data for krb5i may lie partially in the XDR reply buffer's pages and tail. If so, we try to copy the entire MIC into free space in the tail. But as the estimations of the slack space required for authentication and verification have improved there may be less free space in the tail to complete this copy -- see commit 2c94b8eca1a2 ("SUNRPC: Use au_rslack when computing reply buffer size"). In fact, there may only be room in the tail for a single copy of the MIC, and not part of the MIC and then another complete copy.

The real world failure reported is that ls of a directory on NFS may sometimes return -EIO, which can be traced back to xdr_buf_read_netobj() failing to find available free space in the tail to copy the MIC.

Fix this by checking for the case of the MIC crossing the boundaries of head, pages, and tail. If so, shift the buffer until the MIC is contained completely within the pages or tail. This allows the remainder of the function to create a sub buffer that directly address the complete MIC.

---
net/sunrpc/xdr.c | 45 +++++++++++++++++++++++++++------------------

1 file changed, 27 insertions(+), 18 deletions(-)
net/sunrpc/xdr.c +0 -0
index 48c93b9e525e..6e05a9693568 100644

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1242

Quoted 9 lines
net/sunrpc/xdr.c +3 -3
@@ -1237,39 +1237,48 @@ xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head or the tail, set object to point to it; otherwise
- * try to find space for it at the end of the tail, copy it there, and
- * set obj to point to it. */
+ * entirely in the head, pages, or tail, set object to point to it; otherwise
+ * shift the buffer until it is contained entirely within the pages or tail.
+ */

Nit: I would explicitly note in this comment that there is no need for the caller to free @obj, and perhaps it should be noted that the organization of @buf can be changed by this function.

Maybe more appropriate for 2/2.

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1251

Quoted 10 lines
net/sunrpc/xdr.c +3 -1
 int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
 {
 	struct xdr_buf subbuf;
+	unsigned int len_to_boundary;
 
 	if (xdr_decode_word(buf, offset, &obj->len))
 		return -EFAULT;
-	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
+
+	offset += 4;

Nit: No blank line before "offset += 4;" would help me understand how the offset bump is related to xdr_decode_word(). It took me a few blinks to see.

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1255

Quoted 4 lines
net/sunrpc/xdr.c +4 -0
+
+	/* Is the obj partially in the head? */
+	len_to_boundary = buf->head->iov_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)

I'm not especially excited about the integer underflow when offset is larger than buf->head->iov_len. This might be more explicit:

        if (offset < buf->head[0].iov_len &&
            offset + obj->len > buf->head[0].iov_len)

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1260

Quoted 5 lines
net/sunrpc/xdr.c +5 -0
+		xdr_shift_buf(buf, len_to_boundary);
+
+	/* Is the obj partially in the pages? */
+	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)

Ditto.

Quoted 14 lines
net/sunrpc/xdr.c +4 -5
+		xdr_shrink_pagelen(buf, len_to_boundary);
+
+	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
 		return -EFAULT;
 
-	/* Is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
-		return 0;
-	/* ..or is the obj contained entirely in the tail? */
+	/* Most likely: is the obj contained entirely in the tail? */
 	obj->data = subbuf.tail[0].iov_base;
 	if (subbuf.tail[0].iov_len == obj->len)
 		return 0;

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1274

Quoted 10 lines
net/sunrpc/xdr.c +4 -5
 
-	/* use end of tail as storage for obj:
-	 * (We don't copy to the beginning because then we'd have
-	 * to worry about doing a potentially overlapping copy.
-	 * This assumes the object is at most half the length of the
-	 * tail.) */
+	/* ..or is the obj contained entirely in the head? */
+	obj->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == obj->len)
+		return 0;

It looks like you're reversing these two tests as a micro-optimization? Maybe that should be left for another patch, since this is supposed to be a narrow fix.

Also, I found the new comments confusing: here they refer to the head and tail of @subbuf; above they refer to head and tail of @buf. Note for 2/2, I guess.

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1279

Quoted 9 lines
net/sunrpc/xdr.c +3 -4
+
+	/* obj is in the pages: move to tail */
 	if (obj->len > buf->buflen - buf->len)
 		return -ENOMEM;
-	if (buf->tail[0].iov_len != 0)
-		obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
-	else
-		obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;

Not sure this is a safe change. It's possible that the head buffer and tail buffer are not contiguous, which is what the buf->tail.iov_len check is looking for, IMO. Can this hunk be left out?

Quoted 8 lines
net/sunrpc/xdr.c +1 -0
 	__read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
Signature · 1 line
2.20.1

--
Chuck Lever

#8 lore raw
Quoted 55 lines · Chuck Lever

Hi Ben-

A few review comments below.

Quoted 37 lines · Benjamin Coddington

The GSS Message Integrity Check data for krb5i may lie partially in the XDR reply buffer's pages and tail. If so, we try to copy the entire MIC into free space in the tail. But as the estimations of the slack space required for authentication and verification have improved there may be less free space in the tail to complete this copy -- see commit 2c94b8eca1a2 ("SUNRPC: Use au_rslack when computing reply buffer size"). In fact, there may only be room in the tail for a single copy of the MIC, and not part of the MIC and then another complete copy.

The real world failure reported is that ls of a directory on NFS may sometimes return -EIO, which can be traced back to xdr_buf_read_netobj() failing to find available free space in the tail to copy the MIC.

Fix this by checking for the case of the MIC crossing the boundaries of head, pages, and tail. If so, shift the buffer until the MIC is contained completely within the pages or tail. This allows the remainder of the function to create a sub buffer that directly address the complete MIC.

---
net/sunrpc/xdr.c | 45 +++++++++++++++++++++++++++------------------

1 file changed, 27 insertions(+), 18 deletions(-)
net/sunrpc/xdr.c +0 -1
index 48c93b9e525e..6e05a9693568 100644
@@ -1237,39 +1237,48 @@ xdr_encode_word(struct xdr_buf *buf, unsigned 
 int base, u32 obj)
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head or the tail, set object to point to it; 

otherwise

Quoted 8 lines

- * try to find space for it at the end of the tail, copy it there, and
- * set obj to point to it. */
+ * entirely in the head, pages, or tail, set object to point to it; otherwise
+ * shift the buffer until it is contained entirely within the pages or tail.
+ */

Nit: I would explicitly note in this comment that there is no need for the caller to free @obj, and perhaps it should be noted that the organization of @buf can be changed by this function.

Quoted 1 line

Maybe more appropriate for 2/2.

Ok.. yes.. though I don't feel strongly about noting that *obj doesn't need to be freed.

Quoted 24 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1251

Quoted 10 lines

int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)

{
	struct xdr_buf subbuf;
net/sunrpc/xdr.c +1 -0
+	unsigned int len_to_boundary;
	if (xdr_decode_word(buf, offset, &obj->len))
		return -EFAULT;
net/sunrpc/xdr.c +2 -1
-	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
+
+	offset += 4;

Nit: No blank line before "offset += 4;" would help me understand how the offset bump is related to xdr_decode_word(). It took me a few blinks to see.

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1255

Quoted 4 lines
net/sunrpc/xdr.c +4 -0
+
+	/* Is the obj partially in the head? */
+	len_to_boundary = buf->head->iov_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)

I'm not especially excited about the integer underflow when offset is larger than buf->head->iov_len. This might be more explicit:

Quoted 2 lines
        if (offset < buf->head[0].iov_len &&
            offset + obj->len > buf->head[0].iov_len)

Yep, makes sense - and I prefer the clarity.

Quoted 24 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1260

Quoted 5 lines
net/sunrpc/xdr.c +5 -0
+		xdr_shift_buf(buf, len_to_boundary);
+
+	/* Is the obj partially in the pages? */
+	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)

Ditto.

Quoted 15 lines
net/sunrpc/xdr.c +3 -0
+		xdr_shrink_pagelen(buf, len_to_boundary);
+
+	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
		return -EFAULT;
net/sunrpc/xdr.c +1 -5
-	/* Is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
-		return 0;
-	/* ..or is the obj contained entirely in the tail? */
+	/* Most likely: is the obj contained entirely in the tail? */
	obj->data = subbuf.tail[0].iov_base;
	if (subbuf.tail[0].iov_len == obj->len)
		return 0;
Quoted 13 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1274

Quoted 9 lines
net/sunrpc/xdr.c +4 -5
-	/* use end of tail as storage for obj:
-	 * (We don't copy to the beginning because then we'd have
-	 * to worry about doing a potentially overlapping copy.
-	 * This assumes the object is at most half the length of the
-	 * tail.) */
+	/* ..or is the obj contained entirely in the head? */
+	obj->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == obj->len)
+		return 0;

It looks like you're reversing these two tests as a micro-optimization? Maybe that should be left for another patch, since this is supposed to be a narrow fix.

Yes, if not done here - is it even worth another patch?

Quoted 3 lines

Also, I found the new comments confusing: here they refer to the head and tail of @subbuf; above they refer to head and tail of @buf. Note for 2/2, I guess.

I can clarify in 2/2.

Quoted 5 lines
Quoted 5 lines
net/sunrpc/xdr.c +2 -0
+
+	/* obj is in the pages: move to tail */
	if (obj->len > buf->buflen - buf->len)
		return -ENOMEM;
net/sunrpc/xdr.c +0 -1
-	if (buf->tail[0].iov_len != 0)
Quoted 8 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1279

Quoted 4 lines
net/sunrpc/xdr.c +1 -3
-		obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
-	else
-		obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;

Not sure this is a safe change. It's possible that the head buffer and tail buffer are not contiguous, which is what the buf->tail.iov_len check is looking for, IMO. Can this hunk be left out?

That's something I missed somehow, thanks for pointing it out. I see now that the transport can allocate them any way it likes.

Thanks for the review, I'll send a v2.

Ben

#9 lore raw
Quoted 152 lines · Benjamin Coddington
Quoted 55 lines · Chuck Lever

Hi Ben-

A few review comments below.

Quoted 37 lines · Benjamin Coddington

The GSS Message Integrity Check data for krb5i may lie partially in the XDR reply buffer's pages and tail. If so, we try to copy the entire MIC into free space in the tail. But as the estimations of the slack space required for authentication and verification have improved there may be less free space in the tail to complete this copy -- see commit 2c94b8eca1a2 ("SUNRPC: Use au_rslack when computing reply buffer size"). In fact, there may only be room in the tail for a single copy of the MIC, and not part of the MIC and then another complete copy.

The real world failure reported is that ls of a directory on NFS may sometimes return -EIO, which can be traced back to xdr_buf_read_netobj() failing to find available free space in the tail to copy the MIC.

Fix this by checking for the case of the MIC crossing the boundaries of head, pages, and tail. If so, shift the buffer until the MIC is contained completely within the pages or tail. This allows the remainder of the function to create a sub buffer that directly address the complete MIC.

---
net/sunrpc/xdr.c | 45 +++++++++++++++++++++++++++------------------

1 file changed, 27 insertions(+), 18 deletions(-)
net/sunrpc/xdr.c +0 -1
index 48c93b9e525e..6e05a9693568 100644
@@ -1237,39 +1237,48 @@ xdr_encode_word(struct xdr_buf *buf, 
 unsigned int base, u32 obj)
 EXPORT_SYMBOL_GPL(xdr_encode_word);
 
 /* If the netobj starting offset bytes from the start of xdr_buf is contained
- * entirely in the head or the tail, set object to point to it; 

otherwise

Quoted 8 lines

- * try to find space for it at the end of the tail, copy it there, and
- * set obj to point to it. */
+ * entirely in the head, pages, or tail, set object to point to it; otherwise
+ * shift the buffer until it is contained entirely within the pages or tail.
+ */

Nit: I would explicitly note in this comment that there is no need for the caller to free @obj, and perhaps it should be noted that the organization of @buf can be changed by this function.

Quoted 1 line

Maybe more appropriate for 2/2.

Ok.. yes.. though I don't feel strongly about noting that *obj doesn't need to be freed.

Quoted 24 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1251

Quoted 10 lines

int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)

{
	struct xdr_buf subbuf;
net/sunrpc/xdr.c +1 -0
+	unsigned int len_to_boundary;
	if (xdr_decode_word(buf, offset, &obj->len))
		return -EFAULT;
net/sunrpc/xdr.c +2 -1
-	if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
+
+	offset += 4;

Nit: No blank line before "offset += 4;" would help me understand how the offset bump is related to xdr_decode_word(). It took me a few blinks to see.

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1255

Quoted 4 lines
net/sunrpc/xdr.c +4 -0
+
+	/* Is the obj partially in the head? */
+	len_to_boundary = buf->head->iov_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)

I'm not especially excited about the integer underflow when offset is larger than buf->head->iov_len. This might be more explicit:

Quoted 2 lines
        if (offset < buf->head[0].iov_len &&
            offset + obj->len > buf->head[0].iov_len)

Yep, makes sense - and I prefer the clarity.

Quoted 24 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1260

Quoted 5 lines
net/sunrpc/xdr.c +5 -0
+		xdr_shift_buf(buf, len_to_boundary);
+
+	/* Is the obj partially in the pages? */
+	len_to_boundary = buf->head->iov_len + buf->page_len - offset;
+	if (len_to_boundary > 0 && len_to_boundary < obj->len)

Ditto.

Quoted 15 lines
net/sunrpc/xdr.c +3 -0
+		xdr_shrink_pagelen(buf, len_to_boundary);
+
+	if (xdr_buf_subsegment(buf, &subbuf, offset, obj->len))
		return -EFAULT;
net/sunrpc/xdr.c +1 -5
-	/* Is the obj contained entirely in the head? */
-	obj->data = subbuf.head[0].iov_base;
-	if (subbuf.head[0].iov_len == obj->len)
-		return 0;
-	/* ..or is the obj contained entirely in the tail? */
+	/* Most likely: is the obj contained entirely in the tail? */
	obj->data = subbuf.tail[0].iov_base;
	if (subbuf.tail[0].iov_len == obj->len)
		return 0;
Quoted 13 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1274

Quoted 9 lines
net/sunrpc/xdr.c +4 -5
-	/* use end of tail as storage for obj:
-	 * (We don't copy to the beginning because then we'd have
-	 * to worry about doing a potentially overlapping copy.
-	 * This assumes the object is at most half the length of the
-	 * tail.) */
+	/* ..or is the obj contained entirely in the head? */
+	obj->data = subbuf.head[0].iov_base;
+	if (subbuf.head[0].iov_len == obj->len)
+		return 0;

It looks like you're reversing these two tests as a micro-optimization? Maybe that should be left for another patch, since this is supposed to be a narrow fix.

Yes, if not done here - is it even worth another patch?

Quoted 3 lines

Also, I found the new comments confusing: here they refer to the head and tail of @subbuf; above they refer to head and tail of @buf. Note for 2/2, I guess.

I can clarify in 2/2.

Quoted 5 lines
Quoted 5 lines
net/sunrpc/xdr.c +2 -0
+
+	/* obj is in the pages: move to tail */
	if (obj->len > buf->buflen - buf->len)
		return -ENOMEM;
net/sunrpc/xdr.c +0 -1
-	if (buf->tail[0].iov_len != 0)
Quoted 8 lines

net/sunrpc/xdr.c xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj) · line 1279

Quoted 4 lines
net/sunrpc/xdr.c +1 -3
-		obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
-	else
-		obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
+	obj->data = buf->head[0].iov_base + buf->head[0].iov_len;

Not sure this is a safe change. It's possible that the head buffer and tail buffer are not contiguous, which is what the buf->tail.iov_len check is looking for, IMO. Can this hunk be left out?

Quoted 2 lines

That's something I missed somehow, thanks for pointing it out. I see now that the transport can allocate them any way it likes.

Just looking at this again today -- we can definitely keep the check, but the second half of the statement also assumes a contiguous head/tail range. I think it's safe to just remove the test altogether and place the netobj at the end of the tail. Then in 2/2, we'll just place it at the beginning of the tail because the function is specialized for the mic.

Ben