Skip to contentSkip to conversation

net/tipc: sprintf/strcpy conversion

PATCH In review Florian Westphal 6 participants 9 messages Nov 1, 2006 – Nov 16, 2006

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

PATCH 1/1 net/tipc: sprintf/strcpy conversion

From: Florian Westphal <[email protected]>

convert sprintf(a,b) to strcpy(a,b). Make tipc_bclink_name[] const.

---

compile tested; diffed against davem/net-2.6.

net/tipc/bcast.c +3 -3
@@ -119,7 +119,7 @@ static struct bclink *bclink = NULL;
 static struct link *bcl = NULL;
 static DEFINE_SPINLOCK(bc_lock);
 
-char tipc_bclink_name[] = "multicast-link";
+const char tipc_bclink_name[] = "multicast-link";
 
 
 static u32 buf_seqno(struct sk_buff *buf)
@@ -790,7 +790,7 @@ int tipc_bclink_init(void)
 	INIT_LIST_HEAD(&bcbearer->bearer.cong_links);
 	bcbearer->bearer.media = &bcbearer->media;
 	bcbearer->media.send_msg = tipc_bcbearer_send;
-	sprintf(bcbearer->media.name, "tipc-multicast");
+	strcpy(bcbearer->media.name, "tipc-multicast");
 
 	bcl = &bclink->link;
 	memset(bclink, 0, sizeof(struct bclink));
@@ -802,7 +802,7 @@ int tipc_bclink_init(void)
 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
 	bcl->b_ptr = &bcbearer->bearer;
 	bcl->state = WORKING_WORKING;
