aboutsummaryrefslogtreecommitdiffstats
path: root/fs/proc
diff options
context:
space:
mode:
authorEric W. Biederman <ebiederm@xmission.com>2012-01-06 07:07:15 -0500
committerEric W. Biederman <ebiederm@xmission.com>2012-01-24 19:37:54 -0500
commit1f87f0b52b1d6581168cb80f86746bc4df918d01 (patch)
tree73576c0872c61c526ade225d6441a6610beb666b /fs/proc
parentde4e83bd6b5e16d491ec068cd22801d5d063b07a (diff)
sysctl: Move the implementation into fs/proc/proc_sysctl.c
Move the core sysctl code from kernel/sysctl.c and kernel/sysctl_check.c into fs/proc/proc_sysctl.c. Currently sysctl maintenance is hampered by the sysctl implementation being split across 3 files with artificial layering between them. Consolidate the entire sysctl implementation into 1 file so that it is easier to see what is going on and hopefully allowing for simpler maintenance. For functions that are now only used in fs/proc/proc_sysctl.c remove their declarations from sysctl.h and make them static in fs/proc/proc_sysctl.c Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Diffstat (limited to 'fs/proc')
-rw-r--r--fs/proc/internal.h3
-rw-r--r--fs/proc/proc_sysctl.c622
2 files changed, 625 insertions, 0 deletions
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 292577531ad1..3b5ecd960d6a 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -10,12 +10,15 @@
10 */ 10 */
11 11
12#include <linux/proc_fs.h> 12#include <linux/proc_fs.h>
13struct ctl_table_header;
13 14
14extern struct proc_dir_entry proc_root; 15extern struct proc_dir_entry proc_root;
15#ifdef CONFIG_PROC_SYSCTL 16#ifdef CONFIG_PROC_SYSCTL
16extern int proc_sys_init(void); 17extern int proc_sys_init(void);
18extern void sysctl_head_put(struct ctl_table_header *head);
17#else 19#else
18static inline void proc_sys_init(void) { } 20static inline void proc_sys_init(void) { }
21static inline void sysctl_head_put(struct ctl_table_header *head) { }
19#endif 22#endif
20#ifdef CONFIG_NET 23#ifdef CONFIG_NET
21extern int proc_net_init(void); 24extern int proc_net_init(void);
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 9d29d28af577..06e6f10ee8ec 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -7,6 +7,7 @@
7#include <linux/proc_fs.h> 7#include <linux/proc_fs.h>
8#include <linux/security.h> 8#include <linux/security.h>
9#include <linux/namei.h> 9#include <linux/namei.h>
10#include <linux/module.h>
10#include "internal.h" 11#include "internal.h"
11 12
12static const struct dentry_operations proc_sys_dentry_operations; 13static const struct dentry_operations proc_sys_dentry_operations;
@@ -24,6 +25,209 @@ void proc_sys_poll_notify(struct ctl_table_poll *poll)
24 wake_up_interruptible(&poll->wait); 25 wake_up_interruptible(&poll->wait);
25} 26}
26 27
28static struct ctl_table root_table[1];
29static struct ctl_table_root sysctl_table_root;
30static struct ctl_table_header root_table_header = {
31 {{.count = 1,
32 .ctl_table = root_table,
33 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
34 .root = &sysctl_table_root,
35 .set = &sysctl_table_root.default_set,
36};
37static struct ctl_table_root sysctl_table_root = {
38 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
39 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
40};
41
42static DEFINE_SPINLOCK(sysctl_lock);
43
44/* called under sysctl_lock */
45static int use_table(struct ctl_table_header *p)
46{
47 if (unlikely(p->unregistering))
48 return 0;
49 p->used++;
50 return 1;
51}
52
53/* called under sysctl_lock */
54static void unuse_table(struct ctl_table_header *p)
55{
56 if (!--p->used)
57 if (unlikely(p->unregistering))
58 complete(p->unregistering);
59}
60
61/* called under sysctl_lock, will reacquire if has to wait */
62static void start_unregistering(struct ctl_table_header *p)
63{
64 /*
65 * if p->used is 0, nobody will ever touch that entry again;
66 * we'll eliminate all paths to it before dropping sysctl_lock
67 */
68 if (unlikely(p->used)) {
69 struct completion wait;
70 init_completion(&wait);
71 p->unregistering = &wait;
72 spin_unlock(&sysctl_lock);
73 wait_for_completion(&wait);
74 spin_lock(&sysctl_lock);
75 } else {
76 /* anything non-NULL; we'll never dereference it */
77 p->unregistering = ERR_PTR(-EINVAL);
78 }
79 /*
80 * do not remove from the list until nobody holds it; walking the
81 * list in do_sysctl() relies on that.
82 */
83 list_del_init(&p->ctl_entry);
84}
85
86static void sysctl_head_get(struct ctl_table_header *head)
87{
88 spin_lock(&sysctl_lock);
89 head->count++;
90 spin_unlock(&sysctl_lock);
91}
92
93void sysctl_head_put(struct ctl_table_header *head)
94{
95 spin_lock(&sysctl_lock);
96 if (!--head->count)
97 kfree_rcu(head, rcu);
98 spin_unlock(&sysctl_lock);
99}
100
101static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
102{
103 if (!head)
104 BUG();
105 spin_lock(&sysctl_lock);
106 if (!use_table(head))
107 head = ERR_PTR(-ENOENT);
108 spin_unlock(&sysctl_lock);
109 return head;
110}
111
112static void sysctl_head_finish(struct ctl_table_header *head)
113{
114 if (!head)
115 return;
116 spin_lock(&sysctl_lock);
117 unuse_table(head);
118 spin_unlock(&sysctl_lock);
119}
120
121static struct ctl_table_set *
122lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
123{
124 struct ctl_table_set *set = &root->default_set;
125 if (root->lookup)
126 set = root->lookup(root, namespaces);
127 return set;
128}
129
130static struct list_head *
131lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
132{
133 struct ctl_table_set *set = lookup_header_set(root, namespaces);
134 return &set->list;
135}
136
137static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
138 struct ctl_table_header *prev)
139{
140 struct ctl_table_root *root;
141 struct list_head *header_list;
142 struct ctl_table_header *head;
143 struct list_head *tmp;
144
145 spin_lock(&sysctl_lock);
146 if (prev) {
147 head = prev;
148 tmp = &prev->ctl_entry;
149 unuse_table(prev);
150 goto next;
151 }
152 tmp = &root_table_header.ctl_entry;
153 for (;;) {
154 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
155
156 if (!use_table(head))
157 goto next;
158 spin_unlock(&sysctl_lock);
159 return head;
160 next:
161 root = head->root;
162 tmp = tmp->next;
163 header_list = lookup_header_list(root, namespaces);
164 if (tmp != header_list)
165 continue;
166
167 do {
168 root = list_entry(root->root_list.next,
169 struct ctl_table_root, root_list);
170 if (root == &sysctl_table_root)
171 goto out;
172 header_list = lookup_header_list(root, namespaces);
173 } while (list_empty(header_list));
174 tmp = header_list->next;
175 }
176out:
177 spin_unlock(&sysctl_lock);
178 return NULL;
179}
180
181static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
182{
183 return __sysctl_head_next(current->nsproxy, prev);
184}
185
186void register_sysctl_root(struct ctl_table_root *root)
187{
188 spin_lock(&sysctl_lock);
189 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
190 spin_unlock(&sysctl_lock);
191}
192
193/*
194 * sysctl_perm does NOT grant the superuser all rights automatically, because
195 * some sysctl variables are readonly even to root.
196 */
197
198static int test_perm(int mode, int op)
199{
200 if (!current_euid())
201 mode >>= 6;
202 else if (in_egroup_p(0))
203 mode >>= 3;
204 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
205 return 0;
206 return -EACCES;
207}
208
209static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
210{
211 int mode;
212
213 if (root->permissions)
214 mode = root->permissions(root, current->nsproxy, table);
215 else
216 mode = table->mode;
217
218 return test_perm(mode, op);
219}
220
221static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
222{
223 for (; table->procname; table++) {
224 table->parent = parent;
225 if (table->child)
226 sysctl_set_parent(table, table->child);
227 }
228}
229
230
27static struct inode *proc_sys_make_inode(struct super_block *sb, 231static struct inode *proc_sys_make_inode(struct super_block *sb,
28 struct ctl_table_header *head, struct ctl_table *table) 232 struct ctl_table_header *head, struct ctl_table *table)
29{ 233{
@@ -435,6 +639,21 @@ static int proc_sys_delete(const struct dentry *dentry)
435 return !!PROC_I(dentry->d_inode)->sysctl->unregistering; 639 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
436} 640}
437 641
642static int sysctl_is_seen(struct ctl_table_header *p)
643{
644 struct ctl_table_set *set = p->set;
645 int res;
646 spin_lock(&sysctl_lock);
647 if (p->unregistering)
648 res = 0;
649 else if (!set->is_seen)
650 res = 1;
651 else
652 res = set->is_seen(set);
653 spin_unlock(&sysctl_lock);
654 return res;
655}
656
438static int proc_sys_compare(const struct dentry *parent, 657static int proc_sys_compare(const struct dentry *parent,
439 const struct inode *pinode, 658 const struct inode *pinode,
440 const struct dentry *dentry, const struct inode *inode, 659 const struct dentry *dentry, const struct inode *inode,
@@ -460,6 +679,409 @@ static const struct dentry_operations proc_sys_dentry_operations = {
460 .d_compare = proc_sys_compare, 679 .d_compare = proc_sys_compare,
461}; 680};
462 681
682static struct ctl_table *is_branch_in(struct ctl_table *branch,
683 struct ctl_table *table)
684{
685 struct ctl_table *p;
686 const char *s = branch->procname;
687
688 /* branch should have named subdirectory as its first element */
689 if (!s || !branch->child)
690 return NULL;
691
692 /* ... and nothing else */
693 if (branch[1].procname)
694 return NULL;
695
696 /* table should contain subdirectory with the same name */
697 for (p = table; p->procname; p++) {
698 if (!p->child)
699 continue;
700 if (p->procname && strcmp(p->procname, s) == 0)
701 return p;
702 }
703 return NULL;
704}
705
706/* see if attaching q to p would be an improvement */
707static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
708{
709 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
710 struct ctl_table *next;
711 int is_better = 0;
712 int not_in_parent = !p->attached_by;
713
714 while ((next = is_branch_in(by, to)) != NULL) {
715 if (by == q->attached_by)
716 is_better = 1;
717 if (to == p->attached_by)
718 not_in_parent = 1;
719 by = by->child;
720 to = next->child;
721 }
722
723 if (is_better && not_in_parent) {
724 q->attached_by = by;
725 q->attached_to = to;
726 q->parent = p;
727 }
728}
729
730#ifdef CONFIG_SYSCTL_SYSCALL_CHECK
731static int sysctl_depth(struct ctl_table *table)
732{
733 struct ctl_table *tmp;
734 int depth;
735
736 depth = 0;
737 for (tmp = table; tmp->parent; tmp = tmp->parent)
738 depth++;
739
740 return depth;
741}
742
743static struct ctl_table *sysctl_parent(struct ctl_table *table, int n)
744{
745 int i;
746
747 for (i = 0; table && i < n; i++)
748 table = table->parent;
749
750 return table;
751}
752
753
754static void sysctl_print_path(struct ctl_table *table)
755{
756 struct ctl_table *tmp;
757 int depth, i;
758 depth = sysctl_depth(table);
759 if (table->procname) {
760 for (i = depth; i >= 0; i--) {
761 tmp = sysctl_parent(table, i);
762 printk("/%s", tmp->procname?tmp->procname:"");
763 }
764 }
765 printk(" ");
766}
767
768static struct ctl_table *sysctl_check_lookup(struct nsproxy *namespaces,
769 struct ctl_table *table)
770{
771 struct ctl_table_header *head;
772 struct ctl_table *ref, *test;
773 int depth, cur_depth;
774
775 depth = sysctl_depth(table);
776
777 for (head = __sysctl_head_next(namespaces, NULL); head;
778 head = __sysctl_head_next(namespaces, head)) {
779 cur_depth = depth;
780 ref = head->ctl_table;
781repeat:
782 test = sysctl_parent(table, cur_depth);
783 for (; ref->procname; ref++) {
784 int match = 0;
785 if (cur_depth && !ref->child)
786 continue;
787
788 if (test->procname && ref->procname &&
789 (strcmp(test->procname, ref->procname) == 0))
790 match++;
791
792 if (match) {
793 if (cur_depth != 0) {
794 cur_depth--;
795 ref = ref->child;
796 goto repeat;
797 }
798 goto out;
799 }
800 }
801 }
802 ref = NULL;
803out:
804 sysctl_head_finish(head);
805 return ref;
806}
807
808static void set_fail(const char **fail, struct ctl_table *table, const char *str)
809{
810 if (*fail) {
811 printk(KERN_ERR "sysctl table check failed: ");
812 sysctl_print_path(table);
813 printk(" %s\n", *fail);
814 dump_stack();
815 }
816 *fail = str;
817}
818
819static void sysctl_check_leaf(struct nsproxy *namespaces,
820 struct ctl_table *table, const char **fail)
821{
822 struct ctl_table *ref;
823
824 ref = sysctl_check_lookup(namespaces, table);
825 if (ref && (ref != table))
826 set_fail(fail, table, "Sysctl already exists");
827}
828
829static int sysctl_check_table(struct nsproxy *namespaces, struct ctl_table *table)
830{
831 int error = 0;
832 for (; table->procname; table++) {
833 const char *fail = NULL;
834
835 if (table->parent) {
836 if (!table->parent->procname)
837 set_fail(&fail, table, "Parent without procname");
838 }
839 if (table->child) {
840 if (table->data)
841 set_fail(&fail, table, "Directory with data?");
842 if (table->maxlen)
843 set_fail(&fail, table, "Directory with maxlen?");
844 if ((table->mode & (S_IRUGO|S_IXUGO)) != table->mode)
845 set_fail(&fail, table, "Writable sysctl directory");
846 if (table->proc_handler)
847 set_fail(&fail, table, "Directory with proc_handler");
848 if (table->extra1)
849 set_fail(&fail, table, "Directory with extra1");
850 if (table->extra2)
851 set_fail(&fail, table, "Directory with extra2");
852 } else {
853 if ((table->proc_handler == proc_dostring) ||
854 (table->proc_handler == proc_dointvec) ||
855 (table->proc_handler == proc_dointvec_minmax) ||
856 (table->proc_handler == proc_dointvec_jiffies) ||
857 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
858 (table->proc_handler == proc_dointvec_ms_jiffies) ||
859 (table->proc_handler == proc_doulongvec_minmax) ||
860 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
861 if (!table->data)
862 set_fail(&fail, table, "No data");
863 if (!table->maxlen)
864 set_fail(&fail, table, "No maxlen");
865 }
866#ifdef CONFIG_PROC_SYSCTL
867 if (!table->proc_handler)
868 set_fail(&fail, table, "No proc_handler");
869#endif
870 sysctl_check_leaf(namespaces, table, &fail);
871 }
872 if (table->mode > 0777)
873 set_fail(&fail, table, "bogus .mode");
874 if (fail) {
875 set_fail(&fail, table, NULL);
876 error = -EINVAL;
877 }
878 if (table->child)
879 error |= sysctl_check_table(namespaces, table->child);
880 }
881 return error;
882}
883#endif /* CONFIG_SYSCTL_SYSCALL_CHECK */
884
885/**
886 * __register_sysctl_paths - register a sysctl hierarchy
887 * @root: List of sysctl headers to register on
888 * @namespaces: Data to compute which lists of sysctl entries are visible
889 * @path: The path to the directory the sysctl table is in.
890 * @table: the top-level table structure
891 *
892 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
893 * array. A completely 0 filled entry terminates the table.
894 *
895 * The members of the &struct ctl_table structure are used as follows:
896 *
897 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
898 * enter a sysctl file
899 *
900 * data - a pointer to data for use by proc_handler
901 *
902 * maxlen - the maximum size in bytes of the data
903 *
904 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
905 *
906 * child - a pointer to the child sysctl table if this entry is a directory, or
907 * %NULL.
908 *
909 * proc_handler - the text handler routine (described below)
910 *
911 * de - for internal use by the sysctl routines
912 *
913 * extra1, extra2 - extra pointers usable by the proc handler routines
914 *
915 * Leaf nodes in the sysctl tree will be represented by a single file
916 * under /proc; non-leaf nodes will be represented by directories.
917 *
918 * sysctl(2) can automatically manage read and write requests through
919 * the sysctl table. The data and maxlen fields of the ctl_table
920 * struct enable minimal validation of the values being written to be
921 * performed, and the mode field allows minimal authentication.
922 *
923 * There must be a proc_handler routine for any terminal nodes
924 * mirrored under /proc/sys (non-terminals are handled by a built-in
925 * directory handler). Several default handlers are available to
926 * cover common cases -
927 *
928 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
929 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
930 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
931 *
932 * It is the handler's job to read the input buffer from user memory
933 * and process it. The handler should return 0 on success.
934 *
935 * This routine returns %NULL on a failure to register, and a pointer
936 * to the table header on success.
937 */
938struct ctl_table_header *__register_sysctl_paths(
939 struct ctl_table_root *root,
940 struct nsproxy *namespaces,
941 const struct ctl_path *path, struct ctl_table *table)
942{
943 struct ctl_table_header *header;
944 struct ctl_table *new, **prevp;
945 unsigned int n, npath;
946 struct ctl_table_set *set;
947
948 /* Count the path components */
949 for (npath = 0; path[npath].procname; ++npath)
950 ;
951
952 /*
953 * For each path component, allocate a 2-element ctl_table array.
954 * The first array element will be filled with the sysctl entry
955 * for this, the second will be the sentinel (procname == 0).
956 *
957 * We allocate everything in one go so that we don't have to
958 * worry about freeing additional memory in unregister_sysctl_table.
959 */
960 header = kzalloc(sizeof(struct ctl_table_header) +
961 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
962 if (!header)
963 return NULL;
964
965 new = (struct ctl_table *) (header + 1);
966
967 /* Now connect the dots */
968 prevp = &header->ctl_table;
969 for (n = 0; n < npath; ++n, ++path) {
970 /* Copy the procname */
971 new->procname = path->procname;
972 new->mode = 0555;
973
974 *prevp = new;
975 prevp = &new->child;
976
977 new += 2;
978 }
979 *prevp = table;
980 header->ctl_table_arg = table;
981
982 INIT_LIST_HEAD(&header->ctl_entry);
983 header->used = 0;
984 header->unregistering = NULL;
985 header->root = root;
986 sysctl_set_parent(NULL, header->ctl_table);
987 header->count = 1;
988#ifdef CONFIG_SYSCTL_SYSCALL_CHECK
989 if (sysctl_check_table(namespaces, header->ctl_table)) {
990 kfree(header);
991 return NULL;
992 }
993#endif
994 spin_lock(&sysctl_lock);
995 header->set = lookup_header_set(root, namespaces);
996 header->attached_by = header->ctl_table;
997 header->attached_to = root_table;
998 header->parent = &root_table_header;
999 for (set = header->set; set; set = set->parent) {
1000 struct ctl_table_header *p;
1001 list_for_each_entry(p, &set->list, ctl_entry) {
1002 if (p->unregistering)
1003 continue;
1004 try_attach(p, header);
1005 }
1006 }
1007 header->parent->count++;
1008 list_add_tail(&header->ctl_entry, &header->set->list);
1009 spin_unlock(&sysctl_lock);
1010
1011 return header;
1012}
1013
1014/**
1015 * register_sysctl_table_path - register a sysctl table hierarchy
1016 * @path: The path to the directory the sysctl table is in.
1017 * @table: the top-level table structure
1018 *
1019 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1020 * array. A completely 0 filled entry terminates the table.
1021 *
1022 * See __register_sysctl_paths for more details.
1023 */
1024struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1025 struct ctl_table *table)
1026{
1027 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1028 path, table);
1029}
1030EXPORT_SYMBOL(register_sysctl_paths);
1031
1032/**
1033 * register_sysctl_table - register a sysctl table hierarchy
1034 * @table: the top-level table structure
1035 *
1036 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1037 * array. A completely 0 filled entry terminates the table.
1038 *
1039 * See register_sysctl_paths for more details.
1040 */
1041struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1042{
1043 static const struct ctl_path null_path[] = { {} };
1044
1045 return register_sysctl_paths(null_path, table);
1046}
1047EXPORT_SYMBOL(register_sysctl_table);
1048
1049/**
1050 * unregister_sysctl_table - unregister a sysctl table hierarchy
1051 * @header: the header returned from register_sysctl_table
1052 *
1053 * Unregisters the sysctl table and all children. proc entries may not
1054 * actually be removed until they are no longer used by anyone.
1055 */
1056void unregister_sysctl_table(struct ctl_table_header * header)
1057{
1058 might_sleep();
1059
1060 if (header == NULL)
1061 return;
1062
1063 spin_lock(&sysctl_lock);
1064 start_unregistering(header);
1065 if (!--header->parent->count) {
1066 WARN_ON(1);
1067 kfree_rcu(header->parent, rcu);
1068 }
1069 if (!--header->count)
1070 kfree_rcu(header, rcu);
1071 spin_unlock(&sysctl_lock);
1072}
1073EXPORT_SYMBOL(unregister_sysctl_table);
1074
1075void setup_sysctl_set(struct ctl_table_set *p,
1076 struct ctl_table_set *parent,
1077 int (*is_seen)(struct ctl_table_set *))
1078{
1079 INIT_LIST_HEAD(&p->list);
1080 p->parent = parent ? parent : &sysctl_table_root.default_set;
1081 p->is_seen = is_seen;
1082}
1083
1084
463int __init proc_sys_init(void) 1085int __init proc_sys_init(void)
464{ 1086{
465 struct proc_dir_entry *proc_sys_root; 1087 struct proc_dir_entry *proc_sys_root;