keys: trusted and encrypted keys
Subsystems (7)
cover keys: trusted and encrypted keys
The previous posting added serveral new trusted-key options (migratable, pcrlock, keyhandle, keyauth, blobauth), described below, based on suggestions by Jason Gunthorpe. This patch requires the pcrlock option be executed with CAP_SYS_ADMIN privileges. By default, trusted keys work as previously described.
Trusted and Encrypted Keys are two new key types added to the existing kernel key ring service. Both of these new types are variable length symmetic keys, and in both cases all keys are created in the kernel, and user space sees, stores, and loads only encrypted blobs. Trusted Keys require the availability of a Trusted Platform Module (TPM) chip for greater security, while Encrypted Keys can be used on any system. All user level blobs, are displayed and loaded in hex ascii for convenience, and are integrity verified.
Trusted Keys use a TPM both to generate and to seal the keys. Keys are sealed under a 2048 bit RSA key in the TPM, and optionally sealed to specified PCR (integrity measurement) values, and only unsealed by the TPM, if PCRs and blob integrity verifications match. A loaded Trusted Key can be updated with new (future) PCR values, so keys are easily migrated to new pcr values, such as when the kernel and initramfs are updated. The same key can have many saved blobs under different PCR values, so multiple boots are easily supported.
By default, trusted keys are sealed under the SRK, which has the default authorization value (20 zeros). This can be set at takeownership time with the trouser's utility:
"tpm_takeownership -u -z".
Usage:
keyctl add trusted name "new keylen [options]" ring keyctl add trusted name "load hex_blob [pcrlock=pcrnum]" ring keyctl update key "update [options]"
keyctl print keyid
options:
keyhandle= ascii hex value of sealing key default 0x40000000 (SRK)
keyauth= ascii hex auth for sealing key default 0x00... (40 ascii zeros)
blobauth= ascii hex auth for sealed data default 0x00... (40 ascii zeros)
pcrinfo= ascii hex of PCR_INFO or PCR_INFO_LONG (no default)
pcrlock= pcr number to be extended to "lock" blob
migratable= 0|1 indicating permission to reseal to new PCR values,
default 1 (resealing allowed)
The key length for new keys are always in bytes. Trusted Keys can be 32 - 128 bytes (256 - 1024 bits), the upper limit is to fit within the 2048 bit SRK (RSA) keylength, with all necessary structure/padding.
Encrypted keys do not depend on a TPM, and are faster, as they use AES for encryption/decryption. New keys are created from kernel generated random numbers, and are encrypted/decrypted using a specified 'master' key. The 'master' key can either be a trusted-key or user-key type. The main disadvantage of encrypted keys is that if they are not rooted in a trusted key, they are only as secure as the user key encrypting them. The master user key should therefore be loaded in as secure a way as possible, preferably early in boot.
Usage:
keyctl add encrypted name "new master-key-name keylen" ring keyctl add encrypted name "load master-key-name keylen hex_blob" ring keyctl update keyid "update master-key-name"
The initial consumer of trusted keys is EVM, which at boot time needs a high quality symmetric key for HMAC protection of file metadata. The use of a trusted key provides strong guarantees that the EVM key has not been compromised by a user level problem, and when sealed to specific boot PCR values, protects against boot and offline attacks. Other uses for trusted and encrypted keys, such as for disk and file encryption are anticipated.
Mimi Zohar
David Safford
Mimi Zohar (4):
lib: hex2bin converts ascii hexadecimal string to binary
key: add tpm_send command
keys: add new trusted key-type
keys: add new key-type encrypted
drivers/char/tpm/tpm.c | 17 + include/keys/encrypted-type.h | 30 + include/keys/trusted-type.h | 32 ++ include/linux/kernel.h | 1 + include/linux/tpm.h | 3 + lib/hexdump.c | 16 + security/Kconfig | 31 + security/keys/Makefile | 2 + security/keys/encrypted_defined.c | 816 +++++++++++++++++++++++++++ security/keys/encrypted_defined.h | 52 ++ security/keys/trusted_defined.c | 1101 +++++++++++++++++++++++++++++++++++++ security/keys/trusted_defined.h | 147 +++++ 12 files changed, 2248 insertions(+), 0 deletions(-) create mode 100644 include/keys/encrypted-type.h create mode 100644 include/keys/trusted-type.h create mode 100644 security/keys/encrypted_defined.c create mode 100644 security/keys/encrypted_defined.h create mode 100644 security/keys/trusted_defined.c create mode 100644 security/keys/trusted_defined.h
Signature · 1 line
1.7.2.2
PATCH 1/4 lib: hex2bin converts ascii hexadecimal string to binary
Similar to the kgdb_hex2mem() code, hex2bin converts a string to binary using the hex_to_bin() library call.
--- include/linux/kernel.h | 1 + lib/hexdump.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 0 deletions(-)
include/linux/kernel.h +1 -0
@@ -419,6 +419,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte) }extern int hex_to_bin (char ch); +extern void hex2bin (unsigned char *mem,char *buf,int count);#ifndef pr_fmt#define pr_fmt (fmt) fmt
lib/hexdump.c +16 -0
@@ -34,6 +34,22 @@ int hex_to_bin(char ch)EXPORT_SYMBOL (hex_to_bin);/** +* hex2bin - convert an ascii hexadecimal string to its binary representation +* @mem: result +* @buf: ascii hexadecimal string +* @count: result length +*/ +void hex2bin (unsigned char *mem,char *buf,int count) +{ +while (count--) { + *mem =hex_to_bin (*buf++) <<4 ; + *mem +=hex_to_bin (*buf++); + mem++; + } +} +EXPORT_SYMBOL (hex2bin); + +/** * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory * @buf: data blob to dump * @len: number of bytes in the @buf
Signature · 1 line
1.7.2.2
David Howells
maintainerRe: [PATCH v1.3 1/4] lib: hex2bin converts ascii hexadecimal string to binary
Quoted 1 line · Mimi Zohar
+void
hex2bin(unsigned char *mem, char *buf, int count)
I think this needs a little adjustment. I would recommend something like the following declaration:
void hex2bin(u8 *buf, const char *data, size_t count)
since the output data is binary (so use u8*), you're only reading the source data (so should use a const pointer) and you don't want to see a negative count (so use size_t).
Also, I'd suggest calling the source arg 'data' and the output arg 'buf' or maybe src/dst.
David
Re: [PATCH v1.3 1/4] lib: hex2bin converts ascii hexadecimal string to binary
Quoted 4 lines · David Howells
Quoted 1 line · Mimi Zohar
+void
hex2bin(unsigned char *mem, char *buf, int count)
Quoted 8 lines
I think this needs a little adjustment. I would recommend something like the following declaration:
void
hex2bin(u8 *buf, const char *data, size_t count)since the output data is binary (so use u8*), you're only reading the source data (so should use a const pointer) and you don't want to see a negative count (so use
size_t).
right, thanks.
Quoted 4 lines
Also, I'd suggest calling the source arg 'data' and the output arg 'buf' or maybe src/dst.
David
Definitely better. (Remnant from kgdb_hex2mem() naming.)
thanks,
Mimi
PATCH 2/4 key: add tpm_send command
In reply to Mimi ZoharAdd internal kernel tpm_send() command used to seal/unseal keys.
--- drivers/char/tpm/tpm.c | 17 +++++++++++++++++ include/linux/tpm.h | 3 +++ 2 files changed, 20 insertions(+), 0 deletions(-)
drivers/char/tpm/tpm.c +17 -0
@@ -780,6 +780,23 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) }EXPORT_SYMBOL_GPL (tpm_pcr_extend); +int tpm_send (u32 chip_num,char *cmd,int buflen) +{ +struct tpm_chip *chip; +int rc; + + chip =tpm_chip_find_get (chip_num); +if (chip ==NULL ) +return -ENODEV ; + + rc =transmit_cmd (chip, (struct tpm_cmd_t *)cmd, buflen, +"attempting tpm_cmd" ); + +module_put (chip->dev->driver->owner); +return rc; +} +EXPORT_SYMBOL_GPL (tpm_send); +ssize_t tpm_show_pcrs (struct device *dev,struct device_attribute *attr,char *buf) {
include/linux/tpm.h +3 -0
@@ -31,6 +31,7 @@extern int tpm_pcr_read (u32 chip_num,int pcr_idx,u8 *res_buf);extern int tpm_pcr_extend (u32 chip_num,int pcr_idx,const u8 *hash); +extern int tpm_send (u32 chip_num,char *cmd,int buflen);#else static inline int tpm_pcr_read (u32 chip_num,int pcr_idx,u8 *res_buf) {return -ENODEV ;
@@ -38,5 +39,7 @@ static inline int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) { static inline int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) { return -ENODEV; } +static inline int tpm_send(u32 chip_num, char *cmd, int buflen) { + return -ENODEV; #endif #endif
Signature · 1 line
1.7.2.2
David Howells
maintainerRe: [PATCH v1.3 2/4] key: add tpm_send command
Quoted 1 line · Mimi Zohar
Reviewd-by Mimi Zohar <[email protected]>
You've missed an 'e'.
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 793
Quoted 5 lines
drivers/char/tpm/tpm.c +4
-0+int tpm_send (u32 chip_num,char *cmd,int buflen) +{ + rc =transmit_cmd (chip, (struct tpm_cmd_t *)cmd, buflen, +"attempting tpm_cmd" );
Make cmd 'void *' to obviate the cast. Preferably it should be const too.
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 795
Quoted 1 line
drivers/char/tpm/tpm.c +1
-0+module_put (chip->dev->driver->owner);
Where's the corresponding module_get()? I suspect this should be wrapped to match tpm_chip_find_get().
David
Re: [PATCH v1.3 2/4] key: add tpm_send command
Quoted 5 lines · David Howells
Quoted 1 line · Mimi Zohar
Reviewd-by Mimi Zohar <[email protected]>You've missed an 'e'.
thanks, will fix
Quoted 7 lines
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 793
Quoted 5 lines
drivers/char/tpm/tpm.c +4
-0+int tpm_send (u32 chip_num,char *cmd,int buflen) +{ + rc =transmit_cmd (chip, (struct tpm_cmd_t *)cmd, buflen, +"attempting tpm_cmd" );Make cmd 'void *' to obviate the cast. Preferably it should be const too.
will do
Quoted 6 lines
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 795
Quoted 1 line
drivers/char/tpm/tpm.c +1
-0+module_put (chip->dev->driver->owner);Where's the corresponding
module_get()? I suspect this should be wrapped to matchtpm_chip_find_get().David
The module_get() is in tpm_chip_find_get(), which is just a helper. (It's used this way throughout tpm.c)
thanks,
Mimi
David Howells
maintainerRe: [PATCH v1.3 2/4] key: add tpm_send command
Quoted 9 lines · Mimi Zohar
Quoted 6 lines
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 795
Quoted 1 line
drivers/char/tpm/tpm.c +1
-0+module_put (chip->dev->driver->owner);Where's the corresponding
module_get()? I suspect this should be wrapped to matchtpm_chip_find_get().David
The
module_get()is intpm_chip_find_get(), which is just a helper. (It's used this way throughouttpm.c)
I'd make a function tpm_chip_find_put() just to wrap module_put() and then place it with tpm_chip_find_get() in the sources. That makes it easier to see what's going on.
David
Re: [PATCH v1.3 2/4] key: add tpm_send command
Quoted 7 lines · David Howells
Quoted 5 lines · Mimi Zohar
Quoted 5 lines
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 795
Quoted 1 line
drivers/char/tpm/tpm.c +1
-0+module_put (chip->dev->driver->owner);Where's the corresponding
module_get()? I suspect this should be wrapped to matchtpm_chip_find_get().
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
David
The
module_get()is intpm_chip_find_get(), which is just a helper. (It's used this way throughouttpm.c)I'd make a function
tpm_chip_find_put()just to wrapmodule_put()and then place it withtpm_chip_find_get()in the sources. That makes it easier to see what's going on.
Or alternately rename the helper to tpm_chip_find_and_module_get()? In either case, this is really up to Rajiv (the maintainer for tpm.c who has already acked this patch), as this usage already appears in other places in tpm.c. There would have to be a separate patch fixing this for all of the instances. Rajiv, your thoughts?
dave
Re: [PATCH v1.3 2/4] key: add tpm_send command
Hi Dave,
Quoted 13 lines · David Safford
Quoted 6 lines · David Howells
Quoted 4 lines · Mimi Zohar
Quoted 4 lines
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 795
Quoted 1 line
drivers/char/tpm/tpm.c +1
-0+module_put (chip->dev->driver->owner);Where's the corresponding
module_get()? I suspect this should be wrapped to matchtpm_chip_find_get().Quoted 6 lines
Quoted 3 lines
Quoted 1 line
David
The
module_get()is intpm_chip_find_get(), which is just a helper. (It's used this way throughouttpm.c)I'd make a function
tpm_chip_find_put()just to wrapmodule_put()and then place it withtpm_chip_find_get()in the sources. That makes it easier to see what's going on.
Quoted 7 lines
Or alternately rename the helper to
tpm_chip_find_and_module_get()? In either case, this is really up to Rajiv (the maintainer fortpm.cwho has already acked this patch), as this usage already appears in other places intpm.c. There would have to be a separate patch fixing this for all of the instances. Rajiv, your thoughts?dave
Rename the helper to tpm_chip_find_and_module_get() solves the naming issue in a simpler and better way in my opinion.
Thanks,
Rajiv
Re: [PATCH v1.3 2/4] key: add tpm_send command
Quoted 24 lines · Rajiv Andrade
Hi Dave,
Quoted 19 lines · David Safford
Quoted 6 lines · David Howells
Quoted 4 lines · Mimi Zohar
Quoted 4 lines
drivers/char/tpm/tpm.c int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) · line 795
Quoted 1 line
drivers/char/tpm/tpm.c +1
-0+module_put (chip->dev->driver->owner);Where's the corresponding
module_get()? I suspect this should be wrapped to matchtpm_chip_find_get().Quoted 6 lines
Quoted 3 lines
Quoted 1 line
David
The
module_get()is intpm_chip_find_get(), which is just a helper. (It's used this way throughouttpm.c)I'd make a function
tpm_chip_find_put()just to wrapmodule_put()and then place it withtpm_chip_find_get()in the sources. That makes it easier to see what's going on.Or alternately rename the helper to
tpm_chip_find_and_module_get()? In either case, this is really up to Rajiv (the maintainer fortpm.cwho has already acked this patch), as this usage already appears in other places intpm.c. There would have to be a separate patch fixing this for all of the instances. Rajiv, your thoughts?
Quoted 6 lines
Quoted 1 line
dave
Rename the helper to
tpm_chip_find_and_module_get()solves the naming issue in a simpler and better way in my opinion.Thanks,
Rajiv
David, does this look ok to you? If so, I will do two patches, one to fix the helper name throughout the existing tpm.c, and then a new version of the tpm_send patch which uses the new name.
thanks
dave
David Howells
maintainerRe: [PATCH v1.3 2/4] key: add tpm_send command
Quoted 3 lines · David Safford
David, does this look ok to you? If so, I will do two patches, one to fix the helper name throughout the existing
tpm.c, and then a new version of thetpm_sendpatch which uses the new name.
I prefer my suggestion: Wrapping the module_put() up so that you don't see it directly. Then you don't need to alter tpm_chip_find_get(). I'll argue that you don't need to know how tpm_chips are got/put, except in the code that wraps it.
David
Re: [PATCH v1.3 2/4] key: add tpm_send command
Quoted 10 lines · David Howells
Quoted 3 lines · David Safford
David, does this look ok to you? If so, I will do two patches, one to fix the helper name throughout the existing
tpm.c, and then a new version of thetpm_sendpatch which uses the new name.I prefer my suggestion: Wrapping the
module_put()up so that you don't see it directly. Then you don't need to altertpm_chip_find_get(). I'll argue that you don't need to know howtpm_chipsare got/put, except in the code that wraps it.
tpm_chip_find_get() not only gets the tpm_chip, but also searches for it given an index in a tpm_chip_list. tpm_chip_put() is then the name that fits the argument here, given it'll only be a wrapper of the tpm_chip put functionality, not the searching one I assume.
I'll ack any of the two approaches (tpm_chip_put or tpm_chip_find_get renaming) in any case.
Rajiv
PATCH 3/4 keys: add new trusted key-type
In reply to Mimi ZoharDefines a new kernel key-type called 'trusted'. Trusted keys are random number symmetric keys, generated and RSA-sealed by the TPM. The TPM only unseals the keys, if the boot PCRs and other criteria match. Userspace can only ever see encrypted blobs.
Based on suggestions by Jason Gunthorpe, several new options have been added to support additional usages.
The new options are:
migratable= designates that the key may/may not ever be updated
(resealed under a new key, new pcrinfo or new auth.)
pcrlock=n extends the designated PCR 'n' with a random value,
so that a key sealed to that PCR may not be unsealed again until after a reboot.
keyhandle= specifies the sealing/unsealing key handle.
keyauth= specifies the sealing/unsealing key auth.
blobauth= specifies the sealed data auth.
Implementation of a kernel reserved locality for trusted keys will be investigated for a possible future extension.
Changelog:
- Add pcrlock CAP_SYS_ADMIN dependency (based on comment by Jason Gunthorpe)
- New options: migratable, pcrlock, keyhandle, keyauth, blobauth (based on discussions with Jason Gunthorpe)
- Free payload on failure to create key(reported/fixed by Roberto Sassu)
- Updated Kconfig and other descriptions (based on Serge Hallyn's suggestion)
- Replaced kzalloc() with kmalloc() (reported by Serge Hallyn)
--- include/keys/trusted-type.h | 32 ++ security/Kconfig | 15 + security/keys/Makefile | 1 + security/keys/trusted_defined.c | 1101 +++++++++++++++++++++++++++++++++++++++ security/keys/trusted_defined.h | 147 ++++++ 5 files changed, 1296 insertions(+), 0 deletions(-) create mode 100644 include/keys/trusted-type.h create mode 100644 security/keys/trusted_defined.c create mode 100644 security/keys/trusted_defined.h
include/keys/trusted-type.h new file +32 -0
@@ -0,0 +1,32 @@ +/* trusted-type.h: trusted-defined key type +* +* Copyright (C) 2010 IBM Corporation +* Author: David Safford <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, version 2 of the License. +*/ + +#ifndef _KEYS_TRUSTED_TYPE_H +#define _KEYS_TRUSTED_TYPE_H + +#include <linux/key.h> +#include <linux/rcupdate.h> + +#define MIN_KEY_SIZE 32 +#define MAX_KEY_SIZE 128 +#define MAX_BLOB_SIZE 320 + +struct trusted_key_payload { +struct rcu_head rcu;/* RCU destructor */ +unsigned int key_len; +unsigned int blob_len; +unsigned char migratable; +unsigned char key[MAX_KEY_SIZE +1 ]; +unsigned char blob[MAX_BLOB_SIZE ]; +}; + +extern struct key_type key_type_trusted; + +#endif /* _KEYS_TRUSTED_TYPE_H */
security/Kconfig +15 -0
@@ -21,6 +21,21 @@ config KEYS If you are unsure as to whether this is required, answer N. +config TRUSTED_KEYS + tristate "TRUSTED KEYS" + depends on KEYS && TCG_TPM + select CRYPTO + select CRYPTO_HMAC + select CRYPTO_SHA1 + help + This option provides support for creating, sealing, and unsealing + keys in the kernel. Trusted keys are random number symmetric keys, + generated and RSA-sealed by the TPM. The TPM only unseals the keys, + if the boot PCRs and other criteria match. Userspace can only ever + see encrypted blobs. + + If you are unsure as to whether this is required, answer N. + config KEYS_DEBUG_PROC_KEYS bool "Enable the /proc/keys file by which keys may be viewed" depends on KEYS
security/keys/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-y := \ request_key_auth.o \ user_defined.o +obj-$(CONFIG_TRUSTED_KEYS) += trusted_defined.o obj-$(CONFIG_KEYS_COMPAT) += compat.o obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_SYSCTL) += sysctl.o
security/keys/trusted_defined.c new file +1101 -0
1,101 lines not on this page · raw message
security/keys/trusted_defined.h new file +147 -0
147 lines not on this page · raw message
Signature · 1 line
1.7.2.2
David Howells
maintainerRe: [PATCH v1.3 3/4] keys: add new trusted key-type
Quoted 4 lines · Mimi Zohar
Defines a new kernel key-type called 'trusted'. Trusted keys are random number symmetric keys, generated and RSA-sealed by the TPM. The TPM only unseals the keys, if the boot PCRs and other criteria match. Userspace can only ever see encrypted blobs.
I'd recommend saying 'Define' not 'Defines'. You're talking in first or second person.
I'd also recommend filling out the text to 80 chars and being consistent about the use of space after a full stop (you sometimes use 1 and sometimes 2 spaces
- I'd recommend 2).
Quoted 9 lines
include/keys/trusted-type.h | 32 ++ security/Kconfig | 15 + security/keys/Makefile | 1 + security/keys/trusted_defined.c | 1101 +++++++++++++++++++++++++++++++++++++++ security/keys/trusted_defined.h | 147 ++++++ 5 files changed, 1296 insertions(+), 0 deletions(-) create mode 100644 include/keys/trusted-type.h create mode 100644 security/keys/trusted_defined.c create mode 100644 security/keys/trusted_defined.h
Documentation/trusted_keys.txt?
include/keys/trusted-type.h line 1
Quoted 2 lines
include/keys/trusted-type.h new file +1
-0@@ -0,0 +1,32 @@ +/* trusted-type.h: trusted-defined key type
There's no need to include the name of the file here. We know what the name of the file is.
include/keys/trusted-type.h line 22
Quoted 2 lines
include/keys/trusted-type.h new file +2
-0+struct trusted_key_payload { +struct rcu_head rcu;/* RCU destructor */
The comment is superfluous. That's what that struct is for.
include/keys/trusted-type.h line 26
Quoted 1 line
include/keys/trusted-type.h new file +1
-0+unsigned char key[MAX_KEY_SIZE +1 ];
In general, there should be spaces either side of binary operators like '+'.
security/Kconfig config KEYS · line 33
Quoted 10 lines
security/Kconfig +10
-0+config TRUSTED_KEYS + tristate "TRUSTED KEYS" + depends on KEYS && TCG_TPM + select CRYPTO + select CRYPTO_HMAC + select CRYPTO_SHA1 + help + This option provides support for creating, sealing, and unsealing + keys in the kernel. Trusted keys are random number symmetric keys, + generated and RSA-sealed by the TPM. The TPM only unseals the keys,
Superfluous comma.
security/Kconfig config KEYS · line 35
Quoted 2 lines
security/Kconfig +2
-0+ if the boot PCRs and other criteria match. Userspace can only ever + see encrypted blobs.
I'd say 'will' rather than 'can'.
Quoted 7 lines
security/keys/trusted_defined.c new file +1
-0@@ -0,0 +1,1101 @@ +/*
security/keys/trusted_defined.c line 11
Quoted 10 lines
security/keys/trusted_defined.c new file +10
-0+* Copyright (C) 2010 IBM Corporation +* +* Author: +* David Safford <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, version 2 of the License. +* +* Defines a new kernel key-type called 'trusted'. Trusted keys are random
Again 'Define'.
security/keys/trusted_defined.c line 14
Quoted 3 lines
security/keys/trusted_defined.c new file +3
-0+* number symmetric keys, generated and RSA-sealed by the TPM. The TPM only +* unseals the keys, if the boot PCRs and other criteria match. Userspace +* can only ever see encrypted blobs.
And two spaces after full stops please.
Quoted 18 lines
security/keys/trusted_defined.c new file +18
-0+* By default, trusted keys are sealed with the Storage Root Key (SRK). +* The SRK usage authorization value is the well-known value of 20 zeroes. +* This can be set at takeownership time with the trouser's utility +* "tpm_takeownership -u -z". +* +* Usage: +* keyctl add trusted name "new keylen [options]" ring +* keyctl update key "update [options]" +* keyctl add trusted name "load hex_blob [pcrlock=pcrnum]" ring +* keyctl print keyid +* +* options: +* keyhandle= ascii hex value of sealing key +* default 0x40000000 (SRK) +* keyauth= ascii hex auth for sealing key +* default 0x00... (40 ascii zeros) +* blobauth= ascii hex auth for sealed data +* default 0x00... (40 ascii zeros)
security/keys/trusted_defined.c line 43
Quoted 10 lines
security/keys/trusted_defined.c new file +10
-0+* pcrinfo= ascii hex of PCR_INFO or PCR_INFO_LONG +* (no default - create with genpcrinfo util) +* pcrlock= pcr number to be extended to "lock" blob +* migratable= 0|1 indicating permission to reseal to new PCR values +* default 1 (resealing allowed) +* +* keyctl print returns an ascii hex copy of the sealed key, which is in +* standard TPM_STORED_DATA format. +* keys can be 32 - 128 bytes, blob max is 1024 hex ascii characters +* binary pcrinfo max is 512 hex ascii characters
I would put this information in a file in the Documentation directory and put a pointer to the file here. You can then expand on the command structure you've defined in the documentation.
security/keys/trusted_defined.c line 66
Quoted 2 lines
security/keys/trusted_defined.c new file +2
-0+static char hmac_alg[] ="hmac(sha1)" ; +static char hash_alg[] ="sha1" ;
Should be const.
security/keys/trusted_defined.c line 75
Quoted 8 lines
security/keys/trusted_defined.c new file +8
-0+static int init_sha1_desc (struct hash_desc *desc) +{ +int rc; + + desc->tfm =crypto_alloc_hash (hash_alg,0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (desc->tfm)) { + rc =PTR_ERR (desc->tfm); +return rc;
Those should be merged into one line which would allow you to ditch the braces also.
security/keys/trusted_defined.c line 92
Quoted 9 lines
security/keys/trusted_defined.c new file +9
-0+static int init_hmac_desc (struct hash_desc *desc,unsigned char *key, +int keylen) +{ +int rc; + + desc->tfm =crypto_alloc_hash (hmac_alg,0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (desc->tfm)) { + rc =PTR_ERR (desc->tfm); +return rc;
Merge.
security/keys/trusted_defined.c line 110
Quoted 9 lines
security/keys/trusted_defined.c new file +9
-0+static int TSS_sha1 (unsigned char *data,int datalen,unsigned char *digest) +{ +struct hash_desc desc; +struct scatterlist sg[1 ]; +int rc; + + rc =init_sha1_desc (&desc); +if (rc !=0 ) +return rc;
I'd recommend you be consistent about the use of (rc != 0) and (rc < 0) to check for errors.
security/keys/trusted_defined.c line 112
Quoted 1 line
security/keys/trusted_defined.c new file +1
-0+sg_init_one (sg, data, datalen);
You should use the shash interface, not the hash interface. That way you can avoid the scatter-gather lists.
security/keys/trusted_defined.c line 117
Quoted 5 lines
security/keys/trusted_defined.c new file +5
-0+ rc =crypto_hash_update (&desc, sg, datalen); +if (rc <0 ) + goto out; + rc =crypto_hash_final (&desc, digest); +out:
You could dispense with the goto and the label simply by inverting the logic of the if-condition.
security/keys/trusted_defined.c line 126
Quoted 5 lines
security/keys/trusted_defined.c new file +5
-0+static int TSS_rawhmac (unsigned char *digest,unsigned char *key, +unsigned int keylen, ...) +{ +struct hash_desc desc; +struct scatterlist sg[1 ];
Again, use shash rather than hash to avoid the SG interface.
security/keys/trusted_defined.c line 138
Quoted 12 lines
security/keys/trusted_defined.c new file +12
-0+unsigned int dlen; +unsigned char *data; +va_list argp; +int error; + + error =init_hmac_desc (&desc, key, keylen); +if (error) +return error; + +va_start (argp, keylen); +for (;;) { + dlen = (unsigned int)va_arg(argp, unsigned int);
Doesn't va_arg() cast for you?
security/keys/trusted_defined.c line 141
Quoted 3 lines
security/keys/trusted_defined.c new file +3
-0+if (dlen ==0 ) +break ; + data = (unsigned char *)va_arg(argp, unsigned char *);
Ditto, plus several more within the file.
Quoted 7 lines
security/keys/trusted_defined.c new file +7
-0+/* +* Have the TPM seal(encrypt) the trusted key, possibly based on +* Platform Configuration Registers (PCRs). AUTH1 for sealing key. +*/ +static int tpm_seal(struct tpm_buf *tb, uint16_t keytype, + uint32_t keyhandle, unsigned char *keyauth, + unsigned char *data, uint32_t datalen,
security/keys/trusted_defined.c line 481
Quoted 10 lines
security/keys/trusted_defined.c new file +10
-0+ unsigned char *blob, uint32_t * bloblen, + unsigned char *blobauth, + unsigned char *pcrinfo, uint32_t pcrinfosize) +{ + struct osapsess sess; +unsigned char encauth[TPM_HASH_SIZE ]; +unsigned char pubauth[TPM_HASH_SIZE ]; +unsigned char xorwork[TPM_HASH_SIZE *2 ]; +unsigned char xorhash[TPM_HASH_SIZE ]; +unsigned char nonceodd[TPM_NONCE_SIZE ];
That's quite a lot of stack space, and you're calling other functions that also allocate chunks of stack space.
David
Re: [PATCH v1.3 3/4] keys: add new trusted key-type
Quoted 1 line · David Howells
Mimi Zohar <[email protected]> wrote:
Thanks for the helpful comments - much appreciated. Willdo on all of them - just one question on the last comment:
Quoted 12 lines
Quoted 12 lines
security/keys/trusted_defined.c new file +12
-0+/* +* Have the TPM seal(encrypt) the trusted key, possibly based on +* Platform Configuration Registers (PCRs). AUTH1 for sealing key. +*/ +static int tpm_seal (struct tpm_buf *tb,uint16_t keytype, +uint32_t keyhandle,unsigned char *keyauth, +unsigned char *data,uint32_t datalen, +unsigned char *blob,uint32_t * bloblen, +unsigned char *blobauth, +unsigned char *pcrinfo,uint32_t pcrinfosize) +{ +struct osapsess sess;
Quoted 8 lines
security/keys/trusted_defined.c line 481
Quoted 5 lines
security/keys/trusted_defined.c new file +5
-0+unsigned char encauth[TPM_HASH_SIZE ]; +unsigned char pubauth[TPM_HASH_SIZE ]; +unsigned char xorwork[TPM_HASH_SIZE *2 ]; +unsigned char xorhash[TPM_HASH_SIZE ]; +unsigned char nonceodd[TPM_NONCE_SIZE ];That's quite a lot of stack space, and you're calling other functions that also allocate chunks of stack space.
checkstack showed that the max stack usage was for tpm_seal, at 530 bytes. (The rest were 300 or less.) I can certainly throw the hashes and nonce (120 bytes) into a dynamically allocated struct, if you think it is worth the extra code...
thanks
dave
David Howells
maintainerRe: [PATCH v1.3 3/4] keys: add new trusted key-type
In reply to Mimi Zoharsecurity/keys/trusted_defined.c line 706
Quoted 6 lines · Mimi Zohar
security/keys/trusted_defined.c new file +6
-0+enum { + Opt_err =-1 , + Opt_new =1 , Opt_load, Opt_update, + Opt_keyhandle, Opt_keyauth, Opt_blobauth, + Opt_pcrinfo, Opt_pcrlock, Opt_migratable +};
The compiler can generate slightly more efficient code if you don't skip 0 in your enum.
Quoted 1 line
+static
match_table_tkey_tokens= {
const.
security/keys/trusted_defined.c line 733
Quoted 12 lines
security/keys/trusted_defined.c new file +12
-0+static int getoptions (char *c,struct trusted_key_payload *pay, +struct trusted_key_options *opt) +{ +substring_t args[MAX_OPT_ARGS ]; +char *p = c; +int token; +int res; +unsigned long handle; +unsigned long lock; + +while ((p =strsep (&c," \t" ))) { +if ((*p =='\0' ) || (*p ==' ' ) || (*p =='\t' ))
Superfluous brackets round the individual comparisons.
Quoted 6 lines
security/keys/trusted_defined.c new file +6
-0+continue ; + token =match_token (p, key_tokens, args); + +switch (token) { +case Opt_pcrinfo: + opt->pcrinfo_len =strlen (args[0 ].from) /2 ;
security/keys/trusted_defined.c line 749
Quoted 10 lines
security/keys/trusted_defined.c new file +10
-0+if (opt->pcrinfo_len >MAX_PCRINFO_SIZE ) +return -EINVAL ; +hex2bin (opt->pcrinfo, args[0 ].from, opt->pcrinfo_len); +break ; +case Opt_keyhandle: + res =strict_strtoul (args[0 ].from,16 , &handle); +if (res <0 ) +return -EINVAL ; + opt->keytype =SEALKEYTYPE ; + opt->keyhandle = (uint32_t ) handle;
Unnecessary cast.
Quoted 12 lines
security/keys/trusted_defined.c new file +12
-0+break ; +case Opt_keyauth: +if (strlen (args[0 ].from) !=2 *TPM_HASH_SIZE ) +return -EINVAL ; +hex2bin (opt->keyauth, args[0 ].from,TPM_HASH_SIZE ); +break ; +case Opt_blobauth: +if (strlen (args[0 ].from) !=2 *TPM_HASH_SIZE ) +return -EINVAL ; +hex2bin (opt->blobauth, args[0 ].from,TPM_HASH_SIZE ); +break ; +case Opt_migratable:
security/keys/trusted_defined.c line 771
Quoted 10 lines
security/keys/trusted_defined.c new file +10
-0+if (*args[0 ].from =='0' ) + pay->migratable =0 ; +else +return -EINVAL ; +break ; +case Opt_pcrlock: + res =strict_strtoul (args[0 ].from,10 , &lock); +if (res <0 ) +return -EINVAL ; + opt->pcrlock = (int )lock;
Unnecessary cast.
Quoted 9 lines
security/keys/trusted_defined.c new file +9
-0+break ; +default : +return -EINVAL ; + } + } +return 0 ; +} + +/*
security/keys/trusted_defined.c line 805
Quoted 10 lines
security/keys/trusted_defined.c new file +9
-0+* datablob_parse - parse the keyctl data and fill in the +* payload and options structures +* +* On success returns 0, otherwise -EINVAL. +*/ +static int datablob_parse (char *datablob,struct trusted_key_payload *p, +struct trusted_key_options *o) +{ + ret =strict_strtol (c,10 , (long *)&p->key_len);
NAK! You cannot do this. It won't work on 64-bit machines, especially big-endian ones. Casting the pointer does not change the size of the destination variable. You must use a temporary var.
security/keys/trusted_defined.c line 807
Quoted 2 lines
security/keys/trusted_defined.c new file +2
-0+if ((ret <0 ) || (p->key_len <MIN_KEY_SIZE ) || + (p->key_len >MAX_KEY_SIZE ))
Superfluous parenthesization.
Quoted 10 lines
security/keys/trusted_defined.c new file +9
-0+static int trusted_instantiate (struct key *key,const void *data, +size_t datalen) +{ +switch (key_cmd) { +case Opt_load: + ret =key_unseal (payload, options); +if (ret <0 ) +pr_info ("trusted_key: key_unseal failed (%d)\n" , ret); +break ;
security/keys/trusted_defined.c line 933
Quoted 10 lines
security/keys/trusted_defined.c new file +10
-0+case Opt_new: + ret =my_get_random (payload->key, payload->key_len); +if (ret <0 ) { +pr_info ("trusted_key: key_create failed (%d)\n" , ret); + goto out; + } + ret =key_seal (payload, options); +if (ret <0 ) +pr_info ("trusted_key: key_seal failed (%d)\n" , ret); +break ;
Aha! I see how this works now. Using add/update key seems the right way to do things.
security/keys/trusted_defined.c line 938
Quoted 5 lines
security/keys/trusted_defined.c new file +5
-0+default : + ret = -EINVAL ; + } +if (options->pcrlock) + ret =pcrlock (options->pcrlock);
Do you really want to go through pcrlock() if you're going to return -EINVAL?
security/keys/trusted_defined.c line 942
Quoted 4 lines
security/keys/trusted_defined.c new file +4
-0+out: +kfree (datablob); +if (options) +kfree (options);
kfree() can handle NULL pointers.
security/keys/trusted_defined.c line 946
Quoted 4 lines
security/keys/trusted_defined.c new file +4
-0+if (!ret) +rcu_assign_pointer (key->payload.data, payload); +else if (payload) +kfree (payload);
Again, kfree() can handle a NULL pointer.
Quoted 21 lines
security/keys/trusted_defined.h new file +21
-0+#define TPM_MAX_BUF_SIZE 512 +#define TPM_TAG_RQU_COMMAND 193 +#define TPM_TAG_RQU_AUTH1_COMMAND 194 +#define TPM_TAG_RQU_AUTH2_COMMAND 195 +#define TPM_TAG_RSP_COMMAND 196 +#define TPM_TAG_RSP_AUTH1_COMMAND 197 +#define TPM_TAG_RSP_AUTH2_COMMAND 198 +#define TPM_NONCE_SIZE 20 +#define TPM_HASH_SIZE 20 +#define TPM_SIZE_OFFSET 2 +#define TPM_RETURN_OFFSET 6 +#define TPM_DATA_OFFSET 10 +#define TPM_U32_SIZE 4 +#define TPM_GETRANDOM_SIZE 14 +#define TPM_GETRANDOM_RETURN 14 +#define TPM_ORD_GETRANDOM 70 +#define TPM_RESET_SIZE 10 +#define TPM_ORD_RESET 90 +#define TPM_OSAP_SIZE 36 +#define TPM_ORD_OSAP 11 +#define TPM_OIAP_SIZE 10
security/keys/trusted_defined.h line 34
Quoted 10 lines
security/keys/trusted_defined.h new file +10
-0+#define TPM_ORD_OIAP 10 +#define TPM_SEAL_SIZE 87 +#define TPM_ORD_SEAL 23 +#define TPM_ORD_UNSEAL 24 +#define TPM_UNSEAL_SIZE 104 +#define SEALKEYTYPE 1 +#define SRKKEYTYPE 4 +#define SRKHANDLE 0x40000000 +#define TPM_ANY_NUM 0xFFFF +#define MAX_PCRINFO_SIZE 64
I suspect some of these should be in somewhere like linux/tpm.h rather than here. They're specific to TPM access not TPM key management.
Quoted 9 lines
security/keys/trusted_defined.h new file +9
-0+#define LOAD32 (buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) +#define LOAD32N (buffer, offset) (*(uint32_t *)&buffer[offset]) +#define LOAD16 (buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) + +struct tpm_buf { +int len; +unsigned char data[TPM_MAX_BUF_SIZE ]; +}; +
security/keys/trusted_defined.h line 54
Quoted 10 lines
security/keys/trusted_defined.h new file +10
-0+#define INIT_BUF (tb) (tb->len = 0) + +struct osapsess { +uint32_t handle; +unsigned char secret[TPM_HASH_SIZE ]; +unsigned char enonce[TPM_NONCE_SIZE ]; +}; + +struct trusted_key_options { +uint16_t keytype;
key type enum?
security/keys/trusted_defined.h line 63
Quoted 9 lines
security/keys/trusted_defined.h new file +9
-0+uint32_t keyhandle; +unsigned char keyauth[TPM_HASH_SIZE ]; +unsigned char blobauth[TPM_HASH_SIZE ]; +uint32_t pcrinfo_len; +unsigned char pcrinfo[MAX_PCRINFO_SIZE ]; +int pcrlock; +}; + +#define TPM_DEBUG 0
The TPM_DEBUG stuff should probably be in the directory with the sources, not in a directory for others to include.
Quoted 72 lines
security/keys/trusted_defined.h new file +72
-0+#if TPM_DEBUG +static inline void dump_options (struct trusted_key_options *o) +{ +pr_info ("trusted_key: sealing key type %d\n" , o->keytype); +pr_info ("trusted_key: sealing key handle %0X\n" , o->keyhandle); +pr_info ("trusted_key: pcrlock %d\n" , o->pcrlock); +pr_info ("trusted_key: pcrinfo %d\n" , o->pcrinfo_len); +print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , +16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); +} + +static inline void dump_payload (struct trusted_key_payload *p) +{ +pr_info ("trusted_key: key_len %d\n" , p->key_len); +print_hex_dump (KERN_INFO ,"key " ,DUMP_PREFIX_NONE , +16 ,1 , p->key, p->key_len,0 ); +pr_info ("trusted_key: bloblen %d\n" , p->blob_len); +print_hex_dump (KERN_INFO ,"blob " ,DUMP_PREFIX_NONE , +16 ,1 , p->blob, p->blob_len,0 ); +pr_info ("trusted_key: migratable %d\n" , p->migratable); +} + +static inline void dump_sess (struct osapsess *s) +{ +print_hex_dump (KERN_INFO ,"trusted-key: handle " ,DUMP_PREFIX_NONE , +16 ,1 , &s->handle,4 ,0 ); +pr_info ("trusted-key: secret:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->secret,TPM_HASH_SIZE ,0 ); +pr_info ("trusted-key: enonce:\n" ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE , +16 ,1 , &s->enonce,TPM_HASH_SIZE ,0 ); +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +int len; + +pr_info ("\ntrusted-key: tpm buffer\n" ); + len =LOAD32 (buf,TPM_SIZE_OFFSET ); +print_hex_dump (KERN_INFO ,"" ,DUMP_PREFIX_NONE ,16 ,1 , buf, len,0 ); +} +#else +static inline void dump_options (struct trusted_key_options *o) +{ +} + +static inline void dump_payload (struct trusted_key_payload *p) +{ +} + +static inline void dump_sess (struct osapsess *s) +{ +} + +static inline void dump_tpm_buf (unsigned char *buf) +{ +} +#endif + +static inline void store8 (struct tpm_buf *buf,unsigned char value) +{ + buf->data[buf->len++] = value; +} + +static inline void store16 (struct tpm_buf *buf,uint16_t value) +{ + *(uint16_t *) & buf->data[buf->len] =htons (value); + buf->len +=sizeof (value); +} + +static inline void store32 (struct tpm_buf *buf,uint32_t value)
security/keys/trusted_defined.h line 146
Quoted 10 lines
security/keys/trusted_defined.h new file +10
-0+{ + *(uint32_t *) & buf->data[buf->len] =htonl (value); + buf->len +=sizeof (value); +} + +static inline void storebytes (struct tpm_buf *buf,unsigned char *in,int len) +{ +memcpy (buf->data + buf->len, in, len); + buf->len += len; +}
Also these look like internal functions which shouldn't be in the global headers.
David
Re: [PATCH v1.3 3/4] keys: add new trusted key-type
Quoted 1 line · David Howells
Mimi Zohar <[email protected]> wrote:
Again, thanks for the detailed review!
Willdo on all suggestions with a couple of comments/questions:
Quoted 26 lines
Quoted 26 lines
security/keys/trusted_defined.h new file +26
-0+#define TPM_MAX_BUF_SIZE 512 +#define TPM_TAG_RQU_COMMAND 193 +#define TPM_TAG_RQU_AUTH1_COMMAND 194 +#define TPM_TAG_RQU_AUTH2_COMMAND 195 +#define TPM_TAG_RSP_COMMAND 196 +#define TPM_TAG_RSP_AUTH1_COMMAND 197 +#define TPM_TAG_RSP_AUTH2_COMMAND 198 +#define TPM_NONCE_SIZE 20 +#define TPM_HASH_SIZE 20 +#define TPM_SIZE_OFFSET 2 +#define TPM_RETURN_OFFSET 6 +#define TPM_DATA_OFFSET 10 +#define TPM_U32_SIZE 4 +#define TPM_GETRANDOM_SIZE 14 +#define TPM_GETRANDOM_RETURN 14 +#define TPM_ORD_GETRANDOM 70 +#define TPM_RESET_SIZE 10 +#define TPM_ORD_RESET 90 +#define TPM_OSAP_SIZE 36 +#define TPM_ORD_OSAP 11 +#define TPM_OIAP_SIZE 10 +#define TPM_ORD_OIAP 10 +#define TPM_SEAL_SIZE 87 +#define TPM_ORD_SEAL 23 +#define TPM_ORD_UNSEAL 24 +#define TPM_UNSEAL_SIZE 104
Quoted 8 lines
security/keys/trusted_defined.h line 34
Quoted 5 lines
security/keys/trusted_defined.h new file +5
-0+#define SEALKEYTYPE 1 +#define SRKKEYTYPE 4 +#define SRKHANDLE 0x40000000 +#define TPM_ANY_NUM 0xFFFF +#define MAX_PCRINFO_SIZE 64I suspect some of these should be in somewhere like
linux/tpm.hrather than here. They're specific to TPM access not TPM key management.
Most of them are not already defined in tpm.h, as they are never used there, but you are right, some are generic. I'll double check these..
Quoted 4 lines
Quoted 1 line
+#define
TPM_DEBUG0The
TPM_DEBUGstuff should probably be in the directory with the sources, not in a directory for others to include.
Maybe some confusion here - trusted_defined.h is in the sources - only trusted-type.h is public in include/keys/.
Quoted 11 lines
Quoted 11 lines
security/keys/trusted_defined.h new file +11
-0+#if TPM_DEBUG +static inline void dump_options (struct trusted_key_options *o) +{ +pr_info ("trusted_key: sealing key type %d\n" , o->keytype); +pr_info ("trusted_key: sealing key handle %0X\n" , o->keyhandle); +pr_info ("trusted_key: pcrlock %d\n" , o->pcrlock); +pr_info ("trusted_key: pcrinfo %d\n" , o->pcrinfo_len); +print_hex_dump (KERN_INFO ,"pcrinfo " ,DUMP_PREFIX_NONE , +16 ,1 , o->pcrinfo, o->pcrinfo_len,0 ); +} +
...
Quoted 13 lines
Quoted 13 lines
security/keys/trusted_defined.h new file +13
-0+ +static inline void store16 (struct tpm_buf *buf,uint16_t value) +{ + *(uint16_t *) & buf->data[buf->len] =htons (value); + buf->len +=sizeof (value); +} + +static inline void store32 (struct tpm_buf *buf,uint32_t value) +{ + *(uint32_t *) & buf->data[buf->len] =htonl (value); + buf->len +=sizeof (value); +} +
Quoted 8 lines
security/keys/trusted_defined.h line 146
Quoted 5 lines
security/keys/trusted_defined.h new file +5
-0+static inline void storebytes (struct tpm_buf *buf,unsigned char *in,int len) +{ +memcpy (buf->data + buf->len, in, len); + buf->len += len; +}Also these look like internal functions which shouldn't be in the global headers.
This is in trusted_defined.h, which is with sources, not in include/keys/
thanks!
dave
David Howells
maintainerRe: [PATCH v1.3 3/4] keys: add new trusted key-type
Quoted 7 lines · David Safford
Quoted 4 lines
Quoted 1 line
+#define
TPM_DEBUG0The
TPM_DEBUGstuff should probably be in the directory with the sources, not in a directory for others to include.Maybe some confusion here -
trusted_defined.his in the sources - onlytrusted-type.his public ininclude/keys/.
Sorry, you're right.
David
PATCH 4/4 keys: add new key-type encrypted
In reply to Mimi ZoharDefines a new kernel key-type called 'encrypted'. Encrypted keys are kernel generated random numbers, which are encrypted/decrypted with a 'trusted' symmetric key. Encrypted keys are created/encrypted/decrypted in the kernel. Userspace only ever sees/stores encrypted blobs.
Changelog:
- allocate derived_buf dynamically to support arbitrary length master key (fixed by Roberto Sassu)
- wait until late_initcall for crypto libraries to be registered
- cleanup security/Kconfig
- Add missing 'update' keyword (reported/fixed by Roberto Sassu)
- Free epayload on failure to create key (reported/fixed by Roberto Sassu)
- Increase the data size limit (requested by Roberto Sassu)
- Crypto return codes are always 0 on success and negative on failure, remove unnecessary tests.
- Replaced kzalloc() with kmalloc()
--- include/keys/encrypted-type.h | 30 ++ security/Kconfig | 16 + security/keys/Makefile | 1 + security/keys/encrypted_defined.c | 816 +++++++++++++++++++++++++++++++++++++ security/keys/encrypted_defined.h | 52 +++ 5 files changed, 915 insertions(+), 0 deletions(-) create mode 100644 include/keys/encrypted-type.h create mode 100644 security/keys/encrypted_defined.c create mode 100644 security/keys/encrypted_defined.h
include/keys/encrypted-type.h new file +30 -0
@@ -0,0 +1,30 @@ +/* encrypted-type.h: encrypted-defined key type +* +* Copyright (C) 2010 IBM Corporation +* Author: Mimi Zohar <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, version 2 of the License. +*/ + +#ifndef _KEYS_ENCRYPTED_TYPE_H +#define _KEYS_ENCRYPTED_TYPE_H + +#include <linux/key.h> +#include <linux/rcupdate.h> + +struct encrypted_key_payload { +struct rcu_head rcu;/* RCU destructor */ +char *master_desc;/* datablob: master key name */ +char *datalen;/* datablob: decrypted key length */ +void *iv;/* datablob: iv */ +void *encrypted_data;/* datablob: encrypted key */ +unsigned short datablob_len;/* length of datablob */ +unsigned short decrypted_datalen;/* decrypted data length */ +char decrypted_data[0 ];/* decrypted data + datablob + hmac */ +}; + +extern struct key_type key_type_encrypted; + +#endif /* _KEYS_ENCRYPTED_TYPE_H */
security/Kconfig +16 -0
@@ -36,6 +36,22 @@ config TRUSTED_KEYS If you are unsure as to whether this is required, answer N. +config ENCRYPTED_KEYS + tristate "ENCRYPTED KEYS" + depends on KEYS && TRUSTED_KEYS + select CRYPTO_AES + select CRYPTO_CBC + select CRYPTO_SHA256 + select CRYPTO_RNG + help + This option provides support for create/encrypting/decrypting keys + in the kernel. Encrypted keys are kernel generated random numbers, + which are encrypted/decrypted with a 'master' symmetric key. The + 'master' key can be either a trusted-key or user-key type. + Userspace only ever sees/stores encrypted blobs. + + If you are unsure as to whether this is required, answer N. + config KEYS_DEBUG_PROC_KEYS bool "Enable the /proc/keys file by which keys may be viewed" depends on KEYS
security/keys/Makefile +1 -0
@@ -14,6 +14,7 @@ obj-y := \ user_defined.o obj-$(CONFIG_TRUSTED_KEYS) += trusted_defined.o +obj-$(CONFIG_ENCRYPTED_KEYS) += encrypted_defined.o obj-$(CONFIG_KEYS_COMPAT) += compat.o obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_SYSCTL) += sysctl.o
security/keys/encrypted_defined.c new file +816 -0
816 lines not on this page · raw message
security/keys/encrypted_defined.h new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
1.7.2.2
David Howells
maintainerRe: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 1 line · Mimi Zohar
Defines a new kernel key-type called 'encrypted'. Encrypted keys are
Many of the comments I made against patch #3 also apply here. Use 'Define' rather than 'Defines' here for example.
include/keys/encrypted-type.h line 1
Quoted 5 lines
index 0000000..e2312e0
include/keys/encrypted-type.h new file +1
-0@@ -0,0 +1,30 @@ +/* encrypted-type.h: encrypted-defined key type
Don't include the names of files in the files.
include/keys/encrypted-type.h line 18
Quoted 2 lines
include/keys/encrypted-type.h new file +2
-0+struct encrypted_key_payload { +struct rcu_head rcu;/* RCU destructor */
That comment is not really needed.
include/keys/encrypted-type.h line 22
Quoted 4 lines
include/keys/encrypted-type.h new file +4
-0+char *master_desc;/* datablob: master key name */ +char *datalen;/* datablob: decrypted key length */ +void *iv;/* datablob: iv */ +void *encrypted_data;/* datablob: encrypted key */
But the variable name is 'encrypted_data'...
include/keys/encrypted-type.h line 25
Quoted 3 lines
include/keys/encrypted-type.h new file +3
-0+unsigned short datablob_len;/* length of datablob */ +unsigned short decrypted_datalen;/* decrypted data length */ +char decrypted_data[0 ];/* decrypted data + datablob + hmac */
If any of these are binary data rather than character data, I'd use u8 rather than char.
Quoted 4 lines
security/keys/encrypted_defined.c new file +1
-0@@ -0,0 +1,816 @@ +/*
security/keys/encrypted_defined.c line 11
Quoted 10 lines
security/keys/encrypted_defined.c new file +10
-0+* Copyright (C) 2010 IBM Corporation +* +* Author: +* Mimi Zohar <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, version 2 of the License. +* +* File: encrypted_defined.c
Don't do that.
security/keys/encrypted_defined.c line 21
Quoted 10 lines
security/keys/encrypted_defined.c new file +10
-0+* +* Defines a new kernel key-type called 'encrypted'. Encrypted keys +* are kernel generated random numbers, which are encrypted/decrypted +* using a 'master' key. The 'master' key can either be a trusted-key or +* user-key type. Encrypted keys are created/encrypted/decrypted in the +* kernel. Userspace ever only sees/stores encrypted blobs. +* +* keyctl add "encrypted" "name" "NEW master-key-name keylen" ring +* keyctl add "encrypted" "name" "LOAD master-key-name keylen hex_blob" ring +* keyctl update keyid "UPDATE master-key-name"
Merge with the documentation file in Documentation/ from patch 3 and stick a pointer to that file here.
security/keys/encrypted_defined.c line 44
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static char hash_alg[] ="sha256" ; +static char hmac_alg[] ="hmac(sha256)" ;
const.
security/keys/encrypted_defined.c line 45
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static int hash_size =SHA256_DIGEST_SIZE ;
??? This is a numeric constant, but you're telling the compiler you want to store it in the .data section. Is there a reason?
security/keys/encrypted_defined.c line 46
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static char blkcipher_alg[] ="cbc(aes)" ;
const.
security/keys/encrypted_defined.c line 50
Quoted 4 lines
security/keys/encrypted_defined.c new file +4
-0+static int ivsize; +static int blksize; +static int MAX_DATA_SIZE =4096 ; +static int MIN_DATA_SIZE =20 ;
Initialised numeric constants.
security/keys/encrypted_defined.c line 52
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static int aes_get_sizes (int *ivsize,int *blksize)
It's only used in one place in the file, and it only sets global vars. Why pass pointers to them?
security/keys/encrypted_defined.c line 71
Quoted 4 lines
security/keys/encrypted_defined.c new file +4
-0+enum { + Opt_err =-1 , Opt_new =1 , Opt_load, + Opt_update, Opt_NEW, Opt_LOAD, Opt_UPDATE +};
Why skip 0?
security/keys/encrypted_defined.c line 73
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static match_table_t key_tokens = {
const.
security/keys/encrypted_defined.c line 80
Quoted 7 lines
security/keys/encrypted_defined.c new file +7
-0+ {Opt_new, "new"}, + {Opt_NEW, "NEW"}, + {Opt_load, "load"}, + {Opt_LOAD, "LOAD"}, + {Opt_update, "update"}, + {Opt_UPDATE, "UPDATE"}, + {Opt_err, NULL}
Is case significant? If not, you don't need duplicated constants. Why do you care about the capitalisation here (and only in a restricted sense) when you didn't in the trusted keys patch?
security/keys/encrypted_defined.c line 150
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+/* datablob_format - format as an ascii string, before copying to userspace */
I would split this over three lines.
security/keys/encrypted_defined.c line 156
Quoted 6 lines
security/keys/encrypted_defined.c new file +6
-0+static int datablob_format (char __user *buffer, +struct encrypted_key_payload *epayload, +int asciiblob_len) +{ +char *ascii_buf, *bufp; +char *iv = (char *)epayload->iv;
Unnecessary cast. iv is void*.
security/keys/encrypted_defined.c line 165
Quoted 9 lines
security/keys/encrypted_defined.c new file +9
-0+int ret =0 ; +int len; +int i; + + ascii_buf =kmalloc (asciiblob_len +1 ,GFP_KERNEL ); +if (!ascii_buf) +return -ENOMEM ; + + *(ascii_buf + asciiblob_len) ='\0' ;
That's what square brackets are for.
security/keys/encrypted_defined.c line 167
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+ len =sprintf (ascii_buf,"%s %s " , epayload->master_desc, + epayload->datalen);
datalen is not a string.
security/keys/encrypted_defined.c line 199
Quoted 13 lines
security/keys/encrypted_defined.c new file +13
-0+static struct key *request_trusted_key (char *trusted_desc,void **master_key, +unsigned int *master_keylen) +{ +struct trusted_key_payload *tpayload; +struct key *tkey; + + tkey =request_key (&key_type_trusted, trusted_desc,NULL ); +if (IS_ERR (tkey)) + goto error; + + tpayload = tkey->payload.data; + *master_key = tpayload->key; + *master_keylen = tpayload->key_len;
You can't do this. You've defined an update method, so the pointer may change under you, and the pointee may be destroyed without warning. You've been using RCU, so you should use that. You need to use rcu_read_{,un}lock() and rcu_dereference() and you must be aware that the payload may be destroyed from the moment you drop the RCU read lock.
You're returning a pointer to the key, so you perhaps don't really need to access the payload at this point.
security/keys/encrypted_defined.c line 221
Quoted 13 lines
security/keys/encrypted_defined.c new file +13
-0+static struct key *request_user_key (char *master_desc,void **master_key, +unsigned int *master_keylen) +{ +struct user_key_payload *upayload; +struct key *ukey; + + ukey =request_key (&key_type_user, master_desc,NULL ); +if (IS_ERR (ukey)) + goto error; + + upayload = ukey->payload.data; + *master_key = upayload->data; + *master_keylen = (unsigned int )upayload->datalen;
Ditto.
security/keys/encrypted_defined.c line 230
Quoted 5 lines
security/keys/encrypted_defined.c new file +5
-0+static int init_desc (struct hash_desc *desc,char *alg) +{ +int ret; + + desc->tfm =crypto_alloc_hash (alg,0 ,CRYPTO_ALG_ASYNC );
Use shash so that you don't have to use the SG interface.
security/keys/encrypted_defined.c line 245
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int calc_hmac (char *digest,char *key,int keylen, +char *buf,size_t buflen)
Can key be const? Should digest, key and buf be u8* or void* if they're binary data rather than string data? This sort of thing should perhaps be percolated through all your functions.
security/keys/encrypted_defined.c line 269
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static int calc_hash (char *digest,void *buf,int buflen)
Can buf be const?
security/keys/encrypted_defined.c line 291
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int get_derived_key (char *derived_key,enum derived_key_type key_type, +void *master_key,unsigned int master_keylen)
master_key should be const. master_keylen should perhaps be size_t.
Quoted 10 lines
security/keys/encrypted_defined.c new file +10
-0+static struct key *request_master_key (struct encrypted_key_payload *epayload, +void **master_key, +unsigned int *master_keylen) +{ +struct key *mkey; + + mkey =request_trusted_key (epayload->master_desc, + master_key, master_keylen); +if (IS_ERR (mkey)) { + mkey = request_user_key(epayload->master_desc,
security/keys/encrypted_defined.c line 360
Quoted 10 lines
security/keys/encrypted_defined.c new file +10
-0+ master_key, master_keylen); +if (IS_ERR (mkey)) { +pr_info ("encrypted_key: trusted/user key %s not found" , + epayload->master_desc); +return mkey; + } + } +dump_master_key (*master_key, *master_keylen); +return mkey; +}
Why do you allow the master key to be supplied by a user-defined key rather than requiring a trusted-key unconditionally?
Note that the description of a user-defined key should prefixed to distinguish it from other random user-defined keys.
security/keys/encrypted_defined.c line 364
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int derived_key_encrypt (struct encrypted_key_payload *epayload, +void *derived_key,unsigned int derived_keylen)
Should derived_key be const and derived_keylen be size_t?
security/keys/encrypted_defined.c line 120
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int datablob_hmac_append (struct encrypted_key_payload *epayload, +void *master_key,unsigned int master_keylen)
More const and size_t? There are plenty more places where you should probably be using const or size_t. I'm not going to list any further
security/keys/encrypted_defined.c line 658
Quoted 7 lines
security/keys/encrypted_defined.c new file +7
-0+static void encrypted_rcu_free (struct rcu_head *rcu) +{ +struct encrypted_key_payload *epayload; + + epayload = container_of(rcu, struct encrypted_key_payload, rcu); +memset (epayload->decrypted_data,0 , epayload->decrypted_datalen); +kfree (epayload);
I presume you don't need to release the stuff pointed to by epayload before the memset because __ekey_init() makes the pointers point at offsets into the payload blob.
security/keys/encrypted_defined.c line 805
Quoted 9 lines
security/keys/encrypted_defined.c new file +9
-0+static int __init init_encrypted (void ) +{ +int ret; + + ret =register_key_type (&key_type_encrypted); +if (ret <0 ) +return ret; + ret =aes_get_sizes (&ivsize, &blksize); +return ret;
Merge those two lines into one.
security/keys/encrypted_defined.c line 140
Quoted 4 lines
security/keys/encrypted_defined.c new file +4
-0+static inline void dump_master_key (void *master_key,unsigned int master_keylen) +{ +print_hex_dump (KERN_ERR ,"master key: " ,DUMP_PREFIX_NONE ,32 ,1 , + master_key, (size_t ) master_keylen,0 );
You don't need to cast master_keylen like this. The compiler will do that automatically. In fact, master_key should be const and master_key_len should probably be size_t.
Quoted 2 lines
security/keys/encrypted_defined.c new file +1
-0+static inline void dump_decrypted_data(struct encrypted_key_payload*epayload)
const.
security/keys/encrypted_defined.c
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static inline void dump_encrypted_data (struct encrypted_key_payload *epayload, +unsigned int encrypted_datalen)
const and size_t. And further instances thereof further down.
David
Re: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 6 lines · David Howells
Quoted 1 line · Mimi Zohar
Defines a new kernel key-type called 'encrypted'. Encrypted keys are
Many of the comments I made against patch #3 also apply here. Use 'Define' rather than 'Defines' here for example.
Was expecting your comments. Already started making the changes ...
Quoted 79 lines
include/keys/encrypted-type.h line 1
Quoted 5 lines
index 0000000..e2312e0
include/keys/encrypted-type.h new file +1
-0@@ -0,0 +1,30 @@ +/* encrypted-type.h: encrypted-defined key type Don't include the names of files in the files.
include/keys/encrypted-type.h line 18
Quoted 2 lines
include/keys/encrypted-type.h new file +2
-0+struct encrypted_key_payload { +struct rcu_head rcu;/* RCU destructor */ That comment is not really needed.
include/keys/encrypted-type.h line 22
Quoted 4 lines
include/keys/encrypted-type.h new file +4
-0+char *master_desc;/* datablob: master key name */ +char *datalen;/* datablob: decrypted key length */ +void *iv;/* datablob: iv */ +void *encrypted_data;/* datablob: encrypted key */ But the variable name is '
encrypted_data'...include/keys/encrypted-type.h line 25
Quoted 3 lines
include/keys/encrypted-type.h new file +3
-0+unsigned short datablob_len;/* length of datablob */ +unsigned short decrypted_datalen;/* decrypted data length */ +char decrypted_data[0 ];/* decrypted data + datablob + hmac */ If any of these are binary data rather than character data, I'd use u8 rather than char.
Quoted 4 lines
security/keys/encrypted_defined.c new file +1
-0@@ -0,0 +1,816 @@ +/* security/keys/encrypted_defined.c line 11
Quoted 10 lines
security/keys/encrypted_defined.c new file +10
-0+* Copyright (C) 2010 IBM Corporation +* +* Author: +* Mimi Zohar <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, version 2 of the License. +* +* File: encrypted_defined.c Don't do that.
security/keys/encrypted_defined.c line 21
Quoted 10 lines
security/keys/encrypted_defined.c new file +10
-0+* +* Defines a new kernel key-type called 'encrypted'. Encrypted keys +* are kernel generated random numbers, which are encrypted/decrypted +* using a 'master' key. The 'master' key can either be a trusted-key or +* user-key type. Encrypted keys are created/encrypted/decrypted in the +* kernel. Userspace ever only sees/stores encrypted blobs. +* +* keyctl add "encrypted" "name" "NEW master-key-name keylen" ring +* keyctl add "encrypted" "name" "LOAD master-key-name keylen hex_blob" ring +* keyctl update keyid "UPDATE master-key-name" Merge with the documentation file in
Documentation/from patch 3 and stick a pointer to that file here.security/keys/encrypted_defined.c line 44
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static char hash_alg[] ="sha256" ; +static char hmac_alg[] ="hmac(sha256)" ;const.
security/keys/encrypted_defined.c line 45
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static int hash_size =SHA256_DIGEST_SIZE ;??? This is a numeric constant, but you're telling the compiler you want to store it in the .data section. Is there a reason?
security/keys/encrypted_defined.c line 46
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static char blkcipher_alg[] ="cbc(aes)" ;const.
security/keys/encrypted_defined.c line 50
Quoted 4 lines
security/keys/encrypted_defined.c new file +4
-0+static int ivsize; +static int blksize; +static int MAX_DATA_SIZE =4096 ; +static int MIN_DATA_SIZE =20 ;Initialised numeric constants.
Quoted 4 lines
security/keys/encrypted_defined.c line 52
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static int aes_get_sizes (int *ivsize,int *blksize)It's only used in one place in the file, and it only sets global vars. Why pass pointers to them?
agreed
Quoted 11 lines
security/keys/encrypted_defined.c line 71
Quoted 4 lines
security/keys/encrypted_defined.c new file +4
-0+enum { + Opt_err =-1 , Opt_new =1 , Opt_load, + Opt_update, Opt_NEW, Opt_LOAD, Opt_UPDATE +};Why skip 0?
security/keys/encrypted_defined.c line 73
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static match_table_t key_tokens = {const.
Quoted 11 lines
security/keys/encrypted_defined.c line 80
Quoted 7 lines
security/keys/encrypted_defined.c new file +7
-0+ {Opt_new, "new"}, + {Opt_NEW, "NEW"}, + {Opt_load, "load"}, + {Opt_LOAD, "LOAD"}, + {Opt_update, "update"}, + {Opt_UPDATE, "UPDATE"}, + {Opt_err, NULL}Is case significant? If not, you don't need duplicated constants. Why do you care about the capitalisation here (and only in a restricted sense) when you didn't in the trusted keys patch?
No, was being consistent with trusted keys, and then uppercase was removed there, but left here. Will remove.
Quoted 13 lines
security/keys/encrypted_defined.c line 150
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+/* datablob_format - format as an ascii string, before copying to userspace */ I would split this over three lines.
security/keys/encrypted_defined.c line 156
Quoted 6 lines
security/keys/encrypted_defined.c new file +6
-0+static int datablob_format (char __user *buffer, +struct encrypted_key_payload *epayload, +int asciiblob_len) +{ +char *ascii_buf, *bufp; +char *iv = (char *)epayload->iv;Unnecessary cast. iv is void*.
Quoted 11 lines
security/keys/encrypted_defined.c line 165
Quoted 9 lines
security/keys/encrypted_defined.c new file +9
-0+int ret =0 ; +int len; +int i; + + ascii_buf =kmalloc (asciiblob_len +1 ,GFP_KERNEL ); +if (!ascii_buf) +return -ENOMEM ; + + *(ascii_buf + asciiblob_len) ='\0' ;That's what square brackets are for.
Yes, that would be clearer.
Quoted 4 lines
security/keys/encrypted_defined.c line 167
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+ len =sprintf (ascii_buf,"%s %s " , epayload->master_desc, + epayload->datalen);datalen is not a string.
I know I already fixed this. Somehow it crept back in.
Quoted 11 lines
Quoted 11 lines
security/keys/encrypted_defined.c new file +11
-0+static struct key *request_trusted_key (char *trusted_desc,void **master_key, +unsigned int *master_keylen) +{ +struct trusted_key_payload *tpayload; +struct key *tkey; + + tkey =request_key (&key_type_trusted, trusted_desc,NULL ); +if (IS_ERR (tkey)) + goto error; + + tpayload = tkey->payload.data;
Quoted 8 lines
security/keys/encrypted_defined.c line 199
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+ *master_key = tpayload->key; + *master_keylen = tpayload->key_len;You can't do this. You've defined an update method, so the pointer may change under you, and the pointee may be destroyed without warning. You've been using RCU, so you should use that. You need to use rcu_read_{,un}lock() and
rcu_dereference()and you must be aware that the payload may be destroyed from the moment you drop the RCU read lock.
Thanks for explaining the locking necessary here. Update of an encrypted key changes the name of the key used to encrypt the data. There are no guarantees that the key will remain around. So I'm not concerned that after we release the RCU lock, the key could disappear.
Quoted 2 lines
You're returning a pointer to the key, so you perhaps don't really need to access the payload at this point.
Returning a pointer to the key was in order to do the key_put. The locking probably should be done in request_master_key.
Quoted 16 lines
security/keys/encrypted_defined.c line 221
Quoted 13 lines
security/keys/encrypted_defined.c new file +13
-0+static struct key *request_user_key (char *master_desc,void **master_key, +unsigned int *master_keylen) +{ +struct user_key_payload *upayload; +struct key *ukey; + + ukey =request_key (&key_type_user, master_desc,NULL ); +if (IS_ERR (ukey)) + goto error; + + upayload = ukey->payload.data; + *master_key = upayload->data; + *master_keylen = (unsigned int )upayload->datalen;Ditto.
Quoted 7 lines
security/keys/encrypted_defined.c line 230
Quoted 5 lines
security/keys/encrypted_defined.c new file +5
-0+static int init_desc (struct hash_desc *desc,char *alg) +{ +int ret; + + desc->tfm =crypto_alloc_hash (alg,0 ,CRYPTO_ALG_ASYNC );Use shash so that you don't have to use the SG interface.
Will look into it.
Quoted 6 lines
security/keys/encrypted_defined.c line 245
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int calc_hmac (char *digest,char *key,int keylen, +char *buf,size_t buflen)Can key be const? Should digest, key and buf be u8* or void* if they're binary data rather than string data? This sort of thing should perhaps be percolated through all your functions.
Yes, will review the types used through out, adding const when possible.
Quoted 24 lines
security/keys/encrypted_defined.c line 269
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+static int calc_hash (char *digest,void *buf,int buflen)Can buf be const?
security/keys/encrypted_defined.c line 291
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int get_derived_key (char *derived_key,enum derived_key_type key_type, +void *master_key,unsigned int master_keylen)
master_keyshould be const.master_keylenshould perhaps besize_t.Quoted 15 lines
security/keys/encrypted_defined.c new file +15
-0+static struct key *request_master_key (struct encrypted_key_payload *epayload, +void **master_key, +unsigned int *master_keylen) +{ +struct key *mkey; + + mkey =request_trusted_key (epayload->master_desc, + master_key, master_keylen); +if (IS_ERR (mkey)) { + mkey =request_user_key (epayload->master_desc, + master_key, master_keylen); +if (IS_ERR (mkey)) { +pr_info ("encrypted_key: trusted/user key %s not found" , + epayload->master_desc); +return mkey;
Quoted 8 lines
security/keys/encrypted_defined.c line 360
Quoted 5 lines
security/keys/encrypted_defined.c new file +5
-0+ } + } +dump_master_key (*master_key, *master_keylen); +return mkey; +}Why do you allow the master key to be supplied by a user-defined key rather than requiring a trusted-key unconditionally?
This is for systems without a TPM. The logic needs to exist, whether it is here or in EVM. By doing it here, a user could provide a passphrase in the initramfs, which is used to decrypt the encrypted key.
Quoted 14 lines
Note that the description of a user-defined key should prefixed to distinguish it from other random user-defined keys.
security/keys/encrypted_defined.c line 364
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int derived_key_encrypt (struct encrypted_key_payload *epayload, +void *derived_key,unsigned int derived_keylen)Should
derived_keybe const andderived_keylenbesize_t?security/keys/encrypted_defined.c line 120
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static int datablob_hmac_append (struct encrypted_key_payload *epayload, +void *master_key,unsigned int master_keylen)More const and
size_t? There are plenty more places where you should probably be using const orsize_t. I'm not going to list any further
Quoted 11 lines
security/keys/encrypted_defined.c line 658
Quoted 7 lines
security/keys/encrypted_defined.c new file +7
-0+static void encrypted_rcu_free (struct rcu_head *rcu) +{ +struct encrypted_key_payload *epayload; + + epayload = container_of(rcu, struct encrypted_key_payload, rcu); +memset (epayload->decrypted_data,0 , epayload->decrypted_datalen); +kfree (epayload);I presume you don't need to release the stuff pointed to by epayload before the memset because
__ekey_init()makes the pointers point at offsets into the payload blob.
right
Quoted 11 lines
security/keys/encrypted_defined.c line 805
Quoted 9 lines
security/keys/encrypted_defined.c new file +9
-0+static int __init init_encrypted (void ) +{ +int ret; + + ret =register_key_type (&key_type_encrypted); +if (ret <0 ) +return ret; + ret =aes_get_sizes (&ivsize, &blksize); +return ret;Merge those two lines into one.
ok
Quoted 12 lines
security/keys/encrypted_defined.c line 140
Quoted 4 lines
security/keys/encrypted_defined.c new file +4
-0+static inline void dump_master_key (void *master_key,unsigned int master_keylen) +{ +print_hex_dump (KERN_ERR ,"master key: " ,DUMP_PREFIX_NONE ,32 ,1 , + master_key, (size_t ) master_keylen,0 );You don't need to cast
master_keylenlike this. The compiler will do that automatically. In fact,master_keyshould be const andmaster_key_lenshould probably besize_t.Quoted 2 lines
security/keys/encrypted_defined.c new file +1
-0+static inline void dump_decrypted_data(struct encrypted_key_payload*epayload)
Quoted 8 lines
const.
security/keys/encrypted_defined.c
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+static inline void dump_encrypted_data (struct encrypted_key_payload *epayload, +unsigned int encrypted_datalen)const and
size_t. And further instances thereof further down.David
thanks,
Mimi
David Howells
maintainerRe: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 6 lines · Mimi Zohar
Quoted 2 lines
Why do you allow the master key to be supplied by a user-defined key rather than requiring a trusted-key unconditionally?
This is for systems without a TPM. The logic needs to exist, whether it is here or in EVM. By doing it here, a user could provide a passphrase in the initramfs, which is used to decrypt the encrypted key.
I thought that might be the case. In which case, it might be better to allow someone to add a trusted key, supplying both encrypted and unencrypted versions of the data so that the TPM need not be consulted. You might want to mark such a key so that it can be seen when it is dumped.
But if you're going to use a user-defined key, you really need to prefix the description with something suitable.
David
Re: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 5 lines · David Howells
Quoted 3 lines · Mimi Zohar
Quoted 2 lines
Why do you allow the master key to be supplied by a user-defined key rather than requiring a trusted-key unconditionally?
Quoted 8 lines
Quoted 3 lines
This is for systems without a TPM. The logic needs to exist, whether it is here or in EVM. By doing it here, a user could provide a passphrase in the initramfs, which is used to decrypt the encrypted key.
I thought that might be the case. In which case, it might be better to allow someone to add a trusted key, supplying both encrypted and unencrypted versions of the data so that the TPM need not be consulted. You might want to mark such a key so that it can be seen when it is dumped.
At least to me, the name 'trusted' implies some form of HW.
Quoted 4 lines
But if you're going to use a user-defined key, you really need to prefix the description with something suitable.
David
Agreed. So instead of:
keyctl add encrypted name "new master-key-name keylen" ring
the description would be prefixed with the key type like:
keyctl add encrypted name "new trusted|user master-key-name keylen" ring
thanks,
Mimi
David Howells
maintainerRe: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 7 lines · Mimi Zohar
Quoted 5 lines
I thought that might be the case. In which case, it might be better to allow someone to add a trusted key, supplying both encrypted and unencrypted versions of the data so that the TPM need not be consulted. You might want to mark such a key so that it can be seen when it is dumped.
At least to me, the name 'trusted' implies some form of HW.
In many ways, I think that the type and description describe the purpose of the key, not its source or derivation.
Quoted 8 lines
Quoted 2 lines
But if you're going to use a user-defined key, you really need to prefix the description with something suitable.
Agreed. So instead of:
keyctl add encrypted name "new master-key-name keylen" ringthe description would be prefixed with the key type like:
keyctl add encrypted name "new trusted|user master-key-name keylen" ring
I don't think you understood what I meant. If you look at the following function:
+static struct key *request_master_key(struct encrypted_key_payload *epayload,
+ void **master_key,
+ unsigned int *master_keylen)
+{
+ struct key *mkey;
+
+ mkey = request_trusted_key(epayload->master_desc,
+ master_key, master_keylen);
+ if (IS_ERR(mkey)) {
+ mkey = request_user_key(epayload->master_desc,
+ master_key, master_keylen);
+ if (IS_ERR(mkey)) {
+ pr_info("encrypted_key: trusted/user key %s not found",
+ epayload->master_desc);
+ return mkey;
+ }
+ }
+ dump_master_key(*master_key, *master_keylen);
+ return mkey;
+}
In the bit where you go for a user key (having failed to get a trusted key), you should prefix the description here (or in request_user_key()) with something like "trusted:". Then you don't need to change the user interface.
David
Re: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 35 lines · David Howells
Quoted 8 lines · Mimi Zohar
Quoted 2 lines
But if you're going to use a user-defined key, you really need to prefix the description with something suitable.
Agreed. So instead of:
keyctl add encrypted name "new master-key-name keylen" ringthe description would be prefixed with the key type like:
keyctl add encrypted name "new trusted|user master-key-name keylen" ringI don't think you understood what I meant. If you look at the following function:
security/keys/encrypted_defined.c new file +20
-0+static struct key *request_master_key (struct encrypted_key_payload *epayload, +void **master_key, +unsigned int *master_keylen) +{ +struct key *mkey; + + mkey =request_trusted_key (epayload->master_desc, + master_key, master_keylen); +if (IS_ERR (mkey)) { + mkey =request_user_key (epayload->master_desc, + master_key, master_keylen); +if (IS_ERR (mkey)) { +pr_info ("encrypted_key: trusted/user key %s not found" , + epayload->master_desc); +return mkey; + } + } +dump_master_key (*master_key, *master_keylen); +return mkey; +}
Quoted 5 lines
In the bit where you go for a user key (having failed to get a trusted key), you should prefix the description here (or in
request_user_key()) with something like "trusted:". Then you don't need to change the user interface.David
Am assuming you mean something like this:
keyctl add encrypted name "new trusted:master-key-name keylen" ring keyctl add encrypted name "new user:master-key-name keylen" ring
and, as you said, works without changing the API.
thanks,
Mimi
David Howells
maintainerRe: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 6 lines · Mimi Zohar
Am assuming you mean something like this:
keyctl add encrypted name "new trusted:master-key-name keylen" ring keyctl add encrypted name "new user:master-key-name keylen" ring
and, as you said, works without changing the API.
No, that's not what I mean. I maeant that when your internal functions look for the user key, they should preface the description with a prefix.
It should be handled in request_user_key() or request_master_key(). The description given to request_trusted_key() should have the prefix applied there. There's no need to mention it at all in the encrypted key add_key command line.
David
Re: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 12 lines · David Howells
Quoted 6 lines · Mimi Zohar
Am assuming you mean something like this:
keyctl add encrypted name "new trusted:master-key-name keylen" ring keyctl add encrypted name "new user:master-key-name keylen" ring
and, as you said, works without changing the API.
No, that's not what I mean. I maeant that when your internal functions look for the user key, they should preface the description with a prefix.
Quoted 6 lines
It should be handled in
request_user_key()orrequest_master_key(). The description given torequest_trusted_key()should have the prefix applied there. There's no need to mention it at all in the encrypted keyadd_keycommand line.David
I actually like keyctl requiring 'trusted:' or 'user:'. Forcing the user to indicate which type of key they want, is actually good - no misunderstandings. Another benefit, would be allowing 'keyctl update' to update the key description, not the key type.
Mimi
David Howells
maintainerRe: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 3 lines · Mimi Zohar
I actually like keyctl requiring 'trusted:' or 'user:'. Forcing the user to indicate which type of key they want, is actually good - no misunderstandings.
You still need to prefix the description of a user-defined key so that you don't collide with other people who're also using user-defined keys for random things.
Quoted 2 lines
Another benefit, would be allowing 'keyctl update' to update the key description, not the key type.
You mean you want to change the description on a key?
David
Re: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 8 lines · David Howells
Quoted 3 lines · Mimi Zohar
I actually like keyctl requiring 'trusted:' or 'user:'. Forcing the user to indicate which type of key they want, is actually good - no misunderstandings.
You still need to prefix the description of a user-defined key so that you don't collide with other people who're also using user-defined keys for random things.
ok
Quoted 6 lines
Quoted 2 lines
Another benefit, would be allowing 'keyctl update' to update the key description, not the key type.
You mean you want to change the description on a key?
David
No, this just updates the name of the key used to encrypt/decrypt the encrypted key. For example, the encrypted key evm-key is initially encrypted/decrypted using 'kmk-trusted'. After the update, it is encrypted/decrypted with 'kmk'. Both now are trusted keys.
$ keyctl show
Session Keyring
-3 --alswrv 500 500 keyring: _ses
117908125 --alswrv 500 -1 \_ keyring: _uid.500
501967942 --alswrv 500 500 \_ trusted: kmk-trusted
317523177 --alswrv 500 500 \_ encrypted: evm-key
666437381 --alswrv 500 500 \_ trusted: kmk
$ keyctl print 317523177
trusted:kmk-trusted 32 ca0ebb83594f14781460 ...$ keyctl update 317523177 "update trusted:kmk" $ keyctl print 317523177 trusted:kmk 32 ca0ebb83594f1478146 ....
Mimi
David Howells
maintainerRe: [PATCH v1.3 4/4] keys: add new key-type encrypted
Quoted 4 lines · Mimi Zohar
No, this just updates the name of the key used to encrypt/decrypt the encrypted key. For example, the encrypted key evm-key is initially encrypted/decrypted using 'kmk-trusted'. After the update, it is encrypted/decrypted with 'kmk'. Both now are trusted keys.
Gotcha. The problem is that there are two many different objects in the system called 'keys':-)
David
Re: [PATCH v1.3 4/4] keys: add new key-type encrypted
In reply to David HowellsQuoted 8 lines · David Howells
Quoted 3 lines · Mimi Zohar
I actually like keyctl requiring 'trusted:' or 'user:'. Forcing the user to indicate which type of key they want, is actually good - no misunderstandings.
You still need to prefix the description of a user-defined key so that you don't collide with other people who're also using user-defined keys for random things.
Although I previously agreed to this change, I'm really not convinced it is necessary. encrypted keys don't create new trusted or user keys, they only use existing keys to encrypt/decrypt encrypted keys(instantiate,read). Key names, user or otherwise, should be left up to the person creating them.
thanks,
Mimi