Skip to contentSkip to conversation

3c505.c: replacing strcpy to strncpy

PATCH In review Douglas Schilling Landgraf 6 participants 12 messages Feb 25, 2007 – Feb 27, 2007 Kernel janitors

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 3c505.c: replacing strcpy to strncpy

Use strncpy instead of strcpy.

Index: linux-2.6.20.1/drivers/net/3c505.c =================================--- linux-2.6.20.1.orig/drivers/net/3c505.c +++ linux-2.6.20.1/drivers/net/3c505.c
@@ -1154,8 +1154,8 @@ static struct net_device_stats *elp_get_
static void netdev_get_drvinfo(struct net_device *dev,
struct ethtool_drvinfo *info)
{
- strcpy(info->driver, DRV_NAME);
- strcpy(info->version, DRV_VERSION);
+ strncpy(info->driver, DRV_NAME, sizeof(info->driver)-1);
+ strncpy(info->version, DRV_VERSION, sizeof(info->driver)-1);
sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
}

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#1 lore raw
Quoted 8 lines · Douglas Schilling Landgraf

Use strncpy instead of strcpy.

Index: linux-2.6.20.1/drivers/net/3c505.c =================================> --- linux-2.6.20.1.orig/drivers/net/3c505.c +++ linux-2.6.20.1/drivers/net/3c505.c

Review static struct net_device_stats *elp_get_ · line 1158

