Skip to content

Commit

Permalink
Merge pull request #1535 from nanovms/fix/open-write-directory
Browse files Browse the repository at this point in the history
Check for directory type on open and read syscalls
  • Loading branch information
sanderssj authored Jul 13, 2021
2 parents 365e11b + 95a58b9 commit 430a9fe
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/unix/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ sysreturn readv(int fd, struct iovec *iov, int iovcnt)
if (!validate_iovec(iov, iovcnt, true))
return -EFAULT;
fdesc f = resolve_fd(current->p, fd);
if (fdesc_type(f) == FDESC_TYPE_DIRECTORY)
return -EISDIR;
iov_op(f, false, iov, iovcnt, infinity, true, syscall_io_complete);
return thread_maybe_sleep_uninterruptible(current);
}
Expand Down Expand Up @@ -458,7 +460,8 @@ closure_function(2, 6, sysreturn, file_read,
void *, dest, u64, length, u64, offset_arg, thread, t, boolean, bh, io_completion, completion)
{
file f = bound(f);

if (fdesc_type(&f->f) == FDESC_TYPE_DIRECTORY)
return -EISDIR;
boolean is_file_offset = offset_arg == infinity;
u64 offset = is_file_offset ? f->offset : offset_arg;
thread_log(t, "%s: f %p, dest %p, offset %ld (%s), length %ld, file length %ld",
Expand Down Expand Up @@ -764,11 +767,18 @@ sysreturn open_internal(filesystem fs, tuple cwd, const char *name, int flags,
case O_RDWR:
if (!(file_meta_perms(current->p, n) & ACCESS_PERM_WRITE))
return -EACCES;
if (is_dir(n))
return -EISDIR;
break;
default:
return -EINVAL;
}

if ((flags & (O_CREAT|O_DIRECTORY)) == O_DIRECTORY && !is_dir(n)) {
thread_log(current, "\"%s\" opened with O_DIRECTORY but is not a directory", name);
return -ENOTDIR;
}

u64 length = 0;
fsfile fsf = 0;

Expand Down
1 change: 1 addition & 0 deletions src/unix/system_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ typedef struct iovec {
#define O_NOATIME 01000000
#define O_CLOEXEC 02000000
#define O_PATH 010000000
#define O_TMPFILE (020000000 | O_DIRECTORY)

#define F_LINUX_SPECIFIC_BASE 0x400

Expand Down
1 change: 1 addition & 0 deletions src/x86_64/unix_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct stat {
} __attribute__((packed));

#define O_DIRECT 00040000
#define O_DIRECTORY 00200000
#define O_NOFOLLOW 00400000

#define MAP_32BIT 0x40
Expand Down
38 changes: 37 additions & 1 deletion test/runtime/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,43 @@ void basic_write_test()
{
char buf[BUFLEN];
ssize_t rv;
int fd = open("hello", O_RDWR);
int fd = open("/", O_RDWR);
if (fd != -1 || errno != EISDIR) {
perror("open directory rdwr");
exit(EXIT_FAILURE);
}
fd = open("/", O_WRONLY);
if (fd != -1 || errno != EISDIR) {
perror("open directory wr");
exit(EXIT_FAILURE);
}
fd = open("/", O_RDONLY | O_DIRECTORY);
if (fd < 0) {
perror("open directory");
exit(EXIT_FAILURE);
}
close(fd);
fd = open("/", O_RDONLY);
if (fd < 0) {
perror("open directory rd");
exit(EXIT_FAILURE);
}
if (read(fd, buf, BUFLEN) != -1 || errno != EISDIR) {
perror("read directory");
exit(EXIT_FAILURE);
}
if (write(fd, buf, BUFLEN) != -1 || errno != EBADF) {
perror("write directory");
exit(EXIT_FAILURE);
}
close(fd);

fd = open("hello", O_RDWR | O_DIRECTORY);
if (fd != -1 || errno != ENOTDIR) {
perror("open file as directory");
exit(EXIT_FAILURE);
}
fd = open("hello", O_RDWR);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
Expand Down

0 comments on commit 430a9fe

Please sign in to comment.