From 1c66b56af1b4961a4a7c765e546f438745ed2a66 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Mon, 12 Feb 2018 19:04:29 +0000 Subject: [PATCH] Update proxy malloc to add the struct size onto the request/response buffer This alters the meaning of NBD_MAX_SIZE to be the actual max request size we'll accept over nbd. Previously it was *nearly* the max size we'd accept depending on the size of the struct. --- src/proxy/proxy.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/proxy/proxy.c b/src/proxy/proxy.c index b323c68..951542c 100644 --- a/src/proxy/proxy.c +++ b/src/proxy/proxy.c @@ -76,8 +76,12 @@ struct proxier* proxy_create( } out->init.buf = xmalloc( sizeof( struct nbd_init_raw ) ); - out->req.buf = xmalloc( NBD_MAX_SIZE ); - out->rsp.buf = xmalloc( NBD_MAX_SIZE ); + + /* Add on the request / response size to our malloc to accommodate both + * the struct and the data + */ + out->req.buf = xmalloc( NBD_MAX_SIZE + NBD_REQUEST_SIZE ); + out->rsp.buf = xmalloc( NBD_MAX_SIZE + NBD_RESPONSE_SIZE ); log_context = xmalloc( strlen(s_upstream_address) + strlen(s_upstream_port) + 2 ); sprintf(log_context, "%s:%s", s_upstream_address, s_upstream_port); @@ -452,15 +456,18 @@ int proxy_read_from_downstream( struct proxier *proxy, int state ) return EXIT; } - /* Simple validations */ + /* Simple validations -- the request / reply size have already + * been taken into account in the xmalloc, so no need to worry + * about them here + */ if ( request->type == REQUEST_READ ) { - if (request->len > ( NBD_MAX_SIZE - NBD_REPLY_SIZE ) ) { + if ( request->len > NBD_MAX_SIZE ) { warn( "NBD read request size %"PRIu32" too large", request->len ); return EXIT; } } if ( request->type == REQUEST_WRITE ) { - if (request->len > ( NBD_MAX_SIZE - NBD_REQUEST_SIZE ) ) { + if ( request->len > NBD_MAX_SIZE ) { warn( "NBD write request size %"PRIu32" too large", request->len ); return EXIT; }