Skip to contentSkip to conversation

net: fix insecure temporary file creation in SLiRP

PATCH In review [email protected] 5 participants 8 messages Jun 1, 2015 – Jun 4, 2015 Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: fix insecure temporary file creation in SLiRP

From: Miroslav Rezanina <[email protected]>

Qemu's user mode networking stack(-net user) is vulnerable to a predictable temporary file creation flaw. This patch uses mkdtemp(3) routine to fix it.

Fixes CVE-2015-4037.

---
[1] http://seclists.org/oss-sec/2015/q2/538

---
 net/slirp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/slirp.c +5 -5
index 0e15cf6..804b095 100644
@@ -27,6 +27,7 @@
 
 #ifndef _WIN32
 #include <pwd.h>
+#include <stdlib.h>
 #include <sys/wait.h>
 #endif
 #include "net/net.h"
@@ -481,9 +482,9 @@ static void slirp_smb_cleanup(SlirpState *s)
 static int slirp_smb(SlirpState* s, const char *exported_dir,
                      struct in_addr vserver_addr)
 {
-    static int instance;
     char smb_conf[128];
     char smb_cmdline[128];
+    char smb_dir[] = "/tmp/qemu-smb.XXXXXX",  *tmpdir = NULL;
     struct passwd *passwd;
     FILE *f;
 
@@ -505,12 +506,11 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
         return -1;
     }
 