-	sprintf(bcl->name, tipc_bclink_name);
+	strcpy(bcl->name, tipc_bclink_name);
 
 	if (BCLINK_LOG_BUF_SIZE) {
 		char *pb = kmalloc(BCLINK_LOG_BUF_SIZE, GFP_ATOMIC);
net/tipc/bcast.h +1 -1
@@ -70,7 +70,7 @@ struct port_list {
 
 struct node;
 
-extern char tipc_bclink_name[];
+extern const char tipc_bclink_name[];
 
 
 /**
net/tipc/node.c +1 -1
@@ -667,7 +667,7 @@ struct sk_buff *tipc_node_get_links(cons
         link_info.dest = tipc_own_addr & 0xfffff00;
 	link_info.dest = htonl(link_info.dest);
         link_info.up = htonl(1);
-        sprintf(link_info.str, tipc_bclink_name);
+        strcpy(link_info.str, tipc_bclink_name);
 	tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
 
 	/* Add TLVs for any other links in scope */

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

#1 lore raw

hi Florian,

These line
+ strcpy(bcbearer->media.name, "tipc-multicast");

i gues that means tipc_bclink_name ?
an even more secure version could be like this:

	strncpy(bcbearer->media.name,sizeof(bcbearer->media.name),tipc_bclink_name);

(in case someone ever changes the size of cbearer->media.name or tipc_bclink_name and the hope that wchat_t will never reach the kernel)

re,
wh

Quoted 62 lines · Florian Westphal

From: Florian Westphal <[email protected]>

convert sprintf(a,b) to strcpy(a,b). Make tipc_bclink_name[] const.

---

compile tested; diffed against davem/net-2.6.

net/tipc/bcast.c +3 -3
@@ -119,7 +119,7 @@ static struct bclink *bclink = NULL;
 static struct link *bcl = NULL;
 static DEFINE_SPINLOCK(bc_lock);
 
-char tipc_bclink_name[] = "multicast-link";
+const char tipc_bclink_name[] = "multicast-link";
 
 
 static u32 buf_seqno(struct sk_buff *buf)
@@ -790,7 +790,7 @@ int tipc_bclink_init(void)
 	INIT_LIST_HEAD(&bcbearer->bearer.cong_links);
 	bcbearer->bearer.media = &bcbearer->media;
 	bcbearer->media.send_msg = tipc_bcbearer_send;
-	sprintf(bcbearer->media.name, "tipc-multicast");
+	strcpy(bcbearer->media.name, "tipc-multicast");
 
 	bcl = &bclink->link;
 	memset(bclink, 0, sizeof(struct bclink));
@@ -802,7 +802,7 @@ int tipc_bclink_init(void)
 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
 	bcl->b_ptr = &bcbearer->bearer;
 	bcl->state = WORKING_WORKING;
-	sprintf(bcl->name, tipc_bclink_name);
+	strcpy(bcl->name, tipc_bclink_name);
 
 	if (BCLINK_LOG_BUF_SIZE) {
 		char *pb = kmalloc(BCLINK_LOG_BUF_SIZE, GFP_ATOMIC);
net/tipc/bcast.h +1 -1
@@ -70,7 +70,7 @@ struct port_list {
 
 struct node;
 
-extern char tipc_bclink_name[];
+extern const char tipc_bclink_name[];
 
 
 /**
net/tipc/node.c +1 -1
@@ -667,7 +667,7 @@ struct sk_buff *tipc_node_get_links(cons
         link_info.dest = tipc_own_addr & 0xfffff00;
 	link_info.dest = htonl(link_info.dest);
         link_info.up = htonl(1);
-        sprintf(link_info.str, tipc_bclink_name);
+        strcpy(link_info.str, tipc_bclink_name);
 	tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
 
 	/* Add TLVs for any other links in scope */
Quoted 7 lines

_______________________________________________
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

maintainer
#2 lore raw

From: walter harms <[email protected]>
Date: Wed, 01 Nov 2006 22:38:26 +0100

Quoted 4 lines

These line

net/tipc/bcast.c +1 -0
+	strcpy(bcbearer->media.name, "tipc-multicast");

i gues that means tipc_bclink_name ?

Why? The original code used "tipc-multicast" which is a different string than tipc_bclink_name which is "multicast-link".

Quoted 2 lines

net/tipc/bcast.c int tipc_bclink_init(void) · line 793

Quoted 2 lines
net/tipc/bcast.c +1 -1
-	sprintf(bcbearer->media.name, "tipc-multicast");
+	strcpy(bcbearer->media.name, "tipc-multicast");

If you are arguing that it should be changed, that's a different changeset to discuss.

#3 lore raw
Quoted 7 lines · David Miller

From: walter harms <[email protected]>
Date: Wed, 01 Nov 2006 22:38:26 +0100

Quoted 4 lines

These line

net/tipc/bcast.c +1 -0
+	strcpy(bcbearer->media.name, "tipc-multicast");

i gues that means tipc_bclink_name ?

mea culpa, i should not write mail when tired.

Quoted 12 lines

Why? The original code used "tipc-multicast" which is a different string than tipc_bclink_name which is "multicast-link".

Quoted 2 lines

net/tipc/bcast.c int tipc_bclink_init(void) · line 793

Quoted 2 lines
net/tipc/bcast.c +1 -1
-	sprintf(bcbearer->media.name, "tipc-multicast");
+	strcpy(bcbearer->media.name, "tipc-multicast");

If you are arguing that it should be changed, that's a different changeset to discuss.

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

#4 lore raw
In reply to walter harms
Quoted 3 lines · walter harms

These line
+ strcpy(bcbearer->media.name, "tipc-multicast");
i gues that means tipc_bclink_name ?

The idea was to change how things are done, not _what_ is being done.

Quoted 3 lines

an even more secure version could be like this:

	strncpy(bcbearer->media.name,sizeof(bcbearer->media.name),tipc_bclink_name);

Ugh, please, no. The size of src is known in all cases; there is absoluty no point in using str(n|l)cpy here.

Quoted 2 lines

(in case someone ever changes the size of cbearer->media.name or tipc_bclink_name and the hope that wchat_t will never reach the kernel)

In this case 'someone' should be really hurt, don't you think?

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

#5 lore raw
Quoted 11 lines · Florian Westphal
Quoted 3 lines · walter harms

These line
+ strcpy(bcbearer->media.name, "tipc-multicast");
i gues that means tipc_bclink_name ?

The idea was to change how things are done, not _what_ is being done.

Quoted 3 lines

an even more secure version could be like this:

	strncpy(bcbearer->media.name,sizeof(bcbearer->media.name),tipc_bclink_name);
Quoted 8 lines

Ugh, please, no. The size of src is known in all cases; there is absoluty no point in using str(n|l)cpy here.

Quoted 2 lines

(in case someone ever changes the size of cbearer->media.name or tipc_bclink_name and the hope that wchat_t will never reach the kernel)

In this case 'someone' should be really hurt, don't you think?

hi florian,
i am on the side of error, the code increase is marginal and the speed penalty also, so why not ? you make sure that an overflow may never happen, and the rest in name gets zeroed.

The problem is that when the error occurs it may be later than the actual changeset.
NTL it is an hint, and if you feel ok with it and the maintainer has no objects i have no problems either.

re,
wh

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

#6 lore raw

Re: [KJ] [tipc-discussion] [patch] net/tipc:sprintf/strcpy

In reply to Florian Westphal
Quoted 5 lines · Elmer

Hi,

Good observations. My comments in <Elmer> </Elmer> way down below.

What is '[email protected]' ?

Mailing list for kernel-janitors work.
See http://www.kerneljanitors.org/ for info.

Quoted 7 lines

<Elmer> If you want to be 'secure', as you say, then strlcpy() is a better choice than strncpy() to guarantee that you will have a null-terminated string in the destination address (as far as I understand the subtle differences). strncpy() will result in a non-null terminated octet-string if the source string is too long. Any future string operations may cause problems that are harder to trace.

Quoted 5 lines

Is strlcpy() available on all implementations? I once compiled code on an architecture that did not support it and had to use strncpy() with an explicit assignment bloating code:

    dest_string[last_octet] = '\0';

</Elmer>

(1-2 minutes of grep:)

Yes, strlcpy() is available on all Linux architectures. An arch. can provide a specific version of it and also #define __HAVE_ARCH_STRLCPY

If that's not done, then the generic version in lib/string.c will be used.

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

#7 lore raw
In reply to Florian Westphal
Quoted 1 line · Florian Westphal

convert sprintf(a,b) to strcpy(a,b). Make tipc_bclink_name[] const.

Ahhh, I missed the start of threads.

Patch is useless because it changes one unbounded string function into another unbounded string function.

Quoted 23 lines
net/tipc/bcast.c +2 -2
@@ -790,7 +790,7 @@ int tipc_bclink_init(void)
 	INIT_LIST_HEAD(&bcbearer->bearer.cong_links);
 	bcbearer->bearer.media = &bcbearer->media;
 	bcbearer->media.send_msg = tipc_bcbearer_send;
-	sprintf(bcbearer->media.name, "tipc-multicast");
+	strcpy(bcbearer->media.name, "tipc-multicast");
 
 	bcl = &bclink->link;
 	memset(bclink, 0, sizeof(struct bclink));
@@ -802,7 +802,7 @@ int tipc_bclink_init(void)
 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
 	bcl->b_ptr = &bcbearer->bearer;
 	bcl->state = WORKING_WORKING;
-	sprintf(bcl->name, tipc_bclink_name);
+	strcpy(bcl->name, tipc_bclink_name);
 
 	if (BCLINK_LOG_BUF_SIZE) {
 		char *pb = kmalloc(BCLINK_LOG_BUF_SIZE, GFP_ATOMIC);

net/tipc/node.c struct sk_buff *tipc_node_get_links(cons · line 670

Quoted 5 lines
net/tipc/node.c +1 -1
@@ -667,7 +667,7 @@ struct sk_buff *tipc_node_get_links(cons
         link_info.dest = tipc_own_addr & 0xfffff00;
 	link_info.dest = htonl(link_info.dest);
         link_info.up = htonl(1);
-        sprintf(link_info.str, tipc_bclink_name);
+        strcpy(link_info.str, tipc_bclink_name);

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

#8 lore raw
Quoted 7 lines · * Alexey Dobriyan | 2006-11-03 03:09:05 [+0300]:
Quoted 1 line · Florian Westphal

convert sprintf(a,b) to strcpy(a,b). Make tipc_bclink_name[] const.

Ahhh, I missed the start of threads.

Patch is useless because it changes one unbounded string function into another unbounded string function.

The discussion in this thread is really back-breaking!

1. To make tipc_bclink_name const there is absolutly no objection
2. Replace sprintf with strcpy

a) First of all: If you _copy_ a string then use also strCPY() Thats a question of good style!

b) If the compiler is smart enough, he realize that you want to copy a string and replace the sprintf call with a
pushl %ebx
call strcpy
Surprise - Surprise!

Assumed you use gcc with -Os or -O2! Don't know how icc handle this case. If you compile without optimization you save at least a "repz movsb %ds:(%esi),%es:(%edi)" instruction.

c) Last but not least I read all the time this patch doesn't introduce bounds-checking. This isn't a argument because the author is aware of the destination length of the buffer. BTW: grep for (sprintf|strcpy) in /usr/src/linux and be surprised how unsecure the kernel is (thats ironical).

This patch is 100% OK!

HGN

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