Skip to content

Commit f56426a

Browse files
committed
ext3: Fix memory leak when quota options are specified multiple times
When usrjquota or grpjquota mount options are specified several times, we leak memory storing the names. Free the memory correctly. Reported-by: Chen Gang <gang.chen@asianux.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 306a749 commit f56426a

1 file changed

Lines changed: 30 additions & 21 deletions

File tree

‎fs/ext3/super.c‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -916,21 +916,24 @@ static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
916916
"Not enough memory for storing quotafile name");
917917
return 0;
918918
}
919-
if (sbi->s_qf_names[qtype] &&
920-
strcmp(sbi->s_qf_names[qtype], qname)) {
921-
ext3_msg(sb, KERN_ERR,
922-
"%s quota file already specified", QTYPE2NAME(qtype));
919+
if (sbi->s_qf_names[qtype]) {
920+
int same = !strcmp(sbi->s_qf_names[qtype], qname);
921+
923922
kfree(qname);
924-
return 0;
923+
if (!same) {
924+
ext3_msg(sb, KERN_ERR,
925+
"%s quota file already specified",
926+
QTYPE2NAME(qtype));
927+
}
928+
return same;
925929
}
926-
sbi->s_qf_names[qtype] = qname;
927-
if (strchr(sbi->s_qf_names[qtype], '/')) {
930+
if (strchr(qname, '/')) {
928931
ext3_msg(sb, KERN_ERR,
929932
"quotafile must be on filesystem root");
930-
kfree(sbi->s_qf_names[qtype]);
931-
sbi->s_qf_names[qtype] = NULL;
933+
kfree(qname);
932934
return 0;
933935
}
936+
sbi->s_qf_names[qtype] = qname;
934937
set_opt(sbi->s_mount_opt, QUOTA);
935938
return 1;
936939
}
@@ -945,11 +948,10 @@ static int clear_qf_name(struct super_block *sb, int qtype) {
945948
" when quota turned on");
946949
return 0;
947950
}
948-
/*
949-
* The space will be released later when all options are confirmed
950-
* to be correct
951-
*/
952-
sbi->s_qf_names[qtype] = NULL;
951+
if (sbi->s_qf_names[qtype]) {
952+
kfree(sbi->s_qf_names[qtype]);
953+
sbi->s_qf_names[qtype] = NULL;
954+
}
953955
return 1;
954956
}
955957
#endif
@@ -2605,7 +2607,18 @@ static int ext3_remount (struct super_block * sb, int * flags, char * data)
26052607
#ifdef CONFIG_QUOTA
26062608
old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
26072609
for (i = 0; i < MAXQUOTAS; i++)
2608-
old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2610+
if (sbi->s_qf_names[i]) {
2611+
old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
2612+
GFP_KERNEL);
2613+
if (!old_opts.s_qf_names[i]) {
2614+
int j;
2615+
2616+
for (j = 0; j < i; j++)
2617+
kfree(old_opts.s_qf_names[j]);
2618+
return -ENOMEM;
2619+
}
2620+
} else
2621+
old_opts.s_qf_names[i] = NULL;
26092622
#endif
26102623

26112624
/*
@@ -2698,9 +2711,7 @@ static int ext3_remount (struct super_block * sb, int * flags, char * data)
26982711
#ifdef CONFIG_QUOTA
26992712
/* Release old quota file names */
27002713
for (i = 0; i < MAXQUOTAS; i++)
2701-
if (old_opts.s_qf_names[i] &&
2702-
old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2703-
kfree(old_opts.s_qf_names[i]);
2714+
kfree(old_opts.s_qf_names[i]);
27042715
#endif
27052716
if (enable_quota)
27062717
dquot_resume(sb, -1);
@@ -2714,9 +2725,7 @@ static int ext3_remount (struct super_block * sb, int * flags, char * data)
27142725
#ifdef CONFIG_QUOTA
27152726
sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
27162727
for (i = 0; i < MAXQUOTAS; i++) {
2717-
if (sbi->s_qf_names[i] &&
2718-
old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2719-
kfree(sbi->s_qf_names[i]);
2728+
kfree(sbi->s_qf_names[i]);
27202729
sbi->s_qf_names[i] = old_opts.s_qf_names[i];
27212730
}
27222731
#endif

0 commit comments

Comments
 (0)