Quoted 7 lines
(quoted diff) +2 -2
@@ -1154,8 +1154,8 @@ static struct net_device_stats *elp_get_
 static void netdev_get_drvinfo(struct net_device *dev,
                               struct ethtool_drvinfo *info)
 {
-       strcpy(info->driver, DRV_NAME);
-       strcpy(info->version, DRV_VERSION);
+       strncpy(info->driver, DRV_NAME, sizeof(info->driver)-1);
+       strncpy(info->version, DRV_VERSION, sizeof(info->driver)-1);
          ^^^^^^^^^^

shouldn't this be info-version instead

Signature · 5 lines
Milind Arun Choudhary
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
#2 lore raw

PATCH 1/1 3c505.c: replacing strcpy to strncpy

In reply to Douglas Schilling Landgraf

Use strncpy instead of strcpy.

Index: linux-2.6.20.1/drivers/net/3c505.c =================================--- linux-2.6.20.1.orig/drivers/net/3c505.c +++ linux-2.6.20.1/drivers/net/3c505.c
@@ -1154,8 +1154,8 @@ static struct net_device_stats *elp_get_
static void netdev_get_drvinfo(struct net_device *dev,
struct ethtool_drvinfo *info)
{
- strcpy(info->driver, DRV_NAME);
- strcpy(info->version, DRV_VERSION);
+ strncpy(info->driver, DRV_NAME, sizeof(info->driver)-1);
+ strncpy(info->version, DRV_VERSION, sizeof(info->version)-1);
sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
}

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#4 lore raw
In reply to Douglas Schilling Landgraf

char buf[150];

strcpy(buf, "LALA");

that's one reason, for example

Quoted 10 lines · On 2/27/07, [email protected] <[email protected]> wrote:
Quoted 1 line · Douglas Schilling Landgraf

Use strncpy instead of strcpy.

Is there ever a reason to use strcpy() over strncpy()? If not, there's probably another todo item here:

~/src/linux-2.6 $ grep -r strcpy . | wc -l
2592

Quoted 6 lines
                --Ethan

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#5 lore raw
In reply to Douglas Schilling Landgraf
Quoted 7 lines · * [email protected] <[email protected]> [070227 01:33]:
Quoted 1 line

Use strncpy instead of strcpy.

Is there ever a reason to use strcpy() over strncpy()? If not, there's probably another todo item here:

~/src/linux-2.6 $ grep -r strcpy . | wc -l
2592

Replacing a strcpy with a strncpy usually makes code more buggy than it was before, as strncpy does not generate 0-terminated strings when the input is too long. (Unless the kernel names something strncpy which is no strncpy, I've no kernel source unpackages around, but I doubt that). Even if the output is 0-terminated, simply truncating it can be a security problem in some cases. (And a problem in most cases)

strcpy is good if you are sure something fits in the destination. If not, you should test before and if it fits not, give an appropiate response, either truncate where it is save, or simply fail. (And when you tested its length and still have that around, you need no strcpy or strncpy but a memcpy can be used, making it even faster on some arches).

So I'd rather suggest looking for strncpy and checking those. I don't know what the state of strncpy in the kernel is, but in an avarage project strncpy usage typically means a bug.

Hochachtungsvoll,
Bernhard R. Link
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#6 lore raw
In reply to Douglas Schilling Landgraf
Quoted 5 lines · Standard Azi

char buf[150];

strcpy(buf, "LALA");

that's one reason, for example

char buf[150] = "LALA";

and if you really need to do it outside of initialization, does the strncpy() really hurt it? What if the size of `buf' gets changed, then we would have to go check for all strcpy()s and make sure that they are still valid, where strncpy would do it by default.

	--Ethan

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#7 lore raw
In reply to Douglas Schilling Landgraf
Quoted 13 lines · Bernhard R. Link

Replacing a strcpy with a strncpy usually makes code more buggy than it was before, as strncpy does not generate 0-terminated strings when the input is too long. (Unless the kernel names something strncpy which is no strncpy, I've no kernel source unpackages around, but I doubt that). Even if the output is 0-terminated, simply truncating it can be a security problem in some cases. (And a problem in most cases)

strcpy is good if you are sure something fits in the destination. If not, you should test before and if it fits not, give an appropiate response, either truncate where it is save, or simply fail. (And when you tested its length and still have that around, you need no strcpy or strncpy but a memcpy can be used, making it even faster on some arches).

Quoted 3 lines

So I'd rather suggest looking for strncpy and checking those. I don't know what the state of strncpy in the kernel is, but in an avarage project strncpy usage typically means a bug.

Looks like strlcpy() would solve both these problems. It guarentees that the destination string is null terminated and it returns the strlen() of the source string:

	ssize = strlcpy(dest, src, dsize);
	if (ssize >= dsize - 1)
		return ERROR;

Here we have dest guarenteed to be terminated, and if the source string is greater than the dest - 1 (for the '\0'), we can bail out.

		--Ethan

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#8 lore raw
In reply to Douglas Schilling Landgraf
Quoted 1 line · * [email protected] <[email protected]> [070227 17:41]:

Looks like strlcpy() would solve both these problems.

While there may be cases where it is useful, it normaly solves a problem that is not there.

        ssize = strlen(src) + 1;
        if( ssize > dsize )
                return ERROR;
        memcpy(dest, src, ssize);

is the same like

Quoted 6 lines
	ssize = strlcpy(dest, src, dsize);
	if (ssize >= dsize - 1)
		return ERROR;

Here we have dest guarenteed to be terminated, and if the source string is greater than the dest - 1 (for the '\0'), we can bail out.

except you test for greater or equal instead of greater ;->

Hochachtungsvoll,
Bernhard R. Link
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#9 lore raw
In reply to Douglas Schilling Landgraf
Quoted 6 lines · Bernhard R. Link
Quoted 1 line · * [email protected] <[email protected]> [070227 17:41]:

Looks like strlcpy() would solve both these problems.

While there may be cases where it is useful, it normally solves a problem that is not there.

Quoted 13 lines
        ssize = strlen(src) + 1;
        if( ssize > dsize )
                return ERROR;
        memcpy(dest, src, ssize);

is the same like

Quoted 6 lines
	ssize = strlcpy(dest, src, dsize);
	if (ssize >= dsize - 1)
		return ERROR;

Here we have dest guaranteed to be terminated, and if the source string is greater than the dest - 1 (for the '\0'), we can bail out.

Sure, however your way allows for the dest buff length test to be left off (forgotten). If the convention was to use strlcpy() the test is forced by requiring the length argument to be passed. strlcpy() uses memcpy() under-the-hood anyway so there's no real efficiency differences here, AFAIK.

Also, the strlcpy() version is 1 line shorter (which is not *terribly* important) and arguably more readable (not a very strong argument).

Finally if truncation is not a problem, strlcpy() handles this case just as fine as anything and is probably preferable to a memcpy() with a length test. So might as well use strlcpy() in both places for consistency.

                --Ethan

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#10 lore raw
In reply to Douglas Schilling Landgraf

Hi guys,

I agree with Ethan, we can use strlcpy() instead of
strcpy()/strncpy()/memcpy() in this case.

Nowadays, we already have two modules using this function:
[douglas@localhost net]$ grep strlcpy *.c

acenic.c:       strlcpy(info->driver, "acenic", sizeof(info->driver));
acenic.c:               strlcpy(info->bus_info, pci_name(ap->pdev),
acenic.mod.c:   { 0x73e20c1c, "strlcpy" },
hp100.c:        strlcpy(lp->id, eid, HP100_SIG_LEN);

Note this part of strlcpy article from http://lwn.net:

"Linus agreed that following OpenBSD's lead was the right way forward, and strlcpy() is in his BitKeeper repository, waiting for 2.5.71. There has also been a flurry of activity to convert kernel code over to the new function. By the time 2.6.0 comes out, strncpy() may no longer have a place in the Linux kernel."

Source: http://lwn.net/Articles/33812/

Sincerely,
Douglas Schilling Landgraf

Quoted 34 lines · On 2/27/07, [email protected] <[email protected]> wrote:
Quoted 6 lines · Bernhard R. Link
Quoted 1 line · * [email protected] <[email protected]> [070227 17:41]:

Looks like strlcpy() would solve both these problems.

While there may be cases where it is useful, it normally solves a problem that is not there.

Quoted 13 lines
        ssize = strlen(src) + 1;
        if( ssize > dsize )
                return ERROR;
        memcpy(dest, src, ssize);

is the same like

Quoted 6 lines
    ssize = strlcpy(dest, src, dsize);
    if (ssize >= dsize - 1)
            return ERROR;

Here we have dest guaranteed to be terminated, and if the source string is greater than the dest - 1 (for the '\0'), we can bail out.

Sure, however your way allows for the dest buff length test to be left off (forgotten). If the convention was to use strlcpy() the test is forced by requiring the length argument to be passed. strlcpy() uses memcpy() under-the-hood anyway so there's no real efficiency differences here, AFAIK.

Also, the strlcpy() version is 1 line shorter (which is not *terribly* important) and arguably more readable (not a very strong argument).

Finally if truncation is not a problem, strlcpy() handles this case just as fine as anything and is probably preferable to a memcpy() with a length test. So might as well use strlcpy() in both places for consistency.

Quoted 6 lines
                --Ethan

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#11 lore raw
In reply to Douglas Schilling Landgraf
Quoted 5 lines · On Tue, 27 Feb 2007 11:12:00 -0500 [email protected] wrote:
Quoted 4 lines · Standard Azi

char buf[150];

strcpy(buf, "LALA");

Quoted 9 lines
Quoted 1 line

that's one reason, for example

char buf[150] = "LALA";

and if you really need to do it outside of initialization, does the strncpy() really hurt it? What if the size of `buf' gets changed, then we would have to go check for all strcpy()s and make sure that they are still valid, where strncpy would do it by default.

Also, what about strlcpy()? It's a drop-in replacement for strncpy(), _ALWAYS_ null terminates the destination string (strncpy() doesn't), and doesn't have to pad out the destination string strncpy().

I pretty much always find it better than strcpy() and strncpy().

Ira

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors