xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Willy Tarreau <w@1wt.eu>
Cc: "security@kernel.org" <security@kernel.org>,
	Jan Beulich <jbeulich@suse.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	X86 ML <x86@kernel.org>, LKML <linux-kernel@vger.kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	xen-devel <xen-devel@lists.xen.org>,
	Borislav Petkov <bp@alien8.de>, Andy Lutomirski <luto@kernel.org>,
	Sasha Levin <sasha.levin@oracle.com>,
	Boris Ostrovsky <boris.ostrovsky@oracle.com>
Subject: Re: [PATCH v3 2/3] x86/ldt: Make modify_ldt optional
Date: Thu, 23 Jul 2015 16:36:37 -0700	[thread overview]
Message-ID: <CAGXu5j+yEWLuxJ9JUGyN7BxME+iOVXfpFgrZ-_2WXUn6A=0dVg__13096.8340010897$1437694706$gmane$org@mail.gmail.com> (raw)
In-Reply-To: <20150723102434.GA2929@1wt.eu>

On Thu, Jul 23, 2015 at 3:24 AM, Willy Tarreau <w@1wt.eu> wrote:
> Hi Andy,
>
> On Wed, Jul 22, 2015 at 12:23:47PM -0700, Andy Lutomirski wrote:
>> The modify_ldt syscall exposes a large attack surface and is
>> unnecessary for modern userspace.  Make it optional.
>
> Wouldn't you prefer something like this which makes it possible to re-enable
> it at runtime so that we can hope distros ship with it disabled by default ?
>
> It's pretty efficient on your ldtgdt testcase :
>
> # echo 1 > /proc/sys/kernel/modify_ldt
> # ./a.out
> [OK]    LDT entry 0 has AR 0x0040FA00 and limit 0x0000000A
> [OK]    LDT entry 0 has AR 0x00C0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 1 is invalid
> [OK]    LDT entry 2 has AR 0x00C0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 1 is invalid
> [OK]    LDT entry 2 has AR 0x00C0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07A00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00907A00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07200 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07000 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07400 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00507600 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507E00 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507C00 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507A00 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507800 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507800 and limit 0x0000000A
> [RUN]   Test fork
> [OK]    LDT entry 2 has AR 0x00507800 and limit 0x0000000A
> [OK]    LDT entry 1 is invalid
> [OK]    LDT entry 0 has AR 0x0040FA00 and limit 0x0000000A
> [OK]    LDT entry 0 has AR 0x00C0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 1 is invalid
> [OK]    LDT entry 2 has AR 0x00C0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 1 is invalid
> [OK]    LDT entry 2 has AR 0x00C0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D0FA00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07A00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00907A00 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07200 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07000 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00D07400 and limit 0x0000AFFF
> [OK]    LDT entry 2 has AR 0x00507600 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507E00 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507C00 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507A00 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507800 and limit 0x0000000A
> [OK]    LDT entry 2 has AR 0x00507800 and limit 0x0000000A
> [RUN]   Test fork
> [OK]    Child succeeded
> [OK]    modify_ldt failure 22
> [OK]    LDT entry 0 has AR 0x0000F200 and limit 0x00000000
> [OK]    LDT entry 0 has AR 0x00007200 and limit 0x00000000
> [OK]    LDT entry 0 has AR 0x0000F000 and limit 0x00000000
> [OK]    LDT entry 0 has AR 0x00007200 and limit 0x00000000
> [OK]    LDT entry 0 has AR 0x00007000 and limit 0x00000001
> [OK]    LDT entry 0 has AR 0x00007000 and limit 0x00000000
> [OK]    LDT entry 0 is invalid
> [OK]    LDT entry 0 has AR 0x0040F200 and limit 0x00000000
> [OK]    LDT entry 0 is invalid
> [SKIP]  Cannot set affinity to CPU 1
>
>
> # echo 0 > /proc/sys/kernel/modify_ldt
> # ./a.out
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    LDT entry 1 is invalid
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    LDT entry 1 is invalid
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [SKIP]  Skipping fork test because have no LDT
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [OK]    modify_ldt is returned -ENOSYS
> [SKIP]  Cannot set affinity to CPU 1
>
> The patch is quite small (I stole your comment for the config option).
>
> Willy
>
>
> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> index 226d569..b926f65 100644
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -1012,6 +1012,23 @@ config X86_16BIT
>           this option saves about 300 bytes on i386, or around 6K text
>           plus 16K runtime memory on x86-64,
>
> +config DEFAULT_MODIFY_LDT_SYSCALL
> +       bool "Allow userspace to modify the LDT (local descriptor table)"
> +       default y
> +       ---help---
> +         Linux can allow user programs to install a per-process x86
> +         Local Descriptor Table (LDT) using the modify_ldt(2) system
> +         call.  This is required to run 16-bit or segmented code such as
> +         DOSEMU or some Wine programs.  It is also used by some very old
> +         threading libraries.
> +
> +         Enabling this feature increases the low-level kernel attack
> +         surface. Disabling it disables the modify_ldt(2) system call by
> +         default. Note that even when disabled it remains possible to
> +         enable it at runtime by setting the sys.kernel.modify_ldt sysctl.
> +
> +         Say 'N' here if you don't expect to use DOSEMU or Wine often.
> +
>  config X86_ESPFIX32
>         def_bool y
>         depends on X86_16BIT && X86_32
> diff --git a/arch/x86/kernel/ldt.c b/arch/x86/kernel/ldt.c
> index c37886d..2f10b6c 100644
> --- a/arch/x86/kernel/ldt.c
> +++ b/arch/x86/kernel/ldt.c
> @@ -20,6 +20,12 @@
>  #include <asm/mmu_context.h>
>  #include <asm/syscalls.h>
>
> +#ifdef CONFIG_DEFAULT_MODIFY_LDT_SYSCALL
> +int sysctl_modify_ldt __read_mostly = 1;
> +#else
> +int sysctl_modify_ldt __read_mostly = 0;
> +#endif
> +
>  #ifdef CONFIG_SMP
>  static void flush_ldt(void *current_mm)
>  {
> @@ -254,6 +260,9 @@ asmlinkage int sys_modify_ldt(int func, void __user *ptr,
>  {
>         int ret = -ENOSYS;
>
> +       if (!sysctl_modify_ldt)
> +               return ret;
> +
>         switch (func) {
>         case 0:
>                 ret = read_ldt(ptr, bytecount);
> diff --git a/kernel/sysctl.c b/kernel/sysctl.c
> index 2082b1a..60270c6 100644
> --- a/kernel/sysctl.c
> +++ b/kernel/sysctl.c
> @@ -111,6 +111,9 @@ extern int sysctl_nr_open_min, sysctl_nr_open_max;
>  #ifndef CONFIG_MMU
>  extern int sysctl_nr_trim_pages;
>  #endif
> +#ifdef CONFIG_X86
> +extern int sysctl_modify_ldt;
> +#endif
>
>  /* Constants used for minimum and  maximum */
>  #ifdef CONFIG_LOCKUP_DETECTOR
> @@ -962,6 +965,13 @@ static struct ctl_table kern_table[] = {
>                 .mode           = 0644,
>                 .proc_handler   = proc_dointvec,
>         },
> +       {
> +               .procname       = "modify_ldt",
> +               .data           = &sysctl_modify_ldt,
> +               .maxlen         = sizeof(int),
> +               .mode           = 0644,
> +               .proc_handler   = proc_dointvec,
> +       },
>  #endif
>  #if defined(CONFIG_MMU)
>         {

I've been pondering something like this that is even MORE generic, for
any syscall. Something like a "syscalls" directory under
/proc/sys/kernel, with 1 entry per syscall. "0" is "available", "1" is
disabled, and "-1" disabled until next boot.

-Kees

-- 
Kees Cook
Chrome OS Security

  parent reply	other threads:[~2015-07-23 23:36 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1437592883.git.luto@kernel.org>
2015-07-22 19:23 ` [PATCH v3 1/3] x86/ldt: Make modify_ldt synchronous Andy Lutomirski
2015-07-22 19:23 ` [PATCH v3 2/3] x86/ldt: Make modify_ldt optional Andy Lutomirski
2015-07-22 19:23 ` [PATCH v3 3/3] selftests/x86, x86/ldt: Add a selftest for modify_ldt Andy Lutomirski
     [not found] ` <7bfde005b84a90a83bf668a320c7d4ad1b940065.1437592883.git.luto@kernel.org>
2015-07-23  7:13   ` [PATCH v3 2/3] x86/ldt: Make modify_ldt optional Jan Beulich
2015-07-23 10:24   ` Willy Tarreau
     [not found]   ` <20150723102434.GA2929@1wt.eu>
2015-07-23 23:36     ` Kees Cook [this message]
     [not found]     ` <CAGXu5j+yEWLuxJ9JUGyN7BxME+iOVXfpFgrZ-_2WXUn6A=0dVg@mail.gmail.com>
2015-07-23 23:40       ` Andy Lutomirski
     [not found]       ` <CALCETrVvfA+_N_tU2LUwvh+2Q_4AExbQkSgW1C4tESAvhY+4Dg@mail.gmail.com>
2015-07-23 23:58         ` Willy Tarreau
     [not found]         ` <20150723235805.GA3191@1wt.eu>
2015-07-24  0:09           ` Kees Cook
     [not found]           ` <CAGXu5jJNfMvvdr0q17nBz+HiEJuFa7Kvo=ZPAnb4E7So4W0QOA@mail.gmail.com>
2015-07-24  7:24             ` Willy Tarreau
     [not found]             ` <20150724072451.GB3293@1wt.eu>
2015-07-24  7:48               ` Willy Tarreau
     [not found] ` <049fdbab8ae2ecac1c8b40ecd558e9df45ccd5d3.1437592883.git.luto@kernel.org>
2015-07-22 22:20   ` [PATCH v3 1/3] x86/ldt: Make modify_ldt synchronous Boris Ostrovsky
2015-07-24  6:37   ` Borislav Petkov
2015-07-24 15:29   ` Borislav Petkov
     [not found]   ` <55B01745.4010702@oracle.com>
2015-07-25  4:13     ` Boris Ostrovsky
     [not found]     ` <55B30CE3.2010902@oracle.com>
2015-07-25  4:58       ` Andy Lutomirski
     [not found]   ` <20150724152955.GC21441@nazgul.tnic>
2015-07-25  4:52     ` Andy Lutomirski
     [not found]     ` <CALCETrX=uGdTfmz7KkbVyMKEmwcS4C43L5eNsy65VjNsL=7KJA@mail.gmail.com>
2015-07-25  8:37       ` Borislav Petkov

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='CAGXu5j+yEWLuxJ9JUGyN7BxME+iOVXfpFgrZ-_2WXUn6A=0dVg__13096.8340010897$1437694706$gmane$org@mail.gmail.com' \
    --to=keescook@chromium.org \
    --cc=andrew.cooper3@citrix.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=bp@alien8.de \
    --cc=jbeulich@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sasha.levin@oracle.com \
    --cc=security@kernel.org \
    --cc=w@1wt.eu \
    --cc=x86@kernel.org \
    --cc=xen-devel@lists.xen.org \
    /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).