Hello,
While reading the code of net/core/sock_reuseport.c, I think I found a
possible race in reuseport_select_sock. The current code has the
following pattern:
socks = READ_ONCE(reuse->num_socks);
smp_rmb(); // paired with reuseport_add_sock to make sure
reuse->socks[i < num_socks] are initialized
while (reuse->socks[i]->sk_state == TCP_ESTABLISHED) {
if (i >= reuse->num_socks) // should be "socks" and not
"reuse->num_socks"?
The barrier seems to be here to make sure that all items of
reuse->socks are initialized before being read, but the barrier only
protects indexes < socks, not indexes < reuse->num_socks.
I have a patch ready to fix this issue, but I wanted to confirm that
the current code is indeed incorrect (if the code is correct for a
non-obvious reason, I'd be happy to add some comments to document the
behavior).
Here is the diff of the patch I was planning to submit:
From: Willem de Bruijn <willemdebruijn.kernel@gmail.com> Date: 2021-01-07 04:58:32
On Wed, Jan 6, 2021 at 10:54 PM Baptiste Lepers
[off-list ref] wrote:
quoted hunk
Hello,
While reading the code of net/core/sock_reuseport.c, I think I found a
possible race in reuseport_select_sock. The current code has the
following pattern:
socks = READ_ONCE(reuse->num_socks);
smp_rmb(); // paired with reuseport_add_sock to make sure
reuse->socks[i < num_socks] are initialized
while (reuse->socks[i]->sk_state == TCP_ESTABLISHED) {
if (i >= reuse->num_socks) // should be "socks" and not
"reuse->num_socks"?
The barrier seems to be here to make sure that all items of
reuse->socks are initialized before being read, but the barrier only
protects indexes < socks, not indexes < reuse->num_socks.
I have a patch ready to fix this issue, but I wanted to confirm that
the current code is indeed incorrect (if the code is correct for a
non-obvious reason, I'd be happy to add some comments to document the
behavior).
Here is the diff of the patch I was planning to submit:
Thanks. I will submit a patch.
Baptiste.
On Thu, Jan 7, 2021 at 3:57 PM Willem de Bruijn
[off-list ref] wrote:
On Wed, Jan 6, 2021 at 10:54 PM Baptiste Lepers
[off-list ref] wrote:
quoted
Hello,
While reading the code of net/core/sock_reuseport.c, I think I found a
possible race in reuseport_select_sock. The current code has the
following pattern:
socks = READ_ONCE(reuse->num_socks);
smp_rmb(); // paired with reuseport_add_sock to make sure
reuse->socks[i < num_socks] are initialized
while (reuse->socks[i]->sk_state == TCP_ESTABLISHED) {
if (i >= reuse->num_socks) // should be "socks" and not
"reuse->num_socks"?
The barrier seems to be here to make sure that all items of
reuse->socks are initialized before being read, but the barrier only
protects indexes < socks, not indexes < reuse->num_socks.
I have a patch ready to fix this issue, but I wanted to confirm that
the current code is indeed incorrect (if the code is correct for a
non-obvious reason, I'd be happy to add some comments to document the
behavior).
Here is the diff of the patch I was planning to submit: