diff mbox series

lib: iov_iter.c: fix a possible calculation error on remaining bytes

Message ID 1582011672-17189-1-git-send-email-macpaul.lin@mediatek.com (mailing list archive)
State New, archived
Headers show
Series lib: iov_iter.c: fix a possible calculation error on remaining bytes | expand

Commit Message

Macpaul Lin Feb. 18, 2020, 7:41 a.m. UTC
This issue was found when adbd trying to open functionfs with AIO mode.
Usually, we need to set "setprop sys.usb.ffs.aio_compat 0" to enable
adbd with AIO mode on Android.

When adbd is opening functionfs, it will try to read 24 bytes at the
fisrt read I/O control. If this reading has been failed, adbd will
try to send FUNCTIONFS_CLEAR_HALT to functionfs. When adbd is in AIO
mode, functionfs will be acted with asyncronized I/O path. After the
successful read transfer has been completed by gadget hardware, the
following series of functions will be called.
  ffs_epfile_async_io_complete() -> ffs_user_copy_worker() ->
    copy_to_iter() -> _copy_to_iter() -> copyout() ->
    iterate_and_advance() -> iterate_iovec()

Adding debug trace to these functions, it has been found that in
iterate_iovec(), the calculation result of n will be turned into zero.
   n = wanted - n; /* 0 == n = 24 - 24; */
Which causes copyout() won't copy data to userspace since the length
to be copied "v.iov_len" will be zero, which isn't correct. This also
leads ffs_copy_to_iter() always return -EFAULT. Finally adbd cannot
open functionfs and send FUNCTIONFS_CLEAR_HALT.

Signed-off-by: Macpaul Lin <macpaul.lin@mediatek.com>
---
 lib/iov_iter.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Al Viro Feb. 18, 2020, 12:41 p.m. UTC | #1
On Tue, Feb 18, 2020 at 03:41:12PM +0800, Macpaul Lin wrote:
> This issue was found when adbd trying to open functionfs with AIO mode.
> Usually, we need to set "setprop sys.usb.ffs.aio_compat 0" to enable
> adbd with AIO mode on Android.
> 
> When adbd is opening functionfs, it will try to read 24 bytes at the
> fisrt read I/O control. If this reading has been failed, adbd will
> try to send FUNCTIONFS_CLEAR_HALT to functionfs. When adbd is in AIO
> mode, functionfs will be acted with asyncronized I/O path. After the
> successful read transfer has been completed by gadget hardware, the
> following series of functions will be called.
>   ffs_epfile_async_io_complete() -> ffs_user_copy_worker() ->
>     copy_to_iter() -> _copy_to_iter() -> copyout() ->
>     iterate_and_advance() -> iterate_iovec()
> 
> Adding debug trace to these functions, it has been found that in
> iterate_iovec(), the calculation result of n will be turned into zero.
>    n = wanted - n; /* 0 == n = 24 - 24; */
> Which causes copyout() won't copy data to userspace since the length
> to be copied "v.iov_len" will be zero, which isn't correct. This also
> leads ffs_copy_to_iter() always return -EFAULT. Finally adbd cannot
> open functionfs and send FUNCTIONFS_CLEAR_HALT.
> 
> Signed-off-by: Macpaul Lin <macpaul.lin@mediatek.com>
> ---
>  lib/iov_iter.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/iov_iter.c b/lib/iov_iter.c
> index fb29c02c6a3c..f9334144e259 100644
> --- a/lib/iov_iter.c
> +++ b/lib/iov_iter.c
> @@ -36,7 +36,8 @@
>  		skip = __v.iov_len;			\
>  		n -= __v.iov_len;			\
>  	}						\
> -	n = wanted - n;					\
> +	if (n != wanted)				\
> +		n = wanted - n;				\
>  }

	First of all, nothing in that line can possibly *cause*
