Skip to content

Commit bf64ff4

Browse files
congwangdavem330
authored andcommitted
genetlink: get rid of family->attrbuf
genl_family_rcv_msg_attrs_parse() reuses the global family->attrbuf when family->parallel_ops is false. However, family->attrbuf is not protected by any lock on the genl_family_rcv_msg_doit() code path. This leads to several different consequences, one of them is UAF, like the following: genl_family_rcv_msg_doit(): genl_start(): genl_family_rcv_msg_attrs_parse() attrbuf = family->attrbuf __nlmsg_parse(attrbuf); genl_family_rcv_msg_attrs_parse() attrbuf = family->attrbuf __nlmsg_parse(attrbuf); info->attrs = attrs; cb->data = info; netlink_unicast_kernel(): consume_skb() genl_lock_dumpit(): genl_dumpit_info(cb)->attrs Note family->attrbuf is an array of pointers to the skb data, once the skb is freed, any dereference of family->attrbuf will be a UAF. Maybe we could serialize the family->attrbuf with genl_mutex too, but that would make the locking more complicated. Instead, we can just get rid of family->attrbuf and always allocate attrbuf from heap like the family->parallel_ops==true code path. This may add some performance overhead but comparing with taking the global genl_mutex, it still looks better. Fixes: 75cdbdd ("net: ieee802154: have genetlink code to parse the attrs during dumpit") Fixes: 057af70 ("net: tipc: have genetlink code to parse the attrs during dumpit") Reported-and-tested-by: syzbot+3039ddf6d7b13daf3787@syzkaller.appspotmail.com Reported-and-tested-by: syzbot+80cad1e3cb4c41cde6ff@syzkaller.appspotmail.com Reported-and-tested-by: syzbot+736bcbcb11b60d0c0792@syzkaller.appspotmail.com Reported-and-tested-by: syzbot+520f8704db2b68091d44@syzkaller.appspotmail.com Reported-and-tested-by: syzbot+c96e4dfb32f8987fdeed@syzkaller.appspotmail.com Cc: Jiri Pirko <jiri@mellanox.com> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 33c568b commit bf64ff4

2 files changed

Lines changed: 13 additions & 37 deletions

File tree

‎include/net/genetlink.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ struct genl_info;
4141
* Note that unbind() will not be called symmetrically if the
4242
* generic netlink family is removed while there are still open
4343
* sockets.
44-
* @attrbuf: buffer to store parsed attributes (private)
4544
* @mcgrps: multicast groups used by this family
4645
* @n_mcgrps: number of multicast groups
4746
* @mcgrp_offset: starting number of multicast group IDs in this family
@@ -66,7 +65,6 @@ struct genl_family {
6665
struct genl_info *info);
6766
int (*mcast_bind)(struct net *net, int group);
6867
void (*mcast_unbind)(struct net *net, int group);
69-
struct nlattr ** attrbuf; /* private */
7068
const struct genl_ops * ops;
7169
const struct genl_multicast_group *mcgrps;
7270
unsigned int n_ops;

‎net/netlink/genetlink.c‎

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -351,22 +351,11 @@ int genl_register_family(struct genl_family *family)
351351
start = end = GENL_ID_VFS_DQUOT;
352352
}
353353

354-
if (family->maxattr && !family->parallel_ops) {
355-
family->attrbuf = kmalloc_array(family->maxattr + 1,
356-
sizeof(struct nlattr *),
357-
GFP_KERNEL);
358-
if (family->attrbuf == NULL) {
359-
err = -ENOMEM;
360-
goto errout_locked;
361-
}
362-
} else
363-
family->attrbuf = NULL;
364-
365354
family->id = idr_alloc_cyclic(&genl_fam_idr, family,
366355
start, end + 1, GFP_KERNEL);
367356
if (family->id < 0) {
368357
err = family->id;
369-
goto errout_free;
358+
goto errout_locked;
370359
}
371360

