diff mbox series

ext4: do the quotafile name safe check before allocating new string

Message ID 1603723397-14344-1-git-send-email-kaixuxia@tencent.com
State New
Headers show
Series ext4: do the quotafile name safe check before allocating new string | expand

Commit Message

kaixuxia Oct. 26, 2020, 2:43 p.m. UTC
From: Kaixu Xia <kaixuxia@tencent.com>

Now we do the quotafile name safe check after allocating the new string
by using kmalloc(), and have to release the string with kfree() if check
fails. Maybe we can check them before allocating memory and directly
return error if check fails to avoid the unnecessary kmalloc()/kfree()
operations.

Signed-off-by: Kaixu Xia <kaixuxia@tencent.com>
---
 fs/ext4/super.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 5308f0d5fb5a..83fdde498414 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1861,7 +1861,6 @@  static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
 {
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
-	int ret = -1;
 
 	if (sb_any_quota_loaded(sb) && !old_qname) {
 		ext4_msg(sb, KERN_ERR,
@@ -1874,32 +1873,30 @@  static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
 			 "ignored when QUOTA feature is enabled");
 		return 1;
 	}
-	qname = match_strdup(args);
-	if (!qname) {
-		ext4_msg(sb, KERN_ERR,
-			"Not enough memory for storing quotafile name");
-		return -1;
-	}
 	if (old_qname) {
-		if (strcmp(old_qname, qname) == 0)
-			ret = 1;
-		else
+		if (strlen(old_qname) != args->to - args->from ||
+		    strncmp(old_qname, args->from, args->to - args->from)) {
 			ext4_msg(sb, KERN_ERR,
 				 "%s quota file already specified",
 				 QTYPE2NAME(qtype));
-		goto errout;
+			return -1;
+		}
+		return 1;
 	}
-	if (strchr(qname, '/')) {
+	if (strnchr(args->from, args->to - args->from, '/')) {
 		ext4_msg(sb, KERN_ERR,
 			"quotafile must be on filesystem root");
-		goto errout;
+		return -1;
+	}
+	qname = match_strdup(args);
+	if (!qname) {
+		ext4_msg(sb, KERN_ERR,
+			"Not enough memory for storing quotafile name");
+		return -1;
 	}
 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
 	set_opt(sb, QUOTA);
 	return 1;
-errout:
-	kfree(qname);
-	return ret;
 }
 
 static int clear_qf_name(struct super_block *sb, int qtype)