Skip to content

Commit b1fcd35

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: filter: let unattached filters use sock_fprog_kern
The sk_unattached_filter_create() API is used by BPF filters that are not directly attached or related to sockets, and are used in team, ptp, xt_bpf, cls_bpf, etc. As such all users do their own internal managment of obtaining filter blocks and thus already have them in kernel memory and set up before calling into sk_unattached_filter_create(). As a result, due to __user annotation in sock_fprog, sparse triggers false positives (incorrect type in assignment [different address space]) when filters are set up before passing them to sk_unattached_filter_create(). Therefore, let sk_unattached_filter_create() API use sock_fprog_kern to overcome this issue. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8556ce7 commit b1fcd35

9 files changed

Lines changed: 18 additions & 17 deletions

File tree

‎drivers/isdn/i4l/isdn_ppp.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ isdn_ppp_ioctl(int min, struct file *file, unsigned int cmd, unsigned long arg)
634634
#ifdef CONFIG_IPPP_FILTER
635635
case PPPIOCSPASS:
636636
{
637-
struct sock_fprog fprog;
637+
struct sock_fprog_kern fprog;
638638
struct sock_filter *code;
639639
int err, len = get_filter(argp, &code);
640640

@@ -653,7 +653,7 @@ isdn_ppp_ioctl(int min, struct file *file, unsigned int cmd, unsigned long arg)
653653
}
654654
case PPPIOCSACTIVE:
655655
{
656-
struct sock_fprog fprog;
656+
struct sock_fprog_kern fprog;
657657
struct sock_filter *code;
658658
int err, len = get_filter(argp, &code);
659659

‎drivers/net/ppp/ppp_generic.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
757757

758758
err = get_filter(argp, &code);
759759
if (err >= 0) {
760-
struct sock_fprog fprog = {
760+
struct sock_fprog_kern fprog = {
761761
.len = err,
762762
.filter = code,
763763
};
@@ -778,7 +778,7 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
778778

779779
err = get_filter(argp, &code);
780780
if (err >= 0) {
781-
struct sock_fprog fprog = {
781+
struct sock_fprog_kern fprog = {
782782
.len = err,
783783
.filter = code,
784784
};

‎drivers/net/team/team_mode_loadbalance.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct lb_port_mapping {
4949
struct lb_priv_ex {
5050
struct team *team;
5151
struct lb_port_mapping tx_hash_to_port_mapping[LB_TX_HASHTABLE_SIZE];
52-
struct sock_fprog *orig_fprog;
52+
struct sock_fprog_kern *orig_fprog;
5353
struct {
5454
unsigned int refresh_interval; /* in tenths of second */
5555
struct delayed_work refresh_dw;
@@ -241,10 +241,10 @@ static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
241241
return 0;
242242
}
243243

244-
static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
244+
static int __fprog_create(struct sock_fprog_kern **pfprog, u32 data_len,
245245
const void *data)
246246
{
247-
struct sock_fprog *fprog;
247+
struct sock_fprog_kern *fprog;
248248
struct sock_filter *filter = (struct sock_filter *) data;
249249

250250
if (data_len % sizeof(struct sock_filter))
@@ -262,7 +262,7 @@ static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
262262
return 0;
263263
}
264264

265-
static void __fprog_destroy(struct sock_fprog *fprog)
265+
static void __fprog_destroy(struct sock_fprog_kern *fprog)
266266
{
267267
kfree(fprog->filter);
268268
kfree(fprog);
@@ -273,7 +273,7 @@ static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
273273
struct lb_priv *lb_priv = get_lb_priv(team);
274274
struct sk_filter *fp = NULL;
275275
struct sk_filter *orig_fp;
276-
struct sock_fprog *fprog = NULL;
276+
struct sock_fprog_kern *fprog = NULL;
277277
int err;
278278

279279
if (ctx->data.bin_val.len) {

‎include/linux/filter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ int sk_convert_filter(struct sock_filter *prog, int len,
188188
struct sock_filter_int *new_prog, int *new_len);
189189

190190
int sk_unattached_filter_create(struct sk_filter **pfp,
191-
struct sock_fprog *fprog);
191+
struct sock_fprog_kern *fprog);
192192
void sk_unattached_filter_destroy(struct sk_filter *fp);
193193

194194
int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);

‎lib/test_bpf.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ static int run_one(struct sk_filter *fp, struct bpf_test *t)
14721472
static __init int test_bpf(void)
14731473
{
14741474
struct sk_filter *fp, *fp_ext = NULL;
1475-
struct sock_fprog fprog;
1475+
struct sock_fprog_kern fprog;
14761476
int err, i, err_cnt = 0;
14771477

14781478
for (i = 0; i < ARRAY_SIZE(tests); i++) {

‎net/core/filter.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ static struct sk_filter *__sk_prepare_filter(struct sk_filter *fp,
15851585
* a negative errno code is returned. On success the return is zero.
15861586
*/
15871587
int sk_unattached_filter_create(struct sk_filter **pfp,
1588-
struct sock_fprog *fprog)
1588+
struct sock_fprog_kern *fprog)
15891589
{
15901590
unsigned int fsize = sk_filter_proglen(fprog);
15911591
struct sk_filter *fp;

‎net/core/ptp_classifier.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void __init ptp_classifier_init(void)
133133
{ 0x16, 0, 0, 0x00000000 },
134134
{ 0x06, 0, 0, 0x00000000 },
135135
};
136-
struct sock_fprog ptp_prog = {
136+
struct sock_fprog_kern ptp_prog = {
137137
.len = ARRAY_SIZE(ptp_filter), .filter = ptp_filter,
138138
};
139139

‎net/netfilter/xt_bpf.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ MODULE_ALIAS("ip6t_bpf");
2323
static int bpf_mt_check(const struct xt_mtchk_param *par)
2424
{
2525
struct xt_bpf_info *info = par->matchinfo;
26-
struct sock_fprog program;
26+
struct sock_fprog_kern program;
2727

2828
program.len = info->bpf_program_num_elem;
29-
program.filter = (struct sock_filter __user *) info->bpf_program;
29+
program.filter = info->bpf_program;
30+
3031
if (sk_unattached_filter_create(&info->filter, &program)) {
3132
pr_info("bpf: check failed: parse error\n");
3233
return -EINVAL;

‎net/sched/cls_bpf.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
160160
{
161161
struct sock_filter *bpf_ops, *bpf_old;
162162
struct tcf_exts exts;
163-
struct sock_fprog tmp;
163+
struct sock_fprog_kern tmp;
164164
struct sk_filter *fp, *fp_old;
165165
u16 bpf_size, bpf_len;
166166
u32 classid;
@@ -191,7 +191,7 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
191191
memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
192192

193193
tmp.len = bpf_len;
194-
tmp.filter = (struct sock_filter __user *) bpf_ops;
194+
tmp.filter = bpf_ops;
195195

196196
ret = sk_unattached_filter_create(&fp, &tmp);
197197
if (ret)

0 commit comments

Comments
 (0)