Skip to contentSkip to conversation

doc: avoid strncpy in accounting tool

PATCH Superseded Kees Cook 4 participants 4 messages Jun 6, 2013 – Jun 10, 2013 Documentation

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 doc: avoid strncpy in accounting tool

Avoid strncpy anti-pattern. Use strdup() instead, as already done for the logfile optarg.

---
Fix for -mm clean-up-scary-strncpydst-src-strlensrc-uses-fix.patch

---
 Documentation/accounting/getdelays.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/accounting/getdelays.c +2 -2
index f8ebcde..1db89d3 100644
@@ -272,7 +272,7 @@ int main(int argc, char *argv[])
 	char *logfile = NULL;
 	int loop = 0;
 	int containerset = 0;
-	char containerpath[1024];
+	char *containerpath = NULL;
 	int cfd = 0;
 	int forking = 0;
 	sigset_t sigset;
@@ -299,7 +299,7 @@ int main(int argc, char *argv[])
 			break;
 		case 'C':
 			containerset = 1;
-			strncpy(containerpath, optarg, strlen(optarg) + 1);
+			containerpath = strdup(optarg);
 			break;
 		case 'w':
 			logfile = strdup(optarg);
Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
#1 lore raw
Quoted 2 lines · Kees Cook

Avoid strncpy anti-pattern. Use strdup() instead, as already done for the logfile optarg.

There should be no need to copy the string, option arguments are stable.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Acked-by on v1 1/1
#2 lore raw
In reply to Kees Cook
Quoted 24 lines · Kees Cook

Avoid strncpy anti-pattern. Use strdup() instead, as already done for the logfile optarg.

---
Fix for -mm clean-up-scary-strncpydst-src-strlensrc-uses-fix.patch

---
 Documentation/accounting/getdelays.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/accounting/getdelays.c +1 -1
index f8ebcde..1db89d3 100644
@@ -272,7 +272,7 @@ int main(int argc, char *argv[])
 	char *logfile = NULL;
 	int loop = 0;
 	int containerset = 0;
-	char containerpath[1024];
+	char *containerpath = NULL;
 	int cfd = 0;
 	int forking = 0;
 	sigset_t sigset;

Documentation/accounting/getdelays.c int main(int argc, char *argv[]) · line 303

Quoted 6 lines
Documentation/accounting/getdelays.c +1 -1
@@ -299,7 +299,7 @@ int main(int argc, char *argv[])
 			break;
 		case 'C':
 			containerset = 1;
-			strncpy(containerpath, optarg, strlen(optarg) +  
 1);
+			containerpath = strdup(optarg);

*boggle* That an elaborate way of doing a standard strcpy(), isn't it?

(Assuming free() being done by exit() is ok, and that somebody's going to be modifying this string so just keeping the pointer to optarg might make ps look weird...)

if Andrew hasn't grabbed it already (I'm days behind on email) please send through [email protected].

Rob

#3 lore raw
In reply to Kees Cook
Quoted 3 lines · On Wed, 5 Jun 2013 19:49:30 -0700 Kees Cook <[email protected]> wrote:

Avoid strncpy anti-pattern. Use strdup() instead, as already done for the logfile optarg.

What Andreas said :)

From: Andrew Morton <[email protected]>
Subject: documentation-accounting-getdelaysc-avoid-strncpy-in-accounting-tool-fix

remove the str[cpy|dup] altogether

Cc Andreas Schwab <[email protected]>
Cc Kees Cook <[email protected]>
Cc Rob Landley <[email protected]>

---

 Documentation/accounting/getdelays.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/accounting/getdelays.c renamed from Documentation/accounting/getdelays.c~documentation-accounting-getdelaysc-avoid-strncpy-in-accounting-tool-fix +1 -1
diff -puN Documentation/accounting/getdelays.c~documentation-accounting-getdelaysc-avoid-strncpy-in-accounting-tool-fix Documentation/accounting/getdelays.c
@@ -299,7 +299,7 @@ int main(int argc, char *argv[])
 			break;
 		case 'C':
 			containerset = 1;
-			containerpath = strdup(optarg);
+			containerpath = optarg;
 			break;
 		case 'w':
 			logfile = strdup(optarg);

_