All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Lutomirski <luto@kernel.org>
To: Dave Hansen <dave.hansen@intel.com>
Cc: Andrew Lutomirski <luto@kernel.org>,
	Florian Weimer <fweimer@redhat.com>,
	Linux-MM <linux-mm@kvack.org>,
	Linux API <linux-api@vger.kernel.org>,
	linux-x86_64@vger.kernel.org,
	linux-arch <linux-arch@vger.kernel.org>, X86 ML <x86@kernel.org>,
	linuxram@us.ibm.com
Subject: Re: [PATCH] pkeys: Introduce PKEY_ALLOC_SIGNALINHERIT and change signal semantics
Date: Wed, 02 May 2018 22:22:59 +0000	[thread overview]
Message-ID: <CALCETrUM7wWZh55gaLiAoPqtxLLUJ4QC8r8zj62E9avJ6ZVu0w@mail.gmail.com> (raw)
In-Reply-To: <a37b7deb-7f5a-3dfa-f360-956cab8a813a@intel.com>

On Wed, May 2, 2018 at 3:08 PM Dave Hansen <dave.hansen@intel.com> wrote:

> On 05/02/2018 02:23 PM, Andy Lutomirski wrote:
> >> The kernel could do *something*, probably along the membarrier system
> >> call.  I mean, I could implement a reasonable close approximation in
> >> userspace, via the setxid mechanism in glibc (but I really don't want
to).
> >
> > I beg to differ.
> >
> > Thread A:
> > old = RDPKRU();
> > WRPKRU(old & ~3);
> > ...
> > WRPKRU(old);
> >
> > Thread B:
> > pkey_alloc().
> >
> > If pkey_alloc() happens while thread A is in the ... part, you lose.  It
> > makes no difference what the kernel does.  The problem is that the
WRPKRU
> > instruction itself is designed incorrectly.

> Yes, *if* we define pkey_alloc() to be implicitly changing other
> threads' PKRU value.

I think that's the only generally useful behavior.  In general, if
libraries are going to use protection keys, they're going to do this:

int key = pkey_alloc(...);
mmap() or mprotect_key() some memory (for crypto secrets, a database,
whatever).
...
enable_write_access(key);
write to the memory;
disable_write_access(key);

That library wants other threads, signal handlers, and, in general, the
whole rest of the process to be restricted, and that library doesn't want
race conditions.  The problem here is that, to get this right, we either
need the PKRU modifications to be syscalls or to take locks, and the lock
approach is going to be fairly gross.


> Let's say we go to the hardware guys and ask for a new instruction to
> fix this.  We're going to have to make a pretty good case that this is
> either impossible or really hard to do in software.

> Surely we have the locking to tell another thread that we want its PKRU
> value to change without actively going out and having the kernel poke a
> new value in.

I think that doing it in userspace without a new instruction is going to be
slow, ugly, unreliable, or more than one of the above.

Here's my proposal.  We ask the hardware folks for new instructions.  Once
we get tentative agreement, we add new vDSO pkey accessors.  At first those
accessors function will do a syscall.  When we get the new instructions,
they get alternatives.  The vDSO helpers could look like this:

typedef struct { u64 opaque; } pkey_save_t;

pkey_save_t pkey_save_and_set(int key, unsigned int mode);
void pkey_set(int key, unsigned int mode);
void pkey_restore(int key, pkey_save_t prev);

Slight variants are possible.  On new hardware, pkey_set() and
pkey_restore() use the WRPKRU replacement.  pkey_save_and_set() uses the
RDPKRU replacement followed by the WRPKRU replacement.  The usage is:

pkey_save_t prev = pkey_save_and_set(my_key, 0);  /* enable read and write
*/
do something with memory;
pkey_restore(my_key, prev);

or just:

pkey_set(my_key, 0);
...
pkey_set(my_key, 3);  /* or 1 or 2 depending on the application */

And we make it very, very clear to user code that using RDPKRU and WRPKRU
directly is a big no-no.

