aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2014-07-14 17:38:12 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-07-17 21:44:19 -0400
commit0a8adf584759cbcbce5d88d419db01a8d0373abf (patch)
tree30f1901d7c12756f558d259d89299c505697b41c
parentb1425189d0f928babb62bdc0609a4cd4d341654d (diff)
test: add firmware_class loader test
This provides a simple interface to trigger the firmware_class loader to test built-in, filesystem, and user helper modes. Additionally adds tests via the new interface to the selftests tree. Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--lib/Kconfig.debug13
-rw-r--r--lib/Makefile1
-rw-r--r--lib/test_firmware.c117
-rw-r--r--tools/testing/selftests/Makefile1
-rw-r--r--tools/testing/selftests/firmware/Makefile27
-rw-r--r--tools/testing/selftests/firmware/fw_filesystem.sh62
-rw-r--r--tools/testing/selftests/firmware/fw_userhelper.sh89
7 files changed, 310 insertions, 0 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 7a638aa3545b..213cd9f7e957 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1649,6 +1649,19 @@ config TEST_BPF
1649 1649
1650 If unsure, say N. 1650 If unsure, say N.
1651 1651
1652config TEST_FIRMWARE
1653 tristate "Test firmware loading via userspace interface"
1654 default n
1655 depends on FW_LOADER
1656 help
1657 This builds the "test_firmware" module that creates a userspace
1658 interface for testing firmware loading. This can be used to
1659 control the triggering of firmware loading without needing an
1660 actual firmware-using device. The contents can be rechecked by
1661 userspace.
1662
1663 If unsure, say N.
1664
1652source "samples/Kconfig" 1665source "samples/Kconfig"
1653 1666
1654source "lib/Kconfig.kgdb" 1667source "lib/Kconfig.kgdb"
diff --git a/lib/Makefile b/lib/Makefile
index ba967a19edba..230b4b1456d6 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -34,6 +34,7 @@ obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
34obj-$(CONFIG_TEST_MODULE) += test_module.o 34obj-$(CONFIG_TEST_MODULE) += test_module.o
35obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o 35obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
36obj-$(CONFIG_TEST_BPF) += test_bpf.o 36obj-$(CONFIG_TEST_BPF) += test_bpf.o
37obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
37 38
38ifeq ($(CONFIG_DEBUG_KOBJECT),y) 39ifeq ($(CONFIG_DEBUG_KOBJECT),y)
39CFLAGS_kobject.o += -DDEBUG 40CFLAGS_kobject.o += -DDEBUG
diff --git a/lib/test_firmware.c b/lib/test_firmware.c
new file mode 100644
index 000000000000..86374c1c49a4
--- /dev/null
+++ b/lib/test_firmware.c
@@ -0,0 +1,117 @@
1/*
2 * This module provides an interface to trigger and test firmware loading.
3 *
4 * It is designed to be used for basic evaluation of the firmware loading
5 * subsystem (for example when validating firmware verification). It lacks
6 * any extra dependencies, and will not normally be loaded by the system
7 * unless explicitly requested by name.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/printk.h>
15#include <linux/firmware.h>
16#include <linux/device.h>
17#include <linux/fs.h>
18#include <linux/miscdevice.h>
19#include <linux/slab.h>
20#include <linux/uaccess.h>
21
22static DEFINE_MUTEX(test_fw_mutex);
23static const struct firmware *test_firmware;
24
25static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
26 size_t size, loff_t *offset)
27{
28 ssize_t rc = 0;
29
30 mutex_lock(&test_fw_mutex);
31 if (test_firmware)
32 rc = simple_read_from_buffer(buf, size, offset,
33 test_firmware->data,
34 test_firmware->size);
35 mutex_unlock(&test_fw_mutex);
36 return rc;
37}
38
39static const struct file_operations test_fw_fops = {
40 .owner = THIS_MODULE,
41 .read = test_fw_misc_read,
42};
43
44static struct miscdevice test_fw_misc_device = {
45 .minor = MISC_DYNAMIC_MINOR,
46 .name = "test_firmware",
47 .fops = &test_fw_fops,
48};
49
50static ssize_t trigger_request_store(struct device *dev,
51 struct device_attribute *attr,
52 const char *buf, size_t count)
53{
54 int rc;
55 char *name;
56
57 name = kzalloc(count + 1, GFP_KERNEL);
58 if (!name)
59 return -ENOSPC;
60 memcpy(name, buf, count);
61
62 pr_info("loading '%s'\n", name);
63
64 mutex_lock(&test_fw_mutex);
65 release_firmware(test_firmware);
66 test_firmware = NULL;
67 rc = request_firmware(&test_firmware, name, dev);
68 if (rc)
69 pr_info("load of '%s' failed: %d\n", name, rc);
70 pr_info("loaded: %zu\n", test_firmware ? test_firmware->size : 0);
71 mutex_unlock(&test_fw_mutex);
72
73 kfree(name);
74
75 return count;
76}
77static DEVICE_ATTR_WO(trigger_request);
78
79static int __init test_firmware_init(void)
80{
81 int rc;
82
83 rc = misc_register(&test_fw_misc_device);
84 if (rc) {
85 pr_err("could not register misc device: %d\n", rc);
86 return rc;
87 }
88 rc = device_create_file(test_fw_misc_device.this_device,
89 &dev_attr_trigger_request);
90 if (rc) {
91 pr_err("could not create sysfs interface: %d\n", rc);
92 goto dereg;
93 }
94
95 pr_warn("interface ready\n");
96
97 return 0;
98dereg:
99 misc_deregister(&test_fw_misc_device);
100 return rc;
101}
102
103module_init(test_firmware_init);
104
105static void __exit test_firmware_exit(void)
106{
107 release_firmware(test_firmware);
108 device_remove_file(test_fw_misc_device.this_device,
109 &dev_attr_trigger_request);
110 misc_deregister(&test_fw_misc_device);
111 pr_warn("removed interface\n");
112}
113
114module_exit(test_firmware_exit);
115
116MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
117MODULE_LICENSE("GPL");
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index e66e710cc595..5c2bf8ec18f3 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -11,6 +11,7 @@ TARGETS += vm
11TARGETS += powerpc 11TARGETS += powerpc
12TARGETS += user 12TARGETS += user
13TARGETS += sysctl 13TARGETS += sysctl
14TARGETS += firmware
14 15
15all: 16all:
16 for TARGET in $(TARGETS); do \ 17 for TARGET in $(TARGETS); do \
diff --git a/tools/testing/selftests/firmware/Makefile b/tools/testing/selftests/firmware/Makefile
new file mode 100644
index 000000000000..e23cce0bbc3a
--- /dev/null
+++ b/tools/testing/selftests/firmware/Makefile
@@ -0,0 +1,27 @@
1# Makefile for firmware loading selftests
2
3# No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
4all:
5
6fw_filesystem:
7 @if /bin/sh ./fw_filesystem.sh ; then \
8 echo "fw_filesystem: ok"; \
9 else \
10 echo "fw_filesystem: [FAIL]"; \
11 exit 1; \
12 fi
13
14fw_userhelper:
15 @if /bin/sh ./fw_userhelper.sh ; then \
16 echo "fw_userhelper: ok"; \
17 else \
18 echo "fw_userhelper: [FAIL]"; \
19 exit 1; \
20 fi
21
22run_tests: all fw_filesystem fw_userhelper
23
24# Nothing to clean up.
25clean:
26
27.PHONY: all clean run_tests fw_filesystem fw_userhelper
diff --git a/tools/testing/selftests/firmware/fw_filesystem.sh b/tools/testing/selftests/firmware/fw_filesystem.sh
new file mode 100644
index 000000000000..3fc6c10c2479
--- /dev/null
+++ b/tools/testing/selftests/firmware/fw_filesystem.sh
@@ -0,0 +1,62 @@
1#!/bin/sh
2# This validates that the kernel will load firmware out of its list of
3# firmware locations on disk. Since the user helper does similar work,
4# we reset the custom load directory to a location the user helper doesn't
5# know so we can be sure we're not accidentally testing the user helper.
6set -e
7
8modprobe test_firmware
9
10DIR=/sys/devices/virtual/misc/test_firmware
11
12OLD_TIMEOUT=$(cat /sys/class/firmware/timeout)
13OLD_FWPATH=$(cat /sys/module/firmware_class/parameters/path)
14
15FWPATH=$(mktemp -d)
16FW="$FWPATH/test-firmware.bin"
17
18test_finish()
19{
20 echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
21 echo -n "$OLD_PATH" >/sys/module/firmware_class/parameters/path
22 rm -f "$FW"
23 rmdir "$FWPATH"
24}
25
26trap "test_finish" EXIT
27
28# Turn down the timeout so failures don't take so long.
29echo 1 >/sys/class/firmware/timeout
30# Set the kernel search path.
31echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
32
33# This is an unlikely real-world firmware content. :)
34echo "ABCD0123" >"$FW"
35
36NAME=$(basename "$FW")
37
38# Request a firmware that doesn't exist, it should fail.
39echo -n "nope-$NAME" >"$DIR"/trigger_request
40if diff -q "$FW" /dev/test_firmware >/dev/null ; then
41 echo "$0: firmware was not expected to match" >&2
42 exit 1
43else
44 echo "$0: timeout works"
45fi
46
47# This should succeed via kernel load or will fail after 1 second after
48# being handed over to the user helper, which won't find the fw either.
49if ! echo -n "$NAME" >"$DIR"/trigger_request ; then
50 echo "$0: could not trigger request" >&2
51 exit 1
52fi
53
54# Verify the contents are what we expect.
55if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
56 echo "$0: firmware was not loaded" >&2
57 exit 1
58else
59 echo "$0: filesystem loading works"
60fi
61
62exit 0
diff --git a/tools/testing/selftests/firmware/fw_userhelper.sh b/tools/testing/selftests/firmware/fw_userhelper.sh
new file mode 100644
index 000000000000..6efbade12139
--- /dev/null
+++ b/tools/testing/selftests/firmware/fw_userhelper.sh
@@ -0,0 +1,89 @@
1#!/bin/sh
2# This validates that the kernel will fall back to using the user helper
3# to load firmware it can't find on disk itself. We must request a firmware
4# that the kernel won't find, and any installed helper (e.g. udev) also
5# won't find so that we can do the load ourself manually.
6set -e
7
8modprobe test_firmware
9
10DIR=/sys/devices/virtual/misc/test_firmware
11
12OLD_TIMEOUT=$(cat /sys/class/firmware/timeout)
13
14FWPATH=$(mktemp -d)
15FW="$FWPATH/test-firmware.bin"
16
17test_finish()
18{
19 echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
20 rm -f "$FW"
21 rmdir "$FWPATH"
22}
23
24load_fw()
25{
26 local name="$1"
27 local file="$2"
28
29 # This will block until our load (below) has finished.
30 echo -n "$name" >"$DIR"/trigger_request &
31
32 # Give kernel a chance to react.
33 local timeout=10
34 while [ ! -e "$DIR"/"$name"/loading ]; do
35 sleep 0.1
36 timeout=$(( $timeout - 1 ))
37 if [ "$timeout" -eq 0 ]; then
38 echo "$0: firmware interface never appeared" >&2
39 exit 1
40 fi
41 done
42
43 echo 1 >"$DIR"/"$name"/loading
44 cat "$file" >"$DIR"/"$name"/data
45 echo 0 >"$DIR"/"$name"/loading
46
47 # Wait for request to finish.
48 wait
49}
50
51trap "test_finish" EXIT
52
53# This is an unlikely real-world firmware content. :)
54echo "ABCD0123" >"$FW"
55NAME=$(basename "$FW")
56
57# Test failure when doing nothing (timeout works).
58echo 1 >/sys/class/firmware/timeout
59echo -n "$NAME" >"$DIR"/trigger_request
60if diff -q "$FW" /dev/test_firmware >/dev/null ; then
61 echo "$0: firmware was not expected to match" >&2
62 exit 1
63else
64 echo "$0: timeout works"
65fi
66
67# Put timeout high enough for us to do work but not so long that failures
68# slow down this test too much.
69echo 4 >/sys/class/firmware/timeout
70
71# Load this script instead of the desired firmware.
72load_fw "$NAME" "$0"
73if diff -q "$FW" /dev/test_firmware >/dev/null ; then
74 echo "$0: firmware was not expected to match" >&2
75 exit 1
76else
77 echo "$0: firmware comparison works"
78fi
79
80# Do a proper load, which should work correctly.
81load_fw "$NAME" "$FW"
82if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
83 echo "$0: firmware was not loaded" >&2
84 exit 1
85else
86 echo "$0: user helper firmware loading works"
87fi
88
89exit 0