Skip to content

Commit 93a0bbb

Browse files
authored
Add new filter for open+create/create with exec permissions (draios#1637)
* Add new filter for open/create with exec permissions
1 parent 8f361e4 commit 93a0bbb

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed

driver/flags_table.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const struct ppm_name_value file_flags[] = {
6969
{"O_RDONLY", PPM_O_RDONLY},
7070
{"O_CLOEXEC", PPM_O_CLOEXEC},
7171
{"O_NONE", PPM_O_NONE},
72+
{"O_TMPFILE", PPM_O_TMPFILE},
7273
{0, 0},
7374
};
7475

driver/ppm_events_public.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ or GPL2.txt for full copies of the license.
9898
#define PPM_O_DIRECTORY (1 << 10)
9999
#define PPM_O_LARGEFILE (1 << 11)
100100
#define PPM_O_CLOEXEC (1 << 12)
101+
#define PPM_O_TMPFILE (1 << 13)
101102

102103
/*
103104
* File modes

driver/ppm_flag_helpers.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ or GPL2.txt for full copies of the license.
99

1010
#ifndef PPM_FLAG_HELPERS_H_
1111
#define PPM_FLAG_HELPERS_H_
12+
#define _GNU_SOURCE
1213
#include <linux/mman.h>
1314
#include <linux/futex.h>
1415
#include <linux/ptrace.h>
15-
1616
#include "ppm.h"
1717

1818
#define PPM_MS_MGC_MSK 0xffff0000
@@ -37,6 +37,9 @@ static __always_inline uint32_t open_flags_to_scap(unsigned long flags)
3737
if (flags & O_CREAT)
3838
res |= PPM_O_CREAT;
3939

40+
if (flags & O_TMPFILE)
41+
res |= PPM_O_TMPFILE;
42+
4043
if (flags & O_APPEND)
4144
res |= PPM_O_APPEND;
4245

userspace/libscap/scap_fds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ See the License for the specific language governing permissions and
1616
limitations under the License.
1717
1818
*/
19+
#define _GNU_SOURCE
1920

2021
#include <stdio.h>
2122
#include <stdlib.h>
@@ -776,6 +777,9 @@ static inline uint32_t open_flags_to_scap(unsigned long flags)
776777

777778
if (flags & O_CREAT)
778779
res |= PPM_O_CREAT;
780+
781+
if (flags & O_TMPFILE)
782+
res |= PPM_O_TMPFILE;
779783

780784
if (flags & O_APPEND)
781785
res |= PPM_O_APPEND;

userspace/libsinsp/filterchecks.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,7 @@ const filtercheck_field_info sinsp_filter_check_event_fields[] =
28562856
{PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "evt.infra.docker.container.id", "for docker infrastructure events, the id of the impacted container."},
28572857
{PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "evt.infra.docker.container.name", "for docker infrastructure events, the name of the impacted container."},
28582858
{PT_CHARBUF, EPF_TABLE_ONLY, PF_NA, "evt.infra.docker.container.image", "for docker infrastructure events, the image name of the impacted container."},
2859+
{PT_BOOL, EPF_NONE, PF_NA, "evt.is_open_exec", "'true' for open/openat or creat events where a file is created with execute permissions"},
28592860
};
28602861

28612862
sinsp_filter_check_event::sinsp_filter_check_event()
@@ -4386,17 +4387,19 @@ uint8_t* sinsp_filter_check_event::extract(sinsp_evt *evt, OUT uint32_t* len, bo
43864387
break;
43874388
case TYPE_ISOPEN_READ:
43884389
case TYPE_ISOPEN_WRITE:
4390+
case TYPE_ISOPEN_EXEC:
43894391
{
43904392
uint16_t etype = evt->get_type();
43914393

43924394
m_u32val = 0;
4395+
sinsp_evt_param *parinfo;
4396+
// If any of the exec bits is on, we consider this an open+exec
4397+
uint32_t is_exec_mask = (PPM_S_IXUSR | PPM_S_IXGRP | PPM_S_IXOTH);
43934398

43944399
if(etype == PPME_SYSCALL_OPEN_X ||
43954400
etype == PPME_SYSCALL_OPENAT_E ||
43964401
etype == PPME_SYSCALL_OPENAT_2_X)
43974402
{
4398-
sinsp_evt_param *parinfo;
4399-
44004403
// For both OPEN_X and OPENAT_E,
44014404
// flags is the 3rd argument.
44024405
parinfo = evt->get_param(etype == PPME_SYSCALL_OPENAT_2_X ? 3 : 2);
@@ -4417,6 +4420,21 @@ uint8_t* sinsp_filter_check_event::extract(sinsp_evt *evt, OUT uint32_t* len, bo
44174420
{
44184421
m_u32val = 1;
44194422
}
4423+
4424+
if(m_field_id == TYPE_ISOPEN_EXEC && ((flags & PPM_O_TMPFILE) || (flags & PPM_O_CREAT)))
4425+
{
4426+
parinfo = evt->get_param(etype == PPME_SYSCALL_OPENAT_2_X ? 4 : 3);
4427+
ASSERT(parinfo->m_len == sizeof(uint32_t));
4428+
uint32_t mode_bits = *(uint32_t *)parinfo->m_val;
4429+
m_u32val = (mode_bits & is_exec_mask)? 1 : 0;
4430+
}
4431+
}
4432+
else if ((m_field_id == TYPE_ISOPEN_EXEC) && (etype == PPME_SYSCALL_CREAT_X))
4433+
{
4434+
parinfo = evt->get_param(2);
4435+
ASSERT(parinfo->m_len == sizeof(uint32_t));
4436+
uint32_t mode_bits = *(uint32_t *)parinfo->m_val;
4437+
m_u32val = (mode_bits & is_exec_mask)? 1 : 0;
44204438
}
44214439

44224440
RETURN_EXTRACT_VAR(m_u32val);

userspace/libsinsp/filterchecks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ class sinsp_filter_check_event : public sinsp_filter_check
446446
TYPE_INFRA_DOCKER_CONTAINER_ID = 64,
447447
TYPE_INFRA_DOCKER_CONTAINER_NAME = 65,
448448
TYPE_INFRA_DOCKER_CONTAINER_IMAGE = 66,
449+
TYPE_ISOPEN_EXEC = 67,
449450
};
450451

451452
sinsp_filter_check_event();

0 commit comments

Comments
 (0)