From patchwork Tue Feb 18 07:41:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Macpaul Lin X-Patchwork-Id: 11387905 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8830413A4 for ; Tue, 18 Feb 2020 07:41:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 67BEB21D7D for ; Tue, 18 Feb 2020 07:41:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="gV+vQumo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726154AbgBRHlY (ORCPT ); Tue, 18 Feb 2020 02:41:24 -0500 Received: from mailgw02.mediatek.com ([210.61.82.184]:60387 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726104AbgBRHlY (ORCPT ); Tue, 18 Feb 2020 02:41:24 -0500 X-UUID: d408accb050345269bf4092c32b8eabd-20200218 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=R5o8h58rYOA1fQLNSXIwPhnCU0xALWjQ5mRQdByDjFk=; b=gV+vQumofRgeHZQLi18ZL0fXI0cOe/m8m9jHQK1lAAwIwiikMv2ILEXat/eyhU0IKRcshFL0QWupA8xNmwG/j707T7GFs7L/sAdvYuq6fi0fCOzpXQSn9nB2mR/p9Wky/ggJ7CRWDs0fdL5mb+PPO+/bq/uHzSDFyXvILobmlKU=; X-UUID: d408accb050345269bf4092c32b8eabd-20200218 Received: from mtkexhb01.mediatek.inc [(172.21.101.102)] by mailgw02.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 1173835655; Tue, 18 Feb 2020 15:41:16 +0800 Received: from mtkcas09.mediatek.inc (172.21.101.178) by mtkmbs08n1.mediatek.inc (172.21.101.55) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Tue, 18 Feb 2020 15:42:30 +0800 Received: from mtkswgap22.mediatek.inc (172.21.77.33) by mtkcas09.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Tue, 18 Feb 2020 15:40:51 +0800 From: Macpaul Lin To: Alexander Viro , Matthias Brugger , Shen Jing , Sasha Levin , John Stultz , Macpaul Lin , Andrzej Pietrasiewicz , Vincent Pelletier , Jerry Zhang , , , , CC: Mediatek WSD Upstream , CC Hwang , Loda Chou Subject: [PATCH] lib: iov_iter.c: fix a possible calculation error on remaining bytes Date: Tue, 18 Feb 2020 15:41:12 +0800 Message-ID: <1582011672-17189-1-git-send-email-macpaul.lin@mediatek.com> X-Mailer: git-send-email 1.7.9.5 MIME-Version: 1.0 X-MTK: N Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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 --- 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; \ } #define iterate_kvec(i, n, __v, __p, skip, STEP) { \