Skip to content

Commit 796eaf6

Browse files
committed
bpf: add support for op_capabilities_gained
Support the op_capabilities_gained operator. The operator is treated as a specifial case. The second argument index is extracted from the values, from which we get the args offset and, finally, the value. The CapabilitiesGained operator checks whether a capability argument has gained capabilities compared to another argument. For example: ``` apiVersion: cilium.io/v1alpha1 kind: TracingPolicy metadata: name: "capabilities-gained" spec: kprobes: - call: "security_capset" syscall: false return: true args: - index: 1 type: "cap_effective" resolve: "cap_effective" label: "old_creds.cap_effective" - index: 2 type: "cap_effective" label: "cap_effective" returnArg: index: 0 type: "int" selectors: - matchActions: - action: Post matchArgs: - operator: CapabilitiesGained args: - 0 - 1 ``` Signed-off-by: Kornilios Kourtis <[email protected]>
1 parent 41ee9a9 commit 796eaf6

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

bpf/process/types/basic.h

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,13 +1561,24 @@ FUNC_INLINE int match_binaries(__u32 selidx, struct execve_map_value *current)
15611561
return 1;
15621562
}
15631563

1564+
FUNC_INLINE char *
1565+
get_arg(struct msg_generic_kprobe *e, __u32 index)
1566+
{
1567+
long argoff;
1568+
1569+
asm volatile("%[index] &= 0x7;\n" : [index] "+r"(index));
1570+
argoff = e->argsoff[index];
1571+
asm volatile("%[argoff] &= 0x7ff;\n" : [argoff] "+r"(argoff));
1572+
return &e->args[argoff];
1573+
}
1574+
15641575
FUNC_INLINE int
15651576
selector_arg_offset(__u8 *f, struct msg_generic_kprobe *e, __u32 selidx,
15661577
bool is_entry)
15671578
{
15681579
struct selector_arg_filters *filters;
15691580
struct selector_arg_filter *filter;
1570-
long seloff, argoff, argsoff, pass = 1, margsoff;
1581+
long seloff, argsoff, pass = 1, margsoff;
15711582
__u32 i = 0, index;
15721583
char *args;
15731584

@@ -1619,13 +1630,7 @@ selector_arg_offset(__u8 *f, struct msg_generic_kprobe *e, __u32 selidx,
16191630
if (index > 5)
16201631
return 0;
16211632

1622-
asm volatile("%[index] &= 0x7;\n"
1623-
: [index] "+r"(index));
1624-
argoff = e->argsoff[index];
1625-
asm volatile("%[argoff] &= 0x7ff;\n"
1626-
: [argoff] "+r"(argoff));
1627-
args = &e->args[argoff];
1628-
1633+
args = get_arg(e, index);
16291634
switch (filter->type) {
16301635
case fd_ty:
16311636
/* Advance args past fd */
@@ -1650,13 +1655,21 @@ selector_arg_offset(__u8 *f, struct msg_generic_kprobe *e, __u32 selidx,
16501655
*/
16511656
pass &= filter_char_buf(filter, args, 8);
16521657
break;
1653-
case syscall64_type:
1654-
case s64_ty:
1655-
case u64_ty:
1656-
case kernel_cap_ty:
16571658
case cap_inh_ty:
16581659
case cap_prm_ty:
16591660
case cap_eff_ty:
1661+
case kernel_cap_ty:
1662+
if (filter->op == op_capabilities_gained) {
1663+
__u64 cap_old = *(__u64 *)args;
1664+
__u32 index2 = *((__u32 *)&filter->value);
1665+
__u64 cap_new = *(__u64 *)get_arg(e, index2);
1666+
pass = !!((cap_old ^ cap_new) & cap_new);
1667+
break;
1668+
}
1669+
/* falltrough */
1670+
case syscall64_type:
1671+
case s64_ty:
1672+
case u64_ty:
16601673
pass &= filter_64ty(filter, args);
16611674
break;
16621675
case size_type:

bpf/process/types/operations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum {
4141
// more socket ops
4242
op_filter_family = 28,
4343
op_filter_state = 29,
44+
// capability ops
45+
op_capabilities_gained = 30,
4446
};
4547

4648
#endif // __OPERATIONS_H__

0 commit comments

Comments
 (0)