Skip to contentSkip to conversation

: fs: pipe.c null pointer dereference

PATCH Superseded Earl Chew 4 participants 8 messages Oct 16, 2009 – Oct 21, 2009 VFS

lore
Share & tools
lore

Earl Chew

author
#0 lore raw

PATCH 1/1 : fs: pipe.c null pointer dereference

This patch fixes a null pointer exception in pipe_rdwr_open() which generates the stack trace:

Quoted 6 lines
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

This defect is also described in:
http://lkml.org/lkml/2009/10/14/184
http://bugzilla.kernel.org/show_bug.cgi?id=14416

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%% *}"
DELAY=$((RANDOM * 1000 / 32768))
usleep $((DELAY * 1000 + RANDOM % 1000 ))
echo n > /proc/$OUT/fd/1 # Trigger defect
done
=============================================================

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.

fs/pipe.c renamed from fs/pipe.c.orig +30 -11
@@ -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;
 }
 
 /*
#1 lore raw

[ adding some CCs, so that this userspace-triggerable NULL pointer
dereference doesn't get lost in the deep dark waters of LKML ]

Quoted 138 lines · Earl Chew

This patch fixes a null pointer exception in pipe_rdwr_open() which generates the stack trace:

Quoted 6 lines
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

This defect is also described in:
http://lkml.org/lkml/2009/10/14/184
http://bugzilla.kernel.org/show_bug.cgi?id=14416

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%% *}"
DELAY=$((RANDOM * 1000 / 32768))
usleep $((DELAY * 1000 + RANDOM % 1000 ))
echo n > /proc/$OUT/fd/1 # Trigger defect
done
=============================================================

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.

--- 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

fs/pipe.c renamed from fs/pipe.c.orig +30 -12
@@ -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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 2 lines
Jiri Kosina
SUSE Labs, Novell Inc.
author
#2 lore raw

PATCH v2 1/1 : fs: pipe.c null pointer dereference + sign off

In reply to Earl Chew

[ Exactly as before, but with sign off ]

This patch fixes a null pointer exception in pipe_rdwr_open() which generates the stack trace:

Quoted 6 lines
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

This defect is also described in:
http://lkml.org/lkml/2009/10/14/184
http://bugzilla.kernel.org/show_bug.cgi?id=14416

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%% *}"
DELAY=$((RANDOM * 1000 / 32768))
usleep $((DELAY * 1000 + RANDOM % 1000 ))
echo n > /proc/$OUT/fd/1 # Trigger defect
done
=============================================================

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.

fs/pipe.c 2009-10-15 21:21:25.000000000 -0700 renamed from fs/pipe.c.orig 2009-10-15 20:33:53.000000000 -0700 +30 -11
@@ -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;
 }
 
 /*
#3 lore raw

Re: [PATCH v2 1/1]: fs: pipe.c null pointer dereference + sign off

Quoted 2 lines · Earl Chew

[ Exactly as before, but with sign off ]

You've got a few more submission related issues, see below.

Quoted 65 lines

This patch fixes a null pointer exception in pipe_rdwr_open() which generates the stack trace:

Quoted 6 lines
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

This defect is also described in:

   http://lkml.org/lkml/2009/10/14/184
   http://bugzilla.kernel.org/show_bug.cgi?id=14416

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%% *}"
    DELAY=$((RANDOM * 1000 / 32768))
    usleep $((DELAY * 1000 + RANDOM % 1000 ))
    echo n > /proc/$OUT/fd/1                 # Trigger defect

done
=============================================================

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.

--- 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

Review pipe_rdwr_release(struct inode *inode, s · line 712

Quoted 1 line
(quoted diff) +0 -0
@@ -712,36 +712,55 @@ pipe_rdwr_release(struct inode *inode, s
  static int

You patch looks like it might be line wrapped .. Sometimes mailers will do that if you don't tell them the content is already formatted.

Also I see your using a MontaVista kernel, which is older than current mainline .. I would assume you tested this on a recent kernel also?

fs/pipe.c 2009-10-15 21:21:25.000000000 -0700 pipe_rdwr_release(struct inode *inode, s

Quoted 7 lines
fs/pipe.c 2009-10-15 21:21:25.000000000 -0700 renamed from fs/pipe.c.orig 2009-10-15 20:33:53.000000000 -0700 +1 -2
  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;
 
 From checkpatch,

ERROR: code indent should use tabs where possible
#125: FILE: fs/pipe.c:720:
+ ret = 0;$

It looks like maybe your mailer (or copy and paste) may have stripped all the tabs off your patch. This makes is very difficult to apply.

Daniel

author
#4 lore raw

Re: [PATCH v2 1/1]: fs: pipe.c null pointer dereference + sign off

Quoted 2 lines · Daniel Walker

You patch looks like it might be line wrapped .. Sometimes mailers will do that if you don't tell them the content is already formatted.

Arghhh ... struck by Thunderbirditis.

Apparently mailnews.send_plaintext_flowed=false is the key to preserving tabs, and the Toggle Word Wrap extension is useful for to avoid inserting additional newlines:

https://addons.mozilla.org/en-US/thunderbird/addon/2351

As you can see, I'm new to this game. Your patience is appreciated.

I'll repost.

Quoted 2 lines

Also I see your using a MontaVista kernel, which is older than current mainline .. I would assume you tested this on a recent kernel also?

I don't have immediate access to a more recent kernel.

The changes are fairly straightforward and I have added extensive documentation on how to reproduce and verify the defect.

Is anyone interested in helping out here by verifying on a more recent kernel?

Earl

author
#5 lore raw

PATCH v3 1/1 : fs: pipe.c null pointer dereference + sign off + unmangled diffs

In reply to Earl Chew

[ Exactly as before, but with sign off and tabs preserved ]

This patch fixes a null pointer exception in pipe_rdwr_open() which generates the stack trace:

Quoted 6 lines
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%% *}"
DELAY=$((RANDOM * 1000 / 32768))
usleep $((DELAY * 1000 + RANDOM % 1000 ))
echo n > /proc/$OUT/fd/1 # Trigger defect
done
=============================================================

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.

fs/pipe.c renamed from fs/pipe.c.orig +30 -11
@@ -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;
 }
 
 /*
author
#6 lore raw

PATCH v4 1/1 : fs: pipe.c null pointer dereference + really sign off + unmangled diffs

[ 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:

Quoted 6 lines
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%% *}"
DELAY=$((RANDOM * 1000 / 32768))
usleep $((DELAY * 1000 + RANDOM % 1000 ))
echo n > /proc/$OUT/fd/1 # Trigger defect
done
=============================================================

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.

fs/pipe.c renamed from fs/pipe.c.orig +30 -11
@@ -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;
 }
 
 /*
#7 lore raw

Re: [PATCH v4 1/1]: fs: pipe.c null pointer dereference + really sign off + unmangled diffs

Quoted 18 lines · Earl Chew

[ 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:

Quoted 6 lines
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:

Quoted 8 lines

=============================================================
#!/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

Quoted 6 lines

  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.

Quoted 15 lines

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.

Quoted 8 lines

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.

Quoted 3 lines

Add some Cc, fs-devel and Al.

Quoted 70 lines
fs/pipe.c    2009-10-15 21:21:25.000000000 -0700 renamed from fs/pipe.c.orig       2009-10-15 20:33:53.000000000 -0700 +0 -0
@@ -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++;

fs/pipe.c    2009-10-15 21:21:25.000000000 -0700 renamed from fs/pipe.c.orig       2009-10-15 20:33:53.000000000 -0700 +6 -0
+
+       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++;

fs/pipe.c    2009-10-15 21:21:25.000000000 -0700 renamed from fs/pipe.c.orig       2009-10-15 20:33:53.000000000 -0700 +6 -0
+
+       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++;

fs/pipe.c    2009-10-15 21:21:25.000000000 -0700 renamed from fs/pipe.c.orig       2009-10-15 20:33:53.000000000 -0700 +9 -0
+
+       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;
 }

Quoted 9 lines

 /*

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
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-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html