mbox series

[v6,00/13] Enroll kernel keys thru MOK

Message ID 20210914211416.34096-1-eric.snowberg@oracle.com (mailing list archive)
Headers show
Series Enroll kernel keys thru MOK | expand

Message

Eric Snowberg Sept. 14, 2021, 9:14 p.m. UTC
Back in 2013 Linus requested a feature to allow end-users to have the 
ability "to add their own keys and sign modules they trust". This was
his *second* order outlined here [1]. There have been many attempts 
over the years to solve this problem, all have been rejected.  Many 
of the failed attempts loaded all preboot firmware keys into the kernel,
including the Secure Boot keys. Many distributions carry one of these 
rejected attempts [2], [3], [4]. This series tries to solve this problem 
with a solution that takes into account all the problems brought up in 
the previous attempts.

On UEFI based systems, this series introduces a new Linux kernel keyring 
containing the Machine Owner Keys (MOK) called machine. It also defines
a new MOK variable in shim. This variable allows the end-user to decide 
if they want to load MOK keys into the machine keyring. Mimi has suggested 
that only CA keys contained within the MOK be loaded into the machine 
keyring. All other certs will load into the platform keyring instead.

By default, nothing changes; MOK keys are not loaded into the machine
keyring.  They are only loaded after the end-user makes the decision 
themselves.  The end-user would set this through mokutil using a new 
--trust-mok option [5]. This would work similar to how the kernel uses 
MOK variables to enable/disable signature validation as well as use/ignore 
the db. Any kernel operation that uses either the builtin or secondary 
trusted keys as a trust source shall also reference the new machine 
keyring as a trust source.

Secure Boot keys will never be loaded into the machine keyring.  They
will always be loaded into the platform keyring.  If an end-user wanted 
to load one, they would need to enroll it into the MOK.

Steps required by the end user:

Sign kernel module with user created key:
$ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
   machine_signing_key.priv machine_signing_key.x509 my_module.ko

Import the key into the MOK
$ mokutil --import machine_signing_key.x509

Setup the kernel to load MOK keys into the .machine keyring
$ mokutil --trust-mok

Then reboot, the MokManager will load and ask if you want to trust the
MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
module will load.

I have included links to both the mokutil [5] and shim [6] changes I
have made to support this new functionality.

[1] https://marc.info/?l=linux-kernel&m=136185386310140&w=2
[2] https://lore.kernel.org/lkml/1479737095.2487.34.camel@linux.vnet.ibm.com/
[3] https://lore.kernel.org/lkml/1556221605.24945.3.camel@HansenPartnership.com/
[4] https://lore.kernel.org/linux-integrity/1e41f22b1f11784f1e943f32bf62034d4e054cdb.camel@HansenPartnership.com/
[5] https://github.com/esnowberg/mokutil/tree/0.3.0-mokvars-v2
[6] https://github.com/esnowberg/shim/tree/mokvars-v2

Eric Snowberg (13):
  integrity: Introduce a Linux keyring called machine
  integrity: Do not allow machine keyring updates following init
  KEYS: CA link restriction
  integrity: restrict INTEGRITY_KEYRING_MACHINE to restrict_link_by_ca
  integrity: add new keyring handler for mok keys
  KEYS: Rename get_builtin_and_secondary_restriction
  KEYS: add a reference to machine keyring
  KEYS: Introduce link restriction for machine keys
  KEYS: integrity: change link restriction to trust the machine keyring
  KEYS: link secondary_trusted_keys to machine trusted keys
  integrity: store reference to machine keyring
  integrity: Trust MOK keys if MokListTrustedRT found
  integrity: Only use machine keyring when uefi_check_trust_mok_keys is
    true

 certs/system_keyring.c                        | 44 +++++++++-
 crypto/asymmetric_keys/restrict.c             | 40 +++++++++
 include/crypto/public_key.h                   |  5 ++
 include/keys/system_keyring.h                 | 14 +++
 security/integrity/Kconfig                    | 12 +++
 security/integrity/Makefile                   |  1 +
 security/integrity/digsig.c                   | 23 ++++-
 security/integrity/integrity.h                | 17 +++-
 .../platform_certs/keyring_handler.c          | 17 +++-
 .../platform_certs/keyring_handler.h          |  5 ++
 security/integrity/platform_certs/load_uefi.c |  4 +-
 .../platform_certs/machine_keyring.c          | 85 +++++++++++++++++++
 12 files changed, 257 insertions(+), 10 deletions(-)
 create mode 100644 security/integrity/platform_certs/machine_keyring.c


