Skip to contentSkip to conversation

Fix bugs and clean code in __nfs4_get_acl_uncached

PATCH In review Sachin Prabhu 2 participants 5 messages Aug 1, 2012 – Aug 2, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Fix bugs and clean code in __nfs4_get_acl_uncached

Split the bug fixes and code cleanup into separate patches asi requested in
http://www.spinics.net/lists/linux-nfs/msg31677.html

Fix bugs in function __nfs4_get_acl_uncached and clean up code to make it easier to understand.

Sachin Prabhu (2):
Fix array overflow in __nfs4_get_acl_uncached
Simplify check for size of ACL returned in __nfs4_get_acl_uncached

 fs/nfs/nfs4proc.c       | 18 +++++++++---------
 fs/nfs/nfs4xdr.c        |  4 +++-
 include/linux/nfs_xdr.h |  2 +-
 3 files changed, 13 insertions(+), 11 deletions(-)
Signature · 1 line
1.7.11.2
#1 lore raw

PATCH 1/2 Fix array overflow in __nfs4_get_acl_uncached

This fixes a bug introduced by commit
5a00689930ab975fdd1b37b034475017e460cf2a

Bruce Fields pointed out that the changes introduced by the patch will cause the array npages to overflow if a size requested is >= XATTR_SIZE_MAX.

The patch also fixes the check for the size of acl returned in the same function and updates the comment associated with this function.

---
 fs/nfs/nfs4proc.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
fs/nfs/nfs4proc.c +8 -8
index c157b20..be03b95 100644
@@ -3709,16 +3709,16 @@ out:
 /*
  * The getxattr API returns the required buffer length when called with a
  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
- * the required buf.  On a NULL buf, we send a page of data to the server
- * guessing that the ACL request can be serviced by a page. If so, we cache
- * up to the page of ACL data, and the 2nd call to getxattr is serviced by
- * the cache. If not so, we throw away the page, and cache the required
- * length. The next getxattr call will then produce another round trip to
- * the server, this time with the input buf of the required size.
+ * the required buf.  On a NULL buf, we allocate 2 pages guessing that the
+ * ACL request can be serviced by those pages. If so, we cache the ACL data,
+ * and the 2nd call to getxattr is serviced by the cache. If not so, we throw
+ * away the pages, and cache the required length. The next getxattr call will
+ * then produce another round trip to the server, this time with the input buf
+ * of the required size.
  */
 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
 {
-	struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
+	struct page *pages[NFS4ACL_MAXPAGES+1] = {NULL, };
 	struct nfs_getaclargs args = {
 		.fh = NFS_FH(inode),
 		.acl_pages = pages,
@@ -3770,7 +3770,7 @@ static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t bu
 		goto out_free;
 
 	acl_len = res.acl_len - res.acl_data_offset;
-	if (acl_len > args.acl_len)
+	if (res.acl_len > args.acl_len)
 		nfs4_write_cached_acl(inode, NULL, 0, acl_len);
 	else
 		nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Signature · 1 line
1.7.11.2
#2 lore raw

PATCH 2/2 Simplify check for size of ACL returned in __nfs4_get_acl_uncached

In reply to Sachin Prabhu

Rename flag ACL_LEN_REQEST to NFS4_ACL_LEN_ONLY. Apart from using this flag during a request to indicate that the user is only interested in the length of the ACL, we now also use it in the response stage to indicate that the data returned holds the complete ACL or just the length.

---
 fs/nfs/nfs4proc.c       | 4 ++--
 fs/nfs/nfs4xdr.c        | 4 +++-
 include/linux/nfs_xdr.h | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)
fs/nfs/nfs4proc.c +2 -2
index be03b95..aaa98f0 100644
@@ -3760,7 +3760,7 @@ static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t bu
 	/* Let decode_getfacl know not to fail if the ACL data is larger than
 	 * the page we send as a guess */
 	if (buf == NULL)
-		res.acl_flags |= NFS4_ACL_LEN_REQUEST;
+		res.acl_flags |= NFS4_ACL_LEN_ONLY;
 
 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
 		__func__, buf, buflen, npages, args.acl_len);
