Skip to content

Commit 95fac54

Browse files
committed
Merge branch 'raw-ping-fix-locking-in-proc-net-raw-icmp'
Kuniyuki Iwashima says: ==================== raw/ping: Fix locking in /proc/net/{raw,icmp}. The first patch fixes a NULL deref for /proc/net/raw and second one fixes the same issue for ping sockets. The first patch also converts hlist_nulls to hlist, but this is because the current code uses sk_nulls_for_each() for lockless readers, instead of sk_nulls_for_each_rcu() which adds memory barrier, but raw sockets does not use the nulls marker nor SLAB_TYPESAFE_BY_RCU in the first place. OTOH, the ping sockets already uses sk_nulls_for_each_rcu(), and such conversion can be posted later for net-next. ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 218c597 + ab5fb73 commit 95fac54

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

include/net/raw.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int raw_rcv(struct sock *, struct sk_buff *);
3737
struct raw_hashinfo {
3838
spinlock_t lock;
3939

40-
struct hlist_nulls_head ht[RAW_HTABLE_SIZE] ____cacheline_aligned;
40+
struct hlist_head ht[RAW_HTABLE_SIZE] ____cacheline_aligned;
4141
};
4242

4343
static inline u32 raw_hashfunc(const struct net *net, u32 proto)
@@ -51,7 +51,7 @@ static inline void raw_hashinfo_init(struct raw_hashinfo *hashinfo)
5151

5252
spin_lock_init(&hashinfo->lock);
5353
for (i = 0; i < RAW_HTABLE_SIZE; i++)
54-
INIT_HLIST_NULLS_HEAD(&hashinfo->ht[i], i);
54+
INIT_HLIST_HEAD(&hashinfo->ht[i]);
5555
}
5656

5757
#ifdef CONFIG_PROC_FS

net/ipv4/ping.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,13 +1089,13 @@ static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
10891089
}
10901090

10911091
void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
1092-
__acquires(RCU)
1092+
__acquires(ping_table.lock)
10931093
{
10941094
struct ping_iter_state *state = seq->private;
10951095
state->bucket = 0;
10961096
state->family = family;
10971097

1098-
rcu_read_lock();
1098+
spin_lock(&ping_table.lock);
10991099

11001100
return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
11011101
}
@@ -1121,9 +1121,9 @@ void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
11211121
EXPORT_SYMBOL_GPL(ping_seq_next);
11221122

11231123
void ping_seq_stop(struct seq_file *seq, void *v)
1124-
__releases(RCU)
1124+
__releases(ping_table.lock)
11251125
{
1126-
rcu_read_unlock();
1126+
spin_unlock(&ping_table.lock);
11271127
}
11281128
EXPORT_SYMBOL_GPL(ping_seq_stop);
11291129

