Skip to contentSkip to conversation

treewide: correct misuses of strscpy/strlcpy

PATCH Applied Joe Perches 7 participants 18 messages Jul 4, 2019 – Jul 15, 2019

Subsystems (13)
lore
Share & tools
lore
#0 lore raw

cover treewide: correct misuses of strscpy/strlcpy

These are all likely copy/paste defects where the field size of the 'copied to' array is incorrect.

Each patch in this series is independent.

Joe Perches (8):
Input: synaptics: Fix misuse of strlcpy
leds: as3645a: Fix misuse of strlcpy
media: m2m-deinterlace: Fix misuse of strscpy
media: go7007: Fix misuse of strscpy
net: ethernet: sun4i-emac: Fix misuse of strlcpy
net: nixge: Fix misuse of strlcpy
tty: hvcs: Fix odd use of strlcpy
nfsd: Fix misuse of strlcpy

 drivers/input/mouse/synaptics.c             | 2 +-
 drivers/leds/leds-as3645a.c                 | 2 +-
 drivers/media/platform/m2m-deinterlace.c    | 2 +-
 drivers/media/usb/go7007/snd-go7007.c       | 2 +-
 drivers/net/ethernet/allwinner/sun4i-emac.c | 4 ++--
 drivers/net/ethernet/ni/nixge.c             | 2 +-
 drivers/tty/hvc/hvcs.c                      | 4 ++--
 fs/nfsd/nfs4idmap.c                         | 2 +-
 8 files changed, 10 insertions(+), 10 deletions(-)
Signature · 1 line
2.15.0
#1 lore raw

PATCH 1/8 Input: synaptics: Fix misuse of strlcpy

Probable cut&paste typo - use the correct field size.

---
 drivers/input/mouse/synaptics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/mouse/synaptics.c +1 -1
index 1080c0c49815..00a0cf14f27f 100644
@@ -705,7 +705,7 @@ static void synaptics_pt_create(struct psmouse *psmouse)
 
 	serio->id.type = SERIO_PS_PSTHRU;
 	strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
-	strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
+	strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->phys));
 	serio->write = synaptics_pt_write;
 	serio->start = synaptics_pt_start;
 	serio->stop = synaptics_pt_stop;
Signature · 1 line
2.15.0
maintainer Applied
#2 lore raw

Re: [PATCH 1/8] Input: synaptics: Fix misuse of strlcpy

Quoted 1 line · Joe Perches

Probable cut&paste typo - use the correct field size.

Applied, thank you.

Luckily both sizes (name and phys) are the same, so we need not to have this in any of the stables.

Quoted 22 lines
---
 drivers/input/mouse/synaptics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/mouse/synaptics.c +1 -1
index 1080c0c49815..00a0cf14f27f 100644
@@ -705,7 +705,7 @@ static void synaptics_pt_create(struct psmouse *psmouse)
 
 	serio->id.type = SERIO_PS_PSTHRU;
 	strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
-	strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
+	strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->phys));
 	serio->write = synaptics_pt_write;
 	serio->start = synaptics_pt_start;
 	serio->stop = synaptics_pt_stop;
Signature · 1 line
2.15.0
Signature · 1 line
Dmitry
#3 lore raw

PATCH 2/8 leds: as3645a: Fix misuse of strlcpy

In reply to Joe Perches

Probable cut&paste typo - use the correct field size.

---
 drivers/leds/leds-as3645a.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/leds-as3645a.c +1 -1
index 14ab6b0e4de9..050088dff8dd 100644
@@ -668,7 +668,7 @@ static int as3645a_v4l2_setup(struct as3645a *flash)
 	};
 
 	strlcpy(cfg.dev_name, led->name, sizeof(cfg.dev_name));
