Skip to contentSkip to conversation

NFS & long symlinks = stack overflow

DISCUSSION Pascal Schmidt 5 participants 7 messages May 15, 2004 – May 16, 2004 2 messages not in the archive NFS client

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

On Sat, 15 May 2004 16:00:21 +0200, you wrote in linux.kernel:

Quoted 2 lines

Lovely. The real limit imposed by client (apparently not enforced, though) is PAGE_CACHE_SIZE - 4 - 1. What are the protocol limits?

None. An NFSv3 server can enforce an arbitrary limit on filename length, advertised via PATHCONF, though.

Signature · 2 lines
Ciao,
Pascal
#3 lore raw
Quoted 7 lines · Pascal Schmidt

On Sat, 15 May 2004 16:00:21 +0200, you wrote in linux.kernel:

Quoted 2 lines

Lovely. The real limit imposed by client (apparently not enforced, though) is PAGE_CACHE_SIZE - 4 - 1. What are the protocol limits?

None. An NFSv3 server can enforce an arbitrary limit on filename length, advertised via PATHCONF, though.

Lovely... How are other clients dealing with that? Put a reasonable limit on the size and return an error if READLINK brings more than that?

#4 lore raw
Quoted 2 lines · On Sat, 2004-05-15 at 10:53, [email protected] wrote:

Lovely... How are other clients dealing with that? Put a reasonable limit on the size and return an error if READLINK brings more than that?

Yes. The following patch (backported from the NFSv4 code) should do the right thing...

Cheers,
Trond

