Re: [RFC PATCH v2 27/27] x86/cet: Add arch_prctl functions for CET

From: Yu-cheng Yu
Date: Wed Jul 11 2018 - 16:59:26 EST


On Wed, 2018-07-11 at 12:45 -0700, Jann Horn wrote:
> On Tue, Jul 10, 2018 at 3:31 PM Yu-cheng Yu <yu-cheng.yu@xxxxxxxxx>
> wrote:
> >
> >
> > arch_prctl(ARCH_CET_STATUS, unsigned long *addr)
> > ÂÂÂÂReturn CET feature status.
> >
> > ÂÂÂÂThe parameter 'addr' is a pointer to a user buffer.
> > ÂÂÂÂOn returning to the caller, the kernel fills the following
> > ÂÂÂÂinformation:
> >
> > ÂÂÂÂ*addr = SHSTK/IBT status
> > ÂÂÂÂ*(addr + 1) = SHSTK base address
> > ÂÂÂÂ*(addr + 2) = SHSTK size
> >
> > arch_prctl(ARCH_CET_DISABLE, unsigned long features)
> > ÂÂÂÂDisable SHSTK and/or IBT specified in 'features'.ÂÂReturn
> > -EPERM
> > ÂÂÂÂif CET is locked out.
> >
> > arch_prctl(ARCH_CET_LOCK)
> > ÂÂÂÂLock out CET feature.
> >
> > arch_prctl(ARCH_CET_ALLOC_SHSTK, unsigned long *addr)
> > ÂÂÂÂAllocate a new SHSTK.
> >
> > ÂÂÂÂThe parameter 'addr' is a pointer to a user buffer and
> > indicates
> > ÂÂÂÂthe desired SHSTK size to allocate.ÂÂOn returning to the caller
> > ÂÂÂÂthe buffer contains the address of the new SHSTK.
> >
> > arch_prctl(ARCH_CET_LEGACY_BITMAP, unsigned long *addr)
> > ÂÂÂÂAllocate an IBT legacy code bitmap if the current task does not
> > ÂÂÂÂhave one.
> >
> > ÂÂÂÂThe parameter 'addr' is a pointer to a user buffer.
> > ÂÂÂÂOn returning to the caller, the kernel fills the following
> > ÂÂÂÂinformation:
> >
> > ÂÂÂÂ*addr = IBT bitmap base address
> > ÂÂÂÂ*(addr + 1) = IBT bitmap size
> >
> > Signed-off-by: H.J. Lu <hjl.tools@xxxxxxxxx>
> > Signed-off-by: Yu-cheng Yu <yu-cheng.yu@xxxxxxxxx>
> [...]
> >
> > diff --git a/arch/x86/kernel/cet_prctl.c
> > b/arch/x86/kernel/cet_prctl.c
> > new file mode 100644
> > index 000000000000..86bb78ae656d
> > --- /dev/null
> > +++ b/arch/x86/kernel/cet_prctl.c
> > @@ -0,0 +1,141 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +
> > +#include <linux/errno.h>
> > +#include <linux/uaccess.h>
> > +#include <linux/prctl.h>
> > +#include <linux/compat.h>
> > +#include <asm/processor.h>
> > +#include <asm/prctl.h>
> > +#include <asm/elf.h>
> > +#include <asm/elf_property.h>
> > +#include <asm/cet.h>
> > +
> > +/* See Documentation/x86/intel_cet.txt. */
> > +
> > +static int handle_get_status(unsigned long arg2)
> > +{
> > +ÂÂÂÂÂÂÂunsigned int features = 0;
> > +ÂÂÂÂÂÂÂunsigned long shstk_base, shstk_size;
> > +
> > +ÂÂÂÂÂÂÂif (current->thread.cet.shstk_enabled)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfeatures |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
> > +ÂÂÂÂÂÂÂif (current->thread.cet.ibt_enabled)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfeatures |= GNU_PROPERTY_X86_FEATURE_1_IBT;
> > +
> > +ÂÂÂÂÂÂÂshstk_base = current->thread.cet.shstk_base;
> > +ÂÂÂÂÂÂÂshstk_size = current->thread.cet.shstk_size;
> > +
> > +ÂÂÂÂÂÂÂif (in_ia32_syscall()) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂunsigned int buf[3];
> > +
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbuf[0] = features;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbuf[1] = (unsigned int)shstk_base;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbuf[2] = (unsigned int)shstk_size;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn copy_to_user((unsigned int __user *)arg2,
> > buf,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂsizeof(buf));
> > +ÂÂÂÂÂÂÂ} else {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂunsigned long buf[3];
> > +
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbuf[0] = (unsigned long)features;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbuf[1] = shstk_base;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbuf[2] = shstk_size;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn copy_to_user((unsigned long __user *)arg2,
> > buf,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂsizeof(buf));
> > +ÂÂÂÂÂÂÂ}
> Other places in the kernel (e.g. the BPF subsystem) just
> unconditionally use u64 instead of unsigned long to avoid having to
> switch between different sizes. I wonder whether that would make
> sense
> here?

Yes, that simplifies the code. ÂI will make that change.

Yu-cheng