-	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfg.dev_name));
+	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfgind.dev_name));
 
 	flash->vf = v4l2_flash_init(
 		&flash->client->dev, flash->flash_node, &flash->fled, NULL,
Signature · 1 line
2.15.0
Acked-by on v1 2/8
#4 lore raw

Re: [PATCH 2/8] leds: as3645a: Fix misuse of strlcpy

Quoted 3 lines · Joe Perches

Probable cut&paste typo - use the correct field size.

Thanks, Joe!

Quoted 17 lines
---
 drivers/leds/leds-as3645a.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/leds-as3645a.c +1 -1
index 14ab6b0e4de9..050088dff8dd 100644
@@ -668,7 +668,7 @@ static int as3645a_v4l2_setup(struct as3645a *flash)
 	};
 
 	strlcpy(cfg.dev_name, led->name, sizeof(cfg.dev_name));
-	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfg.dev_name));
+	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfgind.dev_name));
 
 	flash->vf = v4l2_flash_init(
 		&flash->client->dev, flash->flash_node, &flash->fled, NULL,
Signature · 3 lines
Kind regards,

Sakari Ailus
#5 lore raw

Re: [PATCH 2/8] leds: as3645a: Fix misuse of strlcpy

In reply to Joe Perches
Quoted 3 lines · Joe Perches

Probable cut&paste typo - use the correct field size.

Ack.
Pavel

Quoted 17 lines
---
 drivers/leds/leds-as3645a.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/leds-as3645a.c +1 -1
index 14ab6b0e4de9..050088dff8dd 100644
@@ -668,7 +668,7 @@ static int as3645a_v4l2_setup(struct as3645a *flash)
 	};
 
 	strlcpy(cfg.dev_name, led->name, sizeof(cfg.dev_name));
-	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfg.dev_name));
+	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfgind.dev_name));
 
 	flash->vf = v4l2_flash_init(
 		&flash->client->dev, flash->flash_node, &flash->fled, NULL,
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#6 lore raw

Re: [PATCH 2/8] leds: as3645a: Fix misuse of strlcpy

In reply to Joe Perches

Hi Joe,

Thank you for the patch.

Quoted 13 lines · Joe Perches

Probable cut&paste typo - use the correct field size.

---
 drivers/leds/leds-as3645a.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/leds-as3645a.c +0 -0
index 14ab6b0e4de9..050088dff8dd 100644

drivers/leds/leds-as3645a.c static int as3645a_v4l2_setup(struct as3645a *flash) · line 674

Quoted 8 lines
drivers/leds/leds-as3645a.c +1 -1
@@ -668,7 +668,7 @@ static int as3645a_v4l2_setup(struct as3645a *flash)
 	};
 
 	strlcpy(cfg.dev_name, led->name, sizeof(cfg.dev_name));
