Skip to content
  • Stefan Hajnoczi's avatar
    ebf10195
    virtiofsd: avoid /proc/self/fd tempdir · ebf10195
    Stefan Hajnoczi authored
    
    
    In order to prevent /proc/self/fd escapes a temporary directory is
    created where /proc/self/fd is bind-mounted. This doesn't work on
    read-only file systems.
    
    Avoid the temporary directory by bind-mounting /proc/self/fd over /proc.
    This does not affect other processes since we remounted / with MS_REC |
    MS_SLAVE. /proc must exist and virtiofsd does not use it so it's safe to
    do this.
    
    Path traversal can be tested with the following function:
    
      static void test_proc_fd_escape(struct lo_data *lo)
      {
          int fd;
          int level = 0;
          ino_t last_ino = 0;
    
          fd = lo->proc_self_fd;
          for (;;) {
              struct stat st;
    
              if (fstat(fd, &st) != 0) {
                  perror("fstat");
                  return;
              }
              if (last_ino && st.st_ino == last_ino) {
                  fprintf(stderr, "inode number unchanged, stopping\n");
                  return;
              }
              last_ino = st.st_ino;
    
              fprintf(stderr, "Level %d dev %lu ino %lu\n", level,
                      (unsigned long)st.st_dev,
                      (unsigned long)last_ino);
              fd = openat(fd, "..", O_PATH | O_DIRECTORY | O_NOFOLLOW);
              level++;
          }
      }
    
    Before and after this patch only Level 0 is displayed. Without
    /proc/self/fd bind-mount protection it is possible to traverse parent
    directories.
    
    Fixes: 397ae982 ("virtiofsd: jail lo->proc_self_fd")
    Cc: Miklos Szeredi <mszeredi@redhat.com>
    Cc: Jens Freimann <jfreimann@redhat.com>
    Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
    Message-Id: <20201006095826.59813-1-stefanha@redhat.com>
    Reviewed-by: default avatarDr. David Alan Gilbert <dgilbert@redhat.com>
    Tested-by: default avatarJens Freimann <jfreimann@redhat.com>
    Reviewed-by: default avatarJens Freimann <jfreimann@redhat.com>
    Signed-off-by: default avatarDr. David Alan Gilbert <dgilbert@redhat.com>
    ebf10195
    virtiofsd: avoid /proc/self/fd tempdir
    Stefan Hajnoczi authored
    
    
    In order to prevent /proc/self/fd escapes a temporary directory is
    created where /proc/self/fd is bind-mounted. This doesn't work on
    read-only file systems.
    
    Avoid the temporary directory by bind-mounting /proc/self/fd over /proc.
    This does not affect other processes since we remounted / with MS_REC |
    MS_SLAVE. /proc must exist and virtiofsd does not use it so it's safe to
    do this.
    
    Path traversal can be tested with the following function:
    
      static void test_proc_fd_escape(struct lo_data *lo)
      {
          int fd;
          int level = 0;
          ino_t last_ino = 0;
    
          fd = lo->proc_self_fd;
          for (;;) {
              struct stat st;
    
              if (fstat(fd, &st) != 0) {
                  perror("fstat");
                  return;
              }
              if (last_ino && st.st_ino == last_ino) {
                  fprintf(stderr, "inode number unchanged, stopping\n");
                  return;
              }
              last_ino = st.st_ino;
    
              fprintf(stderr, "Level %d dev %lu ino %lu\n", level,
                      (unsigned long)st.st_dev,
                      (unsigned long)last_ino);
              fd = openat(fd, "..", O_PATH | O_DIRECTORY | O_NOFOLLOW);
              level++;
          }
      }
    
    Before and after this patch only Level 0 is displayed. Without
    /proc/self/fd bind-mount protection it is possible to traverse parent
    directories.
    
    Fixes: 397ae982 ("virtiofsd: jail lo->proc_self_fd")
    Cc: Miklos Szeredi <mszeredi@redhat.com>
    Cc: Jens Freimann <jfreimann@redhat.com>
    Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
    Message-Id: <20201006095826.59813-1-stefanha@redhat.com>
    Reviewed-by: default avatarDr. David Alan Gilbert <dgilbert@redhat.com>
    Tested-by: default avatarJens Freimann <jfreimann@redhat.com>
    Reviewed-by: default avatarJens Freimann <jfreimann@redhat.com>
    Signed-off-by: default avatarDr. David Alan Gilbert <dgilbert@redhat.com>
Loading