Skip to content

Commit 33758c8

Browse files
Vasily Averinummakynes
authored andcommitted
memcg: enable accounting for nft objects
nftables replaces iptables, but it lacks memcg accounting. This patch account most of the memory allocation associated with nft and should protect the host from misusing nft inside a memcg restricted container. Signed-off-by: Vasily Averin <vvs@openvz.org> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent f2dd495 commit 33758c8

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

net/netfilter/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static struct nf_hook_entries *allocate_hook_entries_size(u16 num)
5858
if (num == 0)
5959
return NULL;
6060

61-
e = kvzalloc(alloc, GFP_KERNEL);
61+
e = kvzalloc(alloc, GFP_KERNEL_ACCOUNT);
6262
if (e)
6363
e->num_hook_entries = num;
6464
return e;

net/netfilter/nf_tables_api.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,16 +1113,16 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
11131113
}
11141114

11151115
err = -ENOMEM;
1116-
table = kzalloc(sizeof(*table), GFP_KERNEL);
1116+
table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT);
11171117
if (table == NULL)
11181118
goto err_kzalloc;
11191119

1120-
table->name = nla_strdup(attr, GFP_KERNEL);
1120+
table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
11211121
if (table->name == NULL)
11221122
goto err_strdup;
11231123

11241124
if (nla[NFTA_TABLE_USERDATA]) {
1125-
table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL);
1125+
table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
11261126
if (table->udata == NULL)
11271127
goto err_table_udata;
11281128

@@ -1803,7 +1803,7 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
18031803
struct nft_hook *hook;
18041804
int err;
18051805

1806-
hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL);
1806+
hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT);
18071807
if (!hook) {
18081808
err = -ENOMEM;
18091809
goto err_hook_alloc;
@@ -2026,7 +2026,7 @@ static struct nft_rule_blob *nf_tables_chain_alloc_rules(unsigned int size)
20262026
if (size > INT_MAX)
20272027
return NULL;
20282028

2029-
blob = kvmalloc(size, GFP_KERNEL);
2029+
blob = kvmalloc(size, GFP_KERNEL_ACCOUNT);
20302030
if (!blob)
20312031
return NULL;
20322032

@@ -2126,7 +2126,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
21262126
if (err < 0)
21272127
return err;
21282128

2129-
basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
2129+
basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT);
21302130
if (basechain == NULL) {
21312131
nft_chain_release_hook(&hook);
21322132
return -ENOMEM;
@@ -2156,7 +2156,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
21562156
if (flags & NFT_CHAIN_HW_OFFLOAD)
21572157
return -EOPNOTSUPP;
21582158

2159-
chain = kzalloc(sizeof(*chain), GFP_KERNEL);
2159+
chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT);
21602160
if (chain == NULL)
21612161
return -ENOMEM;
21622162

@@ -2169,15 +2169,15 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
21692169
chain->table = table;
21702170

21712171
if (nla[NFTA_CHAIN_NAME]) {
2172-
chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
2172+
chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
21732173
} else {
21742174
if (!(flags & NFT_CHAIN_BINDING)) {
21752175
err = -EINVAL;
21762176
goto err_destroy_chain;
21772177
}
21782178

21792179
snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
2180-
chain->name = kstrdup(name, GFP_KERNEL);
2180+
chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT);
21812181
}
21822182

21832183
if (!chain->name) {
@@ -2186,7 +2186,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
21862186
}
21872187

21882188
if (nla[NFTA_CHAIN_USERDATA]) {
2189-
chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL);
2189+
chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT);
21902190
if (chain->udata == NULL) {
21912191
err = -ENOMEM;
21922192
goto err_destroy_chain;
@@ -2349,7 +2349,7 @@ static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
23492349
char *name;
23502350

23512351
err = -ENOMEM;
2352-
name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
2352+
name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
23532353
if (!name)
23542354
goto err;
23552355

@@ -2797,7 +2797,7 @@ static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
27972797
goto err1;
27982798

27992799
err = -ENOMEM;
2800-
expr = kzalloc(expr_info.ops->size, GFP_KERNEL);
2800+
expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT);
28012801
if (expr == NULL)
28022802
goto err2;
28032803

@@ -3405,7 +3405,7 @@ static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info,
34053405
}
34063406

34073407
err = -ENOMEM;
3408-
rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
3408+
rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT);
34093409
if (rule == NULL)
34103410
goto err_release_expr;
34113411

@@ -3818,7 +3818,7 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
38183818
free_page((unsigned long)inuse);
38193819
}
38203820

3821-
set->name = kasprintf(GFP_KERNEL, name, min + n);
3821+
set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n);
38223822
if (!set->name)
38233823
return -ENOMEM;
38243824

@@ -4382,11 +4382,11 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,
43824382
alloc_size = sizeof(*set) + size + udlen;
43834383
if (alloc_size < size || alloc_size > INT_MAX)
43844384
return -ENOMEM;
4385-
set = kvzalloc(alloc_size, GFP_KERNEL);
4385+
set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT);
43864386
if (!set)
43874387
return -ENOMEM;
43884388

4389-
name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL);
4389+
name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT);
43904390
if (!name) {
43914391
err = -ENOMEM;
43924392
goto err_set_name;
@@ -5921,7 +5921,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
59215921
err = -ENOMEM;
59225922
elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
59235923
elem.key_end.val.data, elem.data.val.data,
5924-
timeout, expiration, GFP_KERNEL);
5924+
timeout, expiration, GFP_KERNEL_ACCOUNT);
59255925
if (elem.priv == NULL)
59265926
goto err_parse_data;
59275927

@@ -6165,7 +6165,7 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
61656165
err = -ENOMEM;
61666166
elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
61676167
elem.key_end.val.data, NULL, 0, 0,
6168-
GFP_KERNEL);
6168+
GFP_KERNEL_ACCOUNT);
61696169
if (elem.priv == NULL)
61706170
goto fail_elem;
61716171

@@ -6477,7 +6477,7 @@ static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
64776477
}
64786478

64796479
err = -ENOMEM;
6480-
obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL);
6480+
obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT);
64816481
if (!obj)
64826482
goto err2;
64836483

@@ -6643,7 +6643,7 @@ static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info,
66436643
obj->key.table = table;
66446644
obj->handle = nf_tables_alloc_handle(table);
66456645

6646-
obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL);
6646+
obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT);
66476647
if (!obj->key.name) {
66486648
err = -ENOMEM;
66496649
goto err_strdup;
@@ -7404,15 +7404,15 @@ static int nf_tables_newflowtable(struct sk_buff *skb,
74047404

74057405
nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
74067406

7407-
flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL);
7407+
flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT);
74087408
if (!flowtable)
74097409
return -ENOMEM;
74107410

74117411
flowtable->table = table;
74127412
flowtable->handle = nf_tables_alloc_handle(table);
74137413
INIT_LIST_HEAD(&flowtable->hook_list);
74147414

7415-
flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL);
7415+
flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT);
74167416
if (!flowtable->name) {
74177417
err = -ENOMEM;
74187418
goto err1;

0 commit comments

Comments
 (0)