In one net namespace, after creating a packet socket without binding
it to a device, users in other net namespaces can observe the new
`packet_type` added by this packet socket by reading `/proc/net/ptype`
file. I believe this is minor information leakage as packet socket is
namespace aware.
Add a function pointer in `packet_type` to retrieve the net namespace
of corresponding packet socket. In `ptype_seq_show`, if this
function pointer is not NULL, use it to determine if certain ptype
should be shown.
Signed-off-by: Congyu Liu <[email protected]>
---
include/linux/netdevice.h | 1 +
net/core/net-procfs.c | 3 ++-
net/packet/af_packet.c | 18 ++++++++++++++++++
3 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 3213c7227b59..72d3601850c5 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2548,6 +2548,7 @@ struct packet_type {
struct net_device *);
bool (*id_match)(struct packet_type *ptype,
struct sock *sk);
+ struct net *(*get_net) (struct packet_type *ptype);
void *af_packet_priv;
struct list_head list;
};
diff --git a/net/core/net-procfs.c b/net/core/net-procfs.c
index d8b9dbabd4a4..fd5e8682058c 100644
--- a/net/core/net-procfs.c
+++ b/net/core/net-procfs.c
@@ -260,7 +260,8 @@ static int ptype_seq_show(struct seq_file *seq, void *v)
if (v == SEQ_START_TOKEN)
seq_puts(seq, "Type Device Function\n");
- else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
+ else if ((pt->get_net && net_eq(pt->get_net(pt), seq_file_net(seq))) ||
+ (!pt->get_net && (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)))) {
if (pt->type == htons(ETH_P_ALL))
seq_puts(seq, "ALL ");
else
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 5bd409ab4cc2..7997b9db8280 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -1678,6 +1678,14 @@ static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
return false;
}
+static struct net *fanout_ptype_get_net(struct packet_type *pt)
+{
+ struct packet_fanout *f;
+
+ f = pt->af_packet_priv;
+ return read_pnet(&f->net);
+}
+
static int fanout_add(struct sock *sk, struct fanout_args *args)
{
struct packet_rollover *rollover = NULL;
@@ -1774,6 +1782,7 @@ static int fanout_add(struct sock *sk, struct fanout_args *args)
match->prot_hook.dev = po->prot_hook.dev;
match->prot_hook.func = packet_rcv_fanout;
match->prot_hook.af_packet_priv = match;
+ match->prot_hook.get_net = fanout_ptype_get_net;
match->prot_hook.id_match = match_fanout_group;
match->max_num_members = args->max_num_members;
list_add(&match->list, &fanout_list);
@@ -3294,6 +3303,14 @@ static struct proto packet_proto = {
.obj_size = sizeof(struct packet_sock),
};
+static struct net *packet_ptype_get_net(struct packet_type *pt)
+{
+ struct sock *sk;
+
+ sk = pt->af_packet_priv;
+ return sock_net(sk);
+}
+
/*
* Create a packet of type SOCK_PACKET.
*/
@@ -3353,6 +3370,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
po->prot_hook.func = packet_rcv_spkt;
po->prot_hook.af_packet_priv = sk;
+ po->prot_hook.get_net = packet_ptype_get_net;
if (proto) {
po->prot_hook.type = proto;
--
2.25.1
On Mon, Jan 17, 2022 at 11:01 PM Congyu Liu <[email protected]> wrote:
>
> In one net namespace, after creating a packet socket without binding
> it to a device, users in other net namespaces can observe the new
> `packet_type` added by this packet socket by reading `/proc/net/ptype`
> file. I believe this is minor information leakage as packet socket is
> namespace aware.
>
> Add a function pointer in `packet_type` to retrieve the net namespace
> of corresponding packet socket. In `ptype_seq_show`, if this
> function pointer is not NULL, use it to determine if certain ptype
> should be shown.
>
> Signed-off-by: Congyu Liu <[email protected]>
> ---
> include/linux/netdevice.h | 1 +
> net/core/net-procfs.c | 3 ++-
> net/packet/af_packet.c | 18 ++++++++++++++++++
> 3 files changed, 21 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
> index 3213c7227b59..72d3601850c5 100644
> --- a/include/linux/netdevice.h
> +++ b/include/linux/netdevice.h
> @@ -2548,6 +2548,7 @@ struct packet_type {
> struct net_device *);
> bool (*id_match)(struct packet_type *ptype,
> struct sock *sk);
> + struct net *(*get_net) (struct packet_type *ptype);
> void *af_packet_priv;
> struct list_head list;
> };
Patch looks fine, but the question is:
Can an af_packet socket created in netns A can be moved to netns B later ?
As the answer is probably no, it seems we could simply add a 'struct
net' pointer
in 'struct packet type', no need for a function.
Thanks.
Thanks! That makes sense. I will send a V2 patch.
________________________________________
??????: Eric Dumazet <[email protected]>
????ʱ??: 2022??1??18?? 3:40
?ռ???: Liu, Congyu
????: David Miller; Jakub Kicinski; Yajun Deng; Willem de Bruijn; Marc Kleine-Budde; [email protected]; Wang Hai; Pablo Neira Ayuso; [email protected]; netdev; LKML
????: Re: [PATCH net] net: fix information leakage in /proc/net/ptype
On Mon, Jan 17, 2022 at 11:01 PM Congyu Liu <[email protected]> wrote:
>
> In one net namespace, after creating a packet socket without binding
> it to a device, users in other net namespaces can observe the new
> `packet_type` added by this packet socket by reading `/proc/net/ptype`
> file. I believe this is minor information leakage as packet socket is
> namespace aware.
>
> Add a function pointer in `packet_type` to retrieve the net namespace
> of corresponding packet socket. In `ptype_seq_show`, if this
> function pointer is not NULL, use it to determine if certain ptype
> should be shown.
>
> Signed-off-by: Congyu Liu <[email protected]>
> ---
> include/linux/netdevice.h | 1 +
> net/core/net-procfs.c | 3 ++-
> net/packet/af_packet.c | 18 ++++++++++++++++++
> 3 files changed, 21 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
> index 3213c7227b59..72d3601850c5 100644
> --- a/include/linux/netdevice.h
> +++ b/include/linux/netdevice.h
> @@ -2548,6 +2548,7 @@ struct packet_type {
> struct net_device *);
> bool (*id_match)(struct packet_type *ptype,
> struct sock *sk);
> + struct net *(*get_net) (struct packet_type *ptype);
> void *af_packet_priv;
> struct list_head list;
> };
Patch looks fine, but the question is:
Can an af_packet socket created in netns A can be moved to netns B later ?
As the answer is probably no, it seems we could simply add a 'struct
net' pointer
in 'struct packet type', no need for a function.
Thanks.