Keyrings: How to make them more useful
Subsystems (10)
David Howells
maintainerHi Jarkko, Steve, Chuck, Mimi, et al.,
I think work needs to be done on the keyrings subsystem to make them more useful for network filesystems and other kernel services such as TLS and crypto.
There are a number of issues that I think need addressing:
(1) One of the flaws in the initial design is that whilst keys have a type (which is necessary), this has to be specified as part of the lookup or the search, which is overly restrictive.
It probably would have been better to search by description alone and then, if a key is found, have any type of key with that description returned and let the app/service investigate the key to find the type.
Now, this is still possible to implement on top of the existing API: just allow a NULL type to be passed in - but we might need some way to enumerate all the keys with that description, but of different types. Possibly, the search function should return all the matching keys.
Possibly, within the kernel, for each keyring, all the keys of the same description can be stored within a group structure, and the search returns the group. This could also have the added benefit of maybe making it easier to handle updates.
(2) For certain applications, keys need versioning - and we need to be able to get access to older versions (at least to some extent) of the keys. An example of this is cifs where (if I understand it correctly) the key version gets cranked, but not all servers may have caught up yet, so we need to be able to try the keys in descending order of version.
This could also work within the group idea mentioned above.
(3) For certain applications, such as AFS and AF_RXRPC, we may need to be able to keep a number of keys around that have the same description (e.g. cell name) and basic type (e.g. rxrpc) and version, but that have different crypto types (e.g. Rx security classes and Kerberos types, such as RxGK+aes256-cts-hmac-sha1-96, RxGK+aes128-cts-hmac-sha256-128 or RxKAD) as different servers in the same cell might not support all or we might be implementing a server that is offering multiple crypto types.
So we might need a "subtype" as well as a version.
(4) I think the keyring ACLs idea need to be revived. We have a whole bunch of different keyrings, each with a specific 'domain' of usage for the keys contained therein for checking signatures on things. Can we reduce this to one keyring and use ACLs to declare the specific purposes for which a key may be used or the specific tasks that may use it? Use special subject IDs (ie. not simply UIDs/GIDs) to mark this.
(5) Replace the upcall mechanism with a listenable service channel, so that a userspace service (possibly part of systemd or driven from systemd) can listen on it and perform key creation/maintenance services.
From previous discussions with the systemd maintainer, it would be a lot easier for them to manage if the key is attached to a file descriptor - at least for the duration of the maintenance operation.
Further, this needs to be containerised in some way so that requests from different containers can be handled separately - and can be distinguished.
(6) Move away from keeping DNS records in a keyring, but rather keep them in some sort of shrinkable list. They could still be looked up over a secure channel.
To aid with at least (1), (2) and (3) and possibly (4), I think it might be worth adding an extended add_key() system call that takes an additional parameter string:
key_serial_t add_key2(const char *type,
const char *description, const char *parameters, const void payload, size_t plen, key_serial_t keyring);
The parameters would get passed to the key type driver for it to extract things like version number and subtype from without the need to try and fold it into the payload (which may, for example, be a binary ticket obtained from kerberos). Though possibly that is a bad example as the kerberos ticket may contain multiple keys.
Also, maybe add a multi-key adding syscall for when the payload may contain multiple keys, each to be added separately:
int add_keys(const char *type,
const char *description, const char *parameters, const void payload, size_t plen, key_serial_t keyring);
When it comes to keyrings, I'm thinking that the keyring needs to change such that the index holds CoW groups of keys of the same description, but of different type, version and subtype, e.g.:
struct key_group {
struct rcu_head rcu;
struct key_group *replacement;
char *description;
unsigned int seq;
refcount_t ref;
int nr_keys;
struct {
unsigned long version;
struct key __rcu *key;
} key_list[];
};and that these groups should be made available to kernel services upon searching. I'm tempted to put the version as part of the group as a whole, making it easier to ditch a set of the same version, but that could make RCU CoW-ness tricky.
I could then add two new keyctls, one to unlink all the keys in a keyring that match description and, optionally, type and parameters (e.g. of a particular version):
int keyctl_scrub(const char *type, /* can be NULL */
const char *description,
const char *parameters, /* can be NULL */ key_serial_t keyring);
and one to list all the keys matching a description and, optionally, type and parameters:
int list_keys(const char *type, /* can be NULL */
const char *description,
const char *parameters, /* can be NULL */ key_serial_t keyring, key_serial_t *list,
size_t list_size);
Thoughts?
Thanks,
David
Quoted 55 lines · David Howells
Hi Jarkko, Steve, Chuck, Mimi, et al.,
I think work needs to be done on the keyrings subsystem to make them more useful for network filesystems and other kernel services such as TLS and crypto.
There are a number of issues that I think need addressing:
(1) One of the flaws in the initial design is that whilst keys have a type (which is necessary), this has to be specified as part of the lookup or the search, which is overly restrictive.
It probably would have been better to search by description alone and then, if a key is found, have any type of key with that description returned and let the app/service investigate the key to find the type.
Now, this is still possible to implement on top of the existing API: just allow a NULL type to be passed in - but we might need some way to enumerate all the keys with that description, but of different types. Possibly, the search function should return all the matching keys.
Possibly, within the kernel, for each keyring, all the keys of the same description can be stored within a group structure, and the search returns the group. This could also have the added benefit of maybe making it easier to handle updates.
(2) For certain applications, keys need versioning - and we need to be able to get access to older versions (at least to some extent) of the keys. An example of this is cifs where (if I understand it correctly) the key version gets cranked, but not all servers may have caught up yet, so we need to be able to try the keys in descending order of version.
This could also work within the group idea mentioned above.
(3) For certain applications, such as AFS and
AF_RXRPC, we may need to be able to keep a number of keys around that have the same description (e.g. cell name) and basic type (e.g. rxrpc) and version, but that have different crypto types (e.g. Rx security classes and Kerberos types, such as RxGK+aes256-cts-hmac-sha1-96, RxGK+aes128-cts-hmac-sha256-128 or RxKAD) as different servers in the same cell might not support all or we might be implementing a server that is offering multiple crypto types.So we might need a "subtype" as well as a version.
(4) I think the keyring ACLs idea need to be revived. We have a whole bunch of different keyrings, each with a specific 'domain' of usage for the keys contained therein for checking signatures on things. Can we reduce this to one keyring and use ACLs to declare the specific purposes for which a key may be used or the specific tasks that may use it? Use special subject IDs (ie. not simply UIDs/GIDs) to mark this.
(5) Replace the upcall mechanism with a listenable service channel, so that a userspace service (possibly part of systemd or driven from systemd) can listen on it and perform key creation/maintenance services.
Quoted 7 lines
From previous discussions with the systemd maintainer, it would be a lot easier for them to manage if the key is attached to a file descriptor - at least for the duration of the maintenance operation.
Further, this needs to be containerised in some way so that requests from different containers can be handled separately - and can be distinguished.
Indeed one challenge on this front is configuring how to stitch together the various callers and recievers especially when one wants an upcall from one set of namespaces to be serviced within another.
I had previously posted some work in this area that fleshes out the idea of a "key agent" which is a userspace process that can receive a notification to instantiate a key. The nice part (IMO) of this idea is that the keyagent is represented by a key itself, so the channel is available to any process that has the keyagent key in their keyrings.
This allows a system to have a single keyagent for all processes/namespaces for a particular key type, or to build a more granular configuration where agents are confined within the same (or different) namespaces as the calling process.
I'd be happy to continue work on this front, since long-term it would allow us to convert various NFS upcall mechanisms such that NFS access within a container wouldn't require duplicated gssd/svcgssd/idmapper userspace processes in every container if all the system needed was to use a single global instance. It would also allow the partitioning of secure cryptographic material (like keytabs and certificate secrets) from containers that might still want to use NFS, but not divulge those secrets to the processes in that container.
Ben
On Thu, 2025-06-12 at 13:36 +0100, David Howells wrote:
[...]
Quoted 1 line
Thoughts?
One of the problems I keep tripping over is different special casing for user keyrings (which are real struct key structures) and system keyrings which are special values of the pointer in struct key *.
For examples of what this special handling does, just look at things like bpf_trace.c:bpf_lookup_{user|system}_key
Since the serial allocation code has a hard coded not less than 3 (which looks for all the world like it was designed to mean the two system keyring id's were never used as user serial numbers) I think we could simply allow the two system keyring ids to be passed into lookup_user_key() (which now might be a bit misnamed) and special case not freeing it in put_key().
Regards,
James
David Howells
maintainerQuoted 3 lines · James Bottomley
One of the problems I keep tripping over is different special casing for user keyrings (which are real struct key structures) and system keyrings which are special values of the pointer in struct key *.
It's meant to be like that. The trusted system keyrings are static within system_keyring.c and not so easily accessible by kernel modules for direct modification, bypassing the security checks.
Obviously this is merely a bit of obscurity and enforcement isn't possible against kernel code that is determined to modify those keyrings or otherwise interfere in the verification process.
Quoted 6 lines
For examples of what this special handling does, just look at things like bpf_trace.c:bpf_lookup_{user|system}_key
Since the serial allocation code has a hard coded not less than 3 (which looks for all the world like it was designed to mean the two system keyring id's were never used as user serial numbers)
That's just a coincidence. The <3 thing predates the advent of those system keyring magic pointers.
Quoted 3 lines
I think we could simply allow the two system keyring ids to be passed into
lookup_user_key()(which now might be a bit misnamed) and special case not freeing it input_key().
If you want to make lookup_user_key() provide access to specific keyrings like this, just use the next negative numbers - it's not like we're likely to run out soon.
But I'd rather not let lookup_user_key() return pointers to these keyrings...
David
Quoted 7 lines · David Howells
Quoted 4 lines · James Bottomley
One of the problems I keep tripping over is different special casing for user keyrings (which are real struct key structures) and system keyrings which are special values of the pointer in struct key *.
Quoted 7 lines
It's meant to be like that. The trusted system keyrings are static within
system_keyring.cand not so easily accessible by kernel modules for direct modification, bypassing the security checks.Obviously this is merely a bit of obscurity and enforcement isn't possible against kernel code that is determined to modify those keyrings or otherwise interfere in the verification process.
Yes, I get that, and wasn't proposing to alter it, merely make them a bit more usable as part of the standard workflow.
Are the permissions also deliberate? It would be useful for ordinary users to see the certificates in there in case they want to condition anything on signatures with internal keys.
Quoted 9 lines
Quoted 6 lines
For examples of what this special handling does, just look at things like bpf_trace.c:bpf_lookup_{user|system}_key
Since the serial allocation code has a hard coded not less than 3 (which looks for all the world like it was designed to mean the two system keyring id's were never used as user serial numbers)
That's just a coincidence. The <3 thing predates the advent of those system keyring magic pointers.
Well coincidence or not it makes the scheme workable, but I've no objection to using negatives as suggested below, either.
Quoted 10 lines
Quoted 3 lines
I think we could simply allow the two system keyring ids to be passed into
lookup_user_key()(which now might be a bit misnamed) and special case not freeing it input_key().If you want to make
lookup_user_key()provide access to specific keyrings like this, just use the next negative numbers - it's not like we're likely to run out soon.But I'd rather not let
lookup_user_key()return pointers to these keyrings...
That wasn't what I was proposing. If we were to allow it I think it would return the standard pointer that's actually simply a number as we do now. That would be enough to unify all the workflows into pkcs7_verify_..() and crash soon enough if the key pointers were used inside the kernel for stuff the system keyrings aren't supposed to do.
Regards,
James
On Thu, 2025-06-12 at 13:36 +0100, David Howells wrote:
[ ...]
Quoted 6 lines
(4) I think the keyring ACLs idea need to be revived. We have a whole bunch of different keyrings, each with a specific 'domain' of usage for the keys contained therein for checking signatures on things. Can we reduce this to one keyring and use ACLs to declare the specific purposes for which a key may be used or the specific tasks that may use it? Use special subject IDs (ie. not simply UIDs/GIDs) to mark this.
David, which keyrings are you referring to? What do you mean by 'domain' of usage? At what level of granularity are you thinking of? This needs to be describe in more detail.
thanks,
Mimi
Jarkko Sakkinen
maintainerQuoted 21 lines · David Howells
Hi Jarkko, Steve, Chuck, Mimi, et al.,
I think work needs to be done on the keyrings subsystem to make them more useful for network filesystems and other kernel services such as TLS and crypto.
There are a number of issues that I think need addressing:
(1) One of the flaws in the initial design is that whilst keys have a type (which is necessary), this has to be specified as part of the lookup or the search, which is overly restrictive.
It probably would have been better to search by description alone and then, if a key is found, have any type of key with that description returned and let the app/service investigate the key to find the type.
Now, this is still possible to implement on top of the existing API: just allow a NULL type to be passed in - but we might need some way to enumerate all the keys with that description, but of different types. Possibly, the search function should return all the matching keys.
Quoted 4 lines
Possibly, within the kernel, for each keyring, all the keys of the same description can be stored within a group structure, and the search returns the group. This could also have the added benefit of maybe making it easier to handle updates.
All keys matching the description can be collected by iterating all the key types (instead of passing NULL).
Having only a syscall for the process has of course much better concurrency properties. Just trying to understand the improvements by actual measure, that's all.
Quoted 16 lines
(2) For certain applications, keys need versioning - and we need to be able to get access to older versions (at least to some extent) of the keys. An example of this is cifs where (if I understand it correctly) the key version gets cranked, but not all servers may have caught up yet, so we need to be able to try the keys in descending order of version.
This could also work within the group idea mentioned above.
(3) For certain applications, such as AFS and
AF_RXRPC, we may need to be able to keep a number of keys around that have the same description (e.g. cell name) and basic type (e.g. rxrpc) and version, but that have different crypto types (e.g. Rx security classes and Kerberos types, such as RxGK+aes256-cts-hmac-sha1-96, RxGK+aes128-cts-hmac-sha256-128 or RxKAD) as different servers in the same cell might not support all or we might be implementing a server that is offering multiple crypto types.
Quoted 1 line
So we might need a "subtype" as well as a version.
1. How the history is capped? I presume it is a fixed-size circular buffer, where the oldest entry is deleted, when the key is updated.
1. How history queried by the caller?
2. How a newer version is revoked in favor of a newer version?
Quoted 6 lines
(4) I think the keyring ACLs idea need to be revived. We have a whole bunch of different keyrings, each with a specific 'domain' of usage for the keys contained therein for checking signatures on things. Can we reduce this to one keyring and use ACLs to declare the specific purposes for which a key may be used or the specific tasks that may use it? Use special subject IDs (ie. not simply UIDs/GIDs) to mark this.
Is subject ID some kind of token that a process can hold (and possibly can be sent to a process)?
Quoted 5 lines
(5) Replace the upcall mechanism with a listenable service channel, so that a userspace service (possibly part of systemd or driven from systemd) can listen on it and perform key creation/maintenance services.
Quoted 7 lines
From previous discussions with the systemd maintainer, it would be a lot easier for them to manage if the key is attached to a file descriptor - at least for the duration of the maintenance operation.
Further, this needs to be containerised in some way so that requests from different containers can be handled separately - and can be distinguished
This sounds like an universally sane idea (outside the scope of the patch set).
Quoted 4 lines
(6) Move away from keeping DNS records in a keyring, but rather keep them in some sort of shrinkable list. They could still be looked up over a secure channel.
Don't expertise to understand the benefits of this change.
Quoted 11 lines
To aid with at least (1), (2) and (3) and possibly (4), I think it might be worth adding an extended
add_key()system call that takes an additional parameter string:
key_serial_tadd_key2(const char *type,
const char *description, const char *parameters, const void payload,size_tplen,key_serial_tkeyring);
Quoted 5 lines
The parameters would get passed to the key type driver for it to extract things like version number and subtype from without the need to try and fold it into the payload (which may, for example, be a binary ticket obtained from kerberos). Though possibly that is a bad example as the kerberos ticket may contain multiple keys.
Does the parameter list mimic kernel command-line style of comma separated attributes?
BR, Jarkko
[removed other lists from Cc:]
Hi all,
Quoted 3 lines · David Howells
I think work needs to be done on the keyrings subsystem to make them more useful for network filesystems and other kernel services such as TLS and crypto.
As someone who has recently tried to use keyrings from userspace (in order to bind key material to a TPM, and have it never enter user memory), I would note two things here:
* the request_key() syscall seems to be blocking. Of course you can work around that by creating a thread, but is it not possible to create a better API?
* the trusted key / TPM bits are incredibly poorly documented. It's basically "cobble together things from old patch descriptions", e.g.
https://marc.info/?l=linux-keyrings&m=160074197025246
(I still have not figured out how to do this correctly. Some of that is to blame on the TPM bits, but not all.)
I guess the kernel itself doesn't care about the blocking API, but for trusted keys... you'll have to tie things together from user space, and I don't see how anyone can do that, especially with any level of confidence that they didn't accidentally leave some wide-open hole (as in - you might well get something to work, but without docs you can't check you actually did it *right*.) This is really improving keyctl itself (i.e. how its commands are invoked) and also its documentation.
Hope this isn't too negative as feedback,
equi
P.S.: I wish I could submit patches for this. I can't, because I don't understand it.
Quoted 6 lines · David 'equinox' Lamparter
* the trusted key / TPM bits are incredibly poorly documented. It's basically "cobble together things from old patch descriptions", e.g.
https://marc.info/?l=linux-keyrings&m=160074197025246
(I still have not figured out how to do this correctly. Some of that is to blame on the TPM bits, but not all.)
Quoted 7 lines
I guess the kernel itself doesn't care about the blocking API, but for trusted keys... you'll have to tie things together from user space, and I don't see how anyone can do that, especially with any level of confidence that they didn't accidentally leave some wide-open hole (as in - you might well get something to work, but without docs you can't check you actually did it *right*.) This is really improving keyctl itself (i.e. how its commands are invoked) and also its documentation.
You may be interested in some of the work I've done here:
https://github.com/mathstuf/rust-keyutils
In particular:
https://github.com/mathstuf/rust-keyutils/blob/master/src/keytypes/trusted.rs
There are assorted issues and PRs that may be of interest for trusted keys as well.
Unfortunately, I've not had the time to dedicate to it, so it has languished a bit over time.
--Ben
David Howells
maintainerDavid 'equinox' Lamparter <[email protected]> wrote:
[Apologies, I've been off fixing bugs for a few weeks]
Quoted 5 lines · David Howells
Quoted 3 lines
I think work needs to be done on the keyrings subsystem to make them more useful for network filesystems and other kernel services such as TLS and crypto.
Quoted 7 lines
As someone who has recently tried to use keyrings from userspace (in order to bind key material to a TPM, and have it never enter user memory), I would note two things here:
* the
request_key()syscall seems to be blocking. Of course you can work around that by creating a thread, but is it not possible to create a better API?
Depends what you mean by a "better API". Unfortunately, we're somewhat stuck with this API - it has to go on working pretty much as-is. However, some things can be changed.
request_key() can block, yes - that's part of the design. It can upcall to userspace to go and get the content for a key - and this might involve, say, network calls to a KDC. Now, the exact upcalling mechanism can be done better
- and that's one of the things I included in my list - but it should not block forever and, ideally, if someone presses Ctrl-C, it should perhaps terminate the upcall.
Quoted 5 lines
* the trusted key / TPM bits are incredibly poorly documented. It's basically "cobble together things from old patch descriptions", e.g.
https://marc.info/?l=linux-keyrings&m=160074197025246
(I still have not figured out how to do this correctly. Some of that is to blame on the TPM bits, but not all.)
Note that the trusted key/TPM bits aren't part of the core keyrings code. Trusted and encrypted keys are examples of key types.
Quoted 7 lines
I guess the kernel itself doesn't care about the blocking API, but for trusted keys... you'll have to tie things together from user space, and I don't see how anyone can do that, especially with any level of confidence that they didn't accidentally leave some wide-open hole (as in - you might well get something to work, but without docs you can't check you actually did it *right*.) This is really improving keyctl itself (i.e. how its commands are invoked) and also its documentation.
Are you talking about keyctl() the syscall or keyctl the program?
David
Quoted 1 line · David Howells
David 'equinox' Lamparter <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 7 lines
As someone who has recently tried to use keyrings from userspace (in order to bind key material to a TPM, and have it never enter user memory), I would note two things here:
* the
request_key()syscall seems to be blocking. Of course you can work around that by creating a thread, but is it not possible to create a better API?Depends what you mean by a "better API". Unfortunately, we're somewhat stuck with this API - it has to go on working pretty much as-is. However, some things can be changed.
I'm painfully aware of the no breaking userspace rule, and it certainly applies here :(.
Quoted 6 lines
request_key()can block, yes - that's part of the design. It can upcall to userspace to go and get the content for a key - and this might involve, say, network calls to a KDC. Now, the exact upcalling mechanism can be done better
- and that's one of the things I included in my list - but it should not block forever and, ideally, if someone presses Ctrl-C, it should perhaps terminate the upcall.
Well, I'm a networking person, so my immediate association is netlink. As in, request_key() is a KEYCTL_REQUEST_KEY message that you get a KEYCTL_RESULT_KEY response to -- at some point later. (With request abort if the socket gets closed for whatever reason.)
To be entirely honest I was also surprised to see the keyring monitoring bits have this WATCH_QUEUE thing (that nobody else seems to be using?) rather than just throwing up netlink multicast. There's probably some backstory to this that I'm unaware of.
Quoted 8 lines
Quoted 5 lines
* the trusted key / TPM bits are incredibly poorly documented. It's basically "cobble together things from old patch descriptions", e.g.
https://marc.info/?l=linux-keyrings&m=160074197025246
(I still have not figured out how to do this correctly. Some of that is to blame on the TPM bits, but not all.)Note that the trusted key/TPM bits aren't part of the core keyrings code. Trusted and encrypted keys are examples of key types.
I know -- maybe I should make another comment here: the trusted/TPM bits, through the keyring API, are *actually quite useful* for userspace, especially combined with AF_ALG's ALG_SET_KEY_BY_KEY_SERIAL. Doing crypto with a key that never enters userspace - amazing!
So maybe let me replace my message here with "please don't forget about userspace". Which does include documenting things ;).
Quoted 9 lines
Quoted 7 lines
I guess the kernel itself doesn't care about the blocking API, but for trusted keys... you'll have to tie things together from user space, and I don't see how anyone can do that, especially with any level of confidence that they didn't accidentally leave some wide-open hole (as in - you might well get something to work, but without docs you can't check you actually did it *right*.) This is really improving keyctl itself (i.e. how its commands are invoked) and also its documentation.
Are you talking about
keyctl()the syscall or keyctl the program?
Sorry, keyctl the program. I haven't tried using the syscall directly, libkeyutils is reasonable (easier to understand than the program, maybe.)
Cheers,
equi