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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
/*
Copyright (C) 2021 The Falco Authors.
Falco is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Falco 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Falco. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstddef>
#include <iomanip>
#include <algorithm>
#include <sstream>
#include "stdint.h"
#include "gen_filter.h"
#include "sinsp.h"
#include "sinsp_int.h"
std::set<uint16_t> gen_event_filter_check::s_default_evttypes{1};
gen_event::gen_event()
{
}
gen_event::~gen_event()
{
}
void gen_event::set_check_id(int32_t id)
{
if (id) {
m_check_id = id;
}
}
int32_t gen_event::get_check_id() const
{
return m_check_id;
}
gen_event_filter_check::gen_event_filter_check()
{
}
gen_event_filter_check::~gen_event_filter_check()
{
}
void gen_event_filter_check::set_check_id(int32_t id)
{
m_check_id = id;
}
int32_t gen_event_filter_check::get_check_id()
{
return m_check_id;
}
const std::set<uint16_t> &gen_event_filter_check::evttypes()
{
return s_default_evttypes;
}
const std::set<uint16_t> &gen_event_filter_check::possible_evttypes()
{
return s_default_evttypes;
}
///////////////////////////////////////////////////////////////////////////////
// gen_event_filter_expression implementation
///////////////////////////////////////////////////////////////////////////////
gen_event_filter_expression::gen_event_filter_expression()
{
m_parent = NULL;
}
gen_event_filter_expression::~gen_event_filter_expression()
{
uint32_t j;
for(j = 0; j < m_checks.size(); j++)
{
delete m_checks[j];
}
}
void gen_event_filter_expression::add_check(gen_event_filter_check* chk)
{
m_checks.push_back(chk);
}
bool gen_event_filter_expression::compare(gen_event *evt)
{
uint32_t j;
uint32_t size = (uint32_t)m_checks.size();
bool res = true;
gen_event_filter_check* chk = NULL;
for(j = 0; j < size; j++)
{
chk = m_checks[j];
ASSERT(chk != NULL);
if(j == 0)
{
switch(chk->m_boolop)
{
case BO_NONE:
res = chk->compare(evt);
if (res) {
evt->set_check_id(chk->get_check_id());
}
break;
case BO_NOT:
res = !chk->compare(evt);
break;
default:
ASSERT(false);
break;
}
}
else
{
switch(chk->m_boolop)
{
case BO_OR:
if(res)
{
goto done;
}
res = chk->compare(evt);
if (res) {
evt->set_check_id(chk->get_check_id());
}
break;
case BO_AND:
if(!res)
{
goto done;
}
res = chk->compare(evt);
if (res) {
evt->set_check_id(chk->get_check_id());
}
break;
case BO_ORNOT:
if(res)
{
goto done;
}
res = !chk->compare(evt);
if (res) {
evt->set_check_id(chk->get_check_id());
}
break;
case BO_ANDNOT:
if(!res)
{
goto done;
}
res = !chk->compare(evt);
if (res) {
evt->set_check_id(chk->get_check_id());
}
break;
default:
ASSERT(false);
break;
}
}
}
done:
return res;
}
uint8_t *gen_event_filter_expression::extract(gen_event *evt, uint32_t *len, bool sanitize_strings)
{
return NULL;
}
int32_t gen_event_filter_expression::get_expr_boolop()
{
std::vector<gen_event_filter_check*>* cks = &(m_checks);
if(cks->size() <= 1)
{
return m_boolop;
}
// Reset bit 0 to remove irrelevant not
boolop b0 = (boolop)((uint32_t)(cks->at(1)->m_boolop) & (uint32_t)~1);
if(cks->size() <= 2)
{
return b0;
}
for(uint32_t l = 2; l < cks->size(); l++)
{
if((boolop)((uint32_t)(cks->at(l)->m_boolop) & (uint32_t)~1) != b0)
{
return -1;
}
}
return b0;
}
std::set<uint16_t> gen_event_filter_expression::inverse(const std::set<uint16_t> &evttypes)
{
std::set<uint16_t> ret;
// The inverse of "all events" is still "all events". This
// ensures that when no specific set of event types are named
// in the filter that the filter still runs for all event
// types.
if(evttypes == m_expr_possible_evttypes)
{
ret = evttypes;
return ret;
}
std::set_difference(m_expr_possible_evttypes.begin(), m_expr_possible_evttypes.end(),
evttypes.begin(), evttypes.end(),
std::inserter(ret, ret.begin()));
return ret;
}
void gen_event_filter_expression::combine_evttypes(boolop op,
const std::set<uint16_t> &chk_evttypes)
{
switch(op)
{
case BO_NONE:
// Overwrite with contents of set
// Should only occur for the first check in a list
m_expr_event_types = chk_evttypes;
break;
case BO_NOT:
m_expr_event_types = inverse(chk_evttypes);
break;
case BO_ORNOT:
combine_evttypes(BO_OR, inverse(chk_evttypes));
break;
case BO_ANDNOT:
combine_evttypes(BO_AND, inverse(chk_evttypes));
break;
case BO_OR:
// Merge the event types from the
// other set into this one.
m_expr_event_types.insert(chk_evttypes.begin(), chk_evttypes.end());
break;
case BO_AND:
// Set to the intersection of event types between this
// set and the provided set.
std::set<uint16_t> intersect;
std::set_intersection(m_expr_event_types.begin(), m_expr_event_types.end(),
chk_evttypes.begin(), chk_evttypes.end(),
std::inserter(intersect, intersect.begin()));
m_expr_event_types = intersect;
break;
}
}
const std::set<uint16_t> &gen_event_filter_expression::evttypes()
{
m_expr_event_types.clear();
m_expr_possible_evttypes = possible_evttypes();
for(uint32_t i = 0; i < m_checks.size(); i++)
{
gen_event_filter_check *chk = m_checks[i];
ASSERT(chk != NULL);
const std::set<uint16_t> &chk_evttypes = m_checks[i]->evttypes();
combine_evttypes(chk->m_boolop, chk_evttypes);
}
return m_expr_event_types;
}
const std::set<uint16_t> &gen_event_filter_expression::possible_evttypes()
{
// Return the set of possible event types from the first filtercheck.
if(m_checks.size() == 0)
{
// Shouldn't happen--every filter expression should have a
// real filtercheck somewhere below it.
ASSERT(false);
m_expr_possible_evttypes = s_default_evttypes;
}
else
{
m_expr_possible_evttypes = m_checks[0]->possible_evttypes();
}
return m_expr_possible_evttypes;
}
///////////////////////////////////////////////////////////////////////////////
// sinsp_filter implementation
///////////////////////////////////////////////////////////////////////////////
gen_event_filter::gen_event_filter()
{
m_filter = new gen_event_filter_expression();
m_curexpr = m_filter;
}
gen_event_filter::~gen_event_filter()
{
if(m_filter)
{
delete m_filter;
}
}
void gen_event_filter::push_expression(boolop op)
{
gen_event_filter_expression* newexpr = new gen_event_filter_expression();
newexpr->m_boolop = op;
newexpr->m_parent = m_curexpr;
add_check((gen_event_filter_check*)newexpr);
m_curexpr = newexpr;
}
void gen_event_filter::pop_expression()
{
ASSERT(m_curexpr->m_parent != NULL);
if(m_curexpr->get_expr_boolop() == -1)
{
throw sinsp_exception("expression mixes 'and' and 'or' in an ambiguous way. Please use brackets.");
}
m_curexpr = m_curexpr->m_parent;
}
bool gen_event_filter::run(gen_event *evt)
{
return m_filter->compare(evt);
}
void gen_event_filter::add_check(gen_event_filter_check* chk)
{
m_curexpr->add_check((gen_event_filter_check *) chk);
}
std::set<uint16_t> gen_event_filter::evttypes()
{
return m_filter->evttypes();
}
bool gen_event_filter_factory::filter_field_info::is_skippable()
{
// Skip fields with the EPF_TABLE_ONLY flag.
return (tags.find("EPF_TABLE_ONLY") != tags.end());
}
uint32_t gen_event_filter_factory::filter_fieldclass_info::s_rightblock_start = 30;
uint32_t gen_event_filter_factory::filter_fieldclass_info::s_width = 120;
void gen_event_filter_factory::filter_fieldclass_info::wrapstring(const std::string &in, std::ostringstream &os)
{
std::istringstream is(in);
std::string word;
uint32_t len = 0;
while (is >> word)
{
// + 1 is trailing space.
uint32_t wordlen = word.length() + 1;
if((len + wordlen) <= (s_width-s_rightblock_start))
{
len += wordlen;
}
else
{
os << std::endl;
os << std::left << std::setw(s_rightblock_start) << " ";
len = wordlen;
}
os << word << " ";
}
}
std::string gen_event_filter_factory::filter_fieldclass_info::as_markdown(const std::set<std::string>& event_sources)
{
std::ostringstream os;
os << "## Field Class: " << name << std::endl << std::endl;
if(desc != "")
{
os << desc << std::endl << std::endl;
}
if(!event_sources.empty())
{
os << "Event Sources: ";
for(const auto &src : event_sources)
{
os << src << " ";
}
os << std::endl << std::endl;
}
os << "Name | Type | Description" << std::endl;
os << ":----|:-----|:-----------" << std::endl;
for(auto &fld_info : fields)
{
// Skip fields that should not be included
// (e.g. hidden fields)
if(fld_info.is_skippable())
{
continue;
}
os << "`" << fld_info.name << "` | " << fld_info.data_type << " | " << fld_info.desc << std::endl;
}
return os.str();
}
std::string gen_event_filter_factory::filter_fieldclass_info::as_string(bool verbose, const std::set<std::string>& event_sources)
{
std::ostringstream os;
os << "-------------------------------" << std::endl;
os << std::left << std::setw(s_rightblock_start) << "Field Class:" << name;
if(shortdesc != "")
{
os << " (" << shortdesc << ")";
}
os << std::endl;
if(desc != "")
{
os << std::left << std::setw(s_rightblock_start) << "Description:";
wrapstring(desc, os);
os << std::endl;
}
if(!event_sources.empty())
{
os << std::left << std::setw(s_rightblock_start) << "Event Sources:";
for(const auto &src : event_sources)
{
os << src << " ";
}
os << std::endl;
}
os << std::endl;
for(auto &fld_info : fields)
{
// Skip fields that should not be included
// (e.g. hidden fields)
if(fld_info.is_skippable())
{
continue;
}
if(fld_info.name.length() > s_rightblock_start)
{
os << fld_info.name << std::endl;
os << std::left << std::setw(s_rightblock_start) << " ";
}
else
{
os << std::left << std::setw(s_rightblock_start) << fld_info.name;
}
// Append any tags, and if verbose, add the type, to the description.
std::string desc = fld_info.desc;
if(!fld_info.tags.empty())
{
std::string tagsstr = "(";
for(const auto &tag : fld_info.tags)
{
if(tagsstr != "(")
{
tagsstr += ",";
}
tagsstr += tag;
}
tagsstr += ")";
desc = tagsstr + " " + desc;
}
if(verbose)
{
desc = "(Type: " + fld_info.data_type + ") " + desc;
}
wrapstring(desc, os);
os << std::endl;
}
return os.str();
}
gen_event_formatter::gen_event_formatter()
{
}
gen_event_formatter::~gen_event_formatter()
{
}
gen_event_formatter_factory::gen_event_formatter_factory()
{
}
gen_event_formatter_factory::~gen_event_formatter_factory()
{
}
|