From mboxrd@z Thu Jan 1 00:00:00 1970 From: Harsh Bora Subject: Re: [PATCH] Typecasting required for comparing unlike datatypes Date: Fri, 10 Dec 2010 12:09:42 +0530 Message-ID: <4D01CB2E.4000205@linux.vnet.ibm.com> References: <1291812900-18311-1-git-send-email-harsh@linux.vnet.ibm.com> <20101210095336.a3f33d55.kamezawa.hiroyu@jp.fujitsu.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: linux-fsdevel@vger.kernel.org, fengguang.wu@intel.com, aneesh.kumar@linux.vnet.ibm.com, jvrao@linux.vnet.ibm.com To: KAMEZAWA Hiroyuki Return-path: Received: from e23smtp06.au.ibm.com ([202.81.31.148]:35747 "EHLO e23smtp06.au.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753409Ab0LJGjr (ORCPT ); Fri, 10 Dec 2010 01:39:47 -0500 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [202.81.31.245]) by e23smtp06.au.ibm.com (8.14.4/8.13.1) with ESMTP id oBA6dYIE029235 for ; Fri, 10 Dec 2010 17:39:34 +1100 Received: from d23av01.au.ibm.com (d23av01.au.ibm.com [9.190.234.96]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id oBA6djoo1466530 for ; Fri, 10 Dec 2010 17:39:45 +1100 Received: from d23av01.au.ibm.com (loopback [127.0.0.1]) by d23av01.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id oBA6dix0003277 for ; Fri, 10 Dec 2010 17:39:45 +1100 In-Reply-To: <20101210095336.a3f33d55.kamezawa.hiroyu@jp.fujitsu.com> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: On 12/10/2010 06:23 AM, KAMEZAWA Hiroyuki wrote: > On Wed, 8 Dec 2010 18:25:00 +0530 > Harsh Prateek Bora wrote: > >> The existing code causes the if condition to pass when it should fail >> on a *64-bit kernel* because of implicit data type conversions. It can >> be observed by passing pos = -1 and count = some positive number. >> This results in function returning EOVERFLOW instead of EINVAL. >> >> With this patch, the function returns EINVAL when pos is -1 and count >> is a positive number. This can be tested by calling sendfile with >> offset = -1 and count = some positive number on a 64-bit kernel. >> > > Hmm, is this clearer ? > > == > > commit 4a3956c790290efeb647bbb0c3a90476bb57800e adds support for > negative (unsigned) page offset for very large files as /proc//mem > and /dev/mem. > > In that patch, overlap check routine is added but it was wrong. > > Considering 'pos' is loff_t, a signed value, > > In usual case, at comparing 'pos' and 'pos+count' > > (positive) / (positive) OK > (positive) / (nevative) EOVERFLOW > (negative) / (positive) EINVAL > (negative) / (negative) EINVAL > > In FMODE_UNSIGNED_OFFSET case, > > (positive) / (positive) OK > (positive) / (nevative) OK (ex. 0x7fff -> 0x8000) > (nevative) / (negative) OK > (negative) / (positive) EOVERFLOW (ex. 0xffff -> 0x1) > > Signed-off-by: KAMEZAWA Hiroyuki > > --- > fs/read_write.c | 21 +++++++++++++++++---- > 1 file changed, 17 insertions(+), 4 deletions(-) > > Index: linux-2.6.37-rc5/fs/read_write.c > =================================================================== > --- linux-2.6.37-rc5.orig/fs/read_write.c > +++ linux-2.6.37-rc5/fs/read_write.c > @@ -37,11 +37,24 @@ __negative_fpos_check(struct file *file, > * pos or pos+count is negative here, check overflow. > * too big "count" will be caught in rw_verify_area(). > */ > - if ((pos< 0)&& (pos + count< pos)) > + /* negative pos is allowed only when the flag is set */ > + if (!(file->f_mode& FMODE_UNSIGNED_OFFSET)) { > + if ((pos> 0)&& (pos + count> 0)) > + return 0; > + if ((pos> 0)&& (pos + count< 0)) > + return -EOVERFLOW; > + return -EINVAL; > + } > + /* > + * The file supports 'unsigned long' offset. (but loff_t is signed) > + * When pos is negative, -1 is the biggest number. So if pos + count > + * is larger than pos, it's overflow. > + * (ex) -1 + 10 = 9 ...means > + * 0xffff + 0xa = 0x9 => overflow. > + */ > + if ((pos< 0)&& (pos + count> 0)) Well, that works fine for what I am concerned but I think there is a mismatch in the code and the comment above. As per the comments above, it should be like: if ((pos < 0) && (pos + count > pos)) Regards, Harsh. > return -EOVERFLOW; > - if (file->f_mode& FMODE_UNSIGNED_OFFSET) > - return 0; > - return -EINVAL; > + return 0; > } > > /** > > -- > To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html