Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1306270 > unrolled thread
| Started by | "Herton R. Krzesinski" <herton@redhat.com> |
|---|---|
| First post | 2016-01-11 15:10 +0100 |
| Last post | 2016-01-14 21:10 +0100 |
| Articles | 4 — 2 participants |
Back to article view | Back to linux.kernel
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
[PATCH 2/2] pty: make sure super_block is still valid in final /dev/tty close "Herton R. Krzesinski" <herton@redhat.com> - 2016-01-11 15:10 +0100
Re: [PATCH 2/2] pty: make sure super_block is still valid in final /dev/tty close Peter Hurley <peter@hurleysoftware.com> - 2016-01-13 19:00 +0100
[PATCH 2/2 v2] pty: make sure super_block is still valid in final /dev/tty close "Herton R. Krzesinski" <herton@redhat.com> - 2016-01-14 21:00 +0100
Re: [PATCH 2/2] pty: make sure super_block is still valid in final /dev/tty close "Herton R. Krzesinski" <herton@redhat.com> - 2016-01-14 21:10 +0100
| From | "Herton R. Krzesinski" <herton@redhat.com> |
|---|---|
| Date | 2016-01-11 15:10 +0100 |
| Subject | [PATCH 2/2] pty: make sure super_block is still valid in final /dev/tty close |
| Message-ID | <qPLkw-3rc-41@gated-at.bofh.it> |
Considering current pty code and multiple devpts instances, it's possible
to umount a devpts file system while a program still has /dev/tty opened
pointing to a previosuly closed pty pair in that instance. In the case all
ptmx and pts/N files are closed, umount can be done. If the program closes
/dev/tty after umount is done, devpts_kill_index will use now an invalid
super_block, which was already destroyed in the umount operation after
running ->kill_sb. This is another "use after free" type of issue, but now
related to the allocated super_block instance.
To avoid the problem (warning at ida_remove and potential crashes) for
this specific case, I added two functions in devpts which grabs additional
references to the super_block, which pty code now uses so it makes sure
the super block structure is still valid until pty shutdown is done.
I also moved the additional inode references to the same functions, which
also covered similar case with inode being freed before /dev/tty final
close/shutdown.
Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
Cc: stable@vger.kernel.org # 2.6.29+
---
drivers/tty/pty.c | 9 ++++++---
fs/devpts/inode.c | 20 ++++++++++++++++++++
include/linux/devpts_fs.h | 4 ++++
3 files changed, 30 insertions(+), 3 deletions(-)
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
index 96016e5..7fc1b3e 100644
--- a/drivers/tty/pty.c
+++ b/drivers/tty/pty.c
@@ -688,7 +688,7 @@ static void pty_unix98_shutdown(struct tty_struct *tty)
else
ptmx_inode = tty->link->driver_data;
devpts_kill_index(ptmx_inode, tty->index);
- iput(ptmx_inode); /* drop reference we acquired at ptmx_open */
+ devpts_iput_sb_deactive(ptmx_inode);
}
static const struct tty_operations ptm_unix98_ops = {
@@ -785,9 +785,12 @@ static int ptmx_open(struct inode *inode, struct file *filp)
* still have /dev/tty opened pointing to the master/slave pair (ptmx
* is closed/released before /dev/tty), we must make sure that the inode
* is still valid when we call the final pty_unix98_shutdown, thus we
- * hold an additional reference to the ptmx inode
+ * hold an additional reference to the ptmx inode. For the same /dev/tty
+ * last close case, we also need to make sure the super_block isn't
+ * destroyed (devpts instance unmounted), before /dev/tty is closed and
+ * on its release devpts_kill_index is called.
*/
- ihold(inode);
+ devpts_ihold_sb_active(inode);
tty_add_file(tty, filp);
diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c
index c35ffdc..66a5421 100644
--- a/fs/devpts/inode.c
+++ b/fs/devpts/inode.c
@@ -575,6 +575,26 @@ void devpts_kill_index(struct inode *ptmx_inode, int idx)
mutex_unlock(&allocated_ptys_lock);
}
+/*
+ * pty code needs to hold extra references in case of last /dev/tty close
+ */
+
+void devpts_ihold_sb_active(struct inode *ptmx_inode)
+{
+ struct super_block *sb = pts_sb_from_inode(ptmx_inode);
+
+ atomic_inc(&sb->s_active);
+ ihold(ptmx_inode);
+}
+
+void devpts_iput_sb_deactive(struct inode *ptmx_inode)
+{
+ struct super_block *sb = pts_sb_from_inode(ptmx_inode);
+
+ iput(ptmx_inode);
+ deactivate_super(sb);
+}
+
/**
* devpts_pty_new -- create a new inode in /dev/pts/
* @ptmx_inode: inode of the master
diff --git a/include/linux/devpts_fs.h b/include/linux/devpts_fs.h
index 251a209..f73ef49 100644
--- a/include/linux/devpts_fs.h
+++ b/include/linux/devpts_fs.h
@@ -19,6 +19,8 @@
int devpts_new_index(struct inode *ptmx_inode);
void devpts_kill_index(struct inode *ptmx_inode, int idx);
+void devpts_ihold_sb_active(struct inode *ptmx_inode);
+void devpts_iput_sb_deactive(struct inode *ptmx_inode);
/* mknod in devpts */
struct inode *devpts_pty_new(struct inode *ptmx_inode, dev_t device, int index,
void *priv);
@@ -32,6 +34,8 @@ void devpts_pty_kill(struct inode *inode);
/* Dummy stubs in the no-pty case */
static inline int devpts_new_index(struct inode *ptmx_inode) { return -EINVAL; }
static inline void devpts_kill_index(struct inode *ptmx_inode, int idx) { }
+static inline void devpts_ihold_sb_active(struct inode *ptmx_inode) { }
+static inline void devpts_iput_sb_deactive(struct inode *ptmx_inode) { }
static inline struct inode *devpts_pty_new(struct inode *ptmx_inode,
dev_t device, int index, void *priv)
{
--
2.4.3
[toc] | [next] | [standalone]
| From | Peter Hurley <peter@hurleysoftware.com> |
|---|---|
| Date | 2016-01-13 19:00 +0100 |
| Subject | Re: [PATCH 2/2] pty: make sure super_block is still valid in final /dev/tty close |
| Message-ID | <qQxSb-37P-25@gated-at.bofh.it> |
| In reply to | #1306270 |
Hi Herton,
On 01/11/2016 06:07 AM, Herton R. Krzesinski wrote:
> Considering current pty code and multiple devpts instances, it's possible
> to umount a devpts file system while a program still has /dev/tty opened
> pointing to a previosuly closed pty pair in that instance. In the case all
> ptmx and pts/N files are closed, umount can be done. If the program closes
> /dev/tty after umount is done, devpts_kill_index will use now an invalid
> super_block, which was already destroyed in the umount operation after
> running ->kill_sb. This is another "use after free" type of issue, but now
> related to the allocated super_block instance.
>
> To avoid the problem (warning at ida_remove and potential crashes) for
> this specific case, I added two functions in devpts which grabs additional
> references to the super_block, which pty code now uses so it makes sure
> the super block structure is still valid until pty shutdown is done.
> I also moved the additional inode references to the same functions, which
> also covered similar case with inode being freed before /dev/tty final
> close/shutdown.
Thanks for discovering and working this problem.
Comments below.
> Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
> Cc: stable@vger.kernel.org # 2.6.29+
> ---
> drivers/tty/pty.c | 9 ++++++---
> fs/devpts/inode.c | 20 ++++++++++++++++++++
> include/linux/devpts_fs.h | 4 ++++
> 3 files changed, 30 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
> index 96016e5..7fc1b3e 100644
> --- a/drivers/tty/pty.c
> +++ b/drivers/tty/pty.c
> @@ -688,7 +688,7 @@ static void pty_unix98_shutdown(struct tty_struct *tty)
> else
> ptmx_inode = tty->link->driver_data;
> devpts_kill_index(ptmx_inode, tty->index);
> - iput(ptmx_inode); /* drop reference we acquired at ptmx_open */
> + devpts_iput_sb_deactive(ptmx_inode);
> }
>
> static const struct tty_operations ptm_unix98_ops = {
> @@ -785,9 +785,12 @@ static int ptmx_open(struct inode *inode, struct file *filp)
> * still have /dev/tty opened pointing to the master/slave pair (ptmx
> * is closed/released before /dev/tty), we must make sure that the inode
> * is still valid when we call the final pty_unix98_shutdown, thus we
> - * hold an additional reference to the ptmx inode
> + * hold an additional reference to the ptmx inode. For the same /dev/tty
> + * last close case, we also need to make sure the super_block isn't
> + * destroyed (devpts instance unmounted), before /dev/tty is closed and
> + * on its release devpts_kill_index is called.
> */
> - ihold(inode);
> + devpts_ihold_sb_active(inode);
>
> tty_add_file(tty, filp);
>
> diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c
> index c35ffdc..66a5421 100644
> --- a/fs/devpts/inode.c
> +++ b/fs/devpts/inode.c
> @@ -575,6 +575,26 @@ void devpts_kill_index(struct inode *ptmx_inode, int idx)
> mutex_unlock(&allocated_ptys_lock);
> }
>
> +/*
> + * pty code needs to hold extra references in case of last /dev/tty close
> + */
> +
> +void devpts_ihold_sb_active(struct inode *ptmx_inode)
> +{
> + struct super_block *sb = pts_sb_from_inode(ptmx_inode);
> +
> + atomic_inc(&sb->s_active);
> + ihold(ptmx_inode);
> +}
> +
> +void devpts_iput_sb_deactive(struct inode *ptmx_inode)
> +{
> + struct super_block *sb = pts_sb_from_inode(ptmx_inode);
> +
> + iput(ptmx_inode);
> + deactivate_super(sb);
> +}
We might as well roll in this functionality into
devpts_new_index() and devpts_kill_index().
I realize that's muddying the separation of concern.
Alternatively, name the functions for the logical operation
rather than specifically for what they do (eg. devpts_add_ref())
Regards,
Peter Hurley
> +
> /**
> * devpts_pty_new -- create a new inode in /dev/pts/
> * @ptmx_inode: inode of the master
> diff --git a/include/linux/devpts_fs.h b/include/linux/devpts_fs.h
> index 251a209..f73ef49 100644
> --- a/include/linux/devpts_fs.h
> +++ b/include/linux/devpts_fs.h
> @@ -19,6 +19,8 @@
>
> int devpts_new_index(struct inode *ptmx_inode);
> void devpts_kill_index(struct inode *ptmx_inode, int idx);
> +void devpts_ihold_sb_active(struct inode *ptmx_inode);
> +void devpts_iput_sb_deactive(struct inode *ptmx_inode);
> /* mknod in devpts */
> struct inode *devpts_pty_new(struct inode *ptmx_inode, dev_t device, int index,
> void *priv);
> @@ -32,6 +34,8 @@ void devpts_pty_kill(struct inode *inode);
> /* Dummy stubs in the no-pty case */
> static inline int devpts_new_index(struct inode *ptmx_inode) { return -EINVAL; }
> static inline void devpts_kill_index(struct inode *ptmx_inode, int idx) { }
> +static inline void devpts_ihold_sb_active(struct inode *ptmx_inode) { }
> +static inline void devpts_iput_sb_deactive(struct inode *ptmx_inode) { }
> static inline struct inode *devpts_pty_new(struct inode *ptmx_inode,
> dev_t device, int index, void *priv)
> {
>
[toc] | [prev] | [next] | [standalone]
| From | "Herton R. Krzesinski" <herton@redhat.com> |
|---|---|
| Date | 2016-01-14 21:00 +0100 |
| Subject | [PATCH 2/2 v2] pty: make sure super_block is still valid in final /dev/tty close |
| Message-ID | <qQWdQ-3jT-13@gated-at.bofh.it> |
| In reply to | #1308692 |
Considering current pty code and multiple devpts instances, it's possible
to umount a devpts file system while a program still has /dev/tty opened
pointing to a previosuly closed pty pair in that instance. In the case all
ptmx and pts/N files are closed, umount can be done. If the program closes
/dev/tty after umount is done, devpts_kill_index will use now an invalid
super_block, which was already destroyed in the umount operation after
running ->kill_sb. This is another "use after free" type of issue, but now
related to the allocated super_block instance.
To avoid the problem (warning at ida_remove and potential crashes) for
this specific case, I added two functions in devpts which grabs additional
references to the super_block, which pty code now uses so it makes sure
the super block structure is still valid until pty shutdown is done.
I also moved the additional inode references to the same functions, which
also covered similar case with inode being freed before /dev/tty final
close/shutdown.
Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
Cc: stable@vger.kernel.org # 2.6.29+
---
drivers/tty/pty.c | 9 ++++++---
fs/devpts/inode.c | 20 ++++++++++++++++++++
include/linux/devpts_fs.h | 4 ++++
3 files changed, 30 insertions(+), 3 deletions(-)
v2: rename devpts_ihold_sb_active to devpts_add_ref
rename devpts_iput_sb_deactive to devpts_del_ref
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
index 3b5cde8..2348fa6 100644
--- a/drivers/tty/pty.c
+++ b/drivers/tty/pty.c
@@ -688,7 +688,7 @@ static void pty_unix98_shutdown(struct tty_struct *tty)
else
ptmx_inode = tty->link->driver_data;
devpts_kill_index(ptmx_inode, tty->index);
- iput(ptmx_inode); /* drop reference we acquired at ptmx_open */
+ devpts_del_ref(ptmx_inode);
}
static const struct tty_operations ptm_unix98_ops = {
@@ -785,9 +785,12 @@ static int ptmx_open(struct inode *inode, struct file *filp)
* still have /dev/tty opened pointing to the master/slave pair (ptmx
* is closed/released before /dev/tty), we must make sure that the inode
* is still valid when we call the final pty_unix98_shutdown, thus we
- * hold an additional reference to the ptmx inode
+ * hold an additional reference to the ptmx inode. For the same /dev/tty
+ * last close case, we also need to make sure the super_block isn't
+ * destroyed (devpts instance unmounted), before /dev/tty is closed and
+ * on its release devpts_kill_index is called.
*/
- ihold(inode);
+ devpts_add_ref(inode);
tty_add_file(tty, filp);
diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c
index c35ffdc..706de32 100644
--- a/fs/devpts/inode.c
+++ b/fs/devpts/inode.c
@@ -575,6 +575,26 @@ void devpts_kill_index(struct inode *ptmx_inode, int idx)
mutex_unlock(&allocated_ptys_lock);
}
+/*
+ * pty code needs to hold extra references in case of last /dev/tty close
+ */
+
+void devpts_add_ref(struct inode *ptmx_inode)
+{
+ struct super_block *sb = pts_sb_from_inode(ptmx_inode);
+
+ atomic_inc(&sb->s_active);
+ ihold(ptmx_inode);
+}
+
+void devpts_del_ref(struct inode *ptmx_inode)
+{
+ struct super_block *sb = pts_sb_from_inode(ptmx_inode);
+
+ iput(ptmx_inode);
+ deactivate_super(sb);
+}
+
/**
* devpts_pty_new -- create a new inode in /dev/pts/
* @ptmx_inode: inode of the master
diff --git a/include/linux/devpts_fs.h b/include/linux/devpts_fs.h
index 251a209..e0ee0b3 100644
--- a/include/linux/devpts_fs.h
+++ b/include/linux/devpts_fs.h
@@ -19,6 +19,8 @@
int devpts_new_index(struct inode *ptmx_inode);
void devpts_kill_index(struct inode *ptmx_inode, int idx);
+void devpts_add_ref(struct inode *ptmx_inode);
+void devpts_del_ref(struct inode *ptmx_inode);
/* mknod in devpts */
struct inode *devpts_pty_new(struct inode *ptmx_inode, dev_t device, int index,
void *priv);
@@ -32,6 +34,8 @@ void devpts_pty_kill(struct inode *inode);
/* Dummy stubs in the no-pty case */
static inline int devpts_new_index(struct inode *ptmx_inode) { return -EINVAL; }
static inline void devpts_kill_index(struct inode *ptmx_inode, int idx) { }
+static inline void devpts_add_ref(struct inode *ptmx_inode) { }
+static inline void devpts_del_ref(struct inode *ptmx_inode) { }
static inline struct inode *devpts_pty_new(struct inode *ptmx_inode,
dev_t device, int index, void *priv)
{
--
2.4.3
[toc] | [prev] | [next] | [standalone]
| From | "Herton R. Krzesinski" <herton@redhat.com> |
|---|---|
| Date | 2016-01-14 21:10 +0100 |
| Subject | Re: [PATCH 2/2] pty: make sure super_block is still valid in final /dev/tty close |
| Message-ID | <qQWnw-3Cs-7@gated-at.bofh.it> |
| In reply to | #1308692 |
On Wed, Jan 13, 2016 at 09:54:03AM -0800, Peter Hurley wrote:
> Hi Herton,
>
> On 01/11/2016 06:07 AM, Herton R. Krzesinski wrote:
> > Considering current pty code and multiple devpts instances, it's possible
> > to umount a devpts file system while a program still has /dev/tty opened
> > pointing to a previosuly closed pty pair in that instance. In the case all
> > ptmx and pts/N files are closed, umount can be done. If the program closes
> > /dev/tty after umount is done, devpts_kill_index will use now an invalid
> > super_block, which was already destroyed in the umount operation after
> > running ->kill_sb. This is another "use after free" type of issue, but now
> > related to the allocated super_block instance.
> >
> > To avoid the problem (warning at ida_remove and potential crashes) for
> > this specific case, I added two functions in devpts which grabs additional
> > references to the super_block, which pty code now uses so it makes sure
> > the super block structure is still valid until pty shutdown is done.
> > I also moved the additional inode references to the same functions, which
> > also covered similar case with inode being freed before /dev/tty final
> > close/shutdown.
>
> Thanks for discovering and working this problem.
> Comments below.
Thanks for looking/reviewing the patches! :)
>
>
> > Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
> > Cc: stable@vger.kernel.org # 2.6.29+
> > ---
> > drivers/tty/pty.c | 9 ++++++---
> > fs/devpts/inode.c | 20 ++++++++++++++++++++
> > include/linux/devpts_fs.h | 4 ++++
> > 3 files changed, 30 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
> > index 96016e5..7fc1b3e 100644
> > --- a/drivers/tty/pty.c
> > +++ b/drivers/tty/pty.c
> > @@ -688,7 +688,7 @@ static void pty_unix98_shutdown(struct tty_struct *tty)
> > else
> > ptmx_inode = tty->link->driver_data;
> > devpts_kill_index(ptmx_inode, tty->index);
> > - iput(ptmx_inode); /* drop reference we acquired at ptmx_open */
> > + devpts_iput_sb_deactive(ptmx_inode);
> > }
> >
> > static const struct tty_operations ptm_unix98_ops = {
> > @@ -785,9 +785,12 @@ static int ptmx_open(struct inode *inode, struct file *filp)
> > * still have /dev/tty opened pointing to the master/slave pair (ptmx
> > * is closed/released before /dev/tty), we must make sure that the inode
> > * is still valid when we call the final pty_unix98_shutdown, thus we
> > - * hold an additional reference to the ptmx inode
> > + * hold an additional reference to the ptmx inode. For the same /dev/tty
> > + * last close case, we also need to make sure the super_block isn't
> > + * destroyed (devpts instance unmounted), before /dev/tty is closed and
> > + * on its release devpts_kill_index is called.
> > */
> > - ihold(inode);
> > + devpts_ihold_sb_active(inode);
> >
> > tty_add_file(tty, filp);
> >
> > diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c
> > index c35ffdc..66a5421 100644
> > --- a/fs/devpts/inode.c
> > +++ b/fs/devpts/inode.c
> > @@ -575,6 +575,26 @@ void devpts_kill_index(struct inode *ptmx_inode, int idx)
> > mutex_unlock(&allocated_ptys_lock);
> > }
> >
> > +/*
> > + * pty code needs to hold extra references in case of last /dev/tty close
> > + */
> > +
> > +void devpts_ihold_sb_active(struct inode *ptmx_inode)
> > +{
> > + struct super_block *sb = pts_sb_from_inode(ptmx_inode);
> > +
> > + atomic_inc(&sb->s_active);
> > + ihold(ptmx_inode);
> > +}
> > +
> > +void devpts_iput_sb_deactive(struct inode *ptmx_inode)
> > +{
> > + struct super_block *sb = pts_sb_from_inode(ptmx_inode);
> > +
> > + iput(ptmx_inode);
> > + deactivate_super(sb);
> > +}
>
> We might as well roll in this functionality into
> devpts_new_index() and devpts_kill_index().
>
> I realize that's muddying the separation of concern.
>
> Alternatively, name the functions for the logical operation
> rather than specifically for what they do (eg. devpts_add_ref())
I renamed the functions and submitted a v2. Not sure if you also want the
move to devpts_new_index()/devpts_kill_index(), I left that as is for now.
I personally don't have much preference here, I left as separate functions
which are called at pty.c because that seemed to be "more logical", in that it's
a tty specific requirement and not tied to devpts fs itself, not sure if it makes
sense (and anyway devpts is only used by tty anyway). It's mostly a cosmetic/
different way of doing thigs, but if required or preferred I can do a v3 and
move/incorporate it into devpts_new_index and devpts_kill_index.
>
> Regards,
> Peter Hurley
>
>
> > +
> > /**
> > * devpts_pty_new -- create a new inode in /dev/pts/
> > * @ptmx_inode: inode of the master
> > diff --git a/include/linux/devpts_fs.h b/include/linux/devpts_fs.h
> > index 251a209..f73ef49 100644
> > --- a/include/linux/devpts_fs.h
> > +++ b/include/linux/devpts_fs.h
> > @@ -19,6 +19,8 @@
> >
> > int devpts_new_index(struct inode *ptmx_inode);
> > void devpts_kill_index(struct inode *ptmx_inode, int idx);
> > +void devpts_ihold_sb_active(struct inode *ptmx_inode);
> > +void devpts_iput_sb_deactive(struct inode *ptmx_inode);
> > /* mknod in devpts */
> > struct inode *devpts_pty_new(struct inode *ptmx_inode, dev_t device, int index,
> > void *priv);
> > @@ -32,6 +34,8 @@ void devpts_pty_kill(struct inode *inode);
> > /* Dummy stubs in the no-pty case */
> > static inline int devpts_new_index(struct inode *ptmx_inode) { return -EINVAL; }
> > static inline void devpts_kill_index(struct inode *ptmx_inode, int idx) { }
> > +static inline void devpts_ihold_sb_active(struct inode *ptmx_inode) { }
> > +static inline void devpts_iput_sb_deactive(struct inode *ptmx_inode) { }
> > static inline struct inode *devpts_pty_new(struct inode *ptmx_inode,
> > dev_t device, int index, void *priv)
> > {
> >
>
--
Regards,
Herton
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web