From ced86e1611c925a35187083210c890f8bf7c83e8 Mon Sep 17 00:00:00 2001 From: Chuck Ebbert Date: Fri, 24 Jun 2011 06:12:52 -0400 Subject: [PATCH] CVE-2011-2183: ksm: race between ksmd and exiting task --- kernel.spec | 8 ++ ...reference-in-scan-get-next-rmap-item.patch | 82 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 ksm-fix-null-pointer-dereference-in-scan-get-next-rmap-item.patch diff --git a/kernel.spec b/kernel.spec index e15ec2402..cf0787975 100644 --- a/kernel.spec +++ b/kernel.spec @@ -619,6 +619,9 @@ Patch30: linux-2.6-tracehook.patch Patch31: linux-2.6-utrace.patch Patch32: linux-2.6-utrace-ptrace.patch +# CVE-2011-2183 +Patch80: ksm-fix-null-pointer-dereference-in-scan-get-next-rmap-item.patch + Patch150: linux-2.6.29-sparc-IOC_TYPECHECK.patch Patch160: linux-2.6-32bit-mmap-exec-randomization.patch @@ -1210,6 +1213,10 @@ ApplyPatch linux-2.6-tracehook.patch ApplyPatch linux-2.6-utrace.patch ApplyPatch linux-2.6-utrace-ptrace.patch +# mm patches +# CVE-2011-2183 +ApplyPatch ksm-fix-null-pointer-dereference-in-scan-get-next-rmap-item.patch + # Architecture patches # x86(-64) # Restore reliable stack backtraces, and hopefully @@ -2037,6 +2044,7 @@ fi - block-blkdev_get-should-access-bd_disk-only-after.patch: fix potential oops introduced in 2.6.38.8 - ahci-add-another-pci-id-for-marvell.patch (#705960) +- CVE-2011-2183: ksm: race between ksmd and exiting task * Thu Jun 23 2011 Dave Airlie 2.6.38.8-34 - drm-i915-snb-irq-stalls-fix.patch: fix Sandybridge IRQ stalls diff --git a/ksm-fix-null-pointer-dereference-in-scan-get-next-rmap-item.patch b/ksm-fix-null-pointer-dereference-in-scan-get-next-rmap-item.patch new file mode 100644 index 000000000..d67cdc748 --- /dev/null +++ b/ksm-fix-null-pointer-dereference-in-scan-get-next-rmap-item.patch @@ -0,0 +1,82 @@ +From: Hugh Dickins +Date: Wed, 15 Jun 2011 22:08:58 +0000 (-0700) +Subject: ksm: fix NULL pointer dereference in scan_get_next_rmap_item() +X-Git-Tag: v3.0-rc4~44 +X-Git-Url: http://git.kernel.org/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git;a=commitdiff_plain;h=2b472611a32a72f4a118c069c2d62a1a3f087afd + +ksm: fix NULL pointer dereference in scan_get_next_rmap_item() + +Andrea Righi reported a case where an exiting task can race against +ksmd::scan_get_next_rmap_item (http://lkml.org/lkml/2011/6/1/742) easily +triggering a NULL pointer dereference in ksmd. + +ksm_scan.mm_slot == &ksm_mm_head with only one registered mm + +CPU 1 (__ksm_exit) CPU 2 (scan_get_next_rmap_item) + list_empty() is false +lock slot == &ksm_mm_head +list_del(slot->mm_list) +(list now empty) +unlock + lock + slot = list_entry(slot->mm_list.next) + (list is empty, so slot is still ksm_mm_head) + unlock + slot->mm == NULL ... Oops + +Close this race by revalidating that the new slot is not simply the list +head again. + +Andrea's test case: + +#include +#include +#include +#include + +#define BUFSIZE getpagesize() + +int main(int argc, char **argv) +{ + void *ptr; + + if (posix_memalign(&ptr, getpagesize(), BUFSIZE) < 0) { + perror("posix_memalign"); + exit(1); + } + if (madvise(ptr, BUFSIZE, MADV_MERGEABLE) < 0) { + perror("madvise"); + exit(1); + } + *(char *)NULL = 0; + + return 0; +} + +Reported-by: Andrea Righi +Tested-by: Andrea Righi +Cc: Andrea Arcangeli +Signed-off-by: Hugh Dickins +Signed-off-by: Chris Wright +Cc: +Signed-off-by: Andrew Morton +Signed-off-by: Linus Torvalds +--- + +diff --git a/mm/ksm.c b/mm/ksm.c +index d708b3e..9a68b0c 100644 +--- a/mm/ksm.c ++++ b/mm/ksm.c +@@ -1302,6 +1302,12 @@ static struct rmap_item *scan_get_next_rmap_item(struct page **page) + slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list); + ksm_scan.mm_slot = slot; + spin_unlock(&ksm_mmlist_lock); ++ /* ++ * Although we tested list_empty() above, a racing __ksm_exit ++ * of the last mm on the list may have removed it since then. ++ */ ++ if (slot == &ksm_mm_head) ++ return NULL; + next_mm: + ksm_scan.address = 0; + ksm_scan.rmap_list = &slot->rmap_list;