base-commit: 6880fa6c56601bb8ed59df6c30fd390cc5f6dd8f

Comments

Jarkko Sakkinen Sept. 15, 2021, 5:57 p.m. UTC | #1
On Tue, 2021-09-14 at 17:14 -0400, Eric Snowberg wrote:
> Back in 2013 Linus requested a feature to allow end-users to have the 
> ability "to add their own keys and sign modules they trust". This was
> his *second* order outlined here [1]. There have been many attempts 
> over the years to solve this problem, all have been rejected.  Many 
> of the failed attempts loaded all preboot firmware keys into the kernel,
> including the Secure Boot keys. Many distributions carry one of these 
> rejected attempts [2], [3], [4]. This series tries to solve this problem 
> with a solution that takes into account all the problems brought up in 
> the previous attempts.
> 
> On UEFI based systems, this series introduces a new Linux kernel keyring 
> containing the Machine Owner Keys (MOK) called machine. It also defines
> a new MOK variable in shim. This variable allows the end-user to decide 
> if they want to load MOK keys into the machine keyring. Mimi has suggested 
> that only CA keys contained within the MOK be loaded into the machine 
> keyring. All other certs will load into the platform keyring instead.
> 
> By default, nothing changes; MOK keys are not loaded into the machine
> keyring.  They are only loaded after the end-user makes the decision 
> themselves.  The end-user would set this through mokutil using a new 
> --trust-mok option [5]. This would work similar to how the kernel uses 
> MOK variables to enable/disable signature validation as well as use/ignore 
> the db. Any kernel operation that uses either the builtin or secondary 
> trusted keys as a trust source shall also reference the new machine 
> keyring as a trust source.
> 
> Secure Boot keys will never be loaded into the machine keyring.  They
> will always be loaded into the platform keyring.  If an end-user wanted 
> to load one, they would need to enroll it into the MOK.
> 
> Steps required by the end user:
> 
> Sign kernel module with user created key:
> $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
>    machine_signing_key.priv machine_signing_key.x509 my_module.ko
> 
> Import the key into the MOK
> $ mokutil --import machine_signing_key.x509
> 
> Setup the kernel to load MOK keys into the .machine keyring
> $ mokutil --trust-mok
> 
> Then reboot, the MokManager will load and ask if you want to trust the
> MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
> module will load.
> 
> I have included links to both the mokutil [5] and shim [6] changes I
> have made to support this new functionality.

How hard it is to self-compile shim and boot it with QEMU (I
do not know even the GIT location of Shim)?

I'm all my SGX testing already with TianoCore and QEMU so I
thought it might not be that huge stretch to get testing env
for this.

/Jarkko
Eric Snowberg Sept. 15, 2021, 9:28 p.m. UTC | #2
> On Sep 15, 2021, at 11:57 AM, Jarkko Sakkinen <jarkko@kernel.org> wrote:
> 
> On Tue, 2021-09-14 at 17:14 -0400, Eric Snowberg wrote:
>> Back in 2013 Linus requested a feature to allow end-users to have the 
>> ability "to add their own keys and sign modules they trust". This was
>> his *second* order outlined here [1]. There have been many attempts 
>> over the years to solve this problem, all have been rejected.  Many 
>> of the failed attempts loaded all preboot firmware keys into the kernel,
>> including the Secure Boot keys. Many distributions carry one of these 
>> rejected attempts [2], [3], [4]. This series tries to solve this problem 
>> with a solution that takes into account all the problems brought up in 
>> the previous attempts.
>> 
>> On UEFI based systems, this series introduces a new Linux kernel keyring 
>> containing the Machine Owner Keys (MOK) called machine. It also defines
>> a new MOK variable in shim. This variable allows the end-user to decide 
>> if they want to load MOK keys into the machine keyring. Mimi has suggested 
>> that only CA keys contained within the MOK be loaded into the machine 
>> keyring. All other certs will load into the platform keyring instead.
>> 
>> By default, nothing changes; MOK keys are not loaded into the machine
>> keyring.  They are only loaded after the end-user makes the decision 
>> themselves.  The end-user would set this through mokutil using a new 
>> --trust-mok option [5]. This would work similar to how the kernel uses 
>> MOK variables to enable/disable signature validation as well as use/ignore 
>> the db. Any kernel operation that uses either the builtin or secondary 
>> trusted keys as a trust source shall also reference the new machine 
>> keyring as a trust source.
>> 
>> Secure Boot keys will never be loaded into the machine keyring.  They
>> will always be loaded into the platform keyring.  If an end-user wanted 
>> to load one, they would need to enroll it into the MOK.
>> 
>> Steps required by the end user:
>> 
>> Sign kernel module with user created key:
>> $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
>>   machine_signing_key.priv machine_signing_key.x509 my_module.ko
>> 
>> Import the key into the MOK
>> $ mokutil --import machine_signing_key.x509
>> 
>> Setup the kernel to load MOK keys into the .machine keyring
>> $ mokutil --trust-mok
>> 
>> Then reboot, the MokManager will load and ask if you want to trust the
>> MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
>> module will load.
>> 
>> I have included links to both the mokutil [5] and shim [6] changes I
>> have made to support this new functionality.
> 
> How hard it is to self-compile shim and boot it with QEMU (I
> do not know even the GIT location of Shim)?