fs/nfs/nfs3xdr.c renamed from fs/nfs/nfs3xdr.c.orig +5 -2
@@ -742,8 +742,11 @@ nfs3_xdr_readlinkres(struct rpc_rqst *re
 	strlen = (u32*)kmap_atomic(rcvbuf->pages[0], KM_USER0);
 	/* Convert length of symlink */
 	len = ntohl(*strlen);
-	if (len > rcvbuf->page_len)
-		len = rcvbuf->page_len;
+	if (len > PAGE_CACHE_SIZE - 5) {
+		printk(KERN_WARNING "nfs: server returned giant symlink!\n");
+		kunmap_atomic(strlen, KM_USER0);
+		return -EIO;
+	}
 	*strlen = len;
 	/* NULL terminate the string we got */
 	string = (char *)(strlen + 1);
#5 lore raw

Trond Myklebust <[email protected]> wrote:
TM> On Sat, 2004-05-15 at 10:53, [email protected]

Quoted 2 lines · TM> wrote:
Quoted 2 lines

Lovely... How are other clients dealing with that? Put a reasonable limit on the size and return an error if READLINK brings more than that?

TM> Yes. The following patch (backported from the NFSv4 code) should do the TM> right thing...

Yeah. That helps.
Thank you.

Bye,
Oleg

#6 lore raw
In reply to Trond Myklebust
Quoted 3 lines · Trond Myklebust

Yes. The following patch (backported from the NFSv4 code) should do the right thing...

Why isn't this needed for nfsv2, which has similar code?

Linus

#7 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 3 lines · Trond Myklebust

Yes. The following patch (backported from the NFSv4 code) should do the right thing...

Why isn't this needed for nfsv2, which has similar code?

v2 has a hard limit in protocol (<= 1Kb). However, we shouldn't assume that server is sane...

#8 lore raw

På su , 16/05/2004 klokka 00:55, skreiv
[email protected]:

Quoted 2 lines

v2 has a hard limit in protocol (<= 1Kb). However, we shouldn't assume that server is sane...

True... The other thing is that we need to return ENAMETOOLONG rather than EIO.
Finally, the NFS readlink() methods all take a buffer length argument. Use that instead of assuming PAGE_SIZE...

OK... How about the following?

Cheers,
Trond

 nfs2xdr.c |   11 +++++++----
 nfs3xdr.c |   11 +++++++----
 nfs4xdr.c |   11 ++++++-----
 3 files changed, 20 insertions(+), 13 deletions(-)
fs/nfs/nfs2xdr.c +7 -4
diff -u --recursive --new-file --show-c-function linux-2.6.6-01-reconnect/fs/nfs/nfs2xdr.c linux-2.6.6-02-symlink_overflow/fs/nfs/nfs2xdr.c
@@ -511,8 +511,8 @@ static int
 nfs_xdr_readlinkargs(struct rpc_rqst *req, u32 *p, struct nfs_readlinkargs *args)
 {
 	struct rpc_auth *auth = req->rq_task->tk_auth;
+	unsigned int count = args->count - 5;
 	unsigned int replen;
-	u32 count = args->count - 4;
 
 	p = xdr_encode_fhandle(p, args->fh);
 	req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
@@ -547,12 +547,15 @@ nfs_xdr_readlinkres(struct rpc_rqst *req
 	strlen = (u32*)kmap_atomic(rcvbuf->pages[0], KM_USER0);
 	/* Convert length of symlink */
 	len = ntohl(*strlen);
-	if (len > rcvbuf->page_len)
-		len = rcvbuf->page_len;
+	if (len > rcvbuf->page_len) {
+		dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
+		kunmap_atomic(strlen, KM_USER0);
+		return -ENAMETOOLONG;
+	}
 	*strlen = len;
 	/* NULL terminate the string we got */
 	string = (char *)(strlen + 1);
-	string[len] = 0;
+	string[len] = '\0';
 	kunmap_atomic(strlen, KM_USER0);
 	return 0;
 }
fs/nfs/nfs3xdr.c +7 -4
diff -u --recursive --new-file --show-c-function linux-2.6.6-01-reconnect/fs/nfs/nfs3xdr.c linux-2.6.6-02-symlink_overflow/fs/nfs/nfs3xdr.c
@@ -702,8 +702,8 @@ static int
 nfs3_xdr_readlinkargs(struct rpc_rqst *req, u32 *p, struct nfs3_readlinkargs *args)
 {
 	struct rpc_auth *auth = req->rq_task->tk_auth;
+	unsigned int count = args->count - 5;
 	unsigned int replen;
-	u32 count = args->count - 4;
 
 	p = xdr_encode_fhandle(p, args->fh);
 	req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
@@ -742,12 +742,15 @@ nfs3_xdr_readlinkres(struct rpc_rqst *re
 	strlen = (u32*)kmap_atomic(rcvbuf->pages[0], KM_USER0);
 	/* Convert length of symlink */
 	len = ntohl(*strlen);
-	if (len > rcvbuf->page_len)
-		len = rcvbuf->page_len;
+	if (len > rcvbuf->page_len) {
+		dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
+		kunmap_atomic(strlen, KM_USER0);
+		return -ENAMETOOLONG;
+	}
 	*strlen = len;
 	/* NULL terminate the string we got */
 	string = (char *)(strlen + 1);
-	string[len] = 0;
+	string[len] = '\0';
 	kunmap_atomic(strlen, KM_USER0);
 	return 0;
 }
fs/nfs/nfs4xdr.c +6 -5
diff -u --recursive --new-file --show-c-function linux-2.6.6-01-reconnect/fs/nfs/nfs4xdr.c linux-2.6.6-02-symlink_overflow/fs/nfs/nfs4xdr.c
@@ -947,7 +947,8 @@ static int encode_readdir(struct xdr_str
 static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
 {
 	struct rpc_auth *auth = req->rq_task->tk_auth;
-	int replen;
+	unsigned int count = readlink->count - 5;
+	unsigned int replen;
 	uint32_t *p;
 
 	RESERVE_SPACE(4);
@@ -958,7 +959,7 @@ static int encode_readlink(struct xdr_st
 	 *      + OP_READLINK + status  = 7
 	 */
 	replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
-	xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages, 0, readlink->count);
+	xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages, 0, count);
 	
 	return 0;
 }
@@ -2921,10 +2922,10 @@ static int decode_readlink(struct xdr_st
 	 */
 	strlen = (uint32_t *) kmap_atomic(rcvbuf->pages[0], KM_USER0);
 	len = ntohl(*strlen);
-	if (len > PAGE_CACHE_SIZE - 5) {
-		printk(KERN_WARNING "nfs: server returned giant symlink!\n");
+	if (len > rcvbuf->page_len) {
+		dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
 		kunmap_atomic(strlen, KM_USER0);
-		return -EIO;
+		return -ENAMETOOLONG;
 	}
 	*strlen = len;