From mboxrd@z Thu Jan 1 00:00:00 1970 From: akpm@linux-foundation.org Subject: [patch 04/12] libfs: make simple_read_from_buffer conventional Date: Thu, 06 Aug 2009 16:10:10 -0700 Message-ID: <200908062310.n76NAAPF012855@imap1.linux-foundation.org> Cc: linux-fsdevel@vger.kernel.org, akpm@linux-foundation.org, rostedt@goodmis.org, hpa@zytor.com, mingo@elte.hu, srostedt@redhat.com, tytso@mit.edu To: viro@zeniv.linux.org.uk Return-path: Received: from smtp1.linux-foundation.org ([140.211.169.13]:60386 "EHLO smtp1.linux-foundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932208AbZHFXTN (ORCPT ); Thu, 6 Aug 2009 19:19:13 -0400 Sender: linux-fsdevel-owner@vger.kernel.org List-ID: From: Steven Rostedt Impact: have simple_read_from_buffer conform to standards It was brought to my attention by Andrew Morton, Theodore Tso, and H. Peter Anvin that a read from userspace should only return -EFAULT if nothing was actually read. Looking at the simple_read_from_buffer I noticed that this function does not conform to that rule. This patch fixes that function. [akpm@linux-foundation.org: simplification suggested by hpa] [hpa@zytor.com: fix count==0 handling] Signed-off-by: Steven Rostedt Cc: Al Viro Cc: Theodore Ts'o Cc: Ingo Molnar Signed-off-by: H. Peter Anvin Signed-off-by: Andrew Morton --- fs/libfs.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff -puN fs/libfs.c~libfs-make-simple_read_from_buffer-conventional fs/libfs.c --- a/fs/libfs.c~libfs-make-simple_read_from_buffer-conventional +++ a/fs/libfs.c @@ -527,14 +527,18 @@ ssize_t simple_read_from_buffer(void __u const void *from, size_t available) { loff_t pos = *ppos; + size_t ret; + if (pos < 0) return -EINVAL; - if (pos >= available) + if (pos >= available || !count) return 0; if (count > available - pos) count = available - pos; - if (copy_to_user(to, from + pos, count)) + ret = copy_to_user(to, from + pos, count); + if (ret == count) return -EFAULT; + count -= ret; *ppos = pos + count; return count; } _