@@ -3770,7 +3770,7 @@ static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t bu
 		goto out_free;
 
 	acl_len = res.acl_len - res.acl_data_offset;
-	if (res.acl_len > args.acl_len)
+	if (res.acl_flags & NFS4_ACL_LEN_ONLY)
 		nfs4_write_cached_acl(inode, NULL, 0, acl_len);
 	else
 		nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
fs/nfs/nfs4xdr.c +3 -1
index 18fae29..a88fcd0 100644
@@ -5101,7 +5101,7 @@ static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
 		hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
 		attrlen += res->acl_data_offset;
 		if (attrlen > page_len) {
-			if (res->acl_flags & NFS4_ACL_LEN_REQUEST) {
+			if (res->acl_flags & NFS4_ACL_LEN_ONLY) {
 				/* getxattr interface called with a NULL buf */
 				res->acl_len = attrlen;
 				goto out;
@@ -5110,6 +5110,8 @@ static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
 					attrlen, page_len);
 			return -EINVAL;
 		}
+		/* At this stage we have the complete ACL */
+		res->acl_flags &= ~NFS4_ACL_LEN_ONLY;
 		xdr_read_pages(xdr, attrlen);
 		res->acl_len = attrlen;
 	} else
include/linux/nfs_xdr.h +1 -1
index d3b7c18..c2d77d5 100644
@@ -648,7 +648,7 @@ struct nfs_getaclargs {
 };
 
 /* getxattr ACL interface flags */
