Skip to content

Commit 189da20

Browse files
OriGlassmanrandomname21
authored andcommitted
feat(ebpf): add stdin_info to sched_process_exec
1 parent c3966af commit 189da20

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

pkg/bufferdecoder/eventsreader.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
uint64ArrT
4040
u8T
4141
timespecT
42+
stdinInfoT
4243
)
4344

4445
// These types don't match the ones defined in the ebpf code since they are not being used by syscalls arguments.
@@ -186,6 +187,8 @@ func readArgFromBuff(id events.ID, ebpfMsgDecoder *EbpfDecoder, params []trace.A
186187
err = ebpfMsgDecoder.DecodeInt64(&nsec)
187188
res = float64(sec) + (float64(nsec) / float64(1000000000))
188189

190+
case stdinInfoT:
191+
res, err = readStdinInfoFromBuff(ebpfMsgDecoder)
189192
default:
190193
// if we don't recognize the arg type, we can't parse the rest of the buffer
191194
return uint(argIdx), arg, errfmt.Errorf("error unknown arg type %v", argType)
@@ -241,19 +244,55 @@ func GetParamType(paramType string) ArgType {
241244
return uint64ArrT
242245
case "struct timespec*", "const struct timespec*":
243246
return timespecT
247+
case "struct stdin_info":
248+
return stdinInfoT
244249
default:
245250
// Default to pointer (printed as hex) for unsupported types
246251
return pointerT
247252
}
248253
}
249254

