Skip to contentSkip to conversation

netfilter: nftables: introduce table ownership

PATCH Superseded Pablo Neira Ayuso 2 participants 4 messages Jan 27, 2021 – Feb 1, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nftables: introduce table ownership

A userspace daemon like firewalld might need to monitor for netlink updates to detect its ruleset removal by the (global) flush ruleset command to ensure ruleset persistence. This adds extra complexity from userspace and, for some little time, the firewall policy is not in place.

This patch adds the NFT_MSG_SETOWNER netlink command which allows a userspace program to own the table that creates in exclusivity.

Tables that are owned...

- can only be updated and removed by the owner, non-owners hit EPERM if they try to update it or remove it.
- are destroyed when the owner send the NFT_MSG_UNSETOWNER command, or the netlink socket is closed or the process is gone (implicit netlink socket closure).
- are skipped by the global flush ruleset command.
- are listed in the global ruleset.

The userspace process that sends the new NFT_MSG_SETOWNER command need to leave open the netlink socket.

The NFTA_TABLE_OWNER netlink attribute specifies the netlink port ID to identify the owner.

---
 include/net/netfilter/nf_tables.h        |   1 +
 include/uapi/linux/netfilter/nf_tables.h |  15 ++
 net/netfilter/nf_tables_api.c            | 235 +++++++++++++++++++----
 3 files changed, 210 insertions(+), 41 deletions(-)