-	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfg.dev_name));
+	strlcpy(cfgind.dev_name, flash->iled_cdev.name, sizeof(cfgind.dev_name));
 
 	flash->vf = v4l2_flash_init(
 		&flash->client->dev, flash->flash_node, &flash->fled, NULL,

Applied to for-5.4 branch of linux-leds.git.

Signature · 2 lines
Best regards,
Jacek Anaszewski
#7 lore raw

PATCH 3/8 media: m2m-deinterlace: Fix misuse of strscpy

In reply to Joe Perches

Probable cut&paste typo - use the correct field size.

---
 drivers/media/platform/m2m-deinterlace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/m2m-deinterlace.c +1 -1
index beb7fd7442fb..dd573d23d0ce 100644
@@ -436,7 +436,7 @@ static int vidioc_querycap(struct file *file, void *priv,
 {
 	strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
 	strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
-	strscpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->card));
+	strscpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->bus_info));
 	/*
 	 * This is only a mem-to-mem video device. The capture and output
 	 * device capability flags are left only for backward compatibility
Signature · 1 line
2.15.0
#8 lore raw

PATCH 4/8 media: go7007: Fix misuse of strscpy

In reply to Joe Perches

Probable cut&paste typo - use the correct field size.

---
 drivers/media/usb/go7007/snd-go7007.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/go7007/snd-go7007.c +1 -1
index 4a449c62fc32..b05fa227ffb2 100644
@@ -253,7 +253,7 @@ int go7007_snd_init(struct go7007 *go)
 		return ret;
 	}
 	strscpy(gosnd->card->driver, "go7007", sizeof(gosnd->card->driver));
-	strscpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->driver));
+	strscpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->shortname));
 	strscpy(gosnd->card->longname, gosnd->card->shortname,
 		sizeof(gosnd->card->longname));
 
Signature · 1 line
2.15.0
#9 lore raw

PATCH 5/8 net: ethernet: sun4i-emac: Fix misuse of strlcpy

In reply to Joe Perches

Probable cut&paste typo - use the correct field size.

---
 drivers/net/ethernet/allwinner/sun4i-emac.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/allwinner/sun4i-emac.c +2 -2
index 9e06dff619c3..40a359dd90b4 100644
@@ -224,8 +224,8 @@ static int emac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 static void emac_get_drvinfo(struct net_device *dev,
 			      struct ethtool_drvinfo *info)
 {
-	strlcpy(info->driver, DRV_NAME, sizeof(DRV_NAME));
-	strlcpy(info->version, DRV_VERSION, sizeof(DRV_VERSION));
+	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
+	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
 	strlcpy(info->bus_info, dev_name(&dev->dev), sizeof(info->bus_info));
 }
 
Signature · 1 line
2.15.0
#11 lore raw

PATCH 6/8 net: nixge: Fix misuse of strlcpy

In reply to Joe Perches

Probable cut&paste typo - use the correct field size.

---
 drivers/net/ethernet/ni/nixge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ni/nixge.c +1 -1
index 96f7a9818294..0b384f97d2fd 100644
@@ -990,7 +990,7 @@ static void nixge_ethtools_get_drvinfo(struct net_device *ndev,
 				       struct ethtool_drvinfo *ed)
 {
 	strlcpy(ed->driver, "nixge", sizeof(ed->driver));
-	strlcpy(ed->bus_info, "platform", sizeof(ed->driver));
+	strlcpy(ed->bus_info, "platform", sizeof(ed->bus_info));
 }
 
 static int nixge_ethtools_get_coalesce(struct net_device *ndev,
Signature · 1 line
2.15.0
#13 lore raw

PATCH 7/8 tty: hvcs: Fix odd use of strlcpy

In reply to Joe Perches

Use the typical style of array, not the equivalent &array[0].

---
 drivers/tty/hvc/hvcs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/hvc/hvcs.c +2 -2
index cb4db1b3ca3c..b6c1c1be06f9 100644
@@ -871,8 +871,8 @@ static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
 	hvcsd->p_partition_ID  = pi->partition_ID;
 
 	/* copy the null-term char too */
-	strlcpy(&hvcsd->p_location_code[0],
-			&pi->location_code[0], sizeof(hvcsd->p_location_code));
+	strlcpy(hvcsd->p_location_code, pi->location_code,
+		sizeof(hvcsd->p_location_code));
 }
 
 /*
Signature · 1 line
2.15.0
#14 lore raw

PATCH 8/8 nfsd: Fix misuse of strlcpy

In reply to Joe Perches

Probable cut&paste typo - use the correct field size.

---
 fs/nfsd/nfs4idmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfsd/nfs4idmap.c +1 -1
index 2961016097ac..d1f285245af8 100644
@@ -83,7 +83,7 @@ ent_init(struct cache_head *cnew, struct cache_head *citm)
 	new->type = itm->type;
 
 	strlcpy(new->name, itm->name, sizeof(new->name));
-	strlcpy(new->authname, itm->authname, sizeof(new->name));
+	strlcpy(new->authname, itm->authname, sizeof(new->authname));
 }
 
 static void
Signature · 1 line
2.15.0
#15 lore raw

Re: [PATCH 8/8] nfsd: Fix misuse of strlcpy

Quoted 1 line · Joe Perches

Probable cut&paste typo - use the correct field size.

Huh, that's been there forever, I wonder why we haven't seen crashes? Oh, I see, name and authname both have the same size.

Anyway, makes sense, thanks. Will apply for 5.3.

(Unless someone else is getting this; I didn't get copied on the rest of the series.)

--b.

Quoted 21 lines
---
 fs/nfsd/nfs4idmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfsd/nfs4idmap.c +1 -1
index 2961016097ac..d1f285245af8 100644
@@ -83,7 +83,7 @@ ent_init(struct cache_head *cnew, struct cache_head *citm)
 	new->type = itm->type;
 
 	strlcpy(new->name, itm->name, sizeof(new->name));
-	strlcpy(new->authname, itm->authname, sizeof(new->name));
+	strlcpy(new->authname, itm->authname, sizeof(new->authname));
 }
 
 static void
Signature · 1 line
2.15.0
#16 lore raw

Re: [PATCH 8/8] nfsd: Fix misuse of strlcpy

Quoted 10 lines · J. Bruce Fields
Quoted 1 line · Joe Perches

Probable cut&paste typo - use the correct field size.

Huh, that's been there forever, I wonder why we haven't seen crashes? Oh, I see, name and authname both have the same size.

Anyway, makes sense, thanks. Will apply for 5.3.

(Unless someone else is getting this; I didn't get copied on the rest of the series.)

It's generally hard to cc everyone on treewide fixes like this.

There's no good mechanism I know of.
vger mailing lists reject emails with too many addressees.

Do you have an opinion on adding the stracpy macro which could avoid many of these defects?

---
 include/linux/string.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/string.h +16 -0
index 4deb11f7976b..ef01bd6f19df 100644
@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);
 /* Wraps calls to strscpy()/memset(), no arch specific code required */
 ssize_t strscpy_pad(char *dest, const char *src, size_t count);
 
