Commit Graph

341 Commits

Author SHA1 Message Date
rillig 6c259f3957 fix misspellings of 'available' and nearby typos 2022-09-10 12:14:17 +00:00
riastradh ff733a254d opencrypto(9): Fix missing initialization in error branch.
Reported-by: syzbot+8c519140cac567be1ee1@syzkaller.appspotmail.com
2022-06-26 22:52:30 +00:00
riastradh 41507b6a05 opencrypto: Assert session id is valid in crypto_freesession.
This gives us the opportunity to detect usage mistakes like
use-after-free.

Exception: Continue to silently ignore sid=0.
2022-05-22 11:40:54 +00:00
riastradh 76b7dd34d5 opencrypto: Prune dead code now that crypto_dispatch never fails. 2022-05-22 11:40:38 +00:00
riastradh 893f06d42d opencrypto: crypto_dispatch never fails now. Make it return void.
Same with crypto_kdispatch.
2022-05-22 11:40:29 +00:00
riastradh bcc6b1ebff opencrypto: Assert driver process routine returns 0 or ERESTART.
No other errors are allowed -- other errors must be transmitted by
crypto_done.  All drivers in tree (sun8i_crypto, glxsb, via_padlock,
mvcesa, mvxpsec, hifn, qat, ubsec, cryptosoft) have been audited for
this.
2022-05-22 11:40:15 +00:00
riastradh 3bbdee2433 opencrypto: Rip out EAGAIN logic when unregistering crypto drivers.
I'm pretty sure this never worked reliably based on code inspection,
and it's unlikely to have ever been tested because it only applies
when unregistering a driver -- but we have no crypto drivers for
removable devices, so it would only apply if we went out of our way
to trigger detach with drvctl.

Instead, just make the operation fail with ENODEV, and remove all the
callback logic to resubmit the request on EAGAIN.  (Maybe this should
be ENXIO, but crypto_kdispatch already does ENODEV.)
2022-05-22 11:40:03 +00:00
riastradh faea2b2f4e opencrypto: Assert nonnull callback up front in crypto_dispatch.
Same with crypto_kdispatch.

Convert some dead branches downstream to assertions too.
2022-05-22 11:39:54 +00:00
riastradh 33454e6ea1 crypto(4): Nix dead code now that crypto_freesession never fails. 2022-05-22 11:39:45 +00:00
riastradh a1f5e1f25c opencrypto: Make crypto_freesession return void.
No callers use the return value.  It is not sensible to allow this to
fail.
2022-05-22 11:39:37 +00:00
riastradh ee55792f15 opencrypto: Make freesession callback return void.
No functional change intended: all drivers already return zero
unconditionally.
2022-05-22 11:39:26 +00:00
riastradh 410da8f04c crypto(4): crypto_freesession should never fail here.
It can only fail if we pass it an invalid sid, which the logic to
maintain the user sessions should not do.  So kassert error=0 here.
2022-05-22 11:39:17 +00:00
riastradh 486d5a49d6 cryptosoft(4): Prune dead branches. Assert session id validity. 2022-05-22 11:38:59 +00:00
riastradh aafbfcd2c4 opencrypto: Assert crp_desc and crp_buf are nonnull.
- crypto_getreq ensures crp_desc is nonnull.
- Caller is responsible for setting crp_buf.
2022-05-22 11:34:40 +00:00
riastradh 979128a466 crypto(4): Refuse crypto operations with nothing in them earlier.
This way we avoid passing 0 to crypto_getreq -- makes it easier to
reason about everything downstream.
2022-05-22 11:34:29 +00:00
riastradh a56f0ba8bd opencrypto: Assert num>0 in crypto_getreq, num=1 in crypto_kgetreq.
- For crypto_getreq this makes downstream reasoning easier: on
  success, crp_desc is guaranteed to be nonnull.

- For crypto_kgetreq, this was already assumed, just silently
  ignored and not checked by anything.
2022-05-22 11:34:17 +00:00
riastradh 3ae8d479fa opencrypto: Make crp_callback, krp_callback return void.
Nothing uses the return values inside opencrypto, so let's stop
making users return them.
2022-05-22 11:30:40 +00:00
riastradh 661374afa3 opencrypto: Nix CRYPTO_F_DONE.
Nothing uses it any more.
2022-05-22 11:30:05 +00:00
riastradh cb99de1a82 crypto(4): Fix possible use-after-free in race around detach.
This is extremely unlikely because I don't think we have any drivers
for removable crypto decelerators^Waccelerators...but if we were to
sprout one, and someone ran crypto_dispatch concurrently with
crypto_unregister, cryptodev_cb/mcb would enter with crp->crp_etype =
EAGAIN and with CRYPTO_F_DONE set in crp->crp_flags.  In this case,
cryptodev_cb/mcb would issue crypto_dispatch but -- since nothing
clears CRYPTO_F_DONE -- it would _also_ consider the request done and
notify the ioctl thread of that.

