NFSv4.2: fix listxattr maximum XDR buffer size
Subsystems (2)
PATCH 1/1 NFSv4.2: fix listxattr maximum XDR buffer size
Switch order of operations to avoid creating a short XDR buffer:
e.g., buflen = 12, old xdrlen = 12, new xdrlen = 20.
Having a short XDR buffer leads to lxa_maxcount be a few bytes less than what is needed to retrieve the whole list when using a buflen as returned by a call with size = 0:
buflen = listxattr(path, NULL, 0);
buf = malloc(buflen);
buflen = listxattr(path, buf, buflen);For a file with one attribute (name = '123456'), the first call with size = 0 will return buflen = 12 ('user.123456\x00'). The second call with size = 12, sends LISTXATTRS with lxa_maxcount = 12 + 8 (cookie) + 4 (array count) = 24. The XDR buffer needs 8 (cookie) + 4 (array count) + 4 (name count)
+ 6 (name len) + 2 (padding) + 4 (eof) = 28 which is 4 bytes shorter than the lxa_maxcount provided in the call.
--- fs/nfs/nfs42.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfs/nfs42.h +5 -2
@@ -55,11 +55,14 @@ int nfs42_proc_removexattr(struct inode *inode, const char *name);* They would be 7 bytes long in the eventual buffer ("user.x\0"), and * 8 bytes long XDR-encoded. * -* Include the trailing eof word as well. +* Include the trailing eof word as well and make the result a multiple +* of 4 bytes. */ static inline u32 nfs42_listxattr_xdrsize (u32 buflen) { -return ((buflen / (XATTR_USER_PREFIX_LEN +2 )) *8 ) +4 ; +u32 size =8 * buflen / (XATTR_USER_PREFIX_LEN +2 ) +4 ; + +return (size +3 ) & ~3 ; }#endif /* CONFIG_NFS_V4_2 */ #endif /* __LINUX_FS_NFS_NFS4_2_H */
Signature · 1 line
2.43.0
Quoted 32 lines · Jorge Mora
Switch order of operations to avoid creating a short XDR buffer:
e.g., buflen = 12, old xdrlen = 12, new xdrlen = 20.Having a short XDR buffer leads to
lxa_maxcountbe a few bytes less than what is needed to retrieve the whole list when using a buflen as returned by a call with size = 0:buflen = listxattr(path, NULL, 0); buf = malloc(buflen); buflen = listxattr(path, buf, buflen);For a file with one attribute (name = '123456'), the first call with size = 0 will return buflen = 12 ('user.123456\x00'). The second call with size = 12, sends LISTXATTRS with
lxa_maxcount= 12 + 8 (cookie) + 4 (array count) = 24. The XDR buffer needs 8 (cookie) + 4 (array count) + 4 (name count)
+ 6 (name len) + 2 (padding) + 4 (eof) = 28 which is 4 bytes shorter than thelxa_maxcountprovided in the call.Fixes 04a5da690e8f ("NFSv4.2: define limits and sizes for user xattr handling")Signed-off-by Jorge Mora <[email protected]>--- fs/nfs/nfs42.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/nfs/nfs42.h +0
-0@@ -55,11 +55,14 @@ int nfs42_proc_removexattr(struct inode *inode, const char *name);* They would be 7 bytes long in the eventual buffer ("user.x\0"), and * 8 bytes long XDR-encoded. *
fs/nfs/nfs42.h int nfs42_proc_removexattr(struct inode *inode, const char *name); · line 65
Quoted 10 lines
fs/nfs/nfs42.h +5
-2-* Include the trailing eof word as well. +* Include the trailing eof word as well and make the result a multiple +* of 4 bytes. */ static inline u32 nfs42_listxattr_xdrsize (u32 buflen) { -return ((buflen / (XATTR_USER_PREFIX_LEN +2 )) *8 ) +4 ; +u32 size =8 * buflen / (XATTR_USER_PREFIX_LEN +2 ) +4 ; + +return (size +3 ) & ~3 ;
Maybe XDR_QUADLEN(size) here? Otherwise, looks correct.
Ben
A message that is not in the archive (sent off-list or not yet received): CAG7w-ioC8VkBFWqcaCw1S7YM-riNQKNUeu2-oUB4CpaimzB=7g@mail.gmail.com
fs/nfs/nfs42.h int nfs42_proc_removexattr(struct inode *inode, const char *name);
Quoted 10 lines · Jorge Mora
Thanks Ben,
Do you want me to resubmit the patch using
XDR_QUALEN? Something like this?static inline u32
nfs42_listxattr_xdrsize(u32 buflen){fs/nfs/nfs42.h +3
-1-return ((buflen / (XATTR_USER_PREFIX_LEN +2 )) *8 ) +4 ; +u32 size =8 * buflen / (XATTR_USER_PREFIX_LEN +2 ) +4 ; + +return XDR_QUADLEN (size) <<2 ;
I just thought I'd mention it since it's used all over. I think your version is just fine.
Ben