+#define stracpy(to, from)					\
+({								\
+	size_t size = ARRAY_SIZE(to);				\
+	BUILD_BUG_ON(!__same_type(typeof(*to), char));		\
+								\
+	strscpy(to, from, size);				\
+})
+
+#define stracpy_pad(to, from)					\
+({								\
+	size_t size = ARRAY_SIZE(to);				\
+	BUILD_BUG_ON(!__same_type(typeof(*to), char));		\
+								\
+	strscpy_pad(to, from, size);				\
+})
+
 #ifndef __HAVE_ARCH_STRCAT
 extern char * strcat(char *, const char *);
 #endif
#17 lore raw

Re: [PATCH 8/8] nfsd: Fix misuse of strlcpy

Quoted 9 lines · Joe Perches
Quoted 8 lines · J. Bruce Fields
Quoted 1 line · Joe Perches

Probable cut&paste typo - use the correct field size.

Huh, that's been there forever, I wonder why we haven't seen crashes? Oh, I see, name and authname both have the same size.

Anyway, makes sense, thanks. Will apply for 5.3.

Quoted 7 lines
Quoted 2 lines

(Unless someone else is getting this; I didn't get copied on the rest of the series.)

It's generally hard to cc everyone on treewide fixes like this.

There's no good mechanism I know of.
vger mailing lists reject emails with too many addressees.

Yeah. I guess what I don't understand is why this patch is part of a series at all. It makes me wonder if there's some dependency I missed or if the 0/8 mail actually asked somebody else to apply it.

Whatever, I guess I'm being silly, it clearly stands alone. Applying for 5.3.

Quoted 2 lines

Do you have an opinion on adding the stracpy macro which could avoid many of these defects?

I don't have an opinion.

--b.

Quoted 33 lines
---
 include/linux/string.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/string.h +16 -0
index 4deb11f7976b..ef01bd6f19df 100644
@@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t);
 /* Wraps calls to strscpy()/memset(), no arch specific code required */
 ssize_t strscpy_pad(char *dest, const char *src, size_t count);
 
+#define stracpy(to, from)					\
+({								\
+	size_t size = ARRAY_SIZE(to);				\
+	BUILD_BUG_ON(!__same_type(typeof(*to), char));		\
+								\
+	strscpy(to, from, size);				\
+})
+
+#define stracpy_pad(to, from)					\
+({								\
+	size_t size = ARRAY_SIZE(to);				\
+	BUILD_BUG_ON(!__same_type(typeof(*to), char));		\
+								\
+	strscpy_pad(to, from, size);				\
+})
+
 #ifndef __HAVE_ARCH_STRCAT
 extern char * strcat(char *, const char *);
 #endif