lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Sat, 23 May 2020 10:52:24 +0800
From:   Lai Jiangshan <jiangshanlai+lkml@...il.com>
To:     Peter Zijlstra <peterz@...radead.org>
Cc:     Thomas Gleixner <tglx@...utronix.de>,
        LKML <linux-kernel@...r.kernel.org>, x86@...nel.org,
        "Paul E. McKenney" <paulmck@...nel.org>,
        Andy Lutomirski <luto@...nel.org>,
        Alexandre Chartre <alexandre.chartre@...cle.com>,
        Frederic Weisbecker <frederic@...nel.org>,
        Paolo Bonzini <pbonzini@...hat.com>,
        Sean Christopherson <sean.j.christopherson@...el.com>,
        Masami Hiramatsu <mhiramat@...nel.org>,
        Petr Mladek <pmladek@...e.com>,
        Steven Rostedt <rostedt@...dmis.org>,
        Joel Fernandes <joel@...lfernandes.org>,
        Boris Ostrovsky <boris.ostrovsky@...cle.com>,
        Juergen Gross <jgross@...e.com>,
        Brian Gerst <brgerst@...il.com>,
        Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
        Josh Poimboeuf <jpoimboe@...hat.com>,
        Will Deacon <will@...nel.org>,
        Tom Lendacky <thomas.lendacky@....com>,
        Wei Liu <wei.liu@...nel.org>,
        Michael Kelley <mikelley@...rosoft.com>,
        Jason Chen CJ <jason.cj.chen@...el.com>,
        Zhao Yakui <yakui.zhao@...el.com>
Subject: Re: [patch V6 00/37] x86/entry: Rework leftovers and merge plan

On Tue, May 19, 2020 at 5:04 PM Peter Zijlstra <peterz@...radead.org> wrote:

> +#ifdef CONFIG_DEBUG_ENTRY
>  /* Begin/end of an instrumentation safe region */
> -#define instrumentation_begin() ({                                             \
> +#define instrumentation_begin() ({                                     \
>         asm volatile("%c0:\n\t"                                         \
>                      ".pushsection .discard.instr_begin\n\t"            \
>                      ".long %c0b - .\n\t"                               \
>                      ".popsection\n\t" : : "i" (__COUNTER__));          \
>  })
>
> -#define instrumentation_end() ({                                                       \
> -       asm volatile("%c0:\n\t"                                         \
> +/*
> + * Because instrumentation_{begin,end}() can nest, objtool validation considers
> + * _begin() a +1 and _end() a -1 and computes a sum over the instructions.
> + * When the value is greater than 0, we consider instrumentation allowed.
> + *
> + * There is a problem with code like:
> + *
> + * noinstr void foo()
> + * {
> + *     instrumentation_begin();
> + *     ...
> + *     if (cond) {
> + *             instrumentation_begin();
> + *             ...
> + *             instrumentation_end();
> + *     }
> + *     bar();
> + *     instrumentation_end();
> + * }
> + *
> + * If instrumentation_end() would be an empty label, like all the other
> + * annotations, the inner _end(), which is at the end of a conditional block,
> + * would land on the instruction after the block.
> + *
> + * If we then consider the sum of the !cond path, we'll see that the call to
> + * bar() is with a 0-value, even though, we meant it to happen with a positive
> + * value.
> + *
> + * To avoid this, have _end() be a NOP instruction, this ensures it will be
> + * part of the condition block and does not escape.
> + */
> +#define instrumentation_end() ({                                       \
> +       asm volatile("%c0: nop\n\t"                                     \
>                      ".pushsection .discard.instr_end\n\t"              \
>                      ".long %c0b - .\n\t"                               \
>                      ".popsection\n\t" : : "i" (__COUNTER__));          \
>  })

Hello,

I, who don't know how does the objtool handle it, am just curious.
_begin() and _end() are symmetrical, which means if _end() (without nop)
can escape, so can _begin() in a reverse way. For example:

noinstr void foo()
{
    instrumentation_begin();
    do {
            instrumentation_begin();
            ...
            instrumentation_end();
    } while (cond);
    bar();
    instrumentation_end();
}

Here, the first _begin() can be "dragged" into the do-while block.
Expectedly, objtool validation should not complain here.

But objtool validation's not complaining means it can handle it
magically correctly (by distinguishing how many _begin()s should
be taken around the jmp target when jmp in a specific path), or
handle it by not checking if all paths have the same count onto
a jmp target (a little nervous to me), or other possible ways.

Sorry for my curiosity.
Thanks
Lai.

Powered by blists - more mailing lists