Commit 568c97c7 authored by Jonas Termansen's avatar Jonas Termansen

Fix SEEK_END, file offset overflow, and read/write/mkpartition syscall bugs.

Fix SEEK_END seeking twice as far as requested. Centralize lseek handling in one place and avoid overflow bugs. Inode lseek handlers now only need to handle SEEK_END with offset 0. Prevent the file offset from ever going below zero or overflowing. Character devices are now not seekable, but lseek will pretend they are, yet always stay at the file offset 0. pread/pwrite on character devices will now ignore the file offset and call read/write. This change prevents character devices from being memory mapped, notably /dev/zero can no longer be memory mapped. None of the current ports seem to rely on this behavior and will work with just MAP_ANONYMOUS. Refactor read and write system calls to have a shared return statement for both seekable and non-seekable IO. Fix file offset overflow bugs in read and write system calls. Fix system calls returning EPERM instead of properly returning EBADF when the file has not been opened in the right mode. Truncate IO counts and total vector IO length so the IO operation does not do any IO beyond OFF_MAX. Truncate also total vector IO length for recvmsg and sendmsg. Fail with EINVAL if total vector IO length exceeds SSIZE_MAX. Don't stop early if the total IO length is zero, so zero length IO now block on any locks internal to the inode. Handle reads at the maximum file offset with an end of file condition and handle writes of at least one byte at the maximum file offset by failing with EFBIG. Refactor UtilMemoryBuffer to store the file size using off_t instead of size_t to avoid casts and keep file sizes in the off_t type. Properly handle errors in the code, such as failing with EROFS instead of EBADF if the backing memory is not writeable, and failing with EFBIG if writing beyond the end of the file. Fix mkpartition not rejecting invalid partition start offsets and lengths. Strictly enforce partition start and length checks in the partition code. Enforce partitions exist within regular files or block devices. Fix a few indention issues.
parent f864c59d
This diff is collapsed.
/*
* Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Jonas 'Sortie' Termansen.
* Copyright (c) 2011-2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
......@@ -71,15 +71,8 @@ int PortNode::truncate(ioctx_t* /*ctx*/, off_t length)
off_t PortNode::lseek(ioctx_t* /*ctx*/, off_t offset, int whence)
{
if ( whence == SEEK_SET )
return offset;
if ( whence == SEEK_END )
{
off_t result;
if ( __builtin_add_overflow(harddisk->GetSize(), offset, &result) )
return errno = EOVERFLOW, -1;
return result;
}
if ( whence == SEEK_END && offset == 0 )
return (off_t) harddisk->GetSize();
return errno = EINVAL, -1;
}
......
......@@ -439,10 +439,8 @@ off_t FileCache::GetFileSize()
off_t FileCache::lseek(ioctx_t* /*ctx*/, off_t offset, int whence)
{
ScopedLock lock(&fcache_mutex);
if ( whence == SEEK_SET )
return offset;
if ( whence == SEEK_END )
return (off_t) file_size + offset;
if ( whence == SEEK_END && offset == 0 )
return (off_t) file_size;
return errno = EINVAL, -1;
}
......
......@@ -53,16 +53,6 @@ Full::~Full()
{
}
int Full::truncate(ioctx_t* /*ctx*/, off_t /*length*/)
{
return 0;
}
off_t Full::lseek(ioctx_t* /*ctx*/, off_t offset, int /*whence*/)
{
return offset;
}
ssize_t Full::read(ioctx_t* ctx, uint8_t* buf, size_t count)
{
ctx->zero_dest(buf, count);
......
......@@ -29,8 +29,6 @@ class Full : public AbstractInode
public:
Full(dev_t dev, ino_t ino, uid_t owner, gid_t group, mode_t mode);
virtual ~Full();
virtual int truncate(ioctx_t* ctx, off_t length);
virtual off_t lseek(ioctx_t* ctx, off_t offset, int whence);
virtual ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
virtual ssize_t pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off);
virtual ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
......
......@@ -50,16 +50,6 @@ Null::~Null()
{
}
int Null::truncate(ioctx_t* /*ctx*/, off_t /*length*/)
{
return 0;
}
off_t Null::lseek(ioctx_t* /*ctx*/, off_t offset, int /*whence*/)
{
return offset;
}
ssize_t Null::read(ioctx_t* /*ctx*/, uint8_t* /*buf*/, size_t /*count*/)
{
return 0;
......@@ -77,7 +67,7 @@ ssize_t Null::write(ioctx_t* /*ctx*/, const uint8_t* /*buf*/, size_t count)
}
ssize_t Null::pwrite(ioctx_t* /*ctx*/, const uint8_t* /*buf*/, size_t count,
off_t /*off*/)
off_t /*off*/)
{
return count;
}
......
......@@ -29,8 +29,6 @@ class Null : public AbstractInode
public:
Null(dev_t dev, ino_t ino, uid_t owner, gid_t group, mode_t mode);
virtual ~Null();
virtual int truncate(ioctx_t* ctx, off_t length);
virtual off_t lseek(ioctx_t* ctx, off_t offset, int whence);
virtual ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
virtual ssize_t pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off);
virtual ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
......
......@@ -20,7 +20,6 @@
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sortix/stat.h>
......@@ -32,7 +31,8 @@
namespace Sortix {
DevRandom::DevRandom(dev_t dev, ino_t ino, uid_t owner, gid_t group, mode_t mode)
DevRandom::DevRandom(dev_t dev, ino_t ino, uid_t owner, gid_t group,
mode_t mode)
{
inode_type = INODE_TYPE_STREAM;
if ( !dev )
......@@ -53,16 +53,6 @@ DevRandom::~DevRandom()
{
}
int DevRandom::truncate(ioctx_t* /*ctx*/, off_t /*length*/)
{
return 0;
}
off_t DevRandom::lseek(ioctx_t* /*ctx*/, off_t offset, int /*whence*/)
{
return offset;
}
ssize_t DevRandom::read(ioctx_t* ctx, uint8_t* buf, size_t count)
{
size_t sofar = 0;
......@@ -82,7 +72,8 @@ ssize_t DevRandom::read(ioctx_t* ctx, uint8_t* buf, size_t count)
return (ssize_t) sofar;
}
ssize_t DevRandom::pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t /*off*/)
ssize_t DevRandom::pread(ioctx_t* ctx, uint8_t* buf, size_t count,
off_t /*off*/)
{
return read(ctx, buf, count);
}
......@@ -92,8 +83,8 @@ ssize_t DevRandom::write(ioctx_t* /*ctx*/, const uint8_t* /*buf*/, size_t count)
return count;
}
ssize_t DevRandom::pwrite(ioctx_t* /*ctx*/, const uint8_t* /*buf*/, size_t count,
off_t /*off*/)
ssize_t DevRandom::pwrite(ioctx_t* /*ctx*/, const uint8_t* /*buf*/,
size_t count, off_t /*off*/)
{
return count;
}
......
......@@ -29,8 +29,6 @@ class DevRandom : public AbstractInode
public:
DevRandom(dev_t dev, ino_t ino, uid_t owner, gid_t group, mode_t mode);
virtual ~DevRandom();
virtual int truncate(ioctx_t* ctx, off_t length);
virtual off_t lseek(ioctx_t* ctx, off_t offset, int whence);
virtual ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
virtual ssize_t pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off);
virtual ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
......
......@@ -876,6 +876,8 @@ int Unode::truncate(ioctx_t* ctx, off_t length)
off_t Unode::lseek(ioctx_t* ctx, off_t offset, int whence)
{
if ( whence != SEEK_END && offset != 0 )
return errno = EINVAL, -1;
Channel* channel = server->Connect(ctx);
if ( !channel )
return -1;
......
/*
* Copyright (c) 2012 Jonas 'Sortie' Termansen.
* Copyright (c) 2012, 2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
......@@ -17,6 +17,7 @@
* Utility classes for kernel filesystems.
*/
#include <assert.h>
#include <errno.h>
#include <string.h>
......@@ -36,8 +37,9 @@ namespace Sortix {
UtilMemoryBuffer::UtilMemoryBuffer(dev_t dev, ino_t ino, uid_t owner,
gid_t group, mode_t mode, uint8_t* buf,
size_t bufsize, bool write, bool deletebuf)
off_t bufsize, bool write, bool deletebuf)
{
assert(0 <= bufsize);
inode_type = INODE_TYPE_FILE;
this->filelock = KTHREAD_MUTEX_INITIALIZER;
this->stat_uid = owner;
......@@ -45,7 +47,7 @@ UtilMemoryBuffer::UtilMemoryBuffer(dev_t dev, ino_t ino, uid_t owner,
this->type = S_IFREG;
this->stat_mode = (mode & S_SETABLE) | this->type;
this->stat_blksize = 1;
this->stat_size = (off_t) bufsize;
this->stat_size = bufsize;
this->dev = dev;
this->ino = ino ? ino : (ino_t) this;
this->buf = buf;
......@@ -63,30 +65,27 @@ UtilMemoryBuffer::~UtilMemoryBuffer()
int UtilMemoryBuffer::truncate(ioctx_t* /*ctx*/, off_t length)
{
ScopedLock lock(&filelock);
if ( (uintmax_t) length != (uintmax_t) bufsize )
return errno = ENOTSUP, -1;
if ( length != bufsize )
return errno = EPERM, -1;
return 0;
}
off_t UtilMemoryBuffer::lseek(ioctx_t* /*ctx*/, off_t offset, int whence)
{
ScopedLock lock(&filelock);
if ( whence == SEEK_SET )
return offset;
if ( whence == SEEK_END )
return (off_t) bufsize + offset;
errno = EINVAL;
return -1;
if ( whence == SEEK_END && offset == 0 )
return bufsize;
return errno = EINVAL, -1;
}
ssize_t UtilMemoryBuffer::pread(ioctx_t* ctx, uint8_t* dest, size_t count,
off_t off)
{
ScopedLock lock(&filelock);
if ( (uintmax_t) bufsize < (uintmax_t) off )
if ( bufsize < off )
return 0;
size_t available = bufsize - off;
if ( available < count )
off_t available = bufsize - off;
if ( (uintmax_t) available < (uintmax_t) count )
count = available;
if ( !ctx->copy_to_dest(dest, buf + off, count) )
return -1;
......@@ -97,16 +96,17 @@ ssize_t UtilMemoryBuffer::pwrite(ioctx_t* ctx, const uint8_t* src, size_t count,
off_t off)
{
ScopedLock lock(&filelock);
if ( !write ) { errno = EBADF; return -1; }
// TODO: Avoid having off + count overflow!
if ( bufsize < off + count )
return 0;
if ( (uintmax_t) bufsize <= (uintmax_t) off )
return -1;
size_t available = bufsize - off;
if ( available < count )
if ( !write )
return errno = EROFS, -1;
if ( bufsize < off )
return errno = EFBIG, -1;
off_t available = bufsize - off;
if ( available == 0 && count )
return errno = EFBIG, -1;
if ( (uintmax_t) available < (uintmax_t) count )
count = available;
ctx->copy_from_src(buf + off, src, count);
if ( !ctx->copy_from_src(buf + off, src, count) )
return -1;
return count;
}
......
......@@ -28,7 +28,7 @@ class UtilMemoryBuffer : public AbstractInode
{
public:
UtilMemoryBuffer(dev_t dev, ino_t ino, uid_t owner, gid_t group,
mode_t mode, uint8_t* buf, size_t bufsize,
mode_t mode, uint8_t* buf, off_t bufsize,
bool write = true, bool deletebuf = true);
virtual ~UtilMemoryBuffer();
virtual int truncate(ioctx_t* ctx, off_t length);
......@@ -41,7 +41,7 @@ public:
private:
kthread_mutex_t filelock;
uint8_t* buf;
size_t bufsize;
off_t bufsize;
bool write;
bool deletebuf;
......
/*
* Copyright (c) 2013, 2014 Jonas 'Sortie' Termansen.
* Copyright (c) 2013, 2014, 2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
......@@ -52,19 +52,10 @@ Zero::~Zero()
{
}
int Zero::truncate(ioctx_t* /*ctx*/, off_t /*length*/)
{
return 0;
}
off_t Zero::lseek(ioctx_t* /*ctx*/, off_t offset, int /*whence*/)
{
return offset;
}
ssize_t Zero::read(ioctx_t* ctx, uint8_t* buf, size_t count)
{
ctx->zero_dest(buf, count);
if ( !ctx->zero_dest(buf, count) )
return -1;
return (ssize_t) count;
}
......
......@@ -29,8 +29,6 @@ class Zero : public AbstractInode
public:
Zero(dev_t dev, ino_t ino, uid_t owner, gid_t group, mode_t mode);
virtual ~Zero();
virtual int truncate(ioctx_t* ctx, off_t length);
virtual off_t lseek(ioctx_t* ctx, off_t offset, int whence);
virtual ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
virtual ssize_t pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off);
virtual ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
......
......@@ -150,6 +150,7 @@ int LinkInodeInDir(ioctx_t* ctx, Ref<Descriptor> dir, const char* name,
Ref<Inode> inode);
Ref<Descriptor> OpenDirContainingPath(ioctx_t* ctx, Ref<Descriptor> from,
const char* path, char** finalp);
size_t TruncateIOVec(struct iovec* iov, int iovcnt, off_t limit);
} // namespace Sortix
......
......@@ -146,9 +146,7 @@ int AbstractInode::truncate(ioctx_t* /*ctx*/, off_t /*length*/)
off_t AbstractInode::lseek(ioctx_t* /*ctx*/, off_t /*offset*/, int /*whence*/)
{
if ( inode_type == INODE_TYPE_STREAM || inode_type == INODE_TYPE_TTY )
return errno = ESPIPE, -1;
return errno = EBADF, -1;
return errno = ESPIPE, -1;
}
ssize_t AbstractInode::read(ioctx_t* ctx, uint8_t* buf, size_t count)
......
......@@ -838,6 +838,14 @@ int sys_mkpartition(int fd, off_t start, off_t length, int flags)
Ref<Inode> inner_inode = desc->vnode->inode;
desc.Reset();
if ( !S_ISBLK(inner_inode->type) && !S_ISREG(inner_inode->type) )
return errno = EPERM, -1;
if ( start < 0 || length < 0 )
return errno = EINVAL, -1;
off_t end;
if ( __builtin_add_overflow(start, length, &end) )
return errno = EOVERFLOW, -1;
Ref<Inode> partition(new Partition(inner_inode, start, length));
if ( !partition )
return -1;
......
......@@ -327,7 +327,7 @@ void* sys_mmap(void* addr_ptr, size_t size, int prot, int flags, int fd,
if ( !(desc = process->GetDescriptor(fd)) )
return MAP_FAILED;
// Verify that the file is seekable.
if ( desc->lseek(&ctx, 0, SEEK_CUR) < 0 )
if ( S_ISCHR(desc->type) || desc->lseek(&ctx, 0, SEEK_END) < 0 )
return errno = ENODEV, MAP_FAILED;
// Verify that we have read access to the file.
if ( desc->read(&ctx, NULL, 0) != 0 )
......
......@@ -19,6 +19,7 @@
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <sortix/seek.h>
......@@ -35,6 +36,10 @@ namespace Sortix {
Partition::Partition(Ref<Inode> inner_inode, off_t start, off_t length)
{
assert(0 <= start);
assert(0 <= length);
assert(length <= OFF_MAX - start);
assert(S_ISBLK(inner_inode->type) || S_ISREG(inner_inode->type));
this->dev = (dev_t) this;
this->ino = (ino_t) this;
this->type = inner_inode->type;
......@@ -62,33 +67,48 @@ int Partition::truncate(ioctx_t* /*ctx*/, off_t new_length)
off_t Partition::lseek(ioctx_t* /*ctx*/, off_t offset, int whence)
{
if ( whence == SEEK_SET )
return offset;
if ( whence == SEEK_END )
// TODO: Avoid underflow and overflow!
return length + offset;
if ( whence == SEEK_END && offset == 0 )
return length;
return errno = EINVAL, -1;
}
ssize_t Partition::pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off)
{
if ( off < 0 )
return errno = EINVAL, -1;
if ( length <= off )
return 0;
off_t available = length - off;
if ( (uintmax_t) available < (uintmax_t) count )
count = available;
return inner_inode->pread(ctx, buf, count, start + off);
if ( count == 0 )
return 0;
off_t final_offset;
if ( __builtin_add_overflow(start, off, &final_offset) )
return errno = EOVERFLOW, -1;
assert(start <= final_offset);
assert(final_offset < start + length);
return inner_inode->pread(ctx, buf, count, final_offset);
}
ssize_t Partition::pwrite(ioctx_t* ctx, const uint8_t* buf, size_t count,
off_t off)
{
if ( off < 0 )
return errno = EINVAL, -1;
if ( length <= off )
return 0;
return count ? (errno = EFBIG, -1) : 0;
off_t available = length - off;
if ( (uintmax_t) available < (uintmax_t) count )
count = available;
return inner_inode->pwrite(ctx, buf, count, start + off);
if ( count == 0 )
return 0;
off_t final_offset;
if ( __builtin_add_overflow(start, off, &final_offset) )
return errno = EOVERFLOW, -1;
assert(start <= final_offset);
assert(final_offset < start + length);
return inner_inode->pwrite(ctx, buf, count, final_offset);
}
int Partition::stat(ioctx_t* ctx, struct stat* st)
......
......@@ -205,6 +205,8 @@ ssize_t PipeChannel::recvmsg_internal(ioctx_t* ctx, struct msghdr* msg,
return errno = EINTR, -1;
ssize_t so_far = 0;
size_t peeked = 0;
if ( SSIZE_MAX < TruncateIOVec(msg->msg_iov, msg->msg_iovlen, SSIZE_MAX) )
return errno = EINVAL, -1;
int iov_i = 0;
size_t iov_offset = 0;
while ( iov_i < msg->msg_iovlen && so_far < SSIZE_MAX )
......@@ -336,6 +338,8 @@ ssize_t PipeChannel::sendmsg_internal(ioctx_t* ctx, const struct msghdr* msg,
if ( !lock.IsAcquired() )
return errno = EINTR, -1;
sender_system_tid = this_thread->system_tid;
if ( SSIZE_MAX < TruncateIOVec(msg->msg_iov, msg->msg_iovlen, SSIZE_MAX) )
return errno = EINVAL, -1;
ssize_t so_far = 0;
int iov_i = 0;
size_t iov_offset = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment