Commit e46c823e authored by James Morris's avatar James Morris Committed by Linus Torvalds

[PATCH] SELinux: fix sidtab locking bug

This patch by Kaigai Kohei fixes a bug in the SELinux sidtab code, where we
do a spin_unlock_irq() while nested under another irq lock, which enables
interrupts and allows a deadlock to happen:

  sidtab_set() is called between POLICY_WRLOCK and POLICY_WRUNLOCK in
  services.c:1092.  sidtab_set() uses SIDTAB_LOCK()/SIDTAB_UNLOCK(), but
  SIDTAB_UNLOCK() enables any interruptions because it's defined as
  spin_unlock_irq().  If an interruption occurs between SIDTAB_UNLOCK() and
  POLICY_WRUNLOCK, and interruption context try to hold the POLICY_RDLOCK,
  then a deadlock happen in the result.

The solution is to save & restore flags on the inner lock, per the patch
below.
Signed-off-by: default avatarJames Morris <jmorris@redhat.com>
Signed-off-by: default avatarStephen Smalley <sds@epoch.ncsc.mil>
Signed-off-by: default avatarKaigai Kohei <kaigai@ak.jp.nec.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent de1750ca
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
(sid & SIDTAB_HASH_MASK) (sid & SIDTAB_HASH_MASK)
#define INIT_SIDTAB_LOCK(s) spin_lock_init(&s->lock) #define INIT_SIDTAB_LOCK(s) spin_lock_init(&s->lock)
#define SIDTAB_LOCK(s) spin_lock_irq(&s->lock) #define SIDTAB_LOCK(s, x) spin_lock_irqsave(&s->lock, x)
#define SIDTAB_UNLOCK(s) spin_unlock_irq(&s->lock) #define SIDTAB_UNLOCK(s, x) spin_unlock_irqrestore(&s->lock, x)
int sidtab_init(struct sidtab *s) int sidtab_init(struct sidtab *s)
{ {
...@@ -237,12 +237,13 @@ int sidtab_context_to_sid(struct sidtab *s, ...@@ -237,12 +237,13 @@ int sidtab_context_to_sid(struct sidtab *s,
{ {
u32 sid; u32 sid;
int ret = 0; int ret = 0;
unsigned long flags;
*out_sid = SECSID_NULL; *out_sid = SECSID_NULL;
sid = sidtab_search_context(s, context); sid = sidtab_search_context(s, context);
if (!sid) { if (!sid) {
SIDTAB_LOCK(s); SIDTAB_LOCK(s, flags);
/* Rescan now that we hold the lock. */ /* Rescan now that we hold the lock. */
sid = sidtab_search_context(s, context); sid = sidtab_search_context(s, context);
if (sid) if (sid)
...@@ -257,7 +258,7 @@ int sidtab_context_to_sid(struct sidtab *s, ...@@ -257,7 +258,7 @@ int sidtab_context_to_sid(struct sidtab *s,
if (ret) if (ret)
s->next_sid--; s->next_sid--;
unlock_out: unlock_out:
SIDTAB_UNLOCK(s); SIDTAB_UNLOCK(s, flags);
} }
if (ret) if (ret)
...@@ -320,17 +321,21 @@ void sidtab_destroy(struct sidtab *s) ...@@ -320,17 +321,21 @@ void sidtab_destroy(struct sidtab *s)
void sidtab_set(struct sidtab *dst, struct sidtab *src) void sidtab_set(struct sidtab *dst, struct sidtab *src)
{ {
SIDTAB_LOCK(src); unsigned long flags;
SIDTAB_LOCK(src, flags);
dst->htable = src->htable; dst->htable = src->htable;
dst->nel = src->nel; dst->nel = src->nel;
dst->next_sid = src->next_sid; dst->next_sid = src->next_sid;
dst->shutdown = 0; dst->shutdown = 0;
SIDTAB_UNLOCK(src); SIDTAB_UNLOCK(src, flags);
} }
void sidtab_shutdown(struct sidtab *s) void sidtab_shutdown(struct sidtab *s)
{ {
SIDTAB_LOCK(s); unsigned long flags;
SIDTAB_LOCK(s, flags);
s->shutdown = 1; s->shutdown = 1;
SIDTAB_UNLOCK(s); SIDTAB_UNLOCK(s, flags);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment