]> git.ipfire.org Git - people/ms/linux.git/blame - fs/proc/proc_sysctl.c
Merge tag 'soc-fixes-6.0-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[people/ms/linux.git] / fs / proc / proc_sysctl.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
77b14db5
EB
2/*
3 * /proc/sys support
4 */
1e0edd3f 5#include <linux/init.h>
77b14db5 6#include <linux/sysctl.h>
f1ecf068 7#include <linux/poll.h>
77b14db5 8#include <linux/proc_fs.h>
87ebdc00 9#include <linux/printk.h>
77b14db5 10#include <linux/security.h>
40401530 11#include <linux/sched.h>
5b825c3a 12#include <linux/cred.h>
34286d66 13#include <linux/namei.h>
40401530 14#include <linux/mm.h>
4bd6a735 15#include <linux/uio.h>
1f87f0b5 16#include <linux/module.h>
7b146ceb 17#include <linux/bpf-cgroup.h>
3db978d4 18#include <linux/mount.h>
3ddd9a80 19#include <linux/kmemleak.h>
77b14db5
EB
20#include "internal.h"
21
cb55f27a
MT
22#define list_for_each_table_entry(entry, table) \
23 for ((entry) = (table); (entry)->procname; (entry)++)
24
d72f71eb 25static const struct dentry_operations proc_sys_dentry_operations;
77b14db5 26static const struct file_operations proc_sys_file_operations;
03a44825 27static const struct inode_operations proc_sys_inode_operations;
9043476f
AV
28static const struct file_operations proc_sys_dir_file_operations;
29static const struct inode_operations proc_sys_dir_operations;
77b14db5 30
eec4844f 31/* shared constants to be used in various sysctls */
4c7f24f8 32const int sysctl_vals[] = { 0, 1, 2, 3, 4, 100, 200, 1000, 3000, INT_MAX, 65535, -1 };
eec4844f
MC
33EXPORT_SYMBOL(sysctl_vals);
34
b1f2aff8
LC
35const unsigned long sysctl_long_vals[] = { 0, 1, LONG_MAX };
36EXPORT_SYMBOL_GPL(sysctl_long_vals);
37
f9bd6733
EB
38/* Support for permanently empty directories */
39
40struct ctl_table sysctl_mount_point[] = {
41 { }
42};
43
ee9efac4
LC
44/**
45 * register_sysctl_mount_point() - registers a sysctl mount point
46 * @path: path for the mount point
47 *
48 * Used to create a permanently empty directory to serve as mount point.
49 * There are some subtle but important permission checks this allows in the
50 * case of unprivileged mounts.
51 */
52struct ctl_table_header *register_sysctl_mount_point(const char *path)
53{
54 return register_sysctl(path, sysctl_mount_point);
55}
56EXPORT_SYMBOL(register_sysctl_mount_point);
57
f9bd6733
EB
58static bool is_empty_dir(struct ctl_table_header *head)
59{
60 return head->ctl_table[0].child == sysctl_mount_point;
61}
62
63static void set_empty_dir(struct ctl_dir *dir)
64{
65 dir->header.ctl_table[0].child = sysctl_mount_point;
66}
67
68static void clear_empty_dir(struct ctl_dir *dir)
69
70{
71 dir->header.ctl_table[0].child = NULL;
72}
73
f1ecf068
LDM
74void proc_sys_poll_notify(struct ctl_table_poll *poll)
75{
76 if (!poll)
77 return;
78
79 atomic_inc(&poll->event);
80 wake_up_interruptible(&poll->wait);
81}
82
a194558e
EB
83static struct ctl_table root_table[] = {
84 {
85 .procname = "",
7ec66d06 86 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
a194558e
EB
87 },
88 { }
89};
0e47c99d 90static struct ctl_table_root sysctl_table_root = {
0e47c99d 91 .default_set.dir.header = {
7ec66d06
EB
92 {{.count = 1,
93 .nreg = 1,
ac13ac6f 94 .ctl_table = root_table }},
0e47c99d 95 .ctl_table_arg = root_table,
7ec66d06
EB
96 .root = &sysctl_table_root,
97 .set = &sysctl_table_root.default_set,
98 },
1f87f0b5 99};
1f87f0b5
EB
100
101static DEFINE_SPINLOCK(sysctl_lock);
102
7ec66d06 103static void drop_sysctl_table(struct ctl_table_header *header);
0e47c99d 104static int sysctl_follow_link(struct ctl_table_header **phead,
13bcc6a2 105 struct ctl_table **pentry);
0e47c99d
EB
106static int insert_links(struct ctl_table_header *head);
107static void put_links(struct ctl_table_header *header);
7ec66d06 108
6980128f
EB
109static void sysctl_print_dir(struct ctl_dir *dir)
110{
111 if (dir->header.parent)
112 sysctl_print_dir(dir->header.parent);
87ebdc00 113 pr_cont("%s/", dir->header.ctl_table[0].procname);
6980128f
EB
114}
115
076c3eed
EB
116static int namecmp(const char *name1, int len1, const char *name2, int len2)
117{
076c3eed
EB
118 int cmp;
119
d8fc9b66 120 cmp = memcmp(name1, name2, min(len1, len2));
076c3eed
EB
121 if (cmp == 0)
122 cmp = len1 - len2;
123 return cmp;
124}
125
60f126d9 126/* Called under sysctl_lock */
076c3eed 127static struct ctl_table *find_entry(struct ctl_table_header **phead,
0e47c99d 128 struct ctl_dir *dir, const char *name, int namelen)
076c3eed
EB
129{
130 struct ctl_table_header *head;
131 struct ctl_table *entry;
ac13ac6f 132 struct rb_node *node = dir->root.rb_node;
076c3eed 133
ac13ac6f
EB
134 while (node)
135 {
136 struct ctl_node *ctl_node;
137 const char *procname;
138 int cmp;
139
140 ctl_node = rb_entry(node, struct ctl_node, node);
141 head = ctl_node->header;
142 entry = &head->ctl_table[ctl_node - head->node];
143 procname = entry->procname;
144
145 cmp = namecmp(name, namelen, procname, strlen(procname));
146 if (cmp < 0)
147 node = node->rb_left;
148 else if (cmp > 0)
149 node = node->rb_right;
150 else {
151 *phead = head;
152 return entry;
076c3eed
EB
153 }
154 }
155 return NULL;
156}
157
ac13ac6f
EB
158static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
159{
160 struct rb_node *node = &head->node[entry - head->ctl_table].node;
161 struct rb_node **p = &head->parent->root.rb_node;
162 struct rb_node *parent = NULL;
163 const char *name = entry->procname;
164 int namelen = strlen(name);
165
166 while (*p) {
167 struct ctl_table_header *parent_head;
168 struct ctl_table *parent_entry;
169 struct ctl_node *parent_node;
170 const char *parent_name;
171 int cmp;
172
173 parent = *p;
174 parent_node = rb_entry(parent, struct ctl_node, node);
175 parent_head = parent_node->header;
176 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
177 parent_name = parent_entry->procname;
178
179 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
180 if (cmp < 0)
181 p = &(*p)->rb_left;
182 else if (cmp > 0)
183 p = &(*p)->rb_right;
184 else {
87ebdc00 185 pr_err("sysctl duplicate entry: ");
ac13ac6f 186 sysctl_print_dir(head->parent);
153ee1c4 187 pr_cont("%s\n", entry->procname);
ac13ac6f
EB
188 return -EEXIST;
189 }
190 }
191
192 rb_link_node(node, parent, p);
ea5272f5 193 rb_insert_color(node, &head->parent->root);
ac13ac6f
EB
194 return 0;
195}
196
197static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
198{
199 struct rb_node *node = &head->node[entry - head->ctl_table].node;
200
201 rb_erase(node, &head->parent->root);
202}
203
e0d04529
EB
204static void init_header(struct ctl_table_header *head,
205 struct ctl_table_root *root, struct ctl_table_set *set,
ac13ac6f 206 struct ctl_node *node, struct ctl_table *table)
e0d04529 207{
7ec66d06 208 head->ctl_table = table;
e0d04529 209 head->ctl_table_arg = table;
e0d04529
EB
210 head->used = 0;
211 head->count = 1;
212 head->nreg = 1;
213 head->unregistering = NULL;
214 head->root = root;
215 head->set = set;
216 head->parent = NULL;
ac13ac6f 217 head->node = node;
2fd1d2c4 218 INIT_HLIST_HEAD(&head->inodes);
ac13ac6f
EB
219 if (node) {
220 struct ctl_table *entry;
cb55f27a
MT
221
222 list_for_each_table_entry(entry, table) {
ac13ac6f 223 node->header = head;
cb55f27a
MT
224 node++;
225 }
ac13ac6f 226 }
e0d04529
EB
227}
228
8425d6aa
EB
229static void erase_header(struct ctl_table_header *head)
230{
ac13ac6f 231 struct ctl_table *entry;
cb55f27a
MT
232
233 list_for_each_table_entry(entry, head->ctl_table)
ac13ac6f 234 erase_entry(head, entry);
8425d6aa
EB
235}
236
0e47c99d 237static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
8425d6aa 238{
ac13ac6f 239 struct ctl_table *entry;
0e47c99d
EB
240 int err;
241
f9bd6733
EB
242 /* Is this a permanently empty directory? */
243 if (is_empty_dir(&dir->header))
244 return -EROFS;
245
246 /* Am I creating a permanently empty directory? */
247 if (header->ctl_table == sysctl_mount_point) {
248 if (!RB_EMPTY_ROOT(&dir->root))
249 return -EINVAL;
250 set_empty_dir(dir);
251 }
252
0e47c99d 253 dir->header.nreg++;
7ec66d06 254 header->parent = dir;
0e47c99d
EB
255 err = insert_links(header);
256 if (err)
257 goto fail_links;
cb55f27a 258 list_for_each_table_entry(entry, header->ctl_table) {
ac13ac6f
EB
259 err = insert_entry(header, entry);
260 if (err)
261 goto fail;
262 }
0e47c99d 263 return 0;
ac13ac6f
EB
264fail:
265 erase_header(header);
266 put_links(header);
0e47c99d 267fail_links:
f9bd6733
EB
268 if (header->ctl_table == sysctl_mount_point)
269 clear_empty_dir(dir);
0e47c99d
EB
270 header->parent = NULL;
271 drop_sysctl_table(&dir->header);
272 return err;
8425d6aa
EB
273}
274
1f87f0b5
EB
275/* called under sysctl_lock */
276static int use_table(struct ctl_table_header *p)
277{
278 if (unlikely(p->unregistering))
279 return 0;
280 p->used++;
281 return 1;
282}
283
284/* called under sysctl_lock */
285static void unuse_table(struct ctl_table_header *p)
286{
287 if (!--p->used)
288 if (unlikely(p->unregistering))
289 complete(p->unregistering);
290}
291
f90f3caf 292static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
d6cffbbe 293{
f90f3caf 294 proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
d6cffbbe
KK
295}
296
1f87f0b5
EB
297/* called under sysctl_lock, will reacquire if has to wait */
298static void start_unregistering(struct ctl_table_header *p)
299{
300 /*
301 * if p->used is 0, nobody will ever touch that entry again;
302 * we'll eliminate all paths to it before dropping sysctl_lock
303 */
304 if (unlikely(p->used)) {
305 struct completion wait;
306 init_completion(&wait);
307 p->unregistering = &wait;
308 spin_unlock(&sysctl_lock);
309 wait_for_completion(&wait);
1f87f0b5
EB
310 } else {
311 /* anything non-NULL; we'll never dereference it */
312 p->unregistering = ERR_PTR(-EINVAL);
ace0c791 313 spin_unlock(&sysctl_lock);
1f87f0b5 314 }
d6cffbbe 315 /*
f90f3caf 316 * Invalidate dentries for unregistered sysctls: namespaced sysctls
d6cffbbe
KK
317 * can have duplicate names and contaminate dcache very badly.
318 */
f90f3caf 319 proc_sys_invalidate_dcache(p);
1f87f0b5
EB
320 /*
321 * do not remove from the list until nobody holds it; walking the
322 * list in do_sysctl() relies on that.
323 */
ace0c791 324 spin_lock(&sysctl_lock);
8425d6aa 325 erase_header(p);
1f87f0b5
EB
326}
327
1f87f0b5
EB
328static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
329{
ab4a1f24 330 BUG_ON(!head);
1f87f0b5
EB
331 spin_lock(&sysctl_lock);
332 if (!use_table(head))
333 head = ERR_PTR(-ENOENT);
334 spin_unlock(&sysctl_lock);
335 return head;
336}
337
338static void sysctl_head_finish(struct ctl_table_header *head)
339{
340 if (!head)
341 return;
342 spin_lock(&sysctl_lock);
343 unuse_table(head);
344 spin_unlock(&sysctl_lock);
345}
346
347static struct ctl_table_set *
13bcc6a2 348lookup_header_set(struct ctl_table_root *root)
1f87f0b5
EB
349{
350 struct ctl_table_set *set = &root->default_set;
351 if (root->lookup)
13bcc6a2 352 set = root->lookup(root);
1f87f0b5
EB
353 return set;
354}
355
076c3eed 356static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
7ec66d06 357 struct ctl_dir *dir,
076c3eed
EB
358 const char *name, int namelen)
359{
360 struct ctl_table_header *head;
361 struct ctl_table *entry;
076c3eed
EB
362
363 spin_lock(&sysctl_lock);
0e47c99d
EB
364 entry = find_entry(&head, dir, name, namelen);
365 if (entry && use_table(head))
366 *phead = head;
367 else
368 entry = NULL;
076c3eed
EB
369 spin_unlock(&sysctl_lock);
370 return entry;
371}
372
ac13ac6f 373static struct ctl_node *first_usable_entry(struct rb_node *node)
1f87f0b5 374{
ac13ac6f 375 struct ctl_node *ctl_node;
1f87f0b5 376
ac13ac6f
EB
377 for (;node; node = rb_next(node)) {
378 ctl_node = rb_entry(node, struct ctl_node, node);
379 if (use_table(ctl_node->header))
380 return ctl_node;
1f87f0b5 381 }
1f87f0b5
EB
382 return NULL;
383}
384
7ec66d06 385static void first_entry(struct ctl_dir *dir,
6a75ce16
EB
386 struct ctl_table_header **phead, struct ctl_table **pentry)
387{
ac13ac6f 388 struct ctl_table_header *head = NULL;
7ec66d06 389 struct ctl_table *entry = NULL;
ac13ac6f 390 struct ctl_node *ctl_node;
6a75ce16
EB
391
392 spin_lock(&sysctl_lock);
ac13ac6f 393 ctl_node = first_usable_entry(rb_first(&dir->root));
6a75ce16 394 spin_unlock(&sysctl_lock);
ac13ac6f
EB
395 if (ctl_node) {
396 head = ctl_node->header;
397 entry = &head->ctl_table[ctl_node - head->node];
398 }
6a75ce16
EB
399 *phead = head;
400 *pentry = entry;
401}
402
7ec66d06 403static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
1f87f0b5 404{
6a75ce16
EB
405 struct ctl_table_header *head = *phead;
406 struct ctl_table *entry = *pentry;
ac13ac6f 407 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
6a75ce16 408
ac13ac6f
EB
409 spin_lock(&sysctl_lock);
410 unuse_table(head);
411
412 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
413 spin_unlock(&sysctl_lock);
414 head = NULL;
415 if (ctl_node) {
416 head = ctl_node->header;
417 entry = &head->ctl_table[ctl_node - head->node];
6a75ce16
EB
418 }
419 *phead = head;
420 *pentry = entry;
1f87f0b5
EB
421}
422
1f87f0b5
EB
423/*
424 * sysctl_perm does NOT grant the superuser all rights automatically, because
425 * some sysctl variables are readonly even to root.
426 */
427
428static int test_perm(int mode, int op)
429{
091bd3ea 430 if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
1f87f0b5 431 mode >>= 6;
091bd3ea 432 else if (in_egroup_p(GLOBAL_ROOT_GID))
1f87f0b5
EB
433 mode >>= 3;
434 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
435 return 0;
436 return -EACCES;
437}
438
73f7ef43 439static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
1f87f0b5 440{
73f7ef43 441 struct ctl_table_root *root = head->root;
1f87f0b5
EB
442 int mode;
443
444 if (root->permissions)
73f7ef43 445 mode = root->permissions(head, table);
1f87f0b5
EB
446 else
447 mode = table->mode;
448
449 return test_perm(mode, op);
450}
451
9043476f
AV
452static struct inode *proc_sys_make_inode(struct super_block *sb,
453 struct ctl_table_header *head, struct ctl_table *table)
77b14db5 454{
e79c6a4f 455 struct ctl_table_root *root = head->root;
77b14db5 456 struct inode *inode;
9043476f 457 struct proc_inode *ei;
77b14db5 458
9043476f 459 inode = new_inode(sb);
77b14db5 460 if (!inode)
ea5751cc 461 return ERR_PTR(-ENOMEM);
77b14db5 462
85fe4025
CH
463 inode->i_ino = get_next_ino();
464
77b14db5 465 ei = PROC_I(inode);
9043476f 466
d6cffbbe 467 spin_lock(&sysctl_lock);
ace0c791
EB
468 if (unlikely(head->unregistering)) {
469 spin_unlock(&sysctl_lock);
470 iput(inode);
ea5751cc 471 return ERR_PTR(-ENOENT);
ace0c791
EB
472 }
473 ei->sysctl = head;
474 ei->sysctl_entry = table;
0afa5ca8 475 hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
d6cffbbe
KK
476 head->count++;
477 spin_unlock(&sysctl_lock);
478
078cd827 479 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
9043476f 480 inode->i_mode = table->mode;
7ec66d06 481 if (!S_ISDIR(table->mode)) {
9043476f
AV
482 inode->i_mode |= S_IFREG;
483 inode->i_op = &proc_sys_inode_operations;
484 inode->i_fop = &proc_sys_file_operations;
485 } else {
486 inode->i_mode |= S_IFDIR;
9043476f
AV
487 inode->i_op = &proc_sys_dir_operations;
488 inode->i_fop = &proc_sys_dir_file_operations;
f9bd6733
EB
489 if (is_empty_dir(head))
490 make_empty_dir_inode(inode);
9043476f 491 }
e79c6a4f
DT
492
493 if (root->set_ownership)
494 root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
5ec27ec7
RB
495 else {
496 inode->i_uid = GLOBAL_ROOT_UID;
497 inode->i_gid = GLOBAL_ROOT_GID;
498 }
e79c6a4f 499
77b14db5
EB
500 return inode;
501}
502
d6cffbbe
KK
503void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
504{
505 spin_lock(&sysctl_lock);
0afa5ca8 506 hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
d6cffbbe
KK
507 if (!--head->count)
508 kfree_rcu(head, rcu);
509 spin_unlock(&sysctl_lock);
510}
511
81324364 512static struct ctl_table_header *grab_header(struct inode *inode)
77b14db5 513{
3cc3e046
EB
514 struct ctl_table_header *head = PROC_I(inode)->sysctl;
515 if (!head)
0e47c99d 516 head = &sysctl_table_root.default_set.dir.header;
3cc3e046 517 return sysctl_head_grab(head);
9043476f 518}
77b14db5 519
9043476f 520static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 521 unsigned int flags)
9043476f
AV
522{
523 struct ctl_table_header *head = grab_header(dir);
9043476f 524 struct ctl_table_header *h = NULL;
dc12e909 525 const struct qstr *name = &dentry->d_name;
9043476f
AV
526 struct ctl_table *p;
527 struct inode *inode;
528 struct dentry *err = ERR_PTR(-ENOENT);
7ec66d06 529 struct ctl_dir *ctl_dir;
0e47c99d 530 int ret;
77b14db5 531
9043476f
AV
532 if (IS_ERR(head))
533 return ERR_CAST(head);
77b14db5 534
7ec66d06 535 ctl_dir = container_of(head, struct ctl_dir, header);
77b14db5 536
7ec66d06 537 p = lookup_entry(&h, ctl_dir, name->name, name->len);
9043476f 538 if (!p)
77b14db5
EB
539 goto out;
540
4e757320 541 if (S_ISLNK(p->mode)) {
13bcc6a2 542 ret = sysctl_follow_link(&h, &p);
4e757320
EB
543 err = ERR_PTR(ret);
544 if (ret)
545 goto out;
546 }
0e47c99d 547
9043476f 548 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
ea5751cc
ID
549 if (IS_ERR(inode)) {
550 err = ERR_CAST(inode);
77b14db5 551 goto out;
ea5751cc 552 }
77b14db5 553
fb045adb 554 d_set_d_op(dentry, &proc_sys_dentry_operations);
888e2b03 555 err = d_splice_alias(inode, dentry);
77b14db5
EB
556
557out:
6bf61045
FR
558 if (h)
559 sysctl_head_finish(h);
77b14db5
EB
560 sysctl_head_finish(head);
561 return err;
562}
563
4bd6a735
MWO
564static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
565 int write)
77b14db5 566{
4bd6a735 567 struct inode *inode = file_inode(iocb->ki_filp);
9043476f
AV
568 struct ctl_table_header *head = grab_header(inode);
569 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
4bd6a735
MWO
570 size_t count = iov_iter_count(iter);
571 char *kbuf;
2a2da53b 572 ssize_t error;
77b14db5 573
9043476f
AV
574 if (IS_ERR(head))
575 return PTR_ERR(head);
77b14db5
EB
576
577 /*
578 * At this point we know that the sysctl was not unregistered
579 * and won't be until we finish.
580 */
581 error = -EPERM;
73f7ef43 582 if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
77b14db5
EB
583 goto out;
584
9043476f
AV
585 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
586 error = -EINVAL;
587 if (!table->proc_handler)
588 goto out;
589
ef9d965b 590 /* don't even try if the size is too large */
d4d80e69
MWO
591 error = -ENOMEM;
592 if (count >= KMALLOC_MAX_SIZE)
593 goto out;
45089437 594 kbuf = kvzalloc(count + 1, GFP_KERNEL);
4bd6a735
MWO
595 if (!kbuf)
596 goto out;
ef9d965b 597
32927393 598 if (write) {
4bd6a735
MWO
599 error = -EFAULT;
600 if (!copy_from_iter_full(kbuf, count, iter))
601 goto out_free_buf;
602 kbuf[count] = '\0';
32927393
CH
603 }
604
605 error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
4bd6a735 606 &iocb->ki_pos);
7b146ceb 607 if (error)
32927393 608 goto out_free_buf;
7b146ceb 609
77b14db5 610 /* careful: calling conventions are nasty here */
4bd6a735 611 error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
32927393
CH
612 if (error)
613 goto out_free_buf;
614
615 if (!write) {
616 error = -EFAULT;
4bd6a735 617 if (copy_to_iter(kbuf, count, iter) < count)
32927393 618 goto out_free_buf;
4e63acdf
AI
619 }
620
32927393
CH
621 error = count;
622out_free_buf:
45089437 623 kvfree(kbuf);
77b14db5
EB
624out:
625 sysctl_head_finish(head);
626
627 return error;
628}
629
4bd6a735 630static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
77b14db5 631{
4bd6a735 632 return proc_sys_call_handler(iocb, iter, 0);
7708bfb1 633}
77b14db5 634
4bd6a735 635static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
7708bfb1 636{
4bd6a735 637 return proc_sys_call_handler(iocb, iter, 1);
77b14db5
EB
638}
639
f1ecf068
LDM
640static int proc_sys_open(struct inode *inode, struct file *filp)
641{
4e474a00 642 struct ctl_table_header *head = grab_header(inode);
f1ecf068
LDM
643 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
644
4e474a00
LDM
645 /* sysctl was unregistered */
646 if (IS_ERR(head))
647 return PTR_ERR(head);
648
f1ecf068
LDM
649 if (table->poll)
650 filp->private_data = proc_sys_poll_event(table->poll);
651
4e474a00
LDM
652 sysctl_head_finish(head);
653
f1ecf068
LDM
654 return 0;
655}
656
076ccb76 657static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
f1ecf068 658{
496ad9aa 659 struct inode *inode = file_inode(filp);
4e474a00 660 struct ctl_table_header *head = grab_header(inode);
f1ecf068 661 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
076ccb76 662 __poll_t ret = DEFAULT_POLLMASK;
4e474a00
LDM
663 unsigned long event;
664
665 /* sysctl was unregistered */
666 if (IS_ERR(head))
a9a08845 667 return EPOLLERR | EPOLLHUP;
f1ecf068
LDM
668
669 if (!table->proc_handler)
670 goto out;
671
672 if (!table->poll)
673 goto out;
674
4e474a00 675 event = (unsigned long)filp->private_data;
f1ecf068
LDM
676 poll_wait(filp, &table->poll->wait, wait);
677
678 if (event != atomic_read(&table->poll->event)) {
679 filp->private_data = proc_sys_poll_event(table->poll);
a9a08845 680 ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
f1ecf068
LDM
681 }
682
683out:
4e474a00
LDM
684 sysctl_head_finish(head);
685
f1ecf068
LDM
686 return ret;
687}
77b14db5 688
f0c3b509
AV
689static bool proc_sys_fill_cache(struct file *file,
690 struct dir_context *ctx,
9043476f
AV
691 struct ctl_table_header *head,
692 struct ctl_table *table)
77b14db5 693{
f0c3b509 694 struct dentry *child, *dir = file->f_path.dentry;
77b14db5
EB
695 struct inode *inode;
696 struct qstr qname;
697 ino_t ino = 0;
698 unsigned type = DT_UNKNOWN;
77b14db5
EB
699
700 qname.name = table->procname;
701 qname.len = strlen(table->procname);
8387ff25 702 qname.hash = full_name_hash(dir, qname.name, qname.len);
77b14db5 703
77b14db5
EB
704 child = d_lookup(dir, &qname);
705 if (!child) {
76aab3ab
AV
706 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
707 child = d_alloc_parallel(dir, &qname, &wq);
708 if (IS_ERR(child))
709 return false;
710 if (d_in_lookup(child)) {
888e2b03 711 struct dentry *res;
9043476f 712 inode = proc_sys_make_inode(dir->d_sb, head, table);
ea5751cc 713 if (IS_ERR(inode)) {
76aab3ab 714 d_lookup_done(child);
9043476f 715 dput(child);
f0c3b509 716 return false;
77b14db5 717 }
76aab3ab 718 d_set_d_op(child, &proc_sys_dentry_operations);
888e2b03
AV
719 res = d_splice_alias(inode, child);
720 d_lookup_done(child);
721 if (unlikely(res)) {
722 if (IS_ERR(res)) {
723 dput(child);
724 return false;
725 }
726 dput(child);
727 child = res;
728 }
77b14db5
EB
729 }
730 }
2b0143b5 731 inode = d_inode(child);
9043476f
AV
732 ino = inode->i_ino;
733 type = inode->i_mode >> 12;
77b14db5 734 dput(child);
f0c3b509 735 return dir_emit(ctx, qname.name, qname.len, ino, type);
9043476f
AV
736}
737
f0c3b509
AV
738static bool proc_sys_link_fill_cache(struct file *file,
739 struct dir_context *ctx,
0e47c99d
EB
740 struct ctl_table_header *head,
741 struct ctl_table *table)
742{
f0c3b509 743 bool ret = true;
a0b0d1c3 744
0e47c99d 745 head = sysctl_head_grab(head);
a0b0d1c3
DK
746 if (IS_ERR(head))
747 return false;
0e47c99d 748
835b94e0
DK
749 /* It is not an error if we can not follow the link ignore it */
750 if (sysctl_follow_link(&head, &table))
751 goto out;
0e47c99d 752
f0c3b509 753 ret = proc_sys_fill_cache(file, ctx, head, table);
0e47c99d
EB
754out:
755 sysctl_head_finish(head);
756 return ret;
757}
758
e5eea098 759static int scan(struct ctl_table_header *head, struct ctl_table *table,
9043476f 760 unsigned long *pos, struct file *file,
f0c3b509 761 struct dir_context *ctx)
9043476f 762{
f0c3b509 763 bool res;
9043476f 764
f0c3b509
AV
765 if ((*pos)++ < ctx->pos)
766 return true;
9043476f 767
0e47c99d 768 if (unlikely(S_ISLNK(table->mode)))
f0c3b509 769 res = proc_sys_link_fill_cache(file, ctx, head, table);
0e47c99d 770 else
f0c3b509 771 res = proc_sys_fill_cache(file, ctx, head, table);
9043476f 772
f0c3b509
AV
773 if (res)
774 ctx->pos = *pos;
9043476f 775
6a75ce16 776 return res;
77b14db5
EB
777}
778
f0c3b509 779static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
77b14db5 780{
f0c3b509 781 struct ctl_table_header *head = grab_header(file_inode(file));
9043476f 782 struct ctl_table_header *h = NULL;
6a75ce16 783 struct ctl_table *entry;
7ec66d06 784 struct ctl_dir *ctl_dir;
77b14db5 785 unsigned long pos;
9043476f
AV
786
787 if (IS_ERR(head))
788 return PTR_ERR(head);
77b14db5 789
7ec66d06 790 ctl_dir = container_of(head, struct ctl_dir, header);
77b14db5 791
f0c3b509 792 if (!dir_emit_dots(file, ctx))
93362fa4 793 goto out;
f0c3b509 794
77b14db5
EB
795 pos = 2;
796
7ec66d06 797 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
f0c3b509 798 if (!scan(h, entry, &pos, file, ctx)) {
9043476f
AV
799 sysctl_head_finish(h);
800 break;
77b14db5
EB
801 }
802 }
93362fa4 803out:
77b14db5 804 sysctl_head_finish(head);
f0c3b509 805 return 0;
77b14db5
EB
806}
807
549c7297
CB
808static int proc_sys_permission(struct user_namespace *mnt_userns,
809 struct inode *inode, int mask)
77b14db5
EB
810{
811 /*
812 * sysctl entries that are not writeable,
813 * are _NOT_ writeable, capabilities or not.
814 */
f696a365
MS
815 struct ctl_table_header *head;
816 struct ctl_table *table;
77b14db5
EB
817 int error;
818
f696a365
MS
819 /* Executable files are not allowed under /proc/sys/ */
820 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
821 return -EACCES;
822
823 head = grab_header(inode);
9043476f
AV
824 if (IS_ERR(head))
825 return PTR_ERR(head);
77b14db5 826
f696a365 827 table = PROC_I(inode)->sysctl_entry;
9043476f
AV
828 if (!table) /* global root - r-xr-xr-x */
829 error = mask & MAY_WRITE ? -EACCES : 0;
830 else /* Use the permissions on the sysctl table entry */
73f7ef43 831 error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
77b14db5 832
77b14db5
EB
833 sysctl_head_finish(head);
834 return error;
835}
836
549c7297
CB
837static int proc_sys_setattr(struct user_namespace *mnt_userns,
838 struct dentry *dentry, struct iattr *attr)
77b14db5 839{
2b0143b5 840 struct inode *inode = d_inode(dentry);
77b14db5
EB
841 int error;
842
843 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
844 return -EPERM;
845
2f221d6f 846 error = setattr_prepare(&init_user_ns, dentry, attr);
1025774c
CH
847 if (error)
848 return error;
849
2f221d6f 850 setattr_copy(&init_user_ns, inode, attr);
1025774c
CH
851 mark_inode_dirty(inode);
852 return 0;
77b14db5
EB
853}
854
549c7297
CB
855static int proc_sys_getattr(struct user_namespace *mnt_userns,
856 const struct path *path, struct kstat *stat,
a528d35e 857 u32 request_mask, unsigned int query_flags)
9043476f 858{
a528d35e 859 struct inode *inode = d_inode(path->dentry);
9043476f
AV
860 struct ctl_table_header *head = grab_header(inode);
861 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
862
863 if (IS_ERR(head))
864 return PTR_ERR(head);
865
0d56a451 866 generic_fillattr(&init_user_ns, inode, stat);
9043476f
AV
867 if (table)
868 stat->mode = (stat->mode & S_IFMT) | table->mode;
869
870 sysctl_head_finish(head);
871 return 0;
872}
873
77b14db5 874static const struct file_operations proc_sys_file_operations = {
f1ecf068
LDM
875 .open = proc_sys_open,
876 .poll = proc_sys_poll,
4bd6a735
MWO
877 .read_iter = proc_sys_read,
878 .write_iter = proc_sys_write,
879 .splice_read = generic_file_splice_read,
880 .splice_write = iter_file_splice_write,
6038f373 881 .llseek = default_llseek,
9043476f
AV
882};
883
884static const struct file_operations proc_sys_dir_file_operations = {
887df078 885 .read = generic_read_dir,
f50752ea 886 .iterate_shared = proc_sys_readdir,
3222a3e5 887 .llseek = generic_file_llseek,
77b14db5
EB
888};
889
03a44825 890static const struct inode_operations proc_sys_inode_operations = {
9043476f
AV
891 .permission = proc_sys_permission,
892 .setattr = proc_sys_setattr,
893 .getattr = proc_sys_getattr,
894};
895
896static const struct inode_operations proc_sys_dir_operations = {
77b14db5
EB
897 .lookup = proc_sys_lookup,
898 .permission = proc_sys_permission,
899 .setattr = proc_sys_setattr,
9043476f 900 .getattr = proc_sys_getattr,
77b14db5
EB
901};
902
0b728e19 903static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
77b14db5 904{
0b728e19 905 if (flags & LOOKUP_RCU)
34286d66 906 return -ECHILD;
2b0143b5 907 return !PROC_I(d_inode(dentry))->sysctl->unregistering;
9043476f
AV
908}
909
fe15ce44 910static int proc_sys_delete(const struct dentry *dentry)
9043476f 911{
2b0143b5 912 return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
9043476f
AV
913}
914
1f87f0b5
EB
915static int sysctl_is_seen(struct ctl_table_header *p)
916{
917 struct ctl_table_set *set = p->set;
918 int res;
919 spin_lock(&sysctl_lock);
920 if (p->unregistering)
921 res = 0;
922 else if (!set->is_seen)
923 res = 1;
924 else
925 res = set->is_seen(set);
926 spin_unlock(&sysctl_lock);
927 return res;
928}
929
6fa67e70 930static int proc_sys_compare(const struct dentry *dentry,
621e155a 931 unsigned int len, const char *str, const struct qstr *name)
9043476f 932{
dfef6dcd 933 struct ctl_table_header *head;
da53be12
LT
934 struct inode *inode;
935
31e6b01f
NP
936 /* Although proc doesn't have negative dentries, rcu-walk means
937 * that inode here can be NULL */
dfef6dcd 938 /* AV: can it, indeed? */
2b0143b5 939 inode = d_inode_rcu(dentry);
31e6b01f 940 if (!inode)
dfef6dcd 941 return 1;
621e155a 942 if (name->len != len)
9043476f 943 return 1;
621e155a 944 if (memcmp(name->name, str, len))
9043476f 945 return 1;
dfef6dcd
AV
946 head = rcu_dereference(PROC_I(inode)->sysctl);
947 return !head || !sysctl_is_seen(head);
77b14db5
EB
948}
949
d72f71eb 950static const struct dentry_operations proc_sys_dentry_operations = {
77b14db5 951 .d_revalidate = proc_sys_revalidate,
9043476f
AV
952 .d_delete = proc_sys_delete,
953 .d_compare = proc_sys_compare,
77b14db5
EB
954};
955
0e47c99d
EB
956static struct ctl_dir *find_subdir(struct ctl_dir *dir,
957 const char *name, int namelen)
1f87f0b5 958{
7ec66d06
EB
959 struct ctl_table_header *head;
960 struct ctl_table *entry;
1f87f0b5 961
0e47c99d 962 entry = find_entry(&head, dir, name, namelen);
7ec66d06
EB
963 if (!entry)
964 return ERR_PTR(-ENOENT);
51f72f4a
EB
965 if (!S_ISDIR(entry->mode))
966 return ERR_PTR(-ENOTDIR);
967 return container_of(head, struct ctl_dir, header);
7ec66d06
EB
968}
969
970static struct ctl_dir *new_dir(struct ctl_table_set *set,
0e47c99d 971 const char *name, int namelen)
7ec66d06
EB
972{
973 struct ctl_table *table;
974 struct ctl_dir *new;
ac13ac6f 975 struct ctl_node *node;
7ec66d06 976 char *new_name;
1f87f0b5 977
ac13ac6f
EB
978 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
979 sizeof(struct ctl_table)*2 + namelen + 1,
980 GFP_KERNEL);
7ec66d06 981 if (!new)
1f87f0b5
EB
982 return NULL;
983
ac13ac6f
EB
984 node = (struct ctl_node *)(new + 1);
985 table = (struct ctl_table *)(node + 1);
7ec66d06
EB
986 new_name = (char *)(table + 2);
987 memcpy(new_name, name, namelen);
7ec66d06
EB
988 table[0].procname = new_name;
989 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
ac13ac6f 990 init_header(&new->header, set->dir.header.root, set, node, table);
7ec66d06
EB
991
992 return new;
1f87f0b5
EB
993}
994
60f126d9
EB
995/**
996 * get_subdir - find or create a subdir with the specified name.
997 * @dir: Directory to create the subdirectory in
998 * @name: The name of the subdirectory to find or create
999 * @namelen: The length of name
1000 *
1001 * Takes a directory with an elevated reference count so we know that
1002 * if we drop the lock the directory will not go away. Upon success
1003 * the reference is moved from @dir to the returned subdirectory.
1004 * Upon error an error code is returned and the reference on @dir is
1005 * simply dropped.
1006 */
0e47c99d
EB
1007static struct ctl_dir *get_subdir(struct ctl_dir *dir,
1008 const char *name, int namelen)
1f87f0b5 1009{
0e47c99d 1010 struct ctl_table_set *set = dir->header.set;
7ec66d06 1011 struct ctl_dir *subdir, *new = NULL;
0eb97f38 1012 int err;
1f87f0b5 1013
7ec66d06 1014 spin_lock(&sysctl_lock);
0e47c99d 1015 subdir = find_subdir(dir, name, namelen);
7ec66d06
EB
1016 if (!IS_ERR(subdir))
1017 goto found;
1018 if (PTR_ERR(subdir) != -ENOENT)
1019 goto failed;
1020
1021 spin_unlock(&sysctl_lock);
1022 new = new_dir(set, name, namelen);
1023 spin_lock(&sysctl_lock);
1024 subdir = ERR_PTR(-ENOMEM);
1025 if (!new)
1026 goto failed;
1027
60f126d9 1028 /* Was the subdir added while we dropped the lock? */
0e47c99d 1029 subdir = find_subdir(dir, name, namelen);
7ec66d06
EB
1030 if (!IS_ERR(subdir))
1031 goto found;
1032 if (PTR_ERR(subdir) != -ENOENT)
1033 goto failed;
1034
60f126d9 1035 /* Nope. Use the our freshly made directory entry. */
0eb97f38
EB
1036 err = insert_header(dir, &new->header);
1037 subdir = ERR_PTR(err);
1038 if (err)
0e47c99d 1039 goto failed;
7ec66d06
EB
1040 subdir = new;
1041found:
1042 subdir->header.nreg++;
1043failed:
a1c83681 1044 if (IS_ERR(subdir)) {
87ebdc00 1045 pr_err("sysctl could not get directory: ");
6980128f 1046 sysctl_print_dir(dir);
153ee1c4
GU
1047 pr_cont("%*.*s %ld\n", namelen, namelen, name,
1048 PTR_ERR(subdir));
1f87f0b5 1049 }
7ec66d06
EB
1050 drop_sysctl_table(&dir->header);
1051 if (new)
1052 drop_sysctl_table(&new->header);
1053 spin_unlock(&sysctl_lock);
1054 return subdir;
1f87f0b5
EB
1055}
1056
0e47c99d
EB
1057static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
1058{
1059 struct ctl_dir *parent;
1060 const char *procname;
1061 if (!dir->header.parent)
1062 return &set->dir;
1063 parent = xlate_dir(set, dir->header.parent);
1064 if (IS_ERR(parent))
1065 return parent;
1066 procname = dir->header.ctl_table[0].procname;
1067 return find_subdir(parent, procname, strlen(procname));
1068}
1069
1070static int sysctl_follow_link(struct ctl_table_header **phead,
13bcc6a2 1071 struct ctl_table **pentry)
0e47c99d
EB
1072{
1073 struct ctl_table_header *head;
1074 struct ctl_table_root *root;
1075 struct ctl_table_set *set;
1076 struct ctl_table *entry;
1077 struct ctl_dir *dir;
1078 int ret;
1079
0e47c99d
EB
1080 spin_lock(&sysctl_lock);
1081 root = (*pentry)->data;
13bcc6a2 1082 set = lookup_header_set(root);
0e47c99d
EB
1083 dir = xlate_dir(set, (*phead)->parent);
1084 if (IS_ERR(dir))
1085 ret = PTR_ERR(dir);
1086 else {
1087 const char *procname = (*pentry)->procname;
1088 head = NULL;
1089 entry = find_entry(&head, dir, procname, strlen(procname));
1090 ret = -ENOENT;
1091 if (entry && use_table(head)) {
1092 unuse_table(*phead);
1093 *phead = head;
1094 *pentry = entry;
1095 ret = 0;
1096 }
1097 }
1098
1099 spin_unlock(&sysctl_lock);
1100 return ret;
1101}
1102
7c60c48f 1103static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1f87f0b5 1104{
7c60c48f
EB
1105 struct va_format vaf;
1106 va_list args;
1f87f0b5 1107
7c60c48f
EB
1108 va_start(args, fmt);
1109 vaf.fmt = fmt;
1110 vaf.va = &args;
1111
87ebdc00
AM
1112 pr_err("sysctl table check failed: %s/%s %pV\n",
1113 path, table->procname, &vaf);
1f87f0b5 1114
7c60c48f
EB
1115 va_end(args);
1116 return -EINVAL;
1f87f0b5
EB
1117}
1118
4f2fec00
LR
1119static int sysctl_check_table_array(const char *path, struct ctl_table *table)
1120{
1121 int err = 0;
1122
61d9b56a
LR
1123 if ((table->proc_handler == proc_douintvec) ||
1124 (table->proc_handler == proc_douintvec_minmax)) {
4f2fec00 1125 if (table->maxlen != sizeof(unsigned int))
64a11f3d 1126 err |= sysctl_err(path, table, "array not allowed");
4f2fec00
LR
1127 }
1128
cb944413
ED
1129 if (table->proc_handler == proc_dou8vec_minmax) {
1130 if (table->maxlen != sizeof(u8))
1131 err |= sysctl_err(path, table, "array not allowed");
1132 }
1133
4f2fec00
LR
1134 return err;
1135}
1136
7c60c48f 1137static int sysctl_check_table(const char *path, struct ctl_table *table)
1f87f0b5 1138{
cb55f27a 1139 struct ctl_table *entry;
7c60c48f 1140 int err = 0;
cb55f27a
MT
1141 list_for_each_table_entry(entry, table) {
1142 if (entry->child)
1143 err |= sysctl_err(path, entry, "Not a file");
1144
1145 if ((entry->proc_handler == proc_dostring) ||
1146 (entry->proc_handler == proc_dointvec) ||
1147 (entry->proc_handler == proc_douintvec) ||
1148 (entry->proc_handler == proc_douintvec_minmax) ||
1149 (entry->proc_handler == proc_dointvec_minmax) ||
1150 (entry->proc_handler == proc_dou8vec_minmax) ||
1151 (entry->proc_handler == proc_dointvec_jiffies) ||
1152 (entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1153 (entry->proc_handler == proc_dointvec_ms_jiffies) ||
1154 (entry->proc_handler == proc_doulongvec_minmax) ||
1155 (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1156 if (!entry->data)
1157 err |= sysctl_err(path, entry, "No data");
1158 if (!entry->maxlen)
1159 err |= sysctl_err(path, entry, "No maxlen");
4f2fec00 1160 else
cb55f27a 1161 err |= sysctl_check_table_array(path, entry);
7c60c48f 1162 }
cb55f27a
MT
1163 if (!entry->proc_handler)
1164 err |= sysctl_err(path, entry, "No proc_handler");
7c60c48f 1165
cb55f27a
MT
1166 if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1167 err |= sysctl_err(path, entry, "bogus .mode 0%o",
1168 entry->mode);
1f87f0b5 1169 }
7c60c48f 1170 return err;
1f87f0b5 1171}
1f87f0b5 1172
0e47c99d
EB
1173static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1174 struct ctl_table_root *link_root)
1175{
1176 struct ctl_table *link_table, *entry, *link;
1177 struct ctl_table_header *links;
ac13ac6f 1178 struct ctl_node *node;
0e47c99d
EB
1179 char *link_name;
1180 int nr_entries, name_bytes;
1181
1182 name_bytes = 0;
1183 nr_entries = 0;
cb55f27a 1184 list_for_each_table_entry(entry, table) {
0e47c99d
EB
1185 nr_entries++;
1186 name_bytes += strlen(entry->procname) + 1;
1187 }
1188
1189 links = kzalloc(sizeof(struct ctl_table_header) +
ac13ac6f 1190 sizeof(struct ctl_node)*nr_entries +
0e47c99d
EB
1191 sizeof(struct ctl_table)*(nr_entries + 1) +
1192 name_bytes,
1193 GFP_KERNEL);
1194
1195 if (!links)
1196 return NULL;
1197
ac13ac6f
EB
1198 node = (struct ctl_node *)(links + 1);
1199 link_table = (struct ctl_table *)(node + nr_entries);
0e47c99d 1200 link_name = (char *)&link_table[nr_entries + 1];
cb55f27a 1201 link = link_table;
0e47c99d 1202
cb55f27a 1203 list_for_each_table_entry(entry, table) {
0e47c99d
EB
1204 int len = strlen(entry->procname) + 1;
1205 memcpy(link_name, entry->procname, len);
1206 link->procname = link_name;
1207 link->mode = S_IFLNK|S_IRWXUGO;
1208 link->data = link_root;
1209 link_name += len;
cb55f27a 1210 link++;
0e47c99d 1211 }
ac13ac6f 1212 init_header(links, dir->header.root, dir->header.set, node, link_table);
0e47c99d
EB
1213 links->nreg = nr_entries;
1214
1215 return links;
1216}
1217
1218static bool get_links(struct ctl_dir *dir,
1219 struct ctl_table *table, struct ctl_table_root *link_root)
1220{
1221 struct ctl_table_header *head;
1222 struct ctl_table *entry, *link;
1223
1224 /* Are there links available for every entry in table? */
cb55f27a 1225 list_for_each_table_entry(entry, table) {
0e47c99d
EB
1226 const char *procname = entry->procname;
1227 link = find_entry(&head, dir, procname, strlen(procname));
1228 if (!link)
1229 return false;
1230 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1231 continue;
1232 if (S_ISLNK(link->mode) && (link->data == link_root))
1233 continue;
1234 return false;
1235 }
1236
1237 /* The checks passed. Increase the registration count on the links */
cb55f27a 1238 list_for_each_table_entry(entry, table) {
0e47c99d
EB
1239 const char *procname = entry->procname;
1240 link = find_entry(&head, dir, procname, strlen(procname));
1241 head->nreg++;
1242 }
1243 return true;
1244}
1245
1246static int insert_links(struct ctl_table_header *head)
1247{
1248 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1249 struct ctl_dir *core_parent = NULL;
1250 struct ctl_table_header *links;
1251 int err;
1252
1253 if (head->set == root_set)
1254 return 0;
1255
1256 core_parent = xlate_dir(root_set, head->parent);
1257 if (IS_ERR(core_parent))
1258 return 0;
1259
1260 if (get_links(core_parent, head->ctl_table, head->root))
1261 return 0;
1262
1263 core_parent->header.nreg++;
1264 spin_unlock(&sysctl_lock);
1265
1266 links = new_links(core_parent, head->ctl_table, head->root);
1267
1268 spin_lock(&sysctl_lock);
1269 err = -ENOMEM;
1270 if (!links)
1271 goto out;
1272
1273 err = 0;
1274 if (get_links(core_parent, head->ctl_table, head->root)) {
1275 kfree(links);
1276 goto out;
1277 }
1278
1279 err = insert_header(core_parent, links);
1280 if (err)
1281 kfree(links);
1282out:
1283 drop_sysctl_table(&core_parent->header);
1284 return err;
1285}
1286
1f87f0b5 1287/**
f728019b 1288 * __register_sysctl_table - register a leaf sysctl table
60a47a2e 1289 * @set: Sysctl tree to register on
1f87f0b5
EB
1290 * @path: The path to the directory the sysctl table is in.
1291 * @table: the top-level table structure
1292 *
1293 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1294 * array. A completely 0 filled entry terminates the table.
1295 *
1296 * The members of the &struct ctl_table structure are used as follows:
1297 *
1298 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1299 * enter a sysctl file
1300 *
1301 * data - a pointer to data for use by proc_handler
1302 *
1303 * maxlen - the maximum size in bytes of the data
1304 *
f728019b 1305 * mode - the file permissions for the /proc/sys file
1f87f0b5 1306 *
f728019b 1307 * child - must be %NULL.
1f87f0b5
EB
1308 *
1309 * proc_handler - the text handler routine (described below)
1310 *
1f87f0b5
EB
1311 * extra1, extra2 - extra pointers usable by the proc handler routines
1312 *
1313 * Leaf nodes in the sysctl tree will be represented by a single file
1314 * under /proc; non-leaf nodes will be represented by directories.
1315 *
f728019b
EB
1316 * There must be a proc_handler routine for any terminal nodes.
1317 * Several default handlers are available to cover common cases -
1f87f0b5
EB
1318 *
1319 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1320 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1321 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1322 *
1323 * It is the handler's job to read the input buffer from user memory
1324 * and process it. The handler should return 0 on success.
1325 *
1326 * This routine returns %NULL on a failure to register, and a pointer
1327 * to the table header on success.
1328 */
6e9d5164 1329struct ctl_table_header *__register_sysctl_table(
60a47a2e 1330 struct ctl_table_set *set,
6e9d5164 1331 const char *path, struct ctl_table *table)
1f87f0b5 1332{
60a47a2e 1333 struct ctl_table_root *root = set->dir.header.root;
1f87f0b5 1334 struct ctl_table_header *header;
6e9d5164 1335 const char *name, *nextname;
7ec66d06 1336 struct ctl_dir *dir;
ac13ac6f
EB
1337 struct ctl_table *entry;
1338 struct ctl_node *node;
1339 int nr_entries = 0;
1340
cb55f27a 1341 list_for_each_table_entry(entry, table)
ac13ac6f 1342 nr_entries++;
1f87f0b5 1343
ac13ac6f 1344 header = kzalloc(sizeof(struct ctl_table_header) +
425b9c7f 1345 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL_ACCOUNT);
1f87f0b5
EB
1346 if (!header)
1347 return NULL;
1348
ac13ac6f
EB
1349 node = (struct ctl_node *)(header + 1);
1350 init_header(header, root, set, node, table);
7ec66d06
EB
1351 if (sysctl_check_table(path, table))
1352 goto fail;
1353
1354 spin_lock(&sysctl_lock);
0e47c99d 1355 dir = &set->dir;
60f126d9 1356 /* Reference moved down the diretory tree get_subdir */
7ec66d06
EB
1357 dir->header.nreg++;
1358 spin_unlock(&sysctl_lock);
1f87f0b5 1359
7ec66d06 1360 /* Find the directory for the ctl_table */
6e9d5164
EB
1361 for (name = path; name; name = nextname) {
1362 int namelen;
1363 nextname = strchr(name, '/');
1364 if (nextname) {
1365 namelen = nextname - name;
1366 nextname++;
1367 } else {
1368 namelen = strlen(name);
1369 }
1370 if (namelen == 0)
1371 continue;
1f87f0b5 1372
0e47c99d 1373 dir = get_subdir(dir, name, namelen);
7ec66d06
EB
1374 if (IS_ERR(dir))
1375 goto fail;
1f87f0b5 1376 }
0e47c99d 1377
1f87f0b5 1378 spin_lock(&sysctl_lock);
0e47c99d 1379 if (insert_header(dir, header))
7ec66d06 1380 goto fail_put_dir_locked;
0e47c99d 1381
7ec66d06 1382 drop_sysctl_table(&dir->header);
1f87f0b5
EB
1383 spin_unlock(&sysctl_lock);
1384
1385 return header;
0e47c99d 1386
7ec66d06
EB
1387fail_put_dir_locked:
1388 drop_sysctl_table(&dir->header);
7c60c48f
EB
1389 spin_unlock(&sysctl_lock);
1390fail:
1391 kfree(header);
1392 dump_stack();
1393 return NULL;
1f87f0b5
EB
1394}
1395
fea478d4
EB
1396/**
1397 * register_sysctl - register a sysctl table
1398 * @path: The path to the directory the sysctl table is in.
1399 * @table: the table structure
1400 *
1401 * Register a sysctl table. @table should be a filled in ctl_table
1402 * array. A completely 0 filled entry terminates the table.
1403 *
1404 * See __register_sysctl_table for more details.
1405 */
1406struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1407{
1408 return __register_sysctl_table(&sysctl_table_root.default_set,
1409 path, table);
1410}
1411EXPORT_SYMBOL(register_sysctl);
1412
3ddd9a80
XN
1413/**
1414 * __register_sysctl_init() - register sysctl table to path
1415 * @path: path name for sysctl base
1416 * @table: This is the sysctl table that needs to be registered to the path
1417 * @table_name: The name of sysctl table, only used for log printing when
1418 * registration fails
1419 *
1420 * The sysctl interface is used by userspace to query or modify at runtime
1421 * a predefined value set on a variable. These variables however have default
1422 * values pre-set. Code which depends on these variables will always work even
1423 * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
1424 * ability to query or modify the sysctls dynamically at run time. Chances of
1425 * register_sysctl() failing on init are extremely low, and so for both reasons
1426 * this function does not return any error as it is used by initialization code.
1427 *
1428 * Context: Can only be called after your respective sysctl base path has been
1429 * registered. So for instance, most base directories are registered early on
1430 * init before init levels are processed through proc_sys_init() and
d8c0418a 1431 * sysctl_init_bases().
3ddd9a80
XN
1432 */
1433void __init __register_sysctl_init(const char *path, struct ctl_table *table,
1434 const char *table_name)
1435{
1436 struct ctl_table_header *hdr = register_sysctl(path, table);
1437
1438 if (unlikely(!hdr)) {
1439 pr_err("failed when register_sysctl %s to %s\n", table_name, path);
1440 return;
1441 }
1442 kmemleak_not_leak(hdr);
1443}
1444
6e9d5164
EB
1445static char *append_path(const char *path, char *pos, const char *name)
1446{
1447 int namelen;
1448 namelen = strlen(name);
1449 if (((pos - path) + namelen + 2) >= PATH_MAX)
1450 return NULL;
1451 memcpy(pos, name, namelen);
1452 pos[namelen] = '/';
1453 pos[namelen + 1] = '\0';
1454 pos += namelen + 1;
1455 return pos;
1456}
1457
f728019b
EB
1458static int count_subheaders(struct ctl_table *table)
1459{
1460 int has_files = 0;
1461 int nr_subheaders = 0;
1462 struct ctl_table *entry;
1463
1464 /* special case: no directory and empty directory */
1465 if (!table || !table->procname)
1466 return 1;
1467
cb55f27a 1468 list_for_each_table_entry(entry, table) {
f728019b
EB
1469 if (entry->child)
1470 nr_subheaders += count_subheaders(entry->child);
1471 else
1472 has_files = 1;
1473 }
1474 return nr_subheaders + has_files;
1475}
1476
1477static int register_leaf_sysctl_tables(const char *path, char *pos,
60a47a2e 1478 struct ctl_table_header ***subheader, struct ctl_table_set *set,
f728019b
EB
1479 struct ctl_table *table)
1480{
1481 struct ctl_table *ctl_table_arg = NULL;
1482 struct ctl_table *entry, *files;
1483 int nr_files = 0;
1484 int nr_dirs = 0;
1485 int err = -ENOMEM;
1486
cb55f27a 1487 list_for_each_table_entry(entry, table) {
f728019b
EB
1488 if (entry->child)
1489 nr_dirs++;
1490 else
1491 nr_files++;
1492 }
1493
1494 files = table;
1495 /* If there are mixed files and directories we need a new table */
1496 if (nr_dirs && nr_files) {
1497 struct ctl_table *new;
6396bb22 1498 files = kcalloc(nr_files + 1, sizeof(struct ctl_table),
f728019b
EB
1499 GFP_KERNEL);
1500 if (!files)
1501 goto out;
1502
1503 ctl_table_arg = files;
cb55f27a
MT
1504 new = files;
1505
1506 list_for_each_table_entry(entry, table) {
f728019b
EB
1507 if (entry->child)
1508 continue;
1509 *new = *entry;
1510 new++;
1511 }
1512 }
1513
1514 /* Register everything except a directory full of subdirectories */
1515 if (nr_files || !nr_dirs) {
1516 struct ctl_table_header *header;
60a47a2e 1517 header = __register_sysctl_table(set, path, files);
f728019b
EB
1518 if (!header) {
1519 kfree(ctl_table_arg);
1520 goto out;
1521 }
1522
1523 /* Remember if we need to free the file table */
1524 header->ctl_table_arg = ctl_table_arg;
1525 **subheader = header;
1526 (*subheader)++;
1527 }
1528
1529 /* Recurse into the subdirectories. */
cb55f27a 1530 list_for_each_table_entry(entry, table) {
f728019b
EB
1531 char *child_pos;
1532
1533 if (!entry->child)
1534 continue;
1535
1536 err = -ENAMETOOLONG;
1537 child_pos = append_path(path, pos, entry->procname);
1538 if (!child_pos)
1539 goto out;
1540
1541 err = register_leaf_sysctl_tables(path, child_pos, subheader,
60a47a2e 1542 set, entry->child);
f728019b
EB
1543 pos[0] = '\0';
1544 if (err)
1545 goto out;
1546 }
1547 err = 0;
1548out:
1549 /* On failure our caller will unregister all registered subheaders */
1550 return err;
1551}
1552
6e9d5164
EB
1553/**
1554 * __register_sysctl_paths - register a sysctl table hierarchy
60a47a2e 1555 * @set: Sysctl tree to register on
6e9d5164
EB
1556 * @path: The path to the directory the sysctl table is in.
1557 * @table: the top-level table structure
1558 *
1559 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1560 * array. A completely 0 filled entry terminates the table.
1561 *
1562 * See __register_sysctl_table for more details.
1563 */
1564struct ctl_table_header *__register_sysctl_paths(
60a47a2e 1565 struct ctl_table_set *set,
6e9d5164
EB
1566 const struct ctl_path *path, struct ctl_table *table)
1567{
ec6a5266 1568 struct ctl_table *ctl_table_arg = table;
f728019b
EB
1569 int nr_subheaders = count_subheaders(table);
1570 struct ctl_table_header *header = NULL, **subheaders, **subheader;
6e9d5164
EB
1571 const struct ctl_path *component;
1572 char *new_path, *pos;
1573
1574 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1575 if (!new_path)
1576 return NULL;
1577
1578 pos[0] = '\0';
1579 for (component = path; component->procname; component++) {
1580 pos = append_path(new_path, pos, component->procname);
1581 if (!pos)
1582 goto out;
1583 }
ec6a5266
EB
1584 while (table->procname && table->child && !table[1].procname) {
1585 pos = append_path(new_path, pos, table->procname);
1586 if (!pos)
1587 goto out;
1588 table = table->child;
1589 }
f728019b 1590 if (nr_subheaders == 1) {
60a47a2e 1591 header = __register_sysctl_table(set, new_path, table);
f728019b
EB
1592 if (header)
1593 header->ctl_table_arg = ctl_table_arg;
1594 } else {
1595 header = kzalloc(sizeof(*header) +
1596 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1597 if (!header)
1598 goto out;
1599
1600 subheaders = (struct ctl_table_header **) (header + 1);
1601 subheader = subheaders;
ec6a5266 1602 header->ctl_table_arg = ctl_table_arg;
f728019b
EB
1603
1604 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
60a47a2e 1605 set, table))
f728019b
EB
1606 goto err_register_leaves;
1607 }
1608
6e9d5164
EB
1609out:
1610 kfree(new_path);
1611 return header;
f728019b
EB
1612
1613err_register_leaves:
1614 while (subheader > subheaders) {
1615 struct ctl_table_header *subh = *(--subheader);
1616 struct ctl_table *table = subh->ctl_table_arg;
1617 unregister_sysctl_table(subh);
1618 kfree(table);
1619 }
1620 kfree(header);
1621 header = NULL;
1622 goto out;
6e9d5164
EB
1623}
1624
1f87f0b5 1625/**
5b31a7df 1626 * register_sysctl_paths - register a sysctl table hierarchy
1f87f0b5
EB
1627 * @path: The path to the directory the sysctl table is in.
1628 * @table: the top-level table structure
1629 *
1630 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1631 * array. A completely 0 filled entry terminates the table.
1632 *
1633 * See __register_sysctl_paths for more details.
1634 */
1635struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1636 struct ctl_table *table)
1637{
60a47a2e 1638 return __register_sysctl_paths(&sysctl_table_root.default_set,
1f87f0b5
EB
1639 path, table);
1640}
1641EXPORT_SYMBOL(register_sysctl_paths);
1642
1643/**
1644 * register_sysctl_table - register a sysctl table hierarchy
1645 * @table: the top-level table structure
1646 *
1647 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1648 * array. A completely 0 filled entry terminates the table.
1649 *
1650 * See register_sysctl_paths for more details.
1651 */
1652struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1653{
1654 static const struct ctl_path null_path[] = { {} };
1655
1656 return register_sysctl_paths(null_path, table);
1657}
1658EXPORT_SYMBOL(register_sysctl_table);
1659
51cb8dfc
LC
1660int __register_sysctl_base(struct ctl_table *base_table)
1661{
1662 struct ctl_table_header *hdr;
1663
1664 hdr = register_sysctl_table(base_table);
1665 kmemleak_not_leak(hdr);
1666 return 0;
1667}
1668
0e47c99d
EB
1669static void put_links(struct ctl_table_header *header)
1670{
1671 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1672 struct ctl_table_root *root = header->root;
1673 struct ctl_dir *parent = header->parent;
1674 struct ctl_dir *core_parent;
1675 struct ctl_table *entry;
1676
1677 if (header->set == root_set)
1678 return;
1679
1680 core_parent = xlate_dir(root_set, parent);
1681 if (IS_ERR(core_parent))
1682 return;
1683
cb55f27a 1684 list_for_each_table_entry(entry, header->ctl_table) {
0e47c99d
EB
1685 struct ctl_table_header *link_head;
1686 struct ctl_table *link;
1687 const char *name = entry->procname;
1688
1689 link = find_entry(&link_head, core_parent, name, strlen(name));
1690 if (link &&
1691 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1692 (S_ISLNK(link->mode) && (link->data == root)))) {
1693 drop_sysctl_table(link_head);
1694 }
1695 else {
87ebdc00 1696 pr_err("sysctl link missing during unregister: ");
0e47c99d 1697 sysctl_print_dir(parent);
153ee1c4 1698 pr_cont("%s\n", name);
0e47c99d
EB
1699 }
1700 }
1701}
1702
938aaa4f
EB
1703static void drop_sysctl_table(struct ctl_table_header *header)
1704{
7ec66d06
EB
1705 struct ctl_dir *parent = header->parent;
1706
938aaa4f
EB
1707 if (--header->nreg)
1708 return;
1709
89189557 1710 if (parent) {
23da9588 1711 put_links(header);
89189557
Y
1712 start_unregistering(header);
1713 }
1714
938aaa4f
EB
1715 if (!--header->count)
1716 kfree_rcu(header, rcu);
7ec66d06
EB
1717
1718 if (parent)
1719 drop_sysctl_table(&parent->header);
938aaa4f
EB
1720}
1721
1f87f0b5
EB
1722/**
1723 * unregister_sysctl_table - unregister a sysctl table hierarchy
1724 * @header: the header returned from register_sysctl_table
1725 *
1726 * Unregisters the sysctl table and all children. proc entries may not
1727 * actually be removed until they are no longer used by anyone.
1728 */
1729void unregister_sysctl_table(struct ctl_table_header * header)
1730{
f728019b 1731 int nr_subheaders;
1f87f0b5
EB
1732 might_sleep();
1733
1734 if (header == NULL)
1735 return;
1736
f728019b
EB
1737 nr_subheaders = count_subheaders(header->ctl_table_arg);
1738 if (unlikely(nr_subheaders > 1)) {
1739 struct ctl_table_header **subheaders;
1740 int i;
1741
1742 subheaders = (struct ctl_table_header **)(header + 1);
1743 for (i = nr_subheaders -1; i >= 0; i--) {
1744 struct ctl_table_header *subh = subheaders[i];
1745 struct ctl_table *table = subh->ctl_table_arg;
1746 unregister_sysctl_table(subh);
1747 kfree(table);
1748 }
1749 kfree(header);
1750 return;
1751 }
1752
1f87f0b5 1753 spin_lock(&sysctl_lock);
938aaa4f 1754 drop_sysctl_table(header);
1f87f0b5
EB
1755 spin_unlock(&sysctl_lock);
1756}
1757EXPORT_SYMBOL(unregister_sysctl_table);
1758
0e47c99d 1759void setup_sysctl_set(struct ctl_table_set *set,
9eb47c26 1760 struct ctl_table_root *root,
1f87f0b5
EB
1761 int (*is_seen)(struct ctl_table_set *))
1762{
1347440d 1763 memset(set, 0, sizeof(*set));
0e47c99d 1764 set->is_seen = is_seen;
ac13ac6f 1765 init_header(&set->dir.header, root, set, NULL, root_table);
1f87f0b5
EB
1766}
1767
97324cd8
EB
1768void retire_sysctl_set(struct ctl_table_set *set)
1769{
ac13ac6f 1770 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
97324cd8 1771}
1f87f0b5 1772
1e0edd3f 1773int __init proc_sys_init(void)
77b14db5 1774{
e1675231
AD
1775 struct proc_dir_entry *proc_sys_root;
1776
77b14db5 1777 proc_sys_root = proc_mkdir("sys", NULL);
9043476f 1778 proc_sys_root->proc_iops = &proc_sys_dir_operations;
d56c0d45 1779 proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
77b14db5 1780 proc_sys_root->nlink = 0;
de4e83bd 1781
d8c0418a 1782 return sysctl_init_bases();
77b14db5 1783}
3db978d4 1784
0a477e1a
VB
1785struct sysctl_alias {
1786 const char *kernel_param;
1787 const char *sysctl_param;
1788};
1789
1790/*
1791 * Historically some settings had both sysctl and a command line parameter.
1792 * With the generic sysctl. parameter support, we can handle them at a single
1793 * place and only keep the historical name for compatibility. This is not meant
1794 * to add brand new aliases. When adding existing aliases, consider whether
1795 * the possibly different moment of changing the value (e.g. from early_param
1796 * to the moment do_sysctl_args() is called) is an issue for the specific
1797 * parameter.
1798 */
1799static const struct sysctl_alias sysctl_aliases[] = {
f117955a
GP
1800 {"hardlockup_all_cpu_backtrace", "kernel.hardlockup_all_cpu_backtrace" },
1801 {"hung_task_panic", "kernel.hung_task_panic" },
1802 {"numa_zonelist_order", "vm.numa_zonelist_order" },
1803 {"softlockup_all_cpu_backtrace", "kernel.softlockup_all_cpu_backtrace" },
1804 {"softlockup_panic", "kernel.softlockup_panic" },
0a477e1a
VB
1805 { }
1806};
1807
1808static const char *sysctl_find_alias(char *param)
1809{
1810 const struct sysctl_alias *alias;
1811
1812 for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
1813 if (strcmp(alias->kernel_param, param) == 0)
1814 return alias->sysctl_param;
1815 }
1816
1817 return NULL;
1818}
1819
3db978d4
VB
1820/* Set sysctl value passed on kernel command line. */
1821static int process_sysctl_arg(char *param, char *val,
1822 const char *unused, void *arg)
1823{
1824 char *path;
1825 struct vfsmount **proc_mnt = arg;
1826 struct file_system_type *proc_fs_type;
1827 struct file *file;
1828 int len;
1829 int err;
1830 loff_t pos = 0;
1831 ssize_t wret;
1832
0a477e1a
VB
1833 if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
1834 param += sizeof("sysctl") - 1;
3db978d4 1835
0a477e1a
VB
1836 if (param[0] != '/' && param[0] != '.')
1837 return 0;
3db978d4 1838
0a477e1a
VB
1839 param++;
1840 } else {
1841 param = (char *) sysctl_find_alias(param);
1842 if (!param)
1843 return 0;
1844 }
3db978d4 1845
697edcb0
XN
1846 if (!val)
1847 return -EINVAL;
1848 len = strlen(val);
1849 if (len == 0)
1850 return -EINVAL;
1851
3db978d4
VB
1852 /*
1853 * To set sysctl options, we use a temporary mount of proc, look up the
1854 * respective sys/ file and write to it. To avoid mounting it when no
1855 * options were given, we mount it only when the first sysctl option is
1856 * found. Why not a persistent mount? There are problems with a
1857 * persistent mount of proc in that it forces userspace not to use any
1858 * proc mount options.
1859 */
1860 if (!*proc_mnt) {
1861 proc_fs_type = get_fs_type("proc");
1862 if (!proc_fs_type) {
1863 pr_err("Failed to find procfs to set sysctl from command line\n");
1864 return 0;
1865 }
1866 *proc_mnt = kern_mount(proc_fs_type);
1867 put_filesystem(proc_fs_type);
1868 if (IS_ERR(*proc_mnt)) {
1869 pr_err("Failed to mount procfs to set sysctl from command line\n");
1870 return 0;
1871 }
1872 }
1873
1874 path = kasprintf(GFP_KERNEL, "sys/%s", param);
1875 if (!path)
1876 panic("%s: Failed to allocate path for %s\n", __func__, param);
1877 strreplace(path, '.', '/');
1878
ffb37ca3 1879 file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
3db978d4
VB
1880 if (IS_ERR(file)) {
1881 err = PTR_ERR(file);
1882 if (err == -ENOENT)
1883 pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
1884 param, val);
1885 else if (err == -EACCES)
1886 pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
1887 param, val);
1888 else
1889 pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
1890 file, param, val);
1891 goto out;
1892 }
3db978d4
VB
1893 wret = kernel_write(file, val, len, &pos);
1894 if (wret < 0) {
1895 err = wret;
1896 if (err == -EINVAL)
1897 pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
1898 param, val);
1899 else
1900 pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
1901 ERR_PTR(err), param, val);
1902 } else if (wret != len) {
1903 pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
1904 wret, len, path, param, val);
1905 }
1906
1907 err = filp_close(file, NULL);
1908 if (err)
1909 pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
1910 ERR_PTR(err), param, val);
1911out:
1912 kfree(path);
1913 return 0;
1914}
1915
1916void do_sysctl_args(void)
1917{
1918 char *command_line;
1919 struct vfsmount *proc_mnt = NULL;
1920
1921 command_line = kstrdup(saved_command_line, GFP_KERNEL);
1922 if (!command_line)
1923 panic("%s: Failed to allocate copy of command line\n", __func__);
1924
1925 parse_args("Setting sysctl args", command_line,
1926 NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
1927
1928 if (proc_mnt)
1929 kern_unmount(proc_mnt);
1930
1931 kfree(command_line);
1932}