Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gfs2: Fix error path kobject memory leak

If a call to kobject_init_and_add() fails we must call kobject_put()
otherwise we leak memory.

Function gfs2_sys_fs_add always calls kobject_init_and_add() which
always calls kobject_init().

It is safe to leave object destruction up to the kobject release
function and never free it manually.

Remove call to kfree() and always call kobject_put() in the error path.

Signed-off-by: Tobin C. Harding <tobin@kernel.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Tobin C. Harding and committed by
Linus Torvalds
fbcde197 3aff5fac

+1 -7
+1 -7
fs/gfs2/sys.c
··· 650 650 char ro[20]; 651 651 char spectator[20]; 652 652 char *envp[] = { ro, spectator, NULL }; 653 - int sysfs_frees_sdp = 0; 654 653 655 654 sprintf(ro, "RDONLY=%d", sb_rdonly(sb)); 656 655 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0); ··· 660 661 if (error) 661 662 goto fail_reg; 662 663 663 - sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling 664 - function gfs2_sbd_release. */ 665 664 error = sysfs_create_group(&sdp->sd_kobj, &tune_group); 666 665 if (error) 667 666 goto fail_reg; ··· 684 687 fail_reg: 685 688 free_percpu(sdp->sd_lkstats); 686 689 fs_err(sdp, "error %d adding sysfs files\n", error); 687 - if (sysfs_frees_sdp) 688 - kobject_put(&sdp->sd_kobj); 689 - else 690 - kfree(sdp); 690 + kobject_put(&sdp->sd_kobj); 691 691 sb->s_fs_info = NULL; 692 692 return error; 693 693 }