It is not hard, that is the setup I use for my testing.  Upstream shim 
is located here [1].  Or you can use my repo which contains the necessary
changes [2].

[1] https://github.com/rhboot/shim
[2] https://github.com/esnowberg/shim/tree/mokvars-v2
Jarkko Sakkinen Sept. 16, 2021, 3:15 p.m. UTC | #3
On Wed, 2021-09-15 at 15:28 -0600, Eric Snowberg wrote:
> > On Sep 15, 2021, at 11:57 AM, Jarkko Sakkinen <jarkko@kernel.org> wrote:
> > 
> > On Tue, 2021-09-14 at 17:14 -0400, Eric Snowberg wrote:
> > > Back in 2013 Linus requested a feature to allow end-users to have the 
> > > ability "to add their own keys and sign modules they trust". This was
> > > his *second* order outlined here [1]. There have been many attempts 
> > > over the years to solve this problem, all have been rejected.  Many 
> > > of the failed attempts loaded all preboot firmware keys into the kernel,
> > > including the Secure Boot keys. Many distributions carry one of these 
> > > rejected attempts [2], [3], [4]. This series tries to solve this problem 
> > > with a solution that takes into account all the problems brought up in 
> > > the previous attempts.
> > > 
> > > On UEFI based systems, this series introduces a new Linux kernel keyring 
> > > containing the Machine Owner Keys (MOK) called machine. It also defines
> > > a new MOK variable in shim. This variable allows the end-user to decide 
> > > if they want to load MOK keys into the machine keyring. Mimi has suggested 
> > > that only CA keys contained within the MOK be loaded into the machine 
> > > keyring. All other certs will load into the platform keyring instead.
> > > 
> > > By default, nothing changes; MOK keys are not loaded into the machine
> > > keyring.  They are only loaded after the end-user makes the decision 
> > > themselves.  The end-user would set this through mokutil using a new 
> > > --trust-mok option [5]. This would work similar to how the kernel uses 
> > > MOK variables to enable/disable signature validation as well as use/ignore 
> > > the db. Any kernel operation that uses either the builtin or secondary 
> > > trusted keys as a trust source shall also reference the new machine 
> > > keyring as a trust source.
> > > 
> > > Secure Boot keys will never be loaded into the machine keyring.  They
> > > will always be loaded into the platform keyring.  If an end-user wanted 
> > > to load one, they would need to enroll it into the MOK.
> > > 
> > > Steps required by the end user:
> > > 
> > > Sign kernel module with user created key:
> > > $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
> > >   machine_signing_key.priv machine_signing_key.x509 my_module.ko
> > > 
> > > Import the key into the MOK
> > > $ mokutil --import machine_signing_key.x509
> > > 
> > > Setup the kernel to load MOK keys into the .machine keyring
> > > $ mokutil --trust-mok
> > > 
> > > Then reboot, the MokManager will load and ask if you want to trust the
> > > MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
> > > module will load.
> > > 
> > > I have included links to both the mokutil [5] and shim [6] changes I
> > > have made to support this new functionality.
> > 
> > How hard it is to self-compile shim and boot it with QEMU (I
> > do not know even the GIT location of Shim)?
> 
> It is not hard, that is the setup I use for my testing.  Upstream shim 
> is located here [1].  Or you can use my repo which contains the necessary
> changes [2].
> 
> [1] https://github.com/rhboot/shim
> [2] https://github.com/esnowberg/shim/tree/mokvars-v2
> 

