idx int64 | func_before string | Vulnerability Classification string | vul int64 | func_after string | patch string | CWE ID string | lines_before string | lines_after string |
|---|---|---|---|---|---|---|---|---|
27,100 | long vhost_dev_check_owner(struct vhost_dev *dev)
{
/* Are you the owner? If not, I don't think you mean to do that */
return dev->mm == current->mm ? 0 : -EPERM;
}
| null | 0 | long vhost_dev_check_owner(struct vhost_dev *dev)
{
/* Are you the owner? If not, I don't think you mean to do that */
return dev->mm == current->mm ? 0 : -EPERM;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,101 | void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
{
int i;
for (i = 0; i < dev->nvqs; ++i) {
if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
vhost_poll_stop(&dev->vqs[i].poll);
vhost_poll_flush(&dev->vqs[i].poll);
}
/* Wait for all lower device DMAs done. */
if (dev->vqs[i].ubufs)
vhost_ubuf_put_and_wait(dev->vqs[i].ubufs);
/* Signal guest as appropriate. */
vhost_zerocopy_signal_used(&dev->vqs[i]);
if (dev->vqs[i].error_ctx)
eventfd_ctx_put(dev->vqs[i].error_ctx);
if (dev->vqs[i].error)
fput(dev->vqs[i].error);
if (dev->vqs[i].kick)
fput(dev->vqs[i].kick);
if (dev->vqs[i].call_ctx)
eventfd_ctx_put(dev->vqs[i].call_ctx);
if (dev->vqs[i].call)
fput(dev->vqs[i].call);
vhost_vq_reset(dev, dev->vqs + i);
}
vhost_dev_free_iovecs(dev);
if (dev->log_ctx)
eventfd_ctx_put(dev->log_ctx);
dev->log_ctx = NULL;
if (dev->log_file)
fput(dev->log_file);
dev->log_file = NULL;
/* No one will access memory at this point */
kfree(rcu_dereference_protected(dev->memory,
locked ==
lockdep_is_held(&dev->mutex)));
RCU_INIT_POINTER(dev->memory, NULL);
WARN_ON(!list_empty(&dev->work_list));
if (dev->worker) {
kthread_stop(dev->worker);
dev->worker = NULL;
}
if (dev->mm)
mmput(dev->mm);
dev->mm = NULL;
}
| null | 0 | void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
{
int i;
for (i = 0; i < dev->nvqs; ++i) {
if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
vhost_poll_stop(&dev->vqs[i].poll);
vhost_poll_flush(&dev->vqs[i].poll);
}
/* Wait for all lower device DMAs done. */
if (dev->vqs[i].ubufs)
vhost_ubuf_put_and_wait(dev->vqs[i].ubufs);
/* Signal guest as appropriate. */
vhost_zerocopy_signal_used(&dev->vqs[i]);
if (dev->vqs[i].error_ctx)
eventfd_ctx_put(dev->vqs[i].error_ctx);
if (dev->vqs[i].error)
fput(dev->vqs[i].error);
if (dev->vqs[i].kick)
fput(dev->vqs[i].kick);
if (dev->vqs[i].call_ctx)
eventfd_ctx_put(dev->vqs[i].call_ctx);
if (dev->vqs[i].call)
fput(dev->vqs[i].call);
vhost_vq_reset(dev, dev->vqs + i);
}
vhost_dev_free_iovecs(dev);
if (dev->log_ctx)
eventfd_ctx_put(dev->log_ctx);
dev->log_ctx = NULL;
if (dev->log_file)
fput(dev->log_file);
dev->log_file = NULL;
/* No one will access memory at this point */
kfree(rcu_dereference_protected(dev->memory,
locked ==
lockdep_is_held(&dev->mutex)));
RCU_INIT_POINTER(dev->memory, NULL);
WARN_ON(!list_empty(&dev->work_list));
if (dev->worker) {
kthread_stop(dev->worker);
dev->worker = NULL;
}
if (dev->mm)
mmput(dev->mm);
dev->mm = NULL;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,102 | long vhost_dev_init(struct vhost_dev *dev,
struct vhost_virtqueue *vqs, int nvqs)
{
int i;
dev->vqs = vqs;
dev->nvqs = nvqs;
mutex_init(&dev->mutex);
dev->log_ctx = NULL;
dev->log_file = NULL;
dev->memory = NULL;
dev->mm = NULL;
spin_lock_init(&dev->work_lock);
INIT_LIST_HEAD(&dev->work_list);
dev->worker = NULL;
for (i = 0; i < dev->nvqs; ++i) {
dev->vqs[i].log = NULL;
dev->vqs[i].indirect = NULL;
dev->vqs[i].heads = NULL;
dev->vqs[i].ubuf_info = NULL;
dev->vqs[i].dev = dev;
mutex_init(&dev->vqs[i].mutex);
vhost_vq_reset(dev, dev->vqs + i);
if (dev->vqs[i].handle_kick)
vhost_poll_init(&dev->vqs[i].poll,
dev->vqs[i].handle_kick, POLLIN, dev);
}
return 0;
}
| null | 0 | long vhost_dev_init(struct vhost_dev *dev,
struct vhost_virtqueue *vqs, int nvqs)
{
int i;
dev->vqs = vqs;
dev->nvqs = nvqs;
mutex_init(&dev->mutex);
dev->log_ctx = NULL;
dev->log_file = NULL;
dev->memory = NULL;
dev->mm = NULL;
spin_lock_init(&dev->work_lock);
INIT_LIST_HEAD(&dev->work_list);
dev->worker = NULL;
for (i = 0; i < dev->nvqs; ++i) {
dev->vqs[i].log = NULL;
dev->vqs[i].indirect = NULL;
dev->vqs[i].heads = NULL;
dev->vqs[i].ubuf_info = NULL;
dev->vqs[i].dev = dev;
mutex_init(&dev->vqs[i].mutex);
vhost_vq_reset(dev, dev->vqs + i);
if (dev->vqs[i].handle_kick)
vhost_poll_init(&dev->vqs[i].poll,
dev->vqs[i].handle_kick, POLLIN, dev);
}
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,103 | long vhost_dev_reset_owner(struct vhost_dev *dev)
{
struct vhost_memory *memory;
/* Restore memory to default empty mapping. */
memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
if (!memory)
return -ENOMEM;
vhost_dev_cleanup(dev, true);
memory->nregions = 0;
RCU_INIT_POINTER(dev->memory, memory);
return 0;
}
| null | 0 | long vhost_dev_reset_owner(struct vhost_dev *dev)
{
struct vhost_memory *memory;
/* Restore memory to default empty mapping. */
memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
if (!memory)
return -ENOMEM;
vhost_dev_cleanup(dev, true);
memory->nregions = 0;
RCU_INIT_POINTER(dev->memory, memory);
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,104 | void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
int r;
if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
return;
vq->used_flags |= VRING_USED_F_NO_NOTIFY;
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
r = vhost_update_used_flags(vq);
if (r)
vq_err(vq, "Failed to enable notification at %p: %d\n",
&vq->used->flags, r);
}
}
| null | 0 | void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
int r;
if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
return;
vq->used_flags |= VRING_USED_F_NO_NOTIFY;
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
r = vhost_update_used_flags(vq);
if (r)
vq_err(vq, "Failed to enable notification at %p: %d\n",
&vq->used->flags, r);
}
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,105 | void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
{
vq->last_avail_idx -= n;
}
| null | 0 | void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
{
vq->last_avail_idx -= n;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,106 | bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
u16 avail_idx;
int r;
if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
return false;
vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
r = vhost_update_used_flags(vq);
if (r) {
vq_err(vq, "Failed to enable notification at %p: %d\n",
&vq->used->flags, r);
return false;
}
} else {
r = vhost_update_avail_event(vq, vq->avail_idx);
if (r) {
vq_err(vq, "Failed to update avail event index at %p: %d\n",
vhost_avail_event(vq), r);
return false;
}
}
/* They could have slipped one in as we were doing that: make
* sure it's written, then check again. */
smp_mb();
r = __get_user(avail_idx, &vq->avail->idx);
if (r) {
vq_err(vq, "Failed to check avail idx at %p: %d\n",
&vq->avail->idx, r);
return false;
}
return avail_idx != vq->avail_idx;
}
| null | 0 | bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
u16 avail_idx;
int r;
if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
return false;
vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
r = vhost_update_used_flags(vq);
if (r) {
vq_err(vq, "Failed to enable notification at %p: %d\n",
&vq->used->flags, r);
return false;
}
} else {
r = vhost_update_avail_event(vq, vq->avail_idx);
if (r) {
vq_err(vq, "Failed to update avail event index at %p: %d\n",
vhost_avail_event(vq), r);
return false;
}
}
/* They could have slipped one in as we were doing that: make
* sure it's written, then check again. */
smp_mb();
r = __get_user(avail_idx, &vq->avail->idx);
if (r) {
vq_err(vq, "Failed to check avail idx at %p: %d\n",
&vq->avail->idx, r);
return false;
}
return avail_idx != vq->avail_idx;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,107 | void vhost_enable_zcopy(int vq)
{
vhost_zcopy_mask |= 0x1 << vq;
}
| null | 0 | void vhost_enable_zcopy(int vq)
{
vhost_zcopy_mask |= 0x1 << vq;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,108 | int vhost_init_used(struct vhost_virtqueue *vq)
{
int r;
if (!vq->private_data)
return 0;
r = vhost_update_used_flags(vq);
if (r)
return r;
vq->signalled_used_valid = false;
return get_user(vq->last_used_idx, &vq->used->idx);
}
| null | 0 | int vhost_init_used(struct vhost_virtqueue *vq)
{
int r;
if (!vq->private_data)
return 0;
r = vhost_update_used_flags(vq);
if (r)
return r;
vq->signalled_used_valid = false;
return get_user(vq->last_used_idx, &vq->used->idx);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,109 | int vhost_log_access_ok(struct vhost_dev *dev)
{
struct vhost_memory *mp;
mp = rcu_dereference_protected(dev->memory,
lockdep_is_held(&dev->mutex));
return memory_access_ok(dev, mp, 1);
}
| null | 0 | int vhost_log_access_ok(struct vhost_dev *dev)
{
struct vhost_memory *mp;
mp = rcu_dereference_protected(dev->memory,
lockdep_is_held(&dev->mutex));
return memory_access_ok(dev, mp, 1);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,110 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
unsigned int log_num, u64 len)
{
int i, r;
/* Make sure data written is seen before log. */
smp_wmb();
for (i = 0; i < log_num; ++i) {
u64 l = min(log[i].len, len);
r = log_write(vq->log_base, log[i].addr, l);
if (r < 0)
return r;
len -= l;
if (!len) {
if (vq->log_ctx)
eventfd_signal(vq->log_ctx, 1);
return 0;
}
}
/* Length written exceeds what we have stored. This is a bug. */
BUG();
return 0;
}
| null | 0 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
unsigned int log_num, u64 len)
{
int i, r;
/* Make sure data written is seen before log. */
smp_wmb();
for (i = 0; i < log_num; ++i) {
u64 l = min(log[i].len, len);
r = log_write(vq->log_base, log[i].addr, l);
if (r < 0)
return r;
len -= l;
if (!len) {
if (vq->log_ctx)
eventfd_signal(vq->log_ctx, 1);
return 0;
}
}
/* Length written exceeds what we have stored. This is a bug. */
BUG();
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,111 | static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
__u16 old, new, event;
bool v;
/* Flush out used index updates. This is paired
* with the barrier that the Guest executes when enabling
* interrupts. */
smp_mb();
if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
unlikely(vq->avail_idx == vq->last_avail_idx))
return true;
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
__u16 flags;
if (__get_user(flags, &vq->avail->flags)) {
vq_err(vq, "Failed to get flags");
return true;
}
return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
}
old = vq->signalled_used;
v = vq->signalled_used_valid;
new = vq->signalled_used = vq->last_used_idx;
vq->signalled_used_valid = true;
if (unlikely(!v))
return true;
if (get_user(event, vhost_used_event(vq))) {
vq_err(vq, "Failed to get used event idx");
return true;
}
return vring_need_event(event, new, old);
}
| null | 0 | static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
__u16 old, new, event;
bool v;
/* Flush out used index updates. This is paired
* with the barrier that the Guest executes when enabling
* interrupts. */
smp_mb();
if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
unlikely(vq->avail_idx == vq->last_avail_idx))
return true;
if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
__u16 flags;
if (__get_user(flags, &vq->avail->flags)) {
vq_err(vq, "Failed to get flags");
return true;
}
return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
}
old = vq->signalled_used;
v = vq->signalled_used_valid;
new = vq->signalled_used = vq->last_used_idx;
vq->signalled_used_valid = true;
if (unlikely(!v))
return true;
if (get_user(event, vhost_used_event(vq))) {
vq_err(vq, "Failed to get used event idx");
return true;
}
return vring_need_event(event, new, old);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,112 | void vhost_poll_flush(struct vhost_poll *poll)
{
vhost_work_flush(poll->dev, &poll->work);
}
| null | 0 | void vhost_poll_flush(struct vhost_poll *poll)
{
vhost_work_flush(poll->dev, &poll->work);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,113 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
unsigned long mask, struct vhost_dev *dev)
{
init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
init_poll_funcptr(&poll->table, vhost_poll_func);
poll->mask = mask;
poll->dev = dev;
vhost_work_init(&poll->work, fn);
}
| null | 0 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
unsigned long mask, struct vhost_dev *dev)
{
init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
init_poll_funcptr(&poll->table, vhost_poll_func);
poll->mask = mask;
poll->dev = dev;
vhost_work_init(&poll->work, fn);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,114 | void vhost_poll_start(struct vhost_poll *poll, struct file *file)
{
unsigned long mask;
mask = file->f_op->poll(file, &poll->table);
if (mask)
vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
}
| null | 0 | void vhost_poll_start(struct vhost_poll *poll, struct file *file)
{
unsigned long mask;
mask = file->f_op->poll(file, &poll->table);
if (mask)
vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,115 | static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
void *key)
{
struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
if (!((unsigned long)key & poll->mask))
return 0;
vhost_poll_queue(poll);
return 0;
}
| null | 0 | static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
void *key)
{
struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
if (!((unsigned long)key & poll->mask))
return 0;
vhost_poll_queue(poll);
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,116 | static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
{
struct vhost_memory mem, *newmem, *oldmem;
unsigned long size = offsetof(struct vhost_memory, regions);
if (copy_from_user(&mem, m, size))
return -EFAULT;
if (mem.padding)
return -EOPNOTSUPP;
if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
return -E2BIG;
newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
if (!newmem)
return -ENOMEM;
memcpy(newmem, &mem, size);
if (copy_from_user(newmem->regions, m->regions,
mem.nregions * sizeof *m->regions)) {
kfree(newmem);
return -EFAULT;
}
if (!memory_access_ok(d, newmem,
vhost_has_feature(d, VHOST_F_LOG_ALL))) {
kfree(newmem);
return -EFAULT;
}
oldmem = rcu_dereference_protected(d->memory,
lockdep_is_held(&d->mutex));
rcu_assign_pointer(d->memory, newmem);
synchronize_rcu();
kfree(oldmem);
return 0;
}
| null | 0 | static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
{
struct vhost_memory mem, *newmem, *oldmem;
unsigned long size = offsetof(struct vhost_memory, regions);
if (copy_from_user(&mem, m, size))
return -EFAULT;
if (mem.padding)
return -EOPNOTSUPP;
if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
return -E2BIG;
newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
if (!newmem)
return -ENOMEM;
memcpy(newmem, &mem, size);
if (copy_from_user(newmem->regions, m->regions,
mem.nregions * sizeof *m->regions)) {
kfree(newmem);
return -EFAULT;
}
if (!memory_access_ok(d, newmem,
vhost_has_feature(d, VHOST_F_LOG_ALL))) {
kfree(newmem);
return -EFAULT;
}
oldmem = rcu_dereference_protected(d->memory,
lockdep_is_held(&d->mutex));
rcu_assign_pointer(d->memory, newmem);
synchronize_rcu();
kfree(oldmem);
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,117 | static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
{
struct file *eventfp, *filep = NULL;
bool pollstart = false, pollstop = false;
struct eventfd_ctx *ctx = NULL;
u32 __user *idxp = argp;
struct vhost_virtqueue *vq;
struct vhost_vring_state s;
struct vhost_vring_file f;
struct vhost_vring_addr a;
u32 idx;
long r;
r = get_user(idx, idxp);
if (r < 0)
return r;
if (idx >= d->nvqs)
return -ENOBUFS;
vq = d->vqs + idx;
mutex_lock(&vq->mutex);
switch (ioctl) {
case VHOST_SET_VRING_NUM:
/* Resizing ring with an active backend?
* You don't want to do that. */
if (vq->private_data) {
r = -EBUSY;
break;
}
if (copy_from_user(&s, argp, sizeof s)) {
r = -EFAULT;
break;
}
if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
r = -EINVAL;
break;
}
vq->num = s.num;
break;
case VHOST_SET_VRING_BASE:
/* Moving base with an active backend?
* You don't want to do that. */
if (vq->private_data) {
r = -EBUSY;
break;
}
if (copy_from_user(&s, argp, sizeof s)) {
r = -EFAULT;
break;
}
if (s.num > 0xffff) {
r = -EINVAL;
break;
}
vq->last_avail_idx = s.num;
/* Forget the cached index value. */
vq->avail_idx = vq->last_avail_idx;
break;
case VHOST_GET_VRING_BASE:
s.index = idx;
s.num = vq->last_avail_idx;
if (copy_to_user(argp, &s, sizeof s))
r = -EFAULT;
break;
case VHOST_SET_VRING_ADDR:
if (copy_from_user(&a, argp, sizeof a)) {
r = -EFAULT;
break;
}
if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
r = -EOPNOTSUPP;
break;
}
/* For 32bit, verify that the top 32bits of the user
data are set to zero. */
if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
(u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
(u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
r = -EFAULT;
break;
}
if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
(a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
(a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
r = -EINVAL;
break;
}
/* We only verify access here if backend is configured.
* If it is not, we don't as size might not have been setup.
* We will verify when backend is configured. */
if (vq->private_data) {
if (!vq_access_ok(d, vq->num,
(void __user *)(unsigned long)a.desc_user_addr,
(void __user *)(unsigned long)a.avail_user_addr,
(void __user *)(unsigned long)a.used_user_addr)) {
r = -EINVAL;
break;
}
/* Also validate log access for used ring if enabled. */
if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
!log_access_ok(vq->log_base, a.log_guest_addr,
sizeof *vq->used +
vq->num * sizeof *vq->used->ring)) {
r = -EINVAL;
break;
}
}
vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
vq->log_addr = a.log_guest_addr;
vq->used = (void __user *)(unsigned long)a.used_user_addr;
break;
case VHOST_SET_VRING_KICK:
if (copy_from_user(&f, argp, sizeof f)) {
r = -EFAULT;
break;
}
eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
if (IS_ERR(eventfp)) {
r = PTR_ERR(eventfp);
break;
}
if (eventfp != vq->kick) {
pollstop = (filep = vq->kick) != NULL;
pollstart = (vq->kick = eventfp) != NULL;
} else
filep = eventfp;
break;
case VHOST_SET_VRING_CALL:
if (copy_from_user(&f, argp, sizeof f)) {
r = -EFAULT;
break;
}
eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
if (IS_ERR(eventfp)) {
r = PTR_ERR(eventfp);
break;
}
if (eventfp != vq->call) {
filep = vq->call;
ctx = vq->call_ctx;
vq->call = eventfp;
vq->call_ctx = eventfp ?
eventfd_ctx_fileget(eventfp) : NULL;
} else
filep = eventfp;
break;
case VHOST_SET_VRING_ERR:
if (copy_from_user(&f, argp, sizeof f)) {
r = -EFAULT;
break;
}
eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
if (IS_ERR(eventfp)) {
r = PTR_ERR(eventfp);
break;
}
if (eventfp != vq->error) {
filep = vq->error;
vq->error = eventfp;
ctx = vq->error_ctx;
vq->error_ctx = eventfp ?
eventfd_ctx_fileget(eventfp) : NULL;
} else
filep = eventfp;
break;
default:
r = -ENOIOCTLCMD;
}
if (pollstop && vq->handle_kick)
vhost_poll_stop(&vq->poll);
if (ctx)
eventfd_ctx_put(ctx);
if (filep)
fput(filep);
if (pollstart && vq->handle_kick)
vhost_poll_start(&vq->poll, vq->kick);
mutex_unlock(&vq->mutex);
if (pollstop && vq->handle_kick)
vhost_poll_flush(&vq->poll);
return r;
}
| null | 0 | static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
{
struct file *eventfp, *filep = NULL;
bool pollstart = false, pollstop = false;
struct eventfd_ctx *ctx = NULL;
u32 __user *idxp = argp;
struct vhost_virtqueue *vq;
struct vhost_vring_state s;
struct vhost_vring_file f;
struct vhost_vring_addr a;
u32 idx;
long r;
r = get_user(idx, idxp);
if (r < 0)
return r;
if (idx >= d->nvqs)
return -ENOBUFS;
vq = d->vqs + idx;
mutex_lock(&vq->mutex);
switch (ioctl) {
case VHOST_SET_VRING_NUM:
/* Resizing ring with an active backend?
* You don't want to do that. */
if (vq->private_data) {
r = -EBUSY;
break;
}
if (copy_from_user(&s, argp, sizeof s)) {
r = -EFAULT;
break;
}
if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
r = -EINVAL;
break;
}
vq->num = s.num;
break;
case VHOST_SET_VRING_BASE:
/* Moving base with an active backend?
* You don't want to do that. */
if (vq->private_data) {
r = -EBUSY;
break;
}
if (copy_from_user(&s, argp, sizeof s)) {
r = -EFAULT;
break;
}
if (s.num > 0xffff) {
r = -EINVAL;
break;
}
vq->last_avail_idx = s.num;
/* Forget the cached index value. */
vq->avail_idx = vq->last_avail_idx;
break;
case VHOST_GET_VRING_BASE:
s.index = idx;
s.num = vq->last_avail_idx;
if (copy_to_user(argp, &s, sizeof s))
r = -EFAULT;
break;
case VHOST_SET_VRING_ADDR:
if (copy_from_user(&a, argp, sizeof a)) {
r = -EFAULT;
break;
}
if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
r = -EOPNOTSUPP;
break;
}
/* For 32bit, verify that the top 32bits of the user
data are set to zero. */
if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
(u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
(u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
r = -EFAULT;
break;
}
if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
(a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
(a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
r = -EINVAL;
break;
}
/* We only verify access here if backend is configured.
* If it is not, we don't as size might not have been setup.
* We will verify when backend is configured. */
if (vq->private_data) {
if (!vq_access_ok(d, vq->num,
(void __user *)(unsigned long)a.desc_user_addr,
(void __user *)(unsigned long)a.avail_user_addr,
(void __user *)(unsigned long)a.used_user_addr)) {
r = -EINVAL;
break;
}
/* Also validate log access for used ring if enabled. */
if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
!log_access_ok(vq->log_base, a.log_guest_addr,
sizeof *vq->used +
vq->num * sizeof *vq->used->ring)) {
r = -EINVAL;
break;
}
}
vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
vq->log_addr = a.log_guest_addr;
vq->used = (void __user *)(unsigned long)a.used_user_addr;
break;
case VHOST_SET_VRING_KICK:
if (copy_from_user(&f, argp, sizeof f)) {
r = -EFAULT;
break;
}
eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
if (IS_ERR(eventfp)) {
r = PTR_ERR(eventfp);
break;
}
if (eventfp != vq->kick) {
pollstop = (filep = vq->kick) != NULL;
pollstart = (vq->kick = eventfp) != NULL;
} else
filep = eventfp;
break;
case VHOST_SET_VRING_CALL:
if (copy_from_user(&f, argp, sizeof f)) {
r = -EFAULT;
break;
}
eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
if (IS_ERR(eventfp)) {
r = PTR_ERR(eventfp);
break;
}
if (eventfp != vq->call) {
filep = vq->call;
ctx = vq->call_ctx;
vq->call = eventfp;
vq->call_ctx = eventfp ?
eventfd_ctx_fileget(eventfp) : NULL;
} else
filep = eventfp;
break;
case VHOST_SET_VRING_ERR:
if (copy_from_user(&f, argp, sizeof f)) {
r = -EFAULT;
break;
}
eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
if (IS_ERR(eventfp)) {
r = PTR_ERR(eventfp);
break;
}
if (eventfp != vq->error) {
filep = vq->error;
vq->error = eventfp;
ctx = vq->error_ctx;
vq->error_ctx = eventfp ?
eventfd_ctx_fileget(eventfp) : NULL;
} else
filep = eventfp;
break;
default:
r = -ENOIOCTLCMD;
}
if (pollstop && vq->handle_kick)
vhost_poll_stop(&vq->poll);
if (ctx)
eventfd_ctx_put(ctx);
if (filep)
fput(filep);
if (pollstart && vq->handle_kick)
vhost_poll_start(&vq->poll, vq->kick);
mutex_unlock(&vq->mutex);
if (pollstop && vq->handle_kick)
vhost_poll_flush(&vq->poll);
return r;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,118 | void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
/* Signal the Guest tell them we used something up. */
if (vq->call_ctx && vhost_notify(dev, vq))
eventfd_signal(vq->call_ctx, 1);
}
| null | 0 | void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
{
/* Signal the Guest tell them we used something up. */
if (vq->call_ctx && vhost_notify(dev, vq))
eventfd_signal(vq->call_ctx, 1);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,119 | struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *vq,
bool zcopy)
{
struct vhost_ubuf_ref *ubufs;
/* No zero copy backend? Nothing to count. */
if (!zcopy)
return NULL;
ubufs = kmalloc(sizeof *ubufs, GFP_KERNEL);
if (!ubufs)
return ERR_PTR(-ENOMEM);
kref_init(&ubufs->kref);
init_waitqueue_head(&ubufs->wait);
ubufs->vq = vq;
return ubufs;
}
| null | 0 | struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *vq,
bool zcopy)
{
struct vhost_ubuf_ref *ubufs;
/* No zero copy backend? Nothing to count. */
if (!zcopy)
return NULL;
ubufs = kmalloc(sizeof *ubufs, GFP_KERNEL);
if (!ubufs)
return ERR_PTR(-ENOMEM);
kref_init(&ubufs->kref);
init_waitqueue_head(&ubufs->wait);
ubufs->vq = vq;
return ubufs;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,120 | void vhost_ubuf_put(struct vhost_ubuf_ref *ubufs)
{
kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
}
| null | 0 | void vhost_ubuf_put(struct vhost_ubuf_ref *ubufs)
{
kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,121 | void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
{
kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
kfree(ubufs);
}
| null | 0 | void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
{
kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
kfree(ubufs);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,122 | static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
{
if (__put_user(vq->avail_idx, vhost_avail_event(vq)))
return -EFAULT;
if (unlikely(vq->log_used)) {
void __user *used;
/* Make sure the event is seen before log. */
smp_wmb();
/* Log avail event write */
used = vhost_avail_event(vq);
log_write(vq->log_base, vq->log_addr +
(used - (void __user *)vq->used),
sizeof *vhost_avail_event(vq));
if (vq->log_ctx)
eventfd_signal(vq->log_ctx, 1);
}
return 0;
}
| null | 0 | static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
{
if (__put_user(vq->avail_idx, vhost_avail_event(vq)))
return -EFAULT;
if (unlikely(vq->log_used)) {
void __user *used;
/* Make sure the event is seen before log. */
smp_wmb();
/* Log avail event write */
used = vhost_avail_event(vq);
log_write(vq->log_base, vq->log_addr +
(used - (void __user *)vq->used),
sizeof *vhost_avail_event(vq));
if (vq->log_ctx)
eventfd_signal(vq->log_ctx, 1);
}
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,123 | static int vhost_update_used_flags(struct vhost_virtqueue *vq)
{
void __user *used;
if (__put_user(vq->used_flags, &vq->used->flags) < 0)
return -EFAULT;
if (unlikely(vq->log_used)) {
/* Make sure the flag is seen before log. */
smp_wmb();
/* Log used flag write. */
used = &vq->used->flags;
log_write(vq->log_base, vq->log_addr +
(used - (void __user *)vq->used),
sizeof vq->used->flags);
if (vq->log_ctx)
eventfd_signal(vq->log_ctx, 1);
}
return 0;
}
| null | 0 | static int vhost_update_used_flags(struct vhost_virtqueue *vq)
{
void __user *used;
if (__put_user(vq->used_flags, &vq->used->flags) < 0)
return -EFAULT;
if (unlikely(vq->log_used)) {
/* Make sure the flag is seen before log. */
smp_wmb();
/* Log used flag write. */
used = &vq->used->flags;
log_write(vq->log_base, vq->log_addr +
(used - (void __user *)vq->used),
sizeof vq->used->flags);
if (vq->log_ctx)
eventfd_signal(vq->log_ctx, 1);
}
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,124 | int vhost_vq_access_ok(struct vhost_virtqueue *vq)
{
return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
vq_log_access_ok(vq->dev, vq, vq->log_base);
}
| null | 0 | int vhost_vq_access_ok(struct vhost_virtqueue *vq)
{
return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
vq_log_access_ok(vq->dev, vq, vq->log_base);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,125 | static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
{
kfree(vq->indirect);
vq->indirect = NULL;
kfree(vq->log);
vq->log = NULL;
kfree(vq->heads);
vq->heads = NULL;
kfree(vq->ubuf_info);
vq->ubuf_info = NULL;
}
| null | 0 | static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
{
kfree(vq->indirect);
vq->indirect = NULL;
kfree(vq->log);
vq->log = NULL;
kfree(vq->heads);
vq->heads = NULL;
kfree(vq->ubuf_info);
vq->ubuf_info = NULL;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,126 | static void vhost_vq_reset(struct vhost_dev *dev,
struct vhost_virtqueue *vq)
{
vq->num = 1;
vq->desc = NULL;
vq->avail = NULL;
vq->used = NULL;
vq->last_avail_idx = 0;
vq->avail_idx = 0;
vq->last_used_idx = 0;
vq->signalled_used = 0;
vq->signalled_used_valid = false;
vq->used_flags = 0;
vq->log_used = false;
vq->log_addr = -1ull;
vq->vhost_hlen = 0;
vq->sock_hlen = 0;
vq->private_data = NULL;
vq->log_base = NULL;
vq->error_ctx = NULL;
vq->error = NULL;
vq->kick = NULL;
vq->call_ctx = NULL;
vq->call = NULL;
vq->log_ctx = NULL;
vq->upend_idx = 0;
vq->done_idx = 0;
vq->ubufs = NULL;
}
| null | 0 | static void vhost_vq_reset(struct vhost_dev *dev,
struct vhost_virtqueue *vq)
{
vq->num = 1;
vq->desc = NULL;
vq->avail = NULL;
vq->used = NULL;
vq->last_avail_idx = 0;
vq->avail_idx = 0;
vq->last_used_idx = 0;
vq->signalled_used = 0;
vq->signalled_used_valid = false;
vq->used_flags = 0;
vq->log_used = false;
vq->log_addr = -1ull;
vq->vhost_hlen = 0;
vq->sock_hlen = 0;
vq->private_data = NULL;
vq->log_base = NULL;
vq->error_ctx = NULL;
vq->error = NULL;
vq->kick = NULL;
vq->call_ctx = NULL;
vq->call = NULL;
vq->log_ctx = NULL;
vq->upend_idx = 0;
vq->done_idx = 0;
vq->ubufs = NULL;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,127 | static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
{
unsigned seq;
int flushing;
spin_lock_irq(&dev->work_lock);
seq = work->queue_seq;
work->flushing++;
spin_unlock_irq(&dev->work_lock);
wait_event(work->done, vhost_work_seq_done(dev, work, seq));
spin_lock_irq(&dev->work_lock);
flushing = --work->flushing;
spin_unlock_irq(&dev->work_lock);
BUG_ON(flushing < 0);
}
| null | 0 | static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
{
unsigned seq;
int flushing;
spin_lock_irq(&dev->work_lock);
seq = work->queue_seq;
work->flushing++;
spin_unlock_irq(&dev->work_lock);
wait_event(work->done, vhost_work_seq_done(dev, work, seq));
spin_lock_irq(&dev->work_lock);
flushing = --work->flushing;
spin_unlock_irq(&dev->work_lock);
BUG_ON(flushing < 0);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,128 | void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
{
INIT_LIST_HEAD(&work->node);
work->fn = fn;
init_waitqueue_head(&work->done);
work->flushing = 0;
work->queue_seq = work->done_seq = 0;
}
| null | 0 | void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
{
INIT_LIST_HEAD(&work->node);
work->fn = fn;
init_waitqueue_head(&work->done);
work->flushing = 0;
work->queue_seq = work->done_seq = 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,129 | static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
unsigned seq)
{
int left;
spin_lock_irq(&dev->work_lock);
left = seq - work->done_seq;
spin_unlock_irq(&dev->work_lock);
return left <= 0;
}
| null | 0 | static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
unsigned seq)
{
int left;
spin_lock_irq(&dev->work_lock);
left = seq - work->done_seq;
spin_unlock_irq(&dev->work_lock);
return left <= 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,130 | static int vhost_worker(void *data)
{
struct vhost_dev *dev = data;
struct vhost_work *work = NULL;
unsigned uninitialized_var(seq);
mm_segment_t oldfs = get_fs();
set_fs(USER_DS);
use_mm(dev->mm);
for (;;) {
/* mb paired w/ kthread_stop */
set_current_state(TASK_INTERRUPTIBLE);
spin_lock_irq(&dev->work_lock);
if (work) {
work->done_seq = seq;
if (work->flushing)
wake_up_all(&work->done);
}
if (kthread_should_stop()) {
spin_unlock_irq(&dev->work_lock);
__set_current_state(TASK_RUNNING);
break;
}
if (!list_empty(&dev->work_list)) {
work = list_first_entry(&dev->work_list,
struct vhost_work, node);
list_del_init(&work->node);
seq = work->queue_seq;
} else
work = NULL;
spin_unlock_irq(&dev->work_lock);
if (work) {
__set_current_state(TASK_RUNNING);
work->fn(work);
if (need_resched())
schedule();
} else
schedule();
}
unuse_mm(dev->mm);
set_fs(oldfs);
return 0;
}
| null | 0 | static int vhost_worker(void *data)
{
struct vhost_dev *dev = data;
struct vhost_work *work = NULL;
unsigned uninitialized_var(seq);
mm_segment_t oldfs = get_fs();
set_fs(USER_DS);
use_mm(dev->mm);
for (;;) {
/* mb paired w/ kthread_stop */
set_current_state(TASK_INTERRUPTIBLE);
spin_lock_irq(&dev->work_lock);
if (work) {
work->done_seq = seq;
if (work->flushing)
wake_up_all(&work->done);
}
if (kthread_should_stop()) {
spin_unlock_irq(&dev->work_lock);
__set_current_state(TASK_RUNNING);
break;
}
if (!list_empty(&dev->work_list)) {
work = list_first_entry(&dev->work_list,
struct vhost_work, node);
list_del_init(&work->node);
seq = work->queue_seq;
} else
work = NULL;
spin_unlock_irq(&dev->work_lock);
if (work) {
__set_current_state(TASK_RUNNING);
work->fn(work);
if (need_resched())
schedule();
} else
schedule();
}
unuse_mm(dev->mm);
set_fs(oldfs);
return 0;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,131 | void vhost_zerocopy_callback(struct ubuf_info *ubuf)
{
struct vhost_ubuf_ref *ubufs = ubuf->ctx;
struct vhost_virtqueue *vq = ubufs->vq;
vhost_poll_queue(&vq->poll);
/* set len = 1 to mark this desc buffers done DMA */
vq->heads[ubuf->desc].len = VHOST_DMA_DONE_LEN;
kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
}
| null | 0 | void vhost_zerocopy_callback(struct ubuf_info *ubuf)
{
struct vhost_ubuf_ref *ubufs = ubuf->ctx;
struct vhost_virtqueue *vq = ubufs->vq;
vhost_poll_queue(&vq->poll);
/* set len = 1 to mark this desc buffers done DMA */
vq->heads[ubuf->desc].len = VHOST_DMA_DONE_LEN;
kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,132 | static void vhost_zerocopy_done_signal(struct kref *kref)
{
struct vhost_ubuf_ref *ubufs = container_of(kref, struct vhost_ubuf_ref,
kref);
wake_up(&ubufs->wait);
}
| null | 0 | static void vhost_zerocopy_done_signal(struct kref *kref)
{
struct vhost_ubuf_ref *ubufs = container_of(kref, struct vhost_ubuf_ref,
kref);
wake_up(&ubufs->wait);
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,133 | int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq)
{
int i;
int j = 0;
for (i = vq->done_idx; i != vq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
if ((vq->heads[i].len == VHOST_DMA_DONE_LEN)) {
vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
vhost_add_used_and_signal(vq->dev, vq,
vq->heads[i].id, 0);
++j;
} else
break;
}
if (j)
vq->done_idx = i;
return j;
}
| null | 0 | int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq)
{
int i;
int j = 0;
for (i = vq->done_idx; i != vq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
if ((vq->heads[i].len == VHOST_DMA_DONE_LEN)) {
vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
vhost_add_used_and_signal(vq->dev, vq,
vq->heads[i].id, 0);
++j;
} else
break;
}
if (j)
vq->done_idx = i;
return j;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,134 | static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
void __user *log_base)
{
struct vhost_memory *mp;
size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
mp = rcu_dereference_protected(vq->dev->memory,
lockdep_is_held(&vq->mutex));
return vq_memory_access_ok(log_base, mp,
vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
(!vq->log_used || log_access_ok(log_base, vq->log_addr,
sizeof *vq->used +
vq->num * sizeof *vq->used->ring + s));
}
| null | 0 | static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
void __user *log_base)
{
struct vhost_memory *mp;
size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
mp = rcu_dereference_protected(vq->dev->memory,
lockdep_is_held(&vq->mutex));
return vq_memory_access_ok(log_base, mp,
vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
(!vq->log_used || log_access_ok(log_base, vq->log_addr,
sizeof *vq->used +
vq->num * sizeof *vq->used->ring + s));
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,135 | static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
int log_all)
{
int i;
if (!mem)
return 0;
for (i = 0; i < mem->nregions; ++i) {
struct vhost_memory_region *m = mem->regions + i;
unsigned long a = m->userspace_addr;
if (m->memory_size > ULONG_MAX)
return 0;
else if (!access_ok(VERIFY_WRITE, (void __user *)a,
m->memory_size))
return 0;
else if (log_all && !log_access_ok(log_base,
m->guest_phys_addr,
m->memory_size))
return 0;
}
return 1;
}
| null | 0 | static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
int log_all)
{
int i;
if (!mem)
return 0;
for (i = 0; i < mem->nregions; ++i) {
struct vhost_memory_region *m = mem->regions + i;
unsigned long a = m->userspace_addr;
if (m->memory_size > ULONG_MAX)
return 0;
else if (!access_ok(VERIFY_WRITE, (void __user *)a,
m->memory_size))
return 0;
else if (log_all && !log_access_ok(log_base,
m->guest_phys_addr,
m->memory_size))
return 0;
}
return 1;
}
| @@ -1076,7 +1076,7 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
}
_iov = iov + ret;
size = reg->memory_size - addr + reg->guest_phys_addr;
- _iov->iov_len = min((u64)len, size);
+ _iov->iov_len = min((u64)len - s, size);
_iov->iov_base = (void __user *)(unsigned long)
(reg->userspace_addr + addr - reg->guest_phys_addr);
s += size; | null | null | null |
27,136 | int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
struct netlbl_audit *audit_info)
{
int ret_val = -EINVAL;
u32 iter;
u32 doi;
u32 doi_type;
struct audit_buffer *audit_buf;
doi = doi_def->doi;
doi_type = doi_def->type;
if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
goto doi_add_return;
for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
switch (doi_def->tags[iter]) {
case CIPSO_V4_TAG_RBITMAP:
break;
case CIPSO_V4_TAG_RANGE:
case CIPSO_V4_TAG_ENUM:
if (doi_def->type != CIPSO_V4_MAP_PASS)
goto doi_add_return;
break;
case CIPSO_V4_TAG_LOCAL:
if (doi_def->type != CIPSO_V4_MAP_LOCAL)
goto doi_add_return;
break;
case CIPSO_V4_TAG_INVALID:
if (iter == 0)
goto doi_add_return;
break;
default:
goto doi_add_return;
}
}
atomic_set(&doi_def->refcount, 1);
spin_lock(&cipso_v4_doi_list_lock);
if (cipso_v4_doi_search(doi_def->doi) != NULL) {
spin_unlock(&cipso_v4_doi_list_lock);
ret_val = -EEXIST;
goto doi_add_return;
}
list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
spin_unlock(&cipso_v4_doi_list_lock);
ret_val = 0;
doi_add_return:
audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
if (audit_buf != NULL) {
const char *type_str;
switch (doi_type) {
case CIPSO_V4_MAP_TRANS:
type_str = "trans";
break;
case CIPSO_V4_MAP_PASS:
type_str = "pass";
break;
case CIPSO_V4_MAP_LOCAL:
type_str = "local";
break;
default:
type_str = "(unknown)";
}
audit_log_format(audit_buf,
" cipso_doi=%u cipso_type=%s res=%u",
doi, type_str, ret_val == 0 ? 1 : 0);
audit_log_end(audit_buf);
}
return ret_val;
}
| DoS Overflow | 0 | int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
struct netlbl_audit *audit_info)
{
int ret_val = -EINVAL;
u32 iter;
u32 doi;
u32 doi_type;
struct audit_buffer *audit_buf;
doi = doi_def->doi;
doi_type = doi_def->type;
if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
goto doi_add_return;
for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
switch (doi_def->tags[iter]) {
case CIPSO_V4_TAG_RBITMAP:
break;
case CIPSO_V4_TAG_RANGE:
case CIPSO_V4_TAG_ENUM:
if (doi_def->type != CIPSO_V4_MAP_PASS)
goto doi_add_return;
break;
case CIPSO_V4_TAG_LOCAL:
if (doi_def->type != CIPSO_V4_MAP_LOCAL)
goto doi_add_return;
break;
case CIPSO_V4_TAG_INVALID:
if (iter == 0)
goto doi_add_return;
break;
default:
goto doi_add_return;
}
}
atomic_set(&doi_def->refcount, 1);
spin_lock(&cipso_v4_doi_list_lock);
if (cipso_v4_doi_search(doi_def->doi) != NULL) {
spin_unlock(&cipso_v4_doi_list_lock);
ret_val = -EEXIST;
goto doi_add_return;
}
list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
spin_unlock(&cipso_v4_doi_list_lock);
ret_val = 0;
doi_add_return:
audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
if (audit_buf != NULL) {
const char *type_str;
switch (doi_type) {
case CIPSO_V4_MAP_TRANS:
type_str = "trans";
break;
case CIPSO_V4_MAP_PASS:
type_str = "pass";
break;
case CIPSO_V4_MAP_LOCAL:
type_str = "local";
break;
default:
type_str = "(unknown)";
}
audit_log_format(audit_buf,
" cipso_doi=%u cipso_type=%s res=%u",
doi, type_str, ret_val == 0 ? 1 : 0);
audit_log_end(audit_buf);
}
return ret_val;
}
| @@ -1725,8 +1725,10 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
case CIPSO_V4_TAG_LOCAL:
/* This is a non-standard tag that we only allow for
* local connections, so if the incoming interface is
- * not the loopback device drop the packet. */
- if (!(skb->dev->flags & IFF_LOOPBACK)) {
+ * not the loopback device drop the packet. Further,
+ * there is no legitimate reason for setting this from
+ * userspace so reject it if skb is NULL. */
+ if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) {
err_offset = opt_iter;
goto validate_return_locked;
} | CWE-119 | null | null |
27,137 | void cipso_v4_req_delattr(struct request_sock *req)
{
struct ip_options_rcu *opt;
struct inet_request_sock *req_inet;
req_inet = inet_rsk(req);
opt = req_inet->opt;
if (opt == NULL || opt->opt.cipso == 0)
return;
cipso_v4_delopt(&req_inet->opt);
}
| DoS Overflow | 0 | void cipso_v4_req_delattr(struct request_sock *req)
{
struct ip_options_rcu *opt;
struct inet_request_sock *req_inet;
req_inet = inet_rsk(req);
opt = req_inet->opt;
if (opt == NULL || opt->opt.cipso == 0)
return;
cipso_v4_delopt(&req_inet->opt);
}
| @@ -1725,8 +1725,10 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
case CIPSO_V4_TAG_LOCAL:
/* This is a non-standard tag that we only allow for
* local connections, so if the incoming interface is
- * not the loopback device drop the packet. */
- if (!(skb->dev->flags & IFF_LOOPBACK)) {
+ * not the loopback device drop the packet. Further,
+ * there is no legitimate reason for setting this from
+ * userspace so reject it if skb is NULL. */
+ if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) {
err_offset = opt_iter;
goto validate_return_locked;
} | CWE-119 | null | null |
27,138 | void cipso_v4_sock_delattr(struct sock *sk)
{
int hdr_delta;
struct ip_options_rcu *opt;
struct inet_sock *sk_inet;
sk_inet = inet_sk(sk);
opt = rcu_dereference_protected(sk_inet->inet_opt, 1);
if (opt == NULL || opt->opt.cipso == 0)
return;
hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
if (sk_inet->is_icsk && hdr_delta > 0) {
struct inet_connection_sock *sk_conn = inet_csk(sk);
sk_conn->icsk_ext_hdr_len -= hdr_delta;
sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
}
}
| DoS Overflow | 0 | void cipso_v4_sock_delattr(struct sock *sk)
{
int hdr_delta;
struct ip_options_rcu *opt;
struct inet_sock *sk_inet;
sk_inet = inet_sk(sk);
opt = rcu_dereference_protected(sk_inet->inet_opt, 1);
if (opt == NULL || opt->opt.cipso == 0)
return;
hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
if (sk_inet->is_icsk && hdr_delta > 0) {
struct inet_connection_sock *sk_conn = inet_csk(sk);
sk_conn->icsk_ext_hdr_len -= hdr_delta;
sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
}
}
| @@ -1725,8 +1725,10 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
case CIPSO_V4_TAG_LOCAL:
/* This is a non-standard tag that we only allow for
* local connections, so if the incoming interface is
- * not the loopback device drop the packet. */
- if (!(skb->dev->flags & IFF_LOOPBACK)) {
+ * not the loopback device drop the packet. Further,
+ * there is no legitimate reason for setting this from
+ * userspace so reject it if skb is NULL. */
+ if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) {
err_offset = opt_iter;
goto validate_return_locked;
} | CWE-119 | null | null |
27,139 | int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
{
struct ip_options_rcu *opt;
int res = -ENOMSG;
rcu_read_lock();
opt = rcu_dereference(inet_sk(sk)->inet_opt);
if (opt && opt->opt.cipso)
res = cipso_v4_getattr(opt->opt.__data +
opt->opt.cipso -
sizeof(struct iphdr),
secattr);
rcu_read_unlock();
return res;
}
| DoS Overflow | 0 | int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
{
struct ip_options_rcu *opt;
int res = -ENOMSG;
rcu_read_lock();
opt = rcu_dereference(inet_sk(sk)->inet_opt);
if (opt && opt->opt.cipso)
res = cipso_v4_getattr(opt->opt.__data +
opt->opt.cipso -
sizeof(struct iphdr),
secattr);
rcu_read_unlock();
return res;
}
| @@ -1725,8 +1725,10 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
case CIPSO_V4_TAG_LOCAL:
/* This is a non-standard tag that we only allow for
* local connections, so if the incoming interface is
- * not the loopback device drop the packet. */
- if (!(skb->dev->flags & IFF_LOOPBACK)) {
+ * not the loopback device drop the packet. Further,
+ * there is no legitimate reason for setting this from
+ * userspace so reject it if skb is NULL. */
+ if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) {
err_offset = opt_iter;
goto validate_return_locked;
} | CWE-119 | null | null |
27,140 | int cipso_v4_sock_setattr(struct sock *sk,
const struct cipso_v4_doi *doi_def,
const struct netlbl_lsm_secattr *secattr)
{
int ret_val = -EPERM;
unsigned char *buf = NULL;
u32 buf_len;
u32 opt_len;
struct ip_options_rcu *old, *opt = NULL;
struct inet_sock *sk_inet;
struct inet_connection_sock *sk_conn;
/* In the case of sock_create_lite(), the sock->sk field is not
* defined yet but it is not a problem as the only users of these
* "lite" PF_INET sockets are functions which do an accept() call
* afterwards so we will label the socket as part of the accept(). */
if (sk == NULL)
return 0;
/* We allocate the maximum CIPSO option size here so we are probably
* being a little wasteful, but it makes our life _much_ easier later
* on and after all we are only talking about 40 bytes. */
buf_len = CIPSO_V4_OPT_LEN_MAX;
buf = kmalloc(buf_len, GFP_ATOMIC);
if (buf == NULL) {
ret_val = -ENOMEM;
goto socket_setattr_failure;
}
ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
if (ret_val < 0)
goto socket_setattr_failure;
buf_len = ret_val;
/* We can't use ip_options_get() directly because it makes a call to
* ip_options_get_alloc() which allocates memory with GFP_KERNEL and
* we won't always have CAP_NET_RAW even though we _always_ want to
* set the IPOPT_CIPSO option. */
opt_len = (buf_len + 3) & ~3;
opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
if (opt == NULL) {
ret_val = -ENOMEM;
goto socket_setattr_failure;
}
memcpy(opt->opt.__data, buf, buf_len);
opt->opt.optlen = opt_len;
opt->opt.cipso = sizeof(struct iphdr);
kfree(buf);
buf = NULL;
sk_inet = inet_sk(sk);
old = rcu_dereference_protected(sk_inet->inet_opt, sock_owned_by_user(sk));
if (sk_inet->is_icsk) {
sk_conn = inet_csk(sk);
if (old)
sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
}
rcu_assign_pointer(sk_inet->inet_opt, opt);
if (old)
kfree_rcu(old, rcu);
return 0;
socket_setattr_failure:
kfree(buf);
kfree(opt);
return ret_val;
}
| DoS Overflow | 0 | int cipso_v4_sock_setattr(struct sock *sk,
const struct cipso_v4_doi *doi_def,
const struct netlbl_lsm_secattr *secattr)
{
int ret_val = -EPERM;
unsigned char *buf = NULL;
u32 buf_len;
u32 opt_len;
struct ip_options_rcu *old, *opt = NULL;
struct inet_sock *sk_inet;
struct inet_connection_sock *sk_conn;
/* In the case of sock_create_lite(), the sock->sk field is not
* defined yet but it is not a problem as the only users of these
* "lite" PF_INET sockets are functions which do an accept() call
* afterwards so we will label the socket as part of the accept(). */
if (sk == NULL)
return 0;
/* We allocate the maximum CIPSO option size here so we are probably
* being a little wasteful, but it makes our life _much_ easier later
* on and after all we are only talking about 40 bytes. */
buf_len = CIPSO_V4_OPT_LEN_MAX;
buf = kmalloc(buf_len, GFP_ATOMIC);
if (buf == NULL) {
ret_val = -ENOMEM;
goto socket_setattr_failure;
}
ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
if (ret_val < 0)
goto socket_setattr_failure;
buf_len = ret_val;
/* We can't use ip_options_get() directly because it makes a call to
* ip_options_get_alloc() which allocates memory with GFP_KERNEL and
* we won't always have CAP_NET_RAW even though we _always_ want to
* set the IPOPT_CIPSO option. */
opt_len = (buf_len + 3) & ~3;
opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
if (opt == NULL) {
ret_val = -ENOMEM;
goto socket_setattr_failure;
}
memcpy(opt->opt.__data, buf, buf_len);
opt->opt.optlen = opt_len;
opt->opt.cipso = sizeof(struct iphdr);
kfree(buf);
buf = NULL;
sk_inet = inet_sk(sk);
old = rcu_dereference_protected(sk_inet->inet_opt, sock_owned_by_user(sk));
if (sk_inet->is_icsk) {
sk_conn = inet_csk(sk);
if (old)
sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
}
rcu_assign_pointer(sk_inet->inet_opt, opt);
if (old)
kfree_rcu(old, rcu);
return 0;
socket_setattr_failure:
kfree(buf);
kfree(opt);
return ret_val;
}
| @@ -1725,8 +1725,10 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
case CIPSO_V4_TAG_LOCAL:
/* This is a non-standard tag that we only allow for
* local connections, so if the incoming interface is
- * not the loopback device drop the packet. */
- if (!(skb->dev->flags & IFF_LOOPBACK)) {
+ * not the loopback device drop the packet. Further,
+ * there is no legitimate reason for setting this from
+ * userspace so reject it if skb is NULL. */
+ if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) {
err_offset = opt_iter;
goto validate_return_locked;
} | CWE-119 | null | null |
27,141 | __sum16 __skb_checksum_complete(struct sk_buff *skb)
{
return __skb_checksum_complete_head(skb, skb->len);
}
| DoS | 0 | __sum16 __skb_checksum_complete(struct sk_buff *skb)
{
return __skb_checksum_complete_head(skb, skb->len);
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,142 | unsigned int datagram_poll(struct file *file, struct socket *sock,
poll_table *wait)
{
struct sock *sk = sock->sk;
unsigned int mask;
sock_poll_wait(file, sk_sleep(sk), wait);
mask = 0;
/* exceptional events? */
if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
mask |= POLLERR;
if (sk->sk_shutdown & RCV_SHUTDOWN)
mask |= POLLRDHUP | POLLIN | POLLRDNORM;
if (sk->sk_shutdown == SHUTDOWN_MASK)
mask |= POLLHUP;
/* readable? */
if (!skb_queue_empty(&sk->sk_receive_queue))
mask |= POLLIN | POLLRDNORM;
/* Connection-based need to check for termination and startup */
if (connection_based(sk)) {
if (sk->sk_state == TCP_CLOSE)
mask |= POLLHUP;
/* connection hasn't started yet? */
if (sk->sk_state == TCP_SYN_SENT)
return mask;
}
/* writable? */
if (sock_writeable(sk))
mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
else
set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
return mask;
}
| DoS | 0 | unsigned int datagram_poll(struct file *file, struct socket *sock,
poll_table *wait)
{
struct sock *sk = sock->sk;
unsigned int mask;
sock_poll_wait(file, sk_sleep(sk), wait);
mask = 0;
/* exceptional events? */
if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
mask |= POLLERR;
if (sk->sk_shutdown & RCV_SHUTDOWN)
mask |= POLLRDHUP | POLLIN | POLLRDNORM;
if (sk->sk_shutdown == SHUTDOWN_MASK)
mask |= POLLHUP;
/* readable? */
if (!skb_queue_empty(&sk->sk_receive_queue))
mask |= POLLIN | POLLRDNORM;
/* Connection-based need to check for termination and startup */
if (connection_based(sk)) {
if (sk->sk_state == TCP_CLOSE)
mask |= POLLHUP;
/* connection hasn't started yet? */
if (sk->sk_state == TCP_SYN_SENT)
return mask;
}
/* writable? */
if (sock_writeable(sk))
mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
else
set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
return mask;
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,143 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
u8 __user *to, int len,
__wsum *csump)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
int pos = 0;
/* Copy header. */
if (copy > 0) {
int err = 0;
if (copy > len)
copy = len;
*csump = csum_and_copy_to_user(skb->data + offset, to, copy,
*csump, &err);
if (err)
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
to += copy;
pos = copy;
}
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
__wsum csum2;
int err = 0;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
csum2 = csum_and_copy_to_user(vaddr +
frag->page_offset +
offset - start,
to, copy, 0, &err);
kunmap(page);
if (err)
goto fault;
*csump = csum_block_add(*csump, csum2, pos);
if (!(len -= copy))
return 0;
offset += copy;
to += copy;
pos += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
__wsum csum2 = 0;
if (copy > len)
copy = len;
if (skb_copy_and_csum_datagram(frag_iter,
offset - start,
to, copy,
&csum2))
goto fault;
*csump = csum_block_add(*csump, csum2, pos);
if ((len -= copy) == 0)
return 0;
offset += copy;
to += copy;
pos += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| DoS | 0 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
u8 __user *to, int len,
__wsum *csump)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
int pos = 0;
/* Copy header. */
if (copy > 0) {
int err = 0;
if (copy > len)
copy = len;
*csump = csum_and_copy_to_user(skb->data + offset, to, copy,
*csump, &err);
if (err)
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
to += copy;
pos = copy;
}
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
__wsum csum2;
int err = 0;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
csum2 = csum_and_copy_to_user(vaddr +
frag->page_offset +
offset - start,
to, copy, 0, &err);
kunmap(page);
if (err)
goto fault;
*csump = csum_block_add(*csump, csum2, pos);
if (!(len -= copy))
return 0;
offset += copy;
to += copy;
pos += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
__wsum csum2 = 0;
if (copy > len)
copy = len;
if (skb_copy_and_csum_datagram(frag_iter,
offset - start,
to, copy,
&csum2))
goto fault;
*csump = csum_block_add(*csump, csum2, pos);
if ((len -= copy) == 0)
return 0;
offset += copy;
to += copy;
pos += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,144 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
int hlen, struct iovec *iov)
{
__wsum csum;
int chunk = skb->len - hlen;
if (!chunk)
return 0;
/* Skip filled elements.
* Pretty silly, look at memcpy_toiovec, though 8)
*/
while (!iov->iov_len)
iov++;
if (iov->iov_len < chunk) {
if (__skb_checksum_complete(skb))
goto csum_error;
if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
goto fault;
} else {
csum = csum_partial(skb->data, hlen, skb->csum);
if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
chunk, &csum))
goto fault;
if (csum_fold(csum))
goto csum_error;
if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
netdev_rx_csum_fault(skb->dev);
iov->iov_len -= chunk;
iov->iov_base += chunk;
}
return 0;
csum_error:
return -EINVAL;
fault:
return -EFAULT;
}
| DoS | 0 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
int hlen, struct iovec *iov)
{
__wsum csum;
int chunk = skb->len - hlen;
if (!chunk)
return 0;
/* Skip filled elements.
* Pretty silly, look at memcpy_toiovec, though 8)
*/
while (!iov->iov_len)
iov++;
if (iov->iov_len < chunk) {
if (__skb_checksum_complete(skb))
goto csum_error;
if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
goto fault;
} else {
csum = csum_partial(skb->data, hlen, skb->csum);
if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
chunk, &csum))
goto fault;
if (csum_fold(csum))
goto csum_error;
if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
netdev_rx_csum_fault(skb->dev);
iov->iov_len -= chunk;
iov->iov_base += chunk;
}
return 0;
csum_error:
return -EINVAL;
fault:
return -EFAULT;
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,145 | int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
const struct iovec *to, int to_offset,
int len)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
/* Copy header. */
if (copy > 0) {
if (copy > len)
copy = len;
if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
to_offset += copy;
}
/* Copy paged appendix. Hmm... why does this look so complicated? */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
err = memcpy_toiovecend(to, vaddr + frag->page_offset +
offset - start, to_offset, copy);
kunmap(page);
if (err)
goto fault;
if (!(len -= copy))
return 0;
offset += copy;
to_offset += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
if (skb_copy_datagram_const_iovec(frag_iter,
offset - start,
to, to_offset,
copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
to_offset += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| DoS | 0 | int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
const struct iovec *to, int to_offset,
int len)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
/* Copy header. */
if (copy > 0) {
if (copy > len)
copy = len;
if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
to_offset += copy;
}
/* Copy paged appendix. Hmm... why does this look so complicated? */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
err = memcpy_toiovecend(to, vaddr + frag->page_offset +
offset - start, to_offset, copy);
kunmap(page);
if (err)
goto fault;
if (!(len -= copy))
return 0;
offset += copy;
to_offset += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
if (skb_copy_datagram_const_iovec(frag_iter,
offset - start,
to, to_offset,
copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
to_offset += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,146 | int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
const struct iovec *from, int from_offset,
int len)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
/* Copy header. */
if (copy > 0) {
if (copy > len)
copy = len;
if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
from_offset += copy;
}
/* Copy paged appendix. Hmm... why does this look so complicated? */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
err = memcpy_fromiovecend(vaddr + frag->page_offset +
offset - start,
from, from_offset, copy);
kunmap(page);
if (err)
goto fault;
if (!(len -= copy))
return 0;
offset += copy;
from_offset += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
if (skb_copy_datagram_from_iovec(frag_iter,
offset - start,
from,
from_offset,
copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
from_offset += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| DoS | 0 | int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
const struct iovec *from, int from_offset,
int len)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
/* Copy header. */
if (copy > 0) {
if (copy > len)
copy = len;
if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
from_offset += copy;
}
/* Copy paged appendix. Hmm... why does this look so complicated? */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
err = memcpy_fromiovecend(vaddr + frag->page_offset +
offset - start,
from, from_offset, copy);
kunmap(page);
if (err)
goto fault;
if (!(len -= copy))
return 0;
offset += copy;
from_offset += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
if (skb_copy_datagram_from_iovec(frag_iter,
offset - start,
from,
from_offset,
copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
from_offset += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,147 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
struct iovec *to, int len)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
trace_skb_copy_datagram_iovec(skb, len);
/* Copy header. */
if (copy > 0) {
if (copy > len)
copy = len;
if (memcpy_toiovec(to, skb->data + offset, copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
}
/* Copy paged appendix. Hmm... why does this look so complicated? */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
err = memcpy_toiovec(to, vaddr + frag->page_offset +
offset - start, copy);
kunmap(page);
if (err)
goto fault;
if (!(len -= copy))
return 0;
offset += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
if (skb_copy_datagram_iovec(frag_iter,
offset - start,
to, copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| DoS | 0 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
struct iovec *to, int len)
{
int start = skb_headlen(skb);
int i, copy = start - offset;
struct sk_buff *frag_iter;
trace_skb_copy_datagram_iovec(skb, len);
/* Copy header. */
if (copy > 0) {
if (copy > len)
copy = len;
if (memcpy_toiovec(to, skb->data + offset, copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
}
/* Copy paged appendix. Hmm... why does this look so complicated? */
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
int end;
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
WARN_ON(start > offset + len);
end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len)
copy = len;
vaddr = kmap(page);
err = memcpy_toiovec(to, vaddr + frag->page_offset +
offset - start, copy);
kunmap(page);
if (err)
goto fault;
if (!(len -= copy))
return 0;
offset += copy;
}
start = end;
}
skb_walk_frags(skb, frag_iter) {
int end;
WARN_ON(start > offset + len);
end = start + frag_iter->len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
if (skb_copy_datagram_iovec(frag_iter,
offset - start,
to, copy))
goto fault;
if ((len -= copy) == 0)
return 0;
offset += copy;
}
start = end;
}
if (!len)
return 0;
fault:
return -EFAULT;
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,148 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
{
consume_skb(skb);
sk_mem_reclaim_partial(sk);
}
| DoS | 0 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
{
consume_skb(skb);
sk_mem_reclaim_partial(sk);
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,149 | void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
{
bool slow;
if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users)))
return;
slow = lock_sock_fast(sk);
skb_orphan(skb);
sk_mem_reclaim_partial(sk);
unlock_sock_fast(sk, slow);
/* skb is now orphaned, can be freed outside of locked section */
__kfree_skb(skb);
}
| DoS | 0 | void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
{
bool slow;
if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users)))
return;
slow = lock_sock_fast(sk);
skb_orphan(skb);
sk_mem_reclaim_partial(sk);
unlock_sock_fast(sk, slow);
/* skb is now orphaned, can be freed outside of locked section */
__kfree_skb(skb);
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,150 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
{
int err = 0;
if (flags & MSG_PEEK) {
err = -ENOENT;
spin_lock_bh(&sk->sk_receive_queue.lock);
if (skb == skb_peek(&sk->sk_receive_queue)) {
__skb_unlink(skb, &sk->sk_receive_queue);
atomic_dec(&skb->users);
err = 0;
}
spin_unlock_bh(&sk->sk_receive_queue.lock);
}
kfree_skb(skb);
atomic_inc(&sk->sk_drops);
sk_mem_reclaim_partial(sk);
return err;
}
| DoS | 0 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
{
int err = 0;
if (flags & MSG_PEEK) {
err = -ENOENT;
spin_lock_bh(&sk->sk_receive_queue.lock);
if (skb == skb_peek(&sk->sk_receive_queue)) {
__skb_unlink(skb, &sk->sk_receive_queue);
atomic_dec(&skb->users);
err = 0;
}
spin_unlock_bh(&sk->sk_receive_queue.lock);
}
kfree_skb(skb);
atomic_inc(&sk->sk_drops);
sk_mem_reclaim_partial(sk);
return err;
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,151 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
int noblock, int *err)
{
int peeked, off = 0;
return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
&peeked, &off, err);
}
| DoS | 0 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
int noblock, int *err)
{
int peeked, off = 0;
return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
&peeked, &off, err);
}
| @@ -187,7 +187,7 @@ struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
skb_queue_walk(queue, skb) {
*peeked = skb->peeked;
if (flags & MSG_PEEK) {
- if (*off >= skb->len) {
+ if (*off >= skb->len && skb->len) {
*off -= skb->len;
continue;
} | CWE-20 | null | null |
27,152 | can_write(int flags)
{
return TRUE;
}
| DoS | 0 | can_write(int flags)
{
return TRUE;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,153 | check_local_notify(int bcast_id)
{
cib_local_notify_t *notify = NULL;
if (!local_notify_queue) {
return;
}
notify = g_hash_table_lookup(local_notify_queue, GINT_TO_POINTER(bcast_id));
if (notify) {
do_local_notify(notify->notify_src, notify->client_id, notify->sync_reply, notify->from_peer);
g_hash_table_remove(local_notify_queue, GINT_TO_POINTER(bcast_id));
}
}
| DoS | 0 | check_local_notify(int bcast_id)
{
cib_local_notify_t *notify = NULL;
if (!local_notify_queue) {
return;
}
notify = g_hash_table_lookup(local_notify_queue, GINT_TO_POINTER(bcast_id));
if (notify) {
do_local_notify(notify->notify_src, notify->client_id, notify->sync_reply, notify->from_peer);
g_hash_table_remove(local_notify_queue, GINT_TO_POINTER(bcast_id));
}
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,154 | cib_ccm_dispatch(gpointer user_data)
{
int rc = 0;
oc_ev_t *ccm_token = (oc_ev_t *) user_data;
crm_trace("received callback");
if (ccm_api_handle_event == NULL) {
ccm_api_handle_event =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_handle_event", 1);
}
rc = (*ccm_api_handle_event) (ccm_token);
if (0 == rc) {
return 0;
}
crm_err("CCM connection appears to have failed: rc=%d.", rc);
/* eventually it might be nice to recover and reconnect... but until then... */
crm_err("Exiting to recover from CCM connection failure");
crm_exit(2);
return -1;
}
| DoS | 0 | cib_ccm_dispatch(gpointer user_data)
{
int rc = 0;
oc_ev_t *ccm_token = (oc_ev_t *) user_data;
crm_trace("received callback");
if (ccm_api_handle_event == NULL) {
ccm_api_handle_event =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_handle_event", 1);
}
rc = (*ccm_api_handle_event) (ccm_token);
if (0 == rc) {
return 0;
}
crm_err("CCM connection appears to have failed: rc=%d.", rc);
/* eventually it might be nice to recover and reconnect... but until then... */
crm_err("Exiting to recover from CCM connection failure");
crm_exit(2);
return -1;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,155 | cib_ccm_msg_callback(oc_ed_t event, void *cookie, size_t size, const void *data)
{
gboolean update_id = FALSE;
const oc_ev_membership_t *membership = data;
CRM_ASSERT(membership != NULL);
crm_info("Processing CCM event=%s (id=%d)", ccm_event_name(event), membership->m_instance);
if (current_instance > membership->m_instance) {
crm_err("Membership instance ID went backwards! %d->%d",
current_instance, membership->m_instance);
CRM_ASSERT(current_instance <= membership->m_instance);
}
switch (event) {
case OC_EV_MS_NEW_MEMBERSHIP:
case OC_EV_MS_INVALID:
update_id = TRUE;
break;
case OC_EV_MS_PRIMARY_RESTORED:
update_id = TRUE;
break;
case OC_EV_MS_NOT_PRIMARY:
crm_trace("Ignoring transitional CCM event: %s", ccm_event_name(event));
break;
case OC_EV_MS_EVICTED:
crm_err("Evicted from CCM: %s", ccm_event_name(event));
break;
default:
crm_err("Unknown CCM event: %d", event);
}
if (update_id) {
unsigned int lpc = 0;
CRM_CHECK(membership != NULL, return);
current_instance = membership->m_instance;
for (lpc = 0; lpc < membership->m_n_out; lpc++) {
crm_update_ccm_node(membership, lpc + membership->m_out_idx, CRM_NODE_LOST,
current_instance);
}
for (lpc = 0; lpc < membership->m_n_member; lpc++) {
crm_update_ccm_node(membership, lpc + membership->m_memb_idx, CRM_NODE_ACTIVE,
current_instance);
}
}
if (ccm_api_callback_done == NULL) {
ccm_api_callback_done =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_callback_done", 1);
}
(*ccm_api_callback_done) (cookie);
return;
}
| DoS | 0 | cib_ccm_msg_callback(oc_ed_t event, void *cookie, size_t size, const void *data)
{
gboolean update_id = FALSE;
const oc_ev_membership_t *membership = data;
CRM_ASSERT(membership != NULL);
crm_info("Processing CCM event=%s (id=%d)", ccm_event_name(event), membership->m_instance);
if (current_instance > membership->m_instance) {
crm_err("Membership instance ID went backwards! %d->%d",
current_instance, membership->m_instance);
CRM_ASSERT(current_instance <= membership->m_instance);
}
switch (event) {
case OC_EV_MS_NEW_MEMBERSHIP:
case OC_EV_MS_INVALID:
update_id = TRUE;
break;
case OC_EV_MS_PRIMARY_RESTORED:
update_id = TRUE;
break;
case OC_EV_MS_NOT_PRIMARY:
crm_trace("Ignoring transitional CCM event: %s", ccm_event_name(event));
break;
case OC_EV_MS_EVICTED:
crm_err("Evicted from CCM: %s", ccm_event_name(event));
break;
default:
crm_err("Unknown CCM event: %d", event);
}
if (update_id) {
unsigned int lpc = 0;
CRM_CHECK(membership != NULL, return);
current_instance = membership->m_instance;
for (lpc = 0; lpc < membership->m_n_out; lpc++) {
crm_update_ccm_node(membership, lpc + membership->m_out_idx, CRM_NODE_LOST,
current_instance);
}
for (lpc = 0; lpc < membership->m_n_member; lpc++) {
crm_update_ccm_node(membership, lpc + membership->m_memb_idx, CRM_NODE_ACTIVE,
current_instance);
}
}
if (ccm_api_callback_done == NULL) {
ccm_api_callback_done =
find_library_function(&ccm_library, CCM_LIBRARY, "oc_ev_callback_done", 1);
}
(*ccm_api_callback_done) (cookie);
return;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,156 | cib_common_callback(qb_ipcs_connection_t *c, void *data, size_t size, gboolean privileged)
{
uint32_t id = 0;
uint32_t flags = 0;
int call_options = 0;
xmlNode *op_request = crm_ipcs_recv(c, data, size, &id, &flags);
cib_client_t *cib_client = qb_ipcs_context_get(c);
if(op_request) {
crm_element_value_int(op_request, F_CIB_CALLOPTS, &call_options);
}
crm_trace("Inbound: %.200s", data);
if (op_request == NULL || cib_client == NULL) {
crm_ipcs_send_ack(c, id, "nack", __FUNCTION__, __LINE__);
return 0;
}
if(is_set(call_options, cib_sync_call)) {
CRM_ASSERT(flags & crm_ipc_client_response);
}
if(flags & crm_ipc_client_response) {
CRM_LOG_ASSERT(cib_client->request_id == 0); /* This means the client has two synchronous events in-flight */
cib_client->request_id = id; /* Reply only to the last one */
}
if (cib_client->name == NULL) {
const char *value = crm_element_value(op_request, F_CIB_CLIENTNAME);
if (value == NULL) {
cib_client->name = crm_itoa(crm_ipcs_client_pid(c));
} else {
cib_client->name = strdup(value);
}
}
if (cib_client->callback_id == NULL) {
const char *value = crm_element_value(op_request, F_CIB_CALLBACK_TOKEN);
if (value != NULL) {
cib_client->callback_id = strdup(value);
} else {
cib_client->callback_id = strdup(cib_client->id);
}
}
crm_xml_add(op_request, F_CIB_CLIENTID, cib_client->id);
crm_xml_add(op_request, F_CIB_CLIENTNAME, cib_client->name);
#if ENABLE_ACL
determine_request_user(cib_client->user, op_request, F_CIB_USER);
#endif
crm_log_xml_trace(op_request, "Client[inbound]");
cib_common_callback_worker(id, flags, op_request, cib_client, privileged);
free_xml(op_request);
return 0;
}
| DoS | 0 | cib_common_callback(qb_ipcs_connection_t *c, void *data, size_t size, gboolean privileged)
{
uint32_t id = 0;
uint32_t flags = 0;
int call_options = 0;
xmlNode *op_request = crm_ipcs_recv(c, data, size, &id, &flags);
cib_client_t *cib_client = qb_ipcs_context_get(c);
if(op_request) {
crm_element_value_int(op_request, F_CIB_CALLOPTS, &call_options);
}
crm_trace("Inbound: %.200s", data);
if (op_request == NULL || cib_client == NULL) {
crm_ipcs_send_ack(c, id, "nack", __FUNCTION__, __LINE__);
return 0;
}
if(is_set(call_options, cib_sync_call)) {
CRM_ASSERT(flags & crm_ipc_client_response);
}
if(flags & crm_ipc_client_response) {
CRM_LOG_ASSERT(cib_client->request_id == 0); /* This means the client has two synchronous events in-flight */
cib_client->request_id = id; /* Reply only to the last one */
}
if (cib_client->name == NULL) {
const char *value = crm_element_value(op_request, F_CIB_CLIENTNAME);
if (value == NULL) {
cib_client->name = crm_itoa(crm_ipcs_client_pid(c));
} else {
cib_client->name = strdup(value);
}
}
if (cib_client->callback_id == NULL) {
const char *value = crm_element_value(op_request, F_CIB_CALLBACK_TOKEN);
if (value != NULL) {
cib_client->callback_id = strdup(value);
} else {
cib_client->callback_id = strdup(cib_client->id);
}
}
crm_xml_add(op_request, F_CIB_CLIENTID, cib_client->id);
crm_xml_add(op_request, F_CIB_CLIENTNAME, cib_client->name);
#if ENABLE_ACL
determine_request_user(cib_client->user, op_request, F_CIB_USER);
#endif
crm_log_xml_trace(op_request, "Client[inbound]");
cib_common_callback_worker(id, flags, op_request, cib_client, privileged);
free_xml(op_request);
return 0;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,157 | cib_common_callback_worker(uint32_t id, uint32_t flags, xmlNode * op_request, cib_client_t * cib_client, gboolean privileged)
{
const char *op = crm_element_value(op_request, F_CIB_OPERATION);
if (crm_str_eq(op, CRM_OP_REGISTER, TRUE)) {
if(flags & crm_ipc_client_response) {
xmlNode *ack = create_xml_node(NULL, __FUNCTION__);
crm_xml_add(ack, F_CIB_OPERATION, CRM_OP_REGISTER);
crm_xml_add(ack, F_CIB_CLIENTID, cib_client->id);
crm_ipcs_send(cib_client->ipc, id, ack, FALSE);
cib_client->request_id = 0;
free_xml(ack);
}
return;
} else if (crm_str_eq(op, T_CIB_NOTIFY, TRUE)) {
/* Update the notify filters for this client */
int on_off = 0;
const char *type = crm_element_value(op_request, F_CIB_NOTIFY_TYPE);
crm_element_value_int(op_request, F_CIB_NOTIFY_ACTIVATE, &on_off);
crm_debug("Setting %s callbacks for %s (%s): %s",
type, cib_client->name, cib_client->id, on_off ? "on" : "off");
if (safe_str_eq(type, T_CIB_POST_NOTIFY)) {
cib_client->post_notify = on_off;
} else if (safe_str_eq(type, T_CIB_PRE_NOTIFY)) {
cib_client->pre_notify = on_off;
} else if (safe_str_eq(type, T_CIB_UPDATE_CONFIRM)) {
cib_client->confirmations = on_off;
} else if (safe_str_eq(type, T_CIB_DIFF_NOTIFY)) {
cib_client->diffs = on_off;
} else if (safe_str_eq(type, T_CIB_REPLACE_NOTIFY)) {
cib_client->replace = on_off;
}
if(flags & crm_ipc_client_response) {
/* TODO - include rc */
crm_ipcs_send_ack(cib_client->ipc, id, "ack", __FUNCTION__, __LINE__);
cib_client->request_id = 0;
}
return;
}
cib_client->num_calls++;
cib_process_request(op_request, FALSE, privileged, FALSE, cib_client);
}
| DoS | 0 | cib_common_callback_worker(uint32_t id, uint32_t flags, xmlNode * op_request, cib_client_t * cib_client, gboolean privileged)
{
const char *op = crm_element_value(op_request, F_CIB_OPERATION);
if (crm_str_eq(op, CRM_OP_REGISTER, TRUE)) {
if(flags & crm_ipc_client_response) {
xmlNode *ack = create_xml_node(NULL, __FUNCTION__);
crm_xml_add(ack, F_CIB_OPERATION, CRM_OP_REGISTER);
crm_xml_add(ack, F_CIB_CLIENTID, cib_client->id);
crm_ipcs_send(cib_client->ipc, id, ack, FALSE);
cib_client->request_id = 0;
free_xml(ack);
}
return;
} else if (crm_str_eq(op, T_CIB_NOTIFY, TRUE)) {
/* Update the notify filters for this client */
int on_off = 0;
const char *type = crm_element_value(op_request, F_CIB_NOTIFY_TYPE);
crm_element_value_int(op_request, F_CIB_NOTIFY_ACTIVATE, &on_off);
crm_debug("Setting %s callbacks for %s (%s): %s",
type, cib_client->name, cib_client->id, on_off ? "on" : "off");
if (safe_str_eq(type, T_CIB_POST_NOTIFY)) {
cib_client->post_notify = on_off;
} else if (safe_str_eq(type, T_CIB_PRE_NOTIFY)) {
cib_client->pre_notify = on_off;
} else if (safe_str_eq(type, T_CIB_UPDATE_CONFIRM)) {
cib_client->confirmations = on_off;
} else if (safe_str_eq(type, T_CIB_DIFF_NOTIFY)) {
cib_client->diffs = on_off;
} else if (safe_str_eq(type, T_CIB_REPLACE_NOTIFY)) {
cib_client->replace = on_off;
}
if(flags & crm_ipc_client_response) {
/* TODO - include rc */
crm_ipcs_send_ack(cib_client->ipc, id, "ack", __FUNCTION__, __LINE__);
cib_client->request_id = 0;
}
return;
}
cib_client->num_calls++;
cib_process_request(op_request, FALSE, privileged, FALSE, cib_client);
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,158 | cib_construct_reply(xmlNode * request, xmlNode * output, int rc)
{
int lpc = 0;
xmlNode *reply = NULL;
const char *name = NULL;
const char *value = NULL;
const char *names[] = {
F_CIB_OPERATION,
F_CIB_CALLID,
F_CIB_CLIENTID,
F_CIB_CALLOPTS
};
static int max = DIMOF(names);
crm_trace("Creating a basic reply");
reply = create_xml_node(NULL, "cib-reply");
crm_xml_add(reply, F_TYPE, T_CIB);
for (lpc = 0; lpc < max; lpc++) {
name = names[lpc];
value = crm_element_value(request, name);
crm_xml_add(reply, name, value);
}
crm_xml_add_int(reply, F_CIB_RC, rc);
if (output != NULL) {
crm_trace("Attaching reply output");
add_message_xml(reply, F_CIB_CALLDATA, output);
}
return reply;
}
| DoS | 0 | cib_construct_reply(xmlNode * request, xmlNode * output, int rc)
{
int lpc = 0;
xmlNode *reply = NULL;
const char *name = NULL;
const char *value = NULL;
const char *names[] = {
F_CIB_OPERATION,
F_CIB_CALLID,
F_CIB_CLIENTID,
F_CIB_CALLOPTS
};
static int max = DIMOF(names);
crm_trace("Creating a basic reply");
reply = create_xml_node(NULL, "cib-reply");
crm_xml_add(reply, F_TYPE, T_CIB);
for (lpc = 0; lpc < max; lpc++) {
name = names[lpc];
value = crm_element_value(request, name);
crm_xml_add(reply, name, value);
}
crm_xml_add_int(reply, F_CIB_RC, rc);
if (output != NULL) {
crm_trace("Attaching reply output");
add_message_xml(reply, F_CIB_CALLDATA, output);
}
return reply;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,159 | cib_force_exit(gpointer data)
{
crm_notice("Forcing exit!");
terminate_cib(__FUNCTION__, TRUE);
return FALSE;
}
| DoS | 0 | cib_force_exit(gpointer data)
{
crm_notice("Forcing exit!");
terminate_cib(__FUNCTION__, TRUE);
return FALSE;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,160 | cib_ha_peer_callback(HA_Message * msg, void *private_data)
{
xmlNode *xml = convert_ha_message(NULL, msg, __FUNCTION__);
cib_peer_callback(xml, private_data);
free_xml(xml);
}
| DoS | 0 | cib_ha_peer_callback(HA_Message * msg, void *private_data)
{
xmlNode *xml = convert_ha_message(NULL, msg, __FUNCTION__);
cib_peer_callback(xml, private_data);
free_xml(xml);
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,161 | cib_ipc_accept(qb_ipcs_connection_t *c, uid_t uid, gid_t gid)
{
cib_client_t *new_client = NULL;
#if ENABLE_ACL
struct group *crm_grp = NULL;
#endif
crm_trace("Connecting %p for uid=%d gid=%d pid=%d", c, uid, gid, crm_ipcs_client_pid(c));
if (cib_shutdown_flag) {
crm_info("Ignoring new client [%d] during shutdown", crm_ipcs_client_pid(c));
return -EPERM;
}
new_client = calloc(1, sizeof(cib_client_t));
new_client->ipc = c;
CRM_CHECK(new_client->id == NULL, free(new_client->id));
new_client->id = crm_generate_uuid();
#if ENABLE_ACL
crm_grp = getgrnam(CRM_DAEMON_GROUP);
if (crm_grp) {
qb_ipcs_connection_auth_set(c, -1, crm_grp->gr_gid, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
}
new_client->user = uid2username(uid);
#endif
/* make sure we can find ourselves later for sync calls
* redirected to the master instance
*/
g_hash_table_insert(client_list, new_client->id, new_client);
qb_ipcs_context_set(c, new_client);
return 0;
}
| DoS | 0 | cib_ipc_accept(qb_ipcs_connection_t *c, uid_t uid, gid_t gid)
{
cib_client_t *new_client = NULL;
#if ENABLE_ACL
struct group *crm_grp = NULL;
#endif
crm_trace("Connecting %p for uid=%d gid=%d pid=%d", c, uid, gid, crm_ipcs_client_pid(c));
if (cib_shutdown_flag) {
crm_info("Ignoring new client [%d] during shutdown", crm_ipcs_client_pid(c));
return -EPERM;
}
new_client = calloc(1, sizeof(cib_client_t));
new_client->ipc = c;
CRM_CHECK(new_client->id == NULL, free(new_client->id));
new_client->id = crm_generate_uuid();
#if ENABLE_ACL
crm_grp = getgrnam(CRM_DAEMON_GROUP);
if (crm_grp) {
qb_ipcs_connection_auth_set(c, -1, crm_grp->gr_gid, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
}
new_client->user = uid2username(uid);
#endif
/* make sure we can find ourselves later for sync calls
* redirected to the master instance
*/
g_hash_table_insert(client_list, new_client->id, new_client);
qb_ipcs_context_set(c, new_client);
return 0;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,162 | cib_ipc_closed(qb_ipcs_connection_t *c)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("Connection %p closed", c);
CRM_ASSERT(cib_client != NULL);
CRM_ASSERT(cib_client->id != NULL);
if (!g_hash_table_remove(client_list, cib_client->id)) {
crm_err("Client %s not found in the hashtable", cib_client->name);
}
return 0;
}
| DoS | 0 | cib_ipc_closed(qb_ipcs_connection_t *c)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("Connection %p closed", c);
CRM_ASSERT(cib_client != NULL);
CRM_ASSERT(cib_client->id != NULL);
if (!g_hash_table_remove(client_list, cib_client->id)) {
crm_err("Client %s not found in the hashtable", cib_client->name);
}
return 0;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,163 | cib_ipc_created(qb_ipcs_connection_t *c)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("%p connected for client %s", c, cib_client->id);
}
| DoS | 0 | cib_ipc_created(qb_ipcs_connection_t *c)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("%p connected for client %s", c, cib_client->id);
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,164 | cib_ipc_dispatch_ro(qb_ipcs_connection_t *c, void *data, size_t size)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("%p message from %s", c, cib_client->id);
return cib_common_callback(c, data, size, FALSE);
}
| DoS | 0 | cib_ipc_dispatch_ro(qb_ipcs_connection_t *c, void *data, size_t size)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("%p message from %s", c, cib_client->id);
return cib_common_callback(c, data, size, FALSE);
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,165 | cib_ipc_dispatch_rw(qb_ipcs_connection_t *c, void *data, size_t size)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("%p message from %s", c, cib_client->id);
return cib_common_callback(c, data, size, TRUE);
}
| DoS | 0 | cib_ipc_dispatch_rw(qb_ipcs_connection_t *c, void *data, size_t size)
{
cib_client_t *cib_client = qb_ipcs_context_get(c);
crm_trace("%p message from %s", c, cib_client->id);
return cib_common_callback(c, data, size, TRUE);
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,166 | cib_peer_callback(xmlNode * msg, void *private_data)
{
const char *reason = NULL;
const char *originator = crm_element_value(msg, F_ORIG);
if (originator == NULL || crm_str_eq(originator, cib_our_uname, TRUE)) {
/* message is from ourselves */
int bcast_id = 0;
if (!(crm_element_value_int(msg, F_CIB_LOCAL_NOTIFY_ID, &bcast_id))) {
check_local_notify(bcast_id);
}
return;
} else if (crm_peer_cache == NULL) {
reason = "membership not established";
goto bail;
}
if (crm_element_value(msg, F_CIB_CLIENTNAME) == NULL) {
crm_xml_add(msg, F_CIB_CLIENTNAME, originator);
}
/* crm_log_xml_trace("Peer[inbound]", msg); */
cib_process_request(msg, FALSE, TRUE, TRUE, NULL);
return;
bail:
if (reason) {
const char *seq = crm_element_value(msg, F_SEQ);
const char *op = crm_element_value(msg, F_CIB_OPERATION);
crm_warn("Discarding %s message (%s) from %s: %s", op, seq, originator, reason);
}
}
| DoS | 0 | cib_peer_callback(xmlNode * msg, void *private_data)
{
const char *reason = NULL;
const char *originator = crm_element_value(msg, F_ORIG);
if (originator == NULL || crm_str_eq(originator, cib_our_uname, TRUE)) {
/* message is from ourselves */
int bcast_id = 0;
if (!(crm_element_value_int(msg, F_CIB_LOCAL_NOTIFY_ID, &bcast_id))) {
check_local_notify(bcast_id);
}
return;
} else if (crm_peer_cache == NULL) {
reason = "membership not established";
goto bail;
}
if (crm_element_value(msg, F_CIB_CLIENTNAME) == NULL) {
crm_xml_add(msg, F_CIB_CLIENTNAME, originator);
}
/* crm_log_xml_trace("Peer[inbound]", msg); */
cib_process_request(msg, FALSE, TRUE, TRUE, NULL);
return;
bail:
if (reason) {
const char *seq = crm_element_value(msg, F_SEQ);
const char *op = crm_element_value(msg, F_CIB_OPERATION);
crm_warn("Discarding %s message (%s) from %s: %s", op, seq, originator, reason);
}
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,167 | cib_process_command(xmlNode * request, xmlNode ** reply, xmlNode ** cib_diff, gboolean privileged)
{
xmlNode *input = NULL;
xmlNode *output = NULL;
xmlNode *result_cib = NULL;
xmlNode *current_cib = NULL;
#if ENABLE_ACL
xmlNode *filtered_current_cib = NULL;
#endif
int call_type = 0;
int call_options = 0;
int log_level = LOG_DEBUG_4;
const char *op = NULL;
const char *section = NULL;
int rc = pcmk_ok;
int rc2 = pcmk_ok;
gboolean send_r_notify = FALSE;
gboolean global_update = FALSE;
gboolean config_changed = FALSE;
gboolean manage_counters = TRUE;
CRM_ASSERT(cib_status == pcmk_ok);
*reply = NULL;
*cib_diff = NULL;
current_cib = the_cib;
/* Start processing the request... */
op = crm_element_value(request, F_CIB_OPERATION);
crm_element_value_int(request, F_CIB_CALLOPTS, &call_options);
rc = cib_get_operation_id(op, &call_type);
if (rc == pcmk_ok && privileged == FALSE) {
rc = cib_op_can_run(call_type, call_options, privileged, global_update);
}
rc2 = cib_op_prepare(call_type, request, &input, §ion);
if (rc == pcmk_ok) {
rc = rc2;
}
if (rc != pcmk_ok) {
crm_trace("Call setup failed: %s", pcmk_strerror(rc));
goto done;
} else if (cib_op_modifies(call_type) == FALSE) {
#if ENABLE_ACL
if (acl_enabled(config_hash) == FALSE
|| acl_filter_cib(request, current_cib, current_cib, &filtered_current_cib) == FALSE) {
rc = cib_perform_op(op, call_options, cib_op_func(call_type), TRUE,
section, request, input, FALSE, &config_changed,
current_cib, &result_cib, NULL, &output);
} else if (filtered_current_cib == NULL) {
crm_debug("Pre-filtered the entire cib");
rc = -EACCES;
} else {
crm_debug("Pre-filtered the queried cib according to the ACLs");
rc = cib_perform_op(op, call_options, cib_op_func(call_type), TRUE,
section, request, input, FALSE, &config_changed,
filtered_current_cib, &result_cib, NULL, &output);
}
#else
rc = cib_perform_op(op, call_options, cib_op_func(call_type), TRUE,
section, request, input, FALSE, &config_changed,
current_cib, &result_cib, NULL, &output);
#endif
CRM_CHECK(result_cib == NULL, free_xml(result_cib));
goto done;
}
/* Handle a valid write action */
global_update = crm_is_true(crm_element_value(request, F_CIB_GLOBAL_UPDATE));
if (global_update) {
manage_counters = FALSE;
call_options |= cib_force_diff;
CRM_CHECK(call_type == 3 || call_type == 4, crm_err("Call type: %d", call_type);
crm_log_xml_err(request, "bad op"));
}
#ifdef SUPPORT_PRENOTIFY
if ((call_options & cib_inhibit_notify) == 0) {
cib_pre_notify(call_options, op, the_cib, input);
}
#endif
if (rc == pcmk_ok) {
if (call_options & cib_inhibit_bcast) {
/* skip */
crm_trace("Skipping update: inhibit broadcast");
manage_counters = FALSE;
}
rc = cib_perform_op(op, call_options, cib_op_func(call_type), FALSE,
section, request, input, manage_counters, &config_changed,
current_cib, &result_cib, cib_diff, &output);
#if ENABLE_ACL
if (acl_enabled(config_hash) == TRUE
&& acl_check_diff(request, current_cib, result_cib, *cib_diff) == FALSE) {
rc = -EACCES;
}
#endif
if (rc == pcmk_ok && config_changed) {
time_t now;
char *now_str = NULL;
const char *validation = crm_element_value(result_cib, XML_ATTR_VALIDATION);
if (validation) {
int current_version = get_schema_version(validation);
int support_version = get_schema_version("pacemaker-1.1");
/* Once the later schemas support the "update-*" attributes, change "==" to ">=" -- Changed */
if (current_version >= support_version) {
const char *origin = crm_element_value(request, F_ORIG);
crm_xml_replace(result_cib, XML_ATTR_UPDATE_ORIG,
origin ? origin : cib_our_uname);
crm_xml_replace(result_cib, XML_ATTR_UPDATE_CLIENT,
crm_element_value(request, F_CIB_CLIENTNAME));
#if ENABLE_ACL
crm_xml_replace(result_cib, XML_ATTR_UPDATE_USER,
crm_element_value(request, F_CIB_USER));
#endif
}
}
now = time(NULL);
now_str = ctime(&now);
now_str[24] = EOS; /* replace the newline */
crm_xml_replace(result_cib, XML_CIB_ATTR_WRITTEN, now_str);
}
if (manage_counters == FALSE) {
config_changed = cib_config_changed(current_cib, result_cib, cib_diff);
}
/* Always write to disk for replace ops,
* this negates the need to detect ordering changes
*/
if (config_changed == FALSE && crm_str_eq(CIB_OP_REPLACE, op, TRUE)) {
config_changed = TRUE;
}
}
cib_add_digest(result_cib, *cib_diff);
if (rc == pcmk_ok && (call_options & cib_dryrun) == 0) {
rc = activateCibXml(result_cib, config_changed, op);
if (rc == pcmk_ok && cib_internal_config_changed(*cib_diff)) {
cib_read_config(config_hash, result_cib);
}
if (crm_str_eq(CIB_OP_REPLACE, op, TRUE)) {
if (section == NULL) {
send_r_notify = TRUE;
} else if (safe_str_eq(section, XML_TAG_CIB)) {
send_r_notify = TRUE;
} else if (safe_str_eq(section, XML_CIB_TAG_NODES)) {
send_r_notify = TRUE;
} else if (safe_str_eq(section, XML_CIB_TAG_STATUS)) {
send_r_notify = TRUE;
}
} else if (crm_str_eq(CIB_OP_ERASE, op, TRUE)) {
send_r_notify = TRUE;
}
} else if (rc == -pcmk_err_dtd_validation) {
if (output != NULL) {
crm_log_xml_info(output, "cib:output");
free_xml(output);
}
#if ENABLE_ACL
{
xmlNode *filtered_result_cib = NULL;
if (acl_enabled(config_hash) == FALSE
|| acl_filter_cib(request, current_cib, result_cib,
&filtered_result_cib) == FALSE) {
output = result_cib;
} else {
crm_debug("Filtered the result cib for output according to the ACLs");
output = filtered_result_cib;
if (result_cib != NULL) {
free_xml(result_cib);
}
}
}
#else
output = result_cib;
#endif
} else {
free_xml(result_cib);
}
if ((call_options & cib_inhibit_notify) == 0) {
const char *call_id = crm_element_value(request, F_CIB_CALLID);
const char *client = crm_element_value(request, F_CIB_CLIENTNAME);
#ifdef SUPPORT_POSTNOTIFY
cib_post_notify(call_options, op, input, rc, the_cib);
#endif
cib_diff_notify(call_options, client, call_id, op, input, rc, *cib_diff);
}
if (send_r_notify) {
const char *origin = crm_element_value(request, F_ORIG);
cib_replace_notify(origin, the_cib, rc, *cib_diff);
}
if (rc != pcmk_ok) {
log_level = LOG_DEBUG_4;
if (rc == -pcmk_err_dtd_validation && global_update) {
log_level = LOG_WARNING;
crm_log_xml_info(input, "cib:global_update");
}
} else if (config_changed) {
log_level = LOG_DEBUG_3;
if (cib_is_master) {
log_level = LOG_NOTICE;
}
} else if (cib_is_master) {
log_level = LOG_DEBUG_2;
}
log_cib_diff(log_level, *cib_diff, "cib:diff");
done:
if ((call_options & cib_discard_reply) == 0) {
*reply = cib_construct_reply(request, output, rc);
crm_log_xml_trace(*reply, "cib:reply");
}
#if ENABLE_ACL
if (filtered_current_cib != NULL) {
free_xml(filtered_current_cib);
}
#endif
if (call_type >= 0) {
cib_op_cleanup(call_type, call_options, &input, &output);
}
return rc;
}
| DoS | 0 | cib_process_command(xmlNode * request, xmlNode ** reply, xmlNode ** cib_diff, gboolean privileged)
{
xmlNode *input = NULL;
xmlNode *output = NULL;
xmlNode *result_cib = NULL;
xmlNode *current_cib = NULL;
#if ENABLE_ACL
xmlNode *filtered_current_cib = NULL;
#endif
int call_type = 0;
int call_options = 0;
int log_level = LOG_DEBUG_4;
const char *op = NULL;
const char *section = NULL;
int rc = pcmk_ok;
int rc2 = pcmk_ok;
gboolean send_r_notify = FALSE;
gboolean global_update = FALSE;
gboolean config_changed = FALSE;
gboolean manage_counters = TRUE;
CRM_ASSERT(cib_status == pcmk_ok);
*reply = NULL;
*cib_diff = NULL;
current_cib = the_cib;
/* Start processing the request... */
op = crm_element_value(request, F_CIB_OPERATION);
crm_element_value_int(request, F_CIB_CALLOPTS, &call_options);
rc = cib_get_operation_id(op, &call_type);
if (rc == pcmk_ok && privileged == FALSE) {
rc = cib_op_can_run(call_type, call_options, privileged, global_update);
}
rc2 = cib_op_prepare(call_type, request, &input, §ion);
if (rc == pcmk_ok) {
rc = rc2;
}
if (rc != pcmk_ok) {
crm_trace("Call setup failed: %s", pcmk_strerror(rc));
goto done;
} else if (cib_op_modifies(call_type) == FALSE) {
#if ENABLE_ACL
if (acl_enabled(config_hash) == FALSE
|| acl_filter_cib(request, current_cib, current_cib, &filtered_current_cib) == FALSE) {
rc = cib_perform_op(op, call_options, cib_op_func(call_type), TRUE,
section, request, input, FALSE, &config_changed,
current_cib, &result_cib, NULL, &output);
} else if (filtered_current_cib == NULL) {
crm_debug("Pre-filtered the entire cib");
rc = -EACCES;
} else {
crm_debug("Pre-filtered the queried cib according to the ACLs");
rc = cib_perform_op(op, call_options, cib_op_func(call_type), TRUE,
section, request, input, FALSE, &config_changed,
filtered_current_cib, &result_cib, NULL, &output);
}
#else
rc = cib_perform_op(op, call_options, cib_op_func(call_type), TRUE,
section, request, input, FALSE, &config_changed,
current_cib, &result_cib, NULL, &output);
#endif
CRM_CHECK(result_cib == NULL, free_xml(result_cib));
goto done;
}
/* Handle a valid write action */
global_update = crm_is_true(crm_element_value(request, F_CIB_GLOBAL_UPDATE));
if (global_update) {
manage_counters = FALSE;
call_options |= cib_force_diff;
CRM_CHECK(call_type == 3 || call_type == 4, crm_err("Call type: %d", call_type);
crm_log_xml_err(request, "bad op"));
}
#ifdef SUPPORT_PRENOTIFY
if ((call_options & cib_inhibit_notify) == 0) {
cib_pre_notify(call_options, op, the_cib, input);
}
#endif
if (rc == pcmk_ok) {
if (call_options & cib_inhibit_bcast) {
/* skip */
crm_trace("Skipping update: inhibit broadcast");
manage_counters = FALSE;
}
rc = cib_perform_op(op, call_options, cib_op_func(call_type), FALSE,
section, request, input, manage_counters, &config_changed,
current_cib, &result_cib, cib_diff, &output);
#if ENABLE_ACL
if (acl_enabled(config_hash) == TRUE
&& acl_check_diff(request, current_cib, result_cib, *cib_diff) == FALSE) {
rc = -EACCES;
}
#endif
if (rc == pcmk_ok && config_changed) {
time_t now;
char *now_str = NULL;
const char *validation = crm_element_value(result_cib, XML_ATTR_VALIDATION);
if (validation) {
int current_version = get_schema_version(validation);
int support_version = get_schema_version("pacemaker-1.1");
/* Once the later schemas support the "update-*" attributes, change "==" to ">=" -- Changed */
if (current_version >= support_version) {
const char *origin = crm_element_value(request, F_ORIG);
crm_xml_replace(result_cib, XML_ATTR_UPDATE_ORIG,
origin ? origin : cib_our_uname);
crm_xml_replace(result_cib, XML_ATTR_UPDATE_CLIENT,
crm_element_value(request, F_CIB_CLIENTNAME));
#if ENABLE_ACL
crm_xml_replace(result_cib, XML_ATTR_UPDATE_USER,
crm_element_value(request, F_CIB_USER));
#endif
}
}
now = time(NULL);
now_str = ctime(&now);
now_str[24] = EOS; /* replace the newline */
crm_xml_replace(result_cib, XML_CIB_ATTR_WRITTEN, now_str);
}
if (manage_counters == FALSE) {
config_changed = cib_config_changed(current_cib, result_cib, cib_diff);
}
/* Always write to disk for replace ops,
* this negates the need to detect ordering changes
*/
if (config_changed == FALSE && crm_str_eq(CIB_OP_REPLACE, op, TRUE)) {
config_changed = TRUE;
}
}
cib_add_digest(result_cib, *cib_diff);
if (rc == pcmk_ok && (call_options & cib_dryrun) == 0) {
rc = activateCibXml(result_cib, config_changed, op);
if (rc == pcmk_ok && cib_internal_config_changed(*cib_diff)) {
cib_read_config(config_hash, result_cib);
}
if (crm_str_eq(CIB_OP_REPLACE, op, TRUE)) {
if (section == NULL) {
send_r_notify = TRUE;
} else if (safe_str_eq(section, XML_TAG_CIB)) {
send_r_notify = TRUE;
} else if (safe_str_eq(section, XML_CIB_TAG_NODES)) {
send_r_notify = TRUE;
} else if (safe_str_eq(section, XML_CIB_TAG_STATUS)) {
send_r_notify = TRUE;
}
} else if (crm_str_eq(CIB_OP_ERASE, op, TRUE)) {
send_r_notify = TRUE;
}
} else if (rc == -pcmk_err_dtd_validation) {
if (output != NULL) {
crm_log_xml_info(output, "cib:output");
free_xml(output);
}
#if ENABLE_ACL
{
xmlNode *filtered_result_cib = NULL;
if (acl_enabled(config_hash) == FALSE
|| acl_filter_cib(request, current_cib, result_cib,
&filtered_result_cib) == FALSE) {
output = result_cib;
} else {
crm_debug("Filtered the result cib for output according to the ACLs");
output = filtered_result_cib;
if (result_cib != NULL) {
free_xml(result_cib);
}
}
}
#else
output = result_cib;
#endif
} else {
free_xml(result_cib);
}
if ((call_options & cib_inhibit_notify) == 0) {
const char *call_id = crm_element_value(request, F_CIB_CALLID);
const char *client = crm_element_value(request, F_CIB_CLIENTNAME);
#ifdef SUPPORT_POSTNOTIFY
cib_post_notify(call_options, op, input, rc, the_cib);
#endif
cib_diff_notify(call_options, client, call_id, op, input, rc, *cib_diff);
}
if (send_r_notify) {
const char *origin = crm_element_value(request, F_ORIG);
cib_replace_notify(origin, the_cib, rc, *cib_diff);
}
if (rc != pcmk_ok) {
log_level = LOG_DEBUG_4;
if (rc == -pcmk_err_dtd_validation && global_update) {
log_level = LOG_WARNING;
crm_log_xml_info(input, "cib:global_update");
}
} else if (config_changed) {
log_level = LOG_DEBUG_3;
if (cib_is_master) {
log_level = LOG_NOTICE;
}
} else if (cib_is_master) {
log_level = LOG_DEBUG_2;
}
log_cib_diff(log_level, *cib_diff, "cib:diff");
done:
if ((call_options & cib_discard_reply) == 0) {
*reply = cib_construct_reply(request, output, rc);
crm_log_xml_trace(*reply, "cib:reply");
}
#if ENABLE_ACL
if (filtered_current_cib != NULL) {
free_xml(filtered_current_cib);
}
#endif
if (call_type >= 0) {
cib_op_cleanup(call_type, call_options, &input, &output);
}
return rc;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,168 | cib_process_request(xmlNode * request, gboolean force_synchronous, gboolean privileged,
gboolean from_peer, cib_client_t * cib_client)
{
int call_type = 0;
int call_options = 0;
gboolean process = TRUE;
gboolean is_update = TRUE;
gboolean needs_reply = TRUE;
gboolean local_notify = FALSE;
gboolean needs_forward = FALSE;
gboolean global_update = crm_is_true(crm_element_value(request, F_CIB_GLOBAL_UPDATE));
xmlNode *op_reply = NULL;
xmlNode *result_diff = NULL;
int rc = pcmk_ok;
const char *op = crm_element_value(request, F_CIB_OPERATION);
const char *originator = crm_element_value(request, F_ORIG);
const char *host = crm_element_value(request, F_CIB_HOST);
const char *client_id = crm_element_value(request, F_CIB_CLIENTID);
crm_trace("%s Processing msg %s", cib_our_uname, crm_element_value(request, F_SEQ));
cib_num_ops++;
if (cib_num_ops == 0) {
cib_num_fail = 0;
cib_num_local = 0;
cib_num_updates = 0;
crm_info("Stats wrapped around");
}
if (host != NULL && strlen(host) == 0) {
host = NULL;
}
crm_element_value_int(request, F_CIB_CALLOPTS, &call_options);
if (force_synchronous) {
call_options |= cib_sync_call;
}
crm_trace("Processing %s message (%s) for %s...",
from_peer ? "peer" : "local",
from_peer ? originator : cib_our_uname, host ? host : "master");
rc = cib_get_operation_id(op, &call_type);
if (rc != pcmk_ok) {
/* TODO: construct error reply? */
crm_err("Pre-processing of command failed: %s", pcmk_strerror(rc));
return;
}
is_update = cib_op_modifies(call_type);
if (is_update) {
cib_num_updates++;
}
if (from_peer == FALSE) {
parse_local_options(cib_client, call_type, call_options, host, op,
&local_notify, &needs_reply, &process, &needs_forward);
} else if (parse_peer_options(call_type, request, &local_notify,
&needs_reply, &process, &needs_forward) == FALSE) {
return;
}
crm_trace("Finished determining processing actions");
if (call_options & cib_discard_reply) {
needs_reply = is_update;
local_notify = FALSE;
}
if (needs_forward) {
forward_request(request, cib_client, call_options);
return;
}
if (cib_status != pcmk_ok) {
rc = cib_status;
crm_err("Operation ignored, cluster configuration is invalid."
" Please repair and restart: %s", pcmk_strerror(cib_status));
op_reply = cib_construct_reply(request, the_cib, cib_status);
} else if (process) {
int level = LOG_INFO;
const char *section = crm_element_value(request, F_CIB_SECTION);
cib_num_local++;
rc = cib_process_command(request, &op_reply, &result_diff, privileged);
if (global_update) {
switch (rc) {
case pcmk_ok:
case -pcmk_err_old_data:
case -pcmk_err_diff_resync:
case -pcmk_err_diff_failed:
level = LOG_DEBUG_2;
break;
default:
level = LOG_ERR;
}
} else if (safe_str_eq(op, CIB_OP_QUERY)) {
level = LOG_DEBUG_2;
} else if (rc != pcmk_ok) {
cib_num_fail++;
level = LOG_WARNING;
} else if (safe_str_eq(op, CIB_OP_SLAVE)) {
level = LOG_DEBUG_2;
} else if (safe_str_eq(section, XML_CIB_TAG_STATUS)) {
level = LOG_DEBUG_2;
}
do_crm_log_unlikely(level,
"Operation complete: op %s for section %s (origin=%s/%s/%s, version=%s.%s.%s): %s (rc=%d)",
op, section ? section : "'all'", originator ? originator : "local",
crm_element_value(request, F_CIB_CLIENTNAME), crm_element_value(request,
F_CIB_CALLID),
the_cib ? crm_element_value(the_cib, XML_ATTR_GENERATION_ADMIN) : "0",
the_cib ? crm_element_value(the_cib, XML_ATTR_GENERATION) : "0",
the_cib ? crm_element_value(the_cib, XML_ATTR_NUMUPDATES) : "0",
pcmk_strerror(rc), rc);
if (op_reply == NULL && (needs_reply || local_notify)) {
crm_err("Unexpected NULL reply to message");
crm_log_xml_err(request, "null reply");
needs_reply = FALSE;
local_notify = FALSE;
}
}
crm_trace("processing response cases %.16x %.16x", call_options, cib_sync_call);
/* from now on we are the server */
if (needs_reply == FALSE || stand_alone) {
/* nothing more to do...
* this was a non-originating slave update
*/
crm_trace("Completed slave update");
} else if (rc == pcmk_ok && result_diff != NULL && !(call_options & cib_inhibit_bcast)) {
gboolean broadcast = FALSE;
cib_local_bcast_num++;
crm_xml_add_int(request, F_CIB_LOCAL_NOTIFY_ID, cib_local_bcast_num);
broadcast = send_peer_reply(request, result_diff, originator, TRUE);
if (broadcast &&
client_id &&
local_notify &&
op_reply) {
/* If we have been asked to sync the reply,
* and a bcast msg has gone out, we queue the local notify
* until we know the bcast message has been received */
local_notify = FALSE;
queue_local_notify(op_reply, client_id, (call_options & cib_sync_call), from_peer);
op_reply = NULL; /* the reply is queued, so don't free here */
}
} else if (call_options & cib_discard_reply) {
crm_trace("Caller isn't interested in reply");
} else if (from_peer) {
if (is_update == FALSE || result_diff == NULL) {
crm_trace("Request not broadcast: R/O call");
} else if (call_options & cib_inhibit_bcast) {
crm_trace("Request not broadcast: inhibited");
} else if (rc != pcmk_ok) {
crm_trace("Request not broadcast: call failed: %s", pcmk_strerror(rc));
} else {
crm_trace("Directing reply to %s", originator);
}
send_peer_reply(op_reply, result_diff, originator, FALSE);
}
if (local_notify && client_id) {
if (process == FALSE) {
do_local_notify(request, client_id, call_options & cib_sync_call, from_peer);
} else {
do_local_notify(op_reply, client_id, call_options & cib_sync_call, from_peer);
}
}
free_xml(op_reply);
free_xml(result_diff);
return;
}
| DoS | 0 | cib_process_request(xmlNode * request, gboolean force_synchronous, gboolean privileged,
gboolean from_peer, cib_client_t * cib_client)
{
int call_type = 0;
int call_options = 0;
gboolean process = TRUE;
gboolean is_update = TRUE;
gboolean needs_reply = TRUE;
gboolean local_notify = FALSE;
gboolean needs_forward = FALSE;
gboolean global_update = crm_is_true(crm_element_value(request, F_CIB_GLOBAL_UPDATE));
xmlNode *op_reply = NULL;
xmlNode *result_diff = NULL;
int rc = pcmk_ok;
const char *op = crm_element_value(request, F_CIB_OPERATION);
const char *originator = crm_element_value(request, F_ORIG);
const char *host = crm_element_value(request, F_CIB_HOST);
const char *client_id = crm_element_value(request, F_CIB_CLIENTID);
crm_trace("%s Processing msg %s", cib_our_uname, crm_element_value(request, F_SEQ));
cib_num_ops++;
if (cib_num_ops == 0) {
cib_num_fail = 0;
cib_num_local = 0;
cib_num_updates = 0;
crm_info("Stats wrapped around");
}
if (host != NULL && strlen(host) == 0) {
host = NULL;
}
crm_element_value_int(request, F_CIB_CALLOPTS, &call_options);
if (force_synchronous) {
call_options |= cib_sync_call;
}
crm_trace("Processing %s message (%s) for %s...",
from_peer ? "peer" : "local",
from_peer ? originator : cib_our_uname, host ? host : "master");
rc = cib_get_operation_id(op, &call_type);
if (rc != pcmk_ok) {
/* TODO: construct error reply? */
crm_err("Pre-processing of command failed: %s", pcmk_strerror(rc));
return;
}
is_update = cib_op_modifies(call_type);
if (is_update) {
cib_num_updates++;
}
if (from_peer == FALSE) {
parse_local_options(cib_client, call_type, call_options, host, op,
&local_notify, &needs_reply, &process, &needs_forward);
} else if (parse_peer_options(call_type, request, &local_notify,
&needs_reply, &process, &needs_forward) == FALSE) {
return;
}
crm_trace("Finished determining processing actions");
if (call_options & cib_discard_reply) {
needs_reply = is_update;
local_notify = FALSE;
}
if (needs_forward) {
forward_request(request, cib_client, call_options);
return;
}
if (cib_status != pcmk_ok) {
rc = cib_status;
crm_err("Operation ignored, cluster configuration is invalid."
" Please repair and restart: %s", pcmk_strerror(cib_status));
op_reply = cib_construct_reply(request, the_cib, cib_status);
} else if (process) {
int level = LOG_INFO;
const char *section = crm_element_value(request, F_CIB_SECTION);
cib_num_local++;
rc = cib_process_command(request, &op_reply, &result_diff, privileged);
if (global_update) {
switch (rc) {
case pcmk_ok:
case -pcmk_err_old_data:
case -pcmk_err_diff_resync:
case -pcmk_err_diff_failed:
level = LOG_DEBUG_2;
break;
default:
level = LOG_ERR;
}
} else if (safe_str_eq(op, CIB_OP_QUERY)) {
level = LOG_DEBUG_2;
} else if (rc != pcmk_ok) {
cib_num_fail++;
level = LOG_WARNING;
} else if (safe_str_eq(op, CIB_OP_SLAVE)) {
level = LOG_DEBUG_2;
} else if (safe_str_eq(section, XML_CIB_TAG_STATUS)) {
level = LOG_DEBUG_2;
}
do_crm_log_unlikely(level,
"Operation complete: op %s for section %s (origin=%s/%s/%s, version=%s.%s.%s): %s (rc=%d)",
op, section ? section : "'all'", originator ? originator : "local",
crm_element_value(request, F_CIB_CLIENTNAME), crm_element_value(request,
F_CIB_CALLID),
the_cib ? crm_element_value(the_cib, XML_ATTR_GENERATION_ADMIN) : "0",
the_cib ? crm_element_value(the_cib, XML_ATTR_GENERATION) : "0",
the_cib ? crm_element_value(the_cib, XML_ATTR_NUMUPDATES) : "0",
pcmk_strerror(rc), rc);
if (op_reply == NULL && (needs_reply || local_notify)) {
crm_err("Unexpected NULL reply to message");
crm_log_xml_err(request, "null reply");
needs_reply = FALSE;
local_notify = FALSE;
}
}
crm_trace("processing response cases %.16x %.16x", call_options, cib_sync_call);
/* from now on we are the server */
if (needs_reply == FALSE || stand_alone) {
/* nothing more to do...
* this was a non-originating slave update
*/
crm_trace("Completed slave update");
} else if (rc == pcmk_ok && result_diff != NULL && !(call_options & cib_inhibit_bcast)) {
gboolean broadcast = FALSE;
cib_local_bcast_num++;
crm_xml_add_int(request, F_CIB_LOCAL_NOTIFY_ID, cib_local_bcast_num);
broadcast = send_peer_reply(request, result_diff, originator, TRUE);
if (broadcast &&
client_id &&
local_notify &&
op_reply) {
/* If we have been asked to sync the reply,
* and a bcast msg has gone out, we queue the local notify
* until we know the bcast message has been received */
local_notify = FALSE;
queue_local_notify(op_reply, client_id, (call_options & cib_sync_call), from_peer);
op_reply = NULL; /* the reply is queued, so don't free here */
}
} else if (call_options & cib_discard_reply) {
crm_trace("Caller isn't interested in reply");
} else if (from_peer) {
if (is_update == FALSE || result_diff == NULL) {
crm_trace("Request not broadcast: R/O call");
} else if (call_options & cib_inhibit_bcast) {
crm_trace("Request not broadcast: inhibited");
} else if (rc != pcmk_ok) {
crm_trace("Request not broadcast: call failed: %s", pcmk_strerror(rc));
} else {
crm_trace("Directing reply to %s", originator);
}
send_peer_reply(op_reply, result_diff, originator, FALSE);
}
if (local_notify && client_id) {
if (process == FALSE) {
do_local_notify(request, client_id, call_options & cib_sync_call, from_peer);
} else {
do_local_notify(op_reply, client_id, call_options & cib_sync_call, from_peer);
}
}
free_xml(op_reply);
free_xml(result_diff);
return;
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,169 | disconnect_remote_client(gpointer key, gpointer value, gpointer user_data)
{
cib_client_t *a_client = value;
crm_err("Disconnecting %s... Not implemented", crm_str(a_client->name));
}
| DoS | 0 | disconnect_remote_client(gpointer key, gpointer value, gpointer user_data)
{
cib_client_t *a_client = value;
crm_err("Disconnecting %s... Not implemented", crm_str(a_client->name));
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,170 | forward_request(xmlNode * request, cib_client_t * cib_client, int call_options)
{
const char *op = crm_element_value(request, F_CIB_OPERATION);
const char *host = crm_element_value(request, F_CIB_HOST);
crm_xml_add(request, F_CIB_DELEGATED, cib_our_uname);
if (host != NULL) {
crm_trace("Forwarding %s op to %s", op, host);
send_cluster_message(crm_get_peer(0, host), crm_msg_cib, request, FALSE);
} else {
crm_trace("Forwarding %s op to master instance", op);
send_cluster_message(NULL, crm_msg_cib, request, FALSE);
}
/* Return the request to its original state */
xml_remove_prop(request, F_CIB_DELEGATED);
if (call_options & cib_discard_reply) {
crm_trace("Client not interested in reply");
}
}
| DoS | 0 | forward_request(xmlNode * request, cib_client_t * cib_client, int call_options)
{
const char *op = crm_element_value(request, F_CIB_OPERATION);
const char *host = crm_element_value(request, F_CIB_HOST);
crm_xml_add(request, F_CIB_DELEGATED, cib_our_uname);
if (host != NULL) {
crm_trace("Forwarding %s op to %s", op, host);
send_cluster_message(crm_get_peer(0, host), crm_msg_cib, request, FALSE);
} else {
crm_trace("Forwarding %s op to master instance", op);
send_cluster_message(NULL, crm_msg_cib, request, FALSE);
}
/* Return the request to its original state */
xml_remove_prop(request, F_CIB_DELEGATED);
if (call_options & cib_discard_reply) {
crm_trace("Client not interested in reply");
}
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,171 | initiate_exit(void)
{
int active = 0;
xmlNode *leaving = NULL;
active = crm_active_peers();
if (active < 2) {
terminate_cib(__FUNCTION__, FALSE);
return;
}
crm_info("Sending disconnect notification to %d peers...", active);
leaving = create_xml_node(NULL, "exit-notification");
crm_xml_add(leaving, F_TYPE, "cib");
crm_xml_add(leaving, F_CIB_OPERATION, "cib_shutdown_req");
send_cluster_message(NULL, crm_msg_cib, leaving, TRUE);
free_xml(leaving);
g_timeout_add(crm_get_msec("5s"), cib_force_exit, NULL);
}
| DoS | 0 | initiate_exit(void)
{
int active = 0;
xmlNode *leaving = NULL;
active = crm_active_peers();
if (active < 2) {
terminate_cib(__FUNCTION__, FALSE);
return;
}
crm_info("Sending disconnect notification to %d peers...", active);
leaving = create_xml_node(NULL, "exit-notification");
crm_xml_add(leaving, F_TYPE, "cib");
crm_xml_add(leaving, F_CIB_OPERATION, "cib_shutdown_req");
send_cluster_message(NULL, crm_msg_cib, leaving, TRUE);
free_xml(leaving);
g_timeout_add(crm_get_msec("5s"), cib_force_exit, NULL);
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,172 | parse_local_options(cib_client_t * cib_client, int call_type, int call_options, const char *host,
const char *op, gboolean * local_notify, gboolean * needs_reply,
gboolean * process, gboolean * needs_forward)
{
if (cib_op_modifies(call_type)
&& !(call_options & cib_inhibit_bcast)) {
/* we need to send an update anyway */
*needs_reply = TRUE;
} else {
*needs_reply = FALSE;
}
if (host == NULL && (call_options & cib_scope_local)) {
crm_trace("Processing locally scoped %s op from %s", op, cib_client->name);
*local_notify = TRUE;
} else if (host == NULL && cib_is_master) {
crm_trace("Processing master %s op locally from %s", op, cib_client->name);
*local_notify = TRUE;
} else if (safe_str_eq(host, cib_our_uname)) {
crm_trace("Processing locally addressed %s op from %s", op, cib_client->name);
*local_notify = TRUE;
} else if (stand_alone) {
*needs_forward = FALSE;
*local_notify = TRUE;
*process = TRUE;
} else {
crm_trace("%s op from %s needs to be forwarded to %s",
op, cib_client->name, host ? host : "the master instance");
*needs_forward = TRUE;
*process = FALSE;
}
}
| DoS | 0 | parse_local_options(cib_client_t * cib_client, int call_type, int call_options, const char *host,
const char *op, gboolean * local_notify, gboolean * needs_reply,
gboolean * process, gboolean * needs_forward)
{
if (cib_op_modifies(call_type)
&& !(call_options & cib_inhibit_bcast)) {
/* we need to send an update anyway */
*needs_reply = TRUE;
} else {
*needs_reply = FALSE;
}
if (host == NULL && (call_options & cib_scope_local)) {
crm_trace("Processing locally scoped %s op from %s", op, cib_client->name);
*local_notify = TRUE;
} else if (host == NULL && cib_is_master) {
crm_trace("Processing master %s op locally from %s", op, cib_client->name);
*local_notify = TRUE;
} else if (safe_str_eq(host, cib_our_uname)) {
crm_trace("Processing locally addressed %s op from %s", op, cib_client->name);
*local_notify = TRUE;
} else if (stand_alone) {
*needs_forward = FALSE;
*local_notify = TRUE;
*process = TRUE;
} else {
crm_trace("%s op from %s needs to be forwarded to %s",
op, cib_client->name, host ? host : "the master instance");
*needs_forward = TRUE;
*process = FALSE;
}
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,173 | queue_local_notify(xmlNode * notify_src, const char *client_id, gboolean sync_reply, gboolean from_peer)
{
cib_local_notify_t *notify = calloc(1, sizeof(cib_local_notify_t));
notify->notify_src = notify_src;
notify->client_id = strdup(client_id);
notify->sync_reply = sync_reply;
notify->from_peer = from_peer;
if (!local_notify_queue) {
local_notify_queue = g_hash_table_new_full(g_direct_hash,
g_direct_equal, NULL, local_notify_destroy_callback);
}
g_hash_table_insert(local_notify_queue, GINT_TO_POINTER(cib_local_bcast_num), notify);
}
| DoS | 0 | queue_local_notify(xmlNode * notify_src, const char *client_id, gboolean sync_reply, gboolean from_peer)
{
cib_local_notify_t *notify = calloc(1, sizeof(cib_local_notify_t));
notify->notify_src = notify_src;
notify->client_id = strdup(client_id);
notify->sync_reply = sync_reply;
notify->from_peer = from_peer;
if (!local_notify_queue) {
local_notify_queue = g_hash_table_new_full(g_direct_hash,
g_direct_equal, NULL, local_notify_destroy_callback);
}
g_hash_table_insert(local_notify_queue, GINT_TO_POINTER(cib_local_bcast_num), notify);
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,174 | terminate_cib(const char *caller, gboolean fast)
{
if (remote_fd > 0) {
close(remote_fd);
remote_fd = 0;
}
if (remote_tls_fd > 0) {
close(remote_tls_fd);
remote_tls_fd = 0;
}
if(!fast) {
crm_info("%s: Disconnecting from cluster infrastructure", caller);
crm_cluster_disconnect(&crm_cluster);
}
uninitializeCib();
crm_info("%s: Exiting%s...", caller, fast?" fast":mainloop?" from mainloop":"");
if(fast == FALSE && mainloop != NULL && g_main_is_running(mainloop)) {
g_main_quit(mainloop);
} else {
qb_ipcs_destroy(ipcs_ro);
qb_ipcs_destroy(ipcs_rw);
qb_ipcs_destroy(ipcs_shm);
if (fast) {
crm_exit(EX_USAGE);
} else {
crm_exit(EX_OK);
}
}
}
| DoS | 0 | terminate_cib(const char *caller, gboolean fast)
{
if (remote_fd > 0) {
close(remote_fd);
remote_fd = 0;
}
if (remote_tls_fd > 0) {
close(remote_tls_fd);
remote_tls_fd = 0;
}
if(!fast) {
crm_info("%s: Disconnecting from cluster infrastructure", caller);
crm_cluster_disconnect(&crm_cluster);
}
uninitializeCib();
crm_info("%s: Exiting%s...", caller, fast?" fast":mainloop?" from mainloop":"");
if(fast == FALSE && mainloop != NULL && g_main_is_running(mainloop)) {
g_main_quit(mainloop);
} else {
qb_ipcs_destroy(ipcs_ro);
qb_ipcs_destroy(ipcs_rw);
qb_ipcs_destroy(ipcs_shm);
if (fast) {
crm_exit(EX_USAGE);
} else {
crm_exit(EX_OK);
}
}
}
| @@ -347,13 +347,18 @@ do_local_notify(xmlNode * notify_src, const char *client_id,
int rid = 0;
if(sync_reply) {
- CRM_LOG_ASSERT(client_obj->request_id);
+ if (client_obj->ipc) {
+ CRM_LOG_ASSERT(client_obj->request_id);
- rid = client_obj->request_id;
- client_obj->request_id = 0;
+ rid = client_obj->request_id;
+ client_obj->request_id = 0;
- crm_trace("Sending response %d to %s %s",
+ crm_trace("Sending response %d to %s %s",
rid, client_obj->name, from_peer?"(originator of delegated request)":"");
+ } else {
+ crm_trace("Sending response to %s %s",
+ client_obj->name, from_peer?"(originator of delegated request)":"");
+ }
} else {
crm_trace("Sending an event to %s %s", | CWE-399 | null | null |
27,175 | attach_cib_generation(xmlNode * msg, const char *field, xmlNode * a_cib)
{
xmlNode *generation = create_xml_node(NULL, XML_CIB_TAG_GENERATION_TUPPLE);
if (a_cib != NULL) {
copy_in_properties(generation, a_cib);
}
add_message_xml(msg, field, generation);
free_xml(generation);
}
| DoS | 0 | attach_cib_generation(xmlNode * msg, const char *field, xmlNode * a_cib)
{
xmlNode *generation = create_xml_node(NULL, XML_CIB_TAG_GENERATION_TUPPLE);
if (a_cib != NULL) {
copy_in_properties(generation, a_cib);
}
add_message_xml(msg, field, generation);
free_xml(generation);
}
| @@ -83,7 +83,7 @@ cib_notify_client(gpointer key, gpointer value, gpointer user_data)
CRM_CHECK(client != NULL, return TRUE);
CRM_CHECK(update_msg != NULL, return TRUE);
- if (client->ipc == NULL) {
+ if (client->ipc == NULL && client->session == NULL) {
crm_warn("Skipping client with NULL channel");
return FALSE;
} | CWE-399 | null | null |
27,176 | cib_diff_notify(int options, const char *client, const char *call_id, const char *op,
xmlNode * update, int result, xmlNode * diff)
{
int add_updates = 0;
int add_epoch = 0;
int add_admin_epoch = 0;
int del_updates = 0;
int del_epoch = 0;
int del_admin_epoch = 0;
int log_level = LOG_DEBUG_2;
if (diff == NULL) {
return;
}
if (result != pcmk_ok) {
log_level = LOG_WARNING;
}
cib_diff_version_details(diff, &add_admin_epoch, &add_epoch, &add_updates,
&del_admin_epoch, &del_epoch, &del_updates);
if (add_updates != del_updates) {
do_crm_log(log_level,
"Update (client: %s%s%s): %d.%d.%d -> %d.%d.%d (%s)",
client, call_id ? ", call:" : "", call_id ? call_id : "",
del_admin_epoch, del_epoch, del_updates,
add_admin_epoch, add_epoch, add_updates, pcmk_strerror(result));
} else if (diff != NULL) {
do_crm_log(log_level,
"Local-only Change (client:%s%s%s): %d.%d.%d (%s)",
client, call_id ? ", call: " : "", call_id ? call_id : "",
add_admin_epoch, add_epoch, add_updates, pcmk_strerror(result));
}
do_cib_notify(options, op, update, result, diff, T_CIB_DIFF_NOTIFY);
}
| DoS | 0 | cib_diff_notify(int options, const char *client, const char *call_id, const char *op,
xmlNode * update, int result, xmlNode * diff)
{
int add_updates = 0;
int add_epoch = 0;
int add_admin_epoch = 0;
int del_updates = 0;
int del_epoch = 0;
int del_admin_epoch = 0;
int log_level = LOG_DEBUG_2;
if (diff == NULL) {
return;
}
if (result != pcmk_ok) {
log_level = LOG_WARNING;
}
cib_diff_version_details(diff, &add_admin_epoch, &add_epoch, &add_updates,
&del_admin_epoch, &del_epoch, &del_updates);
if (add_updates != del_updates) {
do_crm_log(log_level,
"Update (client: %s%s%s): %d.%d.%d -> %d.%d.%d (%s)",
client, call_id ? ", call:" : "", call_id ? call_id : "",
del_admin_epoch, del_epoch, del_updates,
add_admin_epoch, add_epoch, add_updates, pcmk_strerror(result));
} else if (diff != NULL) {
do_crm_log(log_level,
"Local-only Change (client:%s%s%s): %d.%d.%d (%s)",
client, call_id ? ", call: " : "", call_id ? call_id : "",
add_admin_epoch, add_epoch, add_updates, pcmk_strerror(result));
}
do_cib_notify(options, op, update, result, diff, T_CIB_DIFF_NOTIFY);
}
| @@ -83,7 +83,7 @@ cib_notify_client(gpointer key, gpointer value, gpointer user_data)
CRM_CHECK(client != NULL, return TRUE);
CRM_CHECK(update_msg != NULL, return TRUE);
- if (client->ipc == NULL) {
+ if (client->ipc == NULL && client->session == NULL) {
crm_warn("Skipping client with NULL channel");
return FALSE;
} | CWE-399 | null | null |
27,177 | cib_post_notify(int options, const char *op, xmlNode * update,
int result, xmlNode * new_obj)
{
gboolean needed = FALSE;
g_hash_table_foreach(client_list, need_post_notify, &needed);
if (needed == FALSE) {
return;
}
do_cib_notify(options, op, update, result, new_obj, T_CIB_UPDATE_CONFIRM);
}
| DoS | 0 | cib_post_notify(int options, const char *op, xmlNode * update,
int result, xmlNode * new_obj)
{
gboolean needed = FALSE;
g_hash_table_foreach(client_list, need_post_notify, &needed);
if (needed == FALSE) {
return;
}
do_cib_notify(options, op, update, result, new_obj, T_CIB_UPDATE_CONFIRM);
}
| @@ -83,7 +83,7 @@ cib_notify_client(gpointer key, gpointer value, gpointer user_data)
CRM_CHECK(client != NULL, return TRUE);
CRM_CHECK(update_msg != NULL, return TRUE);
- if (client->ipc == NULL) {
+ if (client->ipc == NULL && client->session == NULL) {
crm_warn("Skipping client with NULL channel");
return FALSE;
} | CWE-399 | null | null |
27,178 | cib_pre_notify(int options, const char *op, xmlNode * existing, xmlNode * update)
{
xmlNode *update_msg = NULL;
const char *type = NULL;
const char *id = NULL;
gboolean needed = FALSE;
g_hash_table_foreach(client_list, need_pre_notify, &needed);
if (needed == FALSE) {
return;
}
/* TODO: consider pre-notification for removal */
update_msg = create_xml_node(NULL, "pre-notify");
if (update != NULL) {
id = crm_element_value(update, XML_ATTR_ID);
}
crm_xml_add(update_msg, F_TYPE, T_CIB_NOTIFY);
crm_xml_add(update_msg, F_SUBTYPE, T_CIB_PRE_NOTIFY);
crm_xml_add(update_msg, F_CIB_OPERATION, op);
if (id != NULL) {
crm_xml_add(update_msg, F_CIB_OBJID, id);
}
if (update != NULL) {
crm_xml_add(update_msg, F_CIB_OBJTYPE, crm_element_name(update));
} else if (existing != NULL) {
crm_xml_add(update_msg, F_CIB_OBJTYPE, crm_element_name(existing));
}
type = crm_element_value(update_msg, F_CIB_OBJTYPE);
attach_cib_generation(update_msg, "cib_generation", the_cib);
if (existing != NULL) {
add_message_xml(update_msg, F_CIB_EXISTING, existing);
}
if (update != NULL) {
add_message_xml(update_msg, F_CIB_UPDATE, update);
}
g_hash_table_foreach_remove(client_list, cib_notify_client, update_msg);
if (update == NULL) {
crm_trace("Performing operation %s (on section=%s)", op, type);
} else {
crm_trace("Performing %s on <%s%s%s>", op, type, id ? " id=" : "", id ? id : "");
}
free_xml(update_msg);
}
| DoS | 0 | cib_pre_notify(int options, const char *op, xmlNode * existing, xmlNode * update)
{
xmlNode *update_msg = NULL;
const char *type = NULL;
const char *id = NULL;
gboolean needed = FALSE;
g_hash_table_foreach(client_list, need_pre_notify, &needed);
if (needed == FALSE) {
return;
}
/* TODO: consider pre-notification for removal */
update_msg = create_xml_node(NULL, "pre-notify");
if (update != NULL) {
id = crm_element_value(update, XML_ATTR_ID);
}
crm_xml_add(update_msg, F_TYPE, T_CIB_NOTIFY);
crm_xml_add(update_msg, F_SUBTYPE, T_CIB_PRE_NOTIFY);
crm_xml_add(update_msg, F_CIB_OPERATION, op);
if (id != NULL) {
crm_xml_add(update_msg, F_CIB_OBJID, id);
}
if (update != NULL) {
crm_xml_add(update_msg, F_CIB_OBJTYPE, crm_element_name(update));
} else if (existing != NULL) {
crm_xml_add(update_msg, F_CIB_OBJTYPE, crm_element_name(existing));
}
type = crm_element_value(update_msg, F_CIB_OBJTYPE);
attach_cib_generation(update_msg, "cib_generation", the_cib);
if (existing != NULL) {
add_message_xml(update_msg, F_CIB_EXISTING, existing);
}
if (update != NULL) {
add_message_xml(update_msg, F_CIB_UPDATE, update);
}
g_hash_table_foreach_remove(client_list, cib_notify_client, update_msg);
if (update == NULL) {
crm_trace("Performing operation %s (on section=%s)", op, type);
} else {
crm_trace("Performing %s on <%s%s%s>", op, type, id ? " id=" : "", id ? id : "");
}
free_xml(update_msg);
}
| @@ -83,7 +83,7 @@ cib_notify_client(gpointer key, gpointer value, gpointer user_data)
CRM_CHECK(client != NULL, return TRUE);
CRM_CHECK(update_msg != NULL, return TRUE);
- if (client->ipc == NULL) {
+ if (client->ipc == NULL && client->session == NULL) {
crm_warn("Skipping client with NULL channel");
return FALSE;
} | CWE-399 | null | null |
27,179 | need_pre_notify(gpointer key, gpointer value, gpointer user_data)
{
cib_client_t *client = value;
if (client->pre_notify) {
gboolean *needed = user_data;
*needed = TRUE;
}
}
| DoS | 0 | need_pre_notify(gpointer key, gpointer value, gpointer user_data)
{
cib_client_t *client = value;
if (client->pre_notify) {
gboolean *needed = user_data;
*needed = TRUE;
}
}
| @@ -83,7 +83,7 @@ cib_notify_client(gpointer key, gpointer value, gpointer user_data)
CRM_CHECK(client != NULL, return TRUE);
CRM_CHECK(update_msg != NULL, return TRUE);
- if (client->ipc == NULL) {
+ if (client->ipc == NULL && client->session == NULL) {
crm_warn("Skipping client with NULL channel");
return FALSE;
} | CWE-399 | null | null |
27,180 | authenticate_user(const char *user, const char *passwd)
{
#ifndef HAVE_PAM
gboolean pass = TRUE;
#else
int rc = 0;
gboolean pass = FALSE;
const void *p_user = NULL;
struct pam_conv p_conv;
struct pam_handle *pam_h = NULL;
static const char *pam_name = NULL;
if (pam_name == NULL) {
pam_name = getenv("CIB_pam_service");
}
if (pam_name == NULL) {
pam_name = "login";
}
p_conv.conv = construct_pam_passwd;
p_conv.appdata_ptr = strdup(passwd);
rc = pam_start(pam_name, user, &p_conv, &pam_h);
if (rc != PAM_SUCCESS) {
crm_err("Could not initialize PAM: %s (%d)", pam_strerror(pam_h, rc), rc);
goto bail;
}
rc = pam_authenticate(pam_h, 0);
if (rc != PAM_SUCCESS) {
crm_err("Authentication failed for %s: %s (%d)", user, pam_strerror(pam_h, rc), rc);
goto bail;
}
/* Make sure we authenticated the user we wanted to authenticate.
* Since we also run as non-root, it might be worth pre-checking
* the user has the same EID as us, since that the only user we
* can authenticate.
*/
rc = pam_get_item(pam_h, PAM_USER, &p_user);
if (rc != PAM_SUCCESS) {
crm_err("Internal PAM error: %s (%d)", pam_strerror(pam_h, rc), rc);
goto bail;
} else if (p_user == NULL) {
crm_err("Unknown user authenticated.");
goto bail;
} else if (safe_str_neq(p_user, user)) {
crm_err("User mismatch: %s vs. %s.", (const char *)p_user, (const char *)user);
goto bail;
}
rc = pam_acct_mgmt(pam_h, 0);
if (rc != PAM_SUCCESS) {
crm_err("Access denied: %s (%d)", pam_strerror(pam_h, rc), rc);
goto bail;
}
pass = TRUE;
bail:
rc = pam_end(pam_h, rc);
#endif
return pass;
}
| DoS | 0 | authenticate_user(const char *user, const char *passwd)
{
#ifndef HAVE_PAM
gboolean pass = TRUE;
#else
int rc = 0;
gboolean pass = FALSE;
const void *p_user = NULL;
struct pam_conv p_conv;
struct pam_handle *pam_h = NULL;
static const char *pam_name = NULL;
if (pam_name == NULL) {
pam_name = getenv("CIB_pam_service");
}
if (pam_name == NULL) {
pam_name = "login";
}
p_conv.conv = construct_pam_passwd;
p_conv.appdata_ptr = strdup(passwd);
rc = pam_start(pam_name, user, &p_conv, &pam_h);
if (rc != PAM_SUCCESS) {
crm_err("Could not initialize PAM: %s (%d)", pam_strerror(pam_h, rc), rc);
goto bail;
}
rc = pam_authenticate(pam_h, 0);
if (rc != PAM_SUCCESS) {
crm_err("Authentication failed for %s: %s (%d)", user, pam_strerror(pam_h, rc), rc);
goto bail;
}
/* Make sure we authenticated the user we wanted to authenticate.
* Since we also run as non-root, it might be worth pre-checking
* the user has the same EID as us, since that the only user we
* can authenticate.
*/
rc = pam_get_item(pam_h, PAM_USER, &p_user);
if (rc != PAM_SUCCESS) {
crm_err("Internal PAM error: %s (%d)", pam_strerror(pam_h, rc), rc);
goto bail;
} else if (p_user == NULL) {
crm_err("Unknown user authenticated.");
goto bail;
} else if (safe_str_neq(p_user, user)) {
crm_err("User mismatch: %s vs. %s.", (const char *)p_user, (const char *)user);
goto bail;
}
rc = pam_acct_mgmt(pam_h, 0);
if (rc != PAM_SUCCESS) {
crm_err("Access denied: %s (%d)", pam_strerror(pam_h, rc), rc);
goto bail;
}
pass = TRUE;
bail:
rc = pam_end(pam_h, rc);
#endif
return pass;
}
| @@ -60,10 +60,6 @@
# endif
#endif
-#ifdef HAVE_DECL_NANOSLEEP
-# include <time.h>
-#endif
-
extern int remote_tls_fd;
extern gboolean cib_shutdown_flag;
@@ -73,17 +69,16 @@ void cib_remote_connection_destroy(gpointer user_data);
#ifdef HAVE_GNUTLS_GNUTLS_H
# define DH_BITS 1024
gnutls_dh_params dh_params;
-extern gnutls_anon_server_credentials anon_cred_s;
+gnutls_anon_server_credentials anon_cred_s;
static void
debug_log(int level, const char *str)
{
fputs(str, stderr);
}
-
-extern gnutls_session *create_tls_session(int csock, int type);
-
#endif
+#define REMOTE_AUTH_TIMEOUT 10000
+
int num_clients;
int authenticate_user(const char *user, const char *passwd);
int cib_remote_listen(gpointer data);
@@ -121,7 +116,7 @@ init_remote_listener(int port, gboolean encrypted)
#else
crm_notice("Starting a tls listener on port %d.", port);
gnutls_global_init();
-/* gnutls_global_set_log_level (10); */
+ /* gnutls_global_set_log_level (10); */
gnutls_global_set_log_function(debug_log);
gnutls_dh_params_init(&dh_params);
gnutls_dh_params_generate2(dh_params, DH_BITS);
@@ -215,37 +210,89 @@ check_group_membership(const char *usr, const char *grp)
return FALSE;
}
+static gboolean
+cib_remote_auth(xmlNode *login)
+{
+ const char *user = NULL;
+ const char *pass = NULL;
+ const char *tmp = NULL;
+
+ crm_log_xml_info(login, "Login: ");
+ if (login == NULL) {
+ return FALSE;
+ }
+
+ tmp = crm_element_name(login);
+ if (safe_str_neq(tmp, "cib_command")) {
+ crm_err("Wrong tag: %s", tmp);
+ return FALSE;
+ }
+
+ tmp = crm_element_value(login, "op");
+ if (safe_str_neq(tmp, "authenticate")) {
+ crm_err("Wrong operation: %s", tmp);
+ return FALSE;
+ }
+
+ user = crm_element_value(login, "user");
+ pass = crm_element_value(login, "password");
+
+ if (!user || !pass) {
+ crm_err("missing auth credentials");
+ return FALSE;
+ }
+
+ /* Non-root daemons can only validate the password of the
+ * user they're running as
+ */
+ if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
+ crm_err("User is not a member of the required group");
+ return FALSE;
+
+ } else if (authenticate_user(user, pass) == FALSE) {
+ crm_err("PAM auth failed");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+remote_auth_timeout_cb(gpointer data)
+{
+ cib_client_t *client = data;
+
+ client->remote_auth_timeout = 0;
+
+ if (client->remote_auth == TRUE) {
+ return FALSE;
+ }
+
+ mainloop_del_fd(client->remote);
+ crm_err("Remote client authentication timed out");
+
+ return FALSE;
+}
int
cib_remote_listen(gpointer data)
{
- int lpc = 0;
int csock = 0;
unsigned laddr;
- time_t now = 0;
- time_t start = time(NULL);
struct sockaddr_in addr;
int ssock = *(int *)data;
+ int flag;
#ifdef HAVE_GNUTLS_GNUTLS_H
gnutls_session *session = NULL;
#endif
cib_client_t *new_client = NULL;
- xmlNode *login = NULL;
- const char *user = NULL;
- const char *pass = NULL;
- const char *tmp = NULL;
-
-#ifdef HAVE_DECL_NANOSLEEP
- const struct timespec sleepfast = { 0, 10000000 }; /* 10 millisec */
-#endif
-
static struct mainloop_fd_callbacks remote_client_fd_callbacks =
{
.dispatch = cib_remote_msg,
.destroy = cib_remote_connection_destroy,
- };
-
+ };
+
/* accept the connection */
laddr = sizeof(addr);
csock = accept(ssock, (struct sockaddr *)&addr, &laddr);
@@ -257,10 +304,22 @@ cib_remote_listen(gpointer data)
return TRUE;
}
+ if ((flag = fcntl(csock, F_GETFL)) >= 0) {
+ if (fcntl(csock, F_SETFL, flag | O_NONBLOCK) < 0) {
+ crm_err( "fcntl() write failed");
+ close(csock);
+ return TRUE;
+ }
+ } else {
+ crm_err( "fcntl() read failed");
+ close(csock);
+ return TRUE;
+ }
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
/* create gnutls session for the server socket */
- session = create_tls_session(csock, GNUTLS_SERVER);
+ session = crm_create_anon_tls_session(csock, GNUTLS_SERVER, anon_cred_s);
if (session == NULL) {
crm_err("TLS session creation failed");
close(csock);
@@ -269,73 +328,13 @@ cib_remote_listen(gpointer data)
#endif
}
- do {
- crm_trace("Iter: %d", lpc++);
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- login = crm_recv_remote_msg(session, TRUE);
-#endif
- } else {
- login = crm_recv_remote_msg(GINT_TO_POINTER(csock), FALSE);
- }
- if (login != NULL) {
- break;
- }
-#ifdef HAVE_DECL_NANOSLEEP
- nanosleep(&sleepfast, NULL);
-#else
- sleep(1);
-#endif
- now = time(NULL);
-
- /* Peers have 3s to connect */
- } while (login == NULL && (start - now) < 4);
-
- crm_log_xml_info(login, "Login: ");
- if (login == NULL) {
- goto bail;
- }
-
- tmp = crm_element_name(login);
- if (safe_str_neq(tmp, "cib_command")) {
- crm_err("Wrong tag: %s", tmp);
- goto bail;
- }
-
- tmp = crm_element_value(login, "op");
- if (safe_str_neq(tmp, "authenticate")) {
- crm_err("Wrong operation: %s", tmp);
- goto bail;
- }
-
- user = crm_element_value(login, "user");
- pass = crm_element_value(login, "password");
-
- /* Non-root daemons can only validate the password of the
- * user they're running as
- */
- if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
- crm_err("User is not a member of the required group");
- goto bail;
-
- } else if (authenticate_user(user, pass) == FALSE) {
- crm_err("PAM auth failed");
- goto bail;
- }
-
- /* send ACK */
num_clients++;
new_client = calloc(1, sizeof(cib_client_t));
- new_client->name = crm_element_value_copy(login, "name");
-
- CRM_CHECK(new_client->id == NULL, free(new_client->id));
new_client->id = crm_generate_uuid();
-
-#if ENABLE_ACL
- new_client->user = strdup(user);
-#endif
-
new_client->callback_id = NULL;
+ /* clients have a few seconds to perform handshake. */
+ new_client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, new_client);
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
new_client->encrypted = TRUE;
@@ -345,37 +344,19 @@ cib_remote_listen(gpointer data)
new_client->session = GINT_TO_POINTER(csock);
}
- free_xml(login);
- login = create_xml_node(NULL, "cib_result");
- crm_xml_add(login, F_CIB_OPERATION, CRM_OP_REGISTER);
- crm_xml_add(login, F_CIB_CLIENTID, new_client->id);
- crm_send_remote_msg(new_client->session, login, new_client->encrypted);
- free_xml(login);
-
new_client->remote = mainloop_add_fd(
"cib-remote-client", G_PRIORITY_DEFAULT, csock, new_client, &remote_client_fd_callbacks);
g_hash_table_insert(client_list, new_client->id, new_client);
return TRUE;
-
- bail:
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_bye(*session, GNUTLS_SHUT_RDWR);
- gnutls_deinit(*session);
- gnutls_free(session);
-#endif
- }
- close(csock);
- free_xml(login);
- return TRUE;
}
void
cib_remote_connection_destroy(gpointer user_data)
{
cib_client_t *client = user_data;
+ int csock = 0;
if (client == NULL) {
return;
@@ -393,10 +374,36 @@ cib_remote_connection_destroy(gpointer user_data)
crm_trace("Destroying %s (%p)", client->name, user_data);
num_clients--;
crm_trace("Num unfree'd clients: %d", num_clients);
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+
+ if (client->encrypted) {
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->session) {
+ void *sock_ptr = gnutls_transport_get_ptr(*client->session);
+ csock = GPOINTER_TO_INT(sock_ptr);
+ if (client->handshake_complete) {
+ gnutls_bye(*client->session, GNUTLS_SHUT_WR);
+ }
+ gnutls_deinit(*client->session);
+ gnutls_free(client->session);
+ }
+#endif
+ } else {
+ csock = GPOINTER_TO_INT(client->session);
+ }
+ client->session = NULL;
+
+ if (csock > 0) {
+ close(csock);
+ }
+
free(client->name);
free(client->callback_id);
free(client->id);
free(client->user);
+ free(client->recv_buf);
free(client);
crm_trace("Freed the cib client");
@@ -406,24 +413,15 @@ cib_remote_connection_destroy(gpointer user_data)
return;
}
-int
-cib_remote_msg(gpointer data)
+static void
+cib_handle_remote_msg(cib_client_t *client, xmlNode *command)
{
const char *value = NULL;
- xmlNode *command = NULL;
- cib_client_t *client = data;
-
- crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
-
- command = crm_recv_remote_msg(client->session, client->encrypted);
- if (command == NULL) {
- return -1;
- }
value = crm_element_name(command);
if (safe_str_neq(value, "cib_command")) {
crm_log_xml_trace(command, "Bad command: ");
- goto bail;
+ return;
}
if (client->name == NULL) {
@@ -472,9 +470,95 @@ cib_remote_msg(gpointer data)
crm_log_xml_trace(command, "Remote command: ");
cib_common_callback_worker(0, 0, command, client, TRUE);
- bail:
- free_xml(command);
- command = NULL;
+}
+
+int
+cib_remote_msg(gpointer data)
+{
+ xmlNode *command = NULL;
+ cib_client_t *client = data;
+ int disconnected = 0;
+ int timeout = client->remote_auth ? -1 : 1000;
+
+ crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
+
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->encrypted && (client->handshake_complete == FALSE)) {
+ int rc = 0;
+
+ /* Muliple calls to handshake will be required, this callback
+ * will be invoked once the client sends more handshake data. */
+ do {
+ rc = gnutls_handshake(*client->session);
+
+ if (rc < 0 && rc != GNUTLS_E_AGAIN) {
+ crm_err("Remote cib tls handshake failed");
+ return -1;
+ }
+ } while (rc == GNUTLS_E_INTERRUPTED);
+
+ if (rc == 0) {
+ crm_debug("Remote cib tls handshake completed");
+ client->handshake_complete = TRUE;
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+ /* after handshake, clients must send auth in a few seconds */
+ client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, client);
+ }
+ return 0;
+ }
+#endif
+
+ crm_recv_remote_msg(client->session, &client->recv_buf, client->encrypted, timeout, &disconnected);
+
+ /* must pass auth before we will process anything else */
+ if (client->remote_auth == FALSE) {
+ xmlNode *reg;
+#if ENABLE_ACL
+ const char *user = NULL;
+#endif
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ if (cib_remote_auth(command) == FALSE) {
+ free_xml(command);
+ return -1;
+ }
+
+ crm_debug("remote connection authenticated successfully");
+ client->remote_auth = TRUE;
+ g_source_remove(client->remote_auth_timeout);
+ client->remote_auth_timeout = 0;
+ client->name = crm_element_value_copy(command, "name");
+
+#if ENABLE_ACL
+ user = crm_element_value(command, "user");
+ if (user) {
+ new_client->user = strdup(user);
+ }
+#endif
+
+ /* send ACK */
+ reg = create_xml_node(NULL, "cib_result");
+ crm_xml_add(reg, F_CIB_OPERATION, CRM_OP_REGISTER);
+ crm_xml_add(reg, F_CIB_CLIENTID, client->id);
+ crm_send_remote_msg(client->session, reg, client->encrypted);
+ free_xml(reg);
+ free_xml(command);
+ }
+
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ while (command) {
+ crm_trace("command received");
+ cib_handle_remote_msg(client, command);
+ free_xml(command);
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ }
+
+ if (disconnected) {
+ crm_trace("disconnected while receiving remote cib msg.");
+ return -1;
+ }
+
return 0;
}
| CWE-399 | null | null |
27,181 | check_group_membership(const char *usr, const char *grp)
{
int index = 0;
struct passwd *pwd = NULL;
struct group *group = NULL;
CRM_CHECK(usr != NULL, return FALSE);
CRM_CHECK(grp != NULL, return FALSE);
pwd = getpwnam(usr);
if (pwd == NULL) {
crm_err("No user named '%s' exists!", usr);
return FALSE;
}
group = getgrgid(pwd->pw_gid);
if (group != NULL && crm_str_eq(grp, group->gr_name, TRUE)) {
return TRUE;
}
group = getgrnam(grp);
if (group == NULL) {
crm_err("No group named '%s' exists!", grp);
return FALSE;
}
while (TRUE) {
char *member = group->gr_mem[index++];
if (member == NULL) {
break;
} else if (crm_str_eq(usr, member, TRUE)) {
return TRUE;
}
};
return FALSE;
}
| DoS | 0 | check_group_membership(const char *usr, const char *grp)
{
int index = 0;
struct passwd *pwd = NULL;
struct group *group = NULL;
CRM_CHECK(usr != NULL, return FALSE);
CRM_CHECK(grp != NULL, return FALSE);
pwd = getpwnam(usr);
if (pwd == NULL) {
crm_err("No user named '%s' exists!", usr);
return FALSE;
}
group = getgrgid(pwd->pw_gid);
if (group != NULL && crm_str_eq(grp, group->gr_name, TRUE)) {
return TRUE;
}
group = getgrnam(grp);
if (group == NULL) {
crm_err("No group named '%s' exists!", grp);
return FALSE;
}
while (TRUE) {
char *member = group->gr_mem[index++];
if (member == NULL) {
break;
} else if (crm_str_eq(usr, member, TRUE)) {
return TRUE;
}
};
return FALSE;
}
| @@ -60,10 +60,6 @@
# endif
#endif
-#ifdef HAVE_DECL_NANOSLEEP
-# include <time.h>
-#endif
-
extern int remote_tls_fd;
extern gboolean cib_shutdown_flag;
@@ -73,17 +69,16 @@ void cib_remote_connection_destroy(gpointer user_data);
#ifdef HAVE_GNUTLS_GNUTLS_H
# define DH_BITS 1024
gnutls_dh_params dh_params;
-extern gnutls_anon_server_credentials anon_cred_s;
+gnutls_anon_server_credentials anon_cred_s;
static void
debug_log(int level, const char *str)
{
fputs(str, stderr);
}
-
-extern gnutls_session *create_tls_session(int csock, int type);
-
#endif
+#define REMOTE_AUTH_TIMEOUT 10000
+
int num_clients;
int authenticate_user(const char *user, const char *passwd);
int cib_remote_listen(gpointer data);
@@ -121,7 +116,7 @@ init_remote_listener(int port, gboolean encrypted)
#else
crm_notice("Starting a tls listener on port %d.", port);
gnutls_global_init();
-/* gnutls_global_set_log_level (10); */
+ /* gnutls_global_set_log_level (10); */
gnutls_global_set_log_function(debug_log);
gnutls_dh_params_init(&dh_params);
gnutls_dh_params_generate2(dh_params, DH_BITS);
@@ -215,37 +210,89 @@ check_group_membership(const char *usr, const char *grp)
return FALSE;
}
+static gboolean
+cib_remote_auth(xmlNode *login)
+{
+ const char *user = NULL;
+ const char *pass = NULL;
+ const char *tmp = NULL;
+
+ crm_log_xml_info(login, "Login: ");
+ if (login == NULL) {
+ return FALSE;
+ }
+
+ tmp = crm_element_name(login);
+ if (safe_str_neq(tmp, "cib_command")) {
+ crm_err("Wrong tag: %s", tmp);
+ return FALSE;
+ }
+
+ tmp = crm_element_value(login, "op");
+ if (safe_str_neq(tmp, "authenticate")) {
+ crm_err("Wrong operation: %s", tmp);
+ return FALSE;
+ }
+
+ user = crm_element_value(login, "user");
+ pass = crm_element_value(login, "password");
+
+ if (!user || !pass) {
+ crm_err("missing auth credentials");
+ return FALSE;
+ }
+
+ /* Non-root daemons can only validate the password of the
+ * user they're running as
+ */
+ if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
+ crm_err("User is not a member of the required group");
+ return FALSE;
+
+ } else if (authenticate_user(user, pass) == FALSE) {
+ crm_err("PAM auth failed");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+remote_auth_timeout_cb(gpointer data)
+{
+ cib_client_t *client = data;
+
+ client->remote_auth_timeout = 0;
+
+ if (client->remote_auth == TRUE) {
+ return FALSE;
+ }
+
+ mainloop_del_fd(client->remote);
+ crm_err("Remote client authentication timed out");
+
+ return FALSE;
+}
int
cib_remote_listen(gpointer data)
{
- int lpc = 0;
int csock = 0;
unsigned laddr;
- time_t now = 0;
- time_t start = time(NULL);
struct sockaddr_in addr;
int ssock = *(int *)data;
+ int flag;
#ifdef HAVE_GNUTLS_GNUTLS_H
gnutls_session *session = NULL;
#endif
cib_client_t *new_client = NULL;
- xmlNode *login = NULL;
- const char *user = NULL;
- const char *pass = NULL;
- const char *tmp = NULL;
-
-#ifdef HAVE_DECL_NANOSLEEP
- const struct timespec sleepfast = { 0, 10000000 }; /* 10 millisec */
-#endif
-
static struct mainloop_fd_callbacks remote_client_fd_callbacks =
{
.dispatch = cib_remote_msg,
.destroy = cib_remote_connection_destroy,
- };
-
+ };
+
/* accept the connection */
laddr = sizeof(addr);
csock = accept(ssock, (struct sockaddr *)&addr, &laddr);
@@ -257,10 +304,22 @@ cib_remote_listen(gpointer data)
return TRUE;
}
+ if ((flag = fcntl(csock, F_GETFL)) >= 0) {
+ if (fcntl(csock, F_SETFL, flag | O_NONBLOCK) < 0) {
+ crm_err( "fcntl() write failed");
+ close(csock);
+ return TRUE;
+ }
+ } else {
+ crm_err( "fcntl() read failed");
+ close(csock);
+ return TRUE;
+ }
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
/* create gnutls session for the server socket */
- session = create_tls_session(csock, GNUTLS_SERVER);
+ session = crm_create_anon_tls_session(csock, GNUTLS_SERVER, anon_cred_s);
if (session == NULL) {
crm_err("TLS session creation failed");
close(csock);
@@ -269,73 +328,13 @@ cib_remote_listen(gpointer data)
#endif
}
- do {
- crm_trace("Iter: %d", lpc++);
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- login = crm_recv_remote_msg(session, TRUE);
-#endif
- } else {
- login = crm_recv_remote_msg(GINT_TO_POINTER(csock), FALSE);
- }
- if (login != NULL) {
- break;
- }
-#ifdef HAVE_DECL_NANOSLEEP
- nanosleep(&sleepfast, NULL);
-#else
- sleep(1);
-#endif
- now = time(NULL);
-
- /* Peers have 3s to connect */
- } while (login == NULL && (start - now) < 4);
-
- crm_log_xml_info(login, "Login: ");
- if (login == NULL) {
- goto bail;
- }
-
- tmp = crm_element_name(login);
- if (safe_str_neq(tmp, "cib_command")) {
- crm_err("Wrong tag: %s", tmp);
- goto bail;
- }
-
- tmp = crm_element_value(login, "op");
- if (safe_str_neq(tmp, "authenticate")) {
- crm_err("Wrong operation: %s", tmp);
- goto bail;
- }
-
- user = crm_element_value(login, "user");
- pass = crm_element_value(login, "password");
-
- /* Non-root daemons can only validate the password of the
- * user they're running as
- */
- if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
- crm_err("User is not a member of the required group");
- goto bail;
-
- } else if (authenticate_user(user, pass) == FALSE) {
- crm_err("PAM auth failed");
- goto bail;
- }
-
- /* send ACK */
num_clients++;
new_client = calloc(1, sizeof(cib_client_t));
- new_client->name = crm_element_value_copy(login, "name");
-
- CRM_CHECK(new_client->id == NULL, free(new_client->id));
new_client->id = crm_generate_uuid();
-
-#if ENABLE_ACL
- new_client->user = strdup(user);
-#endif
-
new_client->callback_id = NULL;
+ /* clients have a few seconds to perform handshake. */
+ new_client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, new_client);
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
new_client->encrypted = TRUE;
@@ -345,37 +344,19 @@ cib_remote_listen(gpointer data)
new_client->session = GINT_TO_POINTER(csock);
}
- free_xml(login);
- login = create_xml_node(NULL, "cib_result");
- crm_xml_add(login, F_CIB_OPERATION, CRM_OP_REGISTER);
- crm_xml_add(login, F_CIB_CLIENTID, new_client->id);
- crm_send_remote_msg(new_client->session, login, new_client->encrypted);
- free_xml(login);
-
new_client->remote = mainloop_add_fd(
"cib-remote-client", G_PRIORITY_DEFAULT, csock, new_client, &remote_client_fd_callbacks);
g_hash_table_insert(client_list, new_client->id, new_client);
return TRUE;
-
- bail:
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_bye(*session, GNUTLS_SHUT_RDWR);
- gnutls_deinit(*session);
- gnutls_free(session);
-#endif
- }
- close(csock);
- free_xml(login);
- return TRUE;
}
void
cib_remote_connection_destroy(gpointer user_data)
{
cib_client_t *client = user_data;
+ int csock = 0;
if (client == NULL) {
return;
@@ -393,10 +374,36 @@ cib_remote_connection_destroy(gpointer user_data)
crm_trace("Destroying %s (%p)", client->name, user_data);
num_clients--;
crm_trace("Num unfree'd clients: %d", num_clients);
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+
+ if (client->encrypted) {
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->session) {
+ void *sock_ptr = gnutls_transport_get_ptr(*client->session);
+ csock = GPOINTER_TO_INT(sock_ptr);
+ if (client->handshake_complete) {
+ gnutls_bye(*client->session, GNUTLS_SHUT_WR);
+ }
+ gnutls_deinit(*client->session);
+ gnutls_free(client->session);
+ }
+#endif
+ } else {
+ csock = GPOINTER_TO_INT(client->session);
+ }
+ client->session = NULL;
+
+ if (csock > 0) {
+ close(csock);
+ }
+
free(client->name);
free(client->callback_id);
free(client->id);
free(client->user);
+ free(client->recv_buf);
free(client);
crm_trace("Freed the cib client");
@@ -406,24 +413,15 @@ cib_remote_connection_destroy(gpointer user_data)
return;
}
-int
-cib_remote_msg(gpointer data)
+static void
+cib_handle_remote_msg(cib_client_t *client, xmlNode *command)
{
const char *value = NULL;
- xmlNode *command = NULL;
- cib_client_t *client = data;
-
- crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
-
- command = crm_recv_remote_msg(client->session, client->encrypted);
- if (command == NULL) {
- return -1;
- }
value = crm_element_name(command);
if (safe_str_neq(value, "cib_command")) {
crm_log_xml_trace(command, "Bad command: ");
- goto bail;
+ return;
}
if (client->name == NULL) {
@@ -472,9 +470,95 @@ cib_remote_msg(gpointer data)
crm_log_xml_trace(command, "Remote command: ");
cib_common_callback_worker(0, 0, command, client, TRUE);
- bail:
- free_xml(command);
- command = NULL;
+}
+
+int
+cib_remote_msg(gpointer data)
+{
+ xmlNode *command = NULL;
+ cib_client_t *client = data;
+ int disconnected = 0;
+ int timeout = client->remote_auth ? -1 : 1000;
+
+ crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
+
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->encrypted && (client->handshake_complete == FALSE)) {
+ int rc = 0;
+
+ /* Muliple calls to handshake will be required, this callback
+ * will be invoked once the client sends more handshake data. */
+ do {
+ rc = gnutls_handshake(*client->session);
+
+ if (rc < 0 && rc != GNUTLS_E_AGAIN) {
+ crm_err("Remote cib tls handshake failed");
+ return -1;
+ }
+ } while (rc == GNUTLS_E_INTERRUPTED);
+
+ if (rc == 0) {
+ crm_debug("Remote cib tls handshake completed");
+ client->handshake_complete = TRUE;
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+ /* after handshake, clients must send auth in a few seconds */
+ client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, client);
+ }
+ return 0;
+ }
+#endif
+
+ crm_recv_remote_msg(client->session, &client->recv_buf, client->encrypted, timeout, &disconnected);
+
+ /* must pass auth before we will process anything else */
+ if (client->remote_auth == FALSE) {
+ xmlNode *reg;
+#if ENABLE_ACL
+ const char *user = NULL;
+#endif
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ if (cib_remote_auth(command) == FALSE) {
+ free_xml(command);
+ return -1;
+ }
+
+ crm_debug("remote connection authenticated successfully");
+ client->remote_auth = TRUE;
+ g_source_remove(client->remote_auth_timeout);
+ client->remote_auth_timeout = 0;
+ client->name = crm_element_value_copy(command, "name");
+
+#if ENABLE_ACL
+ user = crm_element_value(command, "user");
+ if (user) {
+ new_client->user = strdup(user);
+ }
+#endif
+
+ /* send ACK */
+ reg = create_xml_node(NULL, "cib_result");
+ crm_xml_add(reg, F_CIB_OPERATION, CRM_OP_REGISTER);
+ crm_xml_add(reg, F_CIB_CLIENTID, client->id);
+ crm_send_remote_msg(client->session, reg, client->encrypted);
+ free_xml(reg);
+ free_xml(command);
+ }
+
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ while (command) {
+ crm_trace("command received");
+ cib_handle_remote_msg(client, command);
+ free_xml(command);
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ }
+
+ if (disconnected) {
+ crm_trace("disconnected while receiving remote cib msg.");
+ return -1;
+ }
+
return 0;
}
| CWE-399 | null | null |
27,182 | construct_pam_passwd(int num_msg, const struct pam_message **msg,
struct pam_response **response, void *data)
{
int count = 0;
struct pam_response *reply;
char *string = (char *)data;
CRM_CHECK(data, return PAM_CONV_ERR);
CRM_CHECK(num_msg == 1, return PAM_CONV_ERR); /* We only want to handle one message */
reply = calloc(1, sizeof(struct pam_response));
CRM_ASSERT(reply != NULL);
for (count = 0; count < num_msg; ++count) {
switch (msg[count]->msg_style) {
case PAM_TEXT_INFO:
crm_info("PAM: %s\n", msg[count]->msg);
break;
case PAM_PROMPT_ECHO_OFF:
case PAM_PROMPT_ECHO_ON:
reply[count].resp_retcode = 0;
reply[count].resp = string; /* We already made a copy */
case PAM_ERROR_MSG:
/* In theory we'd want to print this, but then
* we see the password prompt in the logs
*/
/* crm_err("PAM error: %s\n", msg[count]->msg); */
break;
default:
crm_err("Unhandled conversation type: %d", msg[count]->msg_style);
goto bail;
}
}
*response = reply;
reply = NULL;
return PAM_SUCCESS;
bail:
for (count = 0; count < num_msg; ++count) {
if (reply[count].resp != NULL) {
switch (msg[count]->msg_style) {
case PAM_PROMPT_ECHO_ON:
case PAM_PROMPT_ECHO_OFF:
/* Erase the data - it contained a password */
while (*(reply[count].resp)) {
*(reply[count].resp)++ = '\0';
}
free(reply[count].resp);
break;
}
reply[count].resp = NULL;
}
}
free(reply);
reply = NULL;
return PAM_CONV_ERR;
}
| DoS | 0 | construct_pam_passwd(int num_msg, const struct pam_message **msg,
struct pam_response **response, void *data)
{
int count = 0;
struct pam_response *reply;
char *string = (char *)data;
CRM_CHECK(data, return PAM_CONV_ERR);
CRM_CHECK(num_msg == 1, return PAM_CONV_ERR); /* We only want to handle one message */
reply = calloc(1, sizeof(struct pam_response));
CRM_ASSERT(reply != NULL);
for (count = 0; count < num_msg; ++count) {
switch (msg[count]->msg_style) {
case PAM_TEXT_INFO:
crm_info("PAM: %s\n", msg[count]->msg);
break;
case PAM_PROMPT_ECHO_OFF:
case PAM_PROMPT_ECHO_ON:
reply[count].resp_retcode = 0;
reply[count].resp = string; /* We already made a copy */
case PAM_ERROR_MSG:
/* In theory we'd want to print this, but then
* we see the password prompt in the logs
*/
/* crm_err("PAM error: %s\n", msg[count]->msg); */
break;
default:
crm_err("Unhandled conversation type: %d", msg[count]->msg_style);
goto bail;
}
}
*response = reply;
reply = NULL;
return PAM_SUCCESS;
bail:
for (count = 0; count < num_msg; ++count) {
if (reply[count].resp != NULL) {
switch (msg[count]->msg_style) {
case PAM_PROMPT_ECHO_ON:
case PAM_PROMPT_ECHO_OFF:
/* Erase the data - it contained a password */
while (*(reply[count].resp)) {
*(reply[count].resp)++ = '\0';
}
free(reply[count].resp);
break;
}
reply[count].resp = NULL;
}
}
free(reply);
reply = NULL;
return PAM_CONV_ERR;
}
| @@ -60,10 +60,6 @@
# endif
#endif
-#ifdef HAVE_DECL_NANOSLEEP
-# include <time.h>
-#endif
-
extern int remote_tls_fd;
extern gboolean cib_shutdown_flag;
@@ -73,17 +69,16 @@ void cib_remote_connection_destroy(gpointer user_data);
#ifdef HAVE_GNUTLS_GNUTLS_H
# define DH_BITS 1024
gnutls_dh_params dh_params;
-extern gnutls_anon_server_credentials anon_cred_s;
+gnutls_anon_server_credentials anon_cred_s;
static void
debug_log(int level, const char *str)
{
fputs(str, stderr);
}
-
-extern gnutls_session *create_tls_session(int csock, int type);
-
#endif
+#define REMOTE_AUTH_TIMEOUT 10000
+
int num_clients;
int authenticate_user(const char *user, const char *passwd);
int cib_remote_listen(gpointer data);
@@ -121,7 +116,7 @@ init_remote_listener(int port, gboolean encrypted)
#else
crm_notice("Starting a tls listener on port %d.", port);
gnutls_global_init();
-/* gnutls_global_set_log_level (10); */
+ /* gnutls_global_set_log_level (10); */
gnutls_global_set_log_function(debug_log);
gnutls_dh_params_init(&dh_params);
gnutls_dh_params_generate2(dh_params, DH_BITS);
@@ -215,37 +210,89 @@ check_group_membership(const char *usr, const char *grp)
return FALSE;
}
+static gboolean
+cib_remote_auth(xmlNode *login)
+{
+ const char *user = NULL;
+ const char *pass = NULL;
+ const char *tmp = NULL;
+
+ crm_log_xml_info(login, "Login: ");
+ if (login == NULL) {
+ return FALSE;
+ }
+
+ tmp = crm_element_name(login);
+ if (safe_str_neq(tmp, "cib_command")) {
+ crm_err("Wrong tag: %s", tmp);
+ return FALSE;
+ }
+
+ tmp = crm_element_value(login, "op");
+ if (safe_str_neq(tmp, "authenticate")) {
+ crm_err("Wrong operation: %s", tmp);
+ return FALSE;
+ }
+
+ user = crm_element_value(login, "user");
+ pass = crm_element_value(login, "password");
+
+ if (!user || !pass) {
+ crm_err("missing auth credentials");
+ return FALSE;
+ }
+
+ /* Non-root daemons can only validate the password of the
+ * user they're running as
+ */
+ if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
+ crm_err("User is not a member of the required group");
+ return FALSE;
+
+ } else if (authenticate_user(user, pass) == FALSE) {
+ crm_err("PAM auth failed");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+remote_auth_timeout_cb(gpointer data)
+{
+ cib_client_t *client = data;
+
+ client->remote_auth_timeout = 0;
+
+ if (client->remote_auth == TRUE) {
+ return FALSE;
+ }
+
+ mainloop_del_fd(client->remote);
+ crm_err("Remote client authentication timed out");
+
+ return FALSE;
+}
int
cib_remote_listen(gpointer data)
{
- int lpc = 0;
int csock = 0;
unsigned laddr;
- time_t now = 0;
- time_t start = time(NULL);
struct sockaddr_in addr;
int ssock = *(int *)data;
+ int flag;
#ifdef HAVE_GNUTLS_GNUTLS_H
gnutls_session *session = NULL;
#endif
cib_client_t *new_client = NULL;
- xmlNode *login = NULL;
- const char *user = NULL;
- const char *pass = NULL;
- const char *tmp = NULL;
-
-#ifdef HAVE_DECL_NANOSLEEP
- const struct timespec sleepfast = { 0, 10000000 }; /* 10 millisec */
-#endif
-
static struct mainloop_fd_callbacks remote_client_fd_callbacks =
{
.dispatch = cib_remote_msg,
.destroy = cib_remote_connection_destroy,
- };
-
+ };
+
/* accept the connection */
laddr = sizeof(addr);
csock = accept(ssock, (struct sockaddr *)&addr, &laddr);
@@ -257,10 +304,22 @@ cib_remote_listen(gpointer data)
return TRUE;
}
+ if ((flag = fcntl(csock, F_GETFL)) >= 0) {
+ if (fcntl(csock, F_SETFL, flag | O_NONBLOCK) < 0) {
+ crm_err( "fcntl() write failed");
+ close(csock);
+ return TRUE;
+ }
+ } else {
+ crm_err( "fcntl() read failed");
+ close(csock);
+ return TRUE;
+ }
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
/* create gnutls session for the server socket */
- session = create_tls_session(csock, GNUTLS_SERVER);
+ session = crm_create_anon_tls_session(csock, GNUTLS_SERVER, anon_cred_s);
if (session == NULL) {
crm_err("TLS session creation failed");
close(csock);
@@ -269,73 +328,13 @@ cib_remote_listen(gpointer data)
#endif
}
- do {
- crm_trace("Iter: %d", lpc++);
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- login = crm_recv_remote_msg(session, TRUE);
-#endif
- } else {
- login = crm_recv_remote_msg(GINT_TO_POINTER(csock), FALSE);
- }
- if (login != NULL) {
- break;
- }
-#ifdef HAVE_DECL_NANOSLEEP
- nanosleep(&sleepfast, NULL);
-#else
- sleep(1);
-#endif
- now = time(NULL);
-
- /* Peers have 3s to connect */
- } while (login == NULL && (start - now) < 4);
-
- crm_log_xml_info(login, "Login: ");
- if (login == NULL) {
- goto bail;
- }
-
- tmp = crm_element_name(login);
- if (safe_str_neq(tmp, "cib_command")) {
- crm_err("Wrong tag: %s", tmp);
- goto bail;
- }
-
- tmp = crm_element_value(login, "op");
- if (safe_str_neq(tmp, "authenticate")) {
- crm_err("Wrong operation: %s", tmp);
- goto bail;
- }
-
- user = crm_element_value(login, "user");
- pass = crm_element_value(login, "password");
-
- /* Non-root daemons can only validate the password of the
- * user they're running as
- */
- if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
- crm_err("User is not a member of the required group");
- goto bail;
-
- } else if (authenticate_user(user, pass) == FALSE) {
- crm_err("PAM auth failed");
- goto bail;
- }
-
- /* send ACK */
num_clients++;
new_client = calloc(1, sizeof(cib_client_t));
- new_client->name = crm_element_value_copy(login, "name");
-
- CRM_CHECK(new_client->id == NULL, free(new_client->id));
new_client->id = crm_generate_uuid();
-
-#if ENABLE_ACL
- new_client->user = strdup(user);
-#endif
-
new_client->callback_id = NULL;
+ /* clients have a few seconds to perform handshake. */
+ new_client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, new_client);
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
new_client->encrypted = TRUE;
@@ -345,37 +344,19 @@ cib_remote_listen(gpointer data)
new_client->session = GINT_TO_POINTER(csock);
}
- free_xml(login);
- login = create_xml_node(NULL, "cib_result");
- crm_xml_add(login, F_CIB_OPERATION, CRM_OP_REGISTER);
- crm_xml_add(login, F_CIB_CLIENTID, new_client->id);
- crm_send_remote_msg(new_client->session, login, new_client->encrypted);
- free_xml(login);
-
new_client->remote = mainloop_add_fd(
"cib-remote-client", G_PRIORITY_DEFAULT, csock, new_client, &remote_client_fd_callbacks);
g_hash_table_insert(client_list, new_client->id, new_client);
return TRUE;
-
- bail:
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_bye(*session, GNUTLS_SHUT_RDWR);
- gnutls_deinit(*session);
- gnutls_free(session);
-#endif
- }
- close(csock);
- free_xml(login);
- return TRUE;
}
void
cib_remote_connection_destroy(gpointer user_data)
{
cib_client_t *client = user_data;
+ int csock = 0;
if (client == NULL) {
return;
@@ -393,10 +374,36 @@ cib_remote_connection_destroy(gpointer user_data)
crm_trace("Destroying %s (%p)", client->name, user_data);
num_clients--;
crm_trace("Num unfree'd clients: %d", num_clients);
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+
+ if (client->encrypted) {
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->session) {
+ void *sock_ptr = gnutls_transport_get_ptr(*client->session);
+ csock = GPOINTER_TO_INT(sock_ptr);
+ if (client->handshake_complete) {
+ gnutls_bye(*client->session, GNUTLS_SHUT_WR);
+ }
+ gnutls_deinit(*client->session);
+ gnutls_free(client->session);
+ }
+#endif
+ } else {
+ csock = GPOINTER_TO_INT(client->session);
+ }
+ client->session = NULL;
+
+ if (csock > 0) {
+ close(csock);
+ }
+
free(client->name);
free(client->callback_id);
free(client->id);
free(client->user);
+ free(client->recv_buf);
free(client);
crm_trace("Freed the cib client");
@@ -406,24 +413,15 @@ cib_remote_connection_destroy(gpointer user_data)
return;
}
-int
-cib_remote_msg(gpointer data)
+static void
+cib_handle_remote_msg(cib_client_t *client, xmlNode *command)
{
const char *value = NULL;
- xmlNode *command = NULL;
- cib_client_t *client = data;
-
- crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
-
- command = crm_recv_remote_msg(client->session, client->encrypted);
- if (command == NULL) {
- return -1;
- }
value = crm_element_name(command);
if (safe_str_neq(value, "cib_command")) {
crm_log_xml_trace(command, "Bad command: ");
- goto bail;
+ return;
}
if (client->name == NULL) {
@@ -472,9 +470,95 @@ cib_remote_msg(gpointer data)
crm_log_xml_trace(command, "Remote command: ");
cib_common_callback_worker(0, 0, command, client, TRUE);
- bail:
- free_xml(command);
- command = NULL;
+}
+
+int
+cib_remote_msg(gpointer data)
+{
+ xmlNode *command = NULL;
+ cib_client_t *client = data;
+ int disconnected = 0;
+ int timeout = client->remote_auth ? -1 : 1000;
+
+ crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
+
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->encrypted && (client->handshake_complete == FALSE)) {
+ int rc = 0;
+
+ /* Muliple calls to handshake will be required, this callback
+ * will be invoked once the client sends more handshake data. */
+ do {
+ rc = gnutls_handshake(*client->session);
+
+ if (rc < 0 && rc != GNUTLS_E_AGAIN) {
+ crm_err("Remote cib tls handshake failed");
+ return -1;
+ }
+ } while (rc == GNUTLS_E_INTERRUPTED);
+
+ if (rc == 0) {
+ crm_debug("Remote cib tls handshake completed");
+ client->handshake_complete = TRUE;
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+ /* after handshake, clients must send auth in a few seconds */
+ client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, client);
+ }
+ return 0;
+ }
+#endif
+
+ crm_recv_remote_msg(client->session, &client->recv_buf, client->encrypted, timeout, &disconnected);
+
+ /* must pass auth before we will process anything else */
+ if (client->remote_auth == FALSE) {
+ xmlNode *reg;
+#if ENABLE_ACL
+ const char *user = NULL;
+#endif
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ if (cib_remote_auth(command) == FALSE) {
+ free_xml(command);
+ return -1;
+ }
+
+ crm_debug("remote connection authenticated successfully");
+ client->remote_auth = TRUE;
+ g_source_remove(client->remote_auth_timeout);
+ client->remote_auth_timeout = 0;
+ client->name = crm_element_value_copy(command, "name");
+
+#if ENABLE_ACL
+ user = crm_element_value(command, "user");
+ if (user) {
+ new_client->user = strdup(user);
+ }
+#endif
+
+ /* send ACK */
+ reg = create_xml_node(NULL, "cib_result");
+ crm_xml_add(reg, F_CIB_OPERATION, CRM_OP_REGISTER);
+ crm_xml_add(reg, F_CIB_CLIENTID, client->id);
+ crm_send_remote_msg(client->session, reg, client->encrypted);
+ free_xml(reg);
+ free_xml(command);
+ }
+
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ while (command) {
+ crm_trace("command received");
+ cib_handle_remote_msg(client, command);
+ free_xml(command);
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ }
+
+ if (disconnected) {
+ crm_trace("disconnected while receiving remote cib msg.");
+ return -1;
+ }
+
return 0;
}
| CWE-399 | null | null |
27,183 | debug_log(int level, const char *str)
{
fputs(str, stderr);
}
| DoS | 0 | debug_log(int level, const char *str)
{
fputs(str, stderr);
}
| @@ -60,10 +60,6 @@
# endif
#endif
-#ifdef HAVE_DECL_NANOSLEEP
-# include <time.h>
-#endif
-
extern int remote_tls_fd;
extern gboolean cib_shutdown_flag;
@@ -73,17 +69,16 @@ void cib_remote_connection_destroy(gpointer user_data);
#ifdef HAVE_GNUTLS_GNUTLS_H
# define DH_BITS 1024
gnutls_dh_params dh_params;
-extern gnutls_anon_server_credentials anon_cred_s;
+gnutls_anon_server_credentials anon_cred_s;
static void
debug_log(int level, const char *str)
{
fputs(str, stderr);
}
-
-extern gnutls_session *create_tls_session(int csock, int type);
-
#endif
+#define REMOTE_AUTH_TIMEOUT 10000
+
int num_clients;
int authenticate_user(const char *user, const char *passwd);
int cib_remote_listen(gpointer data);
@@ -121,7 +116,7 @@ init_remote_listener(int port, gboolean encrypted)
#else
crm_notice("Starting a tls listener on port %d.", port);
gnutls_global_init();
-/* gnutls_global_set_log_level (10); */
+ /* gnutls_global_set_log_level (10); */
gnutls_global_set_log_function(debug_log);
gnutls_dh_params_init(&dh_params);
gnutls_dh_params_generate2(dh_params, DH_BITS);
@@ -215,37 +210,89 @@ check_group_membership(const char *usr, const char *grp)
return FALSE;
}
+static gboolean
+cib_remote_auth(xmlNode *login)
+{
+ const char *user = NULL;
+ const char *pass = NULL;
+ const char *tmp = NULL;
+
+ crm_log_xml_info(login, "Login: ");
+ if (login == NULL) {
+ return FALSE;
+ }
+
+ tmp = crm_element_name(login);
+ if (safe_str_neq(tmp, "cib_command")) {
+ crm_err("Wrong tag: %s", tmp);
+ return FALSE;
+ }
+
+ tmp = crm_element_value(login, "op");
+ if (safe_str_neq(tmp, "authenticate")) {
+ crm_err("Wrong operation: %s", tmp);
+ return FALSE;
+ }
+
+ user = crm_element_value(login, "user");
+ pass = crm_element_value(login, "password");
+
+ if (!user || !pass) {
+ crm_err("missing auth credentials");
+ return FALSE;
+ }
+
+ /* Non-root daemons can only validate the password of the
+ * user they're running as
+ */
+ if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
+ crm_err("User is not a member of the required group");
+ return FALSE;
+
+ } else if (authenticate_user(user, pass) == FALSE) {
+ crm_err("PAM auth failed");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+remote_auth_timeout_cb(gpointer data)
+{
+ cib_client_t *client = data;
+
+ client->remote_auth_timeout = 0;
+
+ if (client->remote_auth == TRUE) {
+ return FALSE;
+ }
+
+ mainloop_del_fd(client->remote);
+ crm_err("Remote client authentication timed out");
+
+ return FALSE;
+}
int
cib_remote_listen(gpointer data)
{
- int lpc = 0;
int csock = 0;
unsigned laddr;
- time_t now = 0;
- time_t start = time(NULL);
struct sockaddr_in addr;
int ssock = *(int *)data;
+ int flag;
#ifdef HAVE_GNUTLS_GNUTLS_H
gnutls_session *session = NULL;
#endif
cib_client_t *new_client = NULL;
- xmlNode *login = NULL;
- const char *user = NULL;
- const char *pass = NULL;
- const char *tmp = NULL;
-
-#ifdef HAVE_DECL_NANOSLEEP
- const struct timespec sleepfast = { 0, 10000000 }; /* 10 millisec */
-#endif
-
static struct mainloop_fd_callbacks remote_client_fd_callbacks =
{
.dispatch = cib_remote_msg,
.destroy = cib_remote_connection_destroy,
- };
-
+ };
+
/* accept the connection */
laddr = sizeof(addr);
csock = accept(ssock, (struct sockaddr *)&addr, &laddr);
@@ -257,10 +304,22 @@ cib_remote_listen(gpointer data)
return TRUE;
}
+ if ((flag = fcntl(csock, F_GETFL)) >= 0) {
+ if (fcntl(csock, F_SETFL, flag | O_NONBLOCK) < 0) {
+ crm_err( "fcntl() write failed");
+ close(csock);
+ return TRUE;
+ }
+ } else {
+ crm_err( "fcntl() read failed");
+ close(csock);
+ return TRUE;
+ }
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
/* create gnutls session for the server socket */
- session = create_tls_session(csock, GNUTLS_SERVER);
+ session = crm_create_anon_tls_session(csock, GNUTLS_SERVER, anon_cred_s);
if (session == NULL) {
crm_err("TLS session creation failed");
close(csock);
@@ -269,73 +328,13 @@ cib_remote_listen(gpointer data)
#endif
}
- do {
- crm_trace("Iter: %d", lpc++);
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- login = crm_recv_remote_msg(session, TRUE);
-#endif
- } else {
- login = crm_recv_remote_msg(GINT_TO_POINTER(csock), FALSE);
- }
- if (login != NULL) {
- break;
- }
-#ifdef HAVE_DECL_NANOSLEEP
- nanosleep(&sleepfast, NULL);
-#else
- sleep(1);
-#endif
- now = time(NULL);
-
- /* Peers have 3s to connect */
- } while (login == NULL && (start - now) < 4);
-
- crm_log_xml_info(login, "Login: ");
- if (login == NULL) {
- goto bail;
- }
-
- tmp = crm_element_name(login);
- if (safe_str_neq(tmp, "cib_command")) {
- crm_err("Wrong tag: %s", tmp);
- goto bail;
- }
-
- tmp = crm_element_value(login, "op");
- if (safe_str_neq(tmp, "authenticate")) {
- crm_err("Wrong operation: %s", tmp);
- goto bail;
- }
-
- user = crm_element_value(login, "user");
- pass = crm_element_value(login, "password");
-
- /* Non-root daemons can only validate the password of the
- * user they're running as
- */
- if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
- crm_err("User is not a member of the required group");
- goto bail;
-
- } else if (authenticate_user(user, pass) == FALSE) {
- crm_err("PAM auth failed");
- goto bail;
- }
-
- /* send ACK */
num_clients++;
new_client = calloc(1, sizeof(cib_client_t));
- new_client->name = crm_element_value_copy(login, "name");
-
- CRM_CHECK(new_client->id == NULL, free(new_client->id));
new_client->id = crm_generate_uuid();
-
-#if ENABLE_ACL
- new_client->user = strdup(user);
-#endif
-
new_client->callback_id = NULL;
+ /* clients have a few seconds to perform handshake. */
+ new_client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, new_client);
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
new_client->encrypted = TRUE;
@@ -345,37 +344,19 @@ cib_remote_listen(gpointer data)
new_client->session = GINT_TO_POINTER(csock);
}
- free_xml(login);
- login = create_xml_node(NULL, "cib_result");
- crm_xml_add(login, F_CIB_OPERATION, CRM_OP_REGISTER);
- crm_xml_add(login, F_CIB_CLIENTID, new_client->id);
- crm_send_remote_msg(new_client->session, login, new_client->encrypted);
- free_xml(login);
-
new_client->remote = mainloop_add_fd(
"cib-remote-client", G_PRIORITY_DEFAULT, csock, new_client, &remote_client_fd_callbacks);
g_hash_table_insert(client_list, new_client->id, new_client);
return TRUE;
-
- bail:
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_bye(*session, GNUTLS_SHUT_RDWR);
- gnutls_deinit(*session);
- gnutls_free(session);
-#endif
- }
- close(csock);
- free_xml(login);
- return TRUE;
}
void
cib_remote_connection_destroy(gpointer user_data)
{
cib_client_t *client = user_data;
+ int csock = 0;
if (client == NULL) {
return;
@@ -393,10 +374,36 @@ cib_remote_connection_destroy(gpointer user_data)
crm_trace("Destroying %s (%p)", client->name, user_data);
num_clients--;
crm_trace("Num unfree'd clients: %d", num_clients);
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+
+ if (client->encrypted) {
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->session) {
+ void *sock_ptr = gnutls_transport_get_ptr(*client->session);
+ csock = GPOINTER_TO_INT(sock_ptr);
+ if (client->handshake_complete) {
+ gnutls_bye(*client->session, GNUTLS_SHUT_WR);
+ }
+ gnutls_deinit(*client->session);
+ gnutls_free(client->session);
+ }
+#endif
+ } else {
+ csock = GPOINTER_TO_INT(client->session);
+ }
+ client->session = NULL;
+
+ if (csock > 0) {
+ close(csock);
+ }
+
free(client->name);
free(client->callback_id);
free(client->id);
free(client->user);
+ free(client->recv_buf);
free(client);
crm_trace("Freed the cib client");
@@ -406,24 +413,15 @@ cib_remote_connection_destroy(gpointer user_data)
return;
}
-int
-cib_remote_msg(gpointer data)
+static void
+cib_handle_remote_msg(cib_client_t *client, xmlNode *command)
{
const char *value = NULL;
- xmlNode *command = NULL;
- cib_client_t *client = data;
-
- crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
-
- command = crm_recv_remote_msg(client->session, client->encrypted);
- if (command == NULL) {
- return -1;
- }
value = crm_element_name(command);
if (safe_str_neq(value, "cib_command")) {
crm_log_xml_trace(command, "Bad command: ");
- goto bail;
+ return;
}
if (client->name == NULL) {
@@ -472,9 +470,95 @@ cib_remote_msg(gpointer data)
crm_log_xml_trace(command, "Remote command: ");
cib_common_callback_worker(0, 0, command, client, TRUE);
- bail:
- free_xml(command);
- command = NULL;
+}
+
+int
+cib_remote_msg(gpointer data)
+{
+ xmlNode *command = NULL;
+ cib_client_t *client = data;
+ int disconnected = 0;
+ int timeout = client->remote_auth ? -1 : 1000;
+
+ crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
+
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->encrypted && (client->handshake_complete == FALSE)) {
+ int rc = 0;
+
+ /* Muliple calls to handshake will be required, this callback
+ * will be invoked once the client sends more handshake data. */
+ do {
+ rc = gnutls_handshake(*client->session);
+
+ if (rc < 0 && rc != GNUTLS_E_AGAIN) {
+ crm_err("Remote cib tls handshake failed");
+ return -1;
+ }
+ } while (rc == GNUTLS_E_INTERRUPTED);
+
+ if (rc == 0) {
+ crm_debug("Remote cib tls handshake completed");
+ client->handshake_complete = TRUE;
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+ /* after handshake, clients must send auth in a few seconds */
+ client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, client);
+ }
+ return 0;
+ }
+#endif
+
+ crm_recv_remote_msg(client->session, &client->recv_buf, client->encrypted, timeout, &disconnected);
+
+ /* must pass auth before we will process anything else */
+ if (client->remote_auth == FALSE) {
+ xmlNode *reg;
+#if ENABLE_ACL
+ const char *user = NULL;
+#endif
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ if (cib_remote_auth(command) == FALSE) {
+ free_xml(command);
+ return -1;
+ }
+
+ crm_debug("remote connection authenticated successfully");
+ client->remote_auth = TRUE;
+ g_source_remove(client->remote_auth_timeout);
+ client->remote_auth_timeout = 0;
+ client->name = crm_element_value_copy(command, "name");
+
+#if ENABLE_ACL
+ user = crm_element_value(command, "user");
+ if (user) {
+ new_client->user = strdup(user);
+ }
+#endif
+
+ /* send ACK */
+ reg = create_xml_node(NULL, "cib_result");
+ crm_xml_add(reg, F_CIB_OPERATION, CRM_OP_REGISTER);
+ crm_xml_add(reg, F_CIB_CLIENTID, client->id);
+ crm_send_remote_msg(client->session, reg, client->encrypted);
+ free_xml(reg);
+ free_xml(command);
+ }
+
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ while (command) {
+ crm_trace("command received");
+ cib_handle_remote_msg(client, command);
+ free_xml(command);
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ }
+
+ if (disconnected) {
+ crm_trace("disconnected while receiving remote cib msg.");
+ return -1;
+ }
+
return 0;
}
| CWE-399 | null | null |
27,184 | remote_connection_destroy(gpointer user_data)
{
return;
}
| DoS | 0 | remote_connection_destroy(gpointer user_data)
{
return;
}
| @@ -60,10 +60,6 @@
# endif
#endif
-#ifdef HAVE_DECL_NANOSLEEP
-# include <time.h>
-#endif
-
extern int remote_tls_fd;
extern gboolean cib_shutdown_flag;
@@ -73,17 +69,16 @@ void cib_remote_connection_destroy(gpointer user_data);
#ifdef HAVE_GNUTLS_GNUTLS_H
# define DH_BITS 1024
gnutls_dh_params dh_params;
-extern gnutls_anon_server_credentials anon_cred_s;
+gnutls_anon_server_credentials anon_cred_s;
static void
debug_log(int level, const char *str)
{
fputs(str, stderr);
}
-
-extern gnutls_session *create_tls_session(int csock, int type);
-
#endif
+#define REMOTE_AUTH_TIMEOUT 10000
+
int num_clients;
int authenticate_user(const char *user, const char *passwd);
int cib_remote_listen(gpointer data);
@@ -121,7 +116,7 @@ init_remote_listener(int port, gboolean encrypted)
#else
crm_notice("Starting a tls listener on port %d.", port);
gnutls_global_init();
-/* gnutls_global_set_log_level (10); */
+ /* gnutls_global_set_log_level (10); */
gnutls_global_set_log_function(debug_log);
gnutls_dh_params_init(&dh_params);
gnutls_dh_params_generate2(dh_params, DH_BITS);
@@ -215,37 +210,89 @@ check_group_membership(const char *usr, const char *grp)
return FALSE;
}
+static gboolean
+cib_remote_auth(xmlNode *login)
+{
+ const char *user = NULL;
+ const char *pass = NULL;
+ const char *tmp = NULL;
+
+ crm_log_xml_info(login, "Login: ");
+ if (login == NULL) {
+ return FALSE;
+ }
+
+ tmp = crm_element_name(login);
+ if (safe_str_neq(tmp, "cib_command")) {
+ crm_err("Wrong tag: %s", tmp);
+ return FALSE;
+ }
+
+ tmp = crm_element_value(login, "op");
+ if (safe_str_neq(tmp, "authenticate")) {
+ crm_err("Wrong operation: %s", tmp);
+ return FALSE;
+ }
+
+ user = crm_element_value(login, "user");
+ pass = crm_element_value(login, "password");
+
+ if (!user || !pass) {
+ crm_err("missing auth credentials");
+ return FALSE;
+ }
+
+ /* Non-root daemons can only validate the password of the
+ * user they're running as
+ */
+ if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
+ crm_err("User is not a member of the required group");
+ return FALSE;
+
+ } else if (authenticate_user(user, pass) == FALSE) {
+ crm_err("PAM auth failed");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+remote_auth_timeout_cb(gpointer data)
+{
+ cib_client_t *client = data;
+
+ client->remote_auth_timeout = 0;
+
+ if (client->remote_auth == TRUE) {
+ return FALSE;
+ }
+
+ mainloop_del_fd(client->remote);
+ crm_err("Remote client authentication timed out");
+
+ return FALSE;
+}
int
cib_remote_listen(gpointer data)
{
- int lpc = 0;
int csock = 0;
unsigned laddr;
- time_t now = 0;
- time_t start = time(NULL);
struct sockaddr_in addr;
int ssock = *(int *)data;
+ int flag;
#ifdef HAVE_GNUTLS_GNUTLS_H
gnutls_session *session = NULL;
#endif
cib_client_t *new_client = NULL;
- xmlNode *login = NULL;
- const char *user = NULL;
- const char *pass = NULL;
- const char *tmp = NULL;
-
-#ifdef HAVE_DECL_NANOSLEEP
- const struct timespec sleepfast = { 0, 10000000 }; /* 10 millisec */
-#endif
-
static struct mainloop_fd_callbacks remote_client_fd_callbacks =
{
.dispatch = cib_remote_msg,
.destroy = cib_remote_connection_destroy,
- };
-
+ };
+
/* accept the connection */
laddr = sizeof(addr);
csock = accept(ssock, (struct sockaddr *)&addr, &laddr);
@@ -257,10 +304,22 @@ cib_remote_listen(gpointer data)
return TRUE;
}
+ if ((flag = fcntl(csock, F_GETFL)) >= 0) {
+ if (fcntl(csock, F_SETFL, flag | O_NONBLOCK) < 0) {
+ crm_err( "fcntl() write failed");
+ close(csock);
+ return TRUE;
+ }
+ } else {
+ crm_err( "fcntl() read failed");
+ close(csock);
+ return TRUE;
+ }
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
/* create gnutls session for the server socket */
- session = create_tls_session(csock, GNUTLS_SERVER);
+ session = crm_create_anon_tls_session(csock, GNUTLS_SERVER, anon_cred_s);
if (session == NULL) {
crm_err("TLS session creation failed");
close(csock);
@@ -269,73 +328,13 @@ cib_remote_listen(gpointer data)
#endif
}
- do {
- crm_trace("Iter: %d", lpc++);
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- login = crm_recv_remote_msg(session, TRUE);
-#endif
- } else {
- login = crm_recv_remote_msg(GINT_TO_POINTER(csock), FALSE);
- }
- if (login != NULL) {
- break;
- }
-#ifdef HAVE_DECL_NANOSLEEP
- nanosleep(&sleepfast, NULL);
-#else
- sleep(1);
-#endif
- now = time(NULL);
-
- /* Peers have 3s to connect */
- } while (login == NULL && (start - now) < 4);
-
- crm_log_xml_info(login, "Login: ");
- if (login == NULL) {
- goto bail;
- }
-
- tmp = crm_element_name(login);
- if (safe_str_neq(tmp, "cib_command")) {
- crm_err("Wrong tag: %s", tmp);
- goto bail;
- }
-
- tmp = crm_element_value(login, "op");
- if (safe_str_neq(tmp, "authenticate")) {
- crm_err("Wrong operation: %s", tmp);
- goto bail;
- }
-
- user = crm_element_value(login, "user");
- pass = crm_element_value(login, "password");
-
- /* Non-root daemons can only validate the password of the
- * user they're running as
- */
- if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
- crm_err("User is not a member of the required group");
- goto bail;
-
- } else if (authenticate_user(user, pass) == FALSE) {
- crm_err("PAM auth failed");
- goto bail;
- }
-
- /* send ACK */
num_clients++;
new_client = calloc(1, sizeof(cib_client_t));
- new_client->name = crm_element_value_copy(login, "name");
-
- CRM_CHECK(new_client->id == NULL, free(new_client->id));
new_client->id = crm_generate_uuid();
-
-#if ENABLE_ACL
- new_client->user = strdup(user);
-#endif
-
new_client->callback_id = NULL;
+ /* clients have a few seconds to perform handshake. */
+ new_client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, new_client);
+
if (ssock == remote_tls_fd) {
#ifdef HAVE_GNUTLS_GNUTLS_H
new_client->encrypted = TRUE;
@@ -345,37 +344,19 @@ cib_remote_listen(gpointer data)
new_client->session = GINT_TO_POINTER(csock);
}
- free_xml(login);
- login = create_xml_node(NULL, "cib_result");
- crm_xml_add(login, F_CIB_OPERATION, CRM_OP_REGISTER);
- crm_xml_add(login, F_CIB_CLIENTID, new_client->id);
- crm_send_remote_msg(new_client->session, login, new_client->encrypted);
- free_xml(login);
-
new_client->remote = mainloop_add_fd(
"cib-remote-client", G_PRIORITY_DEFAULT, csock, new_client, &remote_client_fd_callbacks);
g_hash_table_insert(client_list, new_client->id, new_client);
return TRUE;
-
- bail:
- if (ssock == remote_tls_fd) {
-#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_bye(*session, GNUTLS_SHUT_RDWR);
- gnutls_deinit(*session);
- gnutls_free(session);
-#endif
- }
- close(csock);
- free_xml(login);
- return TRUE;
}
void
cib_remote_connection_destroy(gpointer user_data)
{
cib_client_t *client = user_data;
+ int csock = 0;
if (client == NULL) {
return;
@@ -393,10 +374,36 @@ cib_remote_connection_destroy(gpointer user_data)
crm_trace("Destroying %s (%p)", client->name, user_data);
num_clients--;
crm_trace("Num unfree'd clients: %d", num_clients);
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+
+ if (client->encrypted) {
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->session) {
+ void *sock_ptr = gnutls_transport_get_ptr(*client->session);
+ csock = GPOINTER_TO_INT(sock_ptr);
+ if (client->handshake_complete) {
+ gnutls_bye(*client->session, GNUTLS_SHUT_WR);
+ }
+ gnutls_deinit(*client->session);
+ gnutls_free(client->session);
+ }
+#endif
+ } else {
+ csock = GPOINTER_TO_INT(client->session);
+ }
+ client->session = NULL;
+
+ if (csock > 0) {
+ close(csock);
+ }
+
free(client->name);
free(client->callback_id);
free(client->id);
free(client->user);
+ free(client->recv_buf);
free(client);
crm_trace("Freed the cib client");
@@ -406,24 +413,15 @@ cib_remote_connection_destroy(gpointer user_data)
return;
}
-int
-cib_remote_msg(gpointer data)
+static void
+cib_handle_remote_msg(cib_client_t *client, xmlNode *command)
{
const char *value = NULL;
- xmlNode *command = NULL;
- cib_client_t *client = data;
-
- crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
-
- command = crm_recv_remote_msg(client->session, client->encrypted);
- if (command == NULL) {
- return -1;
- }
value = crm_element_name(command);
if (safe_str_neq(value, "cib_command")) {
crm_log_xml_trace(command, "Bad command: ");
- goto bail;
+ return;
}
if (client->name == NULL) {
@@ -472,9 +470,95 @@ cib_remote_msg(gpointer data)
crm_log_xml_trace(command, "Remote command: ");
cib_common_callback_worker(0, 0, command, client, TRUE);
- bail:
- free_xml(command);
- command = NULL;
+}
+
+int
+cib_remote_msg(gpointer data)
+{
+ xmlNode *command = NULL;
+ cib_client_t *client = data;
+ int disconnected = 0;
+ int timeout = client->remote_auth ? -1 : 1000;
+
+ crm_trace("%s callback", client->encrypted ? "secure" : "clear-text");
+
+#ifdef HAVE_GNUTLS_GNUTLS_H
+ if (client->encrypted && (client->handshake_complete == FALSE)) {
+ int rc = 0;
+
+ /* Muliple calls to handshake will be required, this callback
+ * will be invoked once the client sends more handshake data. */
+ do {
+ rc = gnutls_handshake(*client->session);
+
+ if (rc < 0 && rc != GNUTLS_E_AGAIN) {
+ crm_err("Remote cib tls handshake failed");
+ return -1;
+ }
+ } while (rc == GNUTLS_E_INTERRUPTED);
+
+ if (rc == 0) {
+ crm_debug("Remote cib tls handshake completed");
+ client->handshake_complete = TRUE;
+ if (client->remote_auth_timeout) {
+ g_source_remove(client->remote_auth_timeout);
+ }
+ /* after handshake, clients must send auth in a few seconds */
+ client->remote_auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT, remote_auth_timeout_cb, client);
+ }
+ return 0;
+ }
+#endif
+
+ crm_recv_remote_msg(client->session, &client->recv_buf, client->encrypted, timeout, &disconnected);
+
+ /* must pass auth before we will process anything else */
+ if (client->remote_auth == FALSE) {
+ xmlNode *reg;
+#if ENABLE_ACL
+ const char *user = NULL;
+#endif
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ if (cib_remote_auth(command) == FALSE) {
+ free_xml(command);
+ return -1;
+ }
+
+ crm_debug("remote connection authenticated successfully");
+ client->remote_auth = TRUE;
+ g_source_remove(client->remote_auth_timeout);
+ client->remote_auth_timeout = 0;
+ client->name = crm_element_value_copy(command, "name");
+
+#if ENABLE_ACL
+ user = crm_element_value(command, "user");
+ if (user) {
+ new_client->user = strdup(user);
+ }
+#endif
+
+ /* send ACK */
+ reg = create_xml_node(NULL, "cib_result");
+ crm_xml_add(reg, F_CIB_OPERATION, CRM_OP_REGISTER);
+ crm_xml_add(reg, F_CIB_CLIENTID, client->id);
+ crm_send_remote_msg(client->session, reg, client->encrypted);
+ free_xml(reg);
+ free_xml(command);
+ }
+
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ while (command) {
+ crm_trace("command received");
+ cib_handle_remote_msg(client, command);
+ free_xml(command);
+ command = crm_parse_remote_buffer(&client->recv_buf);
+ }
+
+ if (disconnected) {
+ crm_trace("disconnected while receiving remote cib msg.");
+ return -1;
+ }
+
return 0;
}
| CWE-399 | null | null |
27,185 | cib_remote_connection_destroy(gpointer user_data)
{
crm_err("Connection destroyed");
#ifdef HAVE_GNUTLS_GNUTLS_H
cib_tls_close(user_data);
#endif
return;
}
| DoS | 0 | cib_remote_connection_destroy(gpointer user_data)
{
crm_err("Connection destroyed");
#ifdef HAVE_GNUTLS_GNUTLS_H
cib_tls_close(user_data);
#endif
return;
}
| @@ -38,14 +38,15 @@
#ifdef HAVE_GNUTLS_GNUTLS_H
# undef KEYFILE
# include <gnutls/gnutls.h>
-extern gnutls_anon_client_credentials anon_cred_c;
-extern gnutls_session *create_tls_session(int csock, int type);
+gnutls_anon_client_credentials anon_cred_c;
+#define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000 /* 5 seconds */
const int kx_prio[] = {
GNUTLS_KX_ANON_DH,
0
};
+static gboolean remote_gnutls_credentials_init = FALSE;
#else
typedef void gnutls_session;
#endif
@@ -61,6 +62,7 @@ struct remote_connection_s {
gnutls_session *session;
mainloop_io_t *source;
char *token;
+ char *recv_buf;
};
typedef struct cib_remote_opaque_s {
@@ -76,7 +78,8 @@ typedef struct cib_remote_opaque_s {
} cib_remote_opaque_t;
void cib_remote_connection_destroy(gpointer user_data);
-int cib_remote_dispatch(gpointer user_data);
+int cib_remote_callback_dispatch(gpointer user_data);
+int cib_remote_command_dispatch(gpointer user_data);
int cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type);
int cib_remote_signoff(cib_t * cib);
int cib_remote_free(cib_t * cib);
@@ -158,117 +161,91 @@ cib_tls_close(cib_t * cib)
{
cib_remote_opaque_t *private = cib->variant_opaque;
- shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
- shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
- close(private->command.socket);
- close(private->callback.socket);
-
#ifdef HAVE_GNUTLS_GNUTLS_H
if (private->command.encrypted) {
- gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->command.session));
- gnutls_free(private->command.session);
-
- gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->callback.session));
- gnutls_free(private->callback.session);
+ if (private->command.session) {
+ gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->command.session));
+ gnutls_free(private->command.session);
+ }
- gnutls_anon_free_client_credentials(anon_cred_c);
- gnutls_global_deinit();
+ if (private->callback.session) {
+ gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->callback.session));
+ gnutls_free(private->callback.session);
+ }
+ private->command.session = NULL;
+ private->callback.session = NULL;
+ if (remote_gnutls_credentials_init) {
+ gnutls_anon_free_client_credentials(anon_cred_c);
+ gnutls_global_deinit();
+ remote_gnutls_credentials_init = FALSE;
+ }
}
#endif
+
+ if (private->command.socket) {
+ shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
+ close(private->command.socket);
+ }
+ if (private->callback.socket) {
+ shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
+ close(private->callback.socket);
+ }
+ private->command.socket = 0;
+ private->callback.socket = 0;
+
+ free(private->command.recv_buf);
+ free(private->callback.recv_buf);
+ private->command.recv_buf = NULL;
+ private->callback.recv_buf = NULL;
+
return 0;
}
static int
-cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
+cib_tls_signon(cib_t * cib, struct remote_connection_s *connection, gboolean event_channel)
{
int sock;
cib_remote_opaque_t *private = cib->variant_opaque;
- struct sockaddr_in addr;
int rc = 0;
- char *server = private->server;
-
- int ret_ga;
- struct addrinfo *res;
- struct addrinfo hints;
+ int disconnected = 0;
xmlNode *answer = NULL;
xmlNode *login = NULL;
- static struct mainloop_fd_callbacks cib_fd_callbacks =
- {
- .dispatch = cib_remote_dispatch,
- .destroy = cib_remote_connection_destroy,
- };
+ static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
+
+ cib_fd_callbacks.dispatch = event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
+ cib_fd_callbacks.destroy = cib_remote_connection_destroy;
connection->socket = 0;
connection->session = NULL;
- /* create socket */
- sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock == -1) {
- crm_perror(LOG_ERR, "Socket creation failed");
- return -1;
- }
-
- /* getaddrinfo */
- bzero(&hints, sizeof(struct addrinfo));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_RAW;
-
- if (hints.ai_family == AF_INET6) {
- hints.ai_protocol = IPPROTO_ICMPV6;
- } else {
- hints.ai_protocol = IPPROTO_ICMP;
- }
-
- crm_debug("Looking up %s", server);
- ret_ga = getaddrinfo(server, NULL, &hints, &res);
- if (ret_ga) {
- crm_err("getaddrinfo: %s", gai_strerror(ret_ga));
- close(sock);
- return -1;
- }
-
- if (res->ai_canonname) {
- server = res->ai_canonname;
- }
-
- crm_debug("Got address %s for %s", server, private->server);
-
- if (!res->ai_addr) {
- fprintf(stderr, "getaddrinfo failed");
- crm_exit(1);
- }
-#if 1
- memcpy(&addr, res->ai_addr, res->ai_addrlen);
-#else
- /* connect to server */
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(server);
-#endif
- addr.sin_port = htons(private->port);
-
- if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
- crm_perror(LOG_ERR, "Connection to %s:%d failed", server, private->port);
- close(sock);
- return -1;
+ sock = crm_remote_tcp_connect(private->server, private->port);
+ if (sock <= 0) {
+ crm_perror(LOG_ERR, "remote tcp connection to %s:%d failed", private->server, private->port);
}
+ connection->socket = sock;
if (connection->encrypted) {
/* initialize GnuTls lib */
#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_global_init();
- gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ if (remote_gnutls_credentials_init == FALSE) {
+ gnutls_global_init();
+ gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ remote_gnutls_credentials_init = TRUE;
+ }
/* bind the socket to GnuTls lib */
- connection->session = create_tls_session(sock, GNUTLS_CLIENT);
- if (connection->session == NULL) {
- crm_perror(LOG_ERR, "Session creation for %s:%d failed", server, private->port);
- close(sock);
+ connection->session = crm_create_anon_tls_session(sock, GNUTLS_CLIENT, anon_cred_c);
+
+ if (crm_initiate_client_tls_handshake(connection->session, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT) != 0) {
+ crm_err("Session creation for %s:%d failed", private->server, private->port);
+
+ gnutls_deinit(*connection->session);
+ gnutls_free(connection->session);
+ connection->session = NULL;
cib_tls_close(cib);
return -1;
}
@@ -289,7 +266,14 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
crm_send_remote_msg(connection->session, login, connection->encrypted);
free_xml(login);
- answer = crm_recv_remote_msg(connection->session, connection->encrypted);
+ crm_recv_remote_msg(connection->session, &connection->recv_buf, connection->encrypted, -1, &disconnected);
+
+ if (disconnected) {
+ rc = -ENOTCONN;
+ }
+
+ answer = crm_parse_remote_buffer(&connection->recv_buf);
+
crm_log_xml_trace(answer, "Reply");
if (answer == NULL) {
rc = -EPROTO;
@@ -310,12 +294,15 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
connection->token = strdup(tmp_ticket);
}
}
+ free_xml(answer);
+ answer = NULL;
if (rc != 0) {
cib_tls_close(cib);
+ return rc;
}
- connection->socket = sock;
+ crm_trace("remote client connection established");
connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH, connection->socket, cib, &cib_fd_callbacks);
return rc;
}
@@ -331,35 +318,61 @@ cib_remote_connection_destroy(gpointer user_data)
}
int
-cib_remote_dispatch(gpointer user_data)
+cib_remote_command_dispatch(gpointer user_data)
+{
+ int disconnected = 0;
+ cib_t *cib = user_data;
+ cib_remote_opaque_t *private = cib->variant_opaque;
+
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, -1, &disconnected);
+
+ free(private->command.recv_buf);
+ private->command.recv_buf = NULL;
+ crm_err("received late reply for remote cib connection, discarding");
+
+ if (disconnected) {
+ return -1;
+ }
+ return 0;
+}
+
+int
+cib_remote_callback_dispatch(gpointer user_data)
{
cib_t *cib = user_data;
cib_remote_opaque_t *private = cib->variant_opaque;
xmlNode *msg = NULL;
- const char *type = NULL;
+ int disconnected = 0;
crm_info("Message on callback channel");
- msg = crm_recv_remote_msg(private->callback.session, private->callback.encrypted);
- type = crm_element_value(msg, F_TYPE);
- crm_trace("Activating %s callbacks...", type);
+ crm_recv_remote_msg(private->callback.session, &private->callback.recv_buf, private->callback.encrypted, -1, &disconnected);
- if (safe_str_eq(type, T_CIB)) {
- cib_native_callback(cib, msg, 0, 0);
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ while (msg) {
+ const char *type = crm_element_value(msg, F_TYPE);
+ crm_trace("Activating %s callbacks...", type);
- } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
- g_list_foreach(cib->notify_list, cib_native_notify, msg);
+ if (safe_str_eq(type, T_CIB)) {
+ cib_native_callback(cib, msg, 0, 0);
- } else {
- crm_err("Unknown message type: %s", type);
- }
+ } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
+ g_list_foreach(cib->notify_list, cib_native_notify, msg);
+
+ } else {
+ crm_err("Unknown message type: %s", type);
+ }
- if (msg != NULL) {
free_xml(msg);
- return 0;
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ }
+
+ if (disconnected) {
+ return -1;
}
- return -1;
+
+ return 0;
}
int
@@ -394,11 +407,11 @@ cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type)
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->command));
+ rc = cib_tls_signon(cib, &(private->command), FALSE);
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->callback));
+ rc = cib_tls_signon(cib, &(private->callback), TRUE);
}
if (rc == pcmk_ok) {
@@ -463,37 +476,20 @@ cib_remote_free(cib_t * cib)
return rc;
}
-static gboolean timer_expired = FALSE;
-static struct timer_rec_s *sync_timer = NULL;
-static gboolean
-cib_timeout_handler(gpointer data)
-{
- struct timer_rec_s *timer = data;
-
- timer_expired = TRUE;
- crm_err("Call %d timed out after %ds", timer->call_id, timer->timeout);
-
- /* Always return TRUE, never remove the handler
- * We do that after the while-loop in cib_native_perform_op()
- */
- return TRUE;
-}
-
int
cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
xmlNode * data, xmlNode ** output_data, int call_options, const char *name)
{
int rc = pcmk_ok;
+ int disconnected = 0;
+ int remaining_time = 0;
+ time_t start_time;
xmlNode *op_msg = NULL;
xmlNode *op_reply = NULL;
cib_remote_opaque_t *private = cib->variant_opaque;
- if (sync_timer == NULL) {
- sync_timer = calloc(1, sizeof(struct timer_rec_s));
- }
-
if (cib->state == cib_disconnected) {
return -ENOTCONN;
}
@@ -524,7 +520,11 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
}
crm_trace("Sending %s message to CIB service", op);
- crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ if (!(call_options & cib_sync_call)) {
+ crm_send_remote_msg(private->callback.session, op_msg, private->command.encrypted);
+ } else {
+ crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ }
free_xml(op_msg);
if ((call_options & cib_discard_reply)) {
@@ -537,30 +537,21 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
crm_trace("Waiting for a syncronous reply");
- if (cib->call_timeout > 0) {
- /* We need this, even with msgfromIPC_timeout(), because we might
- * get other/older replies that don't match the active request
- */
- timer_expired = FALSE;
- sync_timer->call_id = cib->call_id;
- sync_timer->timeout = cib->call_timeout * 1000;
- sync_timer->ref = g_timeout_add(sync_timer->timeout, cib_timeout_handler, sync_timer);
- }
+ start_time = time(NULL);
+ remaining_time = cib->call_timeout ? cib->call_timeout : 60;
- while (timer_expired == FALSE) {
+ while (remaining_time > 0 && !disconnected) {
int reply_id = -1;
int msg_id = cib->call_id;
- op_reply = crm_recv_remote_msg(private->command.session, private->command.encrypted);
- if (op_reply == NULL) {
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, remaining_time * 1000, &disconnected);
+ op_reply = crm_parse_remote_buffer(&private->command.recv_buf);
+
+ if (!op_reply) {
break;
}
crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
- CRM_CHECK(reply_id > 0, free_xml(op_reply);
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref); sync_timer->ref = 0;}
- return -ENOMSG) ;
if (reply_id == msg_id) {
break;
@@ -579,15 +570,9 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
free_xml(op_reply);
op_reply = NULL;
- }
-
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref);
- sync_timer->ref = 0;
- }
- if (timer_expired) {
- return -ETIME;
+ /* wasn't the right reply, try and read some more */
+ remaining_time = time(NULL) - start_time;
}
/* if(IPC_ISRCONN(native->command_channel) == FALSE) { */
@@ -596,7 +581,10 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
/* cib->state = cib_disconnected; */
/* } */
- if (op_reply == NULL) {
+ if (disconnected) {
+ crm_err("Disconnected while waiting for reply.");
+ return -ENOTCONN;
+ } else if (op_reply == NULL) {
crm_err("No reply message - empty");
return -ENOMSG;
} | CWE-399 | null | null |
27,186 | cib_remote_free(cib_t * cib)
{
int rc = pcmk_ok;
crm_warn("Freeing CIB");
if (cib->state != cib_disconnected) {
rc = cib_remote_signoff(cib);
if (rc == pcmk_ok) {
cib_remote_opaque_t *private = cib->variant_opaque;
free(private->server);
free(private->user);
free(private->passwd);
free(cib->cmds);
free(private);
free(cib);
}
}
return rc;
}
| DoS | 0 | cib_remote_free(cib_t * cib)
{
int rc = pcmk_ok;
crm_warn("Freeing CIB");
if (cib->state != cib_disconnected) {
rc = cib_remote_signoff(cib);
if (rc == pcmk_ok) {
cib_remote_opaque_t *private = cib->variant_opaque;
free(private->server);
free(private->user);
free(private->passwd);
free(cib->cmds);
free(private);
free(cib);
}
}
return rc;
}
| @@ -38,14 +38,15 @@
#ifdef HAVE_GNUTLS_GNUTLS_H
# undef KEYFILE
# include <gnutls/gnutls.h>
-extern gnutls_anon_client_credentials anon_cred_c;
-extern gnutls_session *create_tls_session(int csock, int type);
+gnutls_anon_client_credentials anon_cred_c;
+#define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000 /* 5 seconds */
const int kx_prio[] = {
GNUTLS_KX_ANON_DH,
0
};
+static gboolean remote_gnutls_credentials_init = FALSE;
#else
typedef void gnutls_session;
#endif
@@ -61,6 +62,7 @@ struct remote_connection_s {
gnutls_session *session;
mainloop_io_t *source;
char *token;
+ char *recv_buf;
};
typedef struct cib_remote_opaque_s {
@@ -76,7 +78,8 @@ typedef struct cib_remote_opaque_s {
} cib_remote_opaque_t;
void cib_remote_connection_destroy(gpointer user_data);
-int cib_remote_dispatch(gpointer user_data);
+int cib_remote_callback_dispatch(gpointer user_data);
+int cib_remote_command_dispatch(gpointer user_data);
int cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type);
int cib_remote_signoff(cib_t * cib);
int cib_remote_free(cib_t * cib);
@@ -158,117 +161,91 @@ cib_tls_close(cib_t * cib)
{
cib_remote_opaque_t *private = cib->variant_opaque;
- shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
- shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
- close(private->command.socket);
- close(private->callback.socket);
-
#ifdef HAVE_GNUTLS_GNUTLS_H
if (private->command.encrypted) {
- gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->command.session));
- gnutls_free(private->command.session);
-
- gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->callback.session));
- gnutls_free(private->callback.session);
+ if (private->command.session) {
+ gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->command.session));
+ gnutls_free(private->command.session);
+ }
- gnutls_anon_free_client_credentials(anon_cred_c);
- gnutls_global_deinit();
+ if (private->callback.session) {
+ gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->callback.session));
+ gnutls_free(private->callback.session);
+ }
+ private->command.session = NULL;
+ private->callback.session = NULL;
+ if (remote_gnutls_credentials_init) {
+ gnutls_anon_free_client_credentials(anon_cred_c);
+ gnutls_global_deinit();
+ remote_gnutls_credentials_init = FALSE;
+ }
}
#endif
+
+ if (private->command.socket) {
+ shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
+ close(private->command.socket);
+ }
+ if (private->callback.socket) {
+ shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
+ close(private->callback.socket);
+ }
+ private->command.socket = 0;
+ private->callback.socket = 0;
+
+ free(private->command.recv_buf);
+ free(private->callback.recv_buf);
+ private->command.recv_buf = NULL;
+ private->callback.recv_buf = NULL;
+
return 0;
}
static int
-cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
+cib_tls_signon(cib_t * cib, struct remote_connection_s *connection, gboolean event_channel)
{
int sock;
cib_remote_opaque_t *private = cib->variant_opaque;
- struct sockaddr_in addr;
int rc = 0;
- char *server = private->server;
-
- int ret_ga;
- struct addrinfo *res;
- struct addrinfo hints;
+ int disconnected = 0;
xmlNode *answer = NULL;
xmlNode *login = NULL;
- static struct mainloop_fd_callbacks cib_fd_callbacks =
- {
- .dispatch = cib_remote_dispatch,
- .destroy = cib_remote_connection_destroy,
- };
+ static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
+
+ cib_fd_callbacks.dispatch = event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
+ cib_fd_callbacks.destroy = cib_remote_connection_destroy;
connection->socket = 0;
connection->session = NULL;
- /* create socket */
- sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock == -1) {
- crm_perror(LOG_ERR, "Socket creation failed");
- return -1;
- }
-
- /* getaddrinfo */
- bzero(&hints, sizeof(struct addrinfo));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_RAW;
-
- if (hints.ai_family == AF_INET6) {
- hints.ai_protocol = IPPROTO_ICMPV6;
- } else {
- hints.ai_protocol = IPPROTO_ICMP;
- }
-
- crm_debug("Looking up %s", server);
- ret_ga = getaddrinfo(server, NULL, &hints, &res);
- if (ret_ga) {
- crm_err("getaddrinfo: %s", gai_strerror(ret_ga));
- close(sock);
- return -1;
- }
-
- if (res->ai_canonname) {
- server = res->ai_canonname;
- }
-
- crm_debug("Got address %s for %s", server, private->server);
-
- if (!res->ai_addr) {
- fprintf(stderr, "getaddrinfo failed");
- crm_exit(1);
- }
-#if 1
- memcpy(&addr, res->ai_addr, res->ai_addrlen);
-#else
- /* connect to server */
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(server);
-#endif
- addr.sin_port = htons(private->port);
-
- if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
- crm_perror(LOG_ERR, "Connection to %s:%d failed", server, private->port);
- close(sock);
- return -1;
+ sock = crm_remote_tcp_connect(private->server, private->port);
+ if (sock <= 0) {
+ crm_perror(LOG_ERR, "remote tcp connection to %s:%d failed", private->server, private->port);
}
+ connection->socket = sock;
if (connection->encrypted) {
/* initialize GnuTls lib */
#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_global_init();
- gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ if (remote_gnutls_credentials_init == FALSE) {
+ gnutls_global_init();
+ gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ remote_gnutls_credentials_init = TRUE;
+ }
/* bind the socket to GnuTls lib */
- connection->session = create_tls_session(sock, GNUTLS_CLIENT);
- if (connection->session == NULL) {
- crm_perror(LOG_ERR, "Session creation for %s:%d failed", server, private->port);
- close(sock);
+ connection->session = crm_create_anon_tls_session(sock, GNUTLS_CLIENT, anon_cred_c);
+
+ if (crm_initiate_client_tls_handshake(connection->session, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT) != 0) {
+ crm_err("Session creation for %s:%d failed", private->server, private->port);
+
+ gnutls_deinit(*connection->session);
+ gnutls_free(connection->session);
+ connection->session = NULL;
cib_tls_close(cib);
return -1;
}
@@ -289,7 +266,14 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
crm_send_remote_msg(connection->session, login, connection->encrypted);
free_xml(login);
- answer = crm_recv_remote_msg(connection->session, connection->encrypted);
+ crm_recv_remote_msg(connection->session, &connection->recv_buf, connection->encrypted, -1, &disconnected);
+
+ if (disconnected) {
+ rc = -ENOTCONN;
+ }
+
+ answer = crm_parse_remote_buffer(&connection->recv_buf);
+
crm_log_xml_trace(answer, "Reply");
if (answer == NULL) {
rc = -EPROTO;
@@ -310,12 +294,15 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
connection->token = strdup(tmp_ticket);
}
}
+ free_xml(answer);
+ answer = NULL;
if (rc != 0) {
cib_tls_close(cib);
+ return rc;
}
- connection->socket = sock;
+ crm_trace("remote client connection established");
connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH, connection->socket, cib, &cib_fd_callbacks);
return rc;
}
@@ -331,35 +318,61 @@ cib_remote_connection_destroy(gpointer user_data)
}
int
-cib_remote_dispatch(gpointer user_data)
+cib_remote_command_dispatch(gpointer user_data)
+{
+ int disconnected = 0;
+ cib_t *cib = user_data;
+ cib_remote_opaque_t *private = cib->variant_opaque;
+
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, -1, &disconnected);
+
+ free(private->command.recv_buf);
+ private->command.recv_buf = NULL;
+ crm_err("received late reply for remote cib connection, discarding");
+
+ if (disconnected) {
+ return -1;
+ }
+ return 0;
+}
+
+int
+cib_remote_callback_dispatch(gpointer user_data)
{
cib_t *cib = user_data;
cib_remote_opaque_t *private = cib->variant_opaque;
xmlNode *msg = NULL;
- const char *type = NULL;
+ int disconnected = 0;
crm_info("Message on callback channel");
- msg = crm_recv_remote_msg(private->callback.session, private->callback.encrypted);
- type = crm_element_value(msg, F_TYPE);
- crm_trace("Activating %s callbacks...", type);
+ crm_recv_remote_msg(private->callback.session, &private->callback.recv_buf, private->callback.encrypted, -1, &disconnected);
- if (safe_str_eq(type, T_CIB)) {
- cib_native_callback(cib, msg, 0, 0);
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ while (msg) {
+ const char *type = crm_element_value(msg, F_TYPE);
+ crm_trace("Activating %s callbacks...", type);
- } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
- g_list_foreach(cib->notify_list, cib_native_notify, msg);
+ if (safe_str_eq(type, T_CIB)) {
+ cib_native_callback(cib, msg, 0, 0);
- } else {
- crm_err("Unknown message type: %s", type);
- }
+ } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
+ g_list_foreach(cib->notify_list, cib_native_notify, msg);
+
+ } else {
+ crm_err("Unknown message type: %s", type);
+ }
- if (msg != NULL) {
free_xml(msg);
- return 0;
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ }
+
+ if (disconnected) {
+ return -1;
}
- return -1;
+
+ return 0;
}
int
@@ -394,11 +407,11 @@ cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type)
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->command));
+ rc = cib_tls_signon(cib, &(private->command), FALSE);
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->callback));
+ rc = cib_tls_signon(cib, &(private->callback), TRUE);
}
if (rc == pcmk_ok) {
@@ -463,37 +476,20 @@ cib_remote_free(cib_t * cib)
return rc;
}
-static gboolean timer_expired = FALSE;
-static struct timer_rec_s *sync_timer = NULL;
-static gboolean
-cib_timeout_handler(gpointer data)
-{
- struct timer_rec_s *timer = data;
-
- timer_expired = TRUE;
- crm_err("Call %d timed out after %ds", timer->call_id, timer->timeout);
-
- /* Always return TRUE, never remove the handler
- * We do that after the while-loop in cib_native_perform_op()
- */
- return TRUE;
-}
-
int
cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
xmlNode * data, xmlNode ** output_data, int call_options, const char *name)
{
int rc = pcmk_ok;
+ int disconnected = 0;
+ int remaining_time = 0;
+ time_t start_time;
xmlNode *op_msg = NULL;
xmlNode *op_reply = NULL;
cib_remote_opaque_t *private = cib->variant_opaque;
- if (sync_timer == NULL) {
- sync_timer = calloc(1, sizeof(struct timer_rec_s));
- }
-
if (cib->state == cib_disconnected) {
return -ENOTCONN;
}
@@ -524,7 +520,11 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
}
crm_trace("Sending %s message to CIB service", op);
- crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ if (!(call_options & cib_sync_call)) {
+ crm_send_remote_msg(private->callback.session, op_msg, private->command.encrypted);
+ } else {
+ crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ }
free_xml(op_msg);
if ((call_options & cib_discard_reply)) {
@@ -537,30 +537,21 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
crm_trace("Waiting for a syncronous reply");
- if (cib->call_timeout > 0) {
- /* We need this, even with msgfromIPC_timeout(), because we might
- * get other/older replies that don't match the active request
- */
- timer_expired = FALSE;
- sync_timer->call_id = cib->call_id;
- sync_timer->timeout = cib->call_timeout * 1000;
- sync_timer->ref = g_timeout_add(sync_timer->timeout, cib_timeout_handler, sync_timer);
- }
+ start_time = time(NULL);
+ remaining_time = cib->call_timeout ? cib->call_timeout : 60;
- while (timer_expired == FALSE) {
+ while (remaining_time > 0 && !disconnected) {
int reply_id = -1;
int msg_id = cib->call_id;
- op_reply = crm_recv_remote_msg(private->command.session, private->command.encrypted);
- if (op_reply == NULL) {
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, remaining_time * 1000, &disconnected);
+ op_reply = crm_parse_remote_buffer(&private->command.recv_buf);
+
+ if (!op_reply) {
break;
}
crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
- CRM_CHECK(reply_id > 0, free_xml(op_reply);
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref); sync_timer->ref = 0;}
- return -ENOMSG) ;
if (reply_id == msg_id) {
break;
@@ -579,15 +570,9 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
free_xml(op_reply);
op_reply = NULL;
- }
-
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref);
- sync_timer->ref = 0;
- }
- if (timer_expired) {
- return -ETIME;
+ /* wasn't the right reply, try and read some more */
+ remaining_time = time(NULL) - start_time;
}
/* if(IPC_ISRCONN(native->command_channel) == FALSE) { */
@@ -596,7 +581,10 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
/* cib->state = cib_disconnected; */
/* } */
- if (op_reply == NULL) {
+ if (disconnected) {
+ crm_err("Disconnected while waiting for reply.");
+ return -ENOTCONN;
+ } else if (op_reply == NULL) {
crm_err("No reply message - empty");
return -ENOMSG;
} | CWE-399 | null | null |
27,187 | cib_remote_inputfd(cib_t * cib)
{
cib_remote_opaque_t *private = cib->variant_opaque;
return private->callback.socket;
}
| DoS | 0 | cib_remote_inputfd(cib_t * cib)
{
cib_remote_opaque_t *private = cib->variant_opaque;
return private->callback.socket;
}
| @@ -38,14 +38,15 @@
#ifdef HAVE_GNUTLS_GNUTLS_H
# undef KEYFILE
# include <gnutls/gnutls.h>
-extern gnutls_anon_client_credentials anon_cred_c;
-extern gnutls_session *create_tls_session(int csock, int type);
+gnutls_anon_client_credentials anon_cred_c;
+#define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000 /* 5 seconds */
const int kx_prio[] = {
GNUTLS_KX_ANON_DH,
0
};
+static gboolean remote_gnutls_credentials_init = FALSE;
#else
typedef void gnutls_session;
#endif
@@ -61,6 +62,7 @@ struct remote_connection_s {
gnutls_session *session;
mainloop_io_t *source;
char *token;
+ char *recv_buf;
};
typedef struct cib_remote_opaque_s {
@@ -76,7 +78,8 @@ typedef struct cib_remote_opaque_s {
} cib_remote_opaque_t;
void cib_remote_connection_destroy(gpointer user_data);
-int cib_remote_dispatch(gpointer user_data);
+int cib_remote_callback_dispatch(gpointer user_data);
+int cib_remote_command_dispatch(gpointer user_data);
int cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type);
int cib_remote_signoff(cib_t * cib);
int cib_remote_free(cib_t * cib);
@@ -158,117 +161,91 @@ cib_tls_close(cib_t * cib)
{
cib_remote_opaque_t *private = cib->variant_opaque;
- shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
- shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
- close(private->command.socket);
- close(private->callback.socket);
-
#ifdef HAVE_GNUTLS_GNUTLS_H
if (private->command.encrypted) {
- gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->command.session));
- gnutls_free(private->command.session);
-
- gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->callback.session));
- gnutls_free(private->callback.session);
+ if (private->command.session) {
+ gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->command.session));
+ gnutls_free(private->command.session);
+ }
- gnutls_anon_free_client_credentials(anon_cred_c);
- gnutls_global_deinit();
+ if (private->callback.session) {
+ gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->callback.session));
+ gnutls_free(private->callback.session);
+ }
+ private->command.session = NULL;
+ private->callback.session = NULL;
+ if (remote_gnutls_credentials_init) {
+ gnutls_anon_free_client_credentials(anon_cred_c);
+ gnutls_global_deinit();
+ remote_gnutls_credentials_init = FALSE;
+ }
}
#endif
+
+ if (private->command.socket) {
+ shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
+ close(private->command.socket);
+ }
+ if (private->callback.socket) {
+ shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
+ close(private->callback.socket);
+ }
+ private->command.socket = 0;
+ private->callback.socket = 0;
+
+ free(private->command.recv_buf);
+ free(private->callback.recv_buf);
+ private->command.recv_buf = NULL;
+ private->callback.recv_buf = NULL;
+
return 0;
}
static int
-cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
+cib_tls_signon(cib_t * cib, struct remote_connection_s *connection, gboolean event_channel)
{
int sock;
cib_remote_opaque_t *private = cib->variant_opaque;
- struct sockaddr_in addr;
int rc = 0;
- char *server = private->server;
-
- int ret_ga;
- struct addrinfo *res;
- struct addrinfo hints;
+ int disconnected = 0;
xmlNode *answer = NULL;
xmlNode *login = NULL;
- static struct mainloop_fd_callbacks cib_fd_callbacks =
- {
- .dispatch = cib_remote_dispatch,
- .destroy = cib_remote_connection_destroy,
- };
+ static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
+
+ cib_fd_callbacks.dispatch = event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
+ cib_fd_callbacks.destroy = cib_remote_connection_destroy;
connection->socket = 0;
connection->session = NULL;
- /* create socket */
- sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock == -1) {
- crm_perror(LOG_ERR, "Socket creation failed");
- return -1;
- }
-
- /* getaddrinfo */
- bzero(&hints, sizeof(struct addrinfo));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_RAW;
-
- if (hints.ai_family == AF_INET6) {
- hints.ai_protocol = IPPROTO_ICMPV6;
- } else {
- hints.ai_protocol = IPPROTO_ICMP;
- }
-
- crm_debug("Looking up %s", server);
- ret_ga = getaddrinfo(server, NULL, &hints, &res);
- if (ret_ga) {
- crm_err("getaddrinfo: %s", gai_strerror(ret_ga));
- close(sock);
- return -1;
- }
-
- if (res->ai_canonname) {
- server = res->ai_canonname;
- }
-
- crm_debug("Got address %s for %s", server, private->server);
-
- if (!res->ai_addr) {
- fprintf(stderr, "getaddrinfo failed");
- crm_exit(1);
- }
-#if 1
- memcpy(&addr, res->ai_addr, res->ai_addrlen);
-#else
- /* connect to server */
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(server);
-#endif
- addr.sin_port = htons(private->port);
-
- if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
- crm_perror(LOG_ERR, "Connection to %s:%d failed", server, private->port);
- close(sock);
- return -1;
+ sock = crm_remote_tcp_connect(private->server, private->port);
+ if (sock <= 0) {
+ crm_perror(LOG_ERR, "remote tcp connection to %s:%d failed", private->server, private->port);
}
+ connection->socket = sock;
if (connection->encrypted) {
/* initialize GnuTls lib */
#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_global_init();
- gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ if (remote_gnutls_credentials_init == FALSE) {
+ gnutls_global_init();
+ gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ remote_gnutls_credentials_init = TRUE;
+ }
/* bind the socket to GnuTls lib */
- connection->session = create_tls_session(sock, GNUTLS_CLIENT);
- if (connection->session == NULL) {
- crm_perror(LOG_ERR, "Session creation for %s:%d failed", server, private->port);
- close(sock);
+ connection->session = crm_create_anon_tls_session(sock, GNUTLS_CLIENT, anon_cred_c);
+
+ if (crm_initiate_client_tls_handshake(connection->session, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT) != 0) {
+ crm_err("Session creation for %s:%d failed", private->server, private->port);
+
+ gnutls_deinit(*connection->session);
+ gnutls_free(connection->session);
+ connection->session = NULL;
cib_tls_close(cib);
return -1;
}
@@ -289,7 +266,14 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
crm_send_remote_msg(connection->session, login, connection->encrypted);
free_xml(login);
- answer = crm_recv_remote_msg(connection->session, connection->encrypted);
+ crm_recv_remote_msg(connection->session, &connection->recv_buf, connection->encrypted, -1, &disconnected);
+
+ if (disconnected) {
+ rc = -ENOTCONN;
+ }
+
+ answer = crm_parse_remote_buffer(&connection->recv_buf);
+
crm_log_xml_trace(answer, "Reply");
if (answer == NULL) {
rc = -EPROTO;
@@ -310,12 +294,15 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
connection->token = strdup(tmp_ticket);
}
}
+ free_xml(answer);
+ answer = NULL;
if (rc != 0) {
cib_tls_close(cib);
+ return rc;
}
- connection->socket = sock;
+ crm_trace("remote client connection established");
connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH, connection->socket, cib, &cib_fd_callbacks);
return rc;
}
@@ -331,35 +318,61 @@ cib_remote_connection_destroy(gpointer user_data)
}
int
-cib_remote_dispatch(gpointer user_data)
+cib_remote_command_dispatch(gpointer user_data)
+{
+ int disconnected = 0;
+ cib_t *cib = user_data;
+ cib_remote_opaque_t *private = cib->variant_opaque;
+
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, -1, &disconnected);
+
+ free(private->command.recv_buf);
+ private->command.recv_buf = NULL;
+ crm_err("received late reply for remote cib connection, discarding");
+
+ if (disconnected) {
+ return -1;
+ }
+ return 0;
+}
+
+int
+cib_remote_callback_dispatch(gpointer user_data)
{
cib_t *cib = user_data;
cib_remote_opaque_t *private = cib->variant_opaque;
xmlNode *msg = NULL;
- const char *type = NULL;
+ int disconnected = 0;
crm_info("Message on callback channel");
- msg = crm_recv_remote_msg(private->callback.session, private->callback.encrypted);
- type = crm_element_value(msg, F_TYPE);
- crm_trace("Activating %s callbacks...", type);
+ crm_recv_remote_msg(private->callback.session, &private->callback.recv_buf, private->callback.encrypted, -1, &disconnected);
- if (safe_str_eq(type, T_CIB)) {
- cib_native_callback(cib, msg, 0, 0);
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ while (msg) {
+ const char *type = crm_element_value(msg, F_TYPE);
+ crm_trace("Activating %s callbacks...", type);
- } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
- g_list_foreach(cib->notify_list, cib_native_notify, msg);
+ if (safe_str_eq(type, T_CIB)) {
+ cib_native_callback(cib, msg, 0, 0);
- } else {
- crm_err("Unknown message type: %s", type);
- }
+ } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
+ g_list_foreach(cib->notify_list, cib_native_notify, msg);
+
+ } else {
+ crm_err("Unknown message type: %s", type);
+ }
- if (msg != NULL) {
free_xml(msg);
- return 0;
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ }
+
+ if (disconnected) {
+ return -1;
}
- return -1;
+
+ return 0;
}
int
@@ -394,11 +407,11 @@ cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type)
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->command));
+ rc = cib_tls_signon(cib, &(private->command), FALSE);
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->callback));
+ rc = cib_tls_signon(cib, &(private->callback), TRUE);
}
if (rc == pcmk_ok) {
@@ -463,37 +476,20 @@ cib_remote_free(cib_t * cib)
return rc;
}
-static gboolean timer_expired = FALSE;
-static struct timer_rec_s *sync_timer = NULL;
-static gboolean
-cib_timeout_handler(gpointer data)
-{
- struct timer_rec_s *timer = data;
-
- timer_expired = TRUE;
- crm_err("Call %d timed out after %ds", timer->call_id, timer->timeout);
-
- /* Always return TRUE, never remove the handler
- * We do that after the while-loop in cib_native_perform_op()
- */
- return TRUE;
-}
-
int
cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
xmlNode * data, xmlNode ** output_data, int call_options, const char *name)
{
int rc = pcmk_ok;
+ int disconnected = 0;
+ int remaining_time = 0;
+ time_t start_time;
xmlNode *op_msg = NULL;
xmlNode *op_reply = NULL;
cib_remote_opaque_t *private = cib->variant_opaque;
- if (sync_timer == NULL) {
- sync_timer = calloc(1, sizeof(struct timer_rec_s));
- }
-
if (cib->state == cib_disconnected) {
return -ENOTCONN;
}
@@ -524,7 +520,11 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
}
crm_trace("Sending %s message to CIB service", op);
- crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ if (!(call_options & cib_sync_call)) {
+ crm_send_remote_msg(private->callback.session, op_msg, private->command.encrypted);
+ } else {
+ crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ }
free_xml(op_msg);
if ((call_options & cib_discard_reply)) {
@@ -537,30 +537,21 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
crm_trace("Waiting for a syncronous reply");
- if (cib->call_timeout > 0) {
- /* We need this, even with msgfromIPC_timeout(), because we might
- * get other/older replies that don't match the active request
- */
- timer_expired = FALSE;
- sync_timer->call_id = cib->call_id;
- sync_timer->timeout = cib->call_timeout * 1000;
- sync_timer->ref = g_timeout_add(sync_timer->timeout, cib_timeout_handler, sync_timer);
- }
+ start_time = time(NULL);
+ remaining_time = cib->call_timeout ? cib->call_timeout : 60;
- while (timer_expired == FALSE) {
+ while (remaining_time > 0 && !disconnected) {
int reply_id = -1;
int msg_id = cib->call_id;
- op_reply = crm_recv_remote_msg(private->command.session, private->command.encrypted);
- if (op_reply == NULL) {
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, remaining_time * 1000, &disconnected);
+ op_reply = crm_parse_remote_buffer(&private->command.recv_buf);
+
+ if (!op_reply) {
break;
}
crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
- CRM_CHECK(reply_id > 0, free_xml(op_reply);
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref); sync_timer->ref = 0;}
- return -ENOMSG) ;
if (reply_id == msg_id) {
break;
@@ -579,15 +570,9 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
free_xml(op_reply);
op_reply = NULL;
- }
-
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref);
- sync_timer->ref = 0;
- }
- if (timer_expired) {
- return -ETIME;
+ /* wasn't the right reply, try and read some more */
+ remaining_time = time(NULL) - start_time;
}
/* if(IPC_ISRCONN(native->command_channel) == FALSE) { */
@@ -596,7 +581,10 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
/* cib->state = cib_disconnected; */
/* } */
- if (op_reply == NULL) {
+ if (disconnected) {
+ crm_err("Disconnected while waiting for reply.");
+ return -ENOTCONN;
+ } else if (op_reply == NULL) {
crm_err("No reply message - empty");
return -ENOMSG;
} | CWE-399 | null | null |
27,188 | cib_remote_new(const char *server, const char *user, const char *passwd, int port,
gboolean encrypted)
{
cib_remote_opaque_t *private = NULL;
cib_t *cib = cib_new_variant();
private = calloc(1, sizeof(cib_remote_opaque_t));
cib->variant = cib_remote;
cib->variant_opaque = private;
if (server) {
private->server = strdup(server);
}
if (user) {
private->user = strdup(user);
}
if (passwd) {
private->passwd = strdup(passwd);
}
private->port = port;
private->command.encrypted = encrypted;
private->callback.encrypted = encrypted;
/* assign variant specific ops */
cib->delegate_fn = cib_remote_perform_op;
cib->cmds->signon = cib_remote_signon;
cib->cmds->signoff = cib_remote_signoff;
cib->cmds->free = cib_remote_free;
cib->cmds->inputfd = cib_remote_inputfd;
cib->cmds->register_notification = cib_remote_register_notification;
cib->cmds->set_connection_dnotify = cib_remote_set_connection_dnotify;
return cib;
}
| DoS | 0 | cib_remote_new(const char *server, const char *user, const char *passwd, int port,
gboolean encrypted)
{
cib_remote_opaque_t *private = NULL;
cib_t *cib = cib_new_variant();
private = calloc(1, sizeof(cib_remote_opaque_t));
cib->variant = cib_remote;
cib->variant_opaque = private;
if (server) {
private->server = strdup(server);
}
if (user) {
private->user = strdup(user);
}
if (passwd) {
private->passwd = strdup(passwd);
}
private->port = port;
private->command.encrypted = encrypted;
private->callback.encrypted = encrypted;
/* assign variant specific ops */
cib->delegate_fn = cib_remote_perform_op;
cib->cmds->signon = cib_remote_signon;
cib->cmds->signoff = cib_remote_signoff;
cib->cmds->free = cib_remote_free;
cib->cmds->inputfd = cib_remote_inputfd;
cib->cmds->register_notification = cib_remote_register_notification;
cib->cmds->set_connection_dnotify = cib_remote_set_connection_dnotify;
return cib;
}
| @@ -38,14 +38,15 @@
#ifdef HAVE_GNUTLS_GNUTLS_H
# undef KEYFILE
# include <gnutls/gnutls.h>
-extern gnutls_anon_client_credentials anon_cred_c;
-extern gnutls_session *create_tls_session(int csock, int type);
+gnutls_anon_client_credentials anon_cred_c;
+#define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000 /* 5 seconds */
const int kx_prio[] = {
GNUTLS_KX_ANON_DH,
0
};
+static gboolean remote_gnutls_credentials_init = FALSE;
#else
typedef void gnutls_session;
#endif
@@ -61,6 +62,7 @@ struct remote_connection_s {
gnutls_session *session;
mainloop_io_t *source;
char *token;
+ char *recv_buf;
};
typedef struct cib_remote_opaque_s {
@@ -76,7 +78,8 @@ typedef struct cib_remote_opaque_s {
} cib_remote_opaque_t;
void cib_remote_connection_destroy(gpointer user_data);
-int cib_remote_dispatch(gpointer user_data);
+int cib_remote_callback_dispatch(gpointer user_data);
+int cib_remote_command_dispatch(gpointer user_data);
int cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type);
int cib_remote_signoff(cib_t * cib);
int cib_remote_free(cib_t * cib);
@@ -158,117 +161,91 @@ cib_tls_close(cib_t * cib)
{
cib_remote_opaque_t *private = cib->variant_opaque;
- shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
- shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
- close(private->command.socket);
- close(private->callback.socket);
-
#ifdef HAVE_GNUTLS_GNUTLS_H
if (private->command.encrypted) {
- gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->command.session));
- gnutls_free(private->command.session);
-
- gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->callback.session));
- gnutls_free(private->callback.session);
+ if (private->command.session) {
+ gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->command.session));
+ gnutls_free(private->command.session);
+ }
- gnutls_anon_free_client_credentials(anon_cred_c);
- gnutls_global_deinit();
+ if (private->callback.session) {
+ gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->callback.session));
+ gnutls_free(private->callback.session);
+ }
+ private->command.session = NULL;
+ private->callback.session = NULL;
+ if (remote_gnutls_credentials_init) {
+ gnutls_anon_free_client_credentials(anon_cred_c);
+ gnutls_global_deinit();
+ remote_gnutls_credentials_init = FALSE;
+ }
}
#endif
+
+ if (private->command.socket) {
+ shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
+ close(private->command.socket);
+ }
+ if (private->callback.socket) {
+ shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
+ close(private->callback.socket);
+ }
+ private->command.socket = 0;
+ private->callback.socket = 0;
+
+ free(private->command.recv_buf);
+ free(private->callback.recv_buf);
+ private->command.recv_buf = NULL;
+ private->callback.recv_buf = NULL;
+
return 0;
}
static int
-cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
+cib_tls_signon(cib_t * cib, struct remote_connection_s *connection, gboolean event_channel)
{
int sock;
cib_remote_opaque_t *private = cib->variant_opaque;
- struct sockaddr_in addr;
int rc = 0;
- char *server = private->server;
-
- int ret_ga;
- struct addrinfo *res;
- struct addrinfo hints;
+ int disconnected = 0;
xmlNode *answer = NULL;
xmlNode *login = NULL;
- static struct mainloop_fd_callbacks cib_fd_callbacks =
- {
- .dispatch = cib_remote_dispatch,
- .destroy = cib_remote_connection_destroy,
- };
+ static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
+
+ cib_fd_callbacks.dispatch = event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
+ cib_fd_callbacks.destroy = cib_remote_connection_destroy;
connection->socket = 0;
connection->session = NULL;
- /* create socket */
- sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock == -1) {
- crm_perror(LOG_ERR, "Socket creation failed");
- return -1;
- }
-
- /* getaddrinfo */
- bzero(&hints, sizeof(struct addrinfo));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_RAW;
-
- if (hints.ai_family == AF_INET6) {
- hints.ai_protocol = IPPROTO_ICMPV6;
- } else {
- hints.ai_protocol = IPPROTO_ICMP;
- }
-
- crm_debug("Looking up %s", server);
- ret_ga = getaddrinfo(server, NULL, &hints, &res);
- if (ret_ga) {
- crm_err("getaddrinfo: %s", gai_strerror(ret_ga));
- close(sock);
- return -1;
- }
-
- if (res->ai_canonname) {
- server = res->ai_canonname;
- }
-
- crm_debug("Got address %s for %s", server, private->server);
-
- if (!res->ai_addr) {
- fprintf(stderr, "getaddrinfo failed");
- crm_exit(1);
- }
-#if 1
- memcpy(&addr, res->ai_addr, res->ai_addrlen);
-#else
- /* connect to server */
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(server);
-#endif
- addr.sin_port = htons(private->port);
-
- if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
- crm_perror(LOG_ERR, "Connection to %s:%d failed", server, private->port);
- close(sock);
- return -1;
+ sock = crm_remote_tcp_connect(private->server, private->port);
+ if (sock <= 0) {
+ crm_perror(LOG_ERR, "remote tcp connection to %s:%d failed", private->server, private->port);
}
+ connection->socket = sock;
if (connection->encrypted) {
/* initialize GnuTls lib */
#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_global_init();
- gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ if (remote_gnutls_credentials_init == FALSE) {
+ gnutls_global_init();
+ gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ remote_gnutls_credentials_init = TRUE;
+ }
/* bind the socket to GnuTls lib */
- connection->session = create_tls_session(sock, GNUTLS_CLIENT);
- if (connection->session == NULL) {
- crm_perror(LOG_ERR, "Session creation for %s:%d failed", server, private->port);
- close(sock);
+ connection->session = crm_create_anon_tls_session(sock, GNUTLS_CLIENT, anon_cred_c);
+
+ if (crm_initiate_client_tls_handshake(connection->session, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT) != 0) {
+ crm_err("Session creation for %s:%d failed", private->server, private->port);
+
+ gnutls_deinit(*connection->session);
+ gnutls_free(connection->session);
+ connection->session = NULL;
cib_tls_close(cib);
return -1;
}
@@ -289,7 +266,14 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
crm_send_remote_msg(connection->session, login, connection->encrypted);
free_xml(login);
- answer = crm_recv_remote_msg(connection->session, connection->encrypted);
+ crm_recv_remote_msg(connection->session, &connection->recv_buf, connection->encrypted, -1, &disconnected);
+
+ if (disconnected) {
+ rc = -ENOTCONN;
+ }
+
+ answer = crm_parse_remote_buffer(&connection->recv_buf);
+
crm_log_xml_trace(answer, "Reply");
if (answer == NULL) {
rc = -EPROTO;
@@ -310,12 +294,15 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
connection->token = strdup(tmp_ticket);
}
}
+ free_xml(answer);
+ answer = NULL;
if (rc != 0) {
cib_tls_close(cib);
+ return rc;
}
- connection->socket = sock;
+ crm_trace("remote client connection established");
connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH, connection->socket, cib, &cib_fd_callbacks);
return rc;
}
@@ -331,35 +318,61 @@ cib_remote_connection_destroy(gpointer user_data)
}
int
-cib_remote_dispatch(gpointer user_data)
+cib_remote_command_dispatch(gpointer user_data)
+{
+ int disconnected = 0;
+ cib_t *cib = user_data;
+ cib_remote_opaque_t *private = cib->variant_opaque;
+
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, -1, &disconnected);
+
+ free(private->command.recv_buf);
+ private->command.recv_buf = NULL;
+ crm_err("received late reply for remote cib connection, discarding");
+
+ if (disconnected) {
+ return -1;
+ }
+ return 0;
+}
+
+int
+cib_remote_callback_dispatch(gpointer user_data)
{
cib_t *cib = user_data;
cib_remote_opaque_t *private = cib->variant_opaque;
xmlNode *msg = NULL;
- const char *type = NULL;
+ int disconnected = 0;
crm_info("Message on callback channel");
- msg = crm_recv_remote_msg(private->callback.session, private->callback.encrypted);
- type = crm_element_value(msg, F_TYPE);
- crm_trace("Activating %s callbacks...", type);
+ crm_recv_remote_msg(private->callback.session, &private->callback.recv_buf, private->callback.encrypted, -1, &disconnected);
- if (safe_str_eq(type, T_CIB)) {
- cib_native_callback(cib, msg, 0, 0);
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ while (msg) {
+ const char *type = crm_element_value(msg, F_TYPE);
+ crm_trace("Activating %s callbacks...", type);
- } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
- g_list_foreach(cib->notify_list, cib_native_notify, msg);
+ if (safe_str_eq(type, T_CIB)) {
+ cib_native_callback(cib, msg, 0, 0);
- } else {
- crm_err("Unknown message type: %s", type);
- }
+ } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
+ g_list_foreach(cib->notify_list, cib_native_notify, msg);
+
+ } else {
+ crm_err("Unknown message type: %s", type);
+ }
- if (msg != NULL) {
free_xml(msg);
- return 0;
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ }
+
+ if (disconnected) {
+ return -1;
}
- return -1;
+
+ return 0;
}
int
@@ -394,11 +407,11 @@ cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type)
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->command));
+ rc = cib_tls_signon(cib, &(private->command), FALSE);
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->callback));
+ rc = cib_tls_signon(cib, &(private->callback), TRUE);
}
if (rc == pcmk_ok) {
@@ -463,37 +476,20 @@ cib_remote_free(cib_t * cib)
return rc;
}
-static gboolean timer_expired = FALSE;
-static struct timer_rec_s *sync_timer = NULL;
-static gboolean
-cib_timeout_handler(gpointer data)
-{
- struct timer_rec_s *timer = data;
-
- timer_expired = TRUE;
- crm_err("Call %d timed out after %ds", timer->call_id, timer->timeout);
-
- /* Always return TRUE, never remove the handler
- * We do that after the while-loop in cib_native_perform_op()
- */
- return TRUE;
-}
-
int
cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
xmlNode * data, xmlNode ** output_data, int call_options, const char *name)
{
int rc = pcmk_ok;
+ int disconnected = 0;
+ int remaining_time = 0;
+ time_t start_time;
xmlNode *op_msg = NULL;
xmlNode *op_reply = NULL;
cib_remote_opaque_t *private = cib->variant_opaque;
- if (sync_timer == NULL) {
- sync_timer = calloc(1, sizeof(struct timer_rec_s));
- }
-
if (cib->state == cib_disconnected) {
return -ENOTCONN;
}
@@ -524,7 +520,11 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
}
crm_trace("Sending %s message to CIB service", op);
- crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ if (!(call_options & cib_sync_call)) {
+ crm_send_remote_msg(private->callback.session, op_msg, private->command.encrypted);
+ } else {
+ crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ }
free_xml(op_msg);
if ((call_options & cib_discard_reply)) {
@@ -537,30 +537,21 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
crm_trace("Waiting for a syncronous reply");
- if (cib->call_timeout > 0) {
- /* We need this, even with msgfromIPC_timeout(), because we might
- * get other/older replies that don't match the active request
- */
- timer_expired = FALSE;
- sync_timer->call_id = cib->call_id;
- sync_timer->timeout = cib->call_timeout * 1000;
- sync_timer->ref = g_timeout_add(sync_timer->timeout, cib_timeout_handler, sync_timer);
- }
+ start_time = time(NULL);
+ remaining_time = cib->call_timeout ? cib->call_timeout : 60;
- while (timer_expired == FALSE) {
+ while (remaining_time > 0 && !disconnected) {
int reply_id = -1;
int msg_id = cib->call_id;
- op_reply = crm_recv_remote_msg(private->command.session, private->command.encrypted);
- if (op_reply == NULL) {
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, remaining_time * 1000, &disconnected);
+ op_reply = crm_parse_remote_buffer(&private->command.recv_buf);
+
+ if (!op_reply) {
break;
}
crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
- CRM_CHECK(reply_id > 0, free_xml(op_reply);
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref); sync_timer->ref = 0;}
- return -ENOMSG) ;
if (reply_id == msg_id) {
break;
@@ -579,15 +570,9 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
free_xml(op_reply);
op_reply = NULL;
- }
-
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref);
- sync_timer->ref = 0;
- }
- if (timer_expired) {
- return -ETIME;
+ /* wasn't the right reply, try and read some more */
+ remaining_time = time(NULL) - start_time;
}
/* if(IPC_ISRCONN(native->command_channel) == FALSE) { */
@@ -596,7 +581,10 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
/* cib->state = cib_disconnected; */
/* } */
- if (op_reply == NULL) {
+ if (disconnected) {
+ crm_err("Disconnected while waiting for reply.");
+ return -ENOTCONN;
+ } else if (op_reply == NULL) {
crm_err("No reply message - empty");
return -ENOMSG;
} | CWE-399 | null | null |
27,189 | cib_remote_signoff(cib_t * cib)
{
int rc = pcmk_ok;
/* cib_remote_opaque_t *private = cib->variant_opaque; */
crm_debug("Signing out of the CIB Service");
#ifdef HAVE_GNUTLS_GNUTLS_H
cib_tls_close(cib);
#endif
cib->state = cib_disconnected;
cib->type = cib_none;
return rc;
}
| DoS | 0 | cib_remote_signoff(cib_t * cib)
{
int rc = pcmk_ok;
/* cib_remote_opaque_t *private = cib->variant_opaque; */
crm_debug("Signing out of the CIB Service");
#ifdef HAVE_GNUTLS_GNUTLS_H
cib_tls_close(cib);
#endif
cib->state = cib_disconnected;
cib->type = cib_none;
return rc;
}
| @@ -38,14 +38,15 @@
#ifdef HAVE_GNUTLS_GNUTLS_H
# undef KEYFILE
# include <gnutls/gnutls.h>
-extern gnutls_anon_client_credentials anon_cred_c;
-extern gnutls_session *create_tls_session(int csock, int type);
+gnutls_anon_client_credentials anon_cred_c;
+#define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000 /* 5 seconds */
const int kx_prio[] = {
GNUTLS_KX_ANON_DH,
0
};
+static gboolean remote_gnutls_credentials_init = FALSE;
#else
typedef void gnutls_session;
#endif
@@ -61,6 +62,7 @@ struct remote_connection_s {
gnutls_session *session;
mainloop_io_t *source;
char *token;
+ char *recv_buf;
};
typedef struct cib_remote_opaque_s {
@@ -76,7 +78,8 @@ typedef struct cib_remote_opaque_s {
} cib_remote_opaque_t;
void cib_remote_connection_destroy(gpointer user_data);
-int cib_remote_dispatch(gpointer user_data);
+int cib_remote_callback_dispatch(gpointer user_data);
+int cib_remote_command_dispatch(gpointer user_data);
int cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type);
int cib_remote_signoff(cib_t * cib);
int cib_remote_free(cib_t * cib);
@@ -158,117 +161,91 @@ cib_tls_close(cib_t * cib)
{
cib_remote_opaque_t *private = cib->variant_opaque;
- shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
- shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
- close(private->command.socket);
- close(private->callback.socket);
-
#ifdef HAVE_GNUTLS_GNUTLS_H
if (private->command.encrypted) {
- gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->command.session));
- gnutls_free(private->command.session);
-
- gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
- gnutls_deinit(*(private->callback.session));
- gnutls_free(private->callback.session);
+ if (private->command.session) {
+ gnutls_bye(*(private->command.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->command.session));
+ gnutls_free(private->command.session);
+ }
- gnutls_anon_free_client_credentials(anon_cred_c);
- gnutls_global_deinit();
+ if (private->callback.session) {
+ gnutls_bye(*(private->callback.session), GNUTLS_SHUT_RDWR);
+ gnutls_deinit(*(private->callback.session));
+ gnutls_free(private->callback.session);
+ }
+ private->command.session = NULL;
+ private->callback.session = NULL;
+ if (remote_gnutls_credentials_init) {
+ gnutls_anon_free_client_credentials(anon_cred_c);
+ gnutls_global_deinit();
+ remote_gnutls_credentials_init = FALSE;
+ }
}
#endif
+
+ if (private->command.socket) {
+ shutdown(private->command.socket, SHUT_RDWR); /* no more receptions */
+ close(private->command.socket);
+ }
+ if (private->callback.socket) {
+ shutdown(private->callback.socket, SHUT_RDWR); /* no more receptions */
+ close(private->callback.socket);
+ }
+ private->command.socket = 0;
+ private->callback.socket = 0;
+
+ free(private->command.recv_buf);
+ free(private->callback.recv_buf);
+ private->command.recv_buf = NULL;
+ private->callback.recv_buf = NULL;
+
return 0;
}
static int
-cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
+cib_tls_signon(cib_t * cib, struct remote_connection_s *connection, gboolean event_channel)
{
int sock;
cib_remote_opaque_t *private = cib->variant_opaque;
- struct sockaddr_in addr;
int rc = 0;
- char *server = private->server;
-
- int ret_ga;
- struct addrinfo *res;
- struct addrinfo hints;
+ int disconnected = 0;
xmlNode *answer = NULL;
xmlNode *login = NULL;
- static struct mainloop_fd_callbacks cib_fd_callbacks =
- {
- .dispatch = cib_remote_dispatch,
- .destroy = cib_remote_connection_destroy,
- };
+ static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
+
+ cib_fd_callbacks.dispatch = event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
+ cib_fd_callbacks.destroy = cib_remote_connection_destroy;
connection->socket = 0;
connection->session = NULL;
- /* create socket */
- sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock == -1) {
- crm_perror(LOG_ERR, "Socket creation failed");
- return -1;
- }
-
- /* getaddrinfo */
- bzero(&hints, sizeof(struct addrinfo));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_RAW;
-
- if (hints.ai_family == AF_INET6) {
- hints.ai_protocol = IPPROTO_ICMPV6;
- } else {
- hints.ai_protocol = IPPROTO_ICMP;
- }
-
- crm_debug("Looking up %s", server);
- ret_ga = getaddrinfo(server, NULL, &hints, &res);
- if (ret_ga) {
- crm_err("getaddrinfo: %s", gai_strerror(ret_ga));
- close(sock);
- return -1;
- }
-
- if (res->ai_canonname) {
- server = res->ai_canonname;
- }
-
- crm_debug("Got address %s for %s", server, private->server);
-
- if (!res->ai_addr) {
- fprintf(stderr, "getaddrinfo failed");
- crm_exit(1);
- }
-#if 1
- memcpy(&addr, res->ai_addr, res->ai_addrlen);
-#else
- /* connect to server */
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(server);
-#endif
- addr.sin_port = htons(private->port);
-
- if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
- crm_perror(LOG_ERR, "Connection to %s:%d failed", server, private->port);
- close(sock);
- return -1;
+ sock = crm_remote_tcp_connect(private->server, private->port);
+ if (sock <= 0) {
+ crm_perror(LOG_ERR, "remote tcp connection to %s:%d failed", private->server, private->port);
}
+ connection->socket = sock;
if (connection->encrypted) {
/* initialize GnuTls lib */
#ifdef HAVE_GNUTLS_GNUTLS_H
- gnutls_global_init();
- gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ if (remote_gnutls_credentials_init == FALSE) {
+ gnutls_global_init();
+ gnutls_anon_allocate_client_credentials(&anon_cred_c);
+ remote_gnutls_credentials_init = TRUE;
+ }
/* bind the socket to GnuTls lib */
- connection->session = create_tls_session(sock, GNUTLS_CLIENT);
- if (connection->session == NULL) {
- crm_perror(LOG_ERR, "Session creation for %s:%d failed", server, private->port);
- close(sock);
+ connection->session = crm_create_anon_tls_session(sock, GNUTLS_CLIENT, anon_cred_c);
+
+ if (crm_initiate_client_tls_handshake(connection->session, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT) != 0) {
+ crm_err("Session creation for %s:%d failed", private->server, private->port);
+
+ gnutls_deinit(*connection->session);
+ gnutls_free(connection->session);
+ connection->session = NULL;
cib_tls_close(cib);
return -1;
}
@@ -289,7 +266,14 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
crm_send_remote_msg(connection->session, login, connection->encrypted);
free_xml(login);
- answer = crm_recv_remote_msg(connection->session, connection->encrypted);
+ crm_recv_remote_msg(connection->session, &connection->recv_buf, connection->encrypted, -1, &disconnected);
+
+ if (disconnected) {
+ rc = -ENOTCONN;
+ }
+
+ answer = crm_parse_remote_buffer(&connection->recv_buf);
+
crm_log_xml_trace(answer, "Reply");
if (answer == NULL) {
rc = -EPROTO;
@@ -310,12 +294,15 @@ cib_tls_signon(cib_t * cib, struct remote_connection_s *connection)
connection->token = strdup(tmp_ticket);
}
}
+ free_xml(answer);
+ answer = NULL;
if (rc != 0) {
cib_tls_close(cib);
+ return rc;
}
- connection->socket = sock;
+ crm_trace("remote client connection established");
connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH, connection->socket, cib, &cib_fd_callbacks);
return rc;
}
@@ -331,35 +318,61 @@ cib_remote_connection_destroy(gpointer user_data)
}
int
-cib_remote_dispatch(gpointer user_data)
+cib_remote_command_dispatch(gpointer user_data)
+{
+ int disconnected = 0;
+ cib_t *cib = user_data;
+ cib_remote_opaque_t *private = cib->variant_opaque;
+
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, -1, &disconnected);
+
+ free(private->command.recv_buf);
+ private->command.recv_buf = NULL;
+ crm_err("received late reply for remote cib connection, discarding");
+
+ if (disconnected) {
+ return -1;
+ }
+ return 0;
+}
+
+int
+cib_remote_callback_dispatch(gpointer user_data)
{
cib_t *cib = user_data;
cib_remote_opaque_t *private = cib->variant_opaque;
xmlNode *msg = NULL;
- const char *type = NULL;
+ int disconnected = 0;
crm_info("Message on callback channel");
- msg = crm_recv_remote_msg(private->callback.session, private->callback.encrypted);
- type = crm_element_value(msg, F_TYPE);
- crm_trace("Activating %s callbacks...", type);
+ crm_recv_remote_msg(private->callback.session, &private->callback.recv_buf, private->callback.encrypted, -1, &disconnected);
- if (safe_str_eq(type, T_CIB)) {
- cib_native_callback(cib, msg, 0, 0);
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ while (msg) {
+ const char *type = crm_element_value(msg, F_TYPE);
+ crm_trace("Activating %s callbacks...", type);
- } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
- g_list_foreach(cib->notify_list, cib_native_notify, msg);
+ if (safe_str_eq(type, T_CIB)) {
+ cib_native_callback(cib, msg, 0, 0);
- } else {
- crm_err("Unknown message type: %s", type);
- }
+ } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
+ g_list_foreach(cib->notify_list, cib_native_notify, msg);
+
+ } else {
+ crm_err("Unknown message type: %s", type);
+ }
- if (msg != NULL) {
free_xml(msg);
- return 0;
+ msg = crm_parse_remote_buffer(&private->callback.recv_buf);
+ }
+
+ if (disconnected) {
+ return -1;
}
- return -1;
+
+ return 0;
}
int
@@ -394,11 +407,11 @@ cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type)
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->command));
+ rc = cib_tls_signon(cib, &(private->command), FALSE);
}
if (rc == pcmk_ok) {
- rc = cib_tls_signon(cib, &(private->callback));
+ rc = cib_tls_signon(cib, &(private->callback), TRUE);
}
if (rc == pcmk_ok) {
@@ -463,37 +476,20 @@ cib_remote_free(cib_t * cib)
return rc;
}
-static gboolean timer_expired = FALSE;
-static struct timer_rec_s *sync_timer = NULL;
-static gboolean
-cib_timeout_handler(gpointer data)
-{
- struct timer_rec_s *timer = data;
-
- timer_expired = TRUE;
- crm_err("Call %d timed out after %ds", timer->call_id, timer->timeout);
-
- /* Always return TRUE, never remove the handler
- * We do that after the while-loop in cib_native_perform_op()
- */
- return TRUE;
-}
-
int
cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
xmlNode * data, xmlNode ** output_data, int call_options, const char *name)
{
int rc = pcmk_ok;
+ int disconnected = 0;
+ int remaining_time = 0;
+ time_t start_time;
xmlNode *op_msg = NULL;
xmlNode *op_reply = NULL;
cib_remote_opaque_t *private = cib->variant_opaque;
- if (sync_timer == NULL) {
- sync_timer = calloc(1, sizeof(struct timer_rec_s));
- }
-
if (cib->state == cib_disconnected) {
return -ENOTCONN;
}
@@ -524,7 +520,11 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
}
crm_trace("Sending %s message to CIB service", op);
- crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ if (!(call_options & cib_sync_call)) {
+ crm_send_remote_msg(private->callback.session, op_msg, private->command.encrypted);
+ } else {
+ crm_send_remote_msg(private->command.session, op_msg, private->command.encrypted);
+ }
free_xml(op_msg);
if ((call_options & cib_discard_reply)) {
@@ -537,30 +537,21 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
crm_trace("Waiting for a syncronous reply");
- if (cib->call_timeout > 0) {
- /* We need this, even with msgfromIPC_timeout(), because we might
- * get other/older replies that don't match the active request
- */
- timer_expired = FALSE;
- sync_timer->call_id = cib->call_id;
- sync_timer->timeout = cib->call_timeout * 1000;
- sync_timer->ref = g_timeout_add(sync_timer->timeout, cib_timeout_handler, sync_timer);
- }
+ start_time = time(NULL);
+ remaining_time = cib->call_timeout ? cib->call_timeout : 60;
- while (timer_expired == FALSE) {
+ while (remaining_time > 0 && !disconnected) {
int reply_id = -1;
int msg_id = cib->call_id;
- op_reply = crm_recv_remote_msg(private->command.session, private->command.encrypted);
- if (op_reply == NULL) {
+ crm_recv_remote_msg(private->command.session, &private->command.recv_buf, private->command.encrypted, remaining_time * 1000, &disconnected);
+ op_reply = crm_parse_remote_buffer(&private->command.recv_buf);
+
+ if (!op_reply) {
break;
}
crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
- CRM_CHECK(reply_id > 0, free_xml(op_reply);
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref); sync_timer->ref = 0;}
- return -ENOMSG) ;
if (reply_id == msg_id) {
break;
@@ -579,15 +570,9 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
free_xml(op_reply);
op_reply = NULL;
- }
-
- if (sync_timer->ref > 0) {
- g_source_remove(sync_timer->ref);
- sync_timer->ref = 0;
- }
- if (timer_expired) {
- return -ETIME;
+ /* wasn't the right reply, try and read some more */
+ remaining_time = time(NULL) - start_time;
}
/* if(IPC_ISRCONN(native->command_channel) == FALSE) { */
@@ -596,7 +581,10 @@ cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char
/* cib->state = cib_disconnected; */
/* } */
- if (op_reply == NULL) {
+ if (disconnected) {
+ crm_err("Disconnected while waiting for reply.");
+ return -ENOTCONN;
+ } else if (op_reply == NULL) {
crm_err("No reply message - empty");
return -ENOMSG;
} | CWE-399 | null | null |
27,190 | child_death_dispatch(GPid pid, gint status, gpointer user_data)
{
int signo = 0;
int exitcode = 0;
mainloop_child_t *child = user_data;
crm_trace("Managed process %d exited: %p", pid, child);
if (WIFEXITED(status)) {
exitcode = WEXITSTATUS(status);
crm_trace("Managed process %d (%s) exited with rc=%d", pid,
child->desc, exitcode);
} else if (WIFSIGNALED(status)) {
signo = WTERMSIG(status);
crm_trace("Managed process %d (%s) exited with signal=%d", pid,
child->desc, signo);
}
#ifdef WCOREDUMP
if (WCOREDUMP(status)) {
crm_err("Managed process %d (%s) dumped core", pid, child->desc);
}
#endif
if (child->callback) {
child->callback(child, status, signo, exitcode);
}
crm_trace("Removed process entry for %d", pid);
mainloop_child_destroy(child);
return;
}
| DoS | 0 | child_death_dispatch(GPid pid, gint status, gpointer user_data)
{
int signo = 0;
int exitcode = 0;
mainloop_child_t *child = user_data;
crm_trace("Managed process %d exited: %p", pid, child);
if (WIFEXITED(status)) {
exitcode = WEXITSTATUS(status);
crm_trace("Managed process %d (%s) exited with rc=%d", pid,
child->desc, exitcode);
} else if (WIFSIGNALED(status)) {
signo = WTERMSIG(status);
crm_trace("Managed process %d (%s) exited with signal=%d", pid,
child->desc, signo);
}
#ifdef WCOREDUMP
if (WCOREDUMP(status)) {
crm_err("Managed process %d (%s) dumped core", pid, child->desc);
}
#endif
if (child->callback) {
child->callback(child, status, signo, exitcode);
}
crm_trace("Removed process entry for %d", pid);
mainloop_child_destroy(child);
return;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,191 | crm_signal_dispatch(GSource * source, GSourceFunc callback, gpointer userdata)
{
crm_signal_t *sig = (crm_signal_t *) source;
crm_info("Invoking handler for signal %d: %s", sig->signal, strsignal(sig->signal));
sig->trigger.trigger = FALSE;
if (sig->handler) {
sig->handler(sig->signal);
}
return TRUE;
}
| DoS | 0 | crm_signal_dispatch(GSource * source, GSourceFunc callback, gpointer userdata)
{
crm_signal_t *sig = (crm_signal_t *) source;
crm_info("Invoking handler for signal %d: %s", sig->signal, strsignal(sig->signal));
sig->trigger.trigger = FALSE;
if (sig->handler) {
sig->handler(sig->signal);
}
return TRUE;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,192 | crm_trigger_check(GSource * source)
{
crm_trigger_t *trig = (crm_trigger_t *) source;
return trig->trigger;
}
| DoS | 0 | crm_trigger_check(GSource * source)
{
crm_trigger_t *trig = (crm_trigger_t *) source;
return trig->trigger;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,193 | crm_trigger_prepare(GSource * source, gint * timeout)
{
crm_trigger_t *trig = (crm_trigger_t *) source;
/* cluster-glue's FD and IPC related sources make use of
* g_source_add_poll() but do not set a timeout in their prepare
* functions
*
* This means mainloop's poll() will block until an event for one
* of these sources occurs - any /other/ type of source, such as
* this one or g_idle_*, that doesn't use g_source_add_poll() is
* S-O-L and wont be processed until there is something fd-based
* happens.
*
* Luckily the timeout we can set here affects all sources and
* puts an upper limit on how long poll() can take.
*
* So unconditionally set a small-ish timeout, not too small that
* we're in constant motion, which will act as an upper bound on
* how long the signal handling might be delayed for.
*/
*timeout = 500; /* Timeout in ms */
return trig->trigger;
}
| DoS | 0 | crm_trigger_prepare(GSource * source, gint * timeout)
{
crm_trigger_t *trig = (crm_trigger_t *) source;
/* cluster-glue's FD and IPC related sources make use of
* g_source_add_poll() but do not set a timeout in their prepare
* functions
*
* This means mainloop's poll() will block until an event for one
* of these sources occurs - any /other/ type of source, such as
* this one or g_idle_*, that doesn't use g_source_add_poll() is
* S-O-L and wont be processed until there is something fd-based
* happens.
*
* Luckily the timeout we can set here affects all sources and
* puts an upper limit on how long poll() can take.
*
* So unconditionally set a small-ish timeout, not too small that
* we're in constant motion, which will act as an upper bound on
* how long the signal handling might be delayed for.
*/
*timeout = 500; /* Timeout in ms */
return trig->trigger;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,194 | gio_poll_destroy(gpointer data)
{
/* adaptor->source is valid but about to be destroyed (ref_count == 0) in gmain.c
* adaptor->channel will still have ref_count > 0... should be == 1
*/
struct gio_to_qb_poll *adaptor = (struct gio_to_qb_poll *)data;
crm_trace("Destroying adaptor %p channel %p (ref=%d)", adaptor, adaptor->channel, gio_adapter_refcount(adaptor));
adaptor->is_used = QB_FALSE;
adaptor->channel = NULL;
adaptor->source = 0;
}
| DoS | 0 | gio_poll_destroy(gpointer data)
{
/* adaptor->source is valid but about to be destroyed (ref_count == 0) in gmain.c
* adaptor->channel will still have ref_count > 0... should be == 1
*/
struct gio_to_qb_poll *adaptor = (struct gio_to_qb_poll *)data;
crm_trace("Destroying adaptor %p channel %p (ref=%d)", adaptor, adaptor->channel, gio_adapter_refcount(adaptor));
adaptor->is_used = QB_FALSE;
adaptor->channel = NULL;
adaptor->source = 0;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,195 | gio_poll_dispatch_add(enum qb_loop_priority p, int32_t fd, int32_t evts,
void *data, qb_ipcs_dispatch_fn_t fn)
{
struct gio_to_qb_poll *adaptor;
GIOChannel *channel;
int32_t res = 0;
res = qb_array_index(gio_map, fd, (void**)&adaptor);
if (res < 0) {
crm_err("Array lookup failed for fd=%d: %d", fd, res);
return res;
}
crm_trace("Adding fd=%d to mainloop as adapater %p", fd, adaptor);
if (adaptor->is_used) {
crm_err("Adapter for descriptor %d is still in-use", fd);
return -EEXIST;
}
/* channel is created with ref_count = 1 */
channel = g_io_channel_unix_new(fd);
if (!channel) {
crm_err("No memory left to add fd=%d", fd);
return -ENOMEM;
}
/* Because unlike the poll() API, glib doesn't tell us about HUPs by default */
evts |= (G_IO_HUP|G_IO_NVAL|G_IO_ERR);
adaptor->channel = channel;
adaptor->fn = fn;
adaptor->events = evts;
adaptor->data = data;
adaptor->p = p;
adaptor->is_used = QB_TRUE;
adaptor->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, evts, gio_read_socket, adaptor, gio_poll_destroy);
/* Now that mainloop now holds a reference to adaptor->channel,
* thanks to g_io_add_watch_full(), drop ours from g_io_channel_unix_new().
*
* This means that adaptor->channel will be free'd by:
* g_main_context_dispatch()
* -> g_source_destroy_internal()
* -> g_source_callback_unref()
* shortly after gio_poll_destroy() completes
*/
g_io_channel_unref(adaptor->channel);
crm_trace("Added to mainloop with gsource id=%d, ref=%d", adaptor->source, gio_adapter_refcount(adaptor));
if(adaptor->source > 0) {
return 0;
}
return -EINVAL;
}
| DoS | 0 | gio_poll_dispatch_add(enum qb_loop_priority p, int32_t fd, int32_t evts,
void *data, qb_ipcs_dispatch_fn_t fn)
{
struct gio_to_qb_poll *adaptor;
GIOChannel *channel;
int32_t res = 0;
res = qb_array_index(gio_map, fd, (void**)&adaptor);
if (res < 0) {
crm_err("Array lookup failed for fd=%d: %d", fd, res);
return res;
}
crm_trace("Adding fd=%d to mainloop as adapater %p", fd, adaptor);
if (adaptor->is_used) {
crm_err("Adapter for descriptor %d is still in-use", fd);
return -EEXIST;
}
/* channel is created with ref_count = 1 */
channel = g_io_channel_unix_new(fd);
if (!channel) {
crm_err("No memory left to add fd=%d", fd);
return -ENOMEM;
}
/* Because unlike the poll() API, glib doesn't tell us about HUPs by default */
evts |= (G_IO_HUP|G_IO_NVAL|G_IO_ERR);
adaptor->channel = channel;
adaptor->fn = fn;
adaptor->events = evts;
adaptor->data = data;
adaptor->p = p;
adaptor->is_used = QB_TRUE;
adaptor->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, evts, gio_read_socket, adaptor, gio_poll_destroy);
/* Now that mainloop now holds a reference to adaptor->channel,
* thanks to g_io_add_watch_full(), drop ours from g_io_channel_unix_new().
*
* This means that adaptor->channel will be free'd by:
* g_main_context_dispatch()
* -> g_source_destroy_internal()
* -> g_source_callback_unref()
* shortly after gio_poll_destroy() completes
*/
g_io_channel_unref(adaptor->channel);
crm_trace("Added to mainloop with gsource id=%d, ref=%d", adaptor->source, gio_adapter_refcount(adaptor));
if(adaptor->source > 0) {
return 0;
}
return -EINVAL;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,196 | gio_poll_dispatch_del(int32_t fd)
{
struct gio_to_qb_poll *adaptor;
crm_trace("Looking for fd=%d", fd);
if (qb_array_index(gio_map, fd, (void**)&adaptor) == 0) {
crm_trace("Marking adaptor %p unused (ref=%d)", adaptor, gio_adapter_refcount(adaptor));
adaptor->is_used = QB_FALSE;
}
return 0;
}
| DoS | 0 | gio_poll_dispatch_del(int32_t fd)
{
struct gio_to_qb_poll *adaptor;
crm_trace("Looking for fd=%d", fd);
if (qb_array_index(gio_map, fd, (void**)&adaptor) == 0) {
crm_trace("Marking adaptor %p unused (ref=%d)", adaptor, gio_adapter_refcount(adaptor));
adaptor->is_used = QB_FALSE;
}
return 0;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,197 | mainloop_add_child(pid_t pid, int timeout, const char *desc, void * privatedata,
void (*callback)(mainloop_child_t *p, int status, int signo, int exitcode))
{
mainloop_child_t *child = g_new(mainloop_child_t, 1);
child->pid = pid;
child->timerid = 0;
child->timeout = FALSE;
child->desc = strdup(desc);
child->privatedata = privatedata;
child->callback = callback;
if (timeout) {
child->timerid = g_timeout_add(
timeout, child_timeout_callback, child);
}
child->watchid = g_child_watch_add(pid, child_death_dispatch, child);
}
| DoS | 0 | mainloop_add_child(pid_t pid, int timeout, const char *desc, void * privatedata,
void (*callback)(mainloop_child_t *p, int status, int signo, int exitcode))
{
mainloop_child_t *child = g_new(mainloop_child_t, 1);
child->pid = pid;
child->timerid = 0;
child->timeout = FALSE;
child->desc = strdup(desc);
child->privatedata = privatedata;
child->callback = callback;
if (timeout) {
child->timerid = g_timeout_add(
timeout, child_timeout_callback, child);
}
child->watchid = g_child_watch_add(pid, child_death_dispatch, child);
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,198 | mainloop_add_fd(
const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
{
mainloop_io_t *client = NULL;
if(fd > 0) {
client = calloc(1, sizeof(mainloop_io_t));
client->name = strdup(name);
client->userdata = userdata;
if(callbacks) {
client->destroy_fn = callbacks->destroy;
client->dispatch_fn_io = callbacks->dispatch;
}
client->channel = g_io_channel_unix_new(fd);
client->source = g_io_add_watch_full(
client->channel, priority, (G_IO_IN|G_IO_HUP|G_IO_NVAL|G_IO_ERR),
mainloop_gio_callback, client, mainloop_gio_destroy);
/* Now that mainloop now holds a reference to adaptor->channel,
* thanks to g_io_add_watch_full(), drop ours from g_io_channel_unix_new().
*
* This means that adaptor->channel will be free'd by:
* g_main_context_dispatch() or g_source_remove()
* -> g_source_destroy_internal()
* -> g_source_callback_unref()
* shortly after mainloop_gio_destroy() completes
*/
g_io_channel_unref(client->channel);
crm_trace("Added connection %d for %s[%p].%d %d", client->source, client->name, client, fd, mainloop_gio_refcount(client));
}
return client;
}
| DoS | 0 | mainloop_add_fd(
const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
{
mainloop_io_t *client = NULL;
if(fd > 0) {
client = calloc(1, sizeof(mainloop_io_t));
client->name = strdup(name);
client->userdata = userdata;
if(callbacks) {
client->destroy_fn = callbacks->destroy;
client->dispatch_fn_io = callbacks->dispatch;
}
client->channel = g_io_channel_unix_new(fd);
client->source = g_io_add_watch_full(
client->channel, priority, (G_IO_IN|G_IO_HUP|G_IO_NVAL|G_IO_ERR),
mainloop_gio_callback, client, mainloop_gio_destroy);
/* Now that mainloop now holds a reference to adaptor->channel,
* thanks to g_io_add_watch_full(), drop ours from g_io_channel_unix_new().
*
* This means that adaptor->channel will be free'd by:
* g_main_context_dispatch() or g_source_remove()
* -> g_source_destroy_internal()
* -> g_source_callback_unref()
* shortly after mainloop_gio_destroy() completes
*/
g_io_channel_unref(client->channel);
crm_trace("Added connection %d for %s[%p].%d %d", client->source, client->name, client, fd, mainloop_gio_refcount(client));
}
return client;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
27,199 | mainloop_add_ipc_client(
const char *name, int priority, size_t max_size, void *userdata, struct ipc_client_callbacks *callbacks)
{
mainloop_io_t *client = NULL;
crm_ipc_t *conn = crm_ipc_new(name, max_size);
if(conn && crm_ipc_connect(conn)) {
int32_t fd = crm_ipc_get_fd(conn);
client = mainloop_add_fd(name, priority, fd, userdata, NULL);
client->ipc = conn;
client->destroy_fn = callbacks->destroy;
client->dispatch_fn_ipc = callbacks->dispatch;
}
if(conn && client == NULL) {
crm_trace("Connection to %s failed", name);
crm_ipc_close(conn);
crm_ipc_destroy(conn);
}
return client;
}
| DoS | 0 | mainloop_add_ipc_client(
const char *name, int priority, size_t max_size, void *userdata, struct ipc_client_callbacks *callbacks)
{
mainloop_io_t *client = NULL;
crm_ipc_t *conn = crm_ipc_new(name, max_size);
if(conn && crm_ipc_connect(conn)) {
int32_t fd = crm_ipc_get_fd(conn);
client = mainloop_add_fd(name, priority, fd, userdata, NULL);
client->ipc = conn;
client->destroy_fn = callbacks->destroy;
client->dispatch_fn_ipc = callbacks->dispatch;
}
if(conn && client == NULL) {
crm_trace("Connection to %s failed", name);
crm_ipc_close(conn);
crm_ipc_destroy(conn);
}
return client;
}
| @@ -178,6 +178,7 @@ mainloop_destroy_trigger(crm_trigger_t * source)
source->trigger = FALSE;
if (source->id > 0) {
g_source_remove(source->id);
+ source->id = 0;
}
return TRUE;
} | CWE-399 | null | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.