diff mbox series

[v3,03/17] mm: add generic __va_function and __pa_function macros

Message ID 20210323203946.2159693-4-samitolvanen@google.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series Add support for Clang CFI | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Sami Tolvanen March 23, 2021, 8:39 p.m. UTC
With CONFIG_CFI_CLANG, the compiler replaces function addresses
in instrumented C code with jump table addresses. This means that
__pa_symbol(function) returns the physical address of the jump table
entry instead of the actual function, which may not work as the jump
table code will immediately jump to a virtual address that may not be
mapped.

To avoid this address space confusion, this change adds generic
definitions for __va_function and __pa_function, which architectures
that support CFI can override. The typical implementation of the
__va_function macro would use inline assembly to take the function
address, which avoids compiler instrumentation.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
---
 include/linux/mm.h | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Christoph Hellwig March 24, 2021, 7:13 a.m. UTC | #1
On Tue, Mar 23, 2021 at 01:39:32PM -0700, Sami Tolvanen wrote:
> With CONFIG_CFI_CLANG, the compiler replaces function addresses
> in instrumented C code with jump table addresses. This means that
> __pa_symbol(function) returns the physical address of the jump table
> entry instead of the actual function, which may not work as the jump
> table code will immediately jump to a virtual address that may not be
> mapped.
> 
> To avoid this address space confusion, this change adds generic
> definitions for __va_function and __pa_function, which architectures
> that support CFI can override. The typical implementation of the
> __va_function macro would use inline assembly to take the function
> address, which avoids compiler instrumentation.

I think these helper are sensible, but shouldn't they have somewhat
less arcane names and proper documentation?
Sami Tolvanen March 24, 2021, 3:54 p.m. UTC | #2
On Wed, Mar 24, 2021 at 12:14 AM Christoph Hellwig <hch@infradead.org> wrote:
>
> On Tue, Mar 23, 2021 at 01:39:32PM -0700, Sami Tolvanen wrote:
> > With CONFIG_CFI_CLANG, the compiler replaces function addresses
> > in instrumented C code with jump table addresses. This means that
> > __pa_symbol(function) returns the physical address of the jump table
> > entry instead of the actual function, which may not work as the jump
> > table code will immediately jump to a virtual address that may not be
> > mapped.
> >
> > To avoid this address space confusion, this change adds generic
> > definitions for __va_function and __pa_function, which architectures
> > that support CFI can override. The typical implementation of the
> > __va_function macro would use inline assembly to take the function
> > address, which avoids compiler instrumentation.
>
> I think these helper are sensible, but shouldn't they have somewhat
> less arcane names and proper documentation?

Good point, I'll add comments in the next version. I thought
__pa_function would be a fairly straightforward replacement for
__pa_symbol, but I'm fine with renaming these. Any suggestions for
less arcane names?

Sami
Mark Rutland March 25, 2021, 10:16 a.m. UTC | #3
On Wed, Mar 24, 2021 at 08:54:18AM -0700, Sami Tolvanen wrote:
> On Wed, Mar 24, 2021 at 12:14 AM Christoph Hellwig <hch@infradead.org> wrote:
> >
> > On Tue, Mar 23, 2021 at 01:39:32PM -0700, Sami Tolvanen wrote:
> > > With CONFIG_CFI_CLANG, the compiler replaces function addresses
> > > in instrumented C code with jump table addresses. This means that
> > > __pa_symbol(function) returns the physical address of the jump table
> > > entry instead of the actual function, which may not work as the jump
> > > table code will immediately jump to a virtual address that may not be
> > > mapped.
> > >
> > > To avoid this address space confusion, this change adds generic
> > > definitions for __va_function and __pa_function, which architectures
> > > that support CFI can override. The typical implementation of the
> > > __va_function macro would use inline assembly to take the function
> > > address, which avoids compiler instrumentation.
> >
> > I think these helper are sensible, but shouldn't they have somewhat
> > less arcane names and proper documentation?
> 
> Good point, I'll add comments in the next version. I thought
> __pa_function would be a fairly straightforward replacement for
> __pa_symbol, but I'm fine with renaming these. Any suggestions for
> less arcane names?

I think dropping 'nocfi' into the name would be clear enough. I think
that given the usual fun with {symbol,module,virt}->phys conversions
it's not worth having the __pa_* form, and we can leave the phys
conversion to the caller that knows where the function lives.

How about we just add `function_nocfi()` ?

Callers can then do `__pa_symbol(function_nocfi(foo))` and similar.

Thanks,
Mark.
Sami Tolvanen March 25, 2021, 11:17 p.m. UTC | #4
On Thu, Mar 25, 2021 at 3:17 AM Mark Rutland <mark.rutland@arm.com> wrote:
>
> On Wed, Mar 24, 2021 at 08:54:18AM -0700, Sami Tolvanen wrote:
> > On Wed, Mar 24, 2021 at 12:14 AM Christoph Hellwig <hch@infradead.org> wrote:
> > >
> > > On Tue, Mar 23, 2021 at 01:39:32PM -0700, Sami Tolvanen wrote:
> > > > With CONFIG_CFI_CLANG, the compiler replaces function addresses
> > > > in instrumented C code with jump table addresses. This means that
> > > > __pa_symbol(function) returns the physical address of the jump table
> > > > entry instead of the actual function, which may not work as the jump
> > > > table code will immediately jump to a virtual address that may not be
> > > > mapped.
> > > >
> > > > To avoid this address space confusion, this change adds generic
> > > > definitions for __va_function and __pa_function, which architectures
> > > > that support CFI can override. The typical implementation of the
> > > > __va_function macro would use inline assembly to take the function
> > > > address, which avoids compiler instrumentation.
> > >
> > > I think these helper are sensible, but shouldn't they have somewhat
> > > less arcane names and proper documentation?
> >
> > Good point, I'll add comments in the next version. I thought
> > __pa_function would be a fairly straightforward replacement for
> > __pa_symbol, but I'm fine with renaming these. Any suggestions for
> > less arcane names?
>
> I think dropping 'nocfi' into the name would be clear enough. I think
> that given the usual fun with {symbol,module,virt}->phys conversions
> it's not worth having the __pa_* form, and we can leave the phys
> conversion to the caller that knows where the function lives.
>
> How about we just add `function_nocfi()` ?
>
> Callers can then do `__pa_symbol(function_nocfi(foo))` and similar.

Sounds reasonable. I'll drop __pa_function() and rename
__va_function() to function_nocfi() in the next version.

Sami
diff mbox series

Patch

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 64a71bf20536..a0d285cd59ce 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -116,6 +116,14 @@  extern int mmap_rnd_compat_bits __read_mostly;
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif
 
+#ifndef __va_function
+#define __va_function(x) (x)
+#endif
+
+#ifndef __pa_function
+#define __pa_function(x) __pa_symbol(__va_function(x))
+#endif
+
 #ifndef page_to_virt
 #define page_to_virt(x)	__va(PFN_PHYS(page_to_pfn(x)))
 #endif