So, my 2nd Q would be: which order these should be upstreamed?

Linux patch set cannot depend on "yet to be upstreamed" things.

Code changes look good enough to me.

/Jarkko
Nayna Sept. 16, 2021, 8:03 p.m. UTC | #4
On 9/14/21 5:14 PM, Eric Snowberg wrote:
> Back in 2013 Linus requested a feature to allow end-users to have the
> ability "to add their own keys and sign modules they trust". This was
> his *second* order outlined here [1]. There have been many attempts
> over the years to solve this problem, all have been rejected.  Many
> of the failed attempts loaded all preboot firmware keys into the kernel,
> including the Secure Boot keys. Many distributions carry one of these
> rejected attempts [2], [3], [4]. This series tries to solve this problem
> with a solution that takes into account all the problems brought up in
> the previous attempts.
>
> On UEFI based systems, this series introduces a new Linux kernel keyring
> containing the Machine Owner Keys (MOK) called machine. It also defines
> a new MOK variable in shim. This variable allows the end-user to decide
> if they want to load MOK keys into the machine keyring. Mimi has suggested
> that only CA keys contained within the MOK be loaded into the machine
> keyring. All other certs will load into the platform keyring instead.
>
> By default, nothing changes; MOK keys are not loaded into the machine
> keyring.  They are only loaded after the end-user makes the decision
> themselves.  The end-user would set this through mokutil using a new
> --trust-mok option [5]. This would work similar to how the kernel uses
> MOK variables to enable/disable signature validation as well as use/ignore
> the db. Any kernel operation that uses either the builtin or secondary
> trusted keys as a trust source shall also reference the new machine
> keyring as a trust source.
>
> Secure Boot keys will never be loaded into the machine keyring.  They
> will always be loaded into the platform keyring.  If an end-user wanted
> to load one, they would need to enroll it into the MOK.
>
> Steps required by the end user:
>
> Sign kernel module with user created key:
> $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
>     machine_signing_key.priv machine_signing_key.x509 my_module.ko
>
> Import the key into the MOK
> $ mokutil --import machine_signing_key.x509
>
> Setup the kernel to load MOK keys into the .machine keyring
> $ mokutil --trust-mok
>
> Then reboot, the MokManager will load and ask if you want to trust the
> MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
> module will load.

machine_signing_key.x509 appears to be a code-signing, self-signed key.  
It's not a CA key, but the intent of the patchset is to load only CA 
keys to .machine keyring.

Shouldn't there be two steps: one to load the CA key into MOK, and a 
second one to load the code-signing key which is signed by this CA ?

Thanks & Regards,

       - Nayna
