Skip to content

Commit f5603f9

Browse files
committed
fix(libsinsp): purge O_CLOEXEC fds on execve
O_CLOEXEC file descriptors should be removed when processing a successful execve (execveat etc.), otherwise we end up with bloating the fd tables with bogus fds. Signed-off-by: Grzegorz Nosek <grzegorz.nosek@sysdig.com>
1 parent 3c6cab4 commit f5603f9

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

userspace/libsinsp/fdinfo.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,22 @@ class SINSP_PUBLIC sinsp_fdinfo : public libsinsp::state::extensible_struct {
267267
return (m_flags & FLAGS_OVERLAY_LOWER) == FLAGS_OVERLAY_LOWER;
268268
}
269269

270+
inline bool is_close_on_exec() const {
271+
if((m_openflags & PPM_O_CLOEXEC) == PPM_O_CLOEXEC) {
272+
return true;
273+
}
274+
275+
if(m_type == SCAP_FD_EVENTPOLL && (m_openflags & PPM_EPOLL_CLOEXEC) == PPM_EPOLL_CLOEXEC) {
276+
return true;
277+
}
278+
279+
if(m_type == SCAP_FD_MEMFD && (m_openflags & PPM_MFD_CLOEXEC) == PPM_MFD_CLOEXEC) {
280+
return true;
281+
}
282+
283+
return false;
284+
}
285+
270286
void add_filename_raw(std::string_view rawpath);
271287

272288
void add_filename(std::string_view fullpath);

userspace/libsinsp/parsers.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,11 +1745,12 @@ void sinsp_parser::parse_execve_exit(sinsp_evt &evt, sinsp_parser_verdict &verdi
17451745
evt.get_tinfo()->m_gid,
17461746
must_notify_thread_group_update());
17471747
//
1748-
// execve starts with a clean fd list, so we get rid of the fd list that clone
1749-
// copied from the parent
1750-
// XXX validate this
1748+
// Purge CLOEXEC FDs on successful execve/execveat
17511749
//
1752-
// scap_fd_free_table(tinfo);
1750+
if(auto *fd_table = evt.get_tinfo()->get_fd_table(); fd_table != nullptr) {
1751+
fd_table->retain(
1752+
[&](int64_t fd, const sinsp_fdinfo &info) { return !info.is_close_on_exec(); });
1753+
}
17531754

17541755
//
17551756
// Clear the flags for this thread, making sure to propagate the inverted

0 commit comments

Comments
 (0)