What do you all think?  This gives us sane, if slow, behavior for current
CPUs and is decently fast on hypothetical new CPUs.

  reply	other threads:[~2018-05-02 22:23 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-02 13:26 [PATCH] pkeys: Introduce PKEY_ALLOC_SIGNALINHERIT and change signal semantics Florian Weimer
2018-05-02 14:30 ` Dave Hansen
2018-05-02 15:12   ` Florian Weimer
2018-05-02 15:12     ` Florian Weimer
2018-05-02 15:28     ` Dave Hansen
2018-05-02 15:28       ` Dave Hansen
2018-05-02 21:08       ` Florian Weimer
2018-05-02 22:03         ` Dave Hansen
2018-05-02 22:03           ` Dave Hansen
2018-05-07  9:47           ` Florian Weimer
2018-05-07  9:47             ` Florian Weimer
2018-05-02 17:09     ` Andy Lutomirski
2018-05-02 17:17       ` Florian Weimer
2018-05-02 17:17         ` Florian Weimer
2018-05-02 20:41         ` Andy Lutomirski
2018-05-02 21:06           ` Florian Weimer
2018-05-02 21:06             ` Florian Weimer
2018-05-02 21:23             ` Andy Lutomirski
2018-05-02 22:08               ` Dave Hansen
2018-05-02 22:22                 ` Andy Lutomirski [this message]
2018-05-02 22:32                   ` Dave Hansen
2018-05-02 23:32                     ` Andy Lutomirski
2018-05-02 23:58                       ` Dave Hansen
2018-05-02 23:58                         ` Dave Hansen
2018-05-03  1:14                         ` Andy Lutomirski
2018-05-03 14:42                           ` Dave Hansen
2018-05-03 14:42                             ` Dave Hansen
2018-05-03 14:42                           ` Florian Weimer
2018-05-03 14:42                             ` Florian Weimer
2018-05-03  2:10               ` Ram Pai
2018-05-03  4:05                 ` Andy Lutomirski
2018-05-07  9:48                   ` Florian Weimer
2018-05-08  2:49                     ` Andy Lutomirski
2018-05-08  2:49                       ` Andy Lutomirski
2018-05-08 12:40                       ` Florian Weimer
2018-05-08 12:40                         ` Florian Weimer
2018-05-09 14:41                         ` Andy Lutomirski
2018-05-09 14:41                           ` Andy Lutomirski
2018-05-14 12:01                           ` Florian Weimer
2018-05-14 12:01                             ` Florian Weimer
2018-05-14 15:32                             ` Andy Lutomirski
2018-05-14 15:32                               ` Andy Lutomirski
2018-05-14 15:32                               ` Andy Lutomirski
2018-05-14 15:34                               ` Florian Weimer
2018-05-14 15:34                                 ` Florian Weimer
2018-05-14 15:34                                 ` Florian Weimer
2018-05-16 17:01                                 ` Dave Hansen
2018-05-16 17:01                                   ` Dave Hansen
2018-05-16 17:01                                   ` Dave Hansen
2018-05-16 20:52                             ` Ram Pai
2018-05-16 20:52                               ` Ram Pai
2018-05-16 20:54                               ` Andy Lutomirski
2018-05-16 20:54                                 ` Andy Lutomirski
2018-05-16 20:35                         ` Ram Pai
2018-05-16 20:35                           ` Ram Pai
2018-05-16 20:37                           ` Andy Lutomirski
2018-05-16 20:37                             ` Andy Lutomirski
2018-05-16 21:07                             ` Ram Pai
2018-05-16 21:07                               ` Ram Pai
2018-05-17 10:09                               ` Florian Weimer
2018-05-17 10:09                                 ` Florian Weimer
2018-05-17 10:11                           ` Florian Weimer
2018-05-17 10:11                             ` Florian Weimer
2018-05-03 14:37               ` Florian Weimer
2018-05-02 21:12     ` Ram Pai
2018-05-02 21:12       ` Ram Pai
2018-05-02 21:18       ` Andy Lutomirski
2018-05-02 23:38         ` Ram Pai
2018-05-02 23:38           ` Ram Pai
2018-05-07  9:47           ` Florian Weimer
2018-05-07  9:47             ` Florian Weimer
2018-05-07  9:43         ` Florian Weimer

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=CALCETrUM7wWZh55gaLiAoPqtxLLUJ4QC8r8zj62E9avJ6ZVu0w@mail.gmail.com \
    --to=luto@kernel.org \
    --cc=dave.hansen@intel.com \
    --cc=fweimer@redhat.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-x86_64@vger.kernel.org \
    --cc=linuxram@us.ibm.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.