x86/ldt: Fix off by one in get_segment_base()
authorDan Carpenter <dan.carpenter@oracle.com>
Fri, 18 Aug 2017 10:30:30 +0000 (13:30 +0300)
committerIngo Molnar <mingo@kernel.org>
Tue, 29 Aug 2017 09:55:15 +0000 (11:55 +0200)
ldt->entries[] is allocated in alloc_ldt_struct().  It has
ldt->nr_entries elements and ldt->nr_entries is capped at LDT_ENTRIES.
So if "idx" is == ldt->nr_entries then we're reading beyond the end of
the buffer.  It seems duplicative to have two limit checks when one
would work just as well so I removed the check against LDT_ENTRIES.

The gdt_page.gdt[] array has GDT_ENTRIES entries.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Acked-by: Andy Lutomirski <luto@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: kernel-janitors@vger.kernel.org
Fixes: d07bdfd322d3 ("perf/x86: Fix USER/KERNEL tagging of samples properly")
Link: http://lkml.kernel.org/r/20170818102516.gqwm4xdvvuvjw5ho@mwanda
Signed-off-by: Ingo Molnar <mingo@kernel.org>
arch/x86/events/core.c

index af12e294caeda5f518cf8b80ce5765996f1d8c56..939050169d122c9d129219be27c09e6a9a9ef443 100644 (file)
@@ -2335,12 +2335,9 @@ static unsigned long get_segment_base(unsigned int segment)
 #ifdef CONFIG_MODIFY_LDT_SYSCALL
                struct ldt_struct *ldt;
 
-               if (idx > LDT_ENTRIES)
-                       return 0;
-
                /* IRQs are off, so this synchronizes with smp_store_release */
                ldt = lockless_dereference(current->active_mm->context.ldt);
-               if (!ldt || idx > ldt->nr_entries)
+               if (!ldt || idx >= ldt->nr_entries)
                        return 0;
 
                desc = &ldt->entries[idx];
@@ -2348,7 +2345,7 @@ static unsigned long get_segment_base(unsigned int segment)
                return 0;
 #endif
        } else {
-               if (idx > GDT_ENTRIES)
+               if (idx >= GDT_ENTRIES)
                        return 0;
 
                desc = raw_cpu_ptr(gdt_page.gdt) + idx;