misc: tizen-inform-reboot: fix a potential NULL pointer dereference
[platform/kernel/linux-rpi.git] / drivers / misc / tizen-inform-reboot.c
1 /*
2  * Tizen reboot parameter passing notifier
3  *
4  * Written by: Junghoon Kim <jhoon20.kim@samsung.com>
5  *
6  * Copyright (C) 2017 Samsung Electronics Co., Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/notifier.h>
14 #include <linux/reboot.h>
15 #include <linux/syscalls.h>
16 #include <linux/file.h>
17 #include <linux/fcntl.h>
18 #include <linux/uaccess.h>
19
20 static int inform_reboot_notifier(struct notifier_block *nb,
21                                                 unsigned long val, void *buf)
22 {
23         char *cmd = buf;
24         char *filename = CONFIG_TIZEN_INFORM_PATH;
25         struct file *file;
26         loff_t pos = 0;
27         mm_segment_t old_fs = get_fs();
28
29         set_fs(KERNEL_DS);
30
31         file = filp_open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
32         if (!IS_ERR(file)) {
33                 struct super_block *sb = file->f_path.dentry->d_sb;
34
35                 if (cmd) {
36                         if (!strncmp(cmd, "fota", 4))
37                                 cmd = "upgr";
38                         else if (!strncmp(cmd, "recovery", 8))
39                                 cmd = "rcvr";
40                         else if (!strncmp(cmd, "download", 8))
41                                 cmd = "dwnl";
42                         else
43                                 cmd = "ndef";
44                 } else
45                         cmd = "norm";
46
47                 vfs_write(file, cmd, strlen(cmd), &pos);
48
49                 down_read(&sb->s_umount);
50                 sync_filesystem(sb);
51                 up_read(&sb->s_umount);
52
53                 fput(file);
54         } else {
55                 pr_err("Reboot parameter passing is failed.\n"
56                                 "Inform file path should be described correctly in config.\n");
57         }
58
59         set_fs(old_fs);
60
61         return NOTIFY_DONE;
62 }
63
64 static struct notifier_block nb_inform_reboot_block = {
65         .notifier_call = inform_reboot_notifier,
66         .priority = 256,
67 };
68
69 static int __init inform_reboot_init(void)
70 {
71         /* to support reboot parameter passing */
72         register_reboot_notifier(&nb_inform_reboot_block);
73         return 0;
74 }
75
76 subsys_initcall(inform_reboot_init);