[PATCH 3/3] coda: hold vc_mutex while completing a Venus reply

From: Mar Dominguez

Date: Sat Dec 05 2026 - 00:56:11 EST


coda_psdev_write() unlinks the request from vc_processing under
vc_mutex, drops the mutex, and only then copies the reply from Venus
into req->uc_data, sets CODA_REQ_WRITE and wakes the waiter.

The waiter in coda_waitfor_upcall() runs under vc_mutex whenever it is
not asleep. If it is woken by a signal (SIGKILL and SIGINT are never
blocked) after the request was unlinked but before CODA_REQ_WRITE is
set, it sees neither CODA_REQ_WRITE nor CODA_REQ_ABORT and calls
list_del() on the already unlinked entry, then coda_upcall() frees req
and its caller frees req->uc_data. coda_psdev_write() then copies the
reply into the freed buffer and writes req->uc_outSize, req->uc_flags
and req->uc_sleep. A waiter whose one-second timeout expires between
the CODA_REQ_WRITE update and wake_up() frees req the same way, and
wake_up() then runs on freed memory. A failed fget() for
CODA_OPEN_BY_FD also returns without waking the waiter at all.

Keep vc_mutex held from the lookup until CODA_REQ_WRITE or
CODA_REQ_ABORT is set and the waiter is woken, as coda_psdev_read()
already does for its copy_to_user(). Set CODA_REQ_WRITE after the
CODA_OPEN_BY_FD file lookup and wake the waiter when that lookup
fails too; it then sees a NULL host file and coda_open() returns -EIO
as before.

Found and written with an LLM coding assistant.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Mar Dominguez <mar@xxxxxxxxxxxx>
---
fs/coda/psdev.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/fs/coda/psdev.c b/fs/coda/psdev.c
index 3c3148588..6af14a87c 100644
--- a/fs/coda/psdev.c
+++ b/fs/coda/psdev.c
@@ -143,7 +143,12 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
goto out;
}

- /* Look for the message on the processing queue. */
+ /*
+ * Look for the message on the processing queue. Hold vc_mutex until
+ * the reply is in place and the request is marked done: the waiter
+ * in coda_waitfor_upcall() unlinks and frees an interrupted request
+ * under vc_mutex.
+ */
mutex_lock(&vcp->vc_mutex);
list_for_each(lh, &vcp->vc_processing) {
tmp = list_entry(lh, struct upc_req , uc_chain);
@@ -153,9 +158,9 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
break;
}
}
- mutex_unlock(&vcp->vc_mutex);

if (!req) {
+ mutex_unlock(&vcp->vc_mutex);
pr_warn("%s: msg (%d, %d) not found\n",
__func__, hdr.opcode, hdr.unique);
retval = -ESRCH;
@@ -173,12 +178,11 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
req->uc_flags |= CODA_REQ_ABORT;
wake_up(&req->uc_sleep);
retval = -EFAULT;
- goto out;
+ goto out_unlock;
}

/* adjust outsize. is this useful ?? */
req->uc_outSize = nbytes;
- req->uc_flags |= CODA_REQ_WRITE;
count = nbytes;

/* Convert filedescriptor into a file handle */
@@ -187,12 +191,17 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
(struct coda_open_by_fd_out *)req->uc_data;
if (!outp->oh.result) {
outp->fh = fget(outp->fd);
- if (!outp->fh)
- return -EBADF;
+ if (!outp->fh) {
+ count = 0;
+ retval = -EBADF;
+ }
}
}

+ req->uc_flags |= CODA_REQ_WRITE;
wake_up(&req->uc_sleep);
+out_unlock:
+ mutex_unlock(&vcp->vc_mutex);
out:
return(count ? count : retval);
}
--
2.53.0