static int secvault_data_open(struct inode *inode, struct file *filp) { int retval = 0; struct secvault_dev *dev; dev = container_of(inode->i_cdev, struct secvault_dev, cdev); filp->private_data = dev; if (!owns(dev)) { return -EACCES; } // Now trim to 0 the length of the device if open was write-only. if ( (filp->f_flags & O_ACCMODE) == O_WRONLY && (filp->f_flags & O_APPEND) == 0 ) { if (down_interruptible(&dev->sem)) { retval = -ERESTARTSYS; } else { retval = secvault_data_trim(dev); } up(&dev->sem); } return retval; }
static int secvault_data_release(struct inode *inode, struct file *filep) { if (!owns(filep->private_data)) { return -EACCES; } return 0; }
loff_t secvault_data_llseek(struct file *filp, loff_t off, int whence) { struct secvault_dev *dev = filp->private_data; loff_t newpos; if (!owns(dev)) { return -EACCES; } switch(whence) { case SEEK_SET: newpos = off; break; case SEEK_CUR: newpos = filp->f_pos + off; break; case SEEK_END: newpos = dev->size + off; break; default: return -EINVAL; } if (newpos < 0) return -EINVAL; filp->f_pos = newpos; return newpos; }
void pool::deallocate(index_type index) { if (!owns(index)) throw allocate_error(allocate_errc::invalid_ptr); *(index_type*)pointer(index) = index_type(head_); head_ = index; nfree_++; }
ssize_t secvault_data_read( struct file *filp, char __user *buff, size_t count, loff_t *f_pos ) { struct secvault_dev *dev = filp->private_data; ssize_t retval = 0; long curr_pos = (long) *f_pos; char *decrypted = NULL; if (down_interruptible(&dev->sem)) { retval = -ERESTARTSYS; goto up; } if (!owns(dev)) { retval = -EACCES; goto up; } // Requested read is out of bounds. if (curr_pos > dev->size || curr_pos < 0) { retval = -EINVAL; goto up; } if (curr_pos + count > dev->size) { count -= (curr_pos + count - dev->size); } // Allocate buffer for decrypted data. if ((decrypted = kmalloc(count * sizeof(char), GFP_KERNEL)) == NULL) { retval = -ENOMEM; goto up; } // Decrypt requested data. secvault_crypt(curr_pos, &(dev->data[curr_pos]), decrypted, &(dev->key[0]), count); // Copy decrypted data to userspace. if (copy_to_user(buff, decrypted, count)) { retval = -EFAULT; goto up; } *f_pos += count; retval = count; up: kfree(decrypted); up(&dev->sem); return retval; }
~LogItem() { if (owns()) { // todo: use raii and find a way to enable move-capture auto copy_ptr = new LogItem<Head, Tail>(std::move(*this)); logger_thread->dispatch([=] { std::unique_ptr<LogItem<Head, Tail>> ptr(copy_ptr); print(*ptr, '\n'); }); } }
Ticket TransportLayerMock::sourceMessage(Session& session, Message* message, Date_t expiration) { if (inShutdown()) { return Ticket(TransportLayer::ShutdownStatus); } else if (!owns(session.id())) { return Ticket(TransportLayer::SessionUnknownStatus); } else if (session.ended()) { return Ticket(Session::ClosedStatus); } return Ticket(this, stdx::make_unique<TransportLayerMock::TicketMock>(&session, message, expiration)); }
Status TransportLayerMock::wait(Ticket&& ticket) { if (inShutdown()) { return ShutdownStatus; } else if (!ticket.valid()) { return ticket.status(); } else if (!owns(ticket.sessionId())) { return TicketSessionUnknownStatus; } else if (get(ticket.sessionId())->ended()) { return Ticket::SessionClosedStatus; } return Status::OK(); }
Ticket TransportLayerMock::sinkMessage(const SessionHandle& session, const Message& message, Date_t expiration) { if (inShutdown()) { return Ticket(TransportLayer::ShutdownStatus); } else if (!owns(session->id())) { return Ticket(TransportLayer::SessionUnknownStatus); } else if (_sessions[session->id()].ended) { return Ticket(TransportLayer::TicketSessionClosedStatus); } return Ticket(this, stdx::make_unique<transport::MockTicket>(session, expiration)); }
int main() { std::vector<uint8_t> test = { 'a', 'b', 'c' }; // test non-owning range ByteRange alias(test.data(), test.data() + test.size(), ByteRange::Refer{}); auto alias_copy = alias; VERIFY(!alias.owns() && !alias_copy.owns()); VERIFY(are_equal(alias, alias_copy) && are_identical(alias, alias_copy)); // test owning range ByteRange owner(test.data(), test.data() + test.size(), ByteRange::Copy{}); auto owner_copy = owner; VERIFY(owner.owns() && owner_copy.owns()); VERIFY(are_equal(owner, owner_copy) && !are_identical(owner, owner_copy)); // test cross-type assignments VERIFY((alias = owner, alias.owns())); VERIFY((owner = alias_copy, !owner.owns())); }
/** * Frees the given block and resets it */ void deallocate(block &b) noexcept { if (!b) { return; } if (!owns(b)) { assert(!"It is not wise to let me deallocate a foreign Block!"); return; } auto p = find_owning_node(b); if (p != nullptr) { p->allocator.deallocate(b); } }
int secvault_delete(int id) { int retval; // Placeholder for uninitialized owner fields. After // deletion, nobody owns the vault. kuid_t nobody = { .val = -1 }; if (vaults[id].data == NULL) { return -ENXIO; } if (down_interruptible(&vaults[id].sem)) { retval = -ERESTARTSYS; goto up; } if (!owns(&vaults[id])) { retval = -EACCES; goto up; } cdev_del(&(vaults[id].cdev)); kfree(vaults[id].data); vaults[id].data = NULL; vaults[id].owner = nobody; retval = 0; up: up(&vaults[id].sem); return retval; } static void secvault_crypt( int pos, char *data, char *dest, char *key, int count ) { int i = 0; for (i = 0; i < count; i++) { dest[i] = data[i] ^ key[(pos + i) % SECVAULT_KEY_SIZE]; } }
void deallocate(block &b) noexcept { if (!b) { return; } if (!owns(b)) { assert(false); return; } // If it was the most recent allocated MemoryBlock, then we can re-use the // memory. Otherwise this freed MemoryBlock is not available for further // allocations. Since all happens on the stack this is not a leak! if (isLastUsedBlock(b)) { _p = static_cast<char *>(b.ptr); } b.reset(); }
int secvault_erase(int id) { int retval; if (vaults[id].data == NULL) { return -ENXIO; } if (down_interruptible(&vaults[id].sem)) { retval = -ERESTARTSYS; goto up; } if (!owns(&vaults[id])) { retval = -EACCES; goto up; } retval = secvault_data_trim(&vaults[id]); up: up(&vaults[id].sem); return retval; }
SessionHandle TransportLayerMock::get(Session::Id id) { if (!owns(id)) return nullptr; return _sessions[id].session; }
/* Permissions should all be IN-DB!! * Except can_program, because without that someone could take control * of their objects out of the hands of wizards. * Robin Powell, May 14, 1998 * */ int can_program(Objid player, Objid what) { return (owns(player, what) || is_wizard(player)); }
void disown() const { owns_.disown(); assert(!owns()); }
Session* TransportLayerMock::get(Session::Id id) { if (!owns(id)) return nullptr; return _sessions[id].get(); }
ssize_t secvault_data_write( struct file *filp, const char __user *buff, size_t count, loff_t *f_pos ) { struct secvault_dev *dev = filp->private_data; ssize_t retval = 0; long curr_pos = (long) *f_pos; ssize_t add_count = 0; if (down_interruptible(&dev->sem)) { retval = -ERESTARTSYS; goto up; } if (!owns(dev)) { retval = -EACCES; goto up; } // Seek to end of file before write if secvault was opened write only. if ((filp->f_flags & O_APPEND) != 0) { curr_pos = secvault_data_llseek(filp, 0, 2); } if (curr_pos < 0 || curr_pos > dev->size) { retval = -EINVAL; goto up; } if (curr_pos + count >= dev->size) { add_count = (curr_pos + count - dev->size); } // Return EGBIG, if process tries to write more data // then fits into the secvault, nothing is written. if (dev->size + add_count > dev->max_size) { retval = -EFBIG; goto up; } if (copy_from_user(&dev->data[curr_pos], buff, count)) { retval = -EFAULT; goto up; } secvault_crypt( curr_pos, &dev->data[curr_pos], &(dev->data[curr_pos]), &(dev->key[0]), count ); *f_pos += count; retval = count; dev->size += add_count; up: up(&dev->sem); return retval; }
void TransportLayerMock::end(const SessionHandle& session) { if (!owns(session->id())) return; _sessions[session->id()].ended = true; }