Skip to content

Commit 4e26195

Browse files
jgunthorpeJarkko Sakkinen
authored andcommitted
tpm: Provide strong locking for device removal
Add a read/write semaphore around the ops function pointers so ops can be set to null when the driver un-registers. Previously the tpm core expected module locking to be enough to ensure that tpm_unregister could not be called during certain times, however that hasn't been sufficient for a long time. Introduce a read/write semaphore around 'ops' so the core can set it to null when unregistering. This provides a strong fence around the driver callbacks, guaranteeing to the driver that no callbacks are running or will run again. For now the ops_lock is placed very high in the call stack, it could be pushed down and made more granular in future if necessary. Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com> Reviewed-by: Stefan Berger <stefanb@linux.vnet.ibm.com> Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
1 parent 3635e2e commit 4e26195

5 files changed

Lines changed: 100 additions & 21 deletions

File tree

‎drivers/char/tpm/tpm-chip.c‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,60 @@ static DEFINE_SPINLOCK(driver_lock);
3636
struct class *tpm_class;
3737
dev_t tpm_devt;
3838

39-
/*
40-
* tpm_chip_find_get - return tpm_chip for a given chip number
41-
* @chip_num the device number for the chip
39+
/**
40+
* tpm_try_get_ops() - Get a ref to the tpm_chip
41+
* @chip: Chip to ref
42+
*
43+
* The caller must already have some kind of locking to ensure that chip is
44+
* valid. This function will lock the chip so that the ops member can be
45+
* accessed safely. The locking prevents tpm_chip_unregister from
46+
* completing, so it should not be held for long periods.
47+
*
48+
* Returns -ERRNO if the chip could not be got.
4249
*/
50+
int tpm_try_get_ops(struct tpm_chip *chip)
51+
{
52+
int rc = -EIO;
53+
54+
get_device(&chip->dev);
55+
56+
down_read(&chip->ops_sem);
57+
if (!chip->ops)
58+
goto out_lock;
59+
60+
if (!try_module_get(chip->dev.parent->driver->owner))
61+
goto out_lock;
62+
63+
return 0;
64+
out_lock:
65+
up_read(&chip->ops_sem);
66+
put_device(&chip->dev);
67+
return rc;
68+
}
69+
EXPORT_SYMBOL_GPL(tpm_try_get_ops);
70+
71+
/**
72+
* tpm_put_ops() - Release a ref to the tpm_chip
73+
* @chip: Chip to put
74+
*
75+
* This is the opposite pair to tpm_try_get_ops(). After this returns chip may
76+
* be kfree'd.
77+
*/
78+
void tpm_put_ops(struct tpm_chip *chip)
79+
{
80+
module_put(chip->dev.parent->driver->owner);
81+
up_read(&chip->ops_sem);
82+
put_device(&chip->dev);
83+
}
84+
EXPORT_SYMBOL_GPL(tpm_put_ops);
85+
86+
/**
87+
* tpm_chip_find_get() - return tpm_chip for a given chip number
88+
* @chip_num: id to find
89+
*
90+
* The return'd chip has been tpm_try_get_ops'd and must be released via
91+
* tpm_put_ops
92+
*/
4393
struct tpm_chip *tpm_chip_find_get(int chip_num)
4494
{
4595
struct tpm_chip *pos, *chip = NULL;
@@ -49,10 +99,10 @@ struct tpm_chip *tpm_chip_find_get(int chip_num)
4999
if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
50100
continue;
51101

52-
if (try_module_get(pos->dev.parent->driver->owner)) {
102+
/* rcu prevents chip from being free'd */
103+
if (!tpm_try_get_ops(pos))
53104
chip = pos;
54-
break;
55-
}
105+
break;
56106
}
57107
rcu_read_unlock();
58108
return chip;
@@ -95,6 +145,7 @@ struct tpm_chip *tpmm_chip_alloc(struct device *dev,
95145
return ERR_PTR(-ENOMEM);
96146

97147
mutex_init(&chip->tpm_mutex);
148+
init_rwsem(&chip->ops_sem);
98149
INIT_LIST_HEAD(&chip->list);
99150

100151
chip->ops = ops;
@@ -180,6 +231,12 @@ static int tpm_add_char_device(struct tpm_chip *chip)
180231
static void tpm_del_char_device(struct tpm_chip *chip)
181232
{
182233
cdev_del(&chip->cdev);
234+
235+
/* Make the driver uncallable. */
236+
down_write(&chip->ops_sem);
237+
chip->ops = NULL;
238+
up_write(&chip->ops_sem);
239+
183240
device_del(&chip->dev);
184241
}
185242

@@ -265,6 +322,9 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
265322
* Takes the chip first away from the list of available TPM chips and then
266323
* cleans up all the resources reserved by tpm_chip_register().
267324
*
325+
* Once this function returns the driver call backs in 'op's will not be
326+
* running and will no longer start.
327+
*
268328
* NOTE: This function should be only called before deinitializing chip
269329
* resources.
270330
*/

‎drivers/char/tpm/tpm-dev.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,18 @@ static ssize_t tpm_write(struct file *file, const char __user *buf,
136136
return -EFAULT;
137137
}
138138

139-
/* atomic tpm command send and result receive */
139+
/* atomic tpm command send and result receive. We only hold the ops
140+
* lock during this period so that the tpm can be unregistered even if
141+
* the char dev is held open.
142+
*/
143+
if (tpm_try_get_ops(priv->chip)) {
144+
mutex_unlock(&priv->buffer_mutex);
145+
return -EPIPE;
146+
}
140147
out_size = tpm_transmit(priv->chip, priv->data_buffer,
141148
sizeof(priv->data_buffer));
149+
150+
tpm_put_ops(priv->chip);
142151
if (out_size < 0) {
143152
mutex_unlock(&priv->buffer_mutex);
144153
return out_size;

‎drivers/char/tpm/tpm-interface.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ int tpm_is_tpm2(u32 chip_num)
700700

701701
rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
702702

703-
tpm_chip_put(chip);
703+
tpm_put_ops(chip);
704704

705705
return rc;
706706
}
@@ -729,7 +729,7 @@ int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
729729
rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
730730
else
731731
rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
732-
tpm_chip_put(chip);
732+
tpm_put_ops(chip);
733733
return rc;
734734
}
735735
EXPORT_SYMBOL_GPL(tpm_pcr_read);
@@ -764,7 +764,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
764764

