Skip to content

Commit aaf688d

Browse files
ekoopspoiana
authored andcommitted
feat(userspace/libsinsp)!: pass notify into set_user signature
Pass `notify` as `sinsp_threadinfo::set_user()` flag to enable external control over thread user update notification. BREAKING CHANGE: update `sinsp_threadinfo::set_user()`, `sinsp_threadinfo::init()` signatures and `user_group_updater` constructor. Signed-off-by: Leonardo Di Giovanna <[email protected]>
1 parent a358970 commit aaf688d

File tree

8 files changed

+49
-29
lines changed

8 files changed

+49
-29
lines changed

userspace/libsinsp/parsers.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ void sinsp_parser::parse_clone_exit_caller(sinsp_evt *evt, int64_t child_tid) {
11711171
default:
11721172
ASSERT(false);
11731173
}
1174-
child_tinfo->set_user(uid);
1174+
child_tinfo->set_user(uid, must_notify_thread_user_update());
11751175

11761176
/* gid */
11771177
int32_t gid = 0;
@@ -1691,7 +1691,7 @@ void sinsp_parser::parse_clone_exit_child(sinsp_evt *evt) {
16911691
default:
16921692
ASSERT(false);
16931693
}
1694-
child_tinfo->set_user(uid);
1694+
child_tinfo->set_user(uid, must_notify_thread_user_update());
16951695

16961696
/* gid */
16971697
int32_t gid = 0;
@@ -2174,7 +2174,8 @@ void sinsp_parser::parse_execve_exit(sinsp_evt *evt) {
21742174

21752175
// Get uid
21762176
if(evt->get_num_params() > 26) {
2177-
evt->get_tinfo()->set_user(evt->get_param(26)->as<uint32_t>());
2177+
evt->get_tinfo()->set_user(evt->get_param(26)->as<uint32_t>(),
2178+
must_notify_thread_user_update());
21782179
}
21792180

21802181
// Get pgid
@@ -4524,7 +4525,7 @@ void sinsp_parser::parse_setresuid_exit(sinsp_evt *evt) {
45244525
if(new_euid < std::numeric_limits<uint32_t>::max()) {
45254526
sinsp_threadinfo *ti = evt->get_thread_info();
45264527
if(ti) {
4527-
ti->set_user(new_euid);
4528+
ti->set_user(new_euid, must_notify_thread_user_update());
45284529
}
45294530
}
45304531
}
@@ -4544,7 +4545,7 @@ void sinsp_parser::parse_setreuid_exit(sinsp_evt *evt) {
45444545
if(new_euid < std::numeric_limits<uint32_t>::max()) {
45454546
sinsp_threadinfo *ti = evt->get_thread_info();
45464547
if(ti) {
4547-
ti->set_user(new_euid);
4548+
ti->set_user(new_euid, must_notify_thread_user_update());
45484549
}
45494550
}
45504551
}
@@ -4604,7 +4605,7 @@ void sinsp_parser::parse_setuid_exit(sinsp_evt *evt) {
46044605
uint32_t new_euid = enter_evt->get_param(0)->as<uint32_t>();
46054606
sinsp_threadinfo *ti = evt->get_thread_info();
46064607
if(ti) {
4607-
ti->set_user(new_euid);
4608+
ti->set_user(new_euid, must_notify_thread_user_update());
46084609
}
46094610
}
46104611
}

userspace/libsinsp/parsers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ class sinsp_parser {
193193
return (m_sinsp_mode.is_live() || is_syscall_plugin_enabled()) && m_large_envs_enabled;
194194
}
195195

196+
bool must_notify_thread_user_update() const {
197+
return m_sinsp_mode.is_live() || is_syscall_plugin_enabled();
198+
}
199+
196200
// TODO(ekoops): replace references and pointers with owned resources as we determine they
197201
// cannot change at runtime and/or are used only by the parser.
198202
// The following fields are externally provided and access to them is expected to be read-only.

