Skip to content

Commit aa7145c

Browse files
borkmanndavem330
authored andcommitted
bpf, events: fix offset in skb copy handler
This patch fixes the __output_custom() routine we currently use with bpf_skb_copy(). I missed that when len is larger than the size of the current handle, we can issue multiple invocations of copy_func, and __output_custom() advances destination but also source buffer by the written amount of bytes. When we have __output_custom(), this is actually wrong since in that case the source buffer points to a non-linear object, in our case an skb, which the copy_func helper is supposed to walk. Therefore, since this is non-linear we thus need to pass the offset into the helper, so that copy_func can use it for extracting the data from the source object. Therefore, adjust the callback signatures properly and pass offset into the skb_header_pointer() invoked from bpf_skb_copy() callback. The __DEFINE_OUTPUT_COPY_BODY() is adjusted to accommodate for two things: i) to pass in whether we should advance source buffer or not; this is a compile-time constant condition, ii) to pass in the offset for __output_custom(), which we do with help of __VA_ARGS__, so everything can stay inlined as is currently. Both changes allow for adapting the __output_* fast-path helpers w/o extra overhead. Fixes: 555c8a8 ("bpf: avoid stack copy and use skb ctx for event output") Fixes: 7e3f977 ("perf, events: add non-linear data support for raw records") Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a1b43ed commit aa7145c

4 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *f
211211
const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
212212

213213
typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
214-
unsigned long len);
214+
unsigned long off, unsigned long len);
215215

216216
u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
217217
void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);

‎include/linux/perf_event.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct perf_callchain_entry_ctx {
7070
};
7171

7272
typedef unsigned long (*perf_copy_f)(void *dst, const void *src,
73-
unsigned long len);
73+
unsigned long off, unsigned long len);
7474

7575
struct perf_raw_frag {
7676
union {

‎kernel/events/internal.h‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,19 @@ static inline unsigned long perf_aux_size(struct ring_buffer *rb)
123123
return rb->aux_nr_pages << PAGE_SHIFT;
124124
}
125125

126-
#define __DEFINE_OUTPUT_COPY_BODY(memcpy_func) \
126+
#define __DEFINE_OUTPUT_COPY_BODY(advance_buf, memcpy_func, ...) \
127127
{ \
128128
unsigned long size, written; \
129129
\
130130
do { \
131131
size = min(handle->size, len); \
132-
written = memcpy_func(handle->addr, buf, size); \
132+
written = memcpy_func(__VA_ARGS__); \
133133
written = size - written; \
134134
\
135135
len -= written; \
136136
handle->addr += written; \
137-
buf += written; \
137+
if (advance_buf) \
138+
buf += written; \
138139
handle->size -= written; \
139140
if (!handle->size) { \
140141
struct ring_buffer *rb = handle->rb; \
@@ -153,12 +154,16 @@ static inline unsigned long perf_aux_size(struct ring_buffer *rb)
153154
static inline unsigned long \
154155
func_name(struct perf_output_handle *handle, \
155156
const void *buf, unsigned long len) \
156-
__DEFINE_OUTPUT_COPY_BODY(memcpy_func)
157+
__DEFINE_OUTPUT_COPY_BODY(true, memcpy_func, handle->addr, buf, size)
157158

158159
static inline unsigned long
159160
__output_custom(struct perf_output_handle *handle, perf_copy_f copy_func,
160161
const void *buf, unsigned long len)
161-
__DEFINE_OUTPUT_COPY_BODY(copy_func)
162+
{
163+
unsigned long orig_len = len;
164+
__DEFINE_OUTPUT_COPY_BODY(false, copy_func, handle->addr, buf,
165+
orig_len - len, size)
166+
}
162167

163168
static inline unsigned long
164169
memcpy_common(void *dst, const void *src, unsigned long n)

‎net/core/filter.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,9 +2026,9 @@ bool bpf_helper_changes_skb_data(void *func)
20262026
}
20272027

20282028
static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2029-
unsigned long len)
2029+
unsigned long off, unsigned long len)
20302030
{
2031-
void *ptr = skb_header_pointer(skb, 0, len, dst_buff);
2031+
void *ptr = skb_header_pointer(skb, off, len, dst_buff);
20322032

20332033
if (unlikely(!ptr))
20342034
return len;

0 commit comments

Comments
 (0)