This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 0b0a7089e4e4090032dcb3c7ce061c8b81dccc97
Author: makejian <[email protected]>
AuthorDate: Thu Nov 19 21:04:56 2026 +0800

    crypto/cryptodev: protect the async descriptor and add cancellation
    
    An asynchronous completion callback can run in interrupt context while
    cryptof_close() runs in syscall context.  If the descriptor is freed
    while a request is still in flight, the callback dereferences freed
    memory.  Give struct fcrypt a reference count: the descriptor holds one
    reference and every in-flight request holds one more, so the memory is
    released only after the last completion callback has run.
    
    A per-descriptor spinlock taken with spin_lock_irqsave() protects the
    completion queues and the poll waiter against the interrupt-context
    callbacks.  cryptof_close() only detaches the poll waiter and drops the
    descriptor's reference; the last fcrypt_put() drains whatever is left.
    
    Add COP_FLAG_CANCEL so userspace can cancel a pending asynchronous
    operation by reqid through CIOCCRYPT.  Every asynchronous operation is
    tracked on a per-descriptor in-flight queue from submission until its
    completion callback dequeues it.  Cancelling an in-flight operation
    flags it, so the callback discards the result when it arrives; the
    backend still owns the request, so it cannot be freed early.  The
    cancellation is also forwarded to the backend through crypto_invoke(),
    so a backend with its own request queue can drop a still-queued
    operation before it consumes hardware.  Cancelling an operation whose
    result already reached the completion queue removes it, and an unknown
    reqid returns -ENOENT.
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/cryptodev.c         | 271 ++++++++++++++++++++++++++++++++++++++-------
 include/crypto/cryptodev.h |   1 +
 2 files changed, 231 insertions(+), 41 deletions(-)

diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c
index 39a8051466f..8807e2d9997 100644
--- a/crypto/cryptodev.c
+++ b/crypto/cryptodev.c
@@ -43,10 +43,12 @@
 #include <poll.h>
 #include <errno.h>
 
+#include <nuttx/atomic.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/crypto/crypto.h>
 #include <nuttx/drivers/drivers.h>
+#include <nuttx/spinlock.h>
 
 #include <crypto/xform.h>
 #include <crypto/cryptodev.h>
@@ -95,8 +97,11 @@ struct csession
 struct fcrypt
 {
   TAILQ_HEAD(csessionlist, csession) csessions;
+  TAILQ_HEAD(cryptopinflight, cryptop) crp_inflight;
   TAILQ_HEAD(cryptoplist, cryptop) crp_ret;
   TAILQ_HEAD(cryptkoplist, cryptkop) crpk_ret;
+  atomic_t refcount;
+  spinlock_t lock;
   int sesn;
   FAR struct pollfd_s *fds;
 };
@@ -139,6 +144,7 @@ static int csefree(FAR struct csession *);
 static int cryptodev_op(FAR struct fcrypt *,
                         FAR struct csession *,
                         FAR struct crypt_op *);
+static int cryptodev_cancel(FAR struct fcrypt *, uint32_t);
 static int cryptodevop_cb(FAR struct cryptop *);
 static int cryptodev_getopstatus(FAR struct fcrypt *,
                                  FAR struct crypt_op *);
@@ -190,6 +196,49 @@ static struct inode g_cryptoinode =
  * Private Functions
  ****************************************************************************/
 
