Skip to contentSkip to conversation

afs: replace deprecated strcpy with strscpy in afs_lookup_atsys

PATCH RESEND New Thorsten Blum 1 participant 2 messages Jun 6 Afs filesystem

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/2 afs: replace deprecated strcpy with strscpy in afs_lookup_atsys

strcpy() has been deprecated [1] because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Replace it with the safer strscpy(), drop the manual length check and the now-unused local length variable.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy

---
 fs/afs/dir.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/afs/dir.c +3 -4
index 498b99ccdf0e..080664b67861 100644
@@ -9,6 +9,7 @@
 #include <linux/fs.h>
 #include <linux/namei.h>
 #include <linux/pagemap.h>
+#include <linux/string.h>
 #include <linux/swap.h>
 #include <linux/ctype.h>
 #include <linux/sched.h>
@@ -901,7 +902,7 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
 	struct afs_net *net = afs_i2net(dir);
 	struct dentry *ret;
 	char *buf, *p, *name;
-	int len, i;
+	int i;
 
 	_enter("");
 
@@ -922,13 +923,11 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
 
 	for (i = 0; i < subs->nr; i++) {
 		name = subs->subs[i];
-		len = dentry->d_name.len - 4 + strlen(name);
-		if (len >= AFSNAMEMAX) {
+		if (strscpy(p, name, AFSNAMEMAX - (p - buf)) < 0) {
 			ret = ERR_PTR(-ENAMETOOLONG);
 			goto out_s;
 		}
 
-		strcpy(p, name);
 		ret = lookup_noperm(&QSTR(buf), dentry->d_parent);
 		if (IS_ERR(ret) || d_is_positive(ret))
 			goto out_s;
#1 lore raw

PATCH RESEND 2/2 afs: replace goto with direct return in afs_lookup_atsys

Return ERR_PTR(-ENOMEM) directly and remove the obsolete out_p label.

---
 fs/afs/dir.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/afs/dir.c +1 -3
index 080664b67861..bc50f00c0c69 100644
@@ -906,10 +906,9 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
 
 	_enter("");
 
-	ret = ERR_PTR(-ENOMEM);
 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
 	if (!buf)
-		goto out_p;
+		return ERR_PTR(-ENOMEM);
 	if (dentry->d_name.len > 4) {
 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
 		p += dentry->d_name.len - 4;
@@ -941,7 +940,6 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
 out_s:
 	afs_put_sysnames(subs);
 	kfree(buf);
-out_p:
 	return ret;
 }