RE: [PATCH v7 21/21] tpm: use u32 instead of int for PCR index

2018-10-23 Thread Winkler, Tomas
> 
> On Fri, 19 Oct 2018, Tomas Winkler wrote:
> > The TPM specs defines PCR index as a positive number, and there is no
> > reason to use a signed number. It is also a possible security issue as
> > currently no functions check for a negative index, which may become a
> > large number when converted to u32.
> >
> > Adjust the API to use u32 instead of int in all PCR related functions.
> >
> > Signed-off-by: Tomas Winkler 
> > Reviewed-by: Jarkko Sakkinen 
> > Tested-by: Jarkko Sakkinen 
> > ---
> > V3: New in the series.
> > V4: Separate unrelated change to another patches.
> > V5: Fix the commit message.
> > V6: Rebased.
> > V7: Resend.
> >
> > drivers/char/tpm/tpm-interface.c|  6 +++---
> > drivers/char/tpm/tpm-sysfs.c|  2 +-
> > drivers/char/tpm/tpm.h  | 10 +-
> > drivers/char/tpm/tpm1-cmd.c |  6 +++---
> > drivers/char/tpm/tpm2-cmd.c |  5 ++---
> > include/linux/tpm.h | 11 +++
> > security/integrity/ima/ima_crypto.c |  5 +++--
> > 7 files changed, 24 insertions(+), 21 deletions(-)
> >
> > diff --git a/drivers/char/tpm/tpm-interface.c
> > b/drivers/char/tpm/tpm-interface.c
> > index acd647476ae1..d9439f9abe78 100644
> > --- a/drivers/char/tpm/tpm-interface.c
> > +++ b/drivers/char/tpm/tpm-interface.c
> > @@ -38,7 +38,7 @@
> >  * recently changed pcr on suspend, so force the flush
> >  * with an extend to the selected _unused_ non-volatile pcr.
> >  */
> > -static int tpm_suspend_pcr;
> > +static u32 tpm_suspend_pcr;
> > module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
> > MODULE_PARM_DESC(suspend_pcr,
> >  "PCR to use for dummy writes to facilitate flush on
> suspend."); @@
> > -455,7 +455,7 @@ EXPORT_SYMBOL_GPL(tpm_is_tpm2);
> >  *
> >  * Return: same as with tpm_transmit_cmd()  */ -int
> > tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
> > +int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
> > {
> > int rc;
> >
> > @@ -485,7 +485,7 @@ EXPORT_SYMBOL_GPL(tpm_pcr_read);
> >  *
> >  * Return: same as with tpm_transmit_cmd()  */ -int
> > tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
> > +int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8
> > +*hash)
> > {
> > int rc;
> > struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
> > diff --git a/drivers/char/tpm/tpm-sysfs.c
> > b/drivers/char/tpm/tpm-sysfs.c index 96fc7433c57d..b88e08ec2c59 100644
> > --- a/drivers/char/tpm/tpm-sysfs.c
> > +++ b/drivers/char/tpm/tpm-sysfs.c
> > @@ -102,7 +102,7 @@ static ssize_t pcrs_show(struct device *dev, struct
> device_attribute *attr,
> > cap_t cap;
> > u8 digest[TPM_DIGEST_SIZE];
> > ssize_t rc;
> > -   int i, j, num_pcrs;
> > +   u32 i, j, num_pcrs;
> > char *str = buf;
> > struct tpm_chip *chip = to_tpm_chip(dev);
> >
> > diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index
> > e0778d19da98..f27d1f38a93d 100644
> > --- a/drivers/char/tpm/tpm.h
> > +++ b/drivers/char/tpm/tpm.h
> > @@ -509,14 +509,14 @@ ssize_t tpm_transmit_cmd(struct tpm_chip
> *chip,
> > struct tpm_space *space, int tpm_get_timeouts(struct tpm_chip *); int
> > tpm_auto_startup(struct tpm_chip *chip);
> >
> > -int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr);
> > +int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
> > int tpm1_auto_startup(struct tpm_chip *chip); int
> > tpm1_do_selftest(struct tpm_chip *chip); int tpm1_get_timeouts(struct
> > tpm_chip *chip); unsigned long tpm1_calc_ordinal_duration(struct
> > tpm_chip *chip, u32 ordinal); -int tpm1_pcr_extend(struct tpm_chip
> > *chip, int pcr_idx, const u8 *hash,
> > +int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8
> > +*hash,
> > const char *log_msg);
> > -int tpm1_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
> > +int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
> > ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
> > const char *desc, size_t min_cap_length); int
> > tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max); @@ -558,8
> > +558,8 @@ static inline u32 tpm2_rc_value(u32 rc) }
> >
> > int tpm2_get_timeouts(struct tpm_chip *chip); -int
> > tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf); -int
> > tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
> > +int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
> > +int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
> > struct tpm2_digest *digests);
> > int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max); void
> > tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle, diff --git
> > a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c index
> > 6b04648f8184..6f306338953b 100644
> > --- a/drivers/char/tpm/tpm1-cmd.c
> > +++ b/drivers/char/tpm/tpm1-cmd.c
> > @@ -449,7 +449,7 @@ int tpm1_get_timeouts(struct

Re: [PATCH v7 21/21] tpm: use u32 instead of int for PCR index

2018-10-23 Thread Jarkko Sakkinen

On Fri, 19 Oct 2018, Tomas Winkler wrote:

The TPM specs defines PCR index as a positive number, and there is
no reason to use a signed number. It is also a possible security
issue as currently no functions check for a negative index,
which may become a large number when converted to u32.

Adjust the API to use u32 instead of int in all PCR related
functions.

Signed-off-by: Tomas Winkler 
Reviewed-by: Jarkko Sakkinen 
Tested-by: Jarkko Sakkinen 
---
V3: New in the series.
V4: Separate unrelated change to another patches.
V5: Fix the commit message.
V6: Rebased.
V7: Resend.

drivers/char/tpm/tpm-interface.c|  6 +++---
drivers/char/tpm/tpm-sysfs.c|  2 +-
drivers/char/tpm/tpm.h  | 10 +-
drivers/char/tpm/tpm1-cmd.c |  6 +++---
drivers/char/tpm/tpm2-cmd.c |  5 ++---
include/linux/tpm.h | 11 +++
security/integrity/ima/ima_crypto.c |  5 +++--
7 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index acd647476ae1..d9439f9abe78 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -38,7 +38,7 @@
 * recently changed pcr on suspend, so force the flush
 * with an extend to the selected _unused_ non-volatile pcr.
 */
-static int tpm_suspend_pcr;
+static u32 tpm_suspend_pcr;
module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
MODULE_PARM_DESC(suspend_pcr,
 "PCR to use for dummy writes to facilitate flush on suspend.");
@@ -455,7 +455,7 @@ EXPORT_SYMBOL_GPL(tpm_is_tpm2);
 *
 * Return: same as with tpm_transmit_cmd()
 */
-int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
+int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
{
int rc;

@@ -485,7 +485,7 @@ EXPORT_SYMBOL_GPL(tpm_pcr_read);
 *
 * Return: same as with tpm_transmit_cmd()
 */
-int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
+int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
{
int rc;
struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
index 96fc7433c57d..b88e08ec2c59 100644
--- a/drivers/char/tpm/tpm-sysfs.c
+++ b/drivers/char/tpm/tpm-sysfs.c
@@ -102,7 +102,7 @@ static ssize_t pcrs_show(struct device *dev, struct 
device_attribute *attr,
cap_t cap;
u8 digest[TPM_DIGEST_SIZE];
ssize_t rc;
-   int i, j, num_pcrs;
+   u32 i, j, num_pcrs;
char *str = buf;
struct tpm_chip *chip = to_tpm_chip(dev);

diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index e0778d19da98..f27d1f38a93d 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -509,14 +509,14 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct 
tpm_space *space,
int tpm_get_timeouts(struct tpm_chip *);
int tpm_auto_startup(struct tpm_chip *chip);

-int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr);
+int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
int tpm1_auto_startup(struct tpm_chip *chip);
int tpm1_do_selftest(struct tpm_chip *chip);
int tpm1_get_timeouts(struct tpm_chip *chip);
unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
-int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
+int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
const char *log_msg);
-int tpm1_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
+int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
const char *desc, size_t min_cap_length);
int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
@@ -558,8 +558,8 @@ static inline u32 tpm2_rc_value(u32 rc)
}

