mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-19 09:04:57 +00:00
b895bdf5d6
div_u64() divides u64 by u32. nft_limit_init() wants to divide u64 by u64, use the appropriate math function (div64_u64) divide error: 0000 [#1] PREEMPT SMP KASAN CPU: 1 PID: 8390 Comm: syz-executor188 Not tainted 5.12.0-rc4-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 RIP: 0010:div_u64_rem include/linux/math64.h:28 [inline] RIP: 0010:div_u64 include/linux/math64.h:127 [inline] RIP: 0010:nft_limit_init+0x2a2/0x5e0 net/netfilter/nft_limit.c:85 Code: ef 4c 01 eb 41 0f 92 c7 48 89 de e8 38 a5 22 fa 4d 85 ff 0f 85 97 02 00 00 e8 ea 9e 22 fa 4c 0f af f3 45 89 ed 31 d2 4c 89 f0 <49> f7 f5 49 89 c6 e8 d3 9e 22 fa 48 8d 7d 48 48 b8 00 00 00 00 00 RSP: 0018:ffffc90009447198 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 0000200000000000 RCX: 0000000000000000 RDX: 0000000000000000 RSI: ffffffff875152e6 RDI: 0000000000000003 RBP: ffff888020f80908 R08: 0000200000000000 R09: 0000000000000000 R10: ffffffff875152d8 R11: 0000000000000000 R12: ffffc90009447270 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 FS: 000000000097a300(0000) GS:ffff8880b9d00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00000000200001c4 CR3: 0000000026a52000 CR4: 00000000001506e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: nf_tables_newexpr net/netfilter/nf_tables_api.c:2675 [inline] nft_expr_init+0x145/0x2d0 net/netfilter/nf_tables_api.c:2713 nft_set_elem_expr_alloc+0x27/0x280 net/netfilter/nf_tables_api.c:5160 nf_tables_newset+0x1997/0x3150 net/netfilter/nf_tables_api.c:4321 nfnetlink_rcv_batch+0x85a/0x21b0 net/netfilter/nfnetlink.c:456 nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:580 [inline] nfnetlink_rcv+0x3af/0x420 net/netfilter/nfnetlink.c:598 netlink_unicast_kernel net/netlink/af_netlink.c:1312 [inline] netlink_unicast+0x533/0x7d0 net/netlink/af_netlink.c:1338 netlink_sendmsg+0x856/0xd90 net/netlink/af_netlink.c:1927 sock_sendmsg_nosec net/socket.c:654 [inline] sock_sendmsg+0xcf/0x120 net/socket.c:674 ____sys_sendmsg+0x6e8/0x810 net/socket.c:2350 ___sys_sendmsg+0xf3/0x170 net/socket.c:2404 __sys_sendmsg+0xe5/0x1b0 net/socket.c:2433 do_syscall_64+0x2d/0x70 arch/x86/entry/common.c:46 entry_SYSCALL_64_after_hwframe+0x44/0xae Fixes:c26844eda9
("netfilter: nf_tables: Fix nft limit burst handling") Fixes:3e0f64b7dd
("netfilter: nft_limit: fix packet ratelimiting") Signed-off-by: Eric Dumazet <edumazet@google.com> Diagnosed-by: Luigi Rizzo <lrizzo@google.com> Reported-by: syzbot <syzkaller@googlegroups.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
375 lines
9.4 KiB
C
375 lines
9.4 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
|
|
*
|
|
* Development of this code funded by Astaro AG (http://www.astaro.com/)
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/init.h>
|
|
#include <linux/module.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/netlink.h>
|
|
#include <linux/netfilter.h>
|
|
#include <linux/netfilter/nf_tables.h>
|
|
#include <net/netfilter/nf_tables.h>
|
|
|
|
struct nft_limit {
|
|
spinlock_t lock;
|
|
u64 last;
|
|
u64 tokens;
|
|
u64 tokens_max;
|
|
u64 rate;
|
|
u64 nsecs;
|
|
u32 burst;
|
|
bool invert;
|
|
};
|
|
|
|
static inline bool nft_limit_eval(struct nft_limit *limit, u64 cost)
|
|
{
|
|
u64 now, tokens;
|
|
s64 delta;
|
|
|
|
spin_lock_bh(&limit->lock);
|
|
now = ktime_get_ns();
|
|
tokens = limit->tokens + now - limit->last;
|
|
if (tokens > limit->tokens_max)
|
|
tokens = limit->tokens_max;
|
|
|
|
limit->last = now;
|
|
delta = tokens - cost;
|
|
if (delta >= 0) {
|
|
limit->tokens = delta;
|
|
spin_unlock_bh(&limit->lock);
|
|
return limit->invert;
|
|
}
|
|
limit->tokens = tokens;
|
|
spin_unlock_bh(&limit->lock);
|
|
return !limit->invert;
|
|
}
|
|
|
|
/* Use same default as in iptables. */
|
|
#define NFT_LIMIT_PKT_BURST_DEFAULT 5
|
|
|
|
static int nft_limit_init(struct nft_limit *limit,
|
|
const struct nlattr * const tb[], bool pkts)
|
|
{
|
|
u64 unit, tokens;
|
|
|
|
if (tb[NFTA_LIMIT_RATE] == NULL ||
|
|
tb[NFTA_LIMIT_UNIT] == NULL)
|
|
return -EINVAL;
|
|
|
|
limit->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
|
|
unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
|
|
limit->nsecs = unit * NSEC_PER_SEC;
|
|
if (limit->rate == 0 || limit->nsecs < unit)
|
|
return -EOVERFLOW;
|
|
|
|
if (tb[NFTA_LIMIT_BURST])
|
|
limit->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
|
|
|
|
if (pkts && limit->burst == 0)
|
|
limit->burst = NFT_LIMIT_PKT_BURST_DEFAULT;
|
|
|
|
if (limit->rate + limit->burst < limit->rate)
|
|
return -EOVERFLOW;
|
|
|
|
if (pkts) {
|
|
tokens = div64_u64(limit->nsecs, limit->rate) * limit->burst;
|
|
} else {
|
|
/* The token bucket size limits the number of tokens can be
|
|
* accumulated. tokens_max specifies the bucket size.
|
|
* tokens_max = unit * (rate + burst) / rate.
|
|
*/
|
|
tokens = div64_u64(limit->nsecs * (limit->rate + limit->burst),
|
|
limit->rate);
|
|
}
|
|
|
|
limit->tokens = tokens;
|
|
limit->tokens_max = limit->tokens;
|
|
|
|
if (tb[NFTA_LIMIT_FLAGS]) {
|
|
u32 flags = ntohl(nla_get_be32(tb[NFTA_LIMIT_FLAGS]));
|
|
|
|
if (flags & NFT_LIMIT_F_INV)
|
|
limit->invert = true;
|
|
}
|
|
limit->last = ktime_get_ns();
|
|
spin_lock_init(&limit->lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit,
|
|
enum nft_limit_type type)
|
|
{
|
|
u32 flags = limit->invert ? NFT_LIMIT_F_INV : 0;
|
|
u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC);
|
|
|
|
if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(limit->rate),
|
|
NFTA_LIMIT_PAD) ||
|
|
nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs),
|
|
NFTA_LIMIT_PAD) ||
|
|
nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) ||
|
|
nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)) ||
|
|
nla_put_be32(skb, NFTA_LIMIT_FLAGS, htonl(flags)))
|
|
goto nla_put_failure;
|
|
return 0;
|
|
|
|
nla_put_failure:
|
|
return -1;
|
|
}
|
|
|
|
struct nft_limit_pkts {
|
|
struct nft_limit limit;
|
|
u64 cost;
|
|
};
|
|
|
|
static void nft_limit_pkts_eval(const struct nft_expr *expr,
|
|
struct nft_regs *regs,
|
|
const struct nft_pktinfo *pkt)
|
|
{
|
|
struct nft_limit_pkts *priv = nft_expr_priv(expr);
|
|
|
|
if (nft_limit_eval(&priv->limit, priv->cost))
|
|
regs->verdict.code = NFT_BREAK;
|
|
}
|
|
|
|
static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
|
|
[NFTA_LIMIT_RATE] = { .type = NLA_U64 },
|
|
[NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
|
|
[NFTA_LIMIT_BURST] = { .type = NLA_U32 },
|
|
[NFTA_LIMIT_TYPE] = { .type = NLA_U32 },
|
|
[NFTA_LIMIT_FLAGS] = { .type = NLA_U32 },
|
|
};
|
|
|
|
static int nft_limit_pkts_init(const struct nft_ctx *ctx,
|
|
const struct nft_expr *expr,
|
|
const struct nlattr * const tb[])
|
|
{
|
|
struct nft_limit_pkts *priv = nft_expr_priv(expr);
|
|
int err;
|
|
|
|
err = nft_limit_init(&priv->limit, tb, true);
|
|
if (err < 0)
|
|
return err;
|
|
|
|
priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
|
|
return 0;
|
|
}
|
|
|
|
static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr)
|
|
{
|
|
const struct nft_limit_pkts *priv = nft_expr_priv(expr);
|
|
|
|
return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
|
|
}
|
|
|
|
static struct nft_expr_type nft_limit_type;
|
|
static const struct nft_expr_ops nft_limit_pkts_ops = {
|
|
.type = &nft_limit_type,
|
|
.size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
|
|
.eval = nft_limit_pkts_eval,
|
|
.init = nft_limit_pkts_init,
|
|
.dump = nft_limit_pkts_dump,
|
|
};
|
|
|
|
static void nft_limit_bytes_eval(const struct nft_expr *expr,
|
|
struct nft_regs *regs,
|
|
const struct nft_pktinfo *pkt)
|
|
{
|
|
struct nft_limit *priv = nft_expr_priv(expr);
|
|
u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
|
|
|
|
if (nft_limit_eval(priv, cost))
|
|
regs->verdict.code = NFT_BREAK;
|
|
}
|
|
|
|
static int nft_limit_bytes_init(const struct nft_ctx *ctx,
|
|
const struct nft_expr *expr,
|
|
const struct nlattr * const tb[])
|
|
{
|
|
struct nft_limit *priv = nft_expr_priv(expr);
|
|
|
|
return nft_limit_init(priv, tb, false);
|
|
}
|
|
|
|
static int nft_limit_bytes_dump(struct sk_buff *skb,
|
|
const struct nft_expr *expr)
|
|
{
|
|
const struct nft_limit *priv = nft_expr_priv(expr);
|
|
|
|
return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
|
|
}
|
|
|
|
static const struct nft_expr_ops nft_limit_bytes_ops = {
|
|
.type = &nft_limit_type,
|
|
.size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
|
|
.eval = nft_limit_bytes_eval,
|
|
.init = nft_limit_bytes_init,
|
|
.dump = nft_limit_bytes_dump,
|
|
};
|
|
|
|
static const struct nft_expr_ops *
|
|
nft_limit_select_ops(const struct nft_ctx *ctx,
|
|
const struct nlattr * const tb[])
|
|
{
|
|
if (tb[NFTA_LIMIT_TYPE] == NULL)
|
|
return &nft_limit_pkts_ops;
|
|
|
|
switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
|
|
case NFT_LIMIT_PKTS:
|
|
return &nft_limit_pkts_ops;
|
|
case NFT_LIMIT_PKT_BYTES:
|
|
return &nft_limit_bytes_ops;
|
|
}
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
}
|
|
|
|
static struct nft_expr_type nft_limit_type __read_mostly = {
|
|
.name = "limit",
|
|
.select_ops = nft_limit_select_ops,
|
|
.policy = nft_limit_policy,
|
|
.maxattr = NFTA_LIMIT_MAX,
|
|
.flags = NFT_EXPR_STATEFUL,
|
|
.owner = THIS_MODULE,
|
|
};
|
|
|
|
static void nft_limit_obj_pkts_eval(struct nft_object *obj,
|
|
struct nft_regs *regs,
|
|
const struct nft_pktinfo *pkt)
|
|
{
|
|
struct nft_limit_pkts *priv = nft_obj_data(obj);
|
|
|
|
if (nft_limit_eval(&priv->limit, priv->cost))
|
|
regs->verdict.code = NFT_BREAK;
|
|
}
|
|
|
|
static int nft_limit_obj_pkts_init(const struct nft_ctx *ctx,
|
|
const struct nlattr * const tb[],
|
|
struct nft_object *obj)
|
|
{
|
|
struct nft_limit_pkts *priv = nft_obj_data(obj);
|
|
int err;
|
|
|
|
err = nft_limit_init(&priv->limit, tb, true);
|
|
if (err < 0)
|
|
return err;
|
|
|
|
priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
|
|
return 0;
|
|
}
|
|
|
|
static int nft_limit_obj_pkts_dump(struct sk_buff *skb,
|
|
struct nft_object *obj,
|
|
bool reset)
|
|
{
|
|
const struct nft_limit_pkts *priv = nft_obj_data(obj);
|
|
|
|
return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
|
|
}
|
|
|
|
static struct nft_object_type nft_limit_obj_type;
|
|
static const struct nft_object_ops nft_limit_obj_pkts_ops = {
|
|
.type = &nft_limit_obj_type,
|
|
.size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
|
|
.init = nft_limit_obj_pkts_init,
|
|
.eval = nft_limit_obj_pkts_eval,
|
|
.dump = nft_limit_obj_pkts_dump,
|
|
};
|
|
|
|
static void nft_limit_obj_bytes_eval(struct nft_object *obj,
|
|
struct nft_regs *regs,
|
|
const struct nft_pktinfo *pkt)
|
|
{
|
|
struct nft_limit *priv = nft_obj_data(obj);
|
|
u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
|
|
|
|
if (nft_limit_eval(priv, cost))
|
|
regs->verdict.code = NFT_BREAK;
|
|
}
|
|
|
|
static int nft_limit_obj_bytes_init(const struct nft_ctx *ctx,
|
|
const struct nlattr * const tb[],
|
|
struct nft_object *obj)
|
|
{
|
|
struct nft_limit *priv = nft_obj_data(obj);
|
|
|
|
return nft_limit_init(priv, tb, false);
|
|
}
|
|
|
|
static int nft_limit_obj_bytes_dump(struct sk_buff *skb,
|
|
struct nft_object *obj,
|
|
bool reset)
|
|
{
|
|
const struct nft_limit *priv = nft_obj_data(obj);
|
|
|
|
return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
|
|
}
|
|
|
|
static struct nft_object_type nft_limit_obj_type;
|
|
static const struct nft_object_ops nft_limit_obj_bytes_ops = {
|
|
.type = &nft_limit_obj_type,
|
|
.size = sizeof(struct nft_limit),
|
|
.init = nft_limit_obj_bytes_init,
|
|
.eval = nft_limit_obj_bytes_eval,
|
|
.dump = nft_limit_obj_bytes_dump,
|
|
};
|
|
|
|
static const struct nft_object_ops *
|
|
nft_limit_obj_select_ops(const struct nft_ctx *ctx,
|
|
const struct nlattr * const tb[])
|
|
{
|
|
if (!tb[NFTA_LIMIT_TYPE])
|
|
return &nft_limit_obj_pkts_ops;
|
|
|
|
switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
|
|
case NFT_LIMIT_PKTS:
|
|
return &nft_limit_obj_pkts_ops;
|
|
case NFT_LIMIT_PKT_BYTES:
|
|
return &nft_limit_obj_bytes_ops;
|
|
}
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
}
|
|
|
|
static struct nft_object_type nft_limit_obj_type __read_mostly = {
|
|
.select_ops = nft_limit_obj_select_ops,
|
|
.type = NFT_OBJECT_LIMIT,
|
|
.maxattr = NFTA_LIMIT_MAX,
|
|
.policy = nft_limit_policy,
|
|
.owner = THIS_MODULE,
|
|
};
|
|
|
|
static int __init nft_limit_module_init(void)
|
|
{
|
|
int err;
|
|
|
|
err = nft_register_obj(&nft_limit_obj_type);
|
|
if (err < 0)
|
|
return err;
|
|
|
|
err = nft_register_expr(&nft_limit_type);
|
|
if (err < 0)
|
|
goto err1;
|
|
|
|
return 0;
|
|
err1:
|
|
nft_unregister_obj(&nft_limit_obj_type);
|
|
return err;
|
|
}
|
|
|
|
static void __exit nft_limit_module_exit(void)
|
|
{
|
|
nft_unregister_expr(&nft_limit_type);
|
|
nft_unregister_obj(&nft_limit_obj_type);
|
|
}
|
|
|
|
module_init(nft_limit_module_init);
|
|
module_exit(nft_limit_module_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
|
|
MODULE_ALIAS_NFT_EXPR("limit");
|
|
MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_LIMIT);
|
|
MODULE_DESCRIPTION("nftables limit expression support");
|