Skip to contentSkip to conversation

media: cec: syzkaller fixes

PATCH In review Hans Verkuil 2 participants 6 messages May 2, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover media: cec: syzkaller fixes

Chenyuan Yang found a bunch of syzkall issues:

https://lore.kernel.org/linux-media/PH7PR11MB57688E64ADE4FE82E658D86DA09EA@PH7PR11MB5768.namprd11.prod.outlook.com/

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
#1 lore raw

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
index 559a172ebc6cb..6fc7de744ee9a 100644
@@ -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
#2 lore raw

PATCH 2/4 media: cec: cec-api: add locking in cec_release()

In reply to Hans Verkuil

When 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
index 67dc79ef17050..d64bb716f9c68 100644
@@ -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
#3 lore raw

PATCH 3/4 media: cec: core: avoid recursive cec_claim_log_addrs

In reply to Hans Verkuil

Keep 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
index 6fc7de744ee9a..a493cbce24567 100644
@@ -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
index d64bb716f9c68..3ef9153443044 100644
@@ -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
index c8350258f10af..d3abe84c9c9e5 100644
@@ -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
#4 lore raw

PATCH 4/4 media: cec: core: avoid confusing "transmit timed out" message

In reply to Hans Verkuil

If, 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
index a493cbce24567..da09834990b87 100644
@@ -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
#5 lore raw
In reply to Hans Verkuil
Quoted 14 lines · Hans Verkuil

Chenyuan Yang found a bunch of syzkall issues:

https://lore.kernel.org/linux-media/PH7PR11MB57688E64ADE4FE82E658D86DA09EA@PH7PR11MB5768.namprd11.prod.outlook.com/

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 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

Thanks for your patient bug hunts!

Takashi