Skip to content

Commit 2677ca9

Browse files
author
Jarkko Sakkinen
committed
tpm: use tpm_try_get_ops() in tpm-sysfs.c.
Use tpm_try_get_ops() in tpm-sysfs.c so that we can consider moving other decorations (locking, localities, power management for example) inside it. This direction can be of course taken only after other call sites for tpm_transmit() have been treated in the same way. Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com> Tested-by: Stefan Berger <stefanb@linux.ibm.com> Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com> Reviewed-by: James Bottomley <James.Bottomley@HansenPartnership.com> Tested-by: Alexander Steffen <Alexander.Steffen@infineon.com>
1 parent 5faafba commit 2677ca9

File tree

1 file changed

+78
-45
lines changed

1 file changed

+78
-45
lines changed

drivers/char/tpm/tpm-sysfs.c

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,24 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
3939
{
4040
struct tpm_buf tpm_buf;
4141
struct tpm_readpubek_out *out;
42-
ssize_t rc;
4342
int i;
4443
char *str = buf;
4544
struct tpm_chip *chip = to_tpm_chip(dev);
4645
char anti_replay[20];
4746

4847
memset(&anti_replay, 0, sizeof(anti_replay));
4948

50-
if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK))
49+
if (tpm_try_get_ops(chip))
5150
return 0;
5251

52+
if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK))
53+
goto out_ops;
54+
5355
tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
5456

55-
rc = tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
56-
0, "attempting to read the PUBEK");
57-
if (rc) {
58-
tpm_buf_destroy(&tpm_buf);
59-
return 0;
60-
}
57+
if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
58+
0, "attempting to read the PUBEK"))
59+
goto out_buf;
6160

6261
out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
6362
str +=
@@ -88,9 +87,11 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
8887
str += sprintf(str, "\n");
8988
}
9089

91-
rc = str - buf;
90+
out_buf:
9291
tpm_buf_destroy(&tpm_buf);
93-
return rc;
92+
out_ops:
93+
tpm_put_ops(chip);
94+
return str - buf;
9495
}
9596
static DEVICE_ATTR_RO(pubek);
9697

@@ -103,10 +104,15 @@ static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
103104
char *str = buf;
104105
struct tpm_chip *chip = to_tpm_chip(dev);
105106

107+
if (tpm_try_get_ops(chip))
108+
return 0;
109+
106110
if (tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap,
107111
"attempting to determine the number of PCRS",
108-
sizeof(cap.num_pcrs)))
112+
sizeof(cap.num_pcrs))) {
113+
tpm_put_ops(chip);
109114
return 0;
115+
}
110116

111117
num_pcrs = be32_to_cpu(cap.num_pcrs);
112118
for (i = 0; i < num_pcrs; i++) {
@@ -119,74 +125,95 @@ static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
119125
str += sprintf(str, "%02X ", digest[j]);
120126
str += sprintf(str, "\n");
121127
}
128+
tpm_put_ops(chip);
122129
return str - buf;
123130
}
124131
static DEVICE_ATTR_RO(pcrs);
125132

126133
static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
127134
char *buf)
128135
{
136+
struct tpm_chip *chip = to_tpm_chip(dev);
137+
ssize_t rc = 0;
129138
cap_t cap;
130-
ssize_t rc;
131139

132-
rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
133-
"attempting to determine the permanent enabled state",
134-
sizeof(cap.perm_flags));
135-
if (rc)
140+
if (tpm_try_get_ops(chip))
136141
return 0;
137142

143+
if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
144+
"attempting to determine the permanent enabled state",
145+
sizeof(cap.perm_flags)))
146+
goto out_ops;
147+
138148
rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
149+
out_ops:
150+
tpm_put_ops(chip);
139151
return rc;
140152
}
141153
static DEVICE_ATTR_RO(enabled);
142154

143155
static ssize_t active_show(struct device *dev, struct device_attribute *attr,
144156
char *buf)
145157
{
158+
struct tpm_chip *chip = to_tpm_chip(dev);
159+
ssize_t rc = 0;
146160
cap_t cap;
147-
ssize_t rc;
148161

149-
rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
150-
"attempting to determine the permanent active state",
151-
sizeof(cap.perm_flags));
152-
if (rc)
162+
if (tpm_try_get_ops(chip))
153163
return 0;
154164

