1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/types.h>
4 #include <linux/kconfig.h>
5 #include <linux/list.h>
6 #include <linux/slab.h>
7 #include <linux/security.h>
8 #include <linux/highmem.h>
9 #include <linux/umh.h>
10 #include <linux/sysctl.h>
11 #include <linux/vmalloc.h>
12 #include <linux/module.h>
13
14 #include "fallback.h"
15 #include "firmware.h"
16
17 /*
18 * firmware fallback mechanism
19 */
20
21 MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);
22
23 extern struct firmware_fallback_config fw_fallback_config;
24
25 /* These getters are vetted to use int properly */
__firmware_loading_timeout(void)26 static inline int __firmware_loading_timeout(void)
27 {
28 return fw_fallback_config.loading_timeout;
29 }
30
31 /* These setters are vetted to use int properly */
__fw_fallback_set_timeout(int timeout)32 static void __fw_fallback_set_timeout(int timeout)
33 {
34 fw_fallback_config.loading_timeout = timeout;
35 }
36
37 /*
38 * use small loading timeout for caching devices' firmware because all these
39 * firmware images have been loaded successfully at lease once, also system is
40 * ready for completing firmware loading now. The maximum size of firmware in
41 * current distributions is about 2M bytes, so 10 secs should be enough.
42 */
fw_fallback_set_cache_timeout(void)43 void fw_fallback_set_cache_timeout(void)
44 {
45 fw_fallback_config.old_timeout = __firmware_loading_timeout();
46 __fw_fallback_set_timeout(10);
47 }
48
49 /* Restores the timeout to the value last configured during normal operation */
fw_fallback_set_default_timeout(void)50 void fw_fallback_set_default_timeout(void)
51 {
52 __fw_fallback_set_timeout(fw_fallback_config.old_timeout);
53 }
54
firmware_loading_timeout(void)55 static long firmware_loading_timeout(void)
56 {
57 return __firmware_loading_timeout() > 0 ?
58 __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET;
59 }
60
fw_sysfs_done(struct fw_priv * fw_priv)61 static inline bool fw_sysfs_done(struct fw_priv *fw_priv)
62 {
63 return __fw_state_check(fw_priv, FW_STATUS_DONE);
64 }
65
fw_sysfs_loading(struct fw_priv * fw_priv)66 static inline bool fw_sysfs_loading(struct fw_priv *fw_priv)
67 {
68 return __fw_state_check(fw_priv, FW_STATUS_LOADING);
69 }
70
fw_sysfs_wait_timeout(struct fw_priv * fw_priv,long timeout)71 static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long timeout)
72 {
73 return __fw_state_wait_common(fw_priv, timeout);
74 }
75
76 struct fw_sysfs {
77 bool nowait;
78 struct device dev;
79 struct fw_priv *fw_priv;
80 struct firmware *fw;
81 };
82
to_fw_sysfs(struct device * dev)83 static struct fw_sysfs *to_fw_sysfs(struct device *dev)
84 {
85 return container_of(dev, struct fw_sysfs, dev);
86 }
87
__fw_load_abort(struct fw_priv * fw_priv)88 static void __fw_load_abort(struct fw_priv *fw_priv)
89 {
90 /*
91 * There is a small window in which user can write to 'loading'
92 * between loading done/aborted and disappearance of 'loading'
93 */
94 if (fw_state_is_aborted(fw_priv) || fw_sysfs_done(fw_priv))
95 return;
96
97 fw_state_aborted(fw_priv);
98 }
99
fw_load_abort(struct fw_sysfs * fw_sysfs)100 static void fw_load_abort(struct fw_sysfs *fw_sysfs)
101 {
102 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
103
104 __fw_load_abort(fw_priv);
105 }
106
107 static LIST_HEAD(pending_fw_head);
108
kill_pending_fw_fallback_reqs(bool kill_all)109 void kill_pending_fw_fallback_reqs(bool kill_all)
110 {
111 struct fw_priv *fw_priv;
112 struct fw_priv *next;
113
114 mutex_lock(&fw_lock);
115 list_for_each_entry_safe(fw_priv, next, &pending_fw_head,
116 pending_list) {
117 if (kill_all || !fw_priv->need_uevent)
118 __fw_load_abort(fw_priv);
119 }
120
121 if (kill_all)
122 fw_load_abort_all = true;
123
124 mutex_unlock(&fw_lock);
125 }
126
timeout_show(struct class * class,struct class_attribute * attr,char * buf)127 static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
128 char *buf)
129 {
130 return sysfs_emit(buf, "%d\n", __firmware_loading_timeout());
131 }
132
133 /**
134 * timeout_store() - set number of seconds to wait for firmware
135 * @class: device class pointer
136 * @attr: device attribute pointer
137 * @buf: buffer to scan for timeout value
138 * @count: number of bytes in @buf
139 *
140 * Sets the number of seconds to wait for the firmware. Once
141 * this expires an error will be returned to the driver and no
142 * firmware will be provided.
143 *
144 * Note: zero means 'wait forever'.
145 **/
timeout_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)146 static ssize_t timeout_store(struct class *class, struct class_attribute *attr,
147 const char *buf, size_t count)
148 {
149 int tmp_loading_timeout = simple_strtol(buf, NULL, 10);
150
151 if (tmp_loading_timeout < 0)
152 tmp_loading_timeout = 0;
153
154 __fw_fallback_set_timeout(tmp_loading_timeout);
155
156 return count;
157 }
158 static CLASS_ATTR_RW(timeout);
159
160 static struct attribute *firmware_class_attrs[] = {
161 &class_attr_timeout.attr,
162 NULL,
163 };
164 ATTRIBUTE_GROUPS(firmware_class);
165
fw_dev_release(struct device * dev)166 static void fw_dev_release(struct device *dev)
167 {
168 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
169
170 kfree(fw_sysfs);
171 }
172
do_firmware_uevent(struct fw_sysfs * fw_sysfs,struct kobj_uevent_env * env)173 static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_uevent_env *env)
174 {
175 if (add_uevent_var(env, "FIRMWARE=%s", fw_sysfs->fw_priv->fw_name))
176 return -ENOMEM;
177 if (add_uevent_var(env, "TIMEOUT=%i", __firmware_loading_timeout()))
178 return -ENOMEM;
179 if (add_uevent_var(env, "ASYNC=%d", fw_sysfs->nowait))
180 return -ENOMEM;
181
182 return 0;
183 }
184
firmware_uevent(struct device * dev,struct kobj_uevent_env * env)185 static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
186 {
187 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
188 int err = 0;
189
190 mutex_lock(&fw_lock);
191 if (fw_sysfs->fw_priv)
192 err = do_firmware_uevent(fw_sysfs, env);
193 mutex_unlock(&fw_lock);
194 return err;
195 }
196
197 static struct class firmware_class = {
198 .name = "firmware",
199 .class_groups = firmware_class_groups,
200 .dev_uevent = firmware_uevent,
201 .dev_release = fw_dev_release,
202 };
203
register_sysfs_loader(void)204 int register_sysfs_loader(void)
205 {
206 return class_register(&firmware_class);
207 }
208
unregister_sysfs_loader(void)209 void unregister_sysfs_loader(void)
210 {
211 class_unregister(&firmware_class);
212 }
213
firmware_loading_show(struct device * dev,struct device_attribute * attr,char * buf)214 static ssize_t firmware_loading_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
216 {
217 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
218 int loading = 0;
219
220 mutex_lock(&fw_lock);
221 if (fw_sysfs->fw_priv)
222 loading = fw_sysfs_loading(fw_sysfs->fw_priv);
223 mutex_unlock(&fw_lock);
224
225 return sysfs_emit(buf, "%d\n", loading);
226 }
227
228 /**
229 * firmware_loading_store() - set value in the 'loading' control file
230 * @dev: device pointer
231 * @attr: device attribute pointer
232 * @buf: buffer to scan for loading control value
233 * @count: number of bytes in @buf
234 *
235 * The relevant values are:
236 *
237 * 1: Start a load, discarding any previous partial load.
238 * 0: Conclude the load and hand the data to the driver code.
239 * -1: Conclude the load with an error and discard any written data.
240 **/
firmware_loading_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)241 static ssize_t firmware_loading_store(struct device *dev,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
244 {
245 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
246 struct fw_priv *fw_priv;
247 ssize_t written = count;
248 int loading = simple_strtol(buf, NULL, 10);
249
250 mutex_lock(&fw_lock);
251 fw_priv = fw_sysfs->fw_priv;
252 if (fw_state_is_aborted(fw_priv))
253 goto out;
254
255 switch (loading) {
256 case 1:
257 /* discarding any previous partial load */
258 if (!fw_sysfs_done(fw_priv)) {
259 fw_free_paged_buf(fw_priv);
260 fw_state_start(fw_priv);
261 }
262 break;
263 case 0:
264 if (fw_sysfs_loading(fw_priv)) {
265 int rc;
266
267 /*
268 * Several loading requests may be pending on
269 * one same firmware buf, so let all requests
270 * see the mapped 'buf->data' once the loading
271 * is completed.
272 * */
273 rc = fw_map_paged_buf(fw_priv);
274 if (rc)
275 dev_err(dev, "%s: map pages failed\n",
276 __func__);
277 else
278 rc = security_kernel_post_load_data(fw_priv->data,
279 fw_priv->size,
280 LOADING_FIRMWARE, "blob");
281
282 /*
283 * Same logic as fw_load_abort, only the DONE bit
284 * is ignored and we set ABORT only on failure.
285 */
286 if (rc) {
287 fw_state_aborted(fw_priv);
288 written = rc;
289 } else {
290 fw_state_done(fw_priv);
291 }
292 break;
293 }
294 fallthrough;
295 default:
296 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
297 fallthrough;
298 case -1:
299 fw_load_abort(fw_sysfs);
300 break;
301 }
302 out:
303 mutex_unlock(&fw_lock);
304 return written;
305 }
306
307 static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store);
308
firmware_rw_data(struct fw_priv * fw_priv,char * buffer,loff_t offset,size_t count,bool read)309 static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer,
310 loff_t offset, size_t count, bool read)
311 {
312 if (read)
313 memcpy(buffer, fw_priv->data + offset, count);
314 else
315 memcpy(fw_priv->data + offset, buffer, count);
316 }
317
firmware_rw(struct fw_priv * fw_priv,char * buffer,loff_t offset,size_t count,bool read)318 static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
319 loff_t offset, size_t count, bool read)
320 {
321 while (count) {
322 void *page_data;
323 int page_nr = offset >> PAGE_SHIFT;
324 int page_ofs = offset & (PAGE_SIZE-1);
325 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
326
327 page_data = kmap(fw_priv->pages[page_nr]);
328
329 if (read)
330 memcpy(buffer, page_data + page_ofs, page_cnt);
331 else
332 memcpy(page_data + page_ofs, buffer, page_cnt);
333
334 kunmap(fw_priv->pages[page_nr]);
335 buffer += page_cnt;
336 offset += page_cnt;
337 count -= page_cnt;
338 }
339 }
340
firmware_data_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t offset,size_t count)341 static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
342 struct bin_attribute *bin_attr,
343 char *buffer, loff_t offset, size_t count)
344 {
345 struct device *dev = kobj_to_dev(kobj);
346 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
347 struct fw_priv *fw_priv;
348 ssize_t ret_count;
349
350 mutex_lock(&fw_lock);
351 fw_priv = fw_sysfs->fw_priv;
352 if (!fw_priv || fw_sysfs_done(fw_priv)) {
353 ret_count = -ENODEV;
354 goto out;
355 }
356 if (offset > fw_priv->size) {
357 ret_count = 0;
358 goto out;
359 }
360 if (count > fw_priv->size - offset)
361 count = fw_priv->size - offset;
362
363 ret_count = count;
364
365 if (fw_priv->data)
366 firmware_rw_data(fw_priv, buffer, offset, count, true);
367 else
368 firmware_rw(fw_priv, buffer, offset, count, true);
369
370 out:
371 mutex_unlock(&fw_lock);
372 return ret_count;
373 }
374
fw_realloc_pages(struct fw_sysfs * fw_sysfs,int min_size)375 static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size)
376 {
377 int err;
378
379 err = fw_grow_paged_buf(fw_sysfs->fw_priv,
380 PAGE_ALIGN(min_size) >> PAGE_SHIFT);
381 if (err)
382 fw_load_abort(fw_sysfs);
383 return err;
384 }
385
386 /**
387 * firmware_data_write() - write method for firmware
388 * @filp: open sysfs file
389 * @kobj: kobject for the device
390 * @bin_attr: bin_attr structure
391 * @buffer: buffer being written
392 * @offset: buffer offset for write in total data store area
393 * @count: buffer size
394 *
395 * Data written to the 'data' attribute will be later handed to
396 * the driver as a firmware image.
397 **/
firmware_data_write(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t offset,size_t count)398 static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
399 struct bin_attribute *bin_attr,
400 char *buffer, loff_t offset, size_t count)
401 {
402 struct device *dev = kobj_to_dev(kobj);
403 struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
404 struct fw_priv *fw_priv;
405 ssize_t retval;
406
407 if (!capable(CAP_SYS_RAWIO))
408 return -EPERM;
409
410 mutex_lock(&fw_lock);
411 fw_priv = fw_sysfs->fw_priv;
412 if (!fw_priv || fw_sysfs_done(fw_priv)) {
413 retval = -ENODEV;
414 goto out;
415 }
416
417 if (fw_priv->data) {
418 if (offset + count > fw_priv->allocated_size) {
419 retval = -ENOMEM;
420 goto out;
421 }
422 firmware_rw_data(fw_priv, buffer, offset, count, false);
423 retval = count;
424 } else {
425 retval = fw_realloc_pages(fw_sysfs, offset + count);
426 if (retval)
427 goto out;
428
429 retval = count;
430 firmware_rw(fw_priv, buffer, offset, count, false);
431 }
432
433 fw_priv->size = max_t(size_t, offset + count, fw_priv->size);
434 out:
435 mutex_unlock(&fw_lock);
436 return retval;
437 }
438
439 static struct bin_attribute firmware_attr_data = {
440 .attr = { .name = "data", .mode = 0644 },
441 .size = 0,
442 .read = firmware_data_read,
443 .write = firmware_data_write,
444 };
445
446 static struct attribute *fw_dev_attrs[] = {
447 &dev_attr_loading.attr,
448 NULL
449 };
450
451 static struct bin_attribute *fw_dev_bin_attrs[] = {
452 &firmware_attr_data,
453 NULL
454 };
455
456 static const struct attribute_group fw_dev_attr_group = {
457 .attrs = fw_dev_attrs,
458 .bin_attrs = fw_dev_bin_attrs,
459 };
460
461 static const struct attribute_group *fw_dev_attr_groups[] = {
462 &fw_dev_attr_group,
463 NULL
464 };
465
466 static struct fw_sysfs *
fw_create_instance(struct firmware * firmware,const char * fw_name,struct device * device,u32 opt_flags)467 fw_create_instance(struct firmware *firmware, const char *fw_name,
468 struct device *device, u32 opt_flags)
469 {
470 struct fw_sysfs *fw_sysfs;
471 struct device *f_dev;
472
473 fw_sysfs = kzalloc(sizeof(*fw_sysfs), GFP_KERNEL);
474 if (!fw_sysfs) {
475 fw_sysfs = ERR_PTR(-ENOMEM);
476 goto exit;
477 }
478
479 fw_sysfs->nowait = !!(opt_flags & FW_OPT_NOWAIT);
480 fw_sysfs->fw = firmware;
481 f_dev = &fw_sysfs->dev;
482
483 device_initialize(f_dev);
484 dev_set_name(f_dev, "%s", fw_name);
485 f_dev->parent = device;
486 f_dev->class = &firmware_class;
487 f_dev->groups = fw_dev_attr_groups;
488 exit:
489 return fw_sysfs;
490 }
491
492 /**
493 * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mechanism
494 * @fw_sysfs: firmware sysfs information for the firmware to load
495 * @timeout: timeout to wait for the load
496 *
497 * In charge of constructing a sysfs fallback interface for firmware loading.
498 **/
fw_load_sysfs_fallback(struct fw_sysfs * fw_sysfs,long timeout)499 static int fw_load_sysfs_fallback(struct fw_sysfs *fw_sysfs, long timeout)
500 {
501 int retval = 0;
502 struct device *f_dev = &fw_sysfs->dev;
503 struct fw_priv *fw_priv = fw_sysfs->fw_priv;
504
505 /* fall back on userspace loading */
506 if (!fw_priv->data)
507 fw_priv->is_paged_buf = true;
508
509 dev_set_uevent_suppress(f_dev, true);
510
511 retval = device_add(f_dev);
512 if (retval) {
513 dev_err(f_dev, "%s: device_register failed\n", __func__);
514 goto err_put_dev;
515 }
516
517 mutex_lock(&fw_lock);
518 if (fw_load_abort_all || fw_state_is_aborted(fw_priv)) {
519 mutex_unlock(&fw_lock);
520 retval = -EINTR;
521 goto out;
522 }
523 list_add(&fw_priv->pending_list, &pending_fw_head);
524 mutex_unlock(&fw_lock);
525
526 if (fw_priv->opt_flags & FW_OPT_UEVENT) {
527 fw_priv->need_uevent = true;
528 dev_set_uevent_suppress(f_dev, false);
529 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_name);
530 kobject_uevent(&fw_sysfs->dev.kobj, KOBJ_ADD);
531 } else {
532 timeout = MAX_JIFFY_OFFSET;
533 }
534
535 retval = fw_sysfs_wait_timeout(fw_priv, timeout);
536 if (retval < 0 && retval != -ENOENT) {
537 mutex_lock(&fw_lock);
538 fw_load_abort(fw_sysfs);
539 mutex_unlock(&fw_lock);
540 }
541
542 if (fw_state_is_aborted(fw_priv)) {
543 if (retval == -ERESTARTSYS)
544 retval = -EINTR;
545 } else if (fw_priv->is_paged_buf && !fw_priv->data)
546 retval = -ENOMEM;
547
548 out:
549 device_del(f_dev);
550 err_put_dev:
551 put_device(f_dev);
552 return retval;
553 }
554
fw_load_from_user_helper(struct firmware * firmware,const char * name,struct device * device,u32 opt_flags)555 static int fw_load_from_user_helper(struct firmware *firmware,
556 const char *name, struct device *device,
557 u32 opt_flags)
558 {
559 struct fw_sysfs *fw_sysfs;
560 long timeout;
561 int ret;
562
563 timeout = firmware_loading_timeout();
564 if (opt_flags & FW_OPT_NOWAIT) {
565 timeout = usermodehelper_read_lock_wait(timeout);
566 if (!timeout) {
567 dev_dbg(device, "firmware: %s loading timed out\n",
568 name);
569 return -EBUSY;
570 }
571 } else {
572 ret = usermodehelper_read_trylock();
573 if (WARN_ON(ret)) {
574 dev_err(device, "firmware: %s will not be loaded\n",
575 name);
576 return ret;
577 }
578 }
579
580 fw_sysfs = fw_create_instance(firmware, name, device, opt_flags);
581 if (IS_ERR(fw_sysfs)) {
582 ret = PTR_ERR(fw_sysfs);
583 goto out_unlock;
584 }
585
586 fw_sysfs->fw_priv = firmware->priv;
587 ret = fw_load_sysfs_fallback(fw_sysfs, timeout);
588
589 if (!ret)
590 ret = assign_fw(firmware, device);
591
592 out_unlock:
593 usermodehelper_read_unlock();
594
595 return ret;
596 }
597
fw_force_sysfs_fallback(u32 opt_flags)598 static bool fw_force_sysfs_fallback(u32 opt_flags)
599 {
600 if (fw_fallback_config.force_sysfs_fallback)
601 return true;
602 if (!(opt_flags & FW_OPT_USERHELPER))
603 return false;
604 return true;
605 }
606
fw_run_sysfs_fallback(u32 opt_flags)607 static bool fw_run_sysfs_fallback(u32 opt_flags)
608 {
609 int ret;
610
611 if (fw_fallback_config.ignore_sysfs_fallback) {
612 pr_info_once("Ignoring firmware sysfs fallback due to sysctl knob\n");
613 return false;
614 }
615
616 if ((opt_flags & FW_OPT_NOFALLBACK_SYSFS))
617 return false;
618
619 /* Also permit LSMs and IMA to fail firmware sysfs fallback */
620 ret = security_kernel_load_data(LOADING_FIRMWARE, true);
621 if (ret < 0)
622 return false;
623
624 return fw_force_sysfs_fallback(opt_flags);
625 }
626
627 /**
628 * firmware_fallback_sysfs() - use the fallback mechanism to find firmware
629 * @fw: pointer to firmware image
630 * @name: name of firmware file to look for
631 * @device: device for which firmware is being loaded
632 * @opt_flags: options to control firmware loading behaviour, as defined by
633 * &enum fw_opt
634 * @ret: return value from direct lookup which triggered the fallback mechanism
635 *
636 * This function is called if direct lookup for the firmware failed, it enables
637 * a fallback mechanism through userspace by exposing a sysfs loading
638 * interface. Userspace is in charge of loading the firmware through the sysfs
639 * loading interface. This sysfs fallback mechanism may be disabled completely
640 * on a system by setting the proc sysctl value ignore_sysfs_fallback to true.
641 * If this is false we check if the internal API caller set the
642 * @FW_OPT_NOFALLBACK_SYSFS flag, if so it would also disable the fallback
643 * mechanism. A system may want to enforce the sysfs fallback mechanism at all
644 * times, it can do this by setting ignore_sysfs_fallback to false and
645 * force_sysfs_fallback to true.
646 * Enabling force_sysfs_fallback is functionally equivalent to build a kernel
647 * with CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
648 **/
firmware_fallback_sysfs(struct firmware * fw,const char * name,struct device * device,u32 opt_flags,int ret)649 int firmware_fallback_sysfs(struct firmware *fw, const char *name,
650 struct device *device,
651 u32 opt_flags,
652 int ret)
653 {
654 if (!fw_run_sysfs_fallback(opt_flags))
655 return ret;
656
657 if (!(opt_flags & FW_OPT_NO_WARN))
658 dev_warn(device, "Falling back to sysfs fallback for: %s\n",
659 name);
660 else
661 dev_dbg(device, "Falling back to sysfs fallback for: %s\n",
662 name);
663 return fw_load_from_user_helper(fw, name, device, opt_flags);
664 }
665