int tpm2_get_timeouts(struct tpm_chip *chip);
-int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
-int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
+int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
+int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
struct tpm2_digest *digests);
int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index 6b04648f8184..6f306338953b 100644
--- a/drivers/char/tpm/tpm1-cmd.c
+++ b/drivers/char/tpm/tpm1-cmd.c
@@ -449,7 +449,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip)
}

#define TPM_ORD_PCR_EXTEND 20
-int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
+int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
const char *log_msg)
{
struct tpm_buf buf;
@@ -572,7 +572,7 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t 
max)
}

#define TPM_ORD_PCRRE

Re: [PATCH v7 21/21] tpm: use u32 instead of int for PCR index

2018-10-23 Thread Jarkko Sakkinen

On Fri, 19 Oct 2018, Tomas Winkler wrote:

The TPM specs defines PCR index as a positive number, and there is
no reason to use a signed number. It is also a possible security
issue as currently no functions check for a negative index,
which may become a large number when converted to u32.

Adjust the API to use u32 instead of int in all PCR related
functions.

Signed-off-by: Tomas Winkler 
Reviewed-by: Jarkko Sakkinen 
---
V3: New in the series.
V4: Separate unrelated change to another patches.
V5: Fix the commit message.
V6: Rebased.
V7: Resend.