Peter Jones Sept. 16, 2021, 10:14 p.m. UTC | #5
On Thu, Sep 16, 2021 at 06:15:50PM +0300, Jarkko Sakkinen wrote:
> On Wed, 2021-09-15 at 15:28 -0600, Eric Snowberg wrote:
> > > On Sep 15, 2021, at 11:57 AM, Jarkko Sakkinen <jarkko@kernel.org> wrote:
> > > 
> > > On Tue, 2021-09-14 at 17:14 -0400, Eric Snowberg wrote:
> > > > Back in 2013 Linus requested a feature to allow end-users to have the 
> > > > ability "to add their own keys and sign modules they trust". This was
> > > > his *second* order outlined here [1]. There have been many attempts 
> > > > over the years to solve this problem, all have been rejected.  Many 
> > > > of the failed attempts loaded all preboot firmware keys into the kernel,
> > > > including the Secure Boot keys. Many distributions carry one of these 
> > > > rejected attempts [2], [3], [4]. This series tries to solve this problem 
> > > > with a solution that takes into account all the problems brought up in 
> > > > the previous attempts.
> > > > 
> > > > On UEFI based systems, this series introduces a new Linux kernel keyring 
> > > > containing the Machine Owner Keys (MOK) called machine. It also defines
> > > > a new MOK variable in shim. This variable allows the end-user to decide 
> > > > if they want to load MOK keys into the machine keyring. Mimi has suggested 
> > > > that only CA keys contained within the MOK be loaded into the machine 
> > > > keyring. All other certs will load into the platform keyring instead.
> > > > 
> > > > By default, nothing changes; MOK keys are not loaded into the machine
> > > > keyring.  They are only loaded after the end-user makes the decision 
> > > > themselves.  The end-user would set this through mokutil using a new 
> > > > --trust-mok option [5]. This would work similar to how the kernel uses 
> > > > MOK variables to enable/disable signature validation as well as use/ignore 
> > > > the db. Any kernel operation that uses either the builtin or secondary 
> > > > trusted keys as a trust source shall also reference the new machine 
> > > > keyring as a trust source.
> > > > 
> > > > Secure Boot keys will never be loaded into the machine keyring.  They
> > > > will always be loaded into the platform keyring.  If an end-user wanted 
> > > > to load one, they would need to enroll it into the MOK.
> > > > 
> > > > Steps required by the end user:
> > > > 
> > > > Sign kernel module with user created key:
> > > > $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
> > > >   machine_signing_key.priv machine_signing_key.x509 my_module.ko
> > > > 
> > > > Import the key into the MOK
> > > > $ mokutil --import machine_signing_key.x509
> > > > 
> > > > Setup the kernel to load MOK keys into the .machine keyring
> > > > $ mokutil --trust-mok
> > > > 
> > > > Then reboot, the MokManager will load and ask if you want to trust the
> > > > MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
> > > > module will load.
> > > > 
> > > > I have included links to both the mokutil [5] and shim [6] changes I
> > > > have made to support this new functionality.
> > > 
> > > How hard it is to self-compile shim and boot it with QEMU (I
> > > do not know even the GIT location of Shim)?
> > 
> > It is not hard, that is the setup I use for my testing.  Upstream shim 
> > is located here [1].  Or you can use my repo which contains the necessary
> > changes [2].
> > 
> > [1] https://github.com/rhboot/shim
> > [2] https://github.com/esnowberg/shim/tree/mokvars-v2
> > 
> 
> So, my 2nd Q would be: which order these should be upstreamed?
> 
> Linux patch set cannot depend on "yet to be upstreamed" things.
> 
> Code changes look good enough to me.

We can carry this support in shim before it's in kernel.  Eric's current
patch for shim and mokutil looks mostly reasonable, though I see a few
minor nits we'll have to sort out.
Eric Snowberg Sept. 17, 2021, 1:55 a.m. UTC | #6
> On Sep 16, 2021, at 2:03 PM, Nayna <nayna@linux.vnet.ibm.com> wrote:
> 
> 
> On 9/14/21 5:14 PM, Eric Snowberg wrote:
>> Back in 2013 Linus requested a feature to allow end-users to have the
>> ability "to add their own keys and sign modules they trust". This was
>> his *second* order outlined here [1]. There have been many attempts
>> over the years to solve this problem, all have been rejected.  Many
>> of the failed attempts loaded all preboot firmware keys into the kernel,
>> including the Secure Boot keys. Many distributions carry one of these
>> rejected attempts [2], [3], [4]. This series tries to solve this problem
>> with a solution that takes into account all the problems brought up in
>> the previous attempts.
>> 
>> On UEFI based systems, this series introduces a new Linux kernel keyring
>> containing the Machine Owner Keys (MOK) called machine. It also defines
>> a new MOK variable in shim. This variable allows the end-user to decide
>> if they want to load MOK keys into the machine keyring. Mimi has suggested
>> that only CA keys contained within the MOK be loaded into the machine
>> keyring. All other certs will load into the platform keyring instead.
>> 
>> By default, nothing changes; MOK keys are not loaded into the machine
>> keyring.  They are only loaded after the end-user makes the decision
>> themselves.  The end-user would set this through mokutil using a new
>> --trust-mok option [5]. This would work similar to how the kernel uses
>> MOK variables to enable/disable signature validation as well as use/ignore
>> the db. Any kernel operation that uses either the builtin or secondary
>> trusted keys as a trust source shall also reference the new machine
>> keyring as a trust source.
>> 
>> Secure Boot keys will never be loaded into the machine keyring.  They
>> will always be loaded into the platform keyring.  If an end-user wanted
>> to load one, they would need to enroll it into the MOK.
>> 
>> Steps required by the end user:
>> 
>> Sign kernel module with user created key:
>> $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
>>    machine_signing_key.priv machine_signing_key.x509 my_module.ko
>> 
>> Import the key into the MOK
>> $ mokutil --import machine_signing_key.x509
>> 
>> Setup the kernel to load MOK keys into the .machine keyring
>> $ mokutil --trust-mok
>> 
>> Then reboot, the MokManager will load and ask if you want to trust the
>> MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
>> module will load.
> 
> machine_signing_key.x509 appears to be a code-signing, self-signed key.  It's not a CA key, but the intent of the patchset is to load only CA keys to .machine keyring.
> 
> Shouldn't there be two steps: one to load the CA key into MOK, and a second one to load the code-signing key which is signed by this CA ?

