media: cec: syzkaller fixes
Subsystems (2)
cover media: cec: syzkaller fixes
Chenyuan Yang found a bunch of syzkall issues:
The patches in this series fix the various issues, and with these in place, it passed the syzkaller tests.
Many thanks to Chenyuan for reporting this and doing all the testing, since I had a hard timing reproducing them. It took a few months to track it all down because of that.
Regards,
Hans
Hans Verkuil (4):
media: cec: cec-adap: always cancel work in cec_transmit_msg_fh
media: cec: cec-api: add locking in cec_release()
media: cec: core: avoid recursive cec_claim_log_addrs
media: cec: core: avoid confusing "transmit timed out" message
drivers/media/cec/core/cec-adap.c | 24 ++++++++++++++++++++---- drivers/media/cec/core/cec-api.c | 5 ++++- include/media/cec.h | 1 + 3 files changed, 25 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/4 media: cec: cec-adap: always cancel work in cec_transmit_msg_fh
Do not check for !data->completed, just always call cancel_delayed_work_sync(). This fixes a small race condition.
--- drivers/media/cec/core/cec-adap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/media/cec/core/cec-adap.c +1 -2
@@ -936,8 +936,7 @@ int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,*/ mutex_unlock (&adap->lock);wait_for_completion_killable (&data->c); -if (!data->completed) -cancel_delayed_work_sync (&data->work); +cancel_delayed_work_sync (&data->work);mutex_lock (&adap->lock);/* Cancel the transmit if it was interrupted */
Signature · 1 line
2.43.0
PATCH 2/4 media: cec: cec-api: add locking in cec_release()
In reply to Hans VerkuilWhen cec_release() uses fh->msgs it has to take fh->lock, otherwise the list can get corrupted.
--- drivers/media/cec/core/cec-api.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/media/cec/core/cec-api.c +3 -0
@@ -664,6 +664,8 @@ static int cec_release(struct inode *inode, struct file *filp)list_del_init (&data->xfer_list); }mutex_unlock (&adap->lock); + +mutex_lock (&fh->lock);while (!list_empty (&fh->msgs)) {struct cec_msg_entry *entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
@@ -681,6 +683,7 @@ static int cec_release(struct inode *inode, struct file *filp)kfree (entry); } } +mutex_unlock (&fh->lock);kfree (fh);cec_put_device (devnode);
Signature · 1 line
2.43.0
PATCH 3/4 media: cec: core: avoid recursive cec_claim_log_addrs
In reply to Hans VerkuilKeep track if cec_claim_log_addrs() is running, and return -EBUSY if it is when calling CEC_ADAP_S_LOG_ADDRS.
This prevents a case where cec_claim_log_addrs() could be called while it was still in progress.
--- drivers/media/cec/core/cec-adap.c | 6 +++++- drivers/media/cec/core/cec-api.c | 2 +- include/media/cec.h | 1 + 3 files changed, 7 insertions(+), 2 deletions(-)
drivers/media/cec/core/cec-adap.c +5 -1
@@ -1574,9 +1574,12 @@ static int cec_config_thread_func(void *arg)*/ static void cec_claim_log_addrs (struct cec_adapter *adap,bool block) { -if (WARN_ON (adap->is_configuring || adap->is_configured)) +if (WARN_ON (adap->is_claiming_log_addrs || + adap->is_configuring || adap->is_configured))return ; + adap->is_claiming_log_addrs = true; +init_completion (&adap->config_completion);/* Ready to kick off the thread */
@@ -1591,6 +1594,7 @@ static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)wait_for_completion (&adap->config_completion);mutex_lock (&adap->lock); } + adap->is_claiming_log_addrs = false; }/*
drivers/media/cec/core/cec-api.c +1 -1
@@ -178,7 +178,7 @@ static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |CEC_LOG_ADDRS_FL_CDC_ONLY ;mutex_lock (&adap->lock); -if (!adap->is_configuring && +if (!adap->is_claiming_log_addrs && !adap->is_configuring && (!log_addrs.num_log_addrs || !adap->is_configured) && !cec_is_busy (adap, fh)) { err =__cec_s_log_addrs (adap, &log_addrs, block);
include/media/cec.h +1 -0
@@ -258,6 +258,7 @@ struct cec_adapter {u16 phys_addr;bool needs_hpd;bool is_enabled; +bool is_claiming_log_addrs;bool is_configuring;bool must_reconfigure;bool is_configured;
Signature · 1 line
2.43.0
PATCH 4/4 media: cec: core: avoid confusing "transmit timed out" message
In reply to Hans VerkuilIf, when waiting for a transmit to finish, the wait is interrupted, then you might get a "transmit timed out" message, even though the transmit was interrupted and did not actually time out.
Set transmit_in_progress_aborted to true if the wait_for_completion_killable() call was interrupted and ensure that the transmit is properly marked as ABORTED.
--- drivers/media/cec/core/cec-adap.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/media/cec/core/cec-adap.c +14 -1
@@ -490,6 +490,15 @@ int cec_thread_func(void *_adap) goto unlock; } +if (adap->transmit_in_progress && + adap->transmit_in_progress_aborted) { +if (adap->transmitting) +cec_data_cancel (adap->transmitting, +CEC_TX_STATUS_ABORTED ,0 ); + adap->transmit_in_progress = false; + adap->transmit_in_progress_aborted = false; + goto unlock; + }if (adap->transmit_in_progress && timeout) {/* * If we timeout, then log that. Normally this does
@@ -771,6 +780,7 @@ int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg, {struct cec_data *data;bool is_raw =msg_is_raw (msg); +int err;if (adap->devnode.unregistered)return -ENODEV ;
@@ -935,10 +945,13 @@ int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,* Release the lock and wait, retake the lock afterwards. */ mutex_unlock (&adap->lock); -wait_for_completion_killable (&data->c); + err =wait_for_completion_killable (&data->c);cancel_delayed_work_sync (&data->work);mutex_lock (&adap->lock); +if (err) + adap->transmit_in_progress_aborted = true; +/* Cancel the transmit if it was interrupted */ if (!data->completed) {if (data->msg.tx_status &CEC_TX_STATUS_OK )
Signature · 1 line
2.43.0
Quoted 14 lines · Hans Verkuil
Chenyuan Yang found a bunch of syzkall issues:
The patches in this series fix the various issues, and with these in place, it passed the syzkaller tests.
Many thanks to Chenyuan for reporting this and doing all the testing, since I had a hard timing reproducing them. It took a few months to track it all down because of that.
Regards,
Quoted 7 lines
Hans
Hans Verkuil (4):
media: cec: cec-adap: always cancel work incec_transmit_msg_fh
media: cec: cec-api: add locking incec_release()
media: cec: core: avoid recursivecec_claim_log_addrs
media: cec: core: avoid confusing "transmit timed out" message
Thanks for your patient bug hunts!
Takashi