Skip to contentSkip to conversation

m68k: emu: Replace unbounded sprintf in nfhd_init_one

PATCH RESEND In review Thorsten Blum 3 participants 3 messages Mar 18 – Mar 30 m68k architecture

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 m68k: emu: Replace unbounded sprintf in nfhd_init_one

Replace unbounded sprintf() with the safer snprintf().

---
 arch/m68k/emu/nfblock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/m68k/emu/nfblock.c +2 -1
index 93536cf2a38e..fa6256c5af22 100644
@@ -132,7 +132,8 @@ static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)
 	dev->disk->minors = 16;
 	dev->disk->fops = &nfhd_ops;
 	dev->disk->private_data = dev;
-	sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
+	snprintf(dev->disk->disk_name, sizeof(dev->disk->disk_name), "nfhd%u",
+		 dev_id);
 	set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
 	err = add_disk(dev->disk);
 	if (err)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Thorsten Blum

Replace unbounded sprintf() with the safer snprintf().

---
 arch/m68k/emu/nfblock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/m68k/emu/nfblock.c +0 -0
index 93536cf2a38e..fa6256c5af22 100644

arch/m68k/emu/nfblock.c static int __init nfhd_init_one(int id, u32 blocks, u32 bsize) · line 139

Quoted 9 lines
arch/m68k/emu/nfblock.c +2 -1
@@ -132,7 +132,8 @@ static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)
 	dev->disk->minors = 16;
 	dev->disk->fops = &nfhd_ops;
 	dev->disk->private_data = dev;
-	sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
+	snprintf(dev->disk->disk_name, sizeof(dev->disk->disk_name), "nfhd%u",
+		 dev_id);
 	set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
 	err = add_disk(dev->disk);
 	if (err)

This one falls into a "currently impossible" category:

        for (i = NFHD_DEV_OFFSET; i < 24; i++) {
                if (nfhd_get_capacity(i, 0, &blocks, &bsize))
                        continue;
                nfhd_init_one(i, blocks, bsize);

static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)

{
	...
        int dev_id = id - NFHD_DEV_OFFSET;
	...
        sprintf(dev->disk->disk_name, "nfhd%u", dev_id);

I'd almost prefer to see "id" bounds checked prior to the dev_id calculation (in some unlikely future where "id" isn't NFHD_DEV_OFFSET-based).

#define DISK_NAME_LEN 32
...
char disk_name[DISK_NAME_LEN]; /* name of major driver */

The largest the %u could get would be 10 digits, so it'll always be in bounds.

But there's no harm in the change:

Signature · 1 line
Kees Cook
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Thorsten Blum
Quoted 3 lines · Thorsten Blum

Replace unbounded sprintf() with the safer snprintf().

i.e. will queue in the m68k tree for v7.1.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds