Skip to content

Commit ed13998

Browse files
NicolasDichteldavem330
authored andcommitted
sock_diag: fix filter code sent to userspace
Filters need to be translated to real BPF code for userland, like SO_GETFILTER. Signed-off-by: Nicolas Dichtel <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 92bb73e commit ed13998

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

include/linux/filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
4646
extern int sk_detach_filter(struct sock *sk);
4747
extern int sk_chk_filter(struct sock_filter *filter, unsigned int flen);
4848
extern int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, unsigned len);
49+
extern void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to);
4950

5051
#ifdef CONFIG_BPF_JIT
5152
#include <stdarg.h>

net/core/filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ int sk_detach_filter(struct sock *sk)
778778
}
779779
EXPORT_SYMBOL_GPL(sk_detach_filter);
780780

781-
static void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)
781+
void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)
782782
{
783783
static const u16 decodes[] = {
784784
[BPF_S_ALU_ADD_K] = BPF_ALU|BPF_ADD|BPF_K,

net/core/sock_diag.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk,
7373
goto out;
7474
}
7575

76-
if (filter)
77-
memcpy(nla_data(attr), filter->insns, len);
76+
if (filter) {
77+
struct sock_filter *fb = (struct sock_filter *)nla_data(attr);
78+
int i;
79+
80+
for (i = 0; i < filter->len; i++, fb++)
81+
sk_decode_filter(&filter->insns[i], fb);
82+
}
7883

7984
out:
8085
rcu_read_unlock();

0 commit comments

Comments
 (0)