1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (c) 2002-2016 Balabit
* Copyright (c) 2016 Balázs Scheidler
* Copyright (c) 2012-2015 Viktor Juhasz <viktor.juhasz@balabit.com>
* Copyright (c) 2012-2013 Viktor Tusa
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "logmsg-serialize-fixup.h"
#include "nvtable-serialize.h"
#include <stdlib.h>
/**********************************************************************
* This chunk of code fixes up the NVHandle values scattered in a
* deserialized NVTable.
*
* The reason they need fixing is that NVHandles are allocated dynamically
* when they are first used in a syslog-ng process. As the serialized
* representation of a LogMessage can be read back by another syslog-ng
* process, its idea of the name-value pair handle might be different.
*
* This means that we need to iterate through the struct and change the
* handle values. This is not even a simple operation as handles are embedded
* in various locations
* - in the index table, an array sorted by handle
* - as indirect values that refer to other values
* - the SDATA handles array that ensures that SDATA values are ordered
* the same way they were received.
*
**********************************************************************/
static gint
_index_entry_cmp(const void *a, const void *b)
{
NVIndexEntry *entry_a = (NVIndexEntry *) a;
NVIndexEntry *entry_b = (NVIndexEntry *) b;
NVHandle handle_a = entry_a->handle;
NVHandle handle_b = entry_b->handle;
if (handle_a < handle_b)
return -1;
else if (handle_a == handle_b)
return 0;
else
return 1;
}
static void
_copy_updated_sdata_handles(LogMessageSerializationState *state)
{
memcpy(state->msg->sdata, state->updated_sdata_handles, sizeof(state->msg->sdata[0]) * state->msg->num_sdata);
}
static void
_sort_updated_index(LogMessageSerializationState *state)
{
NVTable *self = state->nvtable;
qsort(state->updated_index, self->index_size, sizeof(NVIndexEntry), _index_entry_cmp);
}
static void
_copy_updated_index(LogMessageSerializationState *state)
{
NVTable *self = state->nvtable;
memmove(nv_table_get_index(self), state->updated_index, sizeof(NVIndexEntry) * self->index_size);
}
static void
_fixup_sdata_handle(LogMessageSerializationState *state, NVHandle old_handle, NVHandle new_handle)
{
LogMessage *msg = state->msg;
gint i;
if (msg->sdata)
{
for (i = 0; i < msg->num_sdata; i++)
{
if (msg->sdata[i] == old_handle)
{
state->updated_sdata_handles[i] = new_handle;
break;
}
}
}
}
static void
_fixup_handle_in_index_entry(LogMessageSerializationState *state, NVIndexEntry *index_entry, NVHandle new_handle)
{
gint index_slot = index_entry - nv_table_get_index(state->nvtable);
NVIndexEntry *new_index_entry = &state->updated_index[index_slot];
new_index_entry->ofs = index_entry->ofs;
new_index_entry->handle = new_handle;
}
static inline gboolean
_is_static_entry(NVEntry *entry)
{
return entry->name_len == 0;
}
static gboolean
_old_handle_has_the_same_name(NVHandle old_handle, NVEntry *entry)
{
gssize old_handle_name_len = 0;
const gchar *old_handle_name = log_msg_get_value_name(old_handle, &old_handle_name_len);
if (!old_handle_name)
return FALSE;
if (old_handle_name_len != entry->name_len)
return FALSE;
return memcmp(nv_entry_get_name(entry), old_handle_name, old_handle_name_len) == 0;
}
static NVHandle
_allocate_handle_for_entry_name(NVHandle old_handle, NVEntry *entry)
{
if (_is_static_entry(entry))
return old_handle;
if (_old_handle_has_the_same_name(old_handle, entry))
return old_handle;
return log_msg_get_value_handle(nv_entry_get_name(entry));
}
static NVHandle
_allocate_handle_of_referenced_entry(NVTable *self, NVHandle ref_handle)
{
NVEntry *ref_entry = nv_table_get_entry(self, ref_handle, NULL, NULL);
return _allocate_handle_for_entry_name(ref_handle, ref_entry);
}
static gboolean
_is_indirect(NVEntry *entry)
{
return entry && entry->indirect;
}
static void
_fixup_handle_in_indirect_entry(NVTable *self, NVEntry *entry)
{
entry->vindirect.handle = _allocate_handle_of_referenced_entry(self, entry->vindirect.handle);
}
static gboolean
_validate_entry(LogMessageSerializationState *state, NVEntry *entry)
{
NVTable *nvtable = state->nvtable;
/* check alignment */
if ((GPOINTER_TO_UINT(entry) & 0x3) != 0)
return FALSE;
/* entry points above the start of the NVTable */
if (GPOINTER_TO_UINT(entry) < GPOINTER_TO_UINT(nvtable))
return FALSE;
/* entry header is inside the allocated NVTable */
if (GPOINTER_TO_UINT(entry) + NV_ENTRY_DIRECT_HDR > GPOINTER_TO_UINT(nvtable) + nvtable->size)
return FALSE;
/* entry as a whole is inside the allocated NVTable */
if (GPOINTER_TO_UINT(entry) + entry->alloc_len > GPOINTER_TO_UINT(nvtable) + nvtable->size)
return FALSE;
if (!entry->indirect)
{
if (entry->alloc_len < NV_ENTRY_DIRECT_HDR + entry->name_len + 1 + entry->vdirect.value_len + 1)
return FALSE;
}
else
{
if (entry->alloc_len < NV_ENTRY_INDIRECT_HDR + entry->name_len + 1)
return FALSE;
}
return TRUE;
}
static gboolean
_update_entry(LogMessageSerializationState *state, NVEntry *entry)
{
if ((state->nvtable_flags & NVT_SUPPORTS_UNSET) == 0)
{
/* if this was serialized with a syslog-ng that didn't support unset, make sure that:
* 1) unset is set to FALSE
* 2) the rest of the bits are cleared too
*
* This is needed as earlier syslog-ng versions unfortunately didn't
* set the flags to 0, so it might contain garbage. Anything that is
* past NVT_SUPPORTS_UNSET however sets these bits to zero to make
* adding new flags easier. */
entry->unset = FALSE;
entry->__bit_padding = 0;
}
return TRUE;
}
static gboolean
_fixup_entry(NVHandle old_handle, NVEntry *entry, NVIndexEntry *index_entry, gpointer user_data)
{
LogMessageSerializationState *state = (LogMessageSerializationState *) user_data;
NVTable *self = state->nvtable;
NVHandle new_handle;
if (!_validate_entry(state, entry) ||
!_update_entry(state, entry))
{
/* this return of TRUE indicates failure, as it terminates the foreach loop */
return TRUE;
}
new_handle = _allocate_handle_for_entry_name(old_handle, entry);
if (index_entry)
_fixup_handle_in_index_entry(state, index_entry, new_handle);
if (log_msg_is_handle_sdata(new_handle))
_fixup_sdata_handle(state, old_handle, new_handle);
if (!state->handle_changed)
state->handle_changed = (new_handle != old_handle);
if (_is_indirect(entry))
_fixup_handle_in_indirect_entry(self, entry);
return FALSE;
}
gboolean
log_msg_fixup_handles_after_deserialization(LogMessageSerializationState *state)
{
LogMessage *msg = state->msg;
NVTable *nvtable = state->nvtable;
NVHandle _updated_sdata_handles[msg->num_sdata];
NVIndexEntry _updated_index[nvtable->index_size];
/* NOTE: we are allocating these arrays as auto variables on the stack, so
* we can use some stack space here. However, num_sdata is guint8,
* index_size is guint16 */
state->updated_sdata_handles = _updated_sdata_handles;
state->updated_index = _updated_index;
state->handle_changed = FALSE;
if (nv_table_foreach_entry(nvtable, _fixup_entry, state))
{
/* foreach_entry() returns TRUE if the callback returned failure */
return FALSE;
}
if (state->handle_changed)
{
_copy_updated_sdata_handles(state);
_sort_updated_index(state);
_copy_updated_index(state);
}
return TRUE;
}
|