Skip to content
Snippets Groups Projects
  1. Jun 10, 2022
  2. May 24, 2022
  3. May 16, 2022
    • Kees Cook's avatar
      gcc-plugins: randstruct: Remove cast exception handling · 710e4ebf
      Kees Cook authored
      
      With all randstruct exceptions removed, remove all the exception
      handling code. Any future warnings are likely to be shared between
      this plugin and Clang randstruct, and will need to be addressed in a
      more wholistic fashion.
      
      Cc: Christoph Hellwig <hch@infradead.org>
      Cc: linux-hardening@vger.kernel.org
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      710e4ebf
    • Kees Cook's avatar
      af_unix: Silence randstruct GCC plugin warning · b146cbf2
      Kees Cook authored
      
      While preparing for Clang randstruct support (which duplicated many of
      the warnings the randstruct GCC plugin warned about), one strange one
      remained only for the randstruct GCC plugin. Eliminating this rids
      the plugin of the last exception.
      
      It seems the plugin is happy to dereference individual members of
      a cross-struct cast, but it is upset about casting to a whole object
      pointer. This only manifests in one place in the kernel, so just replace
      the variable with individual member accesses. There is no change in
      executable instruction output.
      
      Drop the last exception from the randstruct GCC plugin.
      
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: Christoph Hellwig <hch@infradead.org>
      Cc: Paolo Abeni <pabeni@redhat.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Cong Wang <cong.wang@bytedance.com>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: netdev@vger.kernel.org
      Cc: linux-hardening@vger.kernel.org
      Acked-by: default avatarKuniyuki Iwashima <kuniyu@amazon.co.jp>
      Link: https://lore.kernel.org/lkml/20220511022217.58586-1-kuniyu@amazon.co.jp
      
      
      Acked-by: default avatarJakub Kicinski <kuba@kernel.org>
      Link: https://lore.kernel.org/lkml/20220511151542.4cb3ff17@kernel.org
      
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      b146cbf2
    • Kees Cook's avatar
      niu: Silence randstruct warnings · 2dcfe9e2
      Kees Cook authored
      Clang randstruct gets upset when it sees struct addresspace (which is
      randomized) being assigned to a struct page (which is not randomized):
      
      drivers/net/ethernet/sun/niu.c:3385:12: error: casting from randomized structure pointer type 'struct address_space *' to 'struct page *'
                              *link = (struct page *) page->mapping;
                                      ^
      
      It looks like niu.c is looking for an in-line place to chain its allocated
      pages together and is overloading the "mapping" member, as it is unused.
      This is very non-standard, and is expected to be cleaned up in the
      future[1], but there is no "correct" way to handle it today.
      
      No meaningful machine code changes result after this change, and source
      readability is improved.
      
      Drop the randstruct exception now that there is no "confusing" cross-type
      assignment.
      
      [1] https://lore.kernel.org/lkml/YnqgjVoMDu5v9PNG@casper.infradead.org/
      
      
      
      Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
      Cc: Christoph Hellwig <hch@infradead.org>
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: Jakub Kicinski <kuba@kernel.org>
      Cc: Paolo Abeni <pabeni@redhat.com>
      Cc: Du Cheng <ducheng2@gmail.com>
      Cc: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: William Kucharski <william.kucharski@oracle.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Nathan Chancellor <nathan@kernel.org>
      Cc: netdev@vger.kernel.org
      Cc: linux-mm@kvack.org
      Cc: linux-hardening@vger.kernel.org
      Acked-by: default avatarJakub Kicinski <kuba@kernel.org>
      Link: https://lore.kernel.org/lkml/20220511151647.7290adbe@kernel.org
      
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      2dcfe9e2
    • Kees Cook's avatar
      big_keys: Use struct for internal payload · c1298a3a
      Kees Cook authored
      
      The randstruct GCC plugin gets upset when it sees struct path (which is
      randomized) being assigned from a "void *" (which it cannot type-check).
      
      There's no need for these casts, as the entire internal payload use is
      following a normal struct layout. Convert the enum-based void * offset
      dereferencing to the new big_key_payload struct. No meaningful machine
      code changes result after this change, and source readability is improved.
      
      Drop the randstruct exception now that there is no "confusing" cross-type
      assignment.
      
      Cc: David Howells <dhowells@redhat.com>
      Cc: Eric Biggers <ebiggers@kernel.org>
      Cc: Christoph Hellwig <hch@infradead.org>
      Cc: Jarkko Sakkinen <jarkko@kernel.org>
      Cc: James Morris <jmorris@namei.org>
      Cc: "Serge E. Hallyn" <serge@hallyn.com>
      Cc: linux-hardening@vger.kernel.org
      Cc: keyrings@vger.kernel.org
      Cc: linux-security-module@vger.kernel.org
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      c1298a3a
  4. May 10, 2022
  5. May 08, 2022
  6. Apr 13, 2022
  7. Apr 12, 2022
    • Jason A. Donenfeld's avatar
      gcc-plugins: latent_entropy: use /dev/urandom · c40160f2
      Jason A. Donenfeld authored
      While the latent entropy plugin mostly doesn't derive entropy from
      get_random_const() for measuring the call graph, when __latent_entropy is
      applied to a constant, then it's initialized statically to output from
      get_random_const(). In that case, this data is derived from a 64-bit
      seed, which means a buffer of 512 bits doesn't really have that amount
      of compile-time entropy.
      
      This patch fixes that shortcoming by just buffering chunks of
      /dev/urandom output and doling it out as requested.
      
      At the same time, it's important that we don't break the use of
      -frandom-seed, for people who want the runtime benefits of the latent
      entropy plugin, while still having compile-time determinism. In that
      case, we detect whether gcc's set_random_seed() has been called by
      making a call to get_random_seed(noinit=true) in the plugin init
      function, which is called after set_random_seed() is called but before
      anything that calls get_random_seed(noinit=false), and seeing if it's
      zero or not. If it's not zero, we're in deterministic mode, and so we
      just generate numbers with a basic xorshift prng.
      
      Note that we don't detect if -frandom-seed is being used using the
      documented local_tick variable, because it's assigned via:
         local_tick = (unsigned) tv.tv_sec * 1000 + tv.tv_usec / 1000;
      which may well overflow and become -1 on its own, and so isn't
      reliable: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105171
      
      
      
      [kees: The 256 byte rnd_buf size was chosen based on average (250),
       median (64), and std deviation (575) bytes of used entropy for a
       defconfig x86_64 build]
      
      Fixes: 38addce8 ("gcc-plugins: Add latent_entropy plugin")
      Cc: stable@vger.kernel.org
      Cc: PaX Team <pageexec@freemail.hu>
      Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      Link: https://lore.kernel.org/r/20220405222815.21155-1-Jason@zx2c4.com
      c40160f2
  8. Feb 06, 2022
  9. Oct 21, 2021
  10. Oct 04, 2021
  11. Sep 27, 2021
  12. Aug 10, 2021
  13. May 01, 2021
  14. Mar 11, 2021
  15. Mar 02, 2021
  16. Jan 06, 2021
    • Valdis Kletnieks's avatar
      gcc-plugins: fix gcc 11 indigestion with plugins... · 67a5a680
      Valdis Kletnieks authored
      
      Fedora Rawhide has started including gcc 11,and the g++ compiler
      throws a wobbly when it hits scripts/gcc-plugins:
      
        HOSTCXX scripts/gcc-plugins/latent_entropy_plugin.so
      In file included from /usr/include/c++/11/type_traits:35,
                       from /usr/lib/gcc/x86_64-redhat-linux/11/plugin/include/system.h:244,
                       from /usr/lib/gcc/x86_64-redhat-linux/11/plugin/include/gcc-plugin.h:28,
                       from scripts/gcc-plugins/gcc-common.h:7,
                       from scripts/gcc-plugins/latent_entropy_plugin.c:78:
      /usr/include/c++/11/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO
       C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
         32 | #error This file requires compiler and library support \
      
      In fact, it works just fine with c++11, which has been in gcc since 4.8,
      and we now require 4.9 as a minimum.
      
      Signed-off-by: default avatarValdis Kletnieks <valdis.kletnieks@vt.edu>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      Link: https://lore.kernel.org/r/82487.1609006918@turing-police
      67a5a680
  17. Dec 04, 2020
  18. Aug 09, 2020
    • Masahiro Yamada's avatar
      kbuild: move host .so build rules to scripts/gcc-plugins/Makefile · 42640b13
      Masahiro Yamada authored
      The host shared library rules are currently implemented in
      scripts/Makefile.host, but actually GCC-plugin is the only user of
      them. (The VDSO .so files are built for the target by different
      build rules) Hence, they do not need to be treewide available.
      
      Move all the relevant build rules to scripts/gcc-plugins/Makefile.
      
      I also optimized the build steps so *.so is directly built from .c
      because every upstream plugin is compiled from a single source file.
      
      I am still keeping the multi-file plugin support, which Kees Cook
      mentioned might be needed by out-of-tree plugins.
      (https://lkml.org/lkml/2019/1/11/1107
      
      )
      
      If the plugin, foo.so, is compiled from two files foo.c and foo2.c,
      then you can do like follows:
      
        foo-objs := foo.o foo2.o
      
      Single-file plugins do not need the *-objs notation.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Acked-by: default avatarKees Cook <keescook@chromium.org>
      42640b13
  19. Jul 13, 2020
  20. Jun 28, 2020
  21. Jun 24, 2020
    • Alexander Popov's avatar
      gcc-plugins/stackleak: Add 'verbose' plugin parameter · 8dd70543
      Alexander Popov authored
      
      Add 'verbose' plugin parameter for stackleak gcc plugin.
      It can be used for printing additional info about the kernel code
      instrumentation.
      
      For using it add the following to scripts/Makefile.gcc-plugins:
        gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \
          += -fplugin-arg-stackleak_plugin-verbose
      
      Signed-off-by: default avatarAlexander Popov <alex.popov@linux.com>
      Link: https://lore.kernel.org/r/20200624123330.83226-6-alex.popov@linux.com
      
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      8dd70543
    • Alexander Popov's avatar
      gcc-plugins/stackleak: Use asm instrumentation to avoid useless register saving · feee1b8c
      Alexander Popov authored
      The kernel code instrumentation in stackleak gcc plugin works in two stages.
      At first, stack tracking is added to GIMPLE representation of every function
      (except some special cases). And later, when stack frame size info is
      available, stack tracking is removed from the RTL representation of the
      functions with small stack frame. There is an unwanted side-effect for these
      functions: some of them do useless work with caller-saved registers.
      
      As an example of such case, proc_sys_write without() instrumentation:
          55                      push   %rbp
          41 b8 01 00 00 00       mov    $0x1,%r8d
          48 89 e5                mov    %rsp,%rbp
          e8 11 ff ff ff          callq  ffffffff81284610 <proc_sys_call_handler>
          5d                      pop    %rbp
          c3                      retq
          0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
          66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
          00 00 00
      
      proc_sys_write() with instrumentation:
          55                      push   %rbp
          48 89 e5                mov    %rsp,%rbp
          41 56                   push   %r14
          41 55                   push   %r13
          41 54                   push   %r12
          53                      push   %rbx
          49 89 f4                mov    %rsi,%r12
          48 89 fb                mov    %rdi,%rbx
          49 89 d5                mov    %rdx,%r13
          49 89 ce                mov    %rcx,%r14
          4c 89 f1                mov    %r14,%rcx
          4c 89 ea                mov    %r13,%rdx
          4c 89 e6                mov    %r12,%rsi
          48 89 df                mov    %rbx,%rdi
          41 b8 01 00 00 00       mov    $0x1,%r8d
          e8 f2 fe ff ff          callq  ffffffff81298e80 <proc_sys_call_handler>
          5b                      pop    %rbx
          41 5c                   pop    %r12
          41 5d                   pop    %r13
          41 5e                   pop    %r14
          5d                      pop    %rbp
          c3                      retq
          66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
          00 00
      
      Let's improve the instrumentation to avoid this:
      
      1. Make stackleak_track_stack() save all register that it works with.
      Use no_caller_saved_registers attribute for that function. This attribute
      is available for x86_64 and i386 starting from gcc-7.
      
      2. Insert calling stackleak_track_stack() in asm:
        asm volatile("call stackleak_track_stack" :: "r" (current_stack_pointer))
      Here we use ASM_CALL_CONSTRAINT trick from arch/x86/include/asm/asm.h.
      The input constraint is taken into account during gcc shrink-wrapping
      optimization. It is needed to be sure that stackleak_track_stack() call is
      inserted after the prologue of the containing function, when the stack
      frame is prepared.
      
      This work is a deep reengineering of the idea described on grsecurity blog
        https://grsecurity.net/resolving_an_unfortunate_stackleak_interaction
      
      
      
      Signed-off-by: default avatarAlexander Popov <alex.popov@linux.com>
      Acked-by: default avatarMiguel Ojeda <miguel.ojeda.sandonis@gmail.com>
      Link: https://lore.kernel.org/r/20200624123330.83226-5-alex.popov@linux.com
      
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      feee1b8c
  22. May 25, 2020
  23. Apr 15, 2020
  24. Apr 13, 2020
    • Frédéric Pierret (fepitre)'s avatar
      gcc-common.h: Update for GCC 10 · c7527373
      Frédéric Pierret (fepitre) authored
      
      Remove "params.h" include, which has been dropped in GCC 10.
      
      Remove is_a_helper() macro, which is now defined in gimple.h, as seen
      when running './scripts/gcc-plugin.sh g++ g++ gcc':
      
      In file included from <stdin>:1:
      ./gcc-plugins/gcc-common.h:852:13: error: redefinition of ‘static bool is_a_helper<T>::test(U*) [with U = const gimple; T = const ggoto*]’
        852 | inline bool is_a_helper<const ggoto *>::test(const_gimple gs)
            |             ^~~~~~~~~~~~~~~~~~~~~~~~~~
      In file included from ./gcc-plugins/gcc-common.h:125,
                       from <stdin>:1:
      /usr/lib/gcc/x86_64-redhat-linux/10/plugin/include/gimple.h:1037:1: note: ‘static bool is_a_helper<T>::test(U*) [with U = const gimple; T = const ggoto*]’ previously declared here
       1037 | is_a_helper <const ggoto *>::test (const gimple *gs)
            | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      Add -Wno-format-diag to scripts/gcc-plugins/Makefile to avoid
      meaningless warnings from error() formats used by plugins:
      
      scripts/gcc-plugins/structleak_plugin.c: In function ‘int plugin_init(plugin_name_args*, plugin_gcc_version*)’:
      scripts/gcc-plugins/structleak_plugin.c:253:12: warning: unquoted sequence of 2 consecutive punctuation characters ‘'-’ in format [-Wformat-diag]
        253 |   error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
            |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      Signed-off-by: default avatarFrédéric Pierret (fepitre) <frederic.pierret@qubes-os.org>
      Link: https://lore.kernel.org/r/20200407113259.270172-1-frederic.pierret@qubes-os.org
      
      
      [kees: include -Wno-format-diag for plugin builds]
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      c7527373
    • Kees Cook's avatar
      gcc-plugins/stackleak: Avoid assignment for unused macro argument · 8d97fb39
      Kees Cook authored
      
      With GCC version >= 8, the cgraph_create_edge() macro argument using
      "frequency" goes unused. Instead of assigning a temporary variable for
      the argument, pass the compute_call_stmt_bb_frequency() call directly
      as the macro argument so that it will just not be called when it is
      not wanted by the macros.
      
      Silences the warning:
      
      scripts/gcc-plugins/stackleak_plugin.c:54:6: warning: variable ‘frequency’ set but not used [-Wunused-but-set-variable]
      
      Now builds cleanly with gcc-7 and gcc-9. Both boot and pass
      STACKLEAK_ERASING LKDTM test.
      
      Signed-off-by: default avatarKees Cook <keescook@chromium.org>
      8d97fb39
  25. Apr 08, 2020
  26. Mar 29, 2020
    • Masahiro Yamada's avatar
      kbuild: add -Wall to KBUILD_HOSTCXXFLAGS · 735aab1e
      Masahiro Yamada authored
      
      Add -Wall to catch more warnings for C++ host programs.
      
      When I submitted the previous version, the 0-day bot reported
      -Wc++11-compat warnings for old GCC:
      
        HOSTCXX -fPIC scripts/gcc-plugins/latent_entropy_plugin.o
      In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/plugin/include/tm.h:28:0,
                       from scripts/gcc-plugins/gcc-common.h:15,
                       from scripts/gcc-plugins/latent_entropy_plugin.c:78:
      /usr/lib/gcc/x86_64-linux-gnu/4.8/plugin/include/config/elfos.h:102:21: warning: C++11 requires a space between string literal and macro [-Wc++11-compat]
          fprintf ((FILE), "%s"HOST_WIDE_INT_PRINT_UNSIGNED"\n",\
                           ^
      /usr/lib/gcc/x86_64-linux-gnu/4.8/plugin/include/config/elfos.h:170:24: warning: C++11 requires a space between string literal and macro [-Wc++11-compat]
             fprintf ((FILE), ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",  \
                              ^
      In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/plugin/include/tm.h:42:0,
                       from scripts/gcc-plugins/gcc-common.h:15,
                       from scripts/gcc-plugins/latent_entropy_plugin.c:78:
      /usr/lib/gcc/x86_64-linux-gnu/4.8/plugin/include/defaults.h:126:24: warning: C++11 requires a space between string literal and macro [-Wc++11-compat]
             fprintf ((FILE), ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",  \
                              ^
      
      The source of the warnings is in the plugin headers, so we have no
      control of it. I just suppressed them by adding -Wno-c++11-compat to
      scripts/gcc-plugins/Makefile.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Acked-by: default avatarKees Cook <keescook@chromium.org>
      735aab1e
  27. Mar 25, 2020
  28. Mar 10, 2020
  29. Feb 03, 2020
    • Masahiro Yamada's avatar
      kbuild: rename hostprogs-y/always to hostprogs/always-y · 5f2fb52f
      Masahiro Yamada authored
      
      In old days, the "host-progs" syntax was used for specifying host
      programs. It was renamed to the current "hostprogs-y" in 2004.
      
      It is typically useful in scripts/Makefile because it allows Kbuild to
      selectively compile host programs based on the kernel configuration.
      
      This commit renames like follows:
      
        always       ->  always-y
        hostprogs-y  ->  hostprogs
      
      So, scripts/Makefile will look like this:
      
        always-$(CONFIG_BUILD_BIN2C) += ...
        always-$(CONFIG_KALLSYMS)    += ...
            ...
        hostprogs := $(always-y) $(always-m)
      
      I think this makes more sense because a host program is always a host
      program, irrespective of the kernel configuration. We want to specify
      which ones to compile by CONFIG options, so always-y will be handier.
      
      The "always", "hostprogs-y", "hostprogs-m" will be kept for backward
      compatibility for a while.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      5f2fb52f
Loading