afs: replace deprecated strcpy with strscpy in afs_lookup_atsys
PATCH 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.
Signed-off-by Thorsten Blum <[email protected]>
--- fs/afs/dir.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/afs/dir.c +3 -4
@@ -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>
@@ -914,7 +915,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 ("" );
@@ -935,13 +936,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;
PATCH 2/2 afs: replace goto with direct return in afs_lookup_atsys
Return ERR_PTR(-ENOMEM) directly and remove the obsolete 'out_p' label.
Signed-off-by Thorsten Blum <[email protected]>
--- fs/afs/dir.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
fs/afs/dir.c +1 -3
@@ -919,10 +919,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 ;
@@ -954,7 +953,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; }