net/ipv4/raw.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ EXPORT_SYMBOL_GPL(raw_v4_hashinfo);
9191
int raw_hash_sk(struct sock *sk)
9292
{
9393
struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
94-
struct hlist_nulls_head *hlist;
94+
struct hlist_head *hlist;
9595

9696
hlist = &h->ht[raw_hashfunc(sock_net(sk), inet_sk(sk)->inet_num)];
9797

9898
spin_lock(&h->lock);
99-
__sk_nulls_add_node_rcu(sk, hlist);
99+
sk_add_node_rcu(sk, hlist);
100100
sock_set_flag(sk, SOCK_RCU_FREE);
101101
spin_unlock(&h->lock);
102102
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
@@ -110,7 +110,7 @@ void raw_unhash_sk(struct sock *sk)
110110
struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
111111

112112
spin_lock(&h->lock);
113-
if (__sk_nulls_del_node_init_rcu(sk))
113+
if (sk_del_node_init_rcu(sk))
114114
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
115115
spin_unlock(&h->lock);
116116
}
@@ -163,16 +163,15 @@ static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
163163
static int raw_v4_input(struct net *net, struct sk_buff *skb,
164164
const struct iphdr *iph, int hash)
165165
{
166-
struct hlist_nulls_head *hlist;
167-
struct hlist_nulls_node *hnode;
168166
int sdif = inet_sdif(skb);
167+
struct hlist_head *hlist;
169168
int dif = inet_iif(skb);
170169
int delivered = 0;
171170
struct sock *sk;
172171

173172
hlist = &raw_v4_hashinfo.ht[hash];
174173
rcu_read_lock();
175-
sk_nulls_for_each(sk, hnode, hlist) {
174+
sk_for_each_rcu(sk, hlist) {
176175
if (!raw_v4_match(net, sk, iph->protocol,
177176
iph->saddr, iph->daddr, dif, sdif))
178177
continue;
@@ -264,10 +263,9 @@ static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
264263
void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
265264
{
266265
struct net *net = dev_net(skb->dev);
267-
struct hlist_nulls_head *hlist;
268-
struct hlist_nulls_node *hnode;
269266
int dif = skb->dev->ifindex;
270267
int sdif = inet_sdif(skb);
268+
struct hlist_head *hlist;
271269
const struct iphdr *iph;
272270
struct sock *sk;
273271
int hash;
@@ -276,7 +274,7 @@ void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
276274
hlist = &raw_v4_hashinfo.ht[hash];
277275

278276
rcu_read_lock();
279-
sk_nulls_for_each(sk, hnode, hlist) {
277+
sk_for_each_rcu(sk, hlist) {
280278
iph = (const struct iphdr *)skb->data;
281279
if (!raw_v4_match(net, sk, iph->protocol,
282280
iph->daddr, iph->saddr, dif, sdif))
@@ -950,14 +948,13 @@ static struct sock *raw_get_first(struct seq_file *seq, int bucket)
950948
{
951949
struct raw_hashinfo *h = pde_data(file_inode(seq->file));
952950
struct raw_iter_state *state = raw_seq_private(seq);
953-
struct hlist_nulls_head *hlist;
954-
struct hlist_nulls_node *hnode;
951+
struct hlist_head *hlist;
955952
struct sock *sk;
956953

957954
for (state->bucket = bucket; state->bucket < RAW_HTABLE_SIZE;
958955
++state->bucket) {
959956
hlist = &h->ht[state->bucket];
960-
sk_nulls_for_each(sk, hnode, hlist) {
957+
sk_for_each(sk, hlist) {
961958
if (sock_net(sk) == seq_file_net(seq))
962959
return sk;
963960
}
@@ -970,7 +967,7 @@ static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
970967
struct raw_iter_state *state = raw_seq_private(seq);
971968

972969
do {
973-
sk = sk_nulls_next(sk);
970+
sk = sk_next(sk);
974971
} while (sk && sock_net(sk) != seq_file_net(seq));
975972

976973
if (!sk)
@@ -989,9 +986,12 @@ static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
989986
}
990987

991988
void *raw_seq_start(struct seq_file *seq, loff_t *pos)
992-
__acquires(RCU)
989+
__acquires(&h->lock)
993990
{
994-
rcu_read_lock();
991+
struct raw_hashinfo *h = pde_data(file_inode(seq->file));
992+
993+
spin_lock(&h->lock);
994+
995995
return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
996996
}
997997
EXPORT_SYMBOL_GPL(raw_seq_start);
@@ -1010,9 +1010,11 @@ void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
10101010
EXPORT_SYMBOL_GPL(raw_seq_next);
10111011

10121012
void raw_seq_stop(struct seq_file *seq, void *v)
1013-
__releases(RCU)
1013+
__releases(&h->lock)
10141014
{
1015-
rcu_read_unlock();
1015+
struct raw_hashinfo *h = pde_data(file_inode(seq->file));
1016+
1017+
spin_unlock(&h->lock);
10161018
}
10171019
EXPORT_SYMBOL_GPL(raw_seq_stop);
10181020

net/ipv4/raw_diag.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ static bool raw_lookup(struct net *net, struct sock *sk,
5757
static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
5858
{
5959
struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
60-
struct hlist_nulls_head *hlist;
61-
struct hlist_nulls_node *hnode;
60+
struct hlist_head *hlist;
6261
struct sock *sk;
6362
int slot;
6463

@@ -68,7 +67,7 @@ static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2
6867
rcu_read_lock();
6968
for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
7069
hlist = &hashinfo->ht[slot];
71-
sk_nulls_for_each(sk, hnode, hlist) {
70+
sk_for_each_rcu(sk, hlist) {
7271
if (raw_lookup(net, sk, r)) {
7372
/*
7473
* Grab it and keep until we fill
@@ -142,9 +141,8 @@ static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
142141
struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
143142
struct net *net = sock_net(skb->sk);
144143
struct inet_diag_dump_data *cb_data;
145-
struct hlist_nulls_head *hlist;
146-
struct hlist_nulls_node *hnode;
147144
int num, s_num, slot, s_slot;
145+
struct hlist_head *hlist;
148146
struct sock *sk = NULL;
149147
struct nlattr *bc;
150148

@@ -161,7 +159,7 @@ static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
161159
num = 0;
162160

163161
hlist = &hashinfo->ht[slot];
164-
sk_nulls_for_each(sk, hnode, hlist) {
162+
sk_for_each_rcu(sk, hlist) {
165163
struct inet_sock *inet = inet_sk(sk);
166164

167165
if (!net_eq(sock_net(sk), net))

net/ipv6/raw.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ EXPORT_SYMBOL(rawv6_mh_filter_unregister);
141141
static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
142142
{
143143
struct net *net = dev_net(skb->dev);
144-
struct hlist_nulls_head *hlist;
145-
struct hlist_nulls_node *hnode;
146144
const struct in6_addr *saddr;
147145
const struct in6_addr *daddr;
146+
struct hlist_head *hlist;
148147
struct sock *sk;
149148
bool delivered = false;
150149
__u8 hash;
@@ -155,7 +154,7 @@ static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
155154
hash = raw_hashfunc(net, nexthdr);
156155
hlist = &raw_v6_hashinfo.ht[hash];
157156
rcu_read_lock();
158-
sk_nulls_for_each(sk, hnode, hlist) {
157+
sk_for_each_rcu(sk, hlist) {
159158
int filtered;
160159

161160
if (!raw_v6_match(net, sk, nexthdr, daddr, saddr,
@@ -333,15 +332,14 @@ void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
333332
u8 type, u8 code, int inner_offset, __be32 info)
334333
{
335334
struct net *net = dev_net(skb->dev);
336-
struct hlist_nulls_head *hlist;
337-
struct hlist_nulls_node *hnode;
335+
struct hlist_head *hlist;
338336
struct sock *sk;
339337
int hash;
340338

341339
hash = raw_hashfunc(net, nexthdr);
342340
hlist = &raw_v6_hashinfo.ht[hash];
343341
rcu_read_lock();
344-
sk_nulls_for_each(sk, hnode, hlist) {
342+
sk_for_each_rcu(sk, hlist) {
345343
/* Note: ipv6_hdr(skb) != skb->data */
346344
const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
347345

0 commit comments

Comments
 (0)