diff options
author | ken <ken@FreeBSD.org> | 2017-07-03 15:34:19 +0000 |
---|---|---|
committer | ken <ken@FreeBSD.org> | 2017-07-03 15:34:19 +0000 |
commit | b2481cc85f5487f19761c00e65d6bbf3283e5c46 (patch) | |
tree | b2fe99c292b8b8575936fc4ac2decb7b789f6c5e /sys/cam | |
parent | 4d2ca05966d9d2991c688985148435a5c8fa23c2 (diff) | |
download | FreeBSD-src-b2481cc85f5487f19761c00e65d6bbf3283e5c46.zip FreeBSD-src-b2481cc85f5487f19761c00e65d6bbf3283e5c46.tar.gz |
MFC r320421:
------------------------------------------------------------------------
r320421 | ken | 2017-06-27 13:26:02 -0600 (Tue, 27 Jun 2017) | 37 lines
Fix a panic in camperiphfree().
If a peripheral driver (e.g. da, sa, cd) is added or removed from the
peripheral driver list while an unrelated peripheral driver instance (e.g.
da0, sa5, cd2) is going away and is inside camperiphfree(), we could
dereference an invalid pointer.
When peripheral drivers are added or removed (see periphdriver_register()
and periphdriver_unregister()), the peripheral driver array is resized
and existing entries are moved.
Although we hold the topology lock while we traverse the peripheral driver
list, we retain a pointer to the location of the peripheral driver pointer
and then drop the topology lock. So we are still vulnerable to the list
getting moved around while the lock is dropped.
To solve the problem, cache a copy of the peripheral driver pointer. If
its storage location in the list changes while we have the lock dropped, it
won't have any effect.
This doesn't solve the issue that peripheral drivers ("da", "cd", as opposed
to individual instances like "da0", "cd0") are not generally part of a
reference counting scheme to guard against deregistering them while there
are instances active. The caller (generally the person unloading a module)
has to be aware of active drivers and not unload something that is in use.
sys/cam/cam_periph.c:
In camperiphfree(), cache a pointer to the peripheral driver
instance to avoid holding a pointer to an invalid memory location
in the event that the peripheral driver list changes while we have
the topology lock dropped.
PR: kern/219701
Submitted by: avg
Sponsored by: Spectra Logic
------------------------------------------------------------------------
PR: kern/219701
Sponsored by: Spectra Logic
Diffstat (limited to 'sys/cam')
-rw-r--r-- | sys/cam/cam_periph.c | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/sys/cam/cam_periph.c b/sys/cam/cam_periph.c index c540780..7f54139 100644 --- a/sys/cam/cam_periph.c +++ b/sys/cam/cam_periph.c @@ -622,6 +622,7 @@ static void camperiphfree(struct cam_periph *periph) { struct periph_driver **p_drv; + struct periph_driver *drv; cam_periph_assert(periph, MA_OWNED); KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating", @@ -634,6 +635,15 @@ camperiphfree(struct cam_periph *periph) printf("camperiphfree: attempt to free non-existant periph\n"); return; } + /* + * Cache a pointer to the periph_driver structure. If a + * periph_driver is added or removed from the array (see + * periphdriver_register()) while we drop the toplogy lock + * below, p_drv may change. This doesn't protect against this + * particular periph_driver going away. That will require full + * reference counting in the periph_driver infrastructure. + */ + drv = *p_drv; /* * We need to set this flag before dropping the topology lock, to @@ -669,8 +679,8 @@ camperiphfree(struct cam_periph *periph) */ xpt_lock_buses(); - TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links); - (*p_drv)->generation++; + TAILQ_REMOVE(&drv->units, periph, unit_links); + drv->generation++; xpt_remove_periph(periph); |