diff options
author | Brian Norris <computersforpeace@gmail.com> | 2015-12-09 17:50:27 -0500 |
---|---|---|
committer | Shuah Khan <shuahkh@osg.samsung.com> | 2016-01-07 15:44:22 -0500 |
commit | eb910947c82f93cee6ee00a59a4ed86f12cf6e7f (patch) | |
tree | 7e941e42f6241a8c62fd4f7393a9b6990407caaf | |
parent | be4a1326d12cce8df1f57017bf4112eaab437a38 (diff) |
test: firmware_class: add asynchronous request trigger
We might want to test for bugs like that found in commit f9692b2699bd
("firmware: fix possible use after free on name on asynchronous
request"), where the asynchronous request API had race conditions.
Let's add a simple file that will launch the async request, then wait
until it's complete and report the status. It's not a true async test
(we're using a mutex + wait_for_completion(), so we can't get more than
one going at the same time), but it does help make sure the basic API is
sane, and it can catch some class of bugs.
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Cc: Luis R. Rodriguez <mcgrof@suse.com>
Acked-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
-rw-r--r-- | lib/test_firmware.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/test_firmware.c b/lib/test_firmware.c index 690b9c35a274..a3e8ec3fb1c5 100644 --- a/lib/test_firmware.c +++ b/lib/test_firmware.c | |||
@@ -12,6 +12,7 @@ | |||
12 | #include <linux/init.h> | 12 | #include <linux/init.h> |
13 | #include <linux/module.h> | 13 | #include <linux/module.h> |
14 | #include <linux/printk.h> | 14 | #include <linux/printk.h> |
15 | #include <linux/completion.h> | ||
15 | #include <linux/firmware.h> | 16 | #include <linux/firmware.h> |
16 | #include <linux/device.h> | 17 | #include <linux/device.h> |
17 | #include <linux/fs.h> | 18 | #include <linux/fs.h> |
@@ -80,6 +81,57 @@ out: | |||
80 | } | 81 | } |
81 | static DEVICE_ATTR_WO(trigger_request); | 82 | static DEVICE_ATTR_WO(trigger_request); |
82 | 83 | ||
84 | static DECLARE_COMPLETION(async_fw_done); | ||
85 | |||
86 | static void trigger_async_request_cb(const struct firmware *fw, void *context) | ||
87 | { | ||
88 | test_firmware = fw; | ||
89 | complete(&async_fw_done); | ||
90 | } | ||
91 | |||
92 | static ssize_t trigger_async_request_store(struct device *dev, | ||
93 | struct device_attribute *attr, | ||
94 | const char *buf, size_t count) | ||
95 | { | ||
96 | int rc; | ||
97 | char *name; | ||
98 | |||
99 | name = kstrndup(buf, count, GFP_KERNEL); | ||
100 | if (!name) | ||
101 | return -ENOSPC; | ||
102 | |||
103 | pr_info("loading '%s'\n", name); | ||
104 | |||
105 | mutex_lock(&test_fw_mutex); | ||
106 | release_firmware(test_firmware); | ||
107 | test_firmware = NULL; | ||
108 | rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL, | ||
109 | NULL, trigger_async_request_cb); | ||
110 | if (rc) { | ||
111 | pr_info("async load of '%s' failed: %d\n", name, rc); | ||
112 | kfree(name); | ||
113 | goto out; | ||
114 | } | ||
115 | /* Free 'name' ASAP, to test for race conditions */ | ||
116 | kfree(name); | ||
117 | |||
118 | wait_for_completion(&async_fw_done); | ||
119 | |||
120 | if (test_firmware) { | ||
121 | pr_info("loaded: %zu\n", test_firmware->size); | ||
122 | rc = count; | ||
123 | } else { | ||
124 | pr_err("failed to async load firmware\n"); | ||
125 | rc = -ENODEV; | ||
126 | } | ||
127 | |||
128 | out: | ||
129 | mutex_unlock(&test_fw_mutex); | ||
130 | |||
131 | return rc; | ||
132 | } | ||
133 | static DEVICE_ATTR_WO(trigger_async_request); | ||
134 | |||
83 | static int __init test_firmware_init(void) | 135 | static int __init test_firmware_init(void) |
84 | { | 136 | { |
85 | int rc; | 137 | int rc; |
@@ -96,9 +148,20 @@ static int __init test_firmware_init(void) | |||
96 | goto dereg; | 148 | goto dereg; |
97 | } | 149 | } |
98 | 150 | ||
151 | rc = device_create_file(test_fw_misc_device.this_device, | ||
152 | &dev_attr_trigger_async_request); | ||
153 | if (rc) { | ||
154 | pr_err("could not create async sysfs interface: %d\n", rc); | ||
155 | goto remove_file; | ||
156 | } | ||
157 | |||
99 | pr_warn("interface ready\n"); | 158 | pr_warn("interface ready\n"); |
100 | 159 | ||
101 | return 0; | 160 | return 0; |
161 | |||
162 | remove_file: | ||
163 | device_remove_file(test_fw_misc_device.this_device, | ||
164 | &dev_attr_trigger_async_request); | ||
102 | dereg: | 165 | dereg: |
103 | misc_deregister(&test_fw_misc_device); | 166 | misc_deregister(&test_fw_misc_device); |
104 | return rc; | 167 | return rc; |
@@ -110,6 +173,8 @@ static void __exit test_firmware_exit(void) | |||
110 | { | 173 | { |
111 | release_firmware(test_firmware); | 174 | release_firmware(test_firmware); |
112 | device_remove_file(test_fw_misc_device.this_device, | 175 | device_remove_file(test_fw_misc_device.this_device, |
176 | &dev_attr_trigger_async_request); | ||
177 | device_remove_file(test_fw_misc_device.this_device, | ||
113 | &dev_attr_trigger_request); | 178 | &dev_attr_trigger_request); |
114 | misc_deregister(&test_fw_misc_device); | 179 | misc_deregister(&test_fw_misc_device); |
115 | pr_warn("removed interface\n"); | 180 | pr_warn("removed interface\n"); |