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
|
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _WIN32
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <algorithm>
#endif
#include "sinsp.h"
#include "sinsp_int.h"
#include "scap-int.h"
///////////////////////////////////////////////////////////////////////////////
// sinsp_fdinfo implementation
///////////////////////////////////////////////////////////////////////////////
template<> sinsp_fdinfo_t::sinsp_fdinfo()
{
m_type = SCAP_FD_UNINITIALIZED;
m_flags = FLAGS_NONE;
m_callbacks = NULL;
m_usrstate = NULL;
}
template<> void sinsp_fdinfo_t::reset()
{
m_type = SCAP_FD_UNINITIALIZED;
m_flags = FLAGS_NONE;
delete(m_callbacks);
m_callbacks = NULL;
m_usrstate = NULL;
}
template<> string* sinsp_fdinfo_t::tostring()
{
return &m_name;
}
template<> char sinsp_fdinfo_t::get_typechar()
{
switch(m_type)
{
case SCAP_FD_FILE_V2:
case SCAP_FD_FILE:
return CHAR_FD_FILE;
case SCAP_FD_IPV4_SOCK:
return CHAR_FD_IPV4_SOCK;
case SCAP_FD_IPV6_SOCK:
return CHAR_FD_IPV6_SOCK;
case SCAP_FD_DIRECTORY:
return CHAR_FD_DIRECTORY;
case SCAP_FD_IPV4_SERVSOCK:
return CHAR_FD_IPV4_SERVSOCK;
case SCAP_FD_IPV6_SERVSOCK:
return CHAR_FD_IPV6_SERVSOCK;
case SCAP_FD_FIFO:
return CHAR_FD_FIFO;
case SCAP_FD_UNIX_SOCK:
return CHAR_FD_UNIX_SOCK;
case SCAP_FD_EVENT:
return CHAR_FD_EVENT;
case SCAP_FD_UNKNOWN:
return CHAR_FD_UNKNOWN;
case SCAP_FD_UNSUPPORTED:
return CHAR_FD_UNSUPPORTED;
case SCAP_FD_SIGNALFD:
return CHAR_FD_SIGNAL;
case SCAP_FD_EVENTPOLL:
return CHAR_FD_EVENTPOLL;
case SCAP_FD_INOTIFY:
return CHAR_FD_INOTIFY;
case SCAP_FD_TIMERFD:
return CHAR_FD_TIMERFD;
case SCAP_FD_NETLINK:
return CHAR_FD_NETLINK;
default:
// ASSERT(false);
return '?';
}
}
template<> char* sinsp_fdinfo_t::get_typestring()
{
switch(m_type)
{
case SCAP_FD_FILE_V2:
case SCAP_FD_FILE:
return (char*)"file";
case SCAP_FD_DIRECTORY:
return (char*)"directory";
case SCAP_FD_IPV4_SOCK:
case SCAP_FD_IPV4_SERVSOCK:
return (char*)"ipv4";
case SCAP_FD_IPV6_SOCK:
case SCAP_FD_IPV6_SERVSOCK:
return (char*)"ipv6";
case SCAP_FD_UNIX_SOCK:
return (char*)"unix";
case SCAP_FD_FIFO:
return (char*)"pipe";
case SCAP_FD_EVENT:
return (char*)"event";
case SCAP_FD_SIGNALFD:
return (char*)"signalfd";
case SCAP_FD_EVENTPOLL:
return (char*)"eventpoll";
case SCAP_FD_INOTIFY:
return (char*)"inotify";
case SCAP_FD_TIMERFD:
return (char*)"timerfd";
case SCAP_FD_NETLINK:
return (char*)"netlink";
default:
return (char*)"<NA>";
}
}
template<> string sinsp_fdinfo_t::tostring_clean()
{
string m_tstr = m_name;
sanitize_string(m_tstr);
return m_tstr;
}
template<> void sinsp_fdinfo_t::add_filename(const char* fullpath)
{
m_name = fullpath;
}
template<> bool sinsp_fdinfo_t::set_net_role_by_guessing(sinsp* inspector,
sinsp_threadinfo* ptinfo,
sinsp_fdinfo_t* pfdinfo,
bool incoming)
{
//
// If this process owns the port, mark it as server, otherwise mark it as client
//
if(ptinfo->is_bound_to_port(pfdinfo->m_sockinfo.m_ipv4info.m_fields.m_dport))
{
if(ptinfo->uses_client_port(pfdinfo->m_sockinfo.m_ipv4info.m_fields.m_sport))
{
goto wildass_guess;
}
pfdinfo->set_role_server();
return true;
}
else
{
pfdinfo->set_role_client();
return true;
}
wildass_guess:
if(!(pfdinfo->m_flags & (sinsp_fdinfo_t::FLAGS_ROLE_CLIENT | sinsp_fdinfo_t::FLAGS_ROLE_SERVER)))
{
//
// We just assume that a server usually starts with a read and a client with a write
//
if(incoming)
{
pfdinfo->set_role_server();
}
else
{
pfdinfo->set_role_client();
}
}
return true;
}
template<> scap_l4_proto sinsp_fdinfo_t::get_l4proto()
{
scap_fd_type evt_type = m_type;
if(evt_type == SCAP_FD_IPV4_SOCK)
{
if((scap_l4_proto)m_sockinfo.m_ipv4info.m_fields.m_l4proto == SCAP_L4_RAW)
{
return SCAP_L4_RAW;
}
if(is_role_none())
{
return SCAP_L4_NA;
}
return (scap_l4_proto)(m_sockinfo.m_ipv4info.m_fields.m_l4proto);
}
else if(evt_type == SCAP_FD_IPV4_SERVSOCK)
{
return (scap_l4_proto)(m_sockinfo.m_ipv4serverinfo.m_l4proto);
}
else if(evt_type == SCAP_FD_IPV6_SOCK)
{
if((scap_l4_proto)m_sockinfo.m_ipv6info.m_fields.m_l4proto == SCAP_L4_RAW)
{
return SCAP_L4_RAW;
}
if(is_role_none())
{
return SCAP_L4_NA;
}
return (scap_l4_proto)(m_sockinfo.m_ipv6info.m_fields.m_l4proto);
}
else if(evt_type == SCAP_FD_IPV6_SERVSOCK)
{
return (scap_l4_proto)(m_sockinfo.m_ipv6serverinfo.m_l4proto);
}
else
{
return SCAP_L4_NA;
}
}
template<> void sinsp_fdinfo_t::register_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec)
{
if(this->m_callbacks == NULL)
{
m_callbacks = new fd_callbacks_info();
}
switch(etype)
{
case CT_READ:
m_callbacks->m_read_callbacks.push_back(dec);
break;
case CT_WRITE:
m_callbacks->m_write_callbacks.push_back(dec);
break;
default:
ASSERT(false);
break;
}
return;
}
template<> void sinsp_fdinfo_t::unregister_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec)
{
vector<sinsp_protodecoder*>::iterator it;
if(m_callbacks == NULL)
{
ASSERT(false);
return;
}
switch(etype)
{
case CT_READ:
for(it = m_callbacks->m_read_callbacks.begin(); it != m_callbacks->m_read_callbacks.end(); ++it)
{
if(*it == dec)
{
m_callbacks->m_read_callbacks.erase(it);
return;
}
}
break;
case CT_WRITE:
for(it = m_callbacks->m_write_callbacks.begin(); it != m_callbacks->m_write_callbacks.end(); ++it)
{
if(*it == dec)
{
m_callbacks->m_write_callbacks.erase(it);
return;
}
}
break;
default:
ASSERT(false);
break;
}
return;
}
///////////////////////////////////////////////////////////////////////////////
// sinsp_fdtable implementation
///////////////////////////////////////////////////////////////////////////////
sinsp_fdtable::sinsp_fdtable(sinsp* inspector)
{
m_inspector = inspector;
reset_cache();
}
sinsp_fdinfo_t* sinsp_fdtable::add(int64_t fd, sinsp_fdinfo_t* fdinfo)
{
//
// Look for the FD in the table
//
auto it = m_table.find(fd);
// Three possible exits here:
// 1. fd is not on the table
// a. the table size is under the limit so create a new entry
// b. table size is over the limit, discard the fd
// 2. fd is already in the table, replace it
if(it == m_table.end())
{
if(m_table.size() < m_inspector->m_max_fdtable_size)
{
//
// No entry in the table, this is the normal case
//
m_last_accessed_fd = -1;
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_added_fds++;
#endif
pair<unordered_map<int64_t, sinsp_fdinfo_t>::iterator, bool> insert_res = m_table.emplace(fd, *fdinfo);
return &(insert_res.first->second);
}
else
{
return nullptr;
}
}
else
{
//
// the fd is already in the table.
//
if(it->second.m_flags & sinsp_fdinfo_t::FLAGS_CLOSE_IN_PROGRESS)
{
//
// Sometimes an FD-creating syscall can be called on an FD that is being closed (i.e
// the close enter has arrived but the close exit has not arrived yet).
// If this is the case, mark the new entry so that the successive close exit won't
// destroy it.
//
fdinfo->m_flags &= ~sinsp_fdinfo_t::FLAGS_CLOSE_IN_PROGRESS;
fdinfo->m_flags |= sinsp_fdinfo_t::FLAGS_CLOSE_CANCELED;
m_table[CANCELED_FD_NUMBER] = it->second;
}
else
{
//
// This can happen if:
// - the event is a dup2 or dup3 that overwrites an existing FD (perfectly legal)
// - a close() has been dropped when capturing
// - an fd has been closed by clone() or execve() (it happens when the fd is opened with the FD_CLOEXEC flag,
// which we don't currently parse.
// In either case, removing the old fd, replacing it with the new one and keeping going is a reasonable
// choice. We include an assertion to catch the situation.
//
// XXX Can't have this enabled until the FD_CLOEXEC flag is supported
//ASSERT(false);
}
//
// Replace the fd as a struct copy
//
it->second.copy(*fdinfo, true);
return &(it->second);
}
}
void sinsp_fdtable::erase(int64_t fd)
{
unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit = m_table.find(fd);
if(fd == m_last_accessed_fd)
{
m_last_accessed_fd = -1;
}
if(fdit == m_table.end())
{
//
// Looks like there's no fd to remove.
// Either the fd creation event was dropped or (more likely) our logic doesn't support the
// call that created this fd. The assertion will detect it, while in release mode we just
// keep going.
//
ASSERT(false);
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_failed_fd_lookups++;
#endif
}
else
{
m_table.erase(fdit);
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_noncached_fd_lookups++;
m_inspector->m_stats.m_n_removed_fds++;
#endif
}
}
void sinsp_fdtable::clear()
{
m_table.clear();
}
size_t sinsp_fdtable::size()
{
return m_table.size();
}
void sinsp_fdtable::reset_cache()
{
m_last_accessed_fd = -1;
}
void sinsp_fdtable::lookup_device(sinsp_fdinfo_t* fdi, uint64_t fd)
{
#ifdef HAS_CAPTURE
#ifndef WIN32
if(m_inspector->is_capture())
{
return;
}
if(fdi->is_file() && fdi->m_dev == 0 && fdi->m_mount_id != 0)
{
char procdir[SCAP_MAX_PATH_SIZE];
snprintf(procdir, sizeof(procdir), "%s/proc/%ld/", scap_get_host_root(), m_tid);
fdi->m_dev = scap_get_device_by_mount_id(m_inspector->m_h, procdir, fdi->m_mount_id);
fdi->m_mount_id = 0; // don't try again
}
#endif // WIN32
#endif // HAS_CAPTURE
}
|