From f566cf181a44063cd93abc2d50fed19c69e177b0 Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Sat, 7 Nov 2020 18:00:34 -0600 Subject: [PATCH] file_systems/nfs: Fix SMAP violations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Try to return relevant errors when we can. * The style on these needs cleaned up badly. * I don't want the fix to be lost in style changes. Change-Id: I8a1661385fbeb8eec86a2c15828b449980050a78 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3386 Reviewed-by: Jérôme Duval --- .../kernel/file_systems/nfs/XDRInPacket.c | 19 ++++++---- .../kernel/file_systems/nfs/XDRInPacket.h | 3 +- src/add-ons/kernel/file_systems/nfs/nfs.h | 4 +-- .../kernel/file_systems/nfs/nfs_add_on.c | 36 +++++++++++++++---- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/add-ons/kernel/file_systems/nfs/XDRInPacket.c b/src/add-ons/kernel/file_systems/nfs/XDRInPacket.c index 6cd8fba9de..afa093f3bd 100644 --- a/src/add-ons/kernel/file_systems/nfs/XDRInPacket.c +++ b/src/add-ons/kernel/file_systems/nfs/XDRInPacket.c @@ -2,6 +2,9 @@ #include #include #include +#include + +#include "nfs.h" extern void XDRInPacketInit(struct XDRInPacket *packet) @@ -26,19 +29,23 @@ XDRInPacketGetInt32(struct XDRInPacket *packet) return val; } -extern void +extern int32 XDRInPacketGetFixed(struct XDRInPacket *packet, void *buffer, size_t len) { - memcpy (buffer,&packet->fBuffer[packet->fOffset],len); - packet->fOffset+=(len+3)&~3; + if (user_memcpy(buffer, &packet->fBuffer[packet->fOffset], len) + != B_OK) { + return NFSERR_IO; + } + + packet->fOffset += (len + 3) & ~3; + return NFS_OK; } extern size_t XDRInPacketGetDynamic(struct XDRInPacket *packet, void *buffer) { size_t size=XDRInPacketGetInt32(packet); - XDRInPacketGetFixed (packet,buffer,size); - + XDRInPacketGetFixed(packet,buffer,size); return size; } @@ -48,7 +55,7 @@ XDRInPacketGetString(struct XDRInPacket *packet) int32 size=XDRInPacketGetInt32(packet); char *string=(char *)malloc(size+1); string[size]=0; - XDRInPacketGetFixed (packet,string,size); + XDRInPacketGetFixed(packet,string,size); return string; } diff --git a/src/add-ons/kernel/file_systems/nfs/XDRInPacket.h b/src/add-ons/kernel/file_systems/nfs/XDRInPacket.h index 43b7e48329..d59fc0757b 100644 --- a/src/add-ons/kernel/file_systems/nfs/XDRInPacket.h +++ b/src/add-ons/kernel/file_systems/nfs/XDRInPacket.h @@ -13,7 +13,8 @@ struct XDRInPacket void XDRInPacketInit (struct XDRInPacket *packet); void XDRInPacketDestroy (struct XDRInPacket *packet); int32 XDRInPacketGetInt32 (struct XDRInPacket *packet); -void XDRInPacketGetFixed (struct XDRInPacket *packet, void *buffer, size_t len); +int32 XDRInPacketGetFixed (struct XDRInPacket *packet, void *buffer, + size_t len); size_t XDRInPacketGetDynamic (struct XDRInPacket *packet, void *buffer); char *XDRInPacketGetString (struct XDRInPacket *packet); void XDRInPacketSetTo (struct XDRInPacket *packet, uint8 *buffer, size_t offset); diff --git a/src/add-ons/kernel/file_systems/nfs/nfs.h b/src/add-ons/kernel/file_systems/nfs/nfs.h index a8869d9ba2..553acaac46 100644 --- a/src/add-ons/kernel/file_systems/nfs/nfs.h +++ b/src/add-ons/kernel/file_systems/nfs/nfs.h @@ -4,8 +4,8 @@ #include -const int32 NFS_VERSION=2; -const int32 NFS_PROGRAM=100003; +#define NFS_VERSION 2 +#define NFS_PROGRAM 100003 typedef enum nfs_stat { diff --git a/src/add-ons/kernel/file_systems/nfs/nfs_add_on.c b/src/add-ons/kernel/file_systems/nfs/nfs_add_on.c index d2d26467b0..01d801d2d1 100644 --- a/src/add-ons/kernel/file_systems/nfs/nfs_add_on.c +++ b/src/add-ons/kernel/file_systems/nfs/nfs_add_on.c @@ -484,17 +484,18 @@ nfs_mount(fs_nspace *ns, const char *path, nfs_fhandle *fhandle) fhstatus = XDRInPacketGetInt32(&reply); - if (fhstatus != 0) { + if (fhstatus != NFS_OK) { XDRInPacketDestroy(&reply); XDROutPacketDestroy(&call); return map_nfs_to_system_error(fhstatus); } - XDRInPacketGetFixed(&reply, fhandle->opaque, NFS_FHSIZE); + fhstatus = XDRInPacketGetFixed(&reply, fhandle->opaque, NFS_FHSIZE); XDRInPacketDestroy(&reply); XDROutPacketDestroy(&call); - return B_OK; + + return map_nfs_to_system_error(fhstatus); } @@ -538,7 +539,13 @@ nfs_lookup (fs_nspace *ns, const nfs_fhandle *dir, const char *filename, return map_nfs_to_system_error(status); } - XDRInPacketGetFixed(&reply, fhandle->opaque, NFS_FHSIZE); + status = XDRInPacketGetFixed(&reply, fhandle->opaque, NFS_FHSIZE); + + if (status != NFS_OK) { + XDRInPacketDestroy(&reply); + XDROutPacketDestroy(&call); + return map_nfs_to_system_error(status); + } if (st) get_nfs_attr(&reply, st); @@ -1002,7 +1009,11 @@ fs_readdir(fs_volume *_volume, fs_vnode *_node, void *_cookie, vnid=XDRInPacketGetInt32(&reply); filename=XDRInPacketGetString(&reply); - XDRInPacketGetFixed(&reply, newCookie.opaque, NFS_COOKIESIZE); + status = XDRInPacketGetFixed(&reply, newCookie.opaque, + NFS_COOKIESIZE); + + if (status != NFS_OK) + return map_nfs_to_system_error(status); //if (strcmp(".",filename)&&strcmp("..",filename)) //if ((ns->rootid != node->vnid) || (strcmp(".",filename)&&strcmp("..",filename))) @@ -1810,7 +1821,13 @@ fs_create(fs_volume *_volume, fs_vnode *_dir, const char *name, int omode, return map_nfs_to_system_error(status); } - XDRInPacketGetFixed(&reply, fhandle.opaque, NFS_FHSIZE); + status = XDRInPacketGetFixed(&reply, fhandle.opaque, NFS_FHSIZE); + + if (status != NFS_OK) { + XDRInPacketDestroy(&reply); + XDROutPacketDestroy(&call); + return map_nfs_to_system_error(status); + } get_nfs_attr(&reply,&st); @@ -2041,7 +2058,12 @@ fs_mkdir(fs_volume *_volume, fs_vnode *_dir, const char *name, int perms) return map_nfs_to_system_error(status); } - XDRInPacketGetFixed(&reply, fhandle.opaque, NFS_FHSIZE); + status = XDRInPacketGetFixed(&reply, fhandle.opaque, NFS_FHSIZE); + + if (status != NFS_OK) { + XDROutPacketDestroy(&call); + return map_nfs_to_system_error(status); + } get_nfs_attr(&reply, &st);