765765
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
766766
rc = tpm2_pcr_extend(chip, pcr_idx, hash);
767-
tpm_chip_put(chip);
767+
tpm_put_ops(chip);
768768
return rc;
769769
}
770770

@@ -774,7 +774,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
774774
rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
775775
"attempting extend a PCR value");
776776

777-
tpm_chip_put(chip);
777+
tpm_put_ops(chip);
778778
return rc;
779779
}
780780
EXPORT_SYMBOL_GPL(tpm_pcr_extend);
@@ -855,7 +855,7 @@ int tpm_send(u32 chip_num, void *cmd, size_t buflen)
855855

856856
rc = tpm_transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
857857

858-
tpm_chip_put(chip);
858+
tpm_put_ops(chip);
859859
return rc;
860860
}
861861
EXPORT_SYMBOL_GPL(tpm_send);
@@ -1037,7 +1037,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
10371037

10381038
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
10391039
err = tpm2_get_random(chip, out, max);
1040-
tpm_chip_put(chip);
1040+
tpm_put_ops(chip);
10411041
return err;
10421042
}
10431043

@@ -1059,7 +1059,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
10591059
num_bytes -= recd;
10601060
} while (retries-- && total < max);
10611061

1062-
tpm_chip_put(chip);
1062+
tpm_put_ops(chip);
10631063
return total ? total : -EIO;
10641064
}
10651065
EXPORT_SYMBOL_GPL(tpm_get_random);
@@ -1085,7 +1085,7 @@ int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
10851085

10861086
rc = tpm2_seal_trusted(chip, payload, options);
10871087

1088-
tpm_chip_put(chip);
1088+
tpm_put_ops(chip);
10891089
return rc;
10901090
}
10911091
EXPORT_SYMBOL_GPL(tpm_seal_trusted);
@@ -1111,7 +1111,8 @@ int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
11111111

11121112
rc = tpm2_unseal_trusted(chip, payload, options);
11131113

1114-
tpm_chip_put(chip);
1114+
tpm_put_ops(chip);
1115+
11151116
return rc;
11161117
}
11171118
EXPORT_SYMBOL_GPL(tpm_unseal_trusted);

‎drivers/char/tpm/tpm-sysfs.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,5 +295,10 @@ int tpm_sysfs_add_device(struct tpm_chip *chip)
295295

296296
void tpm_sysfs_del_device(struct tpm_chip *chip)
297297
{
298+
/* The sysfs routines rely on an implicit tpm_try_get_ops, this
299+
* function is called before ops is null'd and the sysfs core
300+
* synchronizes this removal so that no callbacks are running or can
301+
* run again
302+
*/
298303
sysfs_remove_group(&chip->dev.parent->kobj, &tpm_dev_group);
299304
}

‎drivers/char/tpm/tpm.h‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ struct tpm_chip {
170170
struct device dev;
171171
struct cdev cdev;
172172

173+
/* A driver callback under ops cannot be run unless ops_sem is held
174+
* (sometimes implicitly, eg for the sysfs code). ops becomes null
175+
* when the driver is unregistered, see tpm_try_get_ops.
176+
*/
177+
struct rw_semaphore ops_sem;
173178
const struct tpm_class_ops *ops;
179+
174180
unsigned int flags;
175181

176182
int dev_num; /* /dev/tpm# */
@@ -195,11 +201,6 @@ struct tpm_chip {
195201

196202
#define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
197203

198-
static inline void tpm_chip_put(struct tpm_chip *chip)
199-
{
200-
module_put(chip->dev.parent->driver->owner);
201-
}
202-
203204
static inline int tpm_read_index(int base, int index)
204205
{
205206
outb(index, base);
@@ -507,6 +508,9 @@ extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
507508
wait_queue_head_t *, bool);
508509

509510
struct tpm_chip *tpm_chip_find_get(int chip_num);
511+
__must_check int tpm_try_get_ops(struct tpm_chip *chip);
512+
void tpm_put_ops(struct tpm_chip *chip);
513+
510514
extern struct tpm_chip *tpmm_chip_alloc(struct device *dev,
511515
const struct tpm_class_ops *ops);
512516
extern int tpm_chip_register(struct tpm_chip *chip);

0 commit comments

Comments
 (0)