include/net/netfilter/nf_tables.h +1 -0
index 99d4571fef46..f626706035e2 100644
@@ -1104,6 +1104,7 @@ struct nft_table {
 	u16				family:6,
 					flags:8,
 					genmask:2;
+	u32				nlpid;
 	char				*name;
 	u16				udlen;
 	u8				*udata;
include/uapi/linux/netfilter/nf_tables.h +15 -0
index b1633e7ba529..e073aec8a8d9 100644
@@ -97,6 +97,8 @@ enum nft_verdicts {
  * @NFT_MSG_NEWFLOWTABLE: add new flow table (enum nft_flowtable_attributes)
  * @NFT_MSG_GETFLOWTABLE: get flow table (enum nft_flowtable_attributes)
  * @NFT_MSG_DELFLOWTABLE: delete flow table (enum nft_flowtable_attributes)
+ * @NFT_MSG_SETOWNER: set ruleset owner (enum nft_owner_attributes)
+ * @NFT_MSG_UNSETOWNER: unset ruleset owner (enum nft_owner_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -124,6 +126,8 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWFLOWTABLE,
 	NFT_MSG_GETFLOWTABLE,
 	NFT_MSG_DELFLOWTABLE,
+	NFT_MSG_SETOWNER,
+	NFT_MSG_UNSETOWNER,
 	NFT_MSG_MAX,
 };
 
@@ -173,6 +177,7 @@ enum nft_table_flags {
  * @NFTA_TABLE_FLAGS: bitmask of enum nft_table_flags (NLA_U32)
  * @NFTA_TABLE_USE: number of chains in this table (NLA_U32)
  * @NFTA_TABLE_USERDATA: user data (NLA_BINARY)
+ * @NFTA_TABLE_OWNER: owner of this table through netlink portID (NLA_U32)
  */
 enum nft_table_attributes {
 	NFTA_TABLE_UNSPEC,
@@ -182,6 +187,7 @@ enum nft_table_attributes {
 	NFTA_TABLE_HANDLE,
 	NFTA_TABLE_PAD,
 	NFTA_TABLE_USERDATA,
+	NFTA_TABLE_OWNER,
 	__NFTA_TABLE_MAX
 };
 #define NFTA_TABLE_MAX		(__NFTA_TABLE_MAX - 1)
@@ -1483,6 +1489,15 @@ enum nft_gen_attributes {
 };
 #define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
+/**
+ * enum nft_owner_attributes - nf_tables netlink ownership
+ */
+enum nft_owner_attributes {
+	NFTA_OWNER_UNSPEC,
+	__NFTA_OWNER_MAX
+};
+#define NFTA_OWNER_MAX		(__NFTA_OWNER_MAX - 1)
+
 /*
  * enum nft_fib_attributes - nf_tables fib expression netlink attributes
  *
net/netfilter/nf_tables_api.c +194 -41

491 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 19 lines · Pablo Neira Ayuso

A userspace daemon like firewalld might need to monitor for netlink updates to detect its ruleset removal by the (global) flush ruleset command to ensure ruleset persistence. This adds extra complexity from userspace and, for some little time, the firewall policy is not in place.

This patch adds the NFT_MSG_SETOWNER netlink command which allows a userspace program to own the table that creates in exclusivity.

Tables that are owned...

- can only be updated and removed by the owner, non-owners hit EPERM if they try to update it or remove it.
- are destroyed when the owner send the NFT_MSG_UNSETOWNER command, or the netlink socket is closed or the process is gone (implicit netlink socket closure).
- are skipped by the global flush ruleset command.
- are listed in the global ruleset.

Quoted 5 lines

The userspace process that sends the new NFT_MSG_SETOWNER command need to leave open the netlink socket.

The NFTA_TABLE_OWNER netlink attribute specifies the netlink port ID to identify the owner.

At least for systemd use case, there would be a need to allow add/removal of set elements from other user.

At the moment, table is created by systemd-networkd which will update the masquerade set.

In case systemd-nspawn is used and configured to expose container services via dnat that will need to add the translation map:

add table ip io.systemd.nat
add chain ip io.systemd.nat prerouting { type nat hook prerouting priority dstnat + 1; policy accept; } [..]

# new generation 2 by process 1378 (systemd-network)
add element ip io.systemd.nat masq_saddr { 192.168.159.192/28 }
# new generation 3 by process 1378 (systemd-network)
add element ip io.systemd.nat map_port_ipport { tcp . 2222 : 192.168.159.201 . 22 }
# new generation 4 by process 1512 (systemd-nspawn)

net/netfilter/nf_tables_api.c static LIST_HEAD(nf_tables_objects); · line 40

Quoted 5 lines
net/netfilter/nf_tables_api.c +5 -0
+struct nft_owner {
+	struct list_head	list;
+	possible_net_t		net;
+	u32			nlpid;
+};

I don't see why this is needed.
Isn't it enough to record the nlpid in the table and set a flag that the table is owned by that pid?

net/netfilter/nf_tables_api.c static struct nft_table *nft_table_lookup(const struct net *net, · line 549

Quoted 4 lines
net/netfilter/nf_tables_api.c +4 -0
+		    nft_active_genmask(table, genmask)) {
+			if (nlpid && table->nlpid && table->nlpid != nlpid)
+				return ERR_PTR(-EPERM);
+

i.e., (table->flags & OWNED) && table->nlpid != nlpid)?

On netlink sk destruction the owner flag could be cleared or table could be auto-zapped.

author maintainer
#2 lore raw
Quoted 20 lines · Florian Westphal
Quoted 19 lines · Pablo Neira Ayuso

A userspace daemon like firewalld might need to monitor for netlink updates to detect its ruleset removal by the (global) flush ruleset command to ensure ruleset persistence. This adds extra complexity from userspace and, for some little time, the firewall policy is not in place.

This patch adds the NFT_MSG_SETOWNER netlink command which allows a userspace program to own the table that creates in exclusivity.

Tables that are owned...

- can only be updated and removed by the owner, non-owners hit EPERM if they try to update it or remove it.
- are destroyed when the owner send the NFT_MSG_UNSETOWNER command, or the netlink socket is closed or the process is gone (implicit netlink socket closure).
- are skipped by the global flush ruleset command.
- are listed in the global ruleset.

Quoted 8 lines
Quoted 5 lines

The userspace process that sends the new NFT_MSG_SETOWNER command need to leave open the netlink socket.

The NFTA_TABLE_OWNER netlink attribute specifies the netlink port ID to identify the owner.

At least for systemd use case, there would be a need to allow add/removal of set elements from other user.

Then, probably a flag for this? Such flag would work like this?

- Allow for set element updates (from any process, no ownership).
- nft flush ruleset skips flushing the set.
- nft flush set x y flushes the content of this set.

The table owner would set on such flag.

Would this work for the scenario you describe below?

Quoted 15 lines

At the moment, table is created by systemd-networkd which will update the masquerade set.

In case systemd-nspawn is used and configured to expose container services via dnat that will need to add the translation map:

add table ip io.systemd.nat
add chain ip io.systemd.nat prerouting { type nat hook prerouting priority dstnat + 1; policy accept; } [..]

# new generation 2 by process 1378 (systemd-network)
add element ip io.systemd.nat masq_saddr { 192.168.159.192/28 }
# new generation 3 by process 1378 (systemd-network)
add element ip io.systemd.nat map_port_ipport { tcp . 2222 : 192.168.159.201 . 22 }
# new generation 4 by process 1512 (systemd-nspawn)
Quoted 9 lines

net/netfilter/nf_tables_api.c static LIST_HEAD(nf_tables_objects); · line 40

Quoted 5 lines
net/netfilter/nf_tables_api.c +5 -0
+struct nft_owner {
+	struct list_head	list;
+	possible_net_t		net;
+	u32			nlpid;
+};

I don't see why this is needed.
Isn't it enough to record the nlpid in the table and set a flag that the table is owned by that pid?

I'll have a look.

Quoted 9 lines

net/netfilter/nf_tables_api.c static struct nft_table *nft_table_lookup(const struct net *net, · line 549

Quoted 4 lines
net/netfilter/nf_tables_api.c +4 -0
+		    nft_active_genmask(table, genmask)) {
+			if (nlpid && table->nlpid && table->nlpid != nlpid)
+				return ERR_PTR(-EPERM);
+

i.e., (table->flags & OWNED) && table->nlpid != nlpid)?

On netlink sk destruction the owner flag could be cleared or table could be auto-zapped.

Default behaviour right now is: table is released if owner is gone.

It should be possible to add a flag to leave the ruleset in place (owner flag would be cleared from NETLINK_RELEASE event path).

maintainer
#3 lore raw
Quoted 8 lines · Pablo Neira Ayuso
Quoted 2 lines

At least for systemd use case, there would be a need to allow add/removal of set elements from other user.

Then, probably a flag for this? Such flag would work like this?

- Allow for set element updates (from any process, no ownership).
- nft flush ruleset skips flushing the set.
- nft flush set x y flushes the content of this set.

Right, i'd suggest some permission set that tells what is (dis)allowed.

Quoted 1 line

Would this work for the scenario you describe below?

I think so. We can add this later.

Quoted 11 lines
Quoted 9 lines

net/netfilter/nf_tables_api.c static struct nft_table *nft_table_lookup(const struct net *net, · line 549

Quoted 4 lines
net/netfilter/nf_tables_api.c +4 -0
+		    nft_active_genmask(table, genmask)) {
+			if (nlpid && table->nlpid && table->nlpid != nlpid)
+				return ERR_PTR(-EPERM);
+

i.e., (table->flags & OWNED) && table->nlpid != nlpid)?

On netlink sk destruction the owner flag could be cleared or table could be auto-zapped.

Default behaviour right now is: table is released if owner is gone.

I think thats fine.