diff --git a/debian/changelog b/debian/changelog index df929fd..a1bf2ff 100644 --- a/debian/changelog +++ b/debian/changelog @@ -16,6 +16,8 @@ flexnbd (0.2.0) UNRELEASED; urgency=medium disconnecting and retrying (#36, !40) * Ignore ends of discs that stray outside of 512-byte sector sizes (!42). * Tweak logging for readloop failures (!44) + * Alter semantics of NBD_MAX_BLOCK_SIZE to remove struct size overheads when + calculating if a request exceeds the max block size (!45) -- James Carter Thu, 11 Jan 2018 10:05:35 +0000 diff --git a/src/proxy/proxy.c b/src/proxy/proxy.c index b323c68..291acd6 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 / reply 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_REPLY_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; } diff --git a/tests/acceptance/proxy_tests.rb b/tests/acceptance/proxy_tests.rb index 65fe38a..897c487 100644 --- a/tests/acceptance/proxy_tests.rb +++ b/tests/acceptance/proxy_tests.rb @@ -207,4 +207,17 @@ module ProxyTests end end end + + def test_maximum_write_request_size + # Defined in src/common/nbdtypes.h + nbd_max_block_size = 32 * 1024 * 1024 + @env.writefile1('0' * 40 * 1024) + with_proxied_client do |client| + # This will crash with EPIPE if the proxy dies. + client.write(0, b * nbd_max_block_size) + rsp = client.read_response + assert_equal FlexNBD::REPLY_MAGIC, rsp[:magic] + assert_equal 0, rsp[:error] + end + end end