Tip: keyctl in a bash script

Monday, 06 August 2018
|
Écrit par
Grégory Soutadé

Here is a simple tip to use keyctl in a bash script. keyctl is a wrapper for Linux kernel key management interface. It allows to securely save data in kernel memory. The man documentation is very bigcomplete but I didn't find any example on internet. What I initially wanted to do is to safely store a password entered by user inside a bash shell script and keep private to it (don't share with other processes).

Basically the script looks like :

#!/bin/bash

password=SecretPassword

keyctl new_session > /dev/null
keyid=`keyctl add user mail $password @s`
keyctl show
# echo "KEYID $keyid"
keyctl print $keyid

The first thing to do is to create a new session (to detach the current shared one).

Then we will add the password in the new item "mail". We don't have other choice to set type to "user". The item will be placed into the session keyring (@s). We could create new keyrings to store it with keyctl newring command. The command return item id as a big integer. We can use this integer or its name "%user:mail" for further references.

There is also a command keyctl padd which read data from stdin, but I don't recommend to use it as data is displayed in clear on the terminal.

Finally we show keyring information and print our password. We use print command to have an human friendly output, keyctl read command display it in hex format...

#
1
De
Adam
, le
11 September 2021 01:09
Thank you, very helpful!
Répondre
Auteur :


e-mail* :


Le commentaire :


#
2
De
Greg
, le
11 September 2021 07:09
Thanks
Répondre
Auteur :


e-mail* :


Le commentaire :


#
3
De
Damien
, le
24 July 2023 06:07
Hi. Thanks for your post. I was looking up ways to store private tokens with Linux commands a few days ago then found about keyctl and your post.

My use case is storing credentials for use with sudo in non interactive script so this is helpful. But most advice online does recommend to use pipes for transferring sensitive data. As shell variables and expanded variables in command lines can be easily sniffed for data.

The only information I cannot clearly find is if the private data is internally encrypted by the kernel using a private key. Being pedantic I would prefer to store credentials in encrypted form even if protected from other processes. So I'd probably add an openssl command to the process. :-)
Répondre
Auteur :


e-mail* :


Le commentaire :


#
4
De
Greg
, le
24 July 2023 15:07
Hi Damien,

Thanks for your comment. If I look at the documentation
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/security/keys/core.rst?h=v6.4.5 line 958

I can see there is an interface to do encryption/decryption on you data blob using asymetric functions, so I assume it's not done by default.
Répondre
Auteur :


e-mail* :


Le commentaire :


#
5
De
Damien
, le
25 July 2023 08:07
Thanks for looking into it. Yes I see there is an extra interface for encryption. This doesn't look like it is directly supported by the keyctl command and must be performed manually on the data blob by the looks of it.

Although it's protected in kernel memory I thought it might hide the blob itself internally by obfuscating it with some custom encryption transparently. I suppose I'm being over cautious but the purpose is to store sensitive information you want to to hide from other processes and users. It can be applied with openssl but involving as less layers as possible when needing to store clear text credentials is beneficial, especially when stored from shell scripts.
Répondre
Auteur :


e-mail* :


Le commentaire :


Auteur :


e-mail* :


Le commentaire :




* Seulement pour être notifié d'une réponse à cet article
* Only for email notification