Linux block layer
 help / color / mirror / Atom feed
From: Dmitry Monakhov <dmonakhov@openvz.org>
To: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org,
	martin.petersen@oracle.com
Cc: Dmitry Monakhov <dmonakhov@openvz.org>
Subject: [PATCH 8/8] tcm_fileio: Prevent information leak for short reads
Date: Thu, 30 Mar 2017 17:49:36 +0400	[thread overview]
Message-ID: <1490881776-28735-9-git-send-email-dmonakhov@openvz.org> (raw)
In-Reply-To: <1490881776-28735-1-git-send-email-dmonakhov@openvz.org>

If we failed to read data from backing file (probably because some one
truncate file under us), we must zerofill cmd's data, otherwise it will
be returned as is. Most likely cmd's data are unitialized pages from
page cache. This result in information leak.

xfstests: generic/420
http://marc.info/?l=linux-scsi&m=149087996913448&w=2

Signed-off-by: Dmitry Monakhov <dmonakhov@openvz.org>
---
 drivers/target/target_core_file.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/drivers/target/target_core_file.c b/drivers/target/target_core_file.c
index 87aa376..d69908d 100644
--- a/drivers/target/target_core_file.c
+++ b/drivers/target/target_core_file.c
@@ -277,12 +277,11 @@ static int fd_do_rw(struct se_cmd *cmd, struct file *fd,
 	else
 		ret = vfs_iter_read(fd, &iter, &pos);
 
-	kfree(bvec);
-
 	if (is_write) {
 		if (ret < 0 || ret != data_length) {
 			pr_err("%s() write returned %d\n", __func__, ret);
-			return (ret < 0 ? ret : -EINVAL);
+			if (ret >= 0)
+				ret = -EINVAL;
 		}
 	} else {
 		/*
@@ -295,17 +294,27 @@ static int fd_do_rw(struct se_cmd *cmd, struct file *fd,
 				pr_err("%s() returned %d, expecting %u for "
 						"S_ISBLK\n", __func__, ret,
 						data_length);
-				return (ret < 0 ? ret : -EINVAL);
+				if (ret >= 0)
+					ret = -EINVAL;
 			}
 		} else {
 			if (ret < 0) {
 				pr_err("%s() returned %d for non S_ISBLK\n",
 						__func__, ret);
-				return ret;
+			} else if (ret != data_length) {
+				/*
+				 * Short read case:
+				 * Probably some one truncate file under us.
+				 * We must explicitly zero sg-pages to prevent
+				 * expose uninizialized pages to userspace.
+				 */
+				BUG_ON(ret > data_length);
+				ret += iov_iter_zero(data_length - ret, &iter);
 			}
 		}
 	}
-	return 1;
+	kfree(bvec);
+	return ret;
 }
 
 static sense_reason_t
-- 
2.9.3

      parent reply	other threads:[~2017-03-30 13:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-30 13:49 [PATCH 0/8] block: T10/DIF Fixes and cleanups Dmitry Monakhov
2017-03-30 13:49 ` [PATCH 1/8] Guard bvec iteration logic Dmitry Monakhov
2017-03-31  8:21   ` Ming Lei
2017-03-30 13:49 ` [PATCH 2/8] bio-integrity: Do not allocate integrity context for bio w/o data Dmitry Monakhov
2017-03-30 13:49 ` [PATCH 3/8] bio-integrity: save original iterator for verify stage Dmitry Monakhov
2017-03-30 13:49 ` [PATCH 4/8] bio-integrity: bio_trim should truncate integrity vector accordingly Dmitry Monakhov
2017-03-30 13:49 ` [PATCH 5/8] bio-integrity: fix interface for bio_integrity_trim Dmitry Monakhov
2017-03-30 13:49 ` [PATCH 6/8] bio-integrity: add bio_integrity_setup helper Dmitry Monakhov
2017-03-31 22:15   ` kbuild test robot
2017-03-30 13:49 ` [PATCH 7/8] T10: Move opencoded contants to common header Dmitry Monakhov
2017-03-31 22:09   ` kbuild test robot
2017-03-30 13:49 ` Dmitry Monakhov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1490881776-28735-9-git-send-email-dmonakhov@openvz.org \
    --to=dmonakhov@openvz.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox