NFS: Avoid memcpy() run-time warning for struct sockaddr overflows
Subsystems (2)
PATCH 1/1 NFS: Avoid memcpy() run-time warning for struct sockaddr overflows
The 'nfs_server' and 'mount_server' structures include a union of 'struct sockaddr' (with the older 16 bytes max address size) and 'struct sockaddr_storage' which is large enough to hold all the supported sa_family types (128 bytes max size). The runtime memcpy() buffer overflow checker is seeing attempts to write beyond the 16 bytes as an overflow, but the actual expected size is that of 'struct sockaddr_storage'. Adjust the pointers to the correct union member. Avoids this false positive run-time warning under CONFIG_FORTIFY_SOURCE:
memcpy: detected field-spanning write (size 28) of single field "&ctx->nfs_server.address" at fs/nfs/namespace.c:178 (size 16)
--- fs/nfs/fs_context.c | 2 +- fs/nfs/namespace.c | 2 +- fs/nfs/super.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
fs/nfs/fs_context.c +1 -1
@@ -1540,7 +1540,7 @@ static int nfs_init_fs_context(struct fs_context *fc) ctx->version = nfss->nfs_client->rpc_ops->version; ctx->minorversion = nfss->nfs_client->cl_minorversion; -memcpy (&ctx->nfs_server.address, &nfss->nfs_client->cl_addr, +memcpy (&ctx->nfs_server._address, &nfss->nfs_client->cl_addr, ctx->nfs_server.addrlen);if (fc->net_ns != net) {
fs/nfs/namespace.c +1 -1
@@ -175,7 +175,7 @@ struct vfsmount *nfs_d_automount(struct path *path) }/* for submounts we want the same server; referrals will reassign */ -memcpy (&ctx->nfs_server.address, &client->cl_addr, client->cl_addrlen); +memcpy (&ctx->nfs_server._address, &client->cl_addr, client->cl_addrlen); ctx->nfs_server.addrlen = client->cl_addrlen; ctx->nfs_server.port = server->port;
fs/nfs/super.c +2 -2
@@ -823,7 +823,7 @@ static int nfs_request_mount(struct fs_context *fc,struct nfs_fs_context *ctx =nfs_fc2context (fc);struct nfs_mount_request request = { .sap = (struct sockaddr *) - &ctx->mount_server.address, + &ctx->mount_server._address, .dirpath = ctx->nfs_server.export_path, .protocol = ctx->mount_server.protocol, .fh = root_fh,
@@ -854,7 +854,7 @@ static int nfs_request_mount(struct fs_context *fc,* Construct the mount server's address. */ if (ctx->mount_server.address.sa_family ==AF_UNSPEC ) { -memcpy (request.sap, &ctx->nfs_server.address, +memcpy (request.sap, &ctx->nfs_server._address, ctx->nfs_server.addrlen); ctx->mount_server.addrlen = ctx->nfs_server.addrlen; }
Signature · 1 line
2.34.1
Quoted 9 lines · Kees Cook
The '
nfs_server' and 'mount_server' structures include a union of 'struct sockaddr' (with the older 16 bytes max address size) and 'structsockaddr_storage' which is large enough to hold all the supportedsa_familytypes (128 bytes max size). The runtimememcpy()buffer overflow checker is seeing attempts to write beyond the 16 bytes as an overflow, but the actual expected size is that of 'structsockaddr_storage'. Adjust the pointers to the correct union member. Avoids this false positive run-time warning underCONFIG_FORTIFY_SOURCE:
Quoted 8 lines
memcpy: detected field-spanning write (size 28) of single field "
&ctx->nfs_server.address" atfs/nfs/namespace.c:178(size 16)Reported-by kernel test robot <[email protected]>Cc Trond Myklebust <[email protected]>Cc Anna Schumaker <[email protected]>Signed-off-by Kees Cook <[email protected]>
Thanks!
--
Gustavo
Quoted 57 lines
--- fs/nfs/fs_context.c | 2 +- fs/nfs/namespace.c | 2 +- fs/nfs/super.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)fs/nfs/fs_context.c +1
-1@@ -1540,7 +1540,7 @@ static int nfs_init_fs_context(struct fs_context *fc) ctx->version = nfss->nfs_client->rpc_ops->version; ctx->minorversion = nfss->nfs_client->cl_minorversion; -memcpy (&ctx->nfs_server.address, &nfss->nfs_client->cl_addr, +memcpy (&ctx->nfs_server._address, &nfss->nfs_client->cl_addr, ctx->nfs_server.addrlen);if (fc->net_ns != net) {fs/nfs/namespace.c +1
-1@@ -175,7 +175,7 @@ struct vfsmount *nfs_d_automount(struct path *path) }/* for submounts we want the same server; referrals will reassign */ -memcpy (&ctx->nfs_server.address, &client->cl_addr, client->cl_addrlen); +memcpy (&ctx->nfs_server._address, &client->cl_addr, client->cl_addrlen); ctx->nfs_server.addrlen = client->cl_addrlen; ctx->nfs_server.port = server->port;fs/nfs/super.c +2
-2@@ -823,7 +823,7 @@ static int nfs_request_mount(struct fs_context *fc,struct nfs_fs_context *ctx =nfs_fc2context (fc);struct nfs_mount_request request = { .sap = (struct sockaddr *) - &ctx->mount_server.address, + &ctx->mount_server._address, .dirpath = ctx->nfs_server.export_path, .protocol = ctx->mount_server.protocol, .fh = root_fh,@@ -854,7 +854,7 @@ static int nfs_request_mount(struct fs_context *fc,* Construct the mount server's address. */ if (ctx->mount_server.address.sa_family ==AF_UNSPEC ) { -memcpy (request.sap, &ctx->nfs_server.address, +memcpy (request.sap, &ctx->nfs_server._address, ctx->nfs_server.addrlen); ctx->mount_server.addrlen = ctx->nfs_server.addrlen; }Signature · 1 line
2.34.1
Quoted 12 lines · Gustavo A. R. Silva
Quoted 11 lines · Kees Cook
The '
nfs_server' and 'mount_server' structures include a union of 'struct sockaddr' (with the older 16 bytes max address size) and 'structsockaddr_storage' which is large enough to hold all the supportedsa_familytypes (128 bytes max size). The runtimememcpy()buffer overflow checker is seeing attempts to write beyond the 16 bytes as an overflow, but the actual expected size is that of 'structsockaddr_storage'. Adjust the pointers to the correct union member. Avoids this false positive run-time warning underCONFIG_FORTIFY_SOURCE:memcpy: detected field-spanning write (size 28) of single field "
&ctx->nfs_server.address" atfs/nfs/namespace.c:178(size 16)
Quoted 8 lines
Quoted 6 lines
Reported-by kernel test robot <[email protected]>Cc Trond Myklebust <[email protected]>Cc Anna Schumaker <[email protected]>Signed-off-by Kees Cook <[email protected]>Reviewed-by Gustavo A. R. Silva <[email protected]>
Thanks!
Quoted 14 lines
Quoted 14 lines
--- fs/nfs/fs_context.c | 2 +- fs/nfs/namespace.c | 2 +- fs/nfs/super.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)fs/nfs/fs_context.c +0
-0@@ -1540,7 +1540,7 @@ static int nfs_init_fs_context(struct fs_context *fc) ctx->version = nfss->nfs_client->rpc_ops->version; ctx->minorversion = nfss->nfs_client->cl_minorversion;
Quoted 3 lines
fs/nfs/fs_context.c static int nfs_init_fs_context(struct fs_context *fc) · line 1544
Quoted 3 lines
fs/nfs/fs_context.c +1
-1-memcpy (&ctx->nfs_server.address, &nfss->nfs_client->cl_addr, +memcpy (&ctx->nfs_server._address, &nfss->nfs_client->cl_addr, ctx->nfs_server.addrlen);
So, I spent a bunch more time looking at the plumbing of struct sockaddr vs struct sockaddr_storage. In NFS, everything I could find is actually already backed by a full sockaddr_storage, so I think a more complete fix here would actually be to update all the internals to pass sockaddr_storage instead of sockaddr. The interfaces to other things (e.g. rpc) can cast back to sockaddr for now. It is a pretty reasonable cleanup, IMO.
I'll send a follow-up that makes this change on top of this patch, though they could be squashed if that was desired.
Signature · 1 line
Kees Cook