drivers/char/tpm/tpm-interface.c|  6 +++---
drivers/char/tpm/tpm-sysfs.c|  2 +-
drivers/char/tpm/tpm.h  | 10 +-
drivers/char/tpm/tpm1-cmd.c |  6 +++---
drivers/char/tpm/tpm2-cmd.c |  5 ++---
include/linux/tpm.h | 11 +++
security/integrity/ima/ima_crypto.c |  5 +++--
7 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index acd647476ae1..d9439f9abe78 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -38,7 +38,7 @@
 * recently changed pcr on suspend, so force the flush
 * with an extend to the selected _unused_ non-volatile pcr.
 */
-static int tpm_suspend_pcr;
+static u32 tpm_suspend_pcr;
module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
MODULE_PARM_DESC(suspend_pcr,
 "PCR to use for dummy writes to facilitate flush on suspend.");
@@ -455,7 +455,7 @@ EXPORT_SYMBOL_GPL(tpm_is_tpm2);
 *
 * Return: same as with tpm_transmit_cmd()
 */
-int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
+int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
{
int rc;

@@ -485,7 +485,7 @@ EXPORT_SYMBOL_GPL(tpm_pcr_read);
 *
 * Return: same as with tpm_transmit_cmd()
 */
-int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
+int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
{
int rc;
struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
index 96fc7433c57d..b88e08ec2c59 100644
--- a/drivers/char/tpm/tpm-sysfs.c
+++ b/drivers/char/tpm/tpm-sysfs.c
@@ -102,7 +102,7 @@ static ssize_t pcrs_show(struct device *dev, struct 
device_attribute *attr,
cap_t cap;
u8 digest[TPM_DIGEST_SIZE];
ssize_t rc;
-   int i, j, num_pcrs;
+   u32 i, j, num_pcrs;
char *str = buf;
struct tpm_chip *chip = to_tpm_chip(dev);

diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index e0778d19da98..f27d1f38a93d 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -509,14 +509,14 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct 
tpm_space *space,
int tpm_get_timeouts(struct tpm_chip *);
int tpm_auto_startup(struct tpm_chip *chip);

-int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr);
+int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
int tpm1_auto_startup(struct tpm_chip *chip);
int tpm1_do_selftest(struct tpm_chip *chip);
int tpm1_get_timeouts(struct tpm_chip *chip);
unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
-int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
+int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
const char *log_msg);
-int tpm1_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
+int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
const char *desc, size_t min_cap_length);
int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
@@ -558,8 +558,8 @@ static inline u32 tpm2_rc_value(u32 rc)
}

int tpm2_get_timeouts(struct tpm_chip *chip);
-int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
-int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
+int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
+int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
struct tpm2_digest *digests);
int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index 6b04648f8184..6f306338953b 100644
--- a/drivers/char/tpm/tpm1-cmd.c
+++ b/drivers/char/tpm/tpm1-cmd.c
@@ -449,7 +449,7 @@ int tpm1_get_timeouts(struct tpm_chip *chip)
}

#define TPM_ORD_PCR_EXTEND 20
-int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
+int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
const char *log_msg)
{
struct tpm_buf buf;
@@ -572,7 +572,7 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t 
max)
}

#define TPM_ORD_PCRREAD 21
-int tpm1_pcr_read(str