From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754947AbbHEW2L (ORCPT ); Wed, 5 Aug 2015 18:28:11 -0400 Received: from mx2.suse.de ([195.135.220.15]:33050 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753761AbbHEW2J (ORCPT ); Wed, 5 Aug 2015 18:28:09 -0400 Date: Thu, 6 Aug 2015 08:27:55 +1000 From: NeilBrown To: Tejun Heo , Greg Kroah-Hartman Cc: lkml Subject: [PATCH] sysfs: correctly handle short reads on PREALLOC attrs. Message-ID: <20150806082755.0b121304@noble> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.28; x86_64-suse-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which ignores the 'count' arg. So a 1-byte read request can return more bytes than that. This is seen with the 'dash' shell when 'read' is used on some 'md' sysfs attributes. So only return the 'min' of count and the attribute length. Signed-off-by: NeilBrown --- Possibly kernfs_file_direct_read should be changed instead (or as well) to use the min of 'count' and the return value from ops->read() Thanks, NeilBrown diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index 6c95628ea377..f35523d4fa3a 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -108,6 +108,7 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, { const struct sysfs_ops *ops = sysfs_file_ops(of->kn); struct kobject *kobj = of->kn->parent->priv; + size_t len; /* * If buf != of->prealloc_buf, we don't know how @@ -115,7 +116,8 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, */ if (pos || WARN_ON_ONCE(buf != of->prealloc_buf)) return 0; - return ops->show(kobj, of->kn->priv, buf); + len = ops->show(kobj, of->kn->priv, buf); + return min(count, len); } /* kernfs write callback for regular sysfs files */