I think that would depend on how the end-user wants to do things. If they 
are just looking to quickly self-sign their own kernel module, I doubt they 
would go through the effort of having two keys.  This is what I tried to 
document in the example above. 

If they do want to use multiple keys, nothing in this approach prevents it.
Their CA will be loaded into the machine keyring.  Now that the machine 
keyring is a new trust source,  they could simply load their intermediate 
or leaf cert signed by their CA into the secondary trusted keyring. Afterwards
their signed kernel module would load.
Eric Snowberg Sept. 17, 2021, 1:58 a.m. UTC | #7
> On Sep 16, 2021, at 4:14 PM, Peter Jones <pjones@redhat.com> wrote:
> 
> On Thu, Sep 16, 2021 at 06:15:50PM +0300, Jarkko Sakkinen wrote:
>> On Wed, 2021-09-15 at 15:28 -0600, Eric Snowberg wrote:
>>>> On Sep 15, 2021, at 11:57 AM, Jarkko Sakkinen <jarkko@kernel.org> wrote:
>>>> 
>>>> On Tue, 2021-09-14 at 17:14 -0400, Eric Snowberg wrote:
>>>>> Back in 2013 Linus requested a feature to allow end-users to have the 
>>>>> ability "to add their own keys and sign modules they trust". This was
>>>>> his *second* order outlined here [1]. There have been many attempts 
>>>>> over the years to solve this problem, all have been rejected.  Many 
>>>>> of the failed attempts loaded all preboot firmware keys into the kernel,
>>>>> including the Secure Boot keys. Many distributions carry one of these 
>>>>> rejected attempts [2], [3], [4]. This series tries to solve this problem 
>>>>> with a solution that takes into account all the problems brought up in 
>>>>> the previous attempts.
>>>>> 
>>>>> On UEFI based systems, this series introduces a new Linux kernel keyring 
>>>>> containing the Machine Owner Keys (MOK) called machine. It also defines
>>>>> a new MOK variable in shim. This variable allows the end-user to decide 
>>>>> if they want to load MOK keys into the machine keyring. Mimi has suggested 
>>>>> that only CA keys contained within the MOK be loaded into the machine 
>>>>> keyring. All other certs will load into the platform keyring instead.
>>>>> 
>>>>> By default, nothing changes; MOK keys are not loaded into the machine
>>>>> keyring.  They are only loaded after the end-user makes the decision 
>>>>> themselves.  The end-user would set this through mokutil using a new 
>>>>> --trust-mok option [5]. This would work similar to how the kernel uses 
>>>>> MOK variables to enable/disable signature validation as well as use/ignore 
>>>>> the db. Any kernel operation that uses either the builtin or secondary 
>>>>> trusted keys as a trust source shall also reference the new machine 
>>>>> keyring as a trust source.
>>>>> 
>>>>> Secure Boot keys will never be loaded into the machine keyring.  They
>>>>> will always be loaded into the platform keyring.  If an end-user wanted 
>>>>> to load one, they would need to enroll it into the MOK.
>>>>> 
>>>>> Steps required by the end user:
>>>>> 
>>>>> Sign kernel module with user created key:
>>>>> $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
>>>>>  machine_signing_key.priv machine_signing_key.x509 my_module.ko
>>>>> 
>>>>> Import the key into the MOK
>>>>> $ mokutil --import machine_signing_key.x509
>>>>> 
>>>>> Setup the kernel to load MOK keys into the .machine keyring
>>>>> $ mokutil --trust-mok
>>>>> 
>>>>> Then reboot, the MokManager will load and ask if you want to trust the
>>>>> MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
>>>>> module will load.
>>>>> 
>>>>> I have included links to both the mokutil [5] and shim [6] changes I
>>>>> have made to support this new functionality.
>>>> 
>>>> How hard it is to self-compile shim and boot it with QEMU (I
>>>> do not know even the GIT location of Shim)?
>>> 
>>> It is not hard, that is the setup I use for my testing.  Upstream shim 
>>> is located here [1].  Or you can use my repo which contains the necessary
>>> changes [2].
>>> 
>>> [1] https://github.com/rhboot/shim
>>> [2] https://github.com/esnowberg/shim/tree/mokvars-v2
>>> 
>> 
>> So, my 2nd Q would be: which order these should be upstreamed?
>> 
>> Linux patch set cannot depend on "yet to be upstreamed" things.
>> 
>> Code changes look good enough to me.
> 
> We can carry this support in shim before it's in kernel.  Eric's current
> patch for shim and mokutil looks mostly reasonable, though I see a few
> minor nits we'll have to sort out.

