fs/nilfs2: copy userspace-array safely
PATCH 1/1 fs/nilfs2: copy userspace-array safely
ioctl.c utilizes memdup_user() to copy a userspace array. This is done without an overflow-check.
Use the new wrapper memdup_array_user() to copy the array more safely.
---
Linus recently merged this new wrapper for Kernel v6.7
--- fs/nilfs2/ioctl.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/nilfs2/ioctl.c +4 -4
@@ -877,11 +877,11 @@ static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,/* * argv[4] points to segment numbers this ioctl cleans. We -* use kmalloc() for its buffer because memory used for the -* segment numbers is enough small. +* use kmalloc() for its buffer because the memory used for the +* segment numbers is small enough. */ - kbufs[4 ] =memdup_user ((void __user *)(unsigned long )argv[4 ].v_base, - nsegs *sizeof (__u64)); + kbufs[4 ] =memdup_array_user ((void __user *)(unsigned long )argv[4 ].v_base, + nsegs,sizeof (__u64));if (IS_ERR (kbufs[4 ])) { ret =PTR_ERR (kbufs[4 ]); goto out;
Signature · 1 line
2.41.0
Ryusuke Konishi
maintainerQuoted 10 lines · Philipp Stanner
ioctl.cutilizesmemdup_user()to copy a userspace array. This is done without an overflow-check.Use the new wrapper
memdup_array_user()to copy the array more safely.Suggested-by Dave Airlie <[email protected]>Signed-off-by Philipp Stanner <[email protected]>---
Linus recently merged this new wrapper for Kernel v6.7
The following overflow check is performed just before the usage of memdup_user():
if (nsegs > UINT_MAX / sizeof(__u64))
goto out;This was introduced by commit 1ecd3c7ea76488 ("nilfs2: avoid overflowing segment numbers in nilfs_ioctl_clean_segments()") to avoid overflowing nsegs * sizeof(__u64) in the subsequent call to memdup_user().
I learned about memdup_array_user() this time, and it seems to check for overflow when multiplying two size_t arguments (i.e. the number of elements and size of the array to be copied).
Since size_t is 32-bit or 64-bit depending on the architecture, I think the overflow check that memdup_array_user() does is included in the above upper limit check by UINT_MAX.
So, for security reasons, I don't think this change is necessary. (Am I missing something?)
In terms of cleanup, I think the clarification this patch brings is good, but in that case, I'm concerned about the duplication of overflow checks.
Thanks,
Ryusuke Konishi
Quoted 27 lines
--- fs/nilfs2/ioctl.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)fs/nilfs2/ioctl.c +4
-5@@ -877,11 +877,11 @@ static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,/* * argv[4] points to segment numbers this ioctl cleans. We -* use kmalloc() for its buffer because memory used for the -* segment numbers is enough small. +* use kmalloc() for its buffer because the memory used for the +* segment numbers is small enough. */ - kbufs[4 ] =memdup_user ((void __user *)(unsigned long )argv[4 ].v_base, - nsegs *sizeof (__u64)); + kbufs[4 ] =memdup_array_user ((void __user *)(unsigned long )argv[4 ].v_base, + nsegs,sizeof (__u64));if (IS_ERR (kbufs[4 ])) { ret =PTR_ERR (kbufs[4 ]); goto out; --2.41.0
Quoted 31 lines · Ryusuke Konishi
Quoted 5 lines · Philipp Stanner
ioctl.cutilizesmemdup_user()to copy a userspace array. This is done
without an overflow-check.Quoted 7 lines
Use the new wrapper
memdup_array_user()to copy the array more safely.Suggested-by Dave Airlie <[email protected]>Signed-off-by Philipp Stanner <[email protected]>---
Linus recently merged this new wrapper for Kernel v6.7The following overflow check is performed just before the usage of
memdup_user():if (nsegs >
UINT_MAX/sizeof(__u64))
goto out;This was introduced by commit
1ecd3c7ea76488("nilfs2: avoid overflowing segment numbers innilfs_ioctl_clean_segments()") to avoid
overflowing nsegs *sizeof(__u64)in the subsequent call tomemdup_user().I learned about
memdup_array_user()this time, and it seems to check for overflow when multiplying twosize_targuments (i.e. the number of
elements and size of the array to be copied).
Quoted 7 lines
Since
size_tis 32-bit or 64-bit depending on the architecture, I think the overflow check thatmemdup_array_user()does is included in the above upper limit check byUINT_MAX.So, for security reasons, I don't think this change is necessary. (Am
I missing something?)
No, I think you are right. My commit message was very generic – it's more about unifying array-duplication.
I should rephrase it.
Quoted 4 lines
In terms of cleanup, I think the clarification this patch brings is good, but in that case, I'm concerned about the duplication of overflow checks.
Alright, so would you prefer a patch that uses memdup_array_user() and, consequently, removes the preceding check?
Regards,
P.
Quoted 36 lines
Thanks,
Ryusuke KonishiQuoted 31 lines
---
fs/nilfs2/ioctl.c | 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)fs/nilfs2/ioctl.c +1
-2@@ -877,11 +877,11 @@ static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp, /** argv[4] points to segment numbers this ioctl cleans. We -* use kmalloc() for its buffer because memory used for the -* segment numbers is enough small. +* use kmalloc() for its buffer because the memory used for the
+ * segment numbers is small enough.
*/
- kbufs[4] = memdup_user((void__user*)(unsigned long)argv[4].v_base,
- nsegs *sizeof(__u64)); + kbufs[4] = memdup_array_user((void__user*)(unsigned long)argv[4].v_base,
+ nsegs,sizeof(__u64)); if (IS_ERR(kbufs[4])) {
ret =PTR_ERR(kbufs[4]);
goto out;
--
2.41.0
Ryusuke Konishi
maintainerQuoted 50 lines · Philipp Stanner
Quoted 31 lines · Ryusuke Konishi
Quoted 5 lines · Philipp Stanner
ioctl.cutilizesmemdup_user()to copy a userspace array. This is done
without an overflow-check.Quoted 7 lines
Use the new wrapper
memdup_array_user()to copy the array more safely.Suggested-by Dave Airlie <[email protected]>Signed-off-by Philipp Stanner <[email protected]>---
Linus recently merged this new wrapper for Kernel v6.7The following overflow check is performed just before the usage of
memdup_user():if (nsegs > UINT_MAX / sizeof(__u64)) goto out;This was introduced by commit
1ecd3c7ea76488("nilfs2: avoid overflowing segment numbers innilfs_ioctl_clean_segments()") to avoid
overflowing nsegs *sizeof(__u64)in the subsequent call tomemdup_user().I learned about
memdup_array_user()this time, and it seems to check for overflow when multiplying twosize_targuments (i.e. the number of
elements and size of the array to be copied).Quoted 7 lines
Since
size_tis 32-bit or 64-bit depending on the architecture, I think the overflow check thatmemdup_array_user()does is included in the above upper limit check byUINT_MAX.So, for security reasons, I don't think this change is necessary. (Am
I missing something?)No, I think you are right. My commit message was very generic – it's more about unifying array-duplication.
I should rephrase it.Quoted 4 lines
In terms of cleanup, I think the clarification this patch brings is good, but in that case, I'm concerned about the duplication of overflow checks.
Quoted 5 lines
Alright, so would you prefer a patch that uses
memdup_array_user()and, consequently, removes the preceding check?Regards,
P.
Yeah. If you could revise it as a cleanup patch, I would like to adopt it for the next cycle.
Regards,
Ryusuke Konishi