copyout() to do anything - it's after the calls of step.  What's
more, this changes behaviour only when wanted would've been equal to
n, doesn't it?  Which translates into "no decrements of n have
happened at all", i.e. "nothing has been copied".  IOW, it's
a consequence of no copyout, not the cause of such.  You can
make copy_to_iter() lie and pretend if has copied everything
when it has copied nothing, but that won't change the underlying
bug.

	So I'm afraid your debugging is not finished - you
still need to find out what causes the copyout failures and/or
BS iov_iter padded by caller.
Macpaul Lin Feb. 23, 2020, 1:15 p.m. UTC | #2
On Tue, 2020-02-18 at 12:41 +0000, Al Viro wrote:
> On Tue, Feb 18, 2020 at 03:41:12PM +0800, Macpaul Lin wrote:
> > This issue was found when adbd trying to open functionfs with AIO mode.
> > Usually, we need to set "setprop sys.usb.ffs.aio_compat 0" to enable
> > adbd with AIO mode on Android.
> > 
> > When adbd is opening functionfs, it will try to read 24 bytes at the
> > fisrt read I/O control. If this reading has been failed, adbd will
> > try to send FUNCTIONFS_CLEAR_HALT to functionfs. When adbd is in AIO
> > mode, functionfs will be acted with asyncronized I/O path. After the
> > successful read transfer has been completed by gadget hardware, the
> > following series of functions will be called.
> >   ffs_epfile_async_io_complete() -> ffs_user_copy_worker() ->
> >     copy_to_iter() -> _copy_to_iter() -> copyout() ->
> >     iterate_and_advance() -> iterate_iovec()
> > 
> > Adding debug trace to these functions, it has been found that in
> > iterate_iovec(), the calculation result of n will be turned into zero.
> >    n = wanted - n; /* 0 == n = 24 - 24; */
> > Which causes copyout() won't copy data to userspace since the length
> > to be copied "v.iov_len" will be zero, which isn't correct. This also
> > leads ffs_copy_to_iter() always return -EFAULT. Finally adbd cannot
> > open functionfs and send FUNCTIONFS_CLEAR_HALT.
> > 
> > Signed-off-by: Macpaul Lin <macpaul.lin@mediatek.com>
> > ---
> >  lib/iov_iter.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/lib/iov_iter.c b/lib/iov_iter.c
> > index fb29c02c6a3c..f9334144e259 100644
> > --- a/lib/iov_iter.c
> > +++ b/lib/iov_iter.c
> > @@ -36,7 +36,8 @@
> >  		skip = __v.iov_len;			\
> >  		n -= __v.iov_len;			\
> >  	}						\
> > -	n = wanted - n;					\
> > +	if (n != wanted)				\
> > +		n = wanted - n;				\
> >  }
> 
> 	First of all, nothing in that line can possibly *cause*
> copyout() to do anything - it's after the calls of step.  What's
> more, this changes behaviour only when wanted would've been equal to
> n, doesn't it?  Which translates into "no decrements of n have
> happened at all", i.e. "nothing has been copied".  IOW, it's
> a consequence of no copyout, not the cause of such.  You can
> make copy_to_iter() lie and pretend if has copied everything
> when it has copied nothing, but that won't change the underlying
> bug.
> 
> 	So I'm afraid your debugging is not finished - you
> still need to find out what causes the copyout failures and/or
> BS iov_iter padded by caller.

Thanks for your explanation. After these days, I've found this issue
only happened on arm 64-bit system, while arm 32-bit system works well.
After more debugging, it looks like arm 64 tagged ABI related issue.
I've update a new patch here: 
    usb: gadget: f_fs: try to fix AIO issue under ARM 64 bit TAGGED mode
    https://lkml.org/lkml/2020/2/23/16.

Thanks.
Macpaul Lin.
diff mbox series

Patch

diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index fb29c02c6a3c..f9334144e259 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -36,7 +36,8 @@ 
 		skip = __v.iov_len;			\
 		n -= __v.iov_len;			\
 	}						\
-	n = wanted - n;					\
+	if (n != wanted)				\
+		n = wanted - n;				\
 }
 
 #define iterate_kvec(i, n, __v, __p, skip, STEP) {	\