From: Kees Cook <keescook@chromium.org>
To: Rick Edgecombe <rick.p.edgecombe@intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
X86 ML <x86@kernel.org>, LKML <linux-kernel@vger.kernel.org>,
Linux-MM <linux-mm@kvack.org>,
Kernel Hardening <kernel-hardening@lists.openwall.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Jann Horn <jannh@google.com>,
Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Kristen Carlson Accardi <kristen@linux.intel.com>,
Dave Hansen <dave.hansen@intel.com>,
Arjan van de Ven <arjan@linux.intel.com>
Subject: Re: [PATCH v6 3/4] vmalloc: Add debugfs modfraginfo
Date: Fri, 21 Sep 2018 11:56:41 -0700 [thread overview]
Message-ID: <CAGXu5jJj+08J9UeyQs5ku8CziYWA72iJ+hxMR2Z2tLiVwvU8MA@mail.gmail.com> (raw)
In-Reply-To: <1536874298-23492-4-git-send-email-rick.p.edgecombe@intel.com>
On Thu, Sep 13, 2018 at 2:31 PM, Rick Edgecombe
<rick.p.edgecombe@intel.com> wrote:
> Add debugfs file "modfraginfo" for providing info on module space fragmentation.
> This can be used for determining if loadable module randomization is causing any
> problems for extreme module loading situations, like huge numbers of modules or
> extremely large modules.
>
> Sample output when KASLR is enabled and X86_64 is configured:
> Largest free space: 897912 kB
> Total free space: 1025424 kB
> Allocations in backup area: 0
>
> Sample output when just X86_64:
> Largest free space: 897912 kB
> Total free space: 1025424 kB
>
> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
I like having these statistics available!
> ---
> mm/vmalloc.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 101 insertions(+), 1 deletion(-)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 1954458..a44b902 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -18,6 +18,7 @@
> #include <linux/interrupt.h>
> #include <linux/proc_fs.h>
> #include <linux/seq_file.h>
> +#include <linux/debugfs.h>
> #include <linux/debugobjects.h>
> #include <linux/kallsyms.h>
> #include <linux/list.h>
> @@ -33,6 +34,7 @@
> #include <linux/bitops.h>
>
> #include <linux/uaccess.h>
> +#include <asm/setup.h>
> #include <asm/tlbflush.h>
> #include <asm/shmparam.h>
>
> @@ -2919,7 +2921,105 @@ static int __init proc_vmalloc_init(void)
> proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
> return 0;
> }
> -module_init(proc_vmalloc_init);
> +#else
> +static int __init proc_vmalloc_init(void)
> +{
> + return 0;
> +}
> +#endif
> +
> +#if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_X86_64)
> +static inline unsigned long is_in_backup(unsigned long addr)
> +{
> + return addr >= MODULES_VADDR + MODULES_RAND_LEN;
> +}
> +#else
> +static inline unsigned long is_in_backup(unsigned long addr)
> +{
> + return 0;
> +}
>
> +inline bool kaslr_enabled(void);
> #endif
>
> +
> +#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_64)
> +static int modulefraginfo_debug_show(struct seq_file *m, void *v)
> +{
> + unsigned long last_end = MODULES_VADDR;
> + unsigned long total_free = 0;
> + unsigned long largest_free = 0;
> + unsigned long backup_cnt = 0;
> + unsigned long gap;
> + struct vmap_area *prev, *cur = NULL;
> +
> + spin_lock(&vmap_area_lock);
> +
> + if (!pvm_find_next_prev(MODULES_VADDR, &cur, &prev) || !cur)
> + goto done;
> +
> + for (; cur->va_end <= MODULES_END; cur = list_next_entry(cur, list)) {
> + /* Don't count areas that are marked to be lazily freed */
> + if (!(cur->flags & VM_LAZY_FREE)) {
> + backup_cnt += is_in_backup(cur->va_start);
> + gap = cur->va_start - last_end;
> + if (gap > largest_free)
> + largest_free = gap;
> + total_free += gap;
> + last_end = cur->va_end;
> + }
> +
> + if (list_is_last(&cur->list, &vmap_area_list))
> + break;
> + }
> +
> +done:
> + gap = (MODULES_END - last_end);
> + if (gap > largest_free)
> + largest_free = gap;
> + total_free += gap;
> +
> + spin_unlock(&vmap_area_lock);
> +
> + seq_printf(m, "\tLargest free space:\t%lu kB\n", largest_free / 1024);
> + seq_printf(m, "\t Total free space:\t%lu kB\n", total_free / 1024);
> +
> + if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled())
> + seq_printf(m, "Allocations in backup area:\t%lu\n", backup_cnt);
I don't think the IS_ENABLED is needed here?
I wonder if there is a better way to arrange this code that uses fewer
ifdefs, etc. Maybe a single CONFIG that capture whether or not
fine-grained module randomization is built in, like:
config RANDOMIZE_FINE_MODULE
def_bool y if RANDOMIZE_BASE && X86_64
#ifdef CONFIG_RANDOMIZE_FINE_MODULE
...
#endif
But that doesn't capture the DEBUG_FS and PROC_FS bits ... so ...
maybe not worth it. I guess, either way:
Reviewed-by: Kees Cook <keescook@chromium.org>
-Kees
--
Kees Cook
Pixel Security
next prev parent reply other threads:[~2018-09-21 18:56 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-13 21:31 [PATCH v6 0/4] KASLR feature to randomize each loadable module Rick Edgecombe
2018-09-13 21:31 ` [PATCH v6 1/4] vmalloc: Add __vmalloc_node_try_addr function Rick Edgecombe
2018-09-21 18:46 ` Kees Cook
2018-09-13 21:31 ` [PATCH v6 2/4] x86/modules: Increase randomization for modules Rick Edgecombe
2018-09-21 19:05 ` Kees Cook
2018-09-24 18:57 ` Edgecombe, Rick P
2018-09-24 19:58 ` Kees Cook
2018-09-24 21:27 ` Edgecombe, Rick P
2018-09-24 21:29 ` Kees Cook
2018-09-13 21:31 ` [PATCH v6 3/4] vmalloc: Add debugfs modfraginfo Rick Edgecombe
2018-09-21 18:56 ` Kees Cook [this message]
2018-09-24 18:58 ` Edgecombe, Rick P
2018-09-24 20:03 ` Kees Cook
2018-09-13 21:31 ` [PATCH v6 4/4] Kselftest for module text allocation benchmarking Rick Edgecombe
2018-09-18 0:27 ` kbuild test robot
2018-09-21 19:05 ` [PATCH v6 0/4] KASLR feature to randomize each loadable module Kees Cook
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=CAGXu5jJj+08J9UeyQs5ku8CziYWA72iJ+hxMR2Z2tLiVwvU8MA@mail.gmail.com \
--to=keescook@chromium.org \
--cc=alexei.starovoitov@gmail.com \
--cc=arjan@linux.intel.com \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@intel.com \
--cc=hpa@zytor.com \
--cc=jannh@google.com \
--cc=kernel-hardening@lists.openwall.com \
--cc=kristen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mingo@redhat.com \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.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