From: NeilBrown <email@example.com>
To: Jann Horn <firstname.lastname@example.org>,
Andrew Morton <email@example.com>,
Al Viro <firstname.lastname@example.org>,
Kees Cook <email@example.com>
Cc: Linus Torvalds <firstname.lastname@example.org>,
kernel list <email@example.com>,
firstname.lastname@example.org, Jonathan Corbet <email@example.com>
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: <firstname.lastname@example.org> (raw)
[-- 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()
- 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.
[-- 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
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:
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
* 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).