+static FAR struct fcrypt *fcrypt_get(FAR struct fcrypt *fcr)
+{
+  atomic_add(&fcr->refcount, 1);
+  return fcr;
+}
+
+/* Drop a reference.  The descriptor holds one reference and every
+ * in-flight asynchronous request holds one more, so the last drop is the
+ * only context that can still reach the queues.
+ */
+
+static void fcrypt_put(FAR struct fcrypt *fcr)
+{
+  if (atomic_sub(&fcr->refcount, 1) != 1)
+    {
+      return;
+    }
+
+  fcrypt_free(fcr);
+}
+
+/* Forward a cancellation to the backend that owns the session, so that a
+ * backend with its own request queue can drop a still-queued operation
+ * before it consumes hardware.  The in-flight request itself is flagged
+ * separately, so a backend without a queue is not required to act on
+ * this.
+ */
+
+static void cryptodev_cancel_dispatch(uint64_t sid, uint32_t reqid)
+{
+  struct cryptop crp;
+  struct cryptodesc crd;
+
+  memset(&crp, 0, sizeof(crp));
+  memset(&crd, 0, sizeof(crd));
+
+  crp.crp_sid = sid;
+  crp.crp_reqid = reqid;
+  crp.crp_flags = CRYPTO_F_CANCEL;
+  crp.crp_desc = &crd;
+  crypto_invoke(&crp);
+}
+
 /* ARGSUSED */
 
 static ssize_t cryptof_read(FAR struct file *filep,
@@ -386,6 +435,12 @@ bail:
         break;
       case CIOCCRYPT:
         cop = (FAR struct crypt_op *)arg;
+        if (cop->flags & COP_FLAG_CANCEL)
+          {
+            error = cryptodev_cancel(fcr, cop->reqid);
+            break;
+          }
+
         cse = csefind(fcr, cop->ses);
         if (cse == NULL)
           {
@@ -441,6 +496,7 @@ static int cryptodev_op(FAR struct fcrypt *fcr,
   FAR struct cryptop *crp;
   FAR struct cryptodesc *crda;
   FAR struct cryptodesc *crde;
+  irqstate_t iflags;
   int error = OK;
   uint32_t hid;
 
@@ -572,12 +628,13 @@ static int cryptodev_op(FAR struct fcrypt *fcr,
     {
       /* The result is written to the embedded buffer and copied back on
        * CIOCCRYPTRET, so the caller's buffers may go away once the
-       * request is submitted.
+       * request is submitted.  The request holds a reference to the
+       * descriptor until its completion callback has run.
        */
 
       scrp->crp_reqid    = cop->reqid;
       scrp->crp_flags    = CRYPTO_F_CBIMM;
-      scrp->crp_opaque   = fcr;
+      scrp->crp_opaque   = fcrypt_get(fcr);
       scrp->crp_callback = cryptodevop_cb;
 
       if (cop->dst)
@@ -588,6 +645,10 @@ static int cryptodev_op(FAR struct fcrypt *fcr,
         {
           scrp->crp_mac = (caddr_t)(crde + 1);
         }
+
+      iflags = spin_lock_irqsave(&fcr->lock);
+      TAILQ_INSERT_TAIL(&fcr->crp_inflight, scrp, crp_next);
+      spin_unlock_irqrestore(&fcr->lock, iflags);
     }
 
   /* try the fast path first */
@@ -628,51 +689,134 @@ static int cryptodev_op(FAR struct fcrypt *fcr,
     {
       /* Submission failed: the callback will not run, so clean up here. */
 
+      iflags = spin_lock_irqsave(&fcr->lock);
+      TAILQ_REMOVE(&fcr->crp_inflight, scrp, crp_next);
+      spin_unlock_irqrestore(&fcr->lock, iflags);
+
+      fcrypt_put(fcr);
       kmm_free(scrp);
     }
 
   return error;
 }
 
+/****************************************************************************
+ * Name: cryptodev_cancel
+ *
+ * Description:
+ *   Cancel a pending asynchronous operation identified by reqid.
+ *
+ *   - If the op is still in-flight, flag it so its completion callback
+ *     discards the result when it arrives (the backend still owns the
+ *     cryptop, so it cannot be freed here).
+ *   - If the result already reached the completion queue, remove and free.
+ *   - Otherwise there is no such op: return -ENOENT.
+ *
+ ****************************************************************************/
+
+static int cryptodev_cancel(FAR struct fcrypt *fcr, uint32_t reqid)
+{
+  FAR struct cryptop *crp;
+  irqstate_t flags;
+  uint64_t sid;
+
+  flags = spin_lock_irqsave(&fcr->lock);
+
+  /* Still in-flight: flag it; the completion callback drops the result. */
+
+  TAILQ_FOREACH(crp, &fcr->crp_inflight, crp_next)
+    {
+      if (crp->crp_reqid == reqid)
+        {
+          sid = crp->crp_sid;
+          crp->crp_flags |= CRYPTO_F_CANCEL;
+          spin_unlock_irqrestore(&fcr->lock, flags);
+
+          cryptodev_cancel_dispatch(sid, reqid);
+          return OK;
+        }
+    }
+
+  /* Already completed: drop the result before it is retrieved. */
+
+  TAILQ_FOREACH(crp, &fcr->crp_ret, crp_next)
+    {
+      if (crp->crp_reqid == reqid)
+        {
+          TAILQ_REMOVE(&fcr->crp_ret, crp, crp_next);
+          spin_unlock_irqrestore(&fcr->lock, flags);
+          kmm_free(crp);
+          return OK;
+        }
+    }
+
+  spin_unlock_irqrestore(&fcr->lock, flags);
+  return -ENOENT;
+}
+
 static int cryptodevop_cb(FAR struct cryptop *crp)
 {
   FAR struct fcrypt *fcr = crp->crp_opaque;
+  FAR struct pollfd_s *fds;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&fcr->lock);
+  TAILQ_REMOVE(&fcr->crp_inflight, crp, crp_next);
+
+  /* Discard the result if a concurrent cancel flagged this op. */
+
+  if (crp->crp_flags & CRYPTO_F_CANCEL)
+    {
+      spin_unlock_irqrestore(&fcr->lock, flags);
+      kmm_free(crp);
+      fcrypt_put(fcr);
+      return OK;
+    }
 
   TAILQ_INSERT_TAIL(&fcr->crp_ret, crp, crp_next);
-  if (fcr->fds != NULL)
+  fds = fcr->fds;
+  spin_unlock_irqrestore(&fcr->lock, flags);
+
+  if (fds != NULL)
     {
-      poll_notify(&fcr->fds, 1, POLLIN);
+      poll_notify(&fds, 1, POLLIN);
     }
 
+  fcrypt_put(fcr);
   return OK;
 }
 
-static int cryptodev_getopstatus(FAR struct fcrypt *fcr,
-                                 FAR struct crypt_op *cop)
+static FAR struct cryptop *
+fcrypt_dequeue_op(FAR struct fcrypt *fcr, uint32_t reqid)
 {
-  FAR struct cryptop *crp = NULL;
-  int error;
-
-  if (TAILQ_EMPTY(&fcr->crp_ret))
-    {
-      return -EAGAIN;
-    }
+  FAR struct cryptop *crp;
+  irqstate_t flags;
 
+  flags = spin_lock_irqsave(&fcr->lock);
   TAILQ_FOREACH(crp, &fcr->crp_ret, crp_next)
     {
-      if (crp->crp_reqid == cop->reqid)
+      if (crp->crp_reqid == reqid)
         {
+          TAILQ_REMOVE(&fcr->crp_ret, crp, crp_next);
           break;
         }
     }
 
+  spin_unlock_irqrestore(&fcr->lock, flags);
+  return crp;
+}
+
+static int cryptodev_getopstatus(FAR struct fcrypt *fcr,
+                                 FAR struct crypt_op *cop)
+{
+  FAR struct cryptop *crp = fcrypt_dequeue_op(fcr, cop->reqid);
+  int error;
+
   if (crp == NULL)
     {
-      return -EINVAL;
+      return -EAGAIN;
     }
 
-  TAILQ_REMOVE(&fcr->crp_ret, crp, crp_next);
-
   if (cop->dst && crp->crp_dst)
     {
       memcpy(cop->dst, crp->crp_dst, crp->crp_olen);
@@ -986,40 +1130,54 @@ fail:
 
 static int cryptodevkey_cb(FAR struct cryptkop *krp)
 {
-  TAILQ_INSERT_TAIL(&krp->krp_fcr->crpk_ret, krp, krp_next);
-  if (krp->krp_fcr->fds != NULL)
+  FAR struct fcrypt *fcr = krp->krp_fcr;
+  FAR struct pollfd_s *fds;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&fcr->lock);
+  TAILQ_INSERT_TAIL(&fcr->crpk_ret, krp, krp_next);
+  fds = fcr->fds;
+  spin_unlock_irqrestore(&fcr->lock, flags);
+
+  if (fds != NULL)
     {
-      poll_notify(&krp->krp_fcr->fds, 1, POLLIN);
+      poll_notify(&fds, 1, POLLIN);
     }
 
   return OK;
 }
 
-static int cryptodev_getkeystatus(struct fcrypt *fcr, struct crypt_kop *ret)
+static FAR struct cryptkop *
+fcrypt_dequeue_key(FAR struct fcrypt *fcr, uint32_t reqid)
 {
-  FAR struct cryptkop *krp = NULL;
-  int i;
-  int size;
-
-  if (TAILQ_EMPTY(&fcr->crpk_ret))
-    {
-      return -EAGAIN;
-    }
+  FAR struct cryptkop *krp;
+  irqstate_t flags;
 
+  flags = spin_lock_irqsave(&fcr->lock);
   TAILQ_FOREACH(krp, &fcr->crpk_ret, krp_next)
     {
-      if (krp->krp_reqid == ret->crk_reqid)
+      if (krp->krp_reqid == reqid)
         {
+          TAILQ_REMOVE(&fcr->crpk_ret, krp, krp_next);
           break;
         }
     }
 
+  spin_unlock_irqrestore(&fcr->lock, flags);
+  return krp;
+}
+
+static int cryptodev_getkeystatus(struct fcrypt *fcr, struct crypt_kop *ret)
+{
+  FAR struct cryptkop *krp = fcrypt_dequeue_key(fcr, ret->crk_reqid);
+  int i;
+  int size;
+
   if (krp == NULL)
     {
-      return -EINVAL;
+      return -EAGAIN;
     }
 
-  TAILQ_REMOVE(&fcr->crpk_ret, krp, krp_next);
   ret->crk_op = krp->krp_op;
   ret->crk_status = krp->krp_status;
   ret->crk_iparams = krp->krp_iparams;
@@ -1084,8 +1242,11 @@ static int cryptodev_clone(FAR struct fcrypt *src, FAR 
struct fcrypt **dst)
     }
 
   TAILQ_INIT(&fcrd->csessions);
+  TAILQ_INIT(&fcrd->crp_inflight);
   TAILQ_INIT(&fcrd->crp_ret);
   TAILQ_INIT(&fcrd->crpk_ret);
+  atomic_set(&fcrd->refcount, 1);
+  spin_lock_init(&fcrd->lock);
 
   TAILQ_FOREACH(cse, &src->csessions, next)
     {
@@ -1172,42 +1333,64 @@ static int cryptof_poll(FAR struct file *filep,
                         FAR struct pollfd_s *fds, bool setup)
 {
   FAR struct fcrypt *fcr = filep->f_priv;
+  bool ready = false;
+  irqstate_t flags;
+  int ret = OK;
 
   if (fcr == NULL || fds == NULL)
     {
       return -EINVAL;
     }
 
+  flags = spin_lock_irqsave(&fcr->lock);
   if (setup)
     {
       if (!TAILQ_EMPTY(&fcr->crp_ret) ||
           !TAILQ_EMPTY(&fcr->crpk_ret))
         {
-          poll_notify(&fds, 1, POLLIN);
-          return OK;
+          ready = true;
         }
-
-      if (fcr->fds)
+      else if (fcr->fds)
         {
-          return -EBUSY;
+          ret = -EBUSY;
+        }
+      else
+        {
+          fcr->fds = fds;
         }
-
-      fcr->fds = fds;
     }
   else
     {
       fcr->fds = NULL;
     }
 
-  return OK;
+  spin_unlock_irqrestore(&fcr->lock, flags);
+
+  if (ready)
+    {
+      poll_notify(&fds, 1, POLLIN);
+    }
+
+  return ret;
 }
 
 /* ARGSUSED */
 
 static int cryptof_close(FAR struct file *filep)
 {
-  fcrypt_free(filep->f_priv);
+  FAR struct fcrypt *fcr = filep->f_priv;
+  irqstate_t flags;
+
+  /* Detach the poll waiter; in-flight requests still hold references,
+   * so the descriptor is freed once their callbacks have run.
+   */
+
+  flags = spin_lock_irqsave(&fcr->lock);
+  fcr->fds = NULL;
+  spin_unlock_irqrestore(&fcr->lock, flags);
+
   filep->f_priv = NULL;
+  fcrypt_put(fcr);
   return 0;
 }
 
@@ -1248,7 +1431,10 @@ static int cryptof_open(FAR struct file *filep)
     }
 
   TAILQ_INIT(&fcrd->csessions);
+  TAILQ_INIT(&fcrd->crp_inflight);
   TAILQ_INIT(&fcrd->crp_ret);
+  atomic_set(&fcrd->refcount, 1);
+  spin_lock_init(&fcrd->lock);
   TAILQ_FOREACH(cse, &fcr->csessions, next)
     {
       bzero(&crie, sizeof(crie));
@@ -1375,8 +1561,11 @@ static int cryptoioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
           }
 
         TAILQ_INIT(&fcr->csessions);
+        TAILQ_INIT(&fcr->crp_inflight);
         TAILQ_INIT(&fcr->crp_ret);
         TAILQ_INIT(&fcr->crpk_ret);
+        atomic_set(&fcr->refcount, 1);
+        spin_lock_init(&fcr->lock);
 
         fd = file_allocate_from_inode(&g_cryptoinode, 0, 0, fcr, 0);
         if (fd < 0)
diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h
index d880cb0c495..ecdc2f89982 100644
--- a/include/crypto/cryptodev.h
+++ b/include/crypto/cryptodev.h
@@ -461,6 +461,7 @@ struct crypt_op
  */
 
 #define COP_FLAG_CBIMM  (1 << 2) /* Request asynchronous processing. */
+#define COP_FLAG_CANCEL (1 << 4) /* Cancel a pending async op by reqid. */
 
   uint16_t flags;
   uint32_t iterations;

Reply via email to