372361
err = genl_validate_assign_mc_groups(family);
@@ -385,8 +374,6 @@ int genl_register_family(struct genl_family *family)
385374

386375
errout_remove:
387376
idr_remove(&genl_fam_idr, family->id);
388-
errout_free:
389-
kfree(family->attrbuf);
390377
errout_locked:
391378
genl_unlock_all();
392379
return err;
@@ -419,8 +406,6 @@ int genl_unregister_family(const struct genl_family *family)
419406
atomic_read(&genl_sk_destructing_cnt) == 0);
420407
genl_unlock();
421408

422-
kfree(family->attrbuf);
423-
424409
genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
425410

426411
return 0;
@@ -485,30 +470,23 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
485470
if (!family->maxattr)
486471
return NULL;
487472

488-
if (family->parallel_ops) {
489-
attrbuf = kmalloc_array(family->maxattr + 1,
490-
sizeof(struct nlattr *), GFP_KERNEL);
491-
if (!attrbuf)
492-
return ERR_PTR(-ENOMEM);
493-
} else {
494-
attrbuf = family->attrbuf;
495-
}
473+
attrbuf = kmalloc_array(family->maxattr + 1,
474+
sizeof(struct nlattr *), GFP_KERNEL);
475+
if (!attrbuf)
476+
return ERR_PTR(-ENOMEM);
496477

497478
err = __nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
498479
family->policy, validate, extack);
499480
if (err) {
500-
if (family->parallel_ops)
501-
kfree(attrbuf);
481+
kfree(attrbuf);
502482
return ERR_PTR(err);
503483
}
504484
return attrbuf;
505485
}
506486

507-
static void genl_family_rcv_msg_attrs_free(const struct genl_family *family,
508-
struct nlattr **attrbuf)
487+
static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf)
509488
{
510-
if (family->parallel_ops)
511-
kfree(attrbuf);
489+
kfree(attrbuf);
512490
}
513491

514492
struct genl_start_context {
@@ -542,7 +520,7 @@ static int genl_start(struct netlink_callback *cb)
542520
no_attrs:
543521
info = genl_dumpit_info_alloc();
544522
if (!info) {
545-
genl_family_rcv_msg_attrs_free(ctx->family, attrs);
523+
genl_family_rcv_msg_attrs_free(attrs);
546524
return -ENOMEM;
547525
}
548526
info->family = ctx->family;
@@ -559,7 +537,7 @@ static int genl_start(struct netlink_callback *cb)
559537
}
560538

561539
if (rc) {
562-
genl_family_rcv_msg_attrs_free(info->family, info->attrs);
540+
genl_family_rcv_msg_attrs_free(info->attrs);
563541
genl_dumpit_info_free(info);
564542
cb->data = NULL;
565543
}
@@ -588,7 +566,7 @@ static int genl_lock_done(struct netlink_callback *cb)
588566
rc = ops->done(cb);
589567
genl_unlock();
590568
}
591-
genl_family_rcv_msg_attrs_free(info->family, info->attrs);
569+
genl_family_rcv_msg_attrs_free(info->attrs);
592570
genl_dumpit_info_free(info);
593571
return rc;
594572
}
@@ -601,7 +579,7 @@ static int genl_parallel_done(struct netlink_callback *cb)
601579

602580
if (ops->done)
603581
rc = ops->done(cb);
604-
genl_family_rcv_msg_attrs_free(info->family, info->attrs);
582+
genl_family_rcv_msg_attrs_free(info->attrs);
605583
genl_dumpit_info_free(info);
606584
return rc;
607585
}
@@ -694,7 +672,7 @@ static int genl_family_rcv_msg_doit(const struct genl_family *family,
694672
family->post_doit(ops, skb, &info);
695673

696674
out:
697-
genl_family_rcv_msg_attrs_free(family, attrbuf);
675+
genl_family_rcv_msg_attrs_free(attrbuf);
698676

699677
return err;
700678
}

0 commit comments

Comments
 (0)