From 046e0ee2594eb7f46d7d0bd76ee54371cb0a2669 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sun, 21 Feb 2016 20:11:04 +0100 Subject: [PATCH] exportfs: fix endless loop for eof/error on second read in localread9pmsg() the ini buffer was not reset when we got eof in the message body read causing a endless loop. instead of defining our own read9pmsg() function, just handle the first read specially when we consumed the first 4 bytes for the "impo" protocol escape check. --- sys/src/cmd/exportfs/exportfs.c | 56 ++++++++++----------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/sys/src/cmd/exportfs/exportfs.c b/sys/src/cmd/exportfs/exportfs.c index a01081847..a434d1cdd 100644 --- a/sys/src/cmd/exportfs/exportfs.c +++ b/sys/src/cmd/exportfs/exportfs.c @@ -51,7 +51,6 @@ int encproto = Encnone; int readonly; static void mksecret(char *, uchar *); -static int localread9pmsg(int, void *, uint, void *); static char *anstring = "tcp!*!0"; char *netdir = "", *local = "", *remote = ""; @@ -391,60 +390,37 @@ main(int argc, char **argv) if(ai != nil) auth_freeAI(ai); + if(ini != nil){ + r = getsbuf(); + memmove(r->buf, ini, BIT32SZ); + n = GBIT32(r->buf); + if(n <= BIT32SZ || n > messagesize) + fatal("bad length in 9P2000 message header"); + n -= BIT32SZ; + if(readn(0, r->buf+BIT32SZ, n) != n) + fatal(nil); + n += BIT32SZ; + goto Message; + } + /* * Start serving file requests from the network */ for(;;) { r = getsbuf(); - while((n = localread9pmsg(0, r->buf, messagesize, ini)) == 0) + while((n = read9pmsg(0, r->buf, messagesize)) == 0) ; - if(n < 0) + if(n <= 0) fatal(nil); + Message: if(convM2S(r->buf, n, &r->work) == 0) fatal("convM2S format error"); DEBUG(DFD, "%F\n", &r->work); (fcalls[r->work.type])(r); - ini = nil; } } -/* - * WARNING: Replace this with the original version as soon as all - * _old_ imports have been replaced with negotiating imports. Also - * cpu relies on this (which needs to be fixed!) -- pb. - */ -static int -localread9pmsg(int fd, void *abuf, uint n, void *ini) -{ - int m, len; - uchar *buf; - - buf = abuf; - - /* read count */ - if(ini != nil) - memcpy(buf, ini, BIT32SZ); - else { - m = readn(fd, buf, BIT32SZ); - if(m != BIT32SZ){ - if(m < 0) - return -1; - return 0; - } - } - - len = GBIT32(buf); - if(len <= BIT32SZ || len > n){ - werrstr("bad length in 9P2000 message header"); - return -1; - } - len -= BIT32SZ; - m = readn(fd, buf+BIT32SZ, len); - if(m < len) - return 0; - return BIT32SZ+m; -} void reply(Fcall *r, Fcall *t, char *err) {