Skip to contentSkip to conversation

fsdev-proxy-helper: avoid TOC/TOU race

PATCH In review Paolo Bonzini 2 participants 2 messages Nov 27, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsdev-proxy-helper: avoid TOC/TOU race

There is a minor time of check/time of use race between statfs and chroot. It can be fixed easily by stat-ing the root after it has been changed.

---
 fsdev/virtfs-proxy-helper.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
fsdev/virtfs-proxy-helper.c +10 -10
index 9097d15..ad1da0d 100644
@@ -1128,10 +1128,19 @@ int main(int argc, char **argv)
         }
     }
 
+    if (chdir("/") < 0) {
+        do_perror("chdir");
+        goto error;
+    }
+    if (chroot(rpath) < 0) {
+        do_perror("chroot");
+        goto error;
+    }
+
     get_version = false;
 #ifdef FS_IOC_GETVERSION
     /* check whether underlying FS support IOC_GETVERSION */
-    retval = statfs(rpath, &st_fs);
+    retval = statfs("/", &st_fs);
     if (!retval) {
         switch (st_fs.f_type) {
         case EXT2_SUPER_MAGIC:
@@ -1144,16 +1153,7 @@ int main(int argc, char **argv)
     }
 #endif
 
-    if (chdir("/") < 0) {
-        do_perror("chdir");
-        goto error;
-    }
-    if (chroot(rpath) < 0) {
-        do_perror("chroot");
-        goto error;
-    }
     umask(0);
-
     if (init_capabilities() < 0) {
         goto error;
     }
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · On Fri, 27 Nov 2015 12:43:05 +0100 Paolo Bonzini <[email protected]> wrote:

There is a minor time of check/time of use race between statfs and chroot. It can be fixed easily by stat-ing the root after it has been changed.

---

Quoted 45 lines
 fsdev/virtfs-proxy-helper.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
fsdev/virtfs-proxy-helper.c +10 -10
index 9097d15..ad1da0d 100644
@@ -1128,10 +1128,19 @@ int main(int argc, char **argv)
         }
     }
 
+    if (chdir("/") < 0) {
+        do_perror("chdir");
+        goto error;
+    }
+    if (chroot(rpath) < 0) {
+        do_perror("chroot");
+        goto error;
+    }
+
     get_version = false;
 #ifdef FS_IOC_GETVERSION
     /* check whether underlying FS support IOC_GETVERSION */
-    retval = statfs(rpath, &st_fs);
+    retval = statfs("/", &st_fs);
     if (!retval) {
         switch (st_fs.f_type) {
         case EXT2_SUPER_MAGIC:
@@ -1144,16 +1153,7 @@ int main(int argc, char **argv)
     }
 #endif
 
-    if (chdir("/") < 0) {
-        do_perror("chdir");
-        goto error;
-    }
-    if (chroot(rpath) < 0) {
-        do_perror("chroot");
-        goto error;
-    }
     umask(0);
-
     if (init_capabilities() < 0) {
         goto error;
     }