keys: trusted and encrypted keys
Subsystems (8)
cover keys: trusted and encrypted keys
Changes were made in this patchset based on David Howell's comments. The major technical change was converting from the crypto hash to shash API. The remaining changes are listed in the individual patch changelogs.
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)
keyctl print returns an ascii hex copy of the sealed key, which is in standard TPM_STORED_DATA format. 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 key-type:master-key-name keylen hex_blob" ring keyctl update keyid "update master-key-name"
where 'key-type' is either 'trusted' or 'user'.
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 (5):
lib: hex2bin converts ascii hexadecimal string to binary
tpm: add module_put wrapper
key: add tpm_send command
keys: add new trusted key-type
keys: add new key-type encrypted
Documentation/keys-trusted-encrypted.txt | 68 ++ drivers/char/tpm/tpm.c | 20 +- drivers/char/tpm/tpm.h | 5 + include/keys/encrypted-type.h | 29 + include/keys/trusted-type.h | 31 + include/linux/kernel.h | 1 + include/linux/tpm.h | 4 + lib/hexdump.c | 16 + security/Kconfig | 31 + security/keys/Makefile | 2 + security/keys/encrypted_defined.c | 876 +++++++++++++++++++++++ security/keys/encrypted_defined.h | 56 ++ security/keys/trusted_defined.c | 1136 ++++++++++++++++++++++++++++++ security/keys/trusted_defined.h | 147 ++++ 14 files changed, 2420 insertions(+), 2 deletions(-) create mode 100644 Documentation/keys-trusted-encrypted.txt 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/5 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.
Changelog:
- Replace parameter names with src/dst (based on David Howell's comment)
- Add 'const' where needed (based on David Howell's comment)
- Replace int with size_t (based on David Howell's comment)
--- 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 (u8 *dst,const char *src,size_t 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 +* @dst: binary result +* @src: ascii hexadecimal string +* @count: result length +*/ +void hex2bin (u8 *dst,const char *src,size_t count) +{ +while (count--) { + *dst =hex_to_bin (*src++) <<4 ; + *dst +=hex_to_bin (*src++); + dst++; + } +} +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
Re: [PATCH v1.4 1/5] lib: hex2bin converts ascii hexadecimal string to binary
Quoted 10 lines · Mimi Zohar
Similar to the
kgdb_hex2mem()code, hex2bin converts a string to binary using thehex_to_bin()library call.Changelog:
- Replace parameter names with src/dst (based on David Howell's comment)
- Add 'const' where needed (based on David Howell's comment)
- Replace int withsize_t(based on David Howell's comment)Signed-off-by Mimi Zohar <[email protected]>Acked-by Serge E. Hallyn <[email protected]>
PATCH 2/5 tpm: add module_put wrapper
In reply to Mimi ZoharFor readability, define a tpm_chip_put() wrapper to call module_put(). Replace existing module_put() calls with the wrapper.
(Change based on trusted/encrypted patchset review by David Howells.)
--- drivers/char/tpm/tpm.c | 4 ++-- drivers/char/tpm/tpm.h | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm.c +2 -2
@@ -736,7 +736,7 @@ int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)if (chip ==NULL )return -ENODEV ; rc =__tpm_pcr_read (chip, pcr_idx, res_buf); -module_put (chip->dev->driver->owner); +tpm_chip_put (chip);return rc; }EXPORT_SYMBOL_GPL (tpm_pcr_read);
@@ -775,7 +775,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) rc =transmit_cmd (chip, &cmd,EXTEND_PCR_RESULT_SIZE ,"attempting extend a PCR value" ); -module_put (chip->dev->driver->owner); +tpm_chip_put (chip);return rc; }EXPORT_SYMBOL_GPL (tpm_pcr_extend);
drivers/char/tpm/tpm.h +5 -0
@@ -113,6 +113,11 @@ struct tpm_chip {#define to_tpm_chip (n) container_of(n, struct tpm_chip, vendor) +static inline void tpm_chip_put (struct tpm_chip *chip) +{ +module_put (chip->dev->driver->owner); +} +static inline int tpm_read_index (int base,int index) {outb (index, base);
Signature · 1 line
1.7.2.2
Re: [PATCH v1.4 2/5] tpm: add module_put wrapper
Quoted 7 lines · Mimi Zohar
For readability, define a
tpm_chip_put()wrapper to callmodule_put(). Replace existingmodule_put()calls with the wrapper.(Change based on trusted/encrypted patchset review by David Howells.)
Signed-off-by Mimi Zohar <[email protected]>Signed-off-by David Safford <[email protected]>
PATCH 3/5 key: add tpm_send command
In reply to Mimi ZoharAdd internal kernel tpm_send() command used to seal/unseal keys.
Changelog:
- replaced module_put in tpm_send() with new tpm_chip_put() wrapper (suggested by David Howells)
- Make tpm_send() cmd argument a 'void *' (suggested by David Howells)
--- drivers/char/tpm/tpm.c | 16 ++++++++++++++++ include/linux/tpm.h | 4 ++++ 2 files changed, 20 insertions(+), 0 deletions(-)
drivers/char/tpm/tpm.c +16 -0
@@ -780,6 +780,22 @@ 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,void *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, cmd, buflen,"attempting tpm_cmd" ); + +tpm_chip_put (chip); +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 +4 -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,void *cmd,int buflen);#else static inline int tpm_pcr_read (u32 chip_num,int pcr_idx,u8 *res_buf) {return -ENODEV ;
@@ -38,5 +39,8 @@ 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,void *cmd,int buflen) { +return -ENODEV ; +}#endif #endif
Signature · 1 line
1.7.2.2
Re: [PATCH v1.4 3/5] key: add tpm_send command
Quoted 1 line · Mimi Zohar
+int
tpm_send(u32 chip_num, void *cmd, int buflen)
I'd recommend size_t for buflen; apart from that:
Re: [PATCH v1.4 3/5] key: add tpm_send command
Quoted 7 lines · David Howells
Quoted 1 line · Mimi Zohar
+int
tpm_send(u32 chip_num, void *cmd, int buflen)I'd recommend
size_tfor buflen; apart from that:Acked-by David Howells <[email protected]>
right - that would be better.
Currently transmit_command() in tpm.c expects an int, but it changes it to size_t for the actual tpm_transmit(). We will work with Rajiv to do a cleanup, as this touches a number of calls.
thanks!
dave
David Howells
maintainerRe: [PATCH v1.4 3/5] key: add tpm_send command
Quoted 4 lines · David Safford
Currently
transmit_command()intpm.cexpects an int, but it changes it tosize_tfor the actualtpm_transmit(). We will work with Rajiv to do a cleanup, as this touches a number of calls.
You can pass a size_t value to an int. The compiler will cast automatically.
David
PATCH 4/5 keys: add new trusted key-type
In reply to Mimi ZoharDefine 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:
- moved to late_initcall
- updated from hash to shash (suggestion by David Howells)
- reduced worst stack usage (tpm_seal) from 530 to 312 bytes
- moved documentation to Documentation directory (suggestion by David Howells)
- all the other code cleanups suggested by David Howells
- 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)
--- Documentation/keys-trusted-encrypted.txt | 68 ++ include/keys/trusted-type.h | 31 + security/Kconfig | 15 + security/keys/Makefile | 1 + security/keys/trusted_defined.c | 1136 ++++++++++++++++++++++++++++++ security/keys/trusted_defined.h | 147 ++++ 6 files changed, 1398 insertions(+), 0 deletions(-) create mode 100644 Documentation/keys-trusted-encrypted.txt 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/keys-trusted-encrypted.txt new file +68 -0
@@ -0,0 +1,68 @@ + Trusted and Encrypted Keys + +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...i + (40 ascii zeros) + blobauth= ascii hex auth for sealed data 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) + +keyctl print returns an ascii hex copy of the sealed key, which is in standard +TPM_STORED_DATA format. 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 key-type:master-key-name keylen" ring + keyctl add encrypted name "load key-type:master-key-name keylen hex_blob" ring + keyctl update keyid "update key-type:master-key-name" + +where 'key-type' is either 'trusted' or 'user'. + +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.
include/keys/trusted-type.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* 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; +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 will 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 +1136 -0
1,136 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.4 4/5] keys: add new trusted key-type
Quoted 1 line · Mimi Zohar
+keyctl print returns an ascii hex copy of the sealed key, which is in standard
I'd quote 'keyctl print' just so it's obvious where the command ends and the descriptive text starts.
Documentation/keys-trusted-encrypted.txt line 61
Quoted 6 lines
Documentation/keys-trusted-encrypted.txt new file +6
-0+Usage: + keyctl add encrypted name "new key-type:master-key-name keylen" ring + keyctl add encrypted name "load key-type:master-key-name keylen hex_blob" ring + keyctl update keyid "update key-type:master-key-name" + +where 'key-type' is either 'trusted' or 'user'.
I recommend adding some example commands with all the arguments substituted. Nothing helps get to grip with an API like knowing what a command is supposed to look like when it's actually used.
Quoted 1 line
+static int
trusted_tpm_send(u32 chip_num, unsigned char *cmd, int buflen)
There are still a lot of places in here where you should probably be using const and size_t.
security/keys/trusted_defined.c line 371
Quoted 9 lines
security/keys/trusted_defined.c new file +9
-0+static int my_get_random (unsigned char *buf,int len) +{ +struct tpm_buf *tb; +int ret; + + tb =kzalloc (sizeof *tb,GFP_KERNEL ); +if (!tb) +return -ENOMEM ; + ret =tpm_get_random (tb, buf, len);
Using kzalloc() rather than kmalloc() is a waste of time, I'd've thought. It's a temporary buffer. Does it really need to be precleared?
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash); + return ret;
Merge.
security/keys/trusted_defined.c line 992
Quoted 4 lines
security/keys/trusted_defined.c new file +3
-0+static int trusted_update (struct key *key,const void *data,size_t datalen) +{ + *(datablob + datalen) ='\0' ;
That's what [] is for.
security/keys/trusted_defined.c line 1018
Quoted 2 lines
security/keys/trusted_defined.c new file +2
-0+if (new_o) +kfree (new_o);
kfree() can handle a NULL pointer.
security/keys/trusted_defined.c line 1014
Quoted 4 lines
security/keys/trusted_defined.c new file +4
-0+if (new_o->pcrlock) + ret =pcrlock (new_o->pcrlock); +rcu_assign_pointer (key->payload.data, new_p); +call_rcu (&p->rcu, trusted_rcu_free);
Should there be a check for pcrlock() failure?
security/keys/trusted_defined.h line 6
Quoted 4 lines
security/keys/trusted_defined.h new file +3
-0+/* not already defined in tpm.h - specific to this use */ +#define TPM_TAG_RQU_COMMAND 193 +#define TPM_TAG_RQU_AUTH1_COMMAND 194
Values defined for TPM hardware access really ought to be in a separate file in include/linux/. They aren't strictly specific to the trusted key implementation here; that may be the only user currently in the kernel, but that doesn't mean there can't be another user.
David
Re: [PATCH v1.4 4/5] keys: add new trusted key-type
Quoted 1 line · David Howells
Mimi Zohar <[email protected]> wrote:
thanks for the review! - getting closer...
Quoted 4 lines
Quoted 1 line
+keyctl print returns an ascii hex copy of the sealed key, which is in standard
I'd quote 'keyctl print' just so it's obvious where the command ends and the descriptive text starts.
ok
Quoted 10 lines
Documentation/keys-trusted-encrypted.txt line 61
Quoted 6 lines
Documentation/keys-trusted-encrypted.txt new file +6
-0+Usage: + keyctl add encrypted name "new key-type:master-key-name keylen" ring + keyctl add encrypted name "load key-type:master-key-name keylen hex_blob" ring + keyctl update keyid "update key-type:master-key-name" + +where 'key-type' is either 'trusted' or 'user'.I recommend adding some example commands with all the arguments substituted. Nothing helps get to grip with an API like knowing what a command is supposed to look like when it's actually used.
now that this is moved to documentation, you are right, more examples would be nice... willdo.
Quoted 4 lines
Quoted 1 line
+static int
trusted_tpm_send(u32 chip_num, unsigned char *cmd, int buflen)There are still a lot of places in here where you should probably be using const and
size_t.
will clean up.
Quoted 4 lines
Quoted 4 lines
security/keys/trusted_defined.c new file +4
-0+static int my_get_random (unsigned char *buf,int len) +{ +struct tpm_buf *tb; +int ret;
Quoted 8 lines
security/keys/trusted_defined.c line 371
Quoted 5 lines
security/keys/trusted_defined.c new file +5
-0+ + tb =kzalloc (sizeof *tb,GFP_KERNEL ); +if (!tb) +return -ENOMEM ; + ret =tpm_get_random (tb, buf, len);Using
kzalloc()rather thankmalloc()is a waste of time, I'd've thought. It's a temporary buffer. Does it really need to be precleared?
In this case, none need to be precleared. Will fix.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash); + return ret;Merge.
agreed
Quoted 6 lines
security/keys/trusted_defined.c line 992
Quoted 4 lines
security/keys/trusted_defined.c new file +3
-0+static int trusted_update (struct key *key,const void *data,size_t datalen) +{ + *(datablob + datalen) ='\0' ;That's what [] is for.
agreed.
Quoted 4 lines
security/keys/trusted_defined.c line 1018
Quoted 2 lines
security/keys/trusted_defined.c new file +2
-0+if (new_o) +kfree (new_o);
kfree()can handle a NULL pointer.
agreed.
Quoted 6 lines
security/keys/trusted_defined.c line 1014
Quoted 4 lines
security/keys/trusted_defined.c new file +4
-0+if (new_o->pcrlock) + ret =pcrlock (new_o->pcrlock); +rcu_assign_pointer (key->payload.data, new_p); +call_rcu (&p->rcu, trusted_rcu_free);Should there be a check for
pcrlock()failure?
yes, because if the pcrlock failed, even though the key was successfully created, it is not safe to use. Will correct.
Quoted 9 lines
security/keys/trusted_defined.h line 6
Quoted 4 lines
security/keys/trusted_defined.h new file +3
-0+/* not already defined in tpm.h - specific to this use */ +#define TPM_TAG_RQU_COMMAND 193 +#define TPM_TAG_RQU_AUTH1_COMMAND 194Values defined for TPM hardware access really ought to be in a separate file in
include/linux/. They aren't strictly specific to the trusted key implementation here; that may be the only user currently in the kernel, but that doesn't mean there can't be another user.
Currently tpm request packet defines are private to the tpm driver. With the opening of a tpm_send() interface, perhaps it does make sense for an include/linux/tpm_packet.h for everyone (including tpm.c) to use, even though there is little overlap currently.
We could create that with these current defines to start, and then do a cleanup patch for tpm.c to take advantage.
dave
PATCH 5/5 keys: add new key-type encrypted
In reply to Mimi ZoharDefine 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:
- Moved documentation to Documentation/keys-trusted-encrypted.txt
- Replace hash with shash (based on comments by David Howells)
- Make lengths/counts size_t where possible (based on comments by David Howells) Could not convert most lengths, as crypto expects 'unsigned int' (size_t: on 32 bit is defined as unsigned int, but on 64 bit is unsigned long)
- Add 'const' where possible (based on comments by David Howells)
- 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 | 29 ++ security/Kconfig | 16 + security/keys/Makefile | 1 + security/keys/encrypted_defined.c | 876 +++++++++++++++++++++++++++++++++++++ security/keys/encrypted_defined.h | 56 +++ 5 files changed, 978 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 +29 -0
@@ -0,0 +1,29 @@ +/* +* 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; +char *master_desc;/* datablob: master key name */ +char *datalen;/* datablob: decrypted key length */ +u8 *iv;/* datablob: iv */ +u8 *encrypted_data;/* datablob: encrypted data */ +unsigned short datablob_len;/* length of datablob */ +unsigned short decrypted_datalen;/* decrypted data length */ +u8 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 +876 -0
876 lines not on this page · raw message
security/keys/encrypted_defined.h new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
1.7.2.2
David Howells
maintainerRe: [PATCH v1.4 5/5] keys: add new key-type encrypted
security/keys/encrypted_defined.c line 154
Quoted 3 lines · Mimi Zohar
security/keys/encrypted_defined.c new file +3
-0+static int datablob_format (char __user *buffer, +struct encrypted_key_payload *epayload, +int asciiblob_len)
size_t? There are other instances where you should be using size_t too.
security/keys/encrypted_defined.c line 346
Quoted 3 lines
security/keys/encrypted_defined.c new file +3
-0+ index =strcspn (epayload->master_desc,":" ); +if (index ==strlen (epayload->master_desc)) + goto out;
For single chars, use strchr() rather than strcspn(), and if you happen to know the length too, use memchr(). This also means you don't need to do the strlen() again to check the result.
Review
Quoted 2 lines
(quoted diff) +2
-0+ memset(derived_key, 0, sizeof derived_key); + ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen)
Is the memset() actually necessary?
security/keys/encrypted_defined.c line 508
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+ decrypted_datalen = (unsigned short )dlen;
Unnecessary cast.
security/keys/encrypted_defined.c line 694
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+ *(datablob + datalen) =0 ; + *(buf + datalen) =0 ;
Use [].
security/keys/encrypted_defined.c line 702
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+if (index ==strlen (new_master_desc) + || (strncmp (new_master_desc, epayload->master_desc, index) !=0 )) {
Superfluous brackets.
Quoted 12 lines · Mimi Zohar
security/keys/encrypted_defined.c new file +12
-0+ mkey =request_master_key (epayload, &master_key, &master_keylen); +if (IS_ERR (mkey)) +return PTR_ERR (mkey); + +memset (derived_key,0 ,sizeof derived_key); + ret =get_derived_key (derived_key,ENC_KEY , master_key, master_keylen); +if (ret <0 ) + goto out; + + ret =derived_key_encrypt (epayload, derived_key,sizeof derived_key); +if (ret <0 ) + goto out;
security/keys/encrypted_defined.c line 778
Quoted 10 lines
security/keys/encrypted_defined.c new file +10
-0+ + ret =datablob_hmac_append (epayload, master_key, master_keylen); +if (ret <0 ) + goto out; + + ret =datablob_format (buffer, epayload, asciiblob_len); +if (ret <0 ) + goto out; + +rcu_read_unlock ();
NAK! This is not safe. You may not sleep whilst you hold the RCU read lock on the master key. At the very least, datablob_format() may sleep as it writes to userspace.
I think what you should do is ignore RCU entirely and read lock the master key's semaphore.
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = aes_get_sizes(); + return ret;
Merge.
David
Re: [PATCH v1.4 5/5] keys: add new key-type encrypted
Quoted 7 lines · David Howells
security/keys/encrypted_defined.c line 154
Quoted 3 lines · Mimi Zohar
security/keys/encrypted_defined.c new file +3
-0+static int datablob_format (char __user *buffer, +struct encrypted_key_payload *epayload, +int asciiblob_len)
size_t? There are other instances where you should be usingsize_ttoo.
Most of the crypto library calls use "unsigned int". Obviously didn't quite get everything else. Will look again. thanks.
Quoted 7 lines
security/keys/encrypted_defined.c line 346
Quoted 3 lines
security/keys/encrypted_defined.c new file +3
-0+ index =strcspn (epayload->master_desc,":" ); +if (index ==strlen (epayload->master_desc)) + goto out;For single chars, use
strchr()rather thanstrcspn(), and if you happen to know the length too, usememchr(). This also means you don't need to do thestrlen()again to check the result.
Don't have the length, but getting it once and then using strchr/memchr, should work. thanks.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ memset(derived_key, 0, sizeof derived_key); + ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen)Is the
memset()actually necessary?
not sure, will check.
Quoted 3 lines
security/keys/encrypted_defined.c line 508
Quoted 1 line
security/keys/encrypted_defined.c new file +1
-0+ decrypted_datalen = (unsigned short )dlen;Unnecessary cast.
ok
Quoted 30 lines
security/keys/encrypted_defined.c line 694
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+ *(datablob + datalen) =0 ; + *(buf + datalen) =0 ;Use [].
security/keys/encrypted_defined.c line 702
Quoted 2 lines
security/keys/encrypted_defined.c new file +2
-0+if (index ==strlen (new_master_desc) + || (strncmp (new_master_desc, epayload->master_desc, index) !=0 )) {Superfluous brackets.
Quoted 18 lines · Mimi Zohar
security/keys/encrypted_defined.c new file +18
-0+ mkey =request_master_key (epayload, &master_key, &master_keylen); +if (IS_ERR (mkey)) +return PTR_ERR (mkey); + +memset (derived_key,0 ,sizeof derived_key); + ret =get_derived_key (derived_key,ENC_KEY , master_key, master_keylen); +if (ret <0 ) + goto out; + + ret =derived_key_encrypt (epayload, derived_key,sizeof derived_key); +if (ret <0 ) + goto out; + + ret =datablob_hmac_append (epayload, master_key, master_keylen); +if (ret <0 ) + goto out; + + ret =datablob_format (buffer, epayload, asciiblob_len);
Quoted 8 lines
security/keys/encrypted_defined.c line 567
Quoted 4 lines
security/keys/encrypted_defined.c new file +4
-0+if (ret <0 ) + goto out; + +rcu_read_unlock ();NAK! This is not safe. You may not sleep whilst you hold the RCU read lock on the master key. At the very least,
datablob_format()may sleep as it writes to userspace.
datablob_format() could be split up into formatting the string returned to userspace and copying it userspace, releasing the lock before copying it, but that wouldn't resolve the other places it may sleep, like alloc.
Quoted 2 lines
I think what you should do is ignore RCU entirely and read lock the master key's semaphore.
thanks, will do.
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = aes_get_sizes(); + return ret;Merge.
David
ok
thanks,
Mimi