]> git.ipfire.org Git - people/ms/linux.git/blame - kernel/ksysfs.c
Importing "grsecurity-3.1-3.19.2-201503201903.patch"
[people/ms/linux.git] / kernel / ksysfs.c
CommitLineData
1da177e4
LT
1/*
2 * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
3 * are not related to any other subsystem
4 *
5 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 *
7 * This file is release under the GPLv2
8 *
9 */
10
1da177e4
LT
11#include <linux/kobject.h>
12#include <linux/string.h>
13#include <linux/sysfs.h>
9984de1a 14#include <linux/export.h>
1da177e4 15#include <linux/init.h>
c330dda9 16#include <linux/kexec.h>
22b8ce94 17#include <linux/profile.h>
1596425f 18#include <linux/stat.h>
5cb350ba 19#include <linux/sched.h>
088ab0b4 20#include <linux/capability.h>
52f5684c 21#include <linux/compiler.h>
1da177e4 22
7a754743
PG
23#include <linux/rcupdate.h> /* rcu_expedited */
24
1da177e4 25#define KERNEL_ATTR_RO(_name) \
386f275f 26static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1da177e4
LT
27
28#define KERNEL_ATTR_RW(_name) \
386f275f 29static struct kobj_attribute _name##_attr = \
1da177e4
LT
30 __ATTR(_name, 0644, _name##_show, _name##_store)
31
0f76e5ac 32/* current uevent sequence number */
386f275f
KS
33static ssize_t uevent_seqnum_show(struct kobject *kobj,
34 struct kobj_attribute *attr, char *buf)
1da177e4 35{
386f275f 36 return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
1da177e4 37}
0f76e5ac
KS
38KERNEL_ATTR_RO(uevent_seqnum);
39
86d56134 40#ifdef CONFIG_UEVENT_HELPER
af665852 41/* uevent helper program, used during early boot */
386f275f
KS
42static ssize_t uevent_helper_show(struct kobject *kobj,
43 struct kobj_attribute *attr, char *buf)
0f76e5ac 44{
386f275f 45 return sprintf(buf, "%s\n", uevent_helper);
0f76e5ac 46}
386f275f
KS
47static ssize_t uevent_helper_store(struct kobject *kobj,
48 struct kobj_attribute *attr,
49 const char *buf, size_t count)
0f76e5ac 50{
312c004d 51 if (count+1 > UEVENT_HELPER_PATH_LEN)
0f76e5ac 52 return -ENOENT;
63d9c273
MT
53 if (!capable(CAP_SYS_ADMIN))
54 return -EPERM;
386f275f 55 memcpy(uevent_helper, buf, count);
312c004d
KS
56 uevent_helper[count] = '\0';
57 if (count && uevent_helper[count-1] == '\n')
58 uevent_helper[count-1] = '\0';
0f76e5ac
KS
59 return count;
60}
61KERNEL_ATTR_RW(uevent_helper);
86d56134 62#endif
1da177e4 63
22b8ce94
DH
64#ifdef CONFIG_PROFILING
65static ssize_t profiling_show(struct kobject *kobj,
66 struct kobj_attribute *attr, char *buf)
67{
68 return sprintf(buf, "%d\n", prof_on);
69}
70static ssize_t profiling_store(struct kobject *kobj,
71 struct kobj_attribute *attr,
72 const char *buf, size_t count)
73{
74 int ret;
75
76 if (prof_on)
77 return -EEXIST;
78 /*
79 * This eventually calls into get_option() which
80 * has a ton of callers and is not const. It is
81 * easiest to cast it away here.
82 */
83 profile_setup((char *)buf);
84 ret = profile_init();
85 if (ret)
86 return ret;
87 ret = create_proc_profile();
88 if (ret)
89 return ret;
90 return count;
91}
92KERNEL_ATTR_RW(profiling);
93#endif
94
c330dda9 95#ifdef CONFIG_KEXEC
386f275f
KS
96static ssize_t kexec_loaded_show(struct kobject *kobj,
97 struct kobj_attribute *attr, char *buf)
c330dda9 98{
386f275f 99 return sprintf(buf, "%d\n", !!kexec_image);
c330dda9
JM
100}
101KERNEL_ATTR_RO(kexec_loaded);
102
386f275f
KS
103static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
104 struct kobj_attribute *attr, char *buf)
c330dda9 105{
386f275f 106 return sprintf(buf, "%d\n", !!kexec_crash_image);
c330dda9
JM
107}
108KERNEL_ATTR_RO(kexec_crash_loaded);
fd59d231 109
06a7f711
AW
110static ssize_t kexec_crash_size_show(struct kobject *kobj,
111 struct kobj_attribute *attr, char *buf)
112{
113 return sprintf(buf, "%zu\n", crash_get_memory_size());
114}
115static ssize_t kexec_crash_size_store(struct kobject *kobj,
116 struct kobj_attribute *attr,
117 const char *buf, size_t count)
118{
119 unsigned long cnt;
120 int ret;
121
6072ddc8 122 if (kstrtoul(buf, 0, &cnt))
06a7f711
AW
123 return -EINVAL;
124
125 ret = crash_shrink_memory(cnt);
126 return ret < 0 ? ret : count;
127}
128KERNEL_ATTR_RW(kexec_crash_size);
129
386f275f
KS
130static ssize_t vmcoreinfo_show(struct kobject *kobj,
131 struct kobj_attribute *attr, char *buf)
fd59d231 132{
386f275f 133 return sprintf(buf, "%lx %x\n",
fd59d231 134 paddr_vmcoreinfo_note(),
77019967 135 (unsigned int)sizeof(vmcoreinfo_note));
fd59d231
KO
136}
137KERNEL_ATTR_RO(vmcoreinfo);
138
c330dda9
JM
139#endif /* CONFIG_KEXEC */
140
088ab0b4
LN
141/* whether file capabilities are enabled */
142static ssize_t fscaps_show(struct kobject *kobj,
143 struct kobj_attribute *attr, char *buf)
144{
145 return sprintf(buf, "%d\n", file_caps_enabled);
146}
147KERNEL_ATTR_RO(fscaps);
148
3705b88d
AM
149int rcu_expedited;
150static ssize_t rcu_expedited_show(struct kobject *kobj,
151 struct kobj_attribute *attr, char *buf)
152{
153 return sprintf(buf, "%d\n", rcu_expedited);
154}
155static ssize_t rcu_expedited_store(struct kobject *kobj,
156 struct kobj_attribute *attr,
157 const char *buf, size_t count)
158{
159 if (kstrtoint(buf, 0, &rcu_expedited))
160 return -EINVAL;
161
162 return count;
163}
164KERNEL_ATTR_RW(rcu_expedited);
165
da1a679c
RM
166/*
167 * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
168 */
52f5684c
GID
169extern const void __start_notes __weak;
170extern const void __stop_notes __weak;
da1a679c
RM
171#define notes_size (&__stop_notes - &__start_notes)
172
2c3c8bea
CW
173static ssize_t notes_read(struct file *filp, struct kobject *kobj,
174 struct bin_attribute *bin_attr,
da1a679c
RM
175 char *buf, loff_t off, size_t count)
176{
177 memcpy(buf, &__start_notes + off, count);
178 return count;
179}
180
63d9c273 181static bin_attribute_no_const notes_attr __read_only = {
da1a679c
RM
182 .attr = {
183 .name = "notes",
184 .mode = S_IRUGO,
185 },
186 .read = &notes_read,
187};
188
0ff21e46
GKH
189struct kobject *kernel_kobj;
190EXPORT_SYMBOL_GPL(kernel_kobj);
1da177e4
LT
191
192static struct attribute * kernel_attrs[] = {
088ab0b4 193 &fscaps_attr.attr,
0f76e5ac 194 &uevent_seqnum_attr.attr,
86d56134 195#ifdef CONFIG_UEVENT_HELPER
0f76e5ac 196 &uevent_helper_attr.attr,
86d56134 197#endif
22b8ce94
DH
198#ifdef CONFIG_PROFILING
199 &profiling_attr.attr,
200#endif
c330dda9
JM
201#ifdef CONFIG_KEXEC
202 &kexec_loaded_attr.attr,
203 &kexec_crash_loaded_attr.attr,
06a7f711 204 &kexec_crash_size_attr.attr,
fd59d231 205 &vmcoreinfo_attr.attr,
1da177e4 206#endif
3705b88d 207 &rcu_expedited_attr.attr,
1da177e4
LT
208 NULL
209};
210
211static struct attribute_group kernel_attr_group = {
212 .attrs = kernel_attrs,
213};
214
215static int __init ksysfs_init(void)
216{
bd35b93d 217 int error;
1da177e4 218
0ff21e46
GKH
219 kernel_kobj = kobject_create_and_add("kernel", NULL);
220 if (!kernel_kobj) {
bd35b93d
GKH
221 error = -ENOMEM;
222 goto exit;
223 }
0ff21e46 224 error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
bd35b93d
GKH
225 if (error)
226 goto kset_exit;
227
228 if (notes_size > 0) {
da1a679c 229 notes_attr.size = notes_size;
0ff21e46 230 error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
bd35b93d
GKH
231 if (error)
232 goto group_exit;
da1a679c
RM
233 }
234
bd35b93d
GKH
235 return 0;
236
bd35b93d 237group_exit:
0ff21e46 238 sysfs_remove_group(kernel_kobj, &kernel_attr_group);
bd35b93d 239kset_exit:
78a2d906 240 kobject_put(kernel_kobj);
bd35b93d 241exit:
1da177e4
LT
242 return error;
243}
244
245core_initcall(ksysfs_init);