Re: [PATCH v4 1/1]: fs: pipe.c null pointer dereference + really sign off + unmangled diffs
From: AmÃrico Wang
Date: Wed Oct 21 2009 - 05:38:29 EST
On Tue, Oct 20, 2009 at 6:55 AM, Earl Chew <earl_chew@xxxxxxxxxxx> wrote:
> [ Exactly as before, but really sign off and tabs preserved ]
>
>
> This patch fixes a null pointer exception in pipe_rdwr_open() which
> generates the stack trace:
>
>
>> Unable to handle kernel NULL pointer dereference at 0000000000000028 RIP:
>> Â[<ffffffff802899a5>] pipe_rdwr_open+0x35/0x70
>> Â[<ffffffff8028125c>] __dentry_open+0x13c/0x230
>> Â[<ffffffff8028143d>] do_filp_open+0x2d/0x40
>> Â[<ffffffff802814aa>] do_sys_open+0x5a/0x100
>> Â[<ffffffff8021faf3>] sysenter_do_call+0x1b/0x67
>
>
> The failure mode is triggered by an attempt to open an anonymous
> pipe via /proc/pid/fd/* as exemplified by this script:
>
> =============================================================
> #!/bin/sh
> while : ; do
> Â { echo y ; sleep 1 ; } | { while read ; do echo z$REPLY; done ; } &
> Â PID=$!
> Â OUT=$(ps -efl | grep 'sleep 1' | grep -v grep |
> Â Â Â Â{ read PID REST ; echo $PID; } )
> Â OUT="${OUT%% *}"
Well, you can use 'pgrep', it will save you a lot here.
Try: pgrep -f 'sleep 1' -n
> Â DELAY=$((RANDOM * 1000 / 32768))
> Â usleep $((DELAY * 1000 + RANDOM % 1000 ))
> Â echo n > /proc/$OUT/fd/1 Â Â Â Â Â Â Â Â # Trigger defect
> done
> =============================================================
>
This still has very little chance to trigger it, I am afraid.
I tried on my machine, didn't get any oops.
Trying to use C to write it may be better.
> Note that the failure window is quite small and I could only
> reliably reproduce the defect by inserting a small delay
> in pipe_rdwr_open(). For example:
>
> Âstatic int
> Âpipe_rdwr_open(struct inode *inode, struct file *filp)
> Â{
> Â Â Â msleep(100);
> Â Â Â mutex_lock(&inode->i_mutex);
>
>
> Although the defect was observed in pipe_rdwr_open(), I think it
> makes sense to replicate the change through all the pipe_*_open()
> functions.
>
> The core of the change is to verify that inode->i_pipe has not
> been released before attempting to manipulate it. If inode->i_pipe
> is no longer present, return ENOENT to indicate so.
>
> The comment about potentially using atomic_t for i_pipe->readers
> and i_pipe->writers has also been removed because it is no longer
> relevant in this context. The inode->i_mutex lock must be used so
> that inode->i_pipe can be dealt with correctly.
So, if I understand you correctly, you mean we have a small window
between calling sys_open() and fifo_open(), during this little period,
we don't have i_mutex held, thun another process have a chance
to release that pipe and make i_pipe NULL. Right?
Hmm, sounds reasonable. :-/
I'd like you to put the explanations into the code, as comments.
>
>
> Signed-off-by: Earl Chew <earl_chew@xxxxxxxxxxx>
Add some Cc, fs-devel and Al.
>
>
> --- linux-2.6.21_mvlcge500/fs/pipe.c.orig    2009-10-15 20:33:53.000000000 -0700
> +++ linux-2.6.21_mvlcge500/fs/pipe.c  Â2009-10-15 21:21:25.000000000 -0700
> @@ -712,36 +712,55 @@ pipe_rdwr_release(struct inode *inode, s
> Âstatic int
> Âpipe_read_open(struct inode *inode, struct file *filp)
> Â{
> - Â Â Â /* We could have perhaps used atomic_t, but this and friends
> - Â Â Â Â Âbelow are the only places. ÂSo it doesn't seem worthwhile. Â*/
> + Â Â Â int ret = -ENOENT;
> +
> Â Â Â Âmutex_lock(&inode->i_mutex);
> - Â Â Â inode->i_pipe->readers++;
> +
> + Â Â Â if (inode->i_pipe) {
> + Â Â Â Â Â Â Â ret = 0;
> + Â Â Â Â Â Â Â inode->i_pipe->readers++;
> + Â Â Â }
> +
> Â Â Â Âmutex_unlock(&inode->i_mutex);
>
> - Â Â Â return 0;
> + Â Â Â return ret;
> Â}
>
> Âstatic int
> Âpipe_write_open(struct inode *inode, struct file *filp)
> Â{
> + Â Â Â int ret = -ENOENT;
> +
> Â Â Â Âmutex_lock(&inode->i_mutex);
> - Â Â Â inode->i_pipe->writers++;
> +
> + Â Â Â if (inode->i_pipe) {
> + Â Â Â Â Â Â Â ret = 0;
> + Â Â Â Â Â Â Â inode->i_pipe->writers++;
> + Â Â Â }
> +
> Â Â Â Âmutex_unlock(&inode->i_mutex);
>
> - Â Â Â return 0;
> + Â Â Â return ret;
> Â}
>
> Âstatic int
> Âpipe_rdwr_open(struct inode *inode, struct file *filp)
> Â{
> + Â Â Â int ret = -ENOENT;
> +
> Â Â Â Âmutex_lock(&inode->i_mutex);
> - Â Â Â if (filp->f_mode & FMODE_READ)
> - Â Â Â Â Â Â Â inode->i_pipe->readers++;
> - Â Â Â if (filp->f_mode & FMODE_WRITE)
> - Â Â Â Â Â Â Â inode->i_pipe->writers++;
> +
> + Â Â Â if (inode->i_pipe) {
> + Â Â Â Â Â Â Â ret = 0;
> + Â Â Â Â Â Â Â if (filp->f_mode & FMODE_READ)
> + Â Â Â Â Â Â Â Â Â Â Â inode->i_pipe->readers++;
> + Â Â Â Â Â Â Â if (filp->f_mode & FMODE_WRITE)
> + Â Â Â Â Â Â Â Â Â Â Â inode->i_pipe->writers++;
> + Â Â Â }
> +
> Â Â Â Âmutex_unlock(&inode->i_mutex);
>
> - Â Â Â return 0;
> + Â Â Â return ret;
> Â}
>
> Â/*
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html
> Please read the FAQ at Âhttp://www.tux.org/lkml/
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/