vfs: check userland buffers before reading them.
[haiku.git] / src / tools / fs_shell / uio.cpp
blob6dd3302791ce52338dbe7047bf074ec2e98e526d
1 /*
2 * Copyright 2007-2008, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "compatibility.h"
8 #include "fssh_uio.h"
10 #if defined(HAIKU_HOST_PLATFORM_FREEBSD)
11 #include <unistd.h>
12 #endif
14 #include <new>
16 #include <errno.h>
17 #include <sys/uio.h>
19 #include "partition_support.h"
22 static const fssh_size_t kMaxIOVecs = 1024;
25 bool
26 prepare_iovecs(const struct fssh_iovec *vecs, fssh_size_t count,
27 struct iovec* systemVecs)
29 if (count > kMaxIOVecs) {
30 errno = B_BAD_VALUE;
31 return false;
34 for (fssh_size_t i = 0; i < count; i++) {
35 systemVecs[i].iov_base = vecs[i].iov_base;
36 systemVecs[i].iov_len = vecs[i].iov_len;
39 return true;
43 fssh_ssize_t
44 fssh_readv(int fd, const struct fssh_iovec *vector, fssh_size_t count)
46 struct iovec systemVecs[kMaxIOVecs];
47 if (!prepare_iovecs(vector, count, systemVecs))
48 return -1;
50 fssh_off_t pos = -1;
51 fssh_size_t length = 0;
52 if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
53 return -1;
55 #if !defined(HAIKU_HOST_PLATFORM_FREEBSD)
56 return readv(fd, systemVecs, count);
57 #else
58 return readv_pos(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count);
59 #endif
63 fssh_ssize_t
64 fssh_readv_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec,
65 fssh_size_t count)
67 struct iovec systemVecs[kMaxIOVecs];
68 if (!prepare_iovecs(vec, count, systemVecs))
69 return -1;
71 fssh_size_t length = 0;
72 if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
73 return -1;
75 return readv_pos(fd, pos, systemVecs, count);
79 fssh_ssize_t
80 fssh_writev(int fd, const struct fssh_iovec *vector, fssh_size_t count)
82 struct iovec systemVecs[kMaxIOVecs];
83 if (!prepare_iovecs(vector, count, systemVecs))
84 return -1;
86 fssh_off_t pos = -1;
87 fssh_size_t length = 0;
88 if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
89 return -1;
91 #if !defined(HAIKU_HOST_PLATFORM_FREEBSD)
92 return writev(fd, systemVecs, count);
93 #else
94 return writev_pos(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count);
95 #endif
99 fssh_ssize_t
100 fssh_writev_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec,
101 fssh_size_t count)
103 struct iovec systemVecs[kMaxIOVecs];
104 if (!prepare_iovecs(vec, count, systemVecs))
105 return -1;
107 fssh_size_t length = 0;
108 if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
109 return -1;
111 return writev_pos(fd, pos, systemVecs, count);