Skip to content

Commit 6f25f3f

Browse files
committed
Add safety checks on namespaces initialization
1 parent dc12685 commit 6f25f3f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

kernel/linux/pf_ring.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,8 @@ struct pf_ring_socket {
13951395
/* **************************************** */
13961396

13971397
typedef struct {
1398+
u_int32_t magic;
1399+
13981400
struct net *net;
13991401

14001402
/* /proc entry for ring module */
@@ -1411,7 +1413,7 @@ typedef struct {
14111413

14121414
/* Keep track of number of rings per device (plus any) */
14131415
u_int8_t num_rings_per_device[MAX_NUM_DEV_IDX];
1414-
u_int8_t num_any_rings;
1416+
u_int32_t num_any_rings;
14151417
} pf_ring_net;
14161418

14171419
/* **************************************** */

kernel/pf_ring.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -839,12 +839,16 @@ void term_lockless_list(lockless_list *l, u_int8_t free_memory)
839839
/* ********************************** */
840840

841841
pf_ring_net *netns_lookup(struct net *net) {
842-
pf_ring_net *pf_net = net_generic(net, pf_ring_net_id);
842+
pf_ring_net *netns = net_generic(net, pf_ring_net_id);
843843

844-
if (pf_net == NULL)
845-
printk("[PF_RING] Namespace lookup failure\n");
844+
if (netns == NULL) {
845+
printk("[PF_RING] Namespace lookup failure (not found)\n");
846+
} else if (netns->magic != RING_MAGIC_VALUE) {
847+
printk("[PF_RING] Namespace lookup failure (corruption detected)\n");
848+
netns = NULL;
849+
}
846850

847-
return pf_net;
851+
return netns;
848852
}
849853

850854
/* ********************************** */
@@ -859,6 +863,9 @@ static inline int device_net_eq(pf_ring_device *dev_ptr, struct net *net) {
859863
pf_ring_net *netns_add(struct net *net) {
860864
pf_ring_net *netns = net_generic(net, pf_ring_net_id);
861865

866+
memset(netns, 0, sizeof(pf_ring_net));
867+
netns->magic = RING_MAGIC_VALUE;
868+
862869
netns->net = net;
863870
ring_proc_init(netns);
864871

@@ -9152,7 +9159,7 @@ static struct notifier_block ring_netdev_notifier = {
91529159

91539160
static int __net_init ring_net_init(struct net *net)
91549161
{
9155-
debug_printk(1, "init network namespace [net=%pK]\n", net);
9162+
debug_printk(1, "init network namespace [net=%p]\n", net);
91569163
netns_add(net);
91579164
return 0;
91589165
}
@@ -9161,7 +9168,7 @@ static int __net_init ring_net_init(struct net *net)
91619168

91629169
static void __net_exit ring_net_exit(struct net *net)
91639170
{
9164-
debug_printk(1, "exit network namespace [net=%pK]\n", net);
9171+
debug_printk(1, "exit network namespace [net=%p]\n", net);
91659172
netns_remove(net);
91669173
}
91679174

0 commit comments

Comments
 (0)