Skip to contentSkip to conversation

pstore: Protect unlink with read_mutex

PATCH In review Namhyung Kim 2 participants 4 messages Oct 19, 2016 – Nov 15, 2016 Pstore filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 pstore: Protect unlink with read_mutex

When update_ms is set, pstore_get_records() will be called when there's a new entry. But unlink can be called at the same time and might contend with the open-read-close loop. Depending on the implementation of platform driver, it may be safe or not. But I think it'd be better to protect those race in the first place.

Cc Stefan Hajnoczi <[email protected]>
---
 fs/pstore/inode.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/pstore/inode.c +5 -2
index 1781dc50762e..75728dfae0a6 100644
@@ -197,11 +197,14 @@ static int pstore_unlink(struct inode *dir, struct dentry *dentry)
 	if (err)
 		return err;
 
-	if (p->psi->erase)
+	if (p->psi->erase) {
+		mutex_lock(&p->psi->read_mutex);
 		p->psi->erase(p->type, p->id, p->count,
 			      d_inode(dentry)->i_ctime, p->psi);
-	else
+		mutex_unlock(&p->psi->read_mutex);
+	} else {
 		return -EPERM;
+	}
 
 	return simple_unlink(dir, dentry);
 }
Signature · 1 line
2.9.3
#1 lore raw

PATCH 2/2 pstore: Convert console write to use ->write_buf

Maybe I'm missing something, but I don't know why it needs to copy the input buffer to psinfo->buf and then write. Instead we can write the input buffer directly. The only implementation that supports console message (i.e. ramoops) already does it for ftrace messages.

For the upcoming virtio backend driver, it needs to protect psinfo->buf overwritten from console messages. If it could use ->write_buf method instead of ->write, the problem will be solved easily.

Cc Stefan Hajnoczi <[email protected]>
---
 fs/pstore/platform.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/platform.c +2 -2
index 14984d902a99..960d66251bab 100644
@@ -584,8 +584,8 @@ static void pstore_console_write(struct console *con, const char *s, unsigned c)
 		} else {
 			spin_lock_irqsave(&psinfo->buf_lock, flags);
 		}
-		memcpy(psinfo->buf, s, c);
-		psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo);
+		psinfo->write_buf(PSTORE_TYPE_CONSOLE, 0, &id, 0,
+				  s, 0, c, psinfo);
 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
 		s += c;
 		c = e - s;
Signature · 1 line
2.9.3
#2 lore raw

Re: [RFC/PATCH 2/2] pstore: Convert console write to use ->write_buf

Quoted 8 lines · Namhyung Kim

Maybe I'm missing something, but I don't know why it needs to copy the input buffer to psinfo->buf and then write. Instead we can write the input buffer directly. The only implementation that supports console message (i.e. ramoops) already does it for ftrace messages.

For the upcoming virtio backend driver, it needs to protect psinfo->buf overwritten from console messages. If it could use ->write_buf method instead of ->write, the problem will be solved easily.

Agreed, this looks like needless bounce-buffering. Thanks!

-Kees

Quoted 25 lines
Cc Stefan Hajnoczi <[email protected]>
---
 fs/pstore/platform.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/pstore/platform.c +2 -3
index 14984d902a99..960d66251bab 100644
@@ -584,8 +584,8 @@ static void pstore_console_write(struct console *con, const char *s, unsigned c)
                } else {
                        spin_lock_irqsave(&psinfo->buf_lock, flags);
                }
-               memcpy(psinfo->buf, s, c);
-               psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo);
+               psinfo->write_buf(PSTORE_TYPE_CONSOLE, 0, &id, 0,
+                                 s, 0, c, psinfo);
                spin_unlock_irqrestore(&psinfo->buf_lock, flags);
                s += c;
                c = e - s;
--

2.9.3

Signature · 2 lines
Kees Cook
Nexus Security
#3 lore raw
In reply to Namhyung Kim
Quoted 8 lines · Namhyung Kim

When update_ms is set, pstore_get_records() will be called when there's a new entry. But unlink can be called at the same time and might contend with the open-read-close loop. Depending on the implementation of platform driver, it may be safe or not. But I think it'd be better to protect those race in the first place.

Cc Stefan Hajnoczi <[email protected]>

This looks correct to me, thanks! It should appear in -next shortly...

-Kees

Quoted 28 lines
---
 fs/pstore/inode.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/pstore/inode.c +5 -3
index 1781dc50762e..75728dfae0a6 100644
@@ -197,11 +197,14 @@ static int pstore_unlink(struct inode *dir, struct dentry *dentry)
        if (err)
                return err;
 
-       if (p->psi->erase)
+       if (p->psi->erase) {
+               mutex_lock(&p->psi->read_mutex);
                p->psi->erase(p->type, p->id, p->count,
                              d_inode(dentry)->i_ctime, p->psi);
-       else
+               mutex_unlock(&p->psi->read_mutex);
+       } else {
                return -EPERM;
+       }
 
        return simple_unlink(dir, dentry);
 }
--

2.9.3

Signature · 2 lines
Kees Cook
Nexus Security