Thanks Peter.  Previously I had not sent it for a shim review since I wanted 
to make sure this approach had a path forward.  I’ll work on submitting the 
shim changes for review now.
Mimi Zohar Sept. 17, 2021, 6:02 p.m. UTC | #8
On Thu, 2021-09-16 at 19:55 -0600, Eric Snowberg wrote:
> > On Sep 16, 2021, at 2:03 PM, Nayna <nayna@linux.vnet.ibm.com> wrote:
> > 
> > 
> > On 9/14/21 5:14 PM, Eric Snowberg wrote:
> >> Back in 2013 Linus requested a feature to allow end-users to have the
> >> ability "to add their own keys and sign modules they trust". This was
> >> his *second* order outlined here [1]. There have been many attempts
> >> over the years to solve this problem, all have been rejected.  Many
> >> of the failed attempts loaded all preboot firmware keys into the kernel,
> >> including the Secure Boot keys. Many distributions carry one of these
> >> rejected attempts [2], [3], [4]. This series tries to solve this problem
> >> with a solution that takes into account all the problems brought up in
> >> the previous attempts.
> >> 
> >> On UEFI based systems, this series introduces a new Linux kernel keyring
> >> containing the Machine Owner Keys (MOK) called machine. It also defines
> >> a new MOK variable in shim. This variable allows the end-user to decide
> >> if they want to load MOK keys into the machine keyring. Mimi has suggested
> >> that only CA keys contained within the MOK be loaded into the machine
> >> keyring. All other certs will load into the platform keyring instead.
> >> 
> >> By default, nothing changes; MOK keys are not loaded into the machine
> >> keyring.  They are only loaded after the end-user makes the decision
> >> themselves.  The end-user would set this through mokutil using a new
> >> --trust-mok option [5]. This would work similar to how the kernel uses
> >> MOK variables to enable/disable signature validation as well as use/ignore
> >> the db. Any kernel operation that uses either the builtin or secondary
> >> trusted keys as a trust source shall also reference the new machine
> >> keyring as a trust source.
> >> 
> >> Secure Boot keys will never be loaded into the machine keyring.  They
> >> will always be loaded into the platform keyring.  If an end-user wanted
> >> to load one, they would need to enroll it into the MOK.
> >> 
> >> Steps required by the end user:
> >> 
> >> Sign kernel module with user created key:
> >> $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
> >>    machine_signing_key.priv machine_signing_key.x509 my_module.ko
> >> 
> >> Import the key into the MOK
> >> $ mokutil --import machine_signing_key.x509
> >> 
> >> Setup the kernel to load MOK keys into the .machine keyring
> >> $ mokutil --trust-mok
> >> 
> >> Then reboot, the MokManager will load and ask if you want to trust the
> >> MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
> >> module will load.
> > 
> > machine_signing_key.x509 appears to be a code-signing, self-signed key.  It's not a CA key, but the intent of the patchset is to load only CA keys to .machine keyring.
> > 
> > Shouldn't there be two steps: one to load the CA key into MOK, and a second one to load the code-signing key which is signed by this CA ?
> 
> I think that would depend on how the end-user wants to do things. If they 
> are just looking to quickly self-sign their own kernel module, I doubt they 
> would go through the effort of having two keys.  This is what I tried to 
> document in the example above. 

Thank you for providing the example.  We've spent quite a bit of time
discussing CA keys stored in the MOK db being the new root(s) of trust.
While other certificates signed by the new root(s) of trust would be
added to the secondary or IMA keyrings.

At this point, the patch descriptions, and probably code, are written
in terms of CA keys stored in the MOK db, which the self signed
certificate restriction does not require.  With this design, these self
signed certs may be loaded directly onto the IMA keyring.  That's a
problem.  To fix the discrepancy between the cover letter, patch
descriptions and code, all that is needed is to verify that the self-
signed cert is in fact a CA.

thanks,