165+
if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
166+
"attempting to determine the permanent active state",
167+
sizeof(cap.perm_flags)))
168+
goto out_ops;
169+
155170
rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
171+
out_ops:
172+
tpm_put_ops(chip);
156173
return rc;
157174
}
158175
static DEVICE_ATTR_RO(active);
159176

160177
static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
161178
char *buf)
162179
{
180+
struct tpm_chip *chip = to_tpm_chip(dev);
181+
ssize_t rc = 0;
163182
cap_t cap;
164-
ssize_t rc;
165183

166-
rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
167-
"attempting to determine the owner state",
168-
sizeof(cap.owned));
169-
if (rc)
184+
if (tpm_try_get_ops(chip))
170185
return 0;
171186

187+
if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
188+
"attempting to determine the owner state",
189+
sizeof(cap.owned)))
190+
goto out_ops;
191+
172192
rc = sprintf(buf, "%d\n", cap.owned);
193+
out_ops:
194+
tpm_put_ops(chip);
173195
return rc;
174196
}
175197
static DEVICE_ATTR_RO(owned);
176198

177199
static ssize_t temp_deactivated_show(struct device *dev,
178200
struct device_attribute *attr, char *buf)
179201
{
202+
struct tpm_chip *chip = to_tpm_chip(dev);
203+
ssize_t rc = 0;
180204
cap_t cap;
181-
ssize_t rc;
182205

183-
rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
184-
"attempting to determine the temporary state",
185-
sizeof(cap.stclear_flags));
186-
if (rc)
206+
if (tpm_try_get_ops(chip))
187207
return 0;
188208

209+
if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
210+
"attempting to determine the temporary state",
211+
sizeof(cap.stclear_flags)))
212+
goto out_ops;
213+
189214
rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
215+
out_ops:
216+
tpm_put_ops(chip);
190217
return rc;
191218
}
192219
static DEVICE_ATTR_RO(temp_deactivated);
@@ -195,15 +222,18 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
195222
char *buf)
196223
{
197224
struct tpm_chip *chip = to_tpm_chip(dev);
198-
cap_t cap;
199-
ssize_t rc;
225+
ssize_t rc = 0;
200226
char *str = buf;
227+
cap_t cap;
201228

202-
rc = tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
203-
"attempting to determine the manufacturer",
204-
sizeof(cap.manufacturer_id));
205-
if (rc)
229+
if (tpm_try_get_ops(chip))
206230
return 0;
231+
232+
if (tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
233+
"attempting to determine the manufacturer",
234+
sizeof(cap.manufacturer_id)))
235+
goto out_ops;
236+
207237
str += sprintf(str, "Manufacturer: 0x%x\n",
208238
be32_to_cpu(cap.manufacturer_id));
209239

@@ -220,31 +250,34 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
220250
cap.tpm_version_1_2.revMinor);
221251
} else {
222252
/* Otherwise just use TPM_STRUCT_VER */
223-
rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
224-
"attempting to determine the 1.1 version",
225-
sizeof(cap.tpm_version));
226-
if (rc)
227-
return 0;
253+
if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
254+
"attempting to determine the 1.1 version",
255+
sizeof(cap.tpm_version)))
256+
goto out_ops;
228257
str += sprintf(str,
229258
"TCG version: %d.%d\nFirmware version: %d.%d\n",
230259
cap.tpm_version.Major,
231260
cap.tpm_version.Minor,
232261
cap.tpm_version.revMajor,
233262
cap.tpm_version.revMinor);
234263
}
235-
236-
return str - buf;
264+
rc = str - buf;
265+
out_ops:
266+
tpm_put_ops(chip);
267+
return rc;
237268
}
238269
static DEVICE_ATTR_RO(caps);
239270

240271
static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
241272
const char *buf, size_t count)
242273
{
243274
struct tpm_chip *chip = to_tpm_chip(dev);
244-
if (chip == NULL)
275+
276+
if (tpm_try_get_ops(chip))
245277
return 0;
246278

247279
chip->ops->cancel(chip);
280+
tpm_put_ops(chip);
248281
return count;
249282
}
250283
static DEVICE_ATTR_WO(cancel);

0 commit comments

Comments
 (0)