libceph: protect pending flags in ceph_con_keepalive()
Subsystems (3)
PATCH 1/1 libceph: protect pending flags in ceph_con_keepalive()
con_flag_test_and_set() sets CON_FLAG_KEEPALIVE_PENDING and CON_FLAG_WRITE_PENDING flags without protection in ceph_con_keepalive(). It triggers WARN_ON() in clear_standby() if the flags are set after con_fault() changes connection state to CON_STATE_STANDBY. Move con_flag_test_and_set() to be called before releasing the lock and store the condition to check after the critical section.
--- net/ceph/messenger.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
net/ceph/messenger.c +6 -2
@@ -3216,12 +3216,16 @@ void ceph_msg_revoke_incoming(struct ceph_msg *msg)*/ void ceph_con_keepalive (struct ceph_connection *con) { +bool pending; +dout ("con_keepalive %p\n" , con);mutex_lock (&con->mutex);clear_standby (con); + pending = (con_flag_test_and_set (con, +CON_FLAG_KEEPALIVE_PENDING ) ==0 && +con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 );mutex_unlock (&con->mutex); -if (con_flag_test_and_set (con,CON_FLAG_KEEPALIVE_PENDING ) ==0 && -con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 ) +if (pending)queue_con (con); }EXPORT_SYMBOL (ceph_con_keepalive);
Signature · 1 line
2.17.1
Ilya Dryomov
maintainerQuoted 27 lines · Myungho Jung
con_flag_test_and_set()setsCON_FLAG_KEEPALIVE_PENDINGandCON_FLAG_WRITE_PENDINGflags without protection inceph_con_keepalive(). It triggersWARN_ON()inclear_standby()if the flags are set aftercon_fault()changes connection state toCON_STATE_STANDBY. Movecon_flag_test_and_set()to be called before releasing the lock and store the condition to check after the critical section.Reported-by [email protected]Signed-off-by Myungho Jung <[email protected]>--- net/ceph/messenger.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)net/ceph/messenger.c +2
-0@@ -3216,12 +3216,16 @@ void ceph_msg_revoke_incoming(struct ceph_msg *msg)*/ void ceph_con_keepalive (struct ceph_connection *con) { +bool pending; +dout ("con_keepalive %p\n" , con);mutex_lock (&con->mutex);clear_standby (con);
net/ceph/messenger.c void ceph_msg_revoke_incoming(struct ceph_msg *msg) · line 3231
Quoted 10 lines
net/ceph/messenger.c +4
-2+ pending = (con_flag_test_and_set (con, +CON_FLAG_KEEPALIVE_PENDING ) ==0 && +con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 );mutex_unlock (&con->mutex); -if (con_flag_test_and_set (con,CON_FLAG_KEEPALIVE_PENDING ) ==0 && -con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 ) +if (pending)queue_con (con); }EXPORT_SYMBOL (ceph_con_keepalive);
Hi Myungho,
Were you able to reproduce? If so, did you use the syzkaller output or something else?
Thanks,
Ilya
Quoted 41 lines · Ilya Dryomov
Quoted 27 lines · Myungho Jung
con_flag_test_and_set()setsCON_FLAG_KEEPALIVE_PENDINGandCON_FLAG_WRITE_PENDINGflags without protection inceph_con_keepalive(). It triggersWARN_ON()inclear_standby()if the flags are set aftercon_fault()changes connection state toCON_STATE_STANDBY. Movecon_flag_test_and_set()to be called before releasing the lock and store the condition to check after the critical section.Reported-by [email protected]Signed-off-by Myungho Jung <[email protected]>--- net/ceph/messenger.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)net/ceph/messenger.c +2
-0@@ -3216,12 +3216,16 @@ void ceph_msg_revoke_incoming(struct ceph_msg *msg)*/ void ceph_con_keepalive (struct ceph_connection *con) { +bool pending; +dout ("con_keepalive %p\n" , con);mutex_lock (&con->mutex);clear_standby (con);net/ceph/messenger.c void ceph_msg_revoke_incoming(struct ceph_msg *msg) · line 3231
Quoted 10 lines
net/ceph/messenger.c +4
-2+ pending = (con_flag_test_and_set (con, +CON_FLAG_KEEPALIVE_PENDING ) ==0 && +con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 );mutex_unlock (&con->mutex); -if (con_flag_test_and_set (con,CON_FLAG_KEEPALIVE_PENDING ) ==0 && -con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 ) +if (pending)queue_con (con); }EXPORT_SYMBOL (ceph_con_keepalive);Hi Myungho,
Quoted 6 lines
Were you able to reproduce? If so, did you use the syzkaller output or something else?
Thanks,
Ilya
Hi Ilya,
I reproduced on vm using syzkaller utils and verified the fix by syzbot.
Thanks,
Myungho
Ilya Dryomov
maintainerQuoted 1 line · Myungho Jung
I reproduced on vm using syzkaller utils and verified the fix by syzbot.
Hi Myungho,
I think this might be a better fix:
net/ceph/messenger.c +3 -2
@@ -3205,10 +3205,11 @@ void ceph_con_keepalive(struct ceph_connection *con) {dout ("con_keepalive %p\n" , con);mutex_lock (&con->mutex); +con_flag_set (con,CON_FLAG_KEEPALIVE_PENDING );clear_standby (con);mutex_unlock (&con->mutex); -if (con_flag_test_and_set (con,CON_FLAG_KEEPALIVE_PENDING ) ==0 && -con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 ) + +if (con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 )queue_con (con); }EXPORT_SYMBOL (ceph_con_keepalive);
WRITE_PENDING can be set without con->mutex held from socket callbacks. This is the reason we use atomic bit ops here, so testing WRITE_PENDING under the lock didn't make sense to me.
At the same time, KEEPALIVE_PENDING could have been a non-atomic flag. I spent some time trying to make sense of conditioning queue_con() call on the previous value of KEEPALIVE_PENDING and couldn't see any, so I'm setting it with con_flag_set(), making ceph_con_keepalive() symmetric with ceph_con_send().
Thanks,
Ilya
Quoted 36 lines · Ilya Dryomov
Quoted 1 line · Myungho Jung
I reproduced on vm using syzkaller utils and verified the fix by syzbot.
Hi Myungho,
I think this might be a better fix:
net/ceph/messenger.c +3
-2@@ -3205,10 +3205,11 @@ void ceph_con_keepalive(struct ceph_connection *con) {dout ("con_keepalive %p\n" , con);mutex_lock (&con->mutex); +con_flag_set (con,CON_FLAG_KEEPALIVE_PENDING );clear_standby (con);mutex_unlock (&con->mutex); -if (con_flag_test_and_set (con,CON_FLAG_KEEPALIVE_PENDING ) ==0 && -con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 ) + +if (con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 )queue_con (con); }EXPORT_SYMBOL (ceph_con_keepalive);
WRITE_PENDINGcan be set withoutcon->mutexheld from socket callbacks. This is the reason we use atomic bit ops here, so testingWRITE_PENDINGunder the lock didn't make sense to me.At the same time,
KEEPALIVE_PENDINGcould have been a non-atomic flag. I spent some time trying to make sense of conditioningqueue_con()call on the previous value ofKEEPALIVE_PENDINGand couldn't see any, so I'm setting it withcon_flag_set(), makingceph_con_keepalive()symmetric withceph_con_send().
Quoted 3 lines
Thanks,
Ilya
Hi Ilya,
Yes, it looks clear and makes sense to have an atomic operation in if statement but it still triggers warning. KEEPALIVE_PENDING should be set after clear_standby() because con_fault() can be called right before acquiring the lock here which sets the flag in standby state. I tesed the change with syzbot and confirmed there was no warning.
Thanks,
Myungho
Ilya Dryomov
maintainerQuoted 44 lines · Myungho Jung
Quoted 36 lines · Ilya Dryomov
Quoted 1 line · Myungho Jung
I reproduced on vm using syzkaller utils and verified the fix by syzbot.
Hi Myungho,
I think this might be a better fix:
net/ceph/messenger.c +3
-2@@ -3205,10 +3205,11 @@ void ceph_con_keepalive(struct ceph_connection *con) {dout ("con_keepalive %p\n" , con);mutex_lock (&con->mutex); +con_flag_set (con,CON_FLAG_KEEPALIVE_PENDING );clear_standby (con);mutex_unlock (&con->mutex); -if (con_flag_test_and_set (con,CON_FLAG_KEEPALIVE_PENDING ) ==0 && -con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 ) + +if (con_flag_test_and_set (con,CON_FLAG_WRITE_PENDING ) ==0 )queue_con (con); }EXPORT_SYMBOL (ceph_con_keepalive);
WRITE_PENDINGcan be set withoutcon->mutexheld from socket callbacks. This is the reason we use atomic bit ops here, so testingWRITE_PENDINGunder the lock didn't make sense to me.At the same time,
KEEPALIVE_PENDINGcould have been a non-atomic flag. I spent some time trying to make sense of conditioningqueue_con()call on the previous value ofKEEPALIVE_PENDINGand couldn't see any, so I'm setting it withcon_flag_set(), makingceph_con_keepalive()symmetric withceph_con_send().Quoted 3 lines
Thanks,
Ilya
Hi Ilya,
Quoted 5 lines
Yes, it looks clear and makes sense to have an atomic operation in if statement but it still triggers warning.
KEEPALIVE_PENDINGshould be set afterclear_standby()becausecon_fault()can be called right before acquiring the lock here which sets the flag in standby state. I tesed the change with syzbot and confirmed there was no warning.
Right, it still triggers one of the warnings. I was too focused on WRITE_PENDING and missed that in plain sight. I'll update the patch.
Thanks for testing!
Ilya