Skip to contentSkip to conversation

Fix array overflow in __nfs4_get_acl_uncached

PATCH In review Sachin Prabhu 3 participants 5 messages Jul 24, 2012 – Jul 31, 2012

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

PATCH 1/1 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 length of the ACL returned. The flag ACL_LEN_REQUEST has been renamed to NFS4_ACL_LEN_ONLY and apart from indicating that the user is just interested in the ACL length when making a request, it is also used to indicate if the xdr pages buffer returned in response holds the complete ACL or just the length.

---
 fs/nfs/nfs4proc.c       |   18 +++++++++---------
 fs/nfs/nfs4xdr.c        |    4 +++-
 include/linux/nfs_xdr.h |    2 +-
 3 files changed, 13 insertions(+), 11 deletions(-)
fs/nfs/nfs4proc.c +9 -9
index 15fc7e4..e19c322 100644
@@ -3726,16 +3726,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,
@@ -3777,7 +3777,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);
@@ -3787,7 +3787,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_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 8aadd90..39c8483 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.10.4
#1 lore raw
Quoted 7 lines · Sachin Prabhu

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.

Quoted 5 lines

The patch also fixes the check for the length of the ACL returned. The flag ACL_LEN_REQUEST has been renamed to NFS4_ACL_LEN_ONLY and apart from indicating that the user is just interested in the ACL length when making a request, it is also used to indicate if the xdr pages buffer returned in response holds the complete ACL or just the length.

Looks right to me.

--b.

Quoted 96 lines
---
 fs/nfs/nfs4proc.c       |   18 +++++++++---------
 fs/nfs/nfs4xdr.c        |    4 +++-
 include/linux/nfs_xdr.h |    2 +-
 3 files changed, 13 insertions(+), 11 deletions(-)
fs/nfs/nfs4proc.c +9 -9
index 15fc7e4..e19c322 100644
@@ -3726,16 +3726,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,
@@ -3777,7 +3777,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);
@@ -3787,7 +3787,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_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 8aadd90..39c8483 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 · 6 lines
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to Sachin Prabhu

T24gVHVlLCAyMDEyLTA3LTI0IGF0IDEyOjM2ICswMTAwLCBTYWNoaW4gUHJhYmh1IHdyb3RlOg0K PiBUaGlzIGZpeGVzIGEgYnVnIGludHJvZHVjZWQgYnkgY29tbWl0DQo+IDVhMDA2ODk5MzBhYjk3 NWZkZDFiMzdiMDM0NDc1MDE3ZTQ2MGNmMmENCj4gDQo+IEJydWNlIEZpZWxkcyBwb2ludGVkIG91 dCB0aGF0IHRoZSBjaGFuZ2VzIGludHJvZHVjZWQgYnkgdGhlIHBhdGNoIHdpbGwNCj4gY2F1c2Ug dGhlIGFycmF5IG5wYWdlcyB0byBvdmVyZmxvdyBpZiBhIHNpemUgcmVxdWVzdGVkIGlzID49DQo+ IFhBVFRSX1NJWkVfTUFYLg0KPiANCj4gVGhlIHBhdGNoIGFsc28gZml4ZXMgdGhlIGNoZWNrIGZv ciB0aGUgbGVuZ3RoIG9mIHRoZSBBQ0wgcmV0dXJuZWQuIFRoZQ0KPiBmbGFnIEFDTF9MRU5fUkVR VUVTVCBoYXMgYmVlbiByZW5hbWVkIHRvIE5GUzRfQUNMX0xFTl9PTkxZIGFuZCBhcGFydA0KPiBm cm9tIGluZGljYXRpbmcgdGhhdCB0aGUgdXNlciBpcyBqdXN0IGludGVyZXN0ZWQgaW4gdGhlIEFD TCBsZW5ndGggd2hlbg0KPiBtYWtpbmcgYSByZXF1ZXN0LCBpdCBpcyBhbHNvIHVzZWQgdG8gaW5k aWNhdGUgaWYgdGhlIHhkciBwYWdlcyBidWZmZXINCj4gcmV0dXJuZWQgaW4gcmVzcG9uc2UgaG9s ZHMgdGhlIGNvbXBsZXRlIEFDTCBvciBqdXN0IHRoZSBsZW5ndGguDQo+IA0KPiBTaWduZWQtb2Zm LWJ5OiBTYWNoaW4gUHJhYmh1IDxzcHJhYmh1QHJlZGhhdC5jb20+DQoNCklmIHRoaXMgZml4IGlz IGdvaW5nIGludG8gc3RhYmxlLCB0aGVuIHRoZSBwYXRjaCBuZWVkcyB0byBiZSBzcGxpdCB1cC4N ClRoZSByZW5hbWUgb2YgQUNMX0xFTl9SRVFVRVNUIHRvIE5GUzRfQUNMX0xFTl9PTkxZIGlzIGEg Y2xlYW51cCBhbmQNCnNob3VsZCBub3QgYmUgcHJvcGFnYXRlZCB0byBzdGFibGUuLi4NCg0KQ2hl ZXJzDQogIFRyb25kDQotLSANClRyb25kIE15a2xlYnVzdA0KTGludXggTkZTIGNsaWVudCBtYWlu dGFpbmVyDQoNCk5ldEFwcA0KVHJvbmQuTXlrbGVidXN0QG5ldGFwcC5jb20NCnd3dy5uZXRhcHAu Y29tDQoNCg==