-#define NFS4_ACL_LEN_REQUEST	0x0001	/* zero length getxattr buffer */
+#define NFS4_ACL_LEN_ONLY	0x0001	/* zero length getxattr buffer */
 struct nfs_getaclres {
 	size_t				acl_len;
 	size_t				acl_data_offset;
Signature · 1 line
1.7.11.2
#3 lore raw

Re: [PATCH 2/2] Simplify check for size of ACL returned in __nfs4_get_acl_uncached

T24gV2VkLCAyMDEyLTA4LTAxIGF0IDE0OjI5ICswMTAwLCBTYWNoaW4gUHJhYmh1IHdyb3RlOg0K PiBSZW5hbWUgZmxhZyBBQ0xfTEVOX1JFUUVTVCB0byBORlM0X0FDTF9MRU5fT05MWS4gQXBhcnQg ZnJvbSB1c2luZyB0aGlzDQo+IGZsYWcgZHVya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vbmZzX3hkci5oDQo+ICsrKyBiL2luY2x1ZGUv bGludXgvbmZzX3hkci5oDQo+IEBAIC02NDgsNyArNjQ4LDcgQEAgc3RydWN0IG5mc19nZXRhY2xh cmdzIHsNCj4gIH07DQo+ICANCj4gIC8qIGdldHhhdHRyIEFDTCBpbnRlcmZhY2UgZmxhZ3MgKi8N Cj4gLSNkZWZpbmUgTkZTNF9BQ0xfTEVOX1JFUVVFU1QJMHgwMDAxCS8qIHplcm8gbGVuZ3RoIGdl dHhhdHRyIGJ1ZmZlciAqLw0KPiArI2RlZmluZSBORlM0X0FDTF9MRU5fT05MWQkweDAwMDEJLyog emVybyBsZW5ndGggZ2V0eGF0dHIgYnVmZmVyICovDQo+ICBzdHJ1Y3QgbmZzX2dldGFjbHJlcyB7 DQo+ICAJc2l6ZV90CQkJCWFjbF9sZW47DQo+ICAJc2l6ZV90CQkJCWFjbF9kYXRhX29mZnNldDsN Cg0K

#4 lore raw

Re: [PATCH 2/2] Simplify check for size of ACL returned in __nfs4_get_acl_uncached

Quoted 7 lines · Trond

fs/nfs/nfs4proc.c static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t bu · line 3773

Quoted 4 lines
fs/nfs/nfs4proc.c +1 -1
 	acl_len = res.acl_len - res.acl_data_offset;
-	if (res.acl_len > args.acl_len)
+	if (res.acl_flags & NFS4_ACL_LEN_ONLY)

Is this a bugfix or a cleanup? If the former, then it belongs in the first patch.

The earlier patch fixes this bug with the the if condition. This patch introduces the NFS4_ACL_LEN_ONLY flag which is set when the buffer we have set aside for the ACLs was not enough for the ACLs returned by the server.

Quoted 7 lines

fs/nfs/nfs4proc.c static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t bu · line 3776

Quoted 4 lines
fs/nfs/nfs4proc.c +0 -0
 		nfs4_write_cached_acl(inode, NULL, 0, acl_len);
 	else
 		nfs4_write_cached_acl(inode, pages, res.acl_data_offset,

Now if NFS4_ACL_LEN_ONLY is set, we appear to unconditionally write the acl, even if the buffer overflows...

If NFS4_ACL_LEN_ONLY is set, we call nfs4_write_cached_acl with the second argument set to NULL. This results in only the acl length being cached.

Quoted 5 lines
Quoted 5 lines
fs/nfs/nfs4xdr.c +0 -0
index 18fae29..a88fcd0 100644
@@ -5101,7 +5101,7 @@ static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
Quoted 8 lines

fs/nfs/nfs4xdr.c static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req, · line 5104

Quoted 5 lines
fs/nfs/nfs4xdr.c +1 -1
 		hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
 		attrlen += res->acl_data_offset;
 		if (attrlen > page_len) {
-			if (res->acl_flags & NFS4_ACL_LEN_REQUEST) {
+			if (res->acl_flags & NFS4_ACL_LEN_ONLY) {

The logic here still looks incorrect. If the user is only requesting the acl length, then we shouldn't care about the 'attrlen > page_len' test.

This is a result of a feature introduced with an earlier patch(bf118a342f10dafe44b14451a1392c3254629a1f) and the behaviour is described in the comment above __nfs4_get_acl_uncached().

***
The getxattr API returns the required buffer length when called with a NULL buf. The NFSv4 acl tool then calls getxattr again after allocating the required buf. On a NULL buf, we send a page of data to the server guessing that the ACL request can be serviced by a page. If so, we cache up to the page of ACL data, and the 2nd call to getxattr is serviced by the cache. If not so, we throw away the page, and cache the required length. The next getxattr call will then produce another round trip to the server, this time with the input buf of the required size.
***

Check to see if the ACL attribute length returned by the server is larger than the buffer allocated for it.

if (attrlen > page_len) {
	If the length returned is smaller than buffer allocated. 
	Check flags to see if the user had only requested for length.
	if (res->acl_flags & NFS4_ACL_LEN_ONLY) {
		/* getxattr interface called with a NULL buf */
		If yes, just save length and return
		res->acl_len = attrlen;
		goto out;
	}
	Else return -EINVAL.
	dprintk("NFS: acl reply: attrlen %u > page_len %zu\n",
		attrlen, page_len);
	return -EINVAL;
}

At the end of this if condition, we have determined that the buffer length we allocated is large enough for the length of ACL returned. So clear NFS4_ACL_LEN_ONLY if set.
/* At this stage we have the complete ACL */ res->acl_flags &= ~NFS4_ACL_LEN_ONLY;

Quoted 4 lines

There also appears to remain a lot of illegal deferencing of xdr->p even after this patch. We should be able to fix that by replacing xdr_read_pages with a call to xdr_enter_page() before we call decode_attr_bitmap()...

Sachin Prabhu