riscv-software-src / riscv-pk

RISC-V Proxy Kernel
Other
579 stars 306 forks source link

Fix a file leak in function `at_kfd` #276

Closed MaxXSoft closed 2 years ago

MaxXSoft commented 2 years ago

The following program (test.c) should never exit:

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <assert.h>

int main() {
  int iters = 0;
  for (;;) {
    int dirfd = open(".", O_DIRECTORY);
    assert(dirfd > 0);
    int fd = openat(dirfd, "test.c", O_RDONLY);
    if (fd < 0) {
      break;
    } else {
      close(fd);
      close(dirfd);
    }
    ++iters;
  }
  printf("iters = %d, errno = %d\n", iters, errno);
  return 0;
}

but it fails and the output is iters = 123, errno = 12, because the function at_kfd (in syscall.c) calls file_get, but does not call file_decref before returning.