Skip to content

Commit 365f9ae

Browse files
mkubecekdavem330
authored andcommitted
ethtool: fix genlmsg_put() failure handling in ethnl_default_dumpit()
If the genlmsg_put() call in ethnl_default_dumpit() fails, we bail out without checking if we already have some messages in current skb like we do with ethnl_default_dump_one() failure later. Therefore if existing messages almost fill up the buffer so that there is not enough space even for netlink and genetlink header, we lose all prepared messages and return and error. Rather than duplicating the skb->len check, move the genlmsg_put(), genlmsg_cancel() and genlmsg_end() calls into ethnl_default_dump_one(). This is also more logical as all message composition will be in ethnl_default_dump_one() and only iteration logic will be left in ethnl_default_dumpit(). Fixes: 728480f ("ethtool: default handlers for GET requests") Reported-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Michal Kubecek <mkubecek@suse.cz> Reviewed-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 306381a commit 365f9ae

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

‎net/ethtool/netlink.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,17 @@ static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
376376
}
377377

378378
static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
379-
const struct ethnl_dump_ctx *ctx)
379+
const struct ethnl_dump_ctx *ctx,
380+
struct netlink_callback *cb)
380381
{
382+
void *ehdr;
381383
int ret;
382384

385+
ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
386+
&ethtool_genl_family, 0, ctx->ops->reply_cmd);
387+
if (!ehdr)
388+
return -EMSGSIZE;
389+
383390
ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
384391
rtnl_lock();
385392
ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL);
@@ -395,6 +402,10 @@ static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
395402
if (ctx->ops->cleanup_data)
396403
ctx->ops->cleanup_data(ctx->reply_data);
397404
ctx->reply_data->dev = NULL;
405+
if (ret < 0)
406+
genlmsg_cancel(skb, ehdr);
407+
else
408+
genlmsg_end(skb, ehdr);
398409
return ret;
399410
}
400411

@@ -411,7 +422,6 @@ static int ethnl_default_dumpit(struct sk_buff *skb,
411422
int s_idx = ctx->pos_idx;
412423
int h, idx = 0;
413424
int ret = 0;
414-
void *ehdr;
415425

416426
rtnl_lock();
417427
for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
@@ -431,26 +441,15 @@ static int ethnl_default_dumpit(struct sk_buff *skb,
431441
dev_hold(dev);
432442
rtnl_unlock();
433443

434-
ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
435-
cb->nlh->nlmsg_seq,
436-
&ethtool_genl_family, 0,
437-
ctx->ops->reply_cmd);
438-
if (!ehdr) {
439-
dev_put(dev);
440-
ret = -EMSGSIZE;
441-
goto out;
442-
}
443-
ret = ethnl_default_dump_one(skb, dev, ctx);
444+
ret = ethnl_default_dump_one(skb, dev, ctx, cb);
444445
dev_put(dev);
445446
if (ret < 0) {
446-
genlmsg_cancel(skb, ehdr);
447447
if (ret == -EOPNOTSUPP)
448448
goto lock_and_cont;
449449
if (likely(skb->len))
450450
ret = skb->len;
451451
goto out;
452452
}
453-
genlmsg_end(skb, ehdr);
454453
lock_and_cont:
455454
rtnl_lock();
456455
if (net->dev_base_seq != seq) {

0 commit comments

Comments
 (0)