-    snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
-             (long)getpid(), instance++);
-    if (mkdir(s->smb_dir, 0700) < 0) {
-        error_report("could not create samba server dir '%s'", s->smb_dir);
+    if (!(tmpdir = mkdtemp(smb_dir))) {
+        error_report("could not create samba server dir '%s'", smb_dir);
         return -1;
     }
+    strncpy(s->smb_dir, tmpdir, sizeof(s->smb_dir));
     snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf");
 
     f = fopen(smb_conf, "w");
Signature · 1 line
2.4.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 42 lines · [email protected] writes:

From: Miroslav Rezanina <[email protected]>

Qemu's user mode networking stack(-net user) is vulnerable to a predictable temporary file creation flaw. This patch uses mkdtemp(3) routine to fix it.

Fixes CVE-2015-4037.

---
[1] http://seclists.org/oss-sec/2015/q2/538

---
 net/slirp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/slirp.c +2 -1
index 0e15cf6..804b095 100644
@@ -27,6 +27,7 @@
 
 #ifndef _WIN32
 #include <pwd.h>
+#include <stdlib.h>
 #include <sys/wait.h>
 #endif
 #include "net/net.h"
@@ -481,9 +482,9 @@ static void slirp_smb_cleanup(SlirpState *s)
 static int slirp_smb(SlirpState* s, const char *exported_dir,
                      struct in_addr vserver_addr)
 {
-    static int instance;
     char smb_conf[128];
     char smb_cmdline[128];
+    char smb_dir[] = "/tmp/qemu-smb.XXXXXX",  *tmpdir = NULL;
     struct passwd *passwd;
     FILE *f;
 
@@ -505,12 +506,11 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
         return -1;
     }

net/slirp.c static int slirp_smb(SlirpState* s, const char *exported_dir, · line 513

Quoted 10 lines
net/slirp.c +3 -4
 
-    snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
-             (long)getpid(), instance++);
-    if (mkdir(s->smb_dir, 0700) < 0) {
-        error_report("could not create samba server dir '%s'", s->smb_dir);
+    if (!(tmpdir = mkdtemp(smb_dir))) {
+        error_report("could not create samba server dir '%s'", smb_dir);
         return -1;
     }
+    strncpy(s->smb_dir, tmpdir, sizeof(s->smb_dir));

We tend to eschew strncpy() and use our (slightly less bad) pstrcpy(). Recommend to use it here, too.

Quoted 4 lines
net/slirp.c +0 -0
     snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir,
     "smb.conf");
 
     f = fopen(smb_conf, "w");

Michael (cc'ed) already posted "[PATCH] slirp: use less predictable directory name in /tmp for smb config (CVE-2015-4037)"[*]. His patch clobbers s->smb_dir[] when mkdtemp() fails (missed that in my review), yours doesn't.

Suggest you guys figure out together which solution you want.

Preferably with strncpy() replaced by pstrcpy():

[*] http://lists.nongnu.org/archive/html/qemu-devel/2015-05/msg05767.html

#2 lore raw
Quoted 45 lines · Markus Armbruster
Quoted 43 lines · [email protected] writes:

From: Miroslav Rezanina <[email protected]>

Qemu's user mode networking stack(-net user) is vulnerable to a predictable temporary file creation flaw. This patch uses mkdtemp(3) routine to fix it.

Fixes CVE-2015-4037.

---
[1] http://seclists.org/oss-sec/2015/q2/538

---
 net/slirp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/slirp.c +2 -1
index 0e15cf6..804b095 100644
@@ -27,6 +27,7 @@
 
 #ifndef _WIN32
 #include <pwd.h>
+#include <stdlib.h>
 #include <sys/wait.h>
 #endif
 #include "net/net.h"
@@ -481,9 +482,9 @@ static void slirp_smb_cleanup(SlirpState *s)
 static int slirp_smb(SlirpState* s, const char *exported_dir,
                      struct in_addr vserver_addr)
 {
-    static int instance;
     char smb_conf[128];
     char smb_cmdline[128];
+    char smb_dir[] = "/tmp/qemu-smb.XXXXXX",  *tmpdir = NULL;
     struct passwd *passwd;
     FILE *f;
 
@@ -505,12 +506,11 @@ static int slirp_smb(SlirpState* s, const char *exported_dir,
         return -1;
     }
 
Quoted 12 lines

net/slirp.c static int slirp_smb(SlirpState* s, const char *exported_dir, · line 513

Quoted 9 lines
net/slirp.c +3 -4
-    snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
-             (long)getpid(), instance++);
-    if (mkdir(s->smb_dir, 0700) < 0) {
-        error_report("could not create samba server dir '%s'", s->smb_dir);
+    if (!(tmpdir = mkdtemp(smb_dir))) {
+        error_report("could not create samba server dir '%s'", smb_dir);
         return -1;
     }
+    strncpy(s->smb_dir, tmpdir, sizeof(s->smb_dir));

We tend to eschew strncpy() and use our (slightly less bad) pstrcpy(). Recommend to use it here, too.

Good point. Worth changing.

Quoted 10 lines
Quoted 5 lines
net/slirp.c +0 -0
     snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir,
     "smb.conf");
 
     f = fopen(smb_conf, "w");

Michael (cc'ed) already posted "[PATCH] slirp: use less predictable directory name in /tmp for smb config (CVE-2015-4037)"[*]. His patch clobbers s->smb_dir[] when mkdtemp() fails (missed that in my review), yours doesn't.

I have to remember to check qemu-devel before preparing/sending patch. My "different version already sent" ratio is too high. In this case are both fixes almost identical.

Mirek

Quoted 9 lines

Suggest you guys figure out together which solution you want.

Preferably with strncpy() replaced by pstrcpy():

[*] http://lists.nongnu.org/archive/html/qemu-devel/2015-05/msg05767.html

#3 lore raw
In reply to Markus Armbruster
Quoted 5 lines · Markus Armbruster
Quoted 5 lines

net/slirp.c static int slirp_smb(SlirpState* s, const char *exported_dir, · line 509

Quoted 5 lines
net/slirp.c +1 -4
-    snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
-             (long)getpid(), instance++);
-    if (mkdir(s->smb_dir, 0700) < 0) {
-        error_report("could not create samba server dir '%s'", s->smb_dir);
+    if (!(tmpdir = mkdtemp(smb_dir))) {

mkdtemp is unfortunately missing on Windows, and g_mkdtemp requires glib 2.30.

Paolo

Quoted 20 lines
Quoted 4 lines

net/slirp.c static int slirp_smb(SlirpState* s, const char *exported_dir, · line 513

Quoted 4 lines
net/slirp.c +2 -0
+        error_report("could not create samba server dir '%s'", smb_dir);
         return -1;
     }
+    strncpy(s->smb_dir, tmpdir, sizeof(s->smb_dir));

We tend to eschew strncpy() and use our (slightly less bad) pstrcpy(). Recommend to use it here, too.

Quoted 4 lines
Quoted 4 lines
     snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir,
     "smb.conf");
 
     f = fopen(smb_conf, "w");

Michael (cc'ed) already posted "[PATCH] slirp: use less predictable directory name in /tmp for smb config (CVE-2015-4037)"[*]. His patch clobbers s->smb_dir[] when mkdtemp() fails (missed that in my review), yours doesn't.

Suggest you guys figure out together which solution you want.

Preferably with strncpy() replaced by pstrcpy():

#4 lore raw

01.06.2015 11:47, Paolo Bonzini пишет:

Quoted 11 lines · Markus Armbruster
Quoted 5 lines
Quoted 5 lines

net/slirp.c static int slirp_smb(SlirpState* s, const char *exported_dir, · line 509

Quoted 5 lines
net/slirp.c +1 -4
-    snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
-             (long)getpid(), instance++);
-    if (mkdir(s->smb_dir, 0700) < 0) {
-        error_report("could not create samba server dir '%s'", s->smb_dir);
+    if (!(tmpdir = mkdtemp(smb_dir))) {

mkdtemp is unfortunately missing on Windows, and g_mkdtemp requires glib 2.30.

This code is within #ifndef WIN32 block.

/mjt

#5 lore raw
In reply to Markus Armbruster

Hello Markus,

Quoted 7 lines · Markus Armbruster

Michael (cc'ed) already posted "[PATCH] slirp: use less predictable directory name in /tmp for smb config (CVE-2015-4037)"[*]. His patch clobbers s->smb_dir[] when mkdtemp() fails (missed that in my review), yours doesn't.

Suggest you guys figure out together which solution you want.

Thank you so much for the review. IMO using separate smb_dir[] is prudent than s->smb_dir.

Quoted 1 line

Preferably with strncpy() replaced by pstrcpy():

Yes.

Thank you.

---
Regards
-P J P
http://feedmug.com

#6 lore raw
Quoted 8 lines · P J P

Hello Markus,

Quoted 6 lines · Markus Armbruster

Michael (cc'ed) already posted "[PATCH] slirp: use less predictable directory name in /tmp for smb config (CVE-2015-4037)"[*]. His patch clobbers s->smb_dir[] when mkdtemp() fails (missed that in my review), yours doesn't.

Quoted 5 lines
Quoted 1 line

Suggest you guys figure out together which solution you want.

Thank you so much for the review. IMO using separate smb_dir[] is prudent than s->smb_dir.

Let's go with Michael's v2, because it also fixes the "cleanup after mkdir() / mkdtemp() failed" scenario.

[...]