From: NeilBrown <neilb@suse.com>
To: Jann Horn <jannh@google.com>,
Andrew Morton <akpm@linux-foundation.org>,
Al Viro <viro@zeniv.linux.org.uk>,
Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
linux-doc@vger.kernel.org,
kernel list <linux-kernel@vger.kernel.org>,
linux-fsdevel@vger.kernel.org, Jonathan Corbet <corbet@lwn.net>
Subject: Re: [PATCH resend*3] VFS: simplify seq_file iteration code and interface
Date: Sat, 07 Jul 2018 13:23:08 +1000 [thread overview]
Message-ID: <878t6nybj7.fsf@notabene.neil.brown.name> (raw)
In-Reply-To: <CAG48ez2yTZMye0W3_5R04bV-Qyaz-=zSiVonw_zvRQNH6dcfeg@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2162 bytes --]
On Sat, Jul 07 2018, Jann Horn wrote:
>> @@ -287,11 +278,7 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
>> goto Efault;
>> copied += n;
>> m->count -= n;
>> - if (m->count)
>> - m->from = n;
>> - else
>> - pos++;
>> - m->index = pos;
>> + m->from = n;
>
> This patch introduces a kernel memory disclosure bug when something
> like the following sequence of events happens (starting from a freshly
> opened seq file):
>
> 1. read(seq_fd, buf, 2000): sets m->from=2000, m->count=100
> 2. create a buffer broken_buf which consists of 1000 bytes writable
> memory followed by unmapped memory
> 3. read(seq_fd, broken_buf, 3100):
> - flushes buffered data to userspace, result: m->from=2100, m->count=0
> - accumulates new data, result: m->from=2100, m->count=3050
> - tries to copy new data to userspace, but fails ("goto Efault")
> 4. read(seq_fd, buf, 4096): does copy_to_user(buf, m->buf + m->from, n)
Thanks for testing and for the report.
I think I see where I went wrong in the patch.
As I said in the description:
- don't clear ->from when ->count is zero, as ->from is dead when
->count is zero.
It is true that ->from is dead when ->count is zero, but as soon as
count becomes non-zero, ->from becomes important again.
So we either need to clear ->from whenever ->count is changed from zero
(which would be clumsy and error prone) we we need to clear
->from somewhere else.
->count is only increased in ->show() calls and there are three ->show()
calls.
- in traverse() ->from is set to zero early, and set once more shortly
before the function exits, so it is always correct.
- in "we need at least one record in buffer" ->count starts at zero
so ->from needs to be set to zero as well.
- in "Fill:" ->from is still correct from previous setting.
So I think we just need
m->from = 0;
at "we need at least one record in buffer". I'm fairly sure that
will fix the problem you found. I would appreciate it if you
would test and confirm.
I'll send a patch separately.
Thanks again,
NeilBrown
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]
next prev parent reply other threads:[~2018-07-07 3:23 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-15 22:42 [PATCH] VFS: simplify seq_file iteration code and interface NeilBrown
2018-04-30 1:50 ` [PATCH resend] " NeilBrown
2018-04-30 18:03 ` Jonathan Corbet
2018-05-31 22:26 ` [PATCH resend*2] " NeilBrown
2018-06-18 6:46 ` [PATCH resend*3] " NeilBrown
2018-07-07 0:56 ` Jann Horn
2018-07-07 3:23 ` NeilBrown [this message]
2018-07-07 3:29 ` [PATCH mm] VFS: seq_file: ensure ->from is valid NeilBrown
2018-07-07 3:50 ` Jann Horn
2018-07-09 18:16 ` Kees Cook
2018-07-09 19:40 ` Jann Horn
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=878t6nybj7.fsf@notabene.neil.brown.name \
--to=neilb@suse.com \
--cc=akpm@linux-foundation.org \
--cc=corbet@lwn.net \
--cc=jannh@google.com \
--cc=keescook@chromium.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/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;
as well as URLs for NNTP newsgroup(s).