Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(driver): fix bpf probe verifier on linux 5.14 and llvm 12.0.1 #81

Merged
merged 6 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 21 additions & 18 deletions driver/bpf/filler_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,17 +719,20 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
{
unsigned int len_dyn = 0;
unsigned int len;
unsigned long curoff_bounded;

curoff_bounded = data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF;
if (data->state->tail_ctx.curoff > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

FedeDP marked this conversation as resolved.
Show resolved Hide resolved
if (dyn_idx != (u8)-1) {
*((u8 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = dyn_idx;
*((u8 *)&data->buf[curoff_bounded]) = dyn_idx;
len_dyn = sizeof(u8);
data->state->tail_ctx.curoff += len_dyn;
data->state->tail_ctx.len += len_dyn;
}

curoff_bounded = data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF;
if (data->state->tail_ctx.curoff > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

Expand All @@ -740,7 +743,7 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
if (!data->curarg_already_on_frame) {
int res;

res = bpf_probe_read_str(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF],
res = bpf_probe_read_str(&data->buf[curoff_bounded],
PPM_MAX_ARG_SIZE,
(const void *)val);
if (res == -EFAULT)
Expand All @@ -763,15 +766,15 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
dpi_lookahead_size = len;

if (!data->curarg_already_on_frame) {
volatile unsigned long read_size = dpi_lookahead_size;
volatile u16 read_size = dpi_lookahead_size;

#ifdef BPF_FORBIDS_ZERO_ACCESS
if (read_size)
if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF],
if (bpf_probe_read(&data->buf[curoff_bounded],
((read_size - 1) & SCRATCH_SIZE_HALF) + 1,
(void *)val))
#else
if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF],
if (bpf_probe_read(&data->buf[curoff_bounded],
read_size & SCRATCH_SIZE_HALF,
(void *)val))
#endif
Expand All @@ -787,15 +790,15 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
len = PPM_MAX_ARG_SIZE;

if (!data->curarg_already_on_frame) {
volatile unsigned long read_size = len;
volatile u16 read_size = len;

#ifdef BPF_FORBIDS_ZERO_ACCESS
if (read_size)
if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF],
if (bpf_probe_read(&data->buf[curoff_bounded],
((read_size - 1) & SCRATCH_SIZE_HALF) + 1,
(void *)val))
#else
if (bpf_probe_read(&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF],
if (bpf_probe_read(&data->buf[curoff_bounded],
read_size & SCRATCH_SIZE_HALF,
(void *)val))
#endif
Expand All @@ -821,13 +824,13 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
case PT_FLAGS8:
case PT_UINT8:
case PT_SIGTYPE:
*((u8 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((u8 *)&data->buf[curoff_bounded]) = val;
len = sizeof(u8);
break;
case PT_FLAGS16:
case PT_UINT16:
case PT_SYSCALLID:
*((u16 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((u16 *)&data->buf[curoff_bounded]) = val;
len = sizeof(u16);
break;
case PT_FLAGS32:
Expand All @@ -836,32 +839,32 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
case PT_UID:
case PT_GID:
case PT_SIGSET:
*((u32 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((u32 *)&data->buf[curoff_bounded]) = val;
len = sizeof(u32);
break;
case PT_RELTIME:
case PT_ABSTIME:
case PT_UINT64:
*((u64 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((u64 *)&data->buf[curoff_bounded]) = val;
len = sizeof(u64);
break;
case PT_INT8:
*((s8 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((s8 *)&data->buf[curoff_bounded]) = val;
len = sizeof(s8);
break;
case PT_INT16:
*((s16 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((s16 *)&data->buf[curoff_bounded]) = val;
len = sizeof(s16);
break;
case PT_INT32:
*((s32 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((s32 *)&data->buf[curoff_bounded]) = val;
len = sizeof(s32);
break;
case PT_INT64:
case PT_ERRNO:
case PT_FD:
case PT_PID:
*((s64 *)&data->buf[data->state->tail_ctx.curoff & SCRATCH_SIZE_HALF]) = val;
*((s64 *)&data->buf[curoff_bounded]) = val;
len = sizeof(s64);
break;
default: {
Expand All @@ -871,7 +874,7 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
return PPM_FAILURE_BUG;
}
}

FedeDP marked this conversation as resolved.
Show resolved Hide resolved
if (len_dyn + len > PPM_MAX_ARG_SIZE)
return PPM_FAILURE_BUFFER_FULL;

Expand Down
33 changes: 20 additions & 13 deletions driver/bpf/fillers.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,26 +308,21 @@ static __always_inline int bpf_poll_parse_fds(struct filler_data *data,

#pragma unroll
for (j = 0; j < POLL_MAXFDS; ++j) {
u16 flags;
if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

if (j == nfds)
break;

u16 flags;
if (enter_event) {
flags = poll_events_to_scap(fds[j].events);
} else {
if (!fds[j].revents)
continue;

flags = poll_events_to_scap(fds[j].revents);
}

if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

*(s64 *)&data->buf[off & SCRATCH_SIZE_HALF] = fds[j].fd;
off += sizeof(s64);

if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

Expand Down Expand Up @@ -415,10 +410,14 @@ static __always_inline int bpf_parse_readv_writev_bufs(struct filler_data *data,
#endif
return PPM_FAILURE_INVALID_USER_MEMORY;


#pragma unroll
for (j = 0; j < MAX_IOVCNT; ++j) {
if (j == iovcnt)
break;
// BPF seems to require a hard limit to avoid overflows
if (size == LONG_MAX)
break;

size += iov[j].iov_len;
}
Expand Down Expand Up @@ -1506,23 +1505,27 @@ static __always_inline int __bpf_append_cgroup(struct css_set *cgroups,
char *cgroup_path[MAX_CGROUP_PATHS];
bool prev_empty = false;
int off = *len;
unsigned int off_bounded;

off_bounded = off & SCRATCH_SIZE_HALF;
if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

int res = bpf_probe_read_str(&buf[off & SCRATCH_SIZE_HALF],
int res = bpf_probe_read_str(&buf[off_bounded],
SCRATCH_SIZE_HALF,
subsys_name);
if (res == -EFAULT)
return PPM_FAILURE_INVALID_USER_MEMORY;

off += res - 1;

off_bounded = off & SCRATCH_SIZE_HALF;
if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

buf[off & SCRATCH_SIZE_HALF] = '=';
buf[off_bounded] = '=';
++off;
off_bounded = off & SCRATCH_SIZE_HALF;

#pragma unroll MAX_CGROUP_PATHS
for (int k = 0; k < MAX_CGROUP_PATHS; ++k) {
Expand All @@ -1541,20 +1544,24 @@ static __always_inline int __bpf_append_cgroup(struct css_set *cgroups,
if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

buf[off & SCRATCH_SIZE_HALF] = '/';
buf[off_bounded] = '/';
++off;
off_bounded = off & SCRATCH_SIZE_HALF;
}

prev_empty = false;

if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

res = bpf_probe_read_str(&buf[off & SCRATCH_SIZE_HALF],
res = bpf_probe_read_str(&buf[off_bounded],
SCRATCH_SIZE_HALF,
cgroup_path[k]);
if (res > 1)
{
off += res - 1;
off_bounded = off & SCRATCH_SIZE_HALF;
}
else if (res == 1)
prev_empty = true;
else
Expand All @@ -1565,7 +1572,7 @@ static __always_inline int __bpf_append_cgroup(struct css_set *cgroups,
if (off > SCRATCH_SIZE_HALF)
return PPM_FAILURE_BUFFER_FULL;

buf[off & SCRATCH_SIZE_HALF] = 0;
buf[off_bounded] = 0;
++off;
*len = off;

Expand Down