With this change, we return early if crypto_dispatch succeeds.  No
need to consult CRYPTO_F_DONE: if the callback is invoked it's done,
and if we try to redispatch it on EAGAIN but crypto_dispatch fails,
it's done.  (Soon we'll get rid of the possibility of crypto_dispatch
failing synchronously, but not just yet.)

XXX This path could really use some testing!
2022-05-22 11:29:54 +00:00
riastradh 919316b75e cryptosoft(4): Rip out nonsense to quietly ignore sid=0.
This is no longer necessary because crypto_freesession no longer
calls into the driver for session ids that were never allocated in
the first place.
2022-05-22 11:29:25 +00:00
riastradh 479de1f7b7 opencrypto: Make sid=0 always invalid, but OK to free.
Previously, crypto_newsession could sometimes return 0 as the
driver-specific part of the session id, and 0 as the hid, for sid=0.
But netipsec assumes that it is always safe to free sid=0 from
zero-initialized memory even if crypto_newsession has never
succeeded.  So it was up to every driver in tree to gracefully handle
sid=0, if it happened to get assigned hid=0.  And, as long as the
freesession callback was expected to just return an error code when
given a bogus session id, that worked out fine...because nothing ever
used the error code.

That was a terrible fragile system that should never have been
invented.  Instead, let's just ensure that valid session ids are
nonzero, and make crypto_freesession with sid=0 be a no-op.
2022-05-22 11:25:14 +00:00
riastradh df8ebb18dd crypto(4): Fix set-but-unused variable warning.
This deliberately ignores the error code returned by crypto_dispatch,
but that error code is fundamentally incoherent and the issue will be
mooted by subsequent changes to make it return void and always pass
the error through the callback, as well as subsequent changes to rip
out the EAGAIN logic anyway.
2022-05-21 23:11:03 +00:00
riastradh 113b254adc crypto(4): Don't signal the condvar for multi-operation completion.
The condvar may be destroyed by the time we got here, and nothing
waits on it anyway -- instead the caller is expected to select/poll
for completion in userland.

The bug was already here, but the recent change to eliminate
CRYPTO_F_CBIMM made it happen more often by causing the callback to
_always_ be run asynchronously instead of sometimes being run
synchronously.
2022-05-21 20:37:18 +00:00
riastradh d31c8656a4 opencrypto: Assert !cpu_intr_p() on dispatch and invoke.
These should only ever have been potentially called from hard
interrupt context by CRYPTO_F_CBIMM callbacks (CBIMM = call back
immediately).  CRYPTO_F_CBIMM is no more, so there is no more need to
allow this case of call from hard interrupt context.
2022-05-19 20:51:59 +00:00
riastradh 99541d13d0 opencrypto: Nix CRYPTO_F_USER, CRYPTO_F_CBIMM, CRYPTO_F_CBIFSYNC.
CRYPTO_F_USER is no longer needed.  It was introduced in 2008 by
darran@ in crypto.c 1.30, cryptodev.c 1.45 in an attempt to avoid
double-free between the issuing thread and asynchronous callback.
But the `fix' didn't work.  In 2017, knakahara@ fixed it properly in
cryptodev.c 1.87 by distinguishing `the crypto operation has
completed' (CRYPTO_F_DONE) from `the callback is done touching the
crp object' (CRYPTO_F_DQRETQ, now renamed to CRYPTODEV_F_RET).

CRYPTO_F_CBIMM formerly served to invoke the callback synchronously
from the driver's interrupt completion routine, to reduce contention
on what was once a single cryptoret thread.  Now, there is a per-CPU
queue and softint for much cheaper processing, so there is less
motivation for this in the first place.  So let's remove the
complicated logic.  This means the callbacks never run in hard
interrupt context, which means we don't need to worry about recursion
into crypto_dispatch in hard interrupt context.
2022-05-19 20:51:46 +00:00
riastradh 44ead2179f crypto(4): Simplify error test in cryptodev_op.
No functional change intended.
2022-05-18 20:03:58 +00:00
riastradh c4fb4b18c2 crypto(4): Narrow scope of cryptodev_mtx to cover wait.
No functional change intended -- this only removes an unnecessary
lock/unlock cycle in the error case.
2022-05-18 20:03:45 +00:00
riastradh e5e4a95c25 crypto(4): Nix long-dead code and comments. 2022-05-18 20:03:32 +00:00
riastradh 5768c2c4a5 crypto(4): Use IPL_NONE, not IPL_NET, for /dev/crypto pools.
These are used (pool_get/put) only from thread context, never from
interrupt or even soft interrupt context.
2022-05-18 20:02:49 +00:00
riastradh 46bb8fc46d opencrypto: Factor setting CRYPTO_F_DONE out of branches.
This had been done in 1.30 when the locking was different.  No need
any more.  No functional change intended.
2022-05-17 10:32:58 +00:00
riastradh 1e4e1466aa opencrypto(9): Omit needless casts around callbacks.
Just declare the right types to begin with.  No functional change
intended.
2022-05-17 09:53:09 +00:00
pgoyette 97f8debd62 For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence.  This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

	devsw_attach()
	config_init_component() or config_cf*_attach()
	...
	config_fini_component() or config_cf*_detach()
	devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
	1. compile without build break
	2. no related test failures from atf
	3. modload/modunload work as well as
	   before.

