Skip to content

Commit 5ec5771

Browse files
jgunthorpegregkh
authored andcommitted
tpm: Provide strong locking for device removal
commit 4e26195f240d73150e8308ae42874702e3df8d2c upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5e07de5 commit 5ec5771

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;
@@ -94,6 +144,7 @@ struct tpm_chip *tpmm_chip_alloc(struct device *dev,
94144
return ERR_PTR(-ENOMEM);
95145

96146
mutex_init(&chip->tpm_mutex);
147+
init_rwsem(&chip->ops_sem);
97148
INIT_LIST_HEAD(&chip->list);
98149

99150
chip->ops = ops;
@@ -171,6 +222,12 @@ static int tpm_add_char_device(struct tpm_chip *chip)
171222
static void tpm_del_char_device(struct tpm_chip *chip)
172223
{
173224
cdev_del(&chip->cdev);
225+
226+
/* Make the driver uncallable. */
227+
down_write(&chip->ops_sem);
228+
chip->ops = NULL;
229+
up_write(&chip->ops_sem);
230+
174231
device_del(&chip->dev);
175232
}
176233

@@ -256,6 +313,9 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
256313
* Takes the chip first away from the list of available TPM chips and then
257314
* cleans up all the resources reserved by tpm_chip_register().
258315
*
316+
* Once this function returns the driver call backs in 'op's will not be
317+
* running and will no longer start.
318+
*
259319
* NOTE: This function should be only called before deinitializing chip
260320
* resources.
261321
*/

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), 0);
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
@@ -687,7 +687,7 @@ int tpm_is_tpm2(u32 chip_num)
687687

688688
rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
689689

690-
tpm_chip_put(chip);
690+
tpm_put_ops(chip);
691691

692692
return rc;
693693
}
@@ -716,7 +716,7 @@ int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
716716
rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
717717
else
718718
rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
719-
tpm_chip_put(chip);
719+
tpm_put_ops(chip);
720720
return rc;
721721
}
722722
EXPORT_SYMBOL_GPL(tpm_pcr_read);
@@ -751,7 +751,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
751751

752752
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
753753
rc = tpm2_pcr_extend(chip, pcr_idx, hash);
754-
tpm_chip_put(chip);
754+
tpm_put_ops(chip);
755755
return rc;
756756
}
757757

@@ -761,7 +761,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
761761
rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0,
762762
"attempting extend a PCR value");
763763

764-
tpm_chip_put(chip);
764+
tpm_put_ops(chip);
765765
return rc;
766766
}
767767
EXPORT_SYMBOL_GPL(tpm_pcr_extend);
@@ -842,7 +842,7 @@ int tpm_send(u32 chip_num, void *cmd, size_t buflen)
842842

843843
rc = tpm_transmit_cmd(chip, cmd, buflen, 0, "attempting tpm_cmd");
844844

845-
tpm_chip_put(chip);
845+
tpm_put_ops(chip);
846846
return rc;
847847
}
848848
EXPORT_SYMBOL_GPL(tpm_send);
@@ -1025,7 +1025,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
10251025

10261026
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
10271027
err = tpm2_get_random(chip, out, max);
1028-
tpm_chip_put(chip);
1028+
tpm_put_ops(chip);
10291029
return err;
10301030
}
10311031

@@ -1047,7 +1047,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
10471047
num_bytes -= recd;
10481048
} while (retries-- && total < max);
10491049

1050-
tpm_chip_put(chip);
1050+
tpm_put_ops(chip);
10511051
return total ? total : -EIO;
10521052
}
10531053
EXPORT_SYMBOL_GPL(tpm_get_random);
@@ -1073,7 +1073,7 @@ int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
10731073

10741074
rc = tpm2_seal_trusted(chip, payload, options);
10751075

1076-
tpm_chip_put(chip);
1076+
tpm_put_ops(chip);
10771077
return rc;
10781078
}
10791079
EXPORT_SYMBOL_GPL(tpm_seal_trusted);
@@ -1099,7 +1099,8 @@ int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
10991099

11001100
rc = tpm2_unseal_trusted(chip, payload, options);
11011101

1102-
tpm_chip_put(chip);
1102+
tpm_put_ops(chip);
1103+
11031104
return rc;
11041105
}
11051106
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
@@ -174,7 +174,13 @@ struct tpm_chip {
174174
struct device dev;
175175
struct cdev cdev;
176176

177+
/* A driver callback under ops cannot be run unless ops_sem is held
178+
* (sometimes implicitly, eg for the sysfs code). ops becomes null
179+
* when the driver is unregistered, see tpm_try_get_ops.
180+
*/
181+
struct rw_semaphore ops_sem;
177182
const struct tpm_class_ops *ops;
183+
178184
unsigned int flags;
179185

180186
int dev_num; /* /dev/tpm# */
@@ -200,11 +206,6 @@ struct tpm_chip {
200206

201207
#define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
202208

203-
static inline void tpm_chip_put(struct tpm_chip *chip)
204-
{
205-
module_put(chip->dev.parent->driver->owner);
206-
}
207-
208209
static inline int tpm_read_index(int base, int index)
209210
{
210211
outb(index, base);
@@ -516,6 +517,9 @@ extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
516517
wait_queue_head_t *, bool);
517518

518519
struct tpm_chip *tpm_chip_find_get(int chip_num);
520+
__must_check int tpm_try_get_ops(struct tpm_chip *chip);
521+
void tpm_put_ops(struct tpm_chip *chip);
522+
519523
extern struct tpm_chip *tpmm_chip_alloc(struct device *dev,
520524
const struct tpm_class_ops *ops);
521525
extern int tpm_chip_register(struct tpm_chip *chip);

0 commit comments

Comments
 (0)