255+
func readStdinInfoFromBuff(ebpfMsgDecoder *EbpfDecoder) (map[string]string, error) {
256+
res := make(map[string]string, 5)
257+
var header int16
258+
err := ebpfMsgDecoder.DecodeInt16(&header)
259+
if err != nil {
260+
return nil, errfmt.WrapError(err)
261+
}
262+
263+
if uint64(header) == parsers.S_IFIFO.Value() {
264+
return readStdinFifoFromBuffer(ebpfMsgDecoder)
265+
}
266+
267+
if header == 0 {
268+
return nil, nil
269+
}
270+
271+
socketFamily, err := parsers.ParseSocketDomainArgument(uint64(header))
272+
if err == nil {
273+
return fillSocketInfo(ebpfMsgDecoder, res, int16(socketFamily))
274+
}
275+
276+
return nil, nil
277+
}
278+
279+
// TOOD: implement
280+
func readStdinFifoFromBuffer(ebpfMsgDecoder *EbpfDecoder) (map[string]string, error) {
281+
res := make(map[string]string, 5)
282+
return res, nil
283+
}
284+
250285
func readSockaddrFromBuff(ebpfMsgDecoder *EbpfDecoder) (map[string]string, error) {
251286
res := make(map[string]string, 5)
252287
var family int16
253288
err := ebpfMsgDecoder.DecodeInt16(&family)
254289
if err != nil {
255290
return nil, errfmt.WrapError(err)
256291
}
292+
return fillSocketInfo(ebpfMsgDecoder, res, family)
293+
}
294+
295+
func fillSocketInfo(ebpfMsgDecoder *EbpfDecoder, res map[string]string, family int16) (map[string]string, error) {
257296
socketDomainArg, err := parsers.ParseSocketDomainArgument(uint64(family))
258297
if err != nil {
259298
socketDomainArg = parsers.AF_UNSPEC

pkg/ebpf/c/tracee.bpf.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,43 @@ int tracepoint__sched__sched_process_exec(struct bpf_raw_tracepoint_args *ctx)
13691369

13701370
// clang-format on
13711371

1372+
statfunc void handle_stdin_sock(args_buffer_t *args_buf, struct file *stdin_file, u8 index)
1373+
{
1374+
struct socket *socket_from_file = (struct socket *) BPF_CORE_READ(stdin_file, private_data);
1375+
if (socket_from_file == NULL)
1376+
return;
1377+
1378+
save_sockaddr_to_buf(args_buf, socket_from_file, index);
1379+
}
1380+
1381+
struct stdin_fifo {
1382+
unsigned short file_type;
1383+
};
1384+
1385+
statfunc void handle_stdin_fifo(args_buffer_t *args_buf, struct file *stdin_file, u8 index)
1386+
{
1387+
struct stdin_fifo stdin_fifo = {.file_type = S_IFIFO};
1388+
1389+
save_to_submit_buf(args_buf, (void *) &stdin_fifo, sizeof(stdin_fifo), index);
1390+
}
1391+
1392+
statfunc void save_stdin_details(args_buffer_t *args_buf,
1393+
unsigned short stdin_type,
1394+
struct file *stdin_file,
1395+
u8 index)
1396+
{
1397+
switch (stdin_type) {
1398+
case S_IFSOCK:
1399+
handle_stdin_sock(args_buf, stdin_file, index);
1400+
break;
1401+
case S_IFIFO:
1402+
handle_stdin_fifo(args_buf, stdin_file, index);
1403+
break;
1404+
default:
1405+
return;
1406+
}
1407+
}
1408+
13721409
SEC("raw_tracepoint/sched_process_exec_event_submit_tail")
13731410
int sched_process_exec_event_submit_tail(struct bpf_raw_tracepoint_args *ctx)
13741411
{
@@ -1406,14 +1443,15 @@ int sched_process_exec_event_submit_tail(struct bpf_raw_tracepoint_args *ctx)
14061443
save_str_to_buf(&p.event->args_buf, stdin_path, 13);
14071444
save_to_submit_buf(&p.event->args_buf, &invoked_from_kernel, sizeof(int), 14);
14081445
save_str_to_buf(&p.event->args_buf, (void *) p.task_info->context.comm, 15);
1446+
save_stdin_details(&p.event->args_buf, stdin_type, stdin_file, 16);
14091447
if (p.config->options & OPT_EXEC_ENV) {
14101448
unsigned long env_start, env_end;
14111449
env_start = get_env_start_from_mm(mm);
14121450
env_end = get_env_end_from_mm(mm);
14131451
int envc = get_envc_from_bprm(bprm);
14141452

14151453
save_args_str_arr_to_buf(
1416-
&p.event->args_buf, (void *) env_start, (void *) env_end, envc, 16);
1454+
&p.event->args_buf, (void *) env_start, (void *) env_end, envc, 17);
14171455
}
14181456

14191457
events_perf_submit(&p, 0);

pkg/ebpf/c/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ enum event_id_e
131131
SECURITY_BPRM_CREDS_FOR_EXEC,
132132
SECURITY_TASK_SETRLIMIT,
133133
SECURITY_SETTIME64,
134+
SCHED_PROCESS_EXEC_SOCKET,
134135
MAX_EVENT_ID,
135136
NO_EVENT_SUBMIT,
136137

pkg/ebpf/c/vmlinux.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,17 @@ struct alloc_context {
479479
enum zone_type high_zoneidx;
480480
};
481481

482+
typedef enum
483+
{
484+
SS_FREE = 0, /* not allocated */
485+
SS_UNCONNECTED, /* unconnected to any socket */
486+
SS_CONNECTING, /* in process of connecting */
487+
SS_CONNECTED, /* connected to socket */
488+
SS_DISCONNECTING /* in process of disconnecting */
489+
} socket_state;
490+
482491
struct socket {
492+
socket_state state;
483493
short type;
484494
struct file *file;
485495
struct sock *sk;

pkg/events/core.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11230,6 +11230,7 @@ var CoreEvents = map[ID]Definition{
1123011230
{Type: "char*", Name: "stdin_path"},
1123111231
{Type: "int", Name: "invoked_from_kernel"},
1123211232
{Type: "const char*", Name: "prev_comm"},
11233+
{Type: "struct stdin_info", Name: "stdin_info"},
1123311234
{Type: "const char**", Name: "env"},
1123411235
},
1123511236
},

0 commit comments

Comments
 (0)