Skip to content

Commit 2c47c1b

Browse files
AstralBobagruenba
authored andcommitted
gfs2: clean up iopen glock mess in gfs2_create_inode
Before this patch, gfs2_create_inode had a use-after-free for the iopen glock in some error paths because it did this: gfs2_glock_put(io_gl); fail_gunlock2: if (io_gl) clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags); In some cases, the io_gl was used for create and only had one reference, so the glock might be freed before the clear_bit(). This patch tries to straighten it out by only jumping to the error paths where iopen is properly set, and moving the gfs2_glock_put after the clear_bit. Signed-off-by: Bob Peterson <rpeterso@redhat.com> Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
1 parent d99724c commit 2c47c1b

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎fs/gfs2/inode.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
712712

713713
error = gfs2_trans_begin(sdp, blocks, 0);
714714
if (error)
715-
goto fail_gunlock2;
715+
goto fail_free_inode;
716716

717717
if (blocks > 1) {
718718
ip->i_eattr = ip->i_no_addr + 1;
@@ -723,7 +723,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
723723

724724
error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
725725
if (error)
726-
goto fail_gunlock2;
726+
goto fail_free_inode;
727727

728728
BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
729729

@@ -732,7 +732,6 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
732732
goto fail_gunlock2;
733733

734734
glock_set_object(ip->i_iopen_gh.gh_gl, ip);
735-
gfs2_glock_put(io_gl);
736735
gfs2_set_iop(inode);
737736
insert_inode_hash(inode);
738737

@@ -765,22 +764,24 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
765764

766765
mark_inode_dirty(inode);
767766
d_instantiate(dentry, inode);
767+
/* After instantiate, errors should result in evict which will destroy
768+
* both inode and iopen glocks properly. */
768769
if (file) {
769770
file->f_mode |= FMODE_CREATED;
770771
error = finish_open(file, dentry, gfs2_open_common);
771772
}
772773
gfs2_glock_dq_uninit(ghs);
773774
gfs2_glock_dq_uninit(ghs + 1);
774775
clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
776+
gfs2_glock_put(io_gl);
775777
return error;
776778

777779
fail_gunlock3:
778780
glock_clear_object(io_gl, ip);
779781
gfs2_glock_dq_uninit(&ip->i_iopen_gh);
780-
gfs2_glock_put(io_gl);
781782
fail_gunlock2:
782-
if (io_gl)
783-
clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
783+
clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
784+
gfs2_glock_put(io_gl);
784785
fail_free_inode:
785786
if (ip->i_gl) {
786787
glock_clear_object(ip->i_gl, ip);

0 commit comments

Comments
 (0)