No functional device testing done, since I don't have any
of these devices.  Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken;  see kern/56772.  This commit does not break
any additional modules (as far as I know).
2022-03-31 19:30:15 +00:00
riastradh b943dbddef crypto(4): Refuse count>1 for old CIOCNCRYPTM.
This hasn't worked since it was written in 2009; if anyone cared
surely they would have fixed it by now!

(Fixing this properly -- and putting a more reasonable upper bound
than the maximum that size_t arithmetic allows -- left as an exercise
or the reader.)

Reported-by: syzbot+798d4a16bc15ae88526e@syzkaller.appspotmail.com
2022-03-12 17:15:04 +00:00
andvar 53f067a3a8 fix typo in CRK_ALGORITHM_MIN definition to match CRK_ALGORITHM_MAX one.
while here fix few typos in comments.
2021-08-14 20:43:05 +00:00
andvar d7fca1ab3d fix typos in asymmetry, asymmetric(al), symmetrical. 2021-08-09 19:57:57 +00:00
knakahara 5b59f58d73 Fix ATF failures, sorry. 2021-04-06 03:38:04 +00:00
knakahara cfd5cbf47a refactor: reduce access to swcr_sessions[i] directly 2021-04-05 01:24:50 +00:00
knakahara 59a04e4a9e refactor: reduce changing swcr_sesnum 2021-04-05 01:23:15 +00:00
knakahara 3ca2c3f43f use kmem_{z,}alloc() instead of malloc() 2021-04-05 01:22:22 +00:00
riastradh 5da7826cde Fix kmem_free size in recent malloc->kmem conversion.
Should address this bracket report that has my name all over it:

https://mail-index.netbsd.org/current-users/2020/07/04/msg039059.html
2020-07-04 18:07:31 +00:00
riastradh 5766dd4aa9 Rename enc_xform_rijndael128 -> enc_xform_aes.
Update netipsec dependency.
2020-06-30 04:14:55 +00:00
riastradh 8835ffd082 opencrypto: Switch from legacy rijndael API to new aes API.
While here, apply various rijndael->aes renames, reduce the size
of aesxcbc_ctx by 480 bytes, and convert some malloc->kmem.

Leave in the symbol enc_xform_rijndael128 for now, though, so this
doesn't break any kernel ABI.
2020-06-29 23:34:48 +00:00
riastradh f69a480b24 swcrypto(4): Simplify iv generation logic with cprng_fast. 2020-06-14 23:23:55 +00:00
rin b203ba4088 Make crypto/rijindael optional again as cprng_strong does no longer
depend on it. Dependency is explicitly declared in files.foo if a
component requires it.
2020-04-22 09:15:39 +00:00
chs 328da78dac slightly change and fix the semantics of pool_set*wat(), pool_sethardlimit()
and pool_prime() (and their pool_cache_* counterparts):

 - the pool_set*wat() APIs are supposed to specify thresholds for the count of
   free items in the pool before pool pages are automatically allocated or freed
   during pool_get() / pool_put(), whereas pool_sethardlimit() and pool_prime()
   are supposed to specify minimum and maximum numbers of total items
   in the pool (both free and allocated).  these were somewhat conflated
   in the existing code, so separate them as they were intended.

 - change pool_prime() to take an absolute number of items to preallocate
   rather than an increment over whatever was done before, and wait for
   any memory allocations to succeed.  since pool_prime() can no longer fail
   after this, change its return value to void and adjust all callers.

 - pool_setlowat() is documented as not immediately attempting to allocate
   any memory, but it was changed some time ago to immediately try to allocate
   up to the lowat level, so just fix the manpage to describe the current
   behaviour.

 - add a pool_cache_prime() to complete the API set.
2020-04-13 00:27:16 +00:00
pgoyette 1352bf2652 Revert previous change to use SYSCTL_SETUP since it breaks on macppc.
For some reason, the crypto module fails to link, and this results in
opencrypto sysctl failures.

Should resolve PR kern/55154
2020-04-08 15:27:18 +00:00
pgoyette 9120d4511b Use the module subsystem's ability to process SYSCTL_SETUP() entries to
automate installation of sysctl nodes.

Note that there are still a number of device and pseudo-device modules
that create entries tied to individual device units, rather than to the
module itself.  These are not changed.
2020-03-16 21:20:09 +00:00
riastradh 26cde978c1 softint_disestablish does xc_barrier(0) for us already. 2020-02-01 13:48:08 +00:00
riastradh 3d6eb80076 Switch opencrypto to percpu_create.
Can't sleep for allocation in percpu_foreach.
2020-02-01 12:54:30 +00:00
pgoyette 9f10272659 Remove left-over #includes 2020-01-27 17:11:27 +00:00