Commit 1e327963 authored by Jakob Koschel's avatar Jakob Koschel Committed by Borislav Petkov (AMD)

x86/sgx: Avoid using iterator after loop in sgx_mmu_notifier_release()

If &encl_mm->encl->mm_list does not contain the searched 'encl_mm',
'tmp' will not point to a valid sgx_encl_mm struct.

Linus proposed to avoid any use of the list iterator variable after the
loop, in the attempt to move the list iterator variable declaration into
the macro to avoid any potential misuse after the loop. Using it in
a pointer comparison after the loop is undefined behavior and should be
omitted if possible, see Link tag.

Instead, just use a 'found' boolean to indicate if an element was found.

  [ bp: Massage, fix typos. ]
Signed-off-by: default avatarJakob Koschel <jkl820.git@gmail.com>
Signed-off-by: default avatarBorislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: default avatarJarkko Sakkinen <jarkko@kernel.org>
Acked-by: default avatarDave Hansen <dave.hansen@linux.intel.com>
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/
Link: https://lore.kernel.org/r/20230206-sgx-use-after-iter-v2-1-736ca621adc3@gmail.com
parent 858fd168
...@@ -755,6 +755,7 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn, ...@@ -755,6 +755,7 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn,
{ {
struct sgx_encl_mm *encl_mm = container_of(mn, struct sgx_encl_mm, mmu_notifier); struct sgx_encl_mm *encl_mm = container_of(mn, struct sgx_encl_mm, mmu_notifier);
struct sgx_encl_mm *tmp = NULL; struct sgx_encl_mm *tmp = NULL;
bool found = false;
/* /*
* The enclave itself can remove encl_mm. Note, objects can't be moved * The enclave itself can remove encl_mm. Note, objects can't be moved
...@@ -764,12 +765,13 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn, ...@@ -764,12 +765,13 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn,
list_for_each_entry(tmp, &encl_mm->encl->mm_list, list) { list_for_each_entry(tmp, &encl_mm->encl->mm_list, list) {
if (tmp == encl_mm) { if (tmp == encl_mm) {
list_del_rcu(&encl_mm->list); list_del_rcu(&encl_mm->list);
found = true;
break; break;
} }
} }
spin_unlock(&encl_mm->encl->mm_lock); spin_unlock(&encl_mm->encl->mm_lock);
if (tmp == encl_mm) { if (found) {
synchronize_srcu(&encl_mm->encl->srcu); synchronize_srcu(&encl_mm->encl->srcu);
mmu_notifier_put(mn); mmu_notifier_put(mn);
} }
......
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