cifs: Fix the smbd_request and smbd_reponse slabs to allow usercopy
Subsystems (5)
David Howells
maintainerPATCH 1/1 cifs: Fix the smbd_request and smbd_reponse slabs to allow usercopy
The handling of received data in the smbdirect client code involves using copy_to_iter() to copy data from the smbd_reponse struct's packet trailer to a folioq buffer provided by netfslib that encapsulates a chunk of pagecache.
If, however, CONFIG_HARDENED_USERCOPY=y, this will result in the checks then performed in copy_to_iter() oopsing with something like the following:
CIFS: Attempting to mount //172.31.9.1/test
CIFS: VFS: RDMA transport established
usercopy: Kernel memory exposure attempt detected from SLUB object 'smbd_response_0000000091e24ea1' (offset 81, size 63)!
------------[ cut here ]------------ kernel BUG at mm/usercopy.c:102! ... RIP: 0010:usercopy_abort+0x6c/0x80 ... Call Trace: <TASK> __check_heap_object+0xe3/0x120 __check_object_size+0x4dc/0x6d0 smbd_recv+0x77f/0xfe0 [cifs] cifs_readv_from_socket+0x276/0x8f0 [cifs] cifs_read_from_socket+0xcd/0x120 [cifs] cifs_demultiplex_thread+0x7e9/0x2d50 [cifs] kthread+0x396/0x830 ret_from_fork+0x2b8/0x3b0 ret_from_fork_asm+0x1a/0x30
The problem is that the smbd_response slab's packet field isn't marked as being permitted for usercopy.
Fix this by passing parameters to kmem_slab_create() to indicate that copy_to_iter() is permitted from the packet region of the smbd_response slab objects.
Further, do the same thing for smbd_request slab objects and their packet field.
--- fs/smb/client/smbdirect.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
fs/smb/client/smbdirect.c +15 -6
@@ -1476,12 +1476,17 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info)int rc;scnprintf (name,MAX_NAME_LEN ,"smbd_request_%p" , info); +struct kmem_cache_args request_args = { + .align = __alignof__(struct smbd_request ), + .useroffset = offsetof(struct smbd_request , packet), + .usersize =sizeof (struct smbdirect_data_transfer ), + }; info->request_cache =kmem_cache_create ( name,sizeof (struct smbd_request ) +sizeof (struct smbdirect_data_transfer ), -0 ,SLAB_HWCACHE_ALIGN ,NULL ); + &request_args,SLAB_HWCACHE_ALIGN );if (!info->request_cache)return -ENOMEM ;
@@ -1492,12 +1497,16 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info) goto out1;scnprintf (name,MAX_NAME_LEN ,"smbd_response_%p" , info); + +struct kmem_cache_args response_args = { + .align = __alignof__(struct smbd_response ), + .useroffset = offsetof(struct smbd_response , packet), + .usersize = sp->max_recv_size, + }; info->response_cache = -kmem_cache_create ( - name, -sizeof (struct smbd_response ) + - sp->max_recv_size, -0 ,SLAB_HWCACHE_ALIGN ,NULL ); +kmem_cache_create (name, +sizeof (struct smbd_response ) + sp->max_recv_size, + &response_args,SLAB_HWCACHE_ALIGN );if (!info->response_cache) goto out2;
Stefan Metzmacher
reviewerAm 25.06.25 um 15:37 schrieb David Howells:
Quoted 56 lines
The handling of received data in the smbdirect client code involves using
copy_to_iter()to copy data from thesmbd_reponsestruct's packet trailer to a folioq buffer provided by netfslib that encapsulates a chunk of pagecache.If, however,
CONFIG_HARDENED_USERCOPY=y, this will result in the checks then performed incopy_to_iter()oopsing with something like the following:CIFS: Attempting to mount //172.31.9.1/test
CIFS: VFS: RDMA transport established
usercopy: Kernel memory exposure attempt detected from SLUB object 'smbd_response_0000000091e24ea1' (offset 81, size 63)!------------[ cut here ]------------ kernel BUG at mm/usercopy.c:102! ... RIP: 0010:usercopy_abort+0x6c/0x80 ... Call Trace: <TASK> __check_heap_object+0xe3/0x120 __check_object_size+0x4dc/0x6d0 smbd_recv+0x77f/0xfe0 [cifs] cifs_readv_from_socket+0x276/0x8f0 [cifs] cifs_read_from_socket+0xcd/0x120 [cifs] cifs_demultiplex_thread+0x7e9/0x2d50 [cifs] kthread+0x396/0x830 ret_from_fork+0x2b8/0x3b0 ret_from_fork_asm+0x1a/0x30The problem is that the
smbd_responseslab's packet field isn't marked as being permitted for usercopy.Fix this by passing parameters to
kmem_slab_create()to indicate thatcopy_to_iter()is permitted from the packet region of thesmbd_responseslab objects.Further, do the same thing for
smbd_requestslab objects and their packet field.Fixes ee4cdf7ba857 ("netfs: Speed up buffered reading")Reported-by Stefan Metzmacher <[email protected]>Signed-off-by David Howells <[email protected]>cc Steve French <[email protected]>cc Paulo Alcantara <[email protected]>--- fs/smb/client/smbdirect.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)fs/smb/client/smbdirect.c +0
-0
fs/smb/client/smbdirect.c static int allocate_caches_and_workqueue(struct smbd_connection *info) · line 1482
Quoted 7 lines
fs/smb/client/smbdirect.c +4
-0@@ -1476,12 +1476,17 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info)int rc;scnprintf (name,MAX_NAME_LEN ,"smbd_request_%p" , info); +struct kmem_cache_args request_args = { + .align = __alignof__(struct smbd_request ), + .useroffset = offsetof(struct smbd_request , packet), + .usersize =sizeof (struct smbdirect_data_transfer ),
This looks wrong, the smbdirect_data_transfer header itself should be written by userspace.
So I guess we don't need this at all.
Quoted 12 lines
fs/smb/client/smbdirect.c +2
-1+ }; info->request_cache =kmem_cache_create ( name,sizeof (struct smbd_request ) +sizeof (struct smbdirect_data_transfer ), -0 ,SLAB_HWCACHE_ALIGN ,NULL ); + &request_args,SLAB_HWCACHE_ALIGN );if (!info->request_cache)return -ENOMEM ;
fs/smb/client/smbdirect.c static int allocate_caches_and_workqueue(struct smbd_connection *info) · line 1504
Quoted 8 lines
fs/smb/client/smbdirect.c +5
-0@@ -1492,12 +1497,16 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info) goto out1;scnprintf (name,MAX_NAME_LEN ,"smbd_response_%p" , info); + +struct kmem_cache_args response_args = { + .align = __alignof__(struct smbd_response ), + .useroffset = offsetof(struct smbd_response , packet), + .usersize = sp->max_recv_size,
This should be have + sizeof(struct smbdirect_data_transfer) for useroffset and - sizeof(struct smbdirect_data_transfer) for usersize
As the smbdirect_data_transfer header should not accessed by userspace.
My attempt looks like this:
- kmem_cache_create(
+ kmem_cache_create_usercopy(
name,
sizeof(struct smbd_response) +
sp->max_recv_size,
- 0, SLAB_HWCACHE_ALIGN, NULL); + __alignof__(struct smbd_response), + SLAB_HWCACHE_ALIGN, + /*
+ * only the payload should be exposed
+ */
+ offsetof(struct smbd_response, packet) +
+ sizeof(struct smbdirect_data_transfer),
+ sp->max_recv_size -
+ sizeof(struct smbdirect_data_transfer),
+ NULL);
But I noticed that kmem_cache_create_usercopy is a legacy wrapper.
David Howells
maintainerPATCH v2 1/1 cifs: Fix the smbd_request and smbd_reponse slabs to allow usercopy
In reply to David HowellsThe handling of received data in the smbdirect client code involves using copy_to_iter() to copy data from the smbd_reponse struct's packet trailer to a folioq buffer provided by netfslib that encapsulates a chunk of pagecache.
If, however, CONFIG_HARDENED_USERCOPY=y, this will result in the checks then performed in copy_to_iter() oopsing with something like the following:
CIFS: Attempting to mount //172.31.9.1/test
CIFS: VFS: RDMA transport established
usercopy: Kernel memory exposure attempt detected from SLUB object 'smbd_response_0000000091e24ea1' (offset 81, size 63)!
------------[ cut here ]------------ kernel BUG at mm/usercopy.c:102! ... RIP: 0010:usercopy_abort+0x6c/0x80 ... Call Trace: <TASK> __check_heap_object+0xe3/0x120 __check_object_size+0x4dc/0x6d0 smbd_recv+0x77f/0xfe0 [cifs] cifs_readv_from_socket+0x276/0x8f0 [cifs] cifs_read_from_socket+0xcd/0x120 [cifs] cifs_demultiplex_thread+0x7e9/0x2d50 [cifs] kthread+0x396/0x830 ret_from_fork+0x2b8/0x3b0 ret_from_fork_asm+0x1a/0x30
The problem is that the smbd_response slab's packet field isn't marked as being permitted for usercopy.
Fix this by passing parameters to kmem_slab_create() to indicate that copy_to_iter() is permitted from the packet region of the smbd_response slab objects, less the header space.
--- fs/smb/client/smbdirect.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
fs/smb/client/smbdirect.c +13 -5
@@ -1475,6 +1475,9 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info)char name[MAX_NAME_LEN ];int rc; +if (WARN_ON_ONCE (sp->max_recv_size <sizeof (struct smbdirect_data_transfer ))) +return -ENOMEM ; +scnprintf (name,MAX_NAME_LEN ,"smbd_request_%p" , info); info->request_cache = kmem_cache_create(
@@ -1492,12 +1495,17 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info) goto out1;scnprintf (name,MAX_NAME_LEN ,"smbd_response_%p" , info); + +struct kmem_cache_args response_args = { + .align = __alignof__(struct smbd_response ), + .useroffset = (offsetof(struct smbd_response , packet) + +sizeof (struct smbdirect_data_transfer )), + .usersize = sp->max_recv_size -sizeof (struct smbdirect_data_transfer ), + }; info->response_cache = -kmem_cache_create ( - name, -sizeof (struct smbd_response ) + - sp->max_recv_size, -0 ,SLAB_HWCACHE_ALIGN ,NULL ); +kmem_cache_create (name, +sizeof (struct smbd_response ) + sp->max_recv_size, + &response_args,SLAB_HWCACHE_ALIGN );if (!info->response_cache) goto out2;
Stefan Metzmacher
reviewerreviewed-by and tested-by: Stefan Metzmacher <[email protected]>
Am 25.06.25 um 16:46 schrieb David Howells:
Quoted 86 lines
The handling of received data in the smbdirect client code involves using
copy_to_iter()to copy data from thesmbd_reponsestruct's packet trailer to a folioq buffer provided by netfslib that encapsulates a chunk of pagecache.If, however,
CONFIG_HARDENED_USERCOPY=y, this will result in the checks then performed incopy_to_iter()oopsing with something like the following:CIFS: Attempting to mount //172.31.9.1/test
CIFS: VFS: RDMA transport established
usercopy: Kernel memory exposure attempt detected from SLUB object 'smbd_response_0000000091e24ea1' (offset 81, size 63)!------------[ cut here ]------------ kernel BUG at mm/usercopy.c:102! ... RIP: 0010:usercopy_abort+0x6c/0x80 ... Call Trace: <TASK> __check_heap_object+0xe3/0x120 __check_object_size+0x4dc/0x6d0 smbd_recv+0x77f/0xfe0 [cifs] cifs_readv_from_socket+0x276/0x8f0 [cifs] cifs_read_from_socket+0xcd/0x120 [cifs] cifs_demultiplex_thread+0x7e9/0x2d50 [cifs] kthread+0x396/0x830 ret_from_fork+0x2b8/0x3b0 ret_from_fork_asm+0x1a/0x30The problem is that the
smbd_responseslab's packet field isn't marked as being permitted for usercopy.Fix this by passing parameters to
kmem_slab_create()to indicate thatcopy_to_iter()is permitted from the packet region of thesmbd_responseslab objects, less the header space.Fixes ee4cdf7ba857 ("netfs: Speed up buffered reading")Reported-by Stefan Metzmacher <[email protected]>Signed-off-by David Howells <[email protected]>cc Steve French <[email protected]>cc Paulo Alcantara <[email protected]>--- fs/smb/client/smbdirect.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)fs/smb/client/smbdirect.c +13
-5@@ -1475,6 +1475,9 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info)char name[MAX_NAME_LEN ];int rc; +if (WARN_ON_ONCE (sp->max_recv_size <sizeof (struct smbdirect_data_transfer ))) +return -ENOMEM ; +scnprintf (name,MAX_NAME_LEN ,"smbd_request_%p" , info); info->request_cache = kmem_cache_create(@@ -1492,12 +1495,17 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info) goto out1;scnprintf (name,MAX_NAME_LEN ,"smbd_response_%p" , info); + +struct kmem_cache_args response_args = { + .align = __alignof__(struct smbd_response ), + .useroffset = (offsetof(struct smbd_response , packet) + +sizeof (struct smbdirect_data_transfer )), + .usersize = sp->max_recv_size -sizeof (struct smbdirect_data_transfer ), + }; info->response_cache = -kmem_cache_create ( - name, -sizeof (struct smbd_response ) + - sp->max_recv_size, -0 ,SLAB_HWCACHE_ALIGN ,NULL ); +kmem_cache_create (name, +sizeof (struct smbd_response ) + sp->max_recv_size, + &response_args,SLAB_HWCACHE_ALIGN );if (!info->response_cache) goto out2;
added to cifs-2.6.git for-next and added RB and tested-by
Quoted 92 lines · Stefan Metzmacher
reviewed-by and tested-by: Stefan Metzmacher <[email protected]>
Am 25.06.25 um 16:46 schrieb David Howells:
Quoted 86 lines
The handling of received data in the smbdirect client code involves using
copy_to_iter()to copy data from thesmbd_reponsestruct's packet trailer to a folioq buffer provided by netfslib that encapsulates a chunk of pagecache.If, however,
CONFIG_HARDENED_USERCOPY=y, this will result in the checks then performed incopy_to_iter()oopsing with something like the following:CIFS: Attempting to mount //172.31.9.1/test
CIFS: VFS: RDMA transport established
usercopy: Kernel memory exposure attempt detected from SLUB object 'smbd_response_0000000091e24ea1' (offset 81, size 63)!------------[ cut here ]------------ kernel BUG at mm/usercopy.c:102! ... RIP: 0010:usercopy_abort+0x6c/0x80 ... Call Trace: <TASK> __check_heap_object+0xe3/0x120 __check_object_size+0x4dc/0x6d0 smbd_recv+0x77f/0xfe0 [cifs] cifs_readv_from_socket+0x276/0x8f0 [cifs] cifs_read_from_socket+0xcd/0x120 [cifs] cifs_demultiplex_thread+0x7e9/0x2d50 [cifs] kthread+0x396/0x830 ret_from_fork+0x2b8/0x3b0 ret_from_fork_asm+0x1a/0x30The problem is that the
smbd_responseslab's packet field isn't marked as being permitted for usercopy.Fix this by passing parameters to
kmem_slab_create()to indicate thatcopy_to_iter()is permitted from the packet region of thesmbd_responseslab objects, less the header space.Fixes ee4cdf7ba857 ("netfs: Speed up buffered reading")Reported-by Stefan Metzmacher <[email protected]>Signed-off-by David Howells <[email protected]>cc Steve French <[email protected]>cc Paulo Alcantara <[email protected]>--- fs/smb/client/smbdirect.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)fs/smb/client/smbdirect.c +13
-5@@ -1475,6 +1475,9 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info)char name[MAX_NAME_LEN ];int rc; +if (WARN_ON_ONCE (sp->max_recv_size <sizeof (struct smbdirect_data_transfer ))) +return -ENOMEM ; +scnprintf (name,MAX_NAME_LEN ,"smbd_request_%p" , info); info->request_cache = kmem_cache_create(@@ -1492,12 +1495,17 @@ static int allocate_caches_and_workqueue(struct smbd_connection *info) goto out1;scnprintf (name,MAX_NAME_LEN ,"smbd_response_%p" , info); + +struct kmem_cache_args response_args = { + .align = __alignof__(struct smbd_response ), + .useroffset = (offsetof(struct smbd_response , packet) + +sizeof (struct smbdirect_data_transfer )), + .usersize = sp->max_recv_size -sizeof (struct smbdirect_data_transfer ), + }; info->response_cache = -kmem_cache_create ( - name, -sizeof (struct smbd_response ) + - sp->max_recv_size, -0 ,SLAB_HWCACHE_ALIGN ,NULL ); +kmem_cache_create (name, +sizeof (struct smbd_response ) + sp->max_recv_size, + &response_args,SLAB_HWCACHE_ALIGN );if (!info->response_cache) goto out2;
Signature · 3 lines
Thanks, Steve