public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* seq_file dangerous assumption?
@ 2012-06-04 19:32 Eric Van Hensbergen
  2012-06-04 20:07 ` Jan Kara
  2012-06-09  5:22 ` Al Viro
  0 siblings, 2 replies; 4+ messages in thread
From: Eric Van Hensbergen @ 2012-06-04 19:32 UTC (permalink / raw)
  To: linux-kernel

I was merging up someone else's driver code from a much older kernel
to 3.5-rc1 and ran into some issues with corrupted memory.  The
character driver in question was using seq-file.c to handle reads to
the device.  Based on looking around at other drivers, no one else
does this -- so its probably (well, definitely based on what I found)
not the right way to do this.

seq_open seems to make a fairly general assumption:
(from linux-3.5-rc1 fs/seq_file.c)
...
int seq_open(struct file *file, const struct seq_operations *op)
{
        struct seq_file *p = file->private_data;

        if (!p) {
                p = kmalloc(sizeof(*p), GFP_KERNEL);
                if (!p)
                        return -ENOMEM;
                file->private_data = p;
        }
        memset(p, 0, sizeof(*p));
..

In other words, if something is in file->private_data, then we must
have already allocated and put our structure there.  In the case of
this driver, file->private_data was already populated (with a pointer
to the device structure) -- so the call to seq_open zero'd a portion
of the device structure and then corrupted it with a seq_file
structure.

So, an obvious solution is, don't use seq_file with a character device
-- but shouldn't there also be a fingerprint or something in the
seq_file structure as a sanity check so foolish developers don't trip
over it and corrupt their kernel memory?

      -eric

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-06-09  5:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-04 19:32 seq_file dangerous assumption? Eric Van Hensbergen
2012-06-04 20:07 ` Jan Kara
2012-06-09  5:26   ` Al Viro
2012-06-09  5:22 ` Al Viro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox