Skip to contentSkip to conversation

qemu-sockets: do not test path with access() before unlinking

PATCH Superseded Paolo Bonzini 3 participants 4 messages Nov 2, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-sockets: do not test path with access() before unlinking

Using access() is a time-of-check/time-of-use race condition. It is okay to use them to provide better error messages, but that is pretty much it.

This is not one such case, so just drop the call.

---
 util/qemu-sockets.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
util/qemu-sockets.c +1 -2
index 9142917..2833c70 100644
@@ -751,8 +751,7 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
         qemu_opt_set(opts, "path", un.sun_path, &error_abort);
     }
 
-    if ((access(un.sun_path, F_OK) == 0) &&
-        unlink(un.sun_path) < 0) {
+    if (unlink(un.sun_path) < 0) {
         error_setg_errno(errp, errno,
                          "Failed to unlink socket %s", un.sun_path);
         goto err;
Signature · 1 line
2.5.0
#1 lore raw
Quoted 17 lines · Paolo Bonzini

Using access() is a time-of-check/time-of-use race condition. It is okay to use them to provide better error messages, but that is pretty much it.

This is not one such case, so just drop the call.

---
 util/qemu-sockets.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
util/qemu-sockets.c +0 -0
index 9142917..2833c70 100644

util/qemu-sockets.c int unix_listen_opts(QemuOpts *opts, Error **errp) · line 757

Quoted 9 lines
util/qemu-sockets.c +1 -2
@@ -751,8 +751,7 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
         qemu_opt_set(opts, "path", un.sun_path, &error_abort);
     }
 
-    if ((access(un.sun_path, F_OK) == 0) &&
-        unlink(un.sun_path) < 0) {
+    if (unlink(un.sun_path) < 0) {
         error_setg_errno(errp, errno,
                          "Failed to unlink socket %s", un.sun_path);
         goto err;

This is a serious semantic change, after this patch you will get:

$ ./x86_64-softmmu/qemu-system-x86_64 -vnc unix:/tmp/idontexist.sock
qemu-system-x86_64: -vnc unix:/tmp/idontexist.sock: Failed to start VNC
server: Failed to unlink socket /tmp/idontexist.sock: No such file or directory

Previously it would 'just work'. Common libvirt usage depends on this as well

Yeah there's a TOCTTOU race here, but it's very minor: if sun_path is created after the access() check, qemu is just going to fail to start since bind() will barf if the unix socket path exists.

- Cole

#2 lore raw
Quoted 34 lines · Cole Robinson
Quoted 17 lines · Paolo Bonzini

Using access() is a time-of-check/time-of-use race condition. It is okay to use them to provide better error messages, but that is pretty much it.

This is not one such case, so just drop the call.

---
 util/qemu-sockets.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
util/qemu-sockets.c +0 -0
index 9142917..2833c70 100644

util/qemu-sockets.c int unix_listen_opts(QemuOpts *opts, Error **errp) · line 757

Quoted 9 lines
util/qemu-sockets.c +1 -2
@@ -751,8 +751,7 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
         qemu_opt_set(opts, "path", un.sun_path, &error_abort);
     }
 
-    if ((access(un.sun_path, F_OK) == 0) &&
-        unlink(un.sun_path) < 0) {
+    if (unlink(un.sun_path) < 0) {
         error_setg_errno(errp, errno,
                          "Failed to unlink socket %s", un.sun_path);
         goto err;

This is a serious semantic change, after this patch you will get:

$ ./x86_64-softmmu/qemu-system-x86_64 -vnc unix:/tmp/idontexist.sock
qemu-system-x86_64: -vnc unix:/tmp/idontexist.sock: Failed to start VNC
server: Failed to unlink socket /tmp/idontexist.sock: No such file or directory
Quoted 5 lines

Previously it would 'just work'. Common libvirt usage depends on this as well

Yeah there's a TOCTTOU race here, but it's very minor: if sun_path is created after the access() check, qemu is just going to fail to start since bind() will barf if the unix socket path exists.

As Paolo says, access() is almost never a solution to anything. Here, the proper solution should be something like

    if (unlink(un.sun_path) < 0 && errno != ENOENT) {
#3 lore raw
In reply to Cole Robinson
Quoted 13 lines · Cole Robinson
Quoted 7 lines

util/qemu-sockets.c int unix_listen_opts(QemuOpts *opts, Error **errp) · line 757

Quoted 7 lines
util/qemu-sockets.c +1 -2
-    if ((access(un.sun_path, F_OK) == 0) &&
-        unlink(un.sun_path) < 0) {
+    if (unlink(un.sun_path) < 0) {
         error_setg_errno(errp, errno,
                          "Failed to unlink socket %s", un.sun_path);
         goto err;

This is a serious semantic change, after this patch you will get:

$ ./x86_64-softmmu/qemu-system-x86_64 -vnc unix:/tmp/idontexist.sock
qemu-system-x86_64: -vnc unix:/tmp/idontexist.sock: Failed to start VNC
server: Failed to unlink socket /tmp/idontexist.sock: No such file or directory
Quoted 5 lines

Previously it would 'just work'. Common libvirt usage depends on this as well

Yeah there's a TOCTTOU race here, but it's very minor: if sun_path is created after the access() check, qemu is just going to fail to start since bind() will barf if the unix socket path exists.

You're right. I misread how to test the change.

The right change is what Markus proposed.

Paolo