Skip to content

Commit 60b1afb

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 e2909b3 commit 60b1afb

2 files changed

Lines changed: 32 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: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,11 +1745,23 @@ 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+
std::vector<int64_t> cloexec_fds;
1752+
size_t total_fds = 0;
1753+
fd_table->const_loop([&](int64_t fd, const sinsp_fdinfo &info) {
1754+
total_fds++;
1755+
if(info.is_close_on_exec()) {
1756+
cloexec_fds.push_back(fd);
1757+
}
1758+
return true;
1759+
});
1760+
1761+
for(const auto fd : cloexec_fds) {
1762+
evt.get_tinfo()->remove_fd(fd);
1763+
}
1764+
}
17531765

17541766
//
17551767
// Clear the flags for this thread, making sure to propagate the inverted

0 commit comments

Comments
 (0)