From 85b3a71e69e6e8c79a8c9929e6facf9621af1d82 Mon Sep 17 00:00:00 2001 From: Marcus Overhagen Date: Wed, 25 Jul 2007 22:10:56 +0000 Subject: [PATCH] Check the pos parameter before calling into kernel. This does prevent the unwanted side effect of reading or writing at the current file pointer position when the functions are called with a -1 position. It's save to do this check in user space, because calling the _kern_* function with -1 pos has the same effect as calling the normal read/write posix functions. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21701 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/posix/sys/uio.c | 14 ++++++++++++-- src/system/libroot/posix/unistd/read.c | 14 ++++++++++++-- src/system/libroot/posix/unistd/write.c | 14 ++++++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/system/libroot/posix/sys/uio.c b/src/system/libroot/posix/sys/uio.c index 77eb2e1d7f..2d58ef1fbc 100644 --- a/src/system/libroot/posix/sys/uio.c +++ b/src/system/libroot/posix/sys/uio.c @@ -30,7 +30,12 @@ readv(int fd, const struct iovec *vecs, size_t count) ssize_t readv_pos(int fd, off_t pos, const struct iovec *vecs, size_t count) { - ssize_t bytes = _kern_readv(fd, pos, vecs, count); + ssize_t bytes; + if (pos < 0) { + errno = B_BAD_VALUE; + return -1; + } + bytes = _kern_readv(fd, pos, vecs, count); RETURN_AND_SET_ERRNO(bytes); } @@ -48,7 +53,12 @@ writev(int fd, const struct iovec *vecs, size_t count) ssize_t writev_pos(int fd, off_t pos, const struct iovec *vecs, size_t count) { - ssize_t bytes = _kern_writev(fd, pos, vecs, count); + ssize_t bytes; + if (pos < 0) { + errno = B_BAD_VALUE; + return -1; + } + bytes = _kern_writev(fd, pos, vecs, count); RETURN_AND_SET_ERRNO(bytes); } diff --git a/src/system/libroot/posix/unistd/read.c b/src/system/libroot/posix/unistd/read.c index 60dcec7c81..06b63144ec 100644 --- a/src/system/libroot/posix/unistd/read.c +++ b/src/system/libroot/posix/unistd/read.c @@ -34,7 +34,12 @@ read(int fd, void *buffer, size_t bufferSize) ssize_t read_pos(int fd, off_t pos, void *buffer, size_t bufferSize) { - ssize_t status = _kern_read(fd, pos, buffer, bufferSize); + ssize_t status; + if (pos < 0) { + errno = B_BAD_VALUE; + return -1; + } + status = _kern_read(fd, pos, buffer, bufferSize); RETURN_AND_SET_ERRNO(status); } @@ -43,7 +48,12 @@ read_pos(int fd, off_t pos, void *buffer, size_t bufferSize) ssize_t pread(int fd, void *buffer, size_t bufferSize, off_t pos) { - ssize_t status = _kern_read(fd, pos, buffer, bufferSize); + ssize_t status; + if (pos < 0) { + errno = B_BAD_VALUE; + return -1; + } + status = _kern_read(fd, pos, buffer, bufferSize); RETURN_AND_SET_ERRNO(status); } diff --git a/src/system/libroot/posix/unistd/write.c b/src/system/libroot/posix/unistd/write.c index 9d0c54a044..b881e4c7a9 100644 --- a/src/system/libroot/posix/unistd/write.c +++ b/src/system/libroot/posix/unistd/write.c @@ -32,7 +32,12 @@ write(int fd, void const *buffer, size_t bufferSize) ssize_t write_pos(int fd, off_t pos, const void *buffer, size_t bufferSize) { - int status = _kern_write(fd, pos, buffer, bufferSize); + ssize_t status; + if (pos < 0) { + errno = B_BAD_VALUE; + return -1; + } + status = _kern_write(fd, pos, buffer, bufferSize); RETURN_AND_SET_ERRNO(status); } @@ -41,7 +46,12 @@ write_pos(int fd, off_t pos, const void *buffer, size_t bufferSize) ssize_t pwrite(int fd, const void *buffer, size_t bufferSize, off_t pos) { - int status = _kern_write(fd, pos, buffer, bufferSize); + ssize_t status; + if (pos < 0) { + errno = B_BAD_VALUE; + return -1; + } + status = _kern_write(fd, pos, buffer, bufferSize); RETURN_AND_SET_ERRNO(status); }