]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nft_exthdr.c
netfilter: nf_tables: underflow in nft_parse_u32_check()
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nft_exthdr.c
CommitLineData
96518518
PM
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables.h>
18// FIXME:
19#include <net/ipv6.h>
20
21struct nft_exthdr {
22 u8 type;
23 u8 offset;
24 u8 len;
25 enum nft_registers dreg:8;
26};
27
28static void nft_exthdr_eval(const struct nft_expr *expr,
a55e22e9 29 struct nft_regs *regs,
96518518
PM
30 const struct nft_pktinfo *pkt)
31{
32 struct nft_exthdr *priv = nft_expr_priv(expr);
49499c3e 33 u32 *dest = &regs->data[priv->dreg];
540436c8 34 unsigned int offset = 0;
96518518
PM
35 int err;
36
37 err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
38 if (err < 0)
39 goto err;
40 offset += priv->offset;
41
49499c3e 42 dest[priv->len / NFT_REG32_SIZE] = 0;
fad136ea 43 if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0)
96518518
PM
44 goto err;
45 return;
46err:
a55e22e9 47 regs->verdict.code = NFT_BREAK;
96518518
PM
48}
49
50static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
51 [NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
52 [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
53 [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 },
54 [NFTA_EXTHDR_LEN] = { .type = NLA_U32 },
55};
56
57static int nft_exthdr_init(const struct nft_ctx *ctx,
58 const struct nft_expr *expr,
59 const struct nlattr * const tb[])
60{
61 struct nft_exthdr *priv = nft_expr_priv(expr);
36b701fa 62 u32 offset, len, err;
96518518
PM
63
64 if (tb[NFTA_EXTHDR_DREG] == NULL ||
65 tb[NFTA_EXTHDR_TYPE] == NULL ||
66 tb[NFTA_EXTHDR_OFFSET] == NULL ||
67 tb[NFTA_EXTHDR_LEN] == NULL)
68 return -EINVAL;
69
36b701fa
LGL
70 err = nft_parse_u32_check(tb[NFTA_EXTHDR_OFFSET], U8_MAX, &offset);
71 if (err < 0)
72 return err;
4da449ae 73
36b701fa
LGL
74 err = nft_parse_u32_check(tb[NFTA_EXTHDR_LEN], U8_MAX, &len);
75 if (err < 0)
76 return err;
4da449ae 77
96518518 78 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
4da449ae
LGL
79 priv->offset = offset;
80 priv->len = len;
b1c96ed3 81 priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
96518518 82
1ec10212
PM
83 return nft_validate_register_store(ctx, priv->dreg, NULL,
84 NFT_DATA_VALUE, priv->len);
96518518
PM
85}
86
87static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
88{
89 const struct nft_exthdr *priv = nft_expr_priv(expr);
90
b1c96ed3 91 if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
96518518
PM
92 goto nla_put_failure;
93 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
94 goto nla_put_failure;
95 if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
96 goto nla_put_failure;
97 if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
98 goto nla_put_failure;
99 return 0;
100
101nla_put_failure:
102 return -1;
103}
104
ef1f7df9
PM
105static struct nft_expr_type nft_exthdr_type;
106static const struct nft_expr_ops nft_exthdr_ops = {
107 .type = &nft_exthdr_type,
96518518 108 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
96518518
PM
109 .eval = nft_exthdr_eval,
110 .init = nft_exthdr_init,
111 .dump = nft_exthdr_dump,
ef1f7df9
PM
112};
113
114static struct nft_expr_type nft_exthdr_type __read_mostly = {
115 .name = "exthdr",
116 .ops = &nft_exthdr_ops,
96518518
PM
117 .policy = nft_exthdr_policy,
118 .maxattr = NFTA_EXTHDR_MAX,
ef1f7df9 119 .owner = THIS_MODULE,
96518518
PM
120};
121
122static int __init nft_exthdr_module_init(void)
123{
ef1f7df9 124 return nft_register_expr(&nft_exthdr_type);
96518518
PM
125}
126
127static void __exit nft_exthdr_module_exit(void)
128{
ef1f7df9 129 nft_unregister_expr(&nft_exthdr_type);
96518518
PM
130}
131
132module_init(nft_exthdr_module_init);
133module_exit(nft_exthdr_module_exit);
134
135MODULE_LICENSE("GPL");
136MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
137MODULE_ALIAS_NFT_EXPR("exthdr");