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