Prevent memory leak in devpts
PATCH 1/1 Prevent memory leak in devpts
Hi,
There is a dentry refcount leak in devpts_get_tty.
struct tty_struct *devpts_get_tty(int number)
{
struct dentry *dentry = get_node(number);
struct tty_struct *tty;
tty = (IS_ERR(dentry) || !dentry->d_inode) ? NULL :
dentry->d_inode->u.generic_ip;
up(&devpts_root->d_inode->i_sem);
return tty;
}The get_node function does a lookup on /dev/pts/<number> and returns the dentry, taking a reference. We should dput the dentry after extracting the tty pointer.
The attached patch does this.
Olaf
Signature · 3 lines
Olaf Kirch | The Hardware Gods hate me. [email protected] | ---------------+
fs/devpts/inode.c +1 -0
@@ -183,6 +183,7 @@ struct tty_struct *devpts_get_tty(int nu dentry->d_inode->u.generic_ip;up (&devpts_root->d_inode->i_sem); +dput (dentry);return tty; }
This is a lame self-followup. Kurt Garloff pointed out that I should obviously handle the error case more gracefully than by oopsing. Improved patch attached.
Olaf
Signature · 3 lines
Olaf Kirch | The Hardware Gods hate me. [email protected] | ---------------+
fs/devpts/inode.c +7 -3
@@ -178,9 +178,13 @@ struct tty_struct *devpts_get_tty(int nu {struct dentry *dentry =get_node (number);struct tty_struct *tty; - - tty = (IS_ERR (dentry) || !dentry->d_inode) ?NULL : - dentry->d_inode->u.generic_ip; + + tty =NULL ; +if (!IS_ERR (dentry)) { +if (dentry->d_inode) + tty = dentry->d_inode->u.generic_ip; +dput (dentry); + }up (&devpts_root->d_inode->i_sem);