Mimi
Jarkko Sakkinen Sept. 21, 2021, 9:03 p.m. UTC | #9
On Thu, 2021-09-16 at 18:14 -0400, Peter Jones wrote:
> On Thu, Sep 16, 2021 at 06:15:50PM +0300, Jarkko Sakkinen wrote:
> > On Wed, 2021-09-15 at 15:28 -0600, Eric Snowberg wrote:
> > > > On Sep 15, 2021, at 11:57 AM, Jarkko Sakkinen <jarkko@kernel.org> wrote:
> > > > 
> > > > On Tue, 2021-09-14 at 17:14 -0400, Eric Snowberg wrote:
> > > > > Back in 2013 Linus requested a feature to allow end-users to have the 
> > > > > ability "to add their own keys and sign modules they trust". This was
> > > > > his *second* order outlined here [1]. There have been many attempts 
> > > > > over the years to solve this problem, all have been rejected.  Many 
> > > > > of the failed attempts loaded all preboot firmware keys into the kernel,
> > > > > including the Secure Boot keys. Many distributions carry one of these 
> > > > > rejected attempts [2], [3], [4]. This series tries to solve this problem 
> > > > > with a solution that takes into account all the problems brought up in 
> > > > > the previous attempts.
> > > > > 
> > > > > On UEFI based systems, this series introduces a new Linux kernel keyring 
> > > > > containing the Machine Owner Keys (MOK) called machine. It also defines
> > > > > a new MOK variable in shim. This variable allows the end-user to decide 
> > > > > if they want to load MOK keys into the machine keyring. Mimi has suggested 
> > > > > that only CA keys contained within the MOK be loaded into the machine 
> > > > > keyring. All other certs will load into the platform keyring instead.
> > > > > 
> > > > > By default, nothing changes; MOK keys are not loaded into the machine
> > > > > keyring.  They are only loaded after the end-user makes the decision 
> > > > > themselves.  The end-user would set this through mokutil using a new 
> > > > > --trust-mok option [5]. This would work similar to how the kernel uses 
> > > > > MOK variables to enable/disable signature validation as well as use/ignore 
> > > > > the db. Any kernel operation that uses either the builtin or secondary 
> > > > > trusted keys as a trust source shall also reference the new machine 
> > > > > keyring as a trust source.
> > > > > 
> > > > > Secure Boot keys will never be loaded into the machine keyring.  They
> > > > > will always be loaded into the platform keyring.  If an end-user wanted 
> > > > > to load one, they would need to enroll it into the MOK.
> > > > > 
> > > > > Steps required by the end user:
> > > > > 
> > > > > Sign kernel module with user created key:
> > > > > $ /usr/src/kernels/$(uname -r)/scripts/sign-file sha512 \
> > > > >   machine_signing_key.priv machine_signing_key.x509 my_module.ko
> > > > > 
> > > > > Import the key into the MOK
> > > > > $ mokutil --import machine_signing_key.x509
> > > > > 
> > > > > Setup the kernel to load MOK keys into the .machine keyring
> > > > > $ mokutil --trust-mok
> > > > > 
> > > > > Then reboot, the MokManager will load and ask if you want to trust the
> > > > > MOK key and enroll the MOK into the MOKList.  Afterwards the signed kernel
> > > > > module will load.
> > > > > 
> > > > > I have included links to both the mokutil [5] and shim [6] changes I
> > > > > have made to support this new functionality.
> > > > 
> > > > How hard it is to self-compile shim and boot it with QEMU (I
> > > > do not know even the GIT location of Shim)?
> > > 
> > > It is not hard, that is the setup I use for my testing.  Upstream shim 
> > > is located here [1].  Or you can use my repo which contains the necessary
> > > changes [2].
> > > 
> > > [1] https://github.com/rhboot/shim
> > > [2] https://github.com/esnowberg/shim/tree/mokvars-v2
> > > 
> > 
> > So, my 2nd Q would be: which order these should be upstreamed?
> > 
> > Linux patch set cannot depend on "yet to be upstreamed" things.
> > 
> > Code changes look good enough to me.
> 
> We can carry this support in shim before it's in kernel.  Eric's current
> patch for shim and mokutil looks mostly reasonable, though I see a few
> minor nits we'll have to sort out.

I would revisit this patch set after there is an official shim release
out containing the new API. No  kernel patches, which depend on any
non-upstream changes, can be rightfully reviewed.

/Jarkko