Skip to content

Commit c344b52

Browse files
committed
Implement initial matchScript support by checking if the script filename
(from args[0]) ends with "test_script.sh". This provides a foundation for script-based matching that can be extended to support full path matching and pattern-based selectors in future iterations. Add infrastructure for resolved script paths including tg_script_path_map and tg_script_path_heap, with placeholder for future CWD resolution to handle relative paths. Add Signed-off-by: ariosmon <ariosmon@cisco.com>
1 parent c78811b commit c344b52

File tree

15 files changed

+999
-13
lines changed

15 files changed

+999
-13
lines changed

bpf/lib/process.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ struct binary {
320320
char end_r[STRING_POSTFIX_MAX_LENGTH];
321321
// args for the binary
322322
char args[MAXARGLENGTH];
323+
/**
324+
* filename_length - Length of script path for matchScript support
325+
*
326+
* When matchScript is enabled, this field stores the length of the
327+
* script path found in args[0]. For shebang scripts (e.g., #!/bin/bash),
328+
* this allows matching against the script path (/path/script.sh) instead
329+
* of the interpreter path (/bin/bash).
330+
*
331+
* Set during execve processing and used by matchScript selectors.
332+
* Value of 0 indicates no script path available.
333+
*/
334+
__s32 filename_length;
335+
__s32 __pad_filename; // padding for uint64 alignment
323336
// matchBinary bitset for binary
324337
// NB: everything after and including ->mb_bitset will not be zeroed on a new exec. See
325338
// binary_reset().
@@ -366,6 +379,7 @@ struct {
366379
__type(value, struct binary);
367380
} tg_binary_heap SEC(".maps");
368381

382+
369383
// Parent binaries map is used for saving actual immediate parents
370384
// for processes to get check them in matchParentBinaries selector.
371385
// If multiple execs are called in same process without fork, the map

bpf/process/bpf_execve_event.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,13 @@ execve_send(struct exec_ctx_struct *ctx __arg_ctx)
442442
with_errmetrics(probe_read, curr->bin.args, len, (char *)&event->process + off);
443443

444444
// there's a null byte between each argv element, so we terminate with
445-
// two of them to make it possible to identify the end of the buffer
446445
curr->bin.args[len] = 0x00;
447446
curr->bin.args[len + 1] = 0x00;
447+
448+
// Store filename length for matchScript support.
449+
// The filename (script path) is already in args as the first element.
450+
if (p->size_path > 0 && p->size_path < BINARY_PATH_MAX_LEN)
451+
curr->bin.filename_length = p->size_path;
448452
#else
449453
char *filename = (char *)ctx + (_(ctx->__data_loc_filename) & 0xFFFF);
450454

@@ -467,3 +471,4 @@ execve_send(struct exec_ctx_struct *ctx __arg_ctx)
467471
event_output_metric(ctx, MSG_OP_EXECVE, event, size);
468472
return 0;
469473
}
474+

bpf/process/types/basic.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,9 @@ struct match_binaries_sel_opts {
18491849
__u32 op;
18501850
__u32 map_id;
18511851
__u32 mbset_id;
1852+
// match_script: if true, match against the script path (filename from execve)
1853+
// instead of the interpreter path (exe_file) for shebang scripts
1854+
__u32 match_script;
18521855
};
18531856

18541857
// We need data for:
@@ -1904,7 +1907,20 @@ FUNC_INLINE int match_binaries(__u32 key, struct execve_map_value *current, stru
19041907
if (selector_options->op == op_filter_none)
19051908
return 1; // matchBinaries selector is empty <=> match
19061909

1907-
if (bin->path_length < 0) {
1910+
// Select which path to match against based on match_script option.
1911+
// If match_script is true, use the resolved script path from tg_script_path_map.
1912+
// This handles both absolute and relative paths (resolved at execve time).
1913+
// Otherwise use path (exe_file, which is the interpreter for shebang scripts).
1914+
char *match_path = bin->path;
1915+
__s32 match_path_length = bin->path_length;
1916+
1917+
if (selector_options->match_script) {
1918+
// Use script path (bin->args) instead of interpreter path (bin->path)
1919+
match_path = bin->args;
1920+
match_path_length = bin->filename_length;
1921+
}
1922+
1923+
if (match_path_length < 0) {
19081924
// something wrong happened when copying the filename to execve_map
19091925
return 0;
19101926
}
@@ -1928,7 +1944,7 @@ FUNC_INLINE int match_binaries(__u32 key, struct execve_map_value *current, stru
19281944
path_map = map_lookup_elem(&tg_mb_paths, &key);
19291945
if (!path_map)
19301946
return 0;
1931-
found_key = map_lookup_elem(path_map, bin->path);
1947+
found_key = map_lookup_elem(path_map, match_path);
19321948
break;
19331949
#ifdef __LARGE_BPF_PROG
19341950
case op_filter_str_prefix:

0 commit comments

Comments
 (0)