From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
stable@vger.kernel.org, Al Viro <viro@zeniv.linux.org.uk>,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: [ 18/27] kobject: fix kset_find_obj() race with concurrent last kobject_put()
Date: Sun, 14 Apr 2013 19:43:17 -0700 [thread overview]
Message-ID: <20130415024232.716742509@linuxfoundation.org> (raw)
In-Reply-To: <20130415024231.351969241@linuxfoundation.org>
3.8-stable review patch. If anyone has any objections, please let me know.
------------------
From: Linus Torvalds <torvalds@linux-foundation.org>
commit a49b7e82cab0f9b41f483359be83f44fbb6b4979 upstream.
Anatol Pomozov identified a race condition that hits module unloading
and re-loading. To quote Anatol:
"This is a race codition that exists between kset_find_obj() and
kobject_put(). kset_find_obj() might return kobject that has refcount
equal to 0 if this kobject is freeing by kobject_put() in other
thread.
Here is timeline for the crash in case if kset_find_obj() searches for
an object tht nobody holds and other thread is doing kobject_put() on
the same kobject:
THREAD A (calls kset_find_obj()) THREAD B (calls kobject_put())
splin_lock()
atomic_dec_return(kobj->kref), counter gets zero here
... starts kobject cleanup ....
spin_lock() // WAIT thread A in kobj_kset_leave()
iterate over kset->list
atomic_inc(kobj->kref) (counter becomes 1)
spin_unlock()
spin_lock() // taken
// it does not know that thread A increased counter so it
remove obj from list
spin_unlock()
vfree(module) // frees module object with containing kobj
// kobj points to freed memory area!!
kobject_put(kobj) // OOPS!!!!
The race above happens because module.c tries to use kset_find_obj()
when somebody unloads module. The module.c code was introduced in
commit 6494a93d55fa"
Anatol supplied a patch specific for module.c that worked around the
problem by simply not using kset_find_obj() at all, but rather than make
a local band-aid, this just fixes kset_find_obj() to be thread-safe
using the proper model of refusing the get a new reference if the
refcount has already dropped to zero.
See examples of this proper refcount handling not only in the kref
documentation, but in various other equivalent uses of this pattern by
grepping for atomic_inc_not_zero().
[ Side note: the module race does indicate that module loading and
unloading is not properly serialized wrt sysfs information using the
module mutex. That may require further thought, but this is the
correct fix at the kobject layer regardless. ]
Reported-analyzed-and-tested-by: Anatol Pomozov <anatol.pomozov@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
lib/kobject.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -529,6 +529,13 @@ struct kobject *kobject_get(struct kobje
return kobj;
}
+static struct kobject *kobject_get_unless_zero(struct kobject *kobj)
+{
+ if (!kref_get_unless_zero(&kobj->kref))
+ kobj = NULL;
+ return kobj;
+}
+
/*
* kobject_cleanup - free kobject resources.
* @kobj: object to cleanup
@@ -751,7 +758,7 @@ struct kobject *kset_find_obj(struct kse
list_for_each_entry(k, &kset->list, entry) {
if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
- ret = kobject_get(k);
+ ret = kobject_get_unless_zero(k);
break;
}
}
next prev parent reply other threads:[~2013-04-15 2:43 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-15 2:42 [ 00/27] 3.8.8-stable review Greg Kroah-Hartman
2013-04-15 2:43 ` [ 01/27] ALSA: usb-audio: fix endianness bug in snd_nativeinstruments_* Greg Kroah-Hartman
2013-04-15 2:43 ` [ 02/27] ASoC: core: Fix to check return value of snd_soc_update_bits_locked() Greg Kroah-Hartman
2013-04-15 2:43 ` [ 03/27] ASoC: wm5102: Correct lookup of arizona struct in SYSCLK event Greg Kroah-Hartman
2013-04-15 2:43 ` [ 04/27] ASoC: wm8903: Fix the bypass to HP/LINEOUT when no DAC or ADC is running Greg Kroah-Hartman
2013-04-15 2:43 ` [ 05/27] tracing: Fix double free when function profile init failed Greg Kroah-Hartman
2013-04-15 2:43 ` [ 06/27] ARM: Kirkwood: Fix typo in the definition of ix2-200 rebuild LED Greg Kroah-Hartman
2013-04-15 2:43 ` [ 07/27] ARM: imx35 Bugfix admux clock Greg Kroah-Hartman
2013-04-15 2:43 ` [ 08/27] dmaengine: omap-dma: Start DMA without delay for cyclic channels Greg Kroah-Hartman
2013-04-15 2:43 ` [ 09/27] PM / reboot: call syscore_shutdown() after disable_nonboot_cpus() Greg Kroah-Hartman
2013-04-15 2:43 ` [ 10/27] Revert "brcmsmac: support 4313iPA" Greg Kroah-Hartman
2013-04-15 2:43 ` [ 11/27] ipc: set msg back to -EAGAIN if copy wasnt performed Greg Kroah-Hartman
2013-04-15 2:43 ` [ 12/27] GFS2: Fix unlock of fcntl locks during withdrawn state Greg Kroah-Hartman
2013-04-15 2:43 ` [ 13/27] GFS2: return error if malloc failed in gfs2_rs_alloc() Greg Kroah-Hartman
2013-04-15 2:43 ` [ 14/27] SCSI: libsas: fix handling vacant phy in sas_set_ex_phy() Greg Kroah-Hartman
2013-04-15 2:43 ` [ 15/27] cifs: Allow passwords which begin with a delimitor Greg Kroah-Hartman
2013-04-15 2:43 ` [ 16/27] target: Fix incorrect fallthrough of ALUA Standby/Offline/Transition CDBs Greg Kroah-Hartman
2013-04-15 2:43 ` [ 17/27] vfs: Revert spurious fix to spinning prevention in prune_icache_sb Greg Kroah-Hartman
2013-04-15 2:43 ` Greg Kroah-Hartman [this message]
2013-04-15 2:43 ` [ 19/27] gpio: fix wrong checking condition for gpio range Greg Kroah-Hartman
2013-04-15 2:43 ` [ 20/27] x86-32: Fix possible incomplete TLB invalidate with PAE pagetables Greg Kroah-Hartman
2013-04-15 2:43 ` [ 21/27] tracing: Fix possible NULL pointer dereferences Greg Kroah-Hartman
2013-04-15 2:43 ` [ 22/27] udl: handle EDID failure properly Greg Kroah-Hartman
2013-04-15 2:43 ` [ 23/27] ftrace: Move ftrace_filter_lseek out of CONFIG_DYNAMIC_FTRACE section Greg Kroah-Hartman
2013-04-15 2:43 ` [ 24/27] sched_clock: Prevent 64bit inatomicity on 32bit systems Greg Kroah-Hartman
2013-04-15 2:43 ` [ 25/27] x86, mm, paravirt: Fix vmalloc_fault oops during lazy MMU updates Greg Kroah-Hartman
2013-04-15 2:43 ` [ 26/27] x86, mm: Patch out arch_flush_lazy_mmu_mode() when running on bare metal Greg Kroah-Hartman
2013-04-15 2:43 ` [ 27/27] tty: dont deadlock while flushing workqueue Greg Kroah-Hartman
2013-04-15 19:37 ` Peter Hurley
2013-04-15 19:50 ` Greg Kroah-Hartman
2013-04-15 14:05 ` [ 00/27] 3.8.8-stable review Shuah Khan
2013-04-15 16:07 ` Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130415024232.716742509@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).