#3 lore raw
Quoted 14 lines · Trond
Quoted 13 lines · Sachin Prabhu

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 length of the ACL returned. The flag ACL_LEN_REQUEST has been renamed to NFS4_ACL_LEN_ONLY and apart from indicating that the user is just interested in the ACL length when making a request, it is also used to indicate if the xdr pages buffer returned in response holds the complete ACL or just the length.

Quoted 5 lines
Quoted 1 line

If this fix is going into stable, then the patch needs to be split up. The rename of ACL_LEN_REQUEST to NFS4_ACL_LEN_ONLY is a cleanup and should not be propagated to stable...

Trond the change there was required to fix a mistake in the if condition.

static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
{
..
acl_len = res.acl_len - res.acl_data_offset;
if (acl_len > args.acl_len)
..
}

At this point,
res.acl_data_offset = size of bitmap array + size of length attribute res.acl_length = res.acl_data_offset + ACL length
These 2 variables are set in decode_getacl(). The right check here would have been to compare res.acl_len and args.acl_len.

Instead of simply replacing the if condition, I decided to clean up the code to use NFS4_ACL_LEN_ONLY on advice of Bruce to make the code easier to understand.

So both parts of this patch are actually bug fixes. If you still think that they should be separated, I am happy to do that.

Sachin Prabhu

#4 lore raw

T24gVHVlLCAyMDEyLTA3LTMxIGF0IDIyOjA0ICswMTAwLCBTYWNoaW4gUHJhYmh1IHdyb3RlOg0K PiBPbiBNb24sIDIwMTItMDctMzAgYXQgMjE6NTQgKzAwMDAsIE15a2xlYnVzdCwgVHJvbmQgd3Jv dGU6DQo+ID4gT24gVHVlLCAyMDEyLTA3LTI0IGF0IDEyOjM2ICswMTAwLCBTYWNoaW4gUHJhYmh1 IHdyb3RlOg0KPiA+ID4gVGhpcyBmaXhlcyBhIGJ1ZyBpbnRyb2R1Y2VkIGJ5IGNvbW1pdA0KPiA+ ID4gNWEwMDY4OTkzMGFiOTc1ZmRkMWIzN2IwMzQ0NzUwMTdlNDYwY2YyYQ0KPiA+ID4gDQo+ID4g PiBCcnVjZSBGaWVsZHMgcG9pbnRlZCBvdXQgdGhhdCB0aGUgY2hhbmdlcyBpbnRyb2R1Y2VkIGJ5 IHRoZSBwYXRjaCB3aWxsDQo+ID4gPiBjYXVzZSB0aGUgYXJyYXkgbnBhZ2VzIHRvIG92ZXJmbG93 IGlmIGEgc2l6ZSByZXF1ZXN0ZWQgaXMgPj0NCj4gPiA+IFhBVFRSX1NJWkVfTUFYLg0KPiA+ID4g DQo+ID4gPiBUaGUgcGF0Y2ggYWxzbyBmaXhlcyB0aGUgY2hlY2sgZm9yIHRoZSBsZW5ndGggb2Yg dGhlIEFDTCByZXR1cm5lZC4gVGhlDQo+ID4gPiBmbGFnIEFDTF9MRU5fUkVRVUVTVCBoYXMgYmVl biByZW5hbWVkIHRvIE5GUzRfQUNMX0xFTl9PTkxZIGFuZCBhcGFydA0KPiA+ID4gZnJvbSBpbmRp Y2F0aW5nIHRoYX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