userspace/libsinsp/sinsp.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ void sinsp::on_new_entry_from_proc(void* context,
903903

904904
threadinfo_map_t::ptr_t sinsp_tinfo;
905905
auto newti = m_threadinfo_factory.create();
906-
newti->init(*tinfo, large_envs_enabled());
906+
newti->init(*tinfo, large_envs_enabled(), must_notify_thread_user_update());
907907
if(is_nodriver()) {
908908
auto existing_tinfo = find_thread(tid, true);
909909
if(existing_tinfo == nullptr || newti->m_clone_ts > existing_tinfo->m_clone_ts) {
@@ -976,8 +976,7 @@ void sinsp::on_new_entry_from_proc(void* context,
976976
}
977977

978978
auto newti = m_threadinfo_factory.create();
979-
newti->init(*tinfo, large_envs_enabled());
980-
979+
newti->init(*tinfo, large_envs_enabled(), must_notify_thread_user_update());
981980
sinsp_tinfo = m_thread_manager->add_thread(std::move(newti), true);
982981
if(sinsp_tinfo == nullptr) {
983982
ASSERT(false);
@@ -1381,7 +1380,7 @@ int32_t sinsp::next(sinsp_evt** puevt) {
13811380
// upon threadinfo's container_id changes.
13821381
// Since the threadinfo state might get changed from a plugin parser,
13831382
// evaluate this one after all parsers get run.
1384-
user_group_updater usr_grp_updater(evt);
1383+
user_group_updater usr_grp_updater(evt, must_notify_thread_user_update());
13851384

13861385
if(!evt->is_filtered_out()) {
13871386
//

userspace/libsinsp/sinsp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,8 @@ class SINSP_PUBLIC sinsp : public capture_stats_source {
916916
return left == static_cast<uint64_t>(-1) || left <= right;
917917
}
918918

919+
bool must_notify_thread_user_update() const { return m_mode.is_live() || is_syscall_plugin(); }
920+
919921
std::shared_ptr<sinsp_stats_v2> m_sinsp_stats_v2;
920922
scap_t* m_h;
921923
struct scap_platform* m_platform{};

userspace/libsinsp/thread_manager.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,10 @@ const threadinfo_map_t::ptr_t& sinsp_thread_manager::get_thread_ref(int64_t tid,
839839
}
840840

841841
if(have_scap_proc) {
842-
newti->init(scap_proc, m_inspector->large_envs_enabled());
842+
const bool can_load_env_from_proc = m_inspector->large_envs_enabled();
843+
const bool must_notify_user_update =
844+
m_inspector->is_live() || m_inspector->is_syscall_plugin();
845+
newti->init(scap_proc, can_load_env_from_proc, must_notify_user_update);
843846
} else {
844847
//
845848
// Add a fake entry to avoid a continuous lookup

userspace/libsinsp/threadinfo.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ sinsp_fdinfo* sinsp_threadinfo::add_fd_from_scap(const scap_fdinfo& fdi,
326326
return m_fdtable.add(fdi.fd, std::move(newfdi));
327327
}
328328

329-
void sinsp_threadinfo::init(const scap_threadinfo& pinfo, const bool can_load_env_from_proc) {
329+
void sinsp_threadinfo::init(const scap_threadinfo& pinfo,
330+
const bool can_load_env_from_proc,
331+
const bool notify_user_update) {
330332
init();
331333

332334
m_tid = pinfo.tid;
@@ -388,7 +390,7 @@ void sinsp_threadinfo::init(const scap_threadinfo& pinfo, const bool can_load_en
388390
ASSERT(m_inspector);
389391

390392
set_group(pinfo.gid);
391-
set_user(pinfo.uid);
393+
set_user(pinfo.uid, notify_user_update);
392394
set_loginuid((uint32_t)pinfo.loginuid);
393395
}
394396

@@ -457,20 +459,20 @@ std::string sinsp_threadinfo::get_container_ip() {
457459
return ip;
458460
}
459461

460-
void sinsp_threadinfo::set_user(uint32_t uid) {
462+
void sinsp_threadinfo::set_user(const uint32_t uid, const bool notify) {
461463
const auto container_id = get_container_id();
462464
m_uid = uid;
463-
if(const scap_userinfo* user = m_inspector->m_usergroup_manager->get_user(container_id, uid);
464-
!user) {
465-
const auto notify = m_inspector->is_live() || m_inspector->is_syscall_plugin();
466-
// For uid 0 force set root related infos
467-
if(uid == 0) {
468-
m_inspector->m_usergroup_manager
469-
->add_user(container_id, m_pid, uid, m_gid, "root", "/root", {}, notify);
470-
} else {
471-
m_inspector->m_usergroup_manager
472-
->add_user(container_id, m_pid, uid, m_gid, {}, {}, {}, notify);
473-
}
465+
// Do not notify if the user is already present.
466+
if(m_inspector->m_usergroup_manager->get_user(container_id, uid)) {
467+
return;
468+
}
469+
// For uid 0 force set root related infos
470+
if(uid == 0) {
471+
m_inspector->m_usergroup_manager
472+
->add_user(container_id, m_pid, uid, m_gid, "root", "/root", {}, notify);
473+
} else {
474+
m_inspector->m_usergroup_manager
475+
->add_user(container_id, m_pid, uid, m_gid, {}, {}, {}, notify);
474476
}
475477
}
476478

userspace/libsinsp/threadinfo.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,12 @@ class SINSP_PUBLIC sinsp_threadinfo : public libsinsp::state::table_entry {
409409
*/
410410
std::string get_path_for_dir_fd(int64_t dir_fd);
411411

412-
void set_user(uint32_t uid);
412+
/*!
413+
\brief Set the thread user and optionally notify any interested component.
414+
\param uid The user id.
415+
\param notify A boolean indicating if any interested component must be notified of the update.
416+
*/
417+
void set_user(uint32_t uid, bool notify);
413418
void set_group(uint32_t gid);
414419
void set_loginuid(uint32_t loginuid);
415420

@@ -529,7 +534,7 @@ class SINSP_PUBLIC sinsp_threadinfo : public libsinsp::state::table_entry {
529534
}
530535

531536
void init();
532-
void init(const scap_threadinfo& pinfo, bool can_load_env_from_proc);
537+
void init(const scap_threadinfo& pinfo, bool can_load_env_from_proc, bool notify_user_update);
533538
void fix_sockets_coming_from_proc(const std::set<uint16_t>& ipv4_server_ports,
534539
bool resolve_hostname_and_port);
535540
sinsp_fdinfo* add_fd(int64_t fd, std::shared_ptr<sinsp_fdinfo>&& fdinfo);

userspace/libsinsp/user.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ class sinsp_usergroup_manager {
209209
// RAII struct to manage threadinfos automatic user/group refresh
210210
// upon container_id updates.
211211
struct user_group_updater {
212-
explicit user_group_updater(sinsp_evt *evt): m_check_cleanup(false), m_evt(nullptr) {
212+
explicit user_group_updater(sinsp_evt *const evt, const bool must_notify_user_update):
213+
m_check_cleanup(false),
214+
m_evt(nullptr),
215+
m_must_notify_user_update{must_notify_user_update} {
213216
switch(evt->get_type()) {
214217
case PPME_PROCEXIT_E:
215218
case PPME_PROCEXIT_1_E:
@@ -253,7 +256,7 @@ struct user_group_updater {
253256
if(container_id != m_container_id) {
254257
// Refresh user/group
255258
tinfo->set_group(tinfo->m_gid);
256-
tinfo->set_user(tinfo->m_uid);
259+
tinfo->set_user(tinfo->m_uid, m_must_notify_user_update);
257260
} else if(m_check_cleanup && !container_id.empty()) {
258261
if(tinfo->m_vtid == tinfo->m_vpid && tinfo->m_vpid == 1) {
259262
// main container process left, clean up user and groups for the container
@@ -270,6 +273,7 @@ struct user_group_updater {
270273
bool m_check_cleanup;
271274
sinsp_evt *m_evt;
272275
std::string m_container_id;
276+
bool m_must_notify_user_update;
273277
};
274278

275279
#endif // FALCOSECURITY_LIBS_USER_H

0 commit comments

Comments
 (0)