1 /*
2  * Copyright (C) 2004 IBM Corporation
3  * Copyright (C) 2014 Intel Corporation
4  *
5  * Authors:
6  * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7  * Leendert van Doorn <leendert@watson.ibm.com>
8  * Dave Safford <safford@watson.ibm.com>
9  * Reiner Sailer <sailer@watson.ibm.com>
10  * Kylene Hall <kjhall@us.ibm.com>
11  *
12  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13  *
14  * TPM chip management routines.
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation, version 2 of the
19  * License.
20  *
21  */
22 
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock.h>
27 #include <linux/freezer.h>
28 #include <linux/major.h>
29 #include <linux/tpm_eventlog.h>
30 #include <linux/hw_random.h>
31 #include "tpm.h"
32 
33 DEFINE_IDR(dev_nums_idr);
34 static DEFINE_MUTEX(idr_lock);
35 
36 struct class *tpm_class;
37 struct class *tpmrm_class;
38 dev_t tpm_devt;
39 
40 /**
41  * tpm_try_get_ops() - Get a ref to the tpm_chip
42  * @chip: Chip to ref
43  *
44  * The caller must already have some kind of locking to ensure that chip is
45  * valid. This function will lock the chip so that the ops member can be
46  * accessed safely. The locking prevents tpm_chip_unregister from
47  * completing, so it should not be held for long periods.
48  *
49  * Returns -ERRNO if the chip could not be got.
50  */
tpm_try_get_ops(struct tpm_chip * chip)51 int tpm_try_get_ops(struct tpm_chip *chip)
52 {
53 	int rc = -EIO;
54 
55 	get_device(&chip->dev);
56 
57 	down_read(&chip->ops_sem);
58 	if (!chip->ops)
59 		goto out_lock;
60 
61 	return 0;
62 out_lock:
63 	up_read(&chip->ops_sem);
64 	put_device(&chip->dev);
65 	return rc;
66 }
67 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
68 
69 /**
70  * tpm_put_ops() - Release a ref to the tpm_chip
71  * @chip: Chip to put
72  *
73  * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
74  * be kfree'd.
75  */
tpm_put_ops(struct tpm_chip * chip)76 void tpm_put_ops(struct tpm_chip *chip)
77 {
78 	up_read(&chip->ops_sem);
79 	put_device(&chip->dev);
80 }
81 EXPORT_SYMBOL_GPL(tpm_put_ops);
82 
83 /**
84  * tpm_default_chip() - find a TPM chip and get a reference to it
85  */
tpm_default_chip(void)86 struct tpm_chip *tpm_default_chip(void)
87 {
88 	struct tpm_chip *chip, *res = NULL;
89 	int chip_num = 0;
90 	int chip_prev;
91 
92 	mutex_lock(&idr_lock);
93 
94 	do {
95 		chip_prev = chip_num;
96 		chip = idr_get_next(&dev_nums_idr, &chip_num);
97 		if (chip) {
98 			get_device(&chip->dev);
99 			res = chip;
100 			break;
101 		}
102 	} while (chip_prev != chip_num);
103 
104 	mutex_unlock(&idr_lock);
105 
106 	return res;
107 }
108 EXPORT_SYMBOL_GPL(tpm_default_chip);
109 
110 /**
111  * tpm_find_get_ops() - find and reserve a TPM chip
112  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
113  *
114  * Finds a TPM chip and reserves its class device and operations. The chip must
115  * be released with tpm_put_ops() after use.
116  * This function is for internal use only. It supports existing TPM callers
117  * by accepting NULL, but those callers should be converted to pass in a chip
118  * directly.
119  *
120  * Return:
121  * A reserved &struct tpm_chip instance.
122  * %NULL if a chip is not found.
123  * %NULL if the chip is not available.
124  */
tpm_find_get_ops(struct tpm_chip * chip)125 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
126 {
127 	int rc;
128 
129 	if (chip) {
130 		if (!tpm_try_get_ops(chip))
131 			return chip;
132 		return NULL;
133 	}
134 
135 	chip = tpm_default_chip();
136 	if (!chip)
137 		return NULL;
138 	rc = tpm_try_get_ops(chip);
139 	/* release additional reference we got from tpm_default_chip() */
140 	put_device(&chip->dev);
141 	if (rc)
142 		return NULL;
143 	return chip;
144 }
145 
146 /**
147  * tpm_dev_release() - free chip memory and the device number
148  * @dev: the character device for the TPM chip
149  *
150  * This is used as the release function for the character device.
151  */
tpm_dev_release(struct device * dev)152 static void tpm_dev_release(struct device *dev)
153 {
154 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
155 
156 	mutex_lock(&idr_lock);
157 	idr_remove(&dev_nums_idr, chip->dev_num);
158 	mutex_unlock(&idr_lock);
159 
160 	kfree(chip->log.bios_event_log);
161 	kfree(chip->work_space.context_buf);
162 	kfree(chip->work_space.session_buf);
163 	kfree(chip);
164 }
165 
166 /**
167  * tpm_class_shutdown() - prepare the TPM device for loss of power.
168  * @dev: device to which the chip is associated.
169  *
170  * Issues a TPM2_Shutdown command prior to loss of power, as required by the
171  * TPM 2.0 spec.
172  * Then, calls bus- and device- specific shutdown code.
173  *
174  * XXX: This codepath relies on the fact that sysfs is not enabled for
175  * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
176  * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
177  */
tpm_class_shutdown(struct device * dev)178 static int tpm_class_shutdown(struct device *dev)
179 {
180 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
181 
182 	down_write(&chip->ops_sem);
183 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
184 		tpm2_shutdown(chip, TPM2_SU_CLEAR);
185 		chip->ops = NULL;
186 	}
187 	chip->ops = NULL;
188 	up_write(&chip->ops_sem);
189 
190 	return 0;
191 }
192 
193 /**
194  * tpm_chip_alloc() - allocate a new struct tpm_chip instance
195  * @pdev: device to which the chip is associated
196  *        At this point pdev mst be initialized, but does not have to
197  *        be registered
198  * @ops: struct tpm_class_ops instance
199  *
200  * Allocates a new struct tpm_chip instance and assigns a free
201  * device number for it. Must be paired with put_device(&chip->dev).
202  */
tpm_chip_alloc(struct device * pdev,const struct tpm_class_ops * ops)203 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
204 				const struct tpm_class_ops *ops)
205 {
206 	struct tpm_chip *chip;
207 	int rc;
208 
209 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
210 	if (chip == NULL)
211 		return ERR_PTR(-ENOMEM);
212 
213 	mutex_init(&chip->tpm_mutex);
214 	init_rwsem(&chip->ops_sem);
215 
216 	chip->ops = ops;
217 
218 	mutex_lock(&idr_lock);
219 	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
220 	mutex_unlock(&idr_lock);
221 	if (rc < 0) {
222 		dev_err(pdev, "No available tpm device numbers\n");
223 		kfree(chip);
224 		return ERR_PTR(rc);
225 	}
226 	chip->dev_num = rc;
227 
228 	device_initialize(&chip->dev);
229 
230 	chip->dev.class = tpm_class;
231 	chip->dev.class->shutdown_pre = tpm_class_shutdown;
232 	chip->dev.release = tpm_dev_release;
233 	chip->dev.parent = pdev;
234 	chip->dev.groups = chip->groups;
235 
236 	if (chip->dev_num == 0)
237 		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
238 	else
239 		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
240 
241 	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
242 	if (rc)
243 		goto out;
244 
245 	if (!pdev)
246 		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
247 
248 	cdev_init(&chip->cdev, &tpm_fops);
249 	chip->cdev.owner = THIS_MODULE;
250 
251 	rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
252 	if (rc) {
253 		rc = -ENOMEM;
254 		goto out;
255 	}
256 
257 	chip->locality = -1;
258 	return chip;
259 
260 out:
261 	put_device(&chip->dev);
262 	return ERR_PTR(rc);
263 }
264 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
265 
266 /**
267  * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
268  * @pdev: parent device to which the chip is associated
269  * @ops: struct tpm_class_ops instance
270  *
271  * Same as tpm_chip_alloc except devm is used to do the put_device
272  */
tpmm_chip_alloc(struct device * pdev,const struct tpm_class_ops * ops)273 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
274 				 const struct tpm_class_ops *ops)
275 {
276 	struct tpm_chip *chip;
277 	int rc;
278 
279 	chip = tpm_chip_alloc(pdev, ops);
280 	if (IS_ERR(chip))
281 		return chip;
282 
283 	rc = devm_add_action_or_reset(pdev,
284 				      (void (*)(void *)) put_device,
285 				      &chip->dev);
286 	if (rc)
287 		return ERR_PTR(rc);
288 
289 	dev_set_drvdata(pdev, chip);
290 
291 	return chip;
292 }
293 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
294 
tpm_add_char_device(struct tpm_chip * chip)295 static int tpm_add_char_device(struct tpm_chip *chip)
296 {
297 	int rc;
298 
299 	rc = cdev_device_add(&chip->cdev, &chip->dev);
300 	if (rc) {
301 		dev_err(&chip->dev,
302 			"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
303 			dev_name(&chip->dev), MAJOR(chip->dev.devt),
304 			MINOR(chip->dev.devt), rc);
305 		return rc;
306 	}
307 
308 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
309 		rc = tpm_devs_add(chip);
310 		if (rc)
311 			goto err_del_cdev;
312 	}
313 
314 	/* Make the chip available. */
315 	mutex_lock(&idr_lock);
316 	idr_replace(&dev_nums_idr, chip, chip->dev_num);
317 	mutex_unlock(&idr_lock);
318 
319 	return 0;
320 
321 err_del_cdev:
322 	cdev_device_del(&chip->cdev, &chip->dev);
323 	return rc;
324 }
325 
tpm_del_char_device(struct tpm_chip * chip)326 static void tpm_del_char_device(struct tpm_chip *chip)
327 {
328 	cdev_device_del(&chip->cdev, &chip->dev);
329 
330 	/* Make the chip unavailable. */
331 	mutex_lock(&idr_lock);
332 	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
333 	mutex_unlock(&idr_lock);
334 
335 	/* Make the driver uncallable. */
336 	down_write(&chip->ops_sem);
337 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
338 		tpm2_shutdown(chip, TPM2_SU_CLEAR);
339 	chip->ops = NULL;
340 	up_write(&chip->ops_sem);
341 }
342 
tpm_del_legacy_sysfs(struct tpm_chip * chip)343 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
344 {
345 	struct attribute **i;
346 
347 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
348 		return;
349 
350 	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
351 
352 	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
353 		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
354 }
355 
356 /* For compatibility with legacy sysfs paths we provide symlinks from the
357  * parent dev directory to selected names within the tpm chip directory. Old
358  * kernel versions created these files directly under the parent.
359  */
tpm_add_legacy_sysfs(struct tpm_chip * chip)360 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
361 {
362 	struct attribute **i;
363 	int rc;
364 
365 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
366 		return 0;
367 
368 	rc = __compat_only_sysfs_link_entry_to_kobj(
369 		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
370 	if (rc && rc != -ENOENT)
371 		return rc;
372 
373 	/* All the names from tpm-sysfs */
374 	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
375 		rc = __compat_only_sysfs_link_entry_to_kobj(
376 		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
377 		if (rc) {
378 			tpm_del_legacy_sysfs(chip);
379 			return rc;
380 		}
381 	}
382 
383 	return 0;
384 }
385 
tpm_hwrng_read(struct hwrng * rng,void * data,size_t max,bool wait)386 static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
387 {
388 	struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
389 
390 	return tpm_get_random(chip, data, max);
391 }
392 
tpm_add_hwrng(struct tpm_chip * chip)393 static int tpm_add_hwrng(struct tpm_chip *chip)
394 {
395 	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
396 		return 0;
397 
398 	snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
399 		 "tpm-rng-%d", chip->dev_num);
400 	chip->hwrng.name = chip->hwrng_name;
401 	chip->hwrng.read = tpm_hwrng_read;
402 	return hwrng_register(&chip->hwrng);
403 }
404 
405 /*
406  * tpm_chip_register() - create a character device for the TPM chip
407  * @chip: TPM chip to use.
408  *
409  * Creates a character device for the TPM chip and adds sysfs attributes for
410  * the device. As the last step this function adds the chip to the list of TPM
411  * chips available for in-kernel use.
412  *
413  * This function should be only called after the chip initialization is
414  * complete.
415  */
tpm_chip_register(struct tpm_chip * chip)416 int tpm_chip_register(struct tpm_chip *chip)
417 {
418 	int rc;
419 
420 	if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
421 		if (chip->flags & TPM_CHIP_FLAG_TPM2)
422 			rc = tpm2_auto_startup(chip);
423 		else
424 			rc = tpm1_auto_startup(chip);
425 		if (rc)
426 			return rc;
427 	}
428 
429 	tpm_sysfs_add_device(chip);
430 
431 	tpm_bios_log_setup(chip);
432 
433 	tpm_add_ppi(chip);
434 
435 	rc = tpm_add_hwrng(chip);
436 	if (rc)
437 		goto out_ppi;
438 
439 	rc = tpm_add_char_device(chip);
440 	if (rc)
441 		goto out_hwrng;
442 
443 	rc = tpm_add_legacy_sysfs(chip);
444 	if (rc) {
445 		tpm_chip_unregister(chip);
446 		return rc;
447 	}
448 
449 	return 0;
450 
451 out_hwrng:
452 	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
453 		hwrng_unregister(&chip->hwrng);
454 out_ppi:
455 	tpm_bios_log_teardown(chip);
456 
457 	return rc;
458 }
459 EXPORT_SYMBOL_GPL(tpm_chip_register);
460 
461 /*
462  * tpm_chip_unregister() - release the TPM driver
463  * @chip: TPM chip to use.
464  *
465  * Takes the chip first away from the list of available TPM chips and then
466  * cleans up all the resources reserved by tpm_chip_register().
467  *
468  * Once this function returns the driver call backs in 'op's will not be
469  * running and will no longer start.
470  *
471  * NOTE: This function should be only called before deinitializing chip
472  * resources.
473  */
tpm_chip_unregister(struct tpm_chip * chip)474 void tpm_chip_unregister(struct tpm_chip *chip)
475 {
476 	tpm_del_legacy_sysfs(chip);
477 	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
478 		hwrng_unregister(&chip->hwrng);
479 	tpm_bios_log_teardown(chip);
480 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
481 		tpm_devs_remove(chip);
482 	tpm_del_char_device(chip);
483 }
484 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
485