Hi,

The crypto framework sometimes returns an error, sometimes the
callback is called, and sometimes both.  So the caller cannot release
resources correctly.

A bunch of errors can or should not happen, replace them with an
assert.  Remove redundant checks.  crypto_invoke() should not return
the error, but pass it via callback.

Some old hardware drivers keep part of their inconsistency as I
cannot test them.

ok?

bluhm

Index: arch/amd64/amd64/aesni.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/arch/amd64/amd64/aesni.c,v
retrieving revision 1.50
diff -u -p -r1.50 aesni.c
--- arch/amd64/amd64/aesni.c    8 Jul 2021 09:22:30 -0000       1.50
+++ arch/amd64/amd64/aesni.c    5 Oct 2021 22:14:25 -0000
@@ -639,10 +639,7 @@ aesni_process(struct cryptop *crp)
        int err = 0;
        int i;
 
-       if (crp == NULL || crp->crp_callback == NULL)
-               return (EINVAL);
-       if (crp->crp_ndesc < 1)
-               return (EINVAL);
+       KASSERT(crp->crp_ndesc >= 1);
 
        smr_read_enter();
        ses = aesni_get(crp->crp_sid & 0xffffffff);
Index: arch/amd64/amd64/via.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/arch/amd64/amd64/via.c,v
retrieving revision 1.34
diff -u -p -r1.34 via.c
--- arch/amd64/amd64/via.c      8 Jul 2021 09:22:30 -0000       1.34
+++ arch/amd64/amd64/via.c      5 Oct 2021 22:14:49 -0000
@@ -420,10 +420,7 @@ viac3_crypto_process(struct cryptop *crp
        int sesn, err = 0;
        int i;
 
-       if (crp == NULL || crp->crp_callback == NULL)
-               return (EINVAL);
-       if (crp->crp_ndesc < 1)
-               return (EINVAL);
+       KASSERT(crp->crp_ndesc >= 1);
 
        sesn = VIAC3_SESSION(crp->crp_sid);
        if (sesn >= sc->sc_nsessions) {
Index: arch/arm64/arm64/cryptox.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/arch/arm64/arm64/cryptox.c,v
retrieving revision 1.2
diff -u -p -r1.2 cryptox.c
--- arch/arm64/arm64/cryptox.c  8 Jul 2021 09:22:30 -0000       1.2
+++ arch/arm64/arm64/cryptox.c  5 Oct 2021 22:33:42 -0000
@@ -447,10 +447,7 @@ cryptox_process(struct cryptop *crp)
        int err = 0;
        int i;
 
-       if (crp == NULL || crp->crp_callback == NULL)
-               return (EINVAL);
-       if (crp->crp_ndesc < 1)
-               return (EINVAL);
+       KASSERT(crp->crp_ndesc >= 1);
 
        smr_read_enter();
        ses = cryptox_get(crp->crp_sid & 0xffffffff);
Index: arch/i386/i386/via.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/arch/i386/i386/via.c,v
retrieving revision 1.47
diff -u -p -r1.47 via.c
--- arch/i386/i386/via.c        8 Jul 2021 09:22:30 -0000       1.47
+++ arch/i386/i386/via.c        5 Oct 2021 22:17:36 -0000
@@ -428,10 +428,7 @@ viac3_crypto_process(struct cryptop *crp
        int sesn, err = 0;
        int i;
 
-       if (crp == NULL || crp->crp_callback == NULL)
-               return (EINVAL);
-       if (crp->crp_ndesc < 1)
-               return (EINVAL);
+       KASSERT(crp->crp_ndesc >= 1);
 
        sesn = VIAC3_SESSION(crp->crp_sid);
        if (sesn >= sc->sc_nsessions) {
Index: arch/i386/pci/glxsb.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/arch/i386/pci/glxsb.c,v
retrieving revision 1.37
diff -u -p -r1.37 glxsb.c
--- arch/i386/pci/glxsb.c       8 Jul 2021 09:22:30 -0000       1.37
+++ arch/i386/pci/glxsb.c       5 Oct 2021 22:19:20 -0000
@@ -780,14 +780,7 @@ glxsb_crypto_process(struct cryptop *crp
 
        s = splnet();
 
-       if (crp == NULL || crp->crp_callback == NULL) {
-               err = EINVAL;
-               goto out;
-       }
-       if (crp->crp_ndesc < 1) {
-               err = EINVAL;
-               goto out;
-       }
+       KASSERT(crp->crp_ndesc >= 1);
 
        sesn = GLXSB_SESSION(crp->crp_sid);
        if (sesn >= sc->sc_nsessions) {
Index: arch/octeon/dev/octcrypto.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/arch/octeon/dev/octcrypto.c,v
retrieving revision 1.5
diff -u -p -r1.5 octcrypto.c
--- arch/octeon/dev/octcrypto.c 8 Jul 2021 09:22:30 -0000       1.5
+++ arch/octeon/dev/octcrypto.c 5 Oct 2021 22:32:47 -0000
@@ -597,9 +597,6 @@ octcrypto_process(struct cryptop *crp)
        int error = 0;
        int i;
 
-       if (crp == NULL || crp->crp_callback == NULL)
-               return EINVAL;
-
        KASSERT(crp->crp_ndesc >= 1);
 
        smr_read_enter();
Index: dev/pci/hifn7751.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/dev/pci/hifn7751.c,v
retrieving revision 1.180
diff -u -p -r1.180 hifn7751.c
--- dev/pci/hifn7751.c  29 May 2020 04:42:25 -0000      1.180
+++ dev/pci/hifn7751.c  5 Oct 2021 22:20:50 -0000
@@ -1925,11 +1925,6 @@ hifn_process(struct cryptop *crp)
        struct hifn_softc *sc;
        struct cryptodesc *crd1, *crd2 = NULL, *maccrd, *enccrd;
 
-       if (crp == NULL || crp->crp_callback == NULL) {
-               hifnstats.hst_invalid++;
-               return (EINVAL);
-       }
-
        if (crp->crp_ilen == 0) {
                err = EINVAL;
                goto errout;
Index: dev/pci/safe.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/dev/pci/safe.c,v
retrieving revision 1.45
diff -u -p -r1.45 safe.c
--- dev/pci/safe.c      25 Feb 2021 02:48:20 -0000      1.45
+++ dev/pci/safe.c      5 Oct 2021 22:22:58 -0000
@@ -310,11 +310,7 @@ safe_process(struct cryptop *crp)
        u_int32_t cmd0, cmd1, staterec, iv[4];
 
        s = splnet();
-       if (crp == NULL || crp->crp_callback == NULL) {
-               safestats.st_invalid++;
-               splx(s);
-               return (EINVAL);
-       }
+
        card = SAFE_CARD(crp->crp_sid);
        if (card >= safe_cd.cd_ndevs || safe_cd.cd_devs[card] == NULL) {
                safestats.st_invalid++;
Index: dev/pci/ubsec.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/dev/pci/ubsec.c,v
retrieving revision 1.167
diff -u -p -r1.167 ubsec.c
--- dev/pci/ubsec.c     25 Feb 2021 02:48:20 -0000      1.167
+++ dev/pci/ubsec.c     5 Oct 2021 22:24:13 -0000
@@ -781,10 +781,6 @@ ubsec_process(struct cryptop *crp)
        u_int16_t flags = 0;
        int ivlen = 0, keylen = 0;
 
-       if (crp == NULL || crp->crp_callback == NULL) {
-               ubsecstats.hst_invalid++;
-               return (EINVAL);
-       }
        card = UBSEC_CARD(crp->crp_sid);
        if (card >= ubsec_cd.cd_ndevs || ubsec_cd.cd_devs[card] == NULL) {
                ubsecstats.hst_invalid++;
Index: crypto/crypto.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/crypto/crypto.c,v
retrieving revision 1.85
diff -u -p -r1.85 crypto.c
--- crypto/crypto.c     26 Jul 2021 21:27:56 -0000      1.85
+++ crypto/crypto.c     5 Oct 2021 22:07:26 -0000
@@ -404,7 +404,7 @@ crypto_dispatch(struct cryptop *crp)
        if (crp->crp_flags & CRYPTO_F_NOQUEUE) {
                if (lock)
                        KERNEL_LOCK();
-               error = crypto_invoke(crp);
+               crypto_invoke(crp);
                if (lock)
                        KERNEL_UNLOCK();
        } else {
@@ -421,7 +421,7 @@ crypto_dispatch(struct cryptop *crp)
 /*
  * Dispatch a crypto request to the appropriate crypto devices.
  */
-int
+void
 crypto_invoke(struct cryptop *crp)
 {
        u_int64_t nid;
@@ -430,17 +430,15 @@ crypto_invoke(struct cryptop *crp)
        int s, i;
 
        /* Sanity checks. */
-       if (crp == NULL || crp->crp_callback == NULL)
-               return EINVAL;
+       KASSERT(crp != NULL);
+       KASSERT(crp->crp_callback != NULL);
 
        KERNEL_ASSERT_LOCKED();
 
        s = splvm();
        if (crp->crp_ndesc < 1 || crypto_drivers == NULL) {
                crp->crp_etype = EINVAL;
-               crypto_done(crp);
-               splx(s);
-               return 0;
+               goto done;
        }
 
        hid = (crp->crp_sid >> 32) & 0xffffffff;
@@ -470,7 +468,7 @@ crypto_invoke(struct cryptop *crp)
        }
 
        splx(s);
-       return 0;
+       return;
 
  migrate:
        /* Migrate session. */
@@ -482,9 +480,9 @@ crypto_invoke(struct cryptop *crp)
                crp->crp_sid = nid;
 
        crp->crp_etype = EAGAIN;
+ done:
        crypto_done(crp);
        splx(s);
-       return 0;
 }
 
 /*
Index: crypto/cryptodev.h
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/crypto/cryptodev.h,v
retrieving revision 1.74
diff -u -p -r1.74 cryptodev.h
--- crypto/cryptodev.h  26 Jul 2021 21:27:56 -0000      1.74
+++ crypto/cryptodev.h  5 Oct 2021 22:04:49 -0000
@@ -224,7 +224,7 @@ int crypto_register(u_int32_t, int *,
            int (*)(struct cryptop *));
 int    crypto_unregister(u_int32_t, int);
 int32_t        crypto_get_driverid(u_int8_t);
-int    crypto_invoke(struct cryptop *);
+void   crypto_invoke(struct cryptop *);
 void   crypto_done(struct cryptop *);
 
 void   cuio_copydata(struct uio *, int, int, caddr_t);
Index: crypto/cryptosoft.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/crypto/cryptosoft.c,v
retrieving revision 1.88
diff -u -p -r1.88 cryptosoft.c
--- crypto/cryptosoft.c 9 Jul 2021 15:29:55 -0000       1.88
+++ crypto/cryptosoft.c 5 Oct 2021 22:17:05 -0000
@@ -1035,11 +1035,9 @@ swcr_process(struct cryptop *crp)
        int type;
        int i;
 
-       /* Sanity check */
-       if (crp == NULL)
-               return EINVAL;
+       KASSERT(crp->crp_ndesc >= 1);
 
-       if (crp->crp_ndesc < 1 || crp->crp_buf == NULL) {
+       if (crp->crp_buf == NULL) {
                crp->crp_etype = EINVAL;
                goto done;
        }

Reply via email to