Commit 266d07cb1c9a0c345d7d3aea889f92062894059e

Authored by Patrick McHardy
1 parent 8981f01001

netfilter: nf_log: fix sleeping function called from invalid context

Fix regression introduced by 17625274 "netfilter: sysctl support of
logger choice":

BUG: sleeping function called from invalid context at /mnt/s390test/linux-2.6-tip/arch/s390/include/asm/uaccess.h:234
in_atomic(): 1, irqs_disabled(): 0, pid: 3245, name: sysctl
CPU: 1 Not tainted 2.6.30-rc8-tipjun10-02053-g39ae214 #1
Process sysctl (pid: 3245, task: 000000007f675da0, ksp: 000000007eb17cf0)
0000000000000000 000000007eb17be8 0000000000000002 0000000000000000
       000000007eb17c88 000000007eb17c00 000000007eb17c00 0000000000048156
       00000000003e2de8 000000007f676118 000000007eb17f10 0000000000000000
       0000000000000000 000000007eb17be8 000000000000000d 000000007eb17c58
       00000000003e2050 000000000001635c 000000007eb17be8 000000007eb17c30
Call Trace:
(Ý<00000000000162e6>¨ show_trace+0x13a/0x148)
 Ý<00000000000349ea>¨ __might_sleep+0x13a/0x164
 Ý<0000000000050300>¨ proc_dostring+0x134/0x22c
 Ý<0000000000312b70>¨ nf_log_proc_dostring+0xfc/0x188
 Ý<0000000000136f5e>¨ proc_sys_call_handler+0xf6/0x118
 Ý<0000000000136fda>¨ proc_sys_read+0x26/0x34
 Ý<00000000000d6e9c>¨ vfs_read+0xac/0x158
 Ý<00000000000d703e>¨ SyS_read+0x56/0x88
 Ý<0000000000027f42>¨ sysc_noemu+0x10/0x16

Use the nf_log_mutex instead of RCU to fix this.

Reported-and-tested-by: Maran Pakkirisamy <maranpsamy@in.ibm.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>

Showing 1 changed file with 3 additions and 3 deletions Inline Diff

net/netfilter/nf_log.c
1 #include <linux/kernel.h> 1 #include <linux/kernel.h>
2 #include <linux/init.h> 2 #include <linux/init.h>
3 #include <linux/module.h> 3 #include <linux/module.h>
4 #include <linux/proc_fs.h> 4 #include <linux/proc_fs.h>
5 #include <linux/skbuff.h> 5 #include <linux/skbuff.h>
6 #include <linux/netfilter.h> 6 #include <linux/netfilter.h>
7 #include <linux/seq_file.h> 7 #include <linux/seq_file.h>
8 #include <net/protocol.h> 8 #include <net/protocol.h>
9 #include <net/netfilter/nf_log.h> 9 #include <net/netfilter/nf_log.h>
10 10
11 #include "nf_internals.h" 11 #include "nf_internals.h"
12 12
13 /* Internal logging interface, which relies on the real 13 /* Internal logging interface, which relies on the real
14 LOG target modules */ 14 LOG target modules */
15 15
16 #define NF_LOG_PREFIXLEN 128 16 #define NF_LOG_PREFIXLEN 128
17 #define NFLOGGER_NAME_LEN 64 17 #define NFLOGGER_NAME_LEN 64
18 18
19 static const struct nf_logger *nf_loggers[NFPROTO_NUMPROTO] __read_mostly; 19 static const struct nf_logger *nf_loggers[NFPROTO_NUMPROTO] __read_mostly;
20 static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly; 20 static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly;
21 static DEFINE_MUTEX(nf_log_mutex); 21 static DEFINE_MUTEX(nf_log_mutex);
22 22
23 static struct nf_logger *__find_logger(int pf, const char *str_logger) 23 static struct nf_logger *__find_logger(int pf, const char *str_logger)
24 { 24 {
25 struct nf_logger *t; 25 struct nf_logger *t;
26 26
27 list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) { 27 list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) {
28 if (!strnicmp(str_logger, t->name, strlen(t->name))) 28 if (!strnicmp(str_logger, t->name, strlen(t->name)))
29 return t; 29 return t;
30 } 30 }
31 31
32 return NULL; 32 return NULL;
33 } 33 }
34 34
35 /* return EEXIST if the same logger is registred, 0 on success. */ 35 /* return EEXIST if the same logger is registred, 0 on success. */
36 int nf_log_register(u_int8_t pf, struct nf_logger *logger) 36 int nf_log_register(u_int8_t pf, struct nf_logger *logger)
37 { 37 {
38 const struct nf_logger *llog; 38 const struct nf_logger *llog;
39 int i; 39 int i;
40 40
41 if (pf >= ARRAY_SIZE(nf_loggers)) 41 if (pf >= ARRAY_SIZE(nf_loggers))
42 return -EINVAL; 42 return -EINVAL;
43 43
44 for (i = 0; i < ARRAY_SIZE(logger->list); i++) 44 for (i = 0; i < ARRAY_SIZE(logger->list); i++)
45 INIT_LIST_HEAD(&logger->list[i]); 45 INIT_LIST_HEAD(&logger->list[i]);
46 46
47 mutex_lock(&nf_log_mutex); 47 mutex_lock(&nf_log_mutex);
48 48
49 if (pf == NFPROTO_UNSPEC) { 49 if (pf == NFPROTO_UNSPEC) {
50 int i; 50 int i;
51 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) 51 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
52 list_add_tail(&(logger->list[i]), &(nf_loggers_l[i])); 52 list_add_tail(&(logger->list[i]), &(nf_loggers_l[i]));
53 } else { 53 } else {
54 /* register at end of list to honor first register win */ 54 /* register at end of list to honor first register win */
55 list_add_tail(&logger->list[pf], &nf_loggers_l[pf]); 55 list_add_tail(&logger->list[pf], &nf_loggers_l[pf]);
56 llog = rcu_dereference(nf_loggers[pf]); 56 llog = rcu_dereference(nf_loggers[pf]);
57 if (llog == NULL) 57 if (llog == NULL)
58 rcu_assign_pointer(nf_loggers[pf], logger); 58 rcu_assign_pointer(nf_loggers[pf], logger);
59 } 59 }
60 60
61 mutex_unlock(&nf_log_mutex); 61 mutex_unlock(&nf_log_mutex);
62 62
63 return 0; 63 return 0;
64 } 64 }
65 EXPORT_SYMBOL(nf_log_register); 65 EXPORT_SYMBOL(nf_log_register);
66 66
67 void nf_log_unregister(struct nf_logger *logger) 67 void nf_log_unregister(struct nf_logger *logger)
68 { 68 {
69 const struct nf_logger *c_logger; 69 const struct nf_logger *c_logger;
70 int i; 70 int i;
71 71
72 mutex_lock(&nf_log_mutex); 72 mutex_lock(&nf_log_mutex);
73 for (i = 0; i < ARRAY_SIZE(nf_loggers); i++) { 73 for (i = 0; i < ARRAY_SIZE(nf_loggers); i++) {
74 c_logger = rcu_dereference(nf_loggers[i]); 74 c_logger = rcu_dereference(nf_loggers[i]);
75 if (c_logger == logger) 75 if (c_logger == logger)
76 rcu_assign_pointer(nf_loggers[i], NULL); 76 rcu_assign_pointer(nf_loggers[i], NULL);
77 list_del(&logger->list[i]); 77 list_del(&logger->list[i]);
78 } 78 }
79 mutex_unlock(&nf_log_mutex); 79 mutex_unlock(&nf_log_mutex);
80 80
81 synchronize_rcu(); 81 synchronize_rcu();
82 } 82 }
83 EXPORT_SYMBOL(nf_log_unregister); 83 EXPORT_SYMBOL(nf_log_unregister);
84 84
85 int nf_log_bind_pf(u_int8_t pf, const struct nf_logger *logger) 85 int nf_log_bind_pf(u_int8_t pf, const struct nf_logger *logger)
86 { 86 {
87 mutex_lock(&nf_log_mutex); 87 mutex_lock(&nf_log_mutex);
88 if (__find_logger(pf, logger->name) == NULL) { 88 if (__find_logger(pf, logger->name) == NULL) {
89 mutex_unlock(&nf_log_mutex); 89 mutex_unlock(&nf_log_mutex);
90 return -ENOENT; 90 return -ENOENT;
91 } 91 }
92 rcu_assign_pointer(nf_loggers[pf], logger); 92 rcu_assign_pointer(nf_loggers[pf], logger);
93 mutex_unlock(&nf_log_mutex); 93 mutex_unlock(&nf_log_mutex);
94 return 0; 94 return 0;
95 } 95 }
96 EXPORT_SYMBOL(nf_log_bind_pf); 96 EXPORT_SYMBOL(nf_log_bind_pf);
97 97
98 void nf_log_unbind_pf(u_int8_t pf) 98 void nf_log_unbind_pf(u_int8_t pf)
99 { 99 {
100 mutex_lock(&nf_log_mutex); 100 mutex_lock(&nf_log_mutex);
101 rcu_assign_pointer(nf_loggers[pf], NULL); 101 rcu_assign_pointer(nf_loggers[pf], NULL);
102 mutex_unlock(&nf_log_mutex); 102 mutex_unlock(&nf_log_mutex);
103 } 103 }
104 EXPORT_SYMBOL(nf_log_unbind_pf); 104 EXPORT_SYMBOL(nf_log_unbind_pf);
105 105
106 void nf_log_packet(u_int8_t pf, 106 void nf_log_packet(u_int8_t pf,
107 unsigned int hooknum, 107 unsigned int hooknum,
108 const struct sk_buff *skb, 108 const struct sk_buff *skb,
109 const struct net_device *in, 109 const struct net_device *in,
110 const struct net_device *out, 110 const struct net_device *out,
111 const struct nf_loginfo *loginfo, 111 const struct nf_loginfo *loginfo,
112 const char *fmt, ...) 112 const char *fmt, ...)
113 { 113 {
114 va_list args; 114 va_list args;
115 char prefix[NF_LOG_PREFIXLEN]; 115 char prefix[NF_LOG_PREFIXLEN];
116 const struct nf_logger *logger; 116 const struct nf_logger *logger;
117 117
118 rcu_read_lock(); 118 rcu_read_lock();
119 logger = rcu_dereference(nf_loggers[pf]); 119 logger = rcu_dereference(nf_loggers[pf]);
120 if (logger) { 120 if (logger) {
121 va_start(args, fmt); 121 va_start(args, fmt);
122 vsnprintf(prefix, sizeof(prefix), fmt, args); 122 vsnprintf(prefix, sizeof(prefix), fmt, args);
123 va_end(args); 123 va_end(args);
124 logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix); 124 logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix);
125 } 125 }
126 rcu_read_unlock(); 126 rcu_read_unlock();
127 } 127 }
128 EXPORT_SYMBOL(nf_log_packet); 128 EXPORT_SYMBOL(nf_log_packet);
129 129
130 #ifdef CONFIG_PROC_FS 130 #ifdef CONFIG_PROC_FS
131 static void *seq_start(struct seq_file *seq, loff_t *pos) 131 static void *seq_start(struct seq_file *seq, loff_t *pos)
132 __acquires(RCU) 132 __acquires(RCU)
133 { 133 {
134 rcu_read_lock(); 134 rcu_read_lock();
135 135
136 if (*pos >= ARRAY_SIZE(nf_loggers)) 136 if (*pos >= ARRAY_SIZE(nf_loggers))
137 return NULL; 137 return NULL;
138 138
139 return pos; 139 return pos;
140 } 140 }
141 141
142 static void *seq_next(struct seq_file *s, void *v, loff_t *pos) 142 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
143 { 143 {
144 (*pos)++; 144 (*pos)++;
145 145
146 if (*pos >= ARRAY_SIZE(nf_loggers)) 146 if (*pos >= ARRAY_SIZE(nf_loggers))
147 return NULL; 147 return NULL;
148 148
149 return pos; 149 return pos;
150 } 150 }
151 151
152 static void seq_stop(struct seq_file *s, void *v) 152 static void seq_stop(struct seq_file *s, void *v)
153 __releases(RCU) 153 __releases(RCU)
154 { 154 {
155 rcu_read_unlock(); 155 rcu_read_unlock();
156 } 156 }
157 157
158 static int seq_show(struct seq_file *s, void *v) 158 static int seq_show(struct seq_file *s, void *v)
159 { 159 {
160 loff_t *pos = v; 160 loff_t *pos = v;
161 const struct nf_logger *logger; 161 const struct nf_logger *logger;
162 struct nf_logger *t; 162 struct nf_logger *t;
163 int ret; 163 int ret;
164 164
165 logger = rcu_dereference(nf_loggers[*pos]); 165 logger = rcu_dereference(nf_loggers[*pos]);
166 166
167 if (!logger) 167 if (!logger)
168 ret = seq_printf(s, "%2lld NONE (", *pos); 168 ret = seq_printf(s, "%2lld NONE (", *pos);
169 else 169 else
170 ret = seq_printf(s, "%2lld %s (", *pos, logger->name); 170 ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
171 171
172 if (ret < 0) 172 if (ret < 0)
173 return ret; 173 return ret;
174 174
175 mutex_lock(&nf_log_mutex); 175 mutex_lock(&nf_log_mutex);
176 list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) { 176 list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
177 ret = seq_printf(s, "%s", t->name); 177 ret = seq_printf(s, "%s", t->name);
178 if (ret < 0) { 178 if (ret < 0) {
179 mutex_unlock(&nf_log_mutex); 179 mutex_unlock(&nf_log_mutex);
180 return ret; 180 return ret;
181 } 181 }
182 if (&t->list[*pos] != nf_loggers_l[*pos].prev) { 182 if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
183 ret = seq_printf(s, ","); 183 ret = seq_printf(s, ",");
184 if (ret < 0) { 184 if (ret < 0) {
185 mutex_unlock(&nf_log_mutex); 185 mutex_unlock(&nf_log_mutex);
186 return ret; 186 return ret;
187 } 187 }
188 } 188 }
189 } 189 }
190 mutex_unlock(&nf_log_mutex); 190 mutex_unlock(&nf_log_mutex);
191 191
192 return seq_printf(s, ")\n"); 192 return seq_printf(s, ")\n");
193 } 193 }
194 194
195 static const struct seq_operations nflog_seq_ops = { 195 static const struct seq_operations nflog_seq_ops = {
196 .start = seq_start, 196 .start = seq_start,
197 .next = seq_next, 197 .next = seq_next,
198 .stop = seq_stop, 198 .stop = seq_stop,
199 .show = seq_show, 199 .show = seq_show,
200 }; 200 };
201 201
202 static int nflog_open(struct inode *inode, struct file *file) 202 static int nflog_open(struct inode *inode, struct file *file)
203 { 203 {
204 return seq_open(file, &nflog_seq_ops); 204 return seq_open(file, &nflog_seq_ops);
205 } 205 }
206 206
207 static const struct file_operations nflog_file_ops = { 207 static const struct file_operations nflog_file_ops = {
208 .owner = THIS_MODULE, 208 .owner = THIS_MODULE,
209 .open = nflog_open, 209 .open = nflog_open,
210 .read = seq_read, 210 .read = seq_read,
211 .llseek = seq_lseek, 211 .llseek = seq_lseek,
212 .release = seq_release, 212 .release = seq_release,
213 }; 213 };
214 214
215 215
216 #endif /* PROC_FS */ 216 #endif /* PROC_FS */
217 217
218 #ifdef CONFIG_SYSCTL 218 #ifdef CONFIG_SYSCTL
219 struct ctl_path nf_log_sysctl_path[] = { 219 struct ctl_path nf_log_sysctl_path[] = {
220 { .procname = "net", .ctl_name = CTL_NET, }, 220 { .procname = "net", .ctl_name = CTL_NET, },
221 { .procname = "netfilter", .ctl_name = NET_NETFILTER, }, 221 { .procname = "netfilter", .ctl_name = NET_NETFILTER, },
222 { .procname = "nf_log", .ctl_name = CTL_UNNUMBERED, }, 222 { .procname = "nf_log", .ctl_name = CTL_UNNUMBERED, },
223 { } 223 { }
224 }; 224 };
225 225
226 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3]; 226 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
227 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1]; 227 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
228 static struct ctl_table_header *nf_log_dir_header; 228 static struct ctl_table_header *nf_log_dir_header;
229 229
230 static int nf_log_proc_dostring(ctl_table *table, int write, struct file *filp, 230 static int nf_log_proc_dostring(ctl_table *table, int write, struct file *filp,
231 void *buffer, size_t *lenp, loff_t *ppos) 231 void *buffer, size_t *lenp, loff_t *ppos)
232 { 232 {
233 const struct nf_logger *logger; 233 const struct nf_logger *logger;
234 int r = 0; 234 int r = 0;
235 int tindex = (unsigned long)table->extra1; 235 int tindex = (unsigned long)table->extra1;
236 236
237 if (write) { 237 if (write) {
238 if (!strcmp(buffer, "NONE")) { 238 if (!strcmp(buffer, "NONE")) {
239 nf_log_unbind_pf(tindex); 239 nf_log_unbind_pf(tindex);
240 return 0; 240 return 0;
241 } 241 }
242 mutex_lock(&nf_log_mutex); 242 mutex_lock(&nf_log_mutex);
243 logger = __find_logger(tindex, buffer); 243 logger = __find_logger(tindex, buffer);
244 if (logger == NULL) { 244 if (logger == NULL) {
245 mutex_unlock(&nf_log_mutex); 245 mutex_unlock(&nf_log_mutex);
246 return -ENOENT; 246 return -ENOENT;
247 } 247 }
248 rcu_assign_pointer(nf_loggers[tindex], logger); 248 rcu_assign_pointer(nf_loggers[tindex], logger);
249 mutex_unlock(&nf_log_mutex); 249 mutex_unlock(&nf_log_mutex);
250 } else { 250 } else {
251 rcu_read_lock(); 251 mutex_lock(&nf_log_mutex);
252 logger = rcu_dereference(nf_loggers[tindex]); 252 logger = nf_loggers[tindex];
253 if (!logger) 253 if (!logger)
254 table->data = "NONE"; 254 table->data = "NONE";
255 else 255 else
256 table->data = logger->name; 256 table->data = logger->name;
257 r = proc_dostring(table, write, filp, buffer, lenp, ppos); 257 r = proc_dostring(table, write, filp, buffer, lenp, ppos);
258 rcu_read_unlock(); 258 mutex_unlock(&nf_log_mutex);
259 } 259 }
260 260
261 return r; 261 return r;
262 } 262 }
263 263
264 static __init int netfilter_log_sysctl_init(void) 264 static __init int netfilter_log_sysctl_init(void)
265 { 265 {
266 int i; 266 int i;
267 267
268 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) { 268 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
269 snprintf(nf_log_sysctl_fnames[i-NFPROTO_UNSPEC], 3, "%d", i); 269 snprintf(nf_log_sysctl_fnames[i-NFPROTO_UNSPEC], 3, "%d", i);
270 nf_log_sysctl_table[i].ctl_name = CTL_UNNUMBERED; 270 nf_log_sysctl_table[i].ctl_name = CTL_UNNUMBERED;
271 nf_log_sysctl_table[i].procname = 271 nf_log_sysctl_table[i].procname =
272 nf_log_sysctl_fnames[i-NFPROTO_UNSPEC]; 272 nf_log_sysctl_fnames[i-NFPROTO_UNSPEC];
273 nf_log_sysctl_table[i].data = NULL; 273 nf_log_sysctl_table[i].data = NULL;
274 nf_log_sysctl_table[i].maxlen = 274 nf_log_sysctl_table[i].maxlen =
275 NFLOGGER_NAME_LEN * sizeof(char); 275 NFLOGGER_NAME_LEN * sizeof(char);
276 nf_log_sysctl_table[i].mode = 0644; 276 nf_log_sysctl_table[i].mode = 0644;
277 nf_log_sysctl_table[i].proc_handler = nf_log_proc_dostring; 277 nf_log_sysctl_table[i].proc_handler = nf_log_proc_dostring;
278 nf_log_sysctl_table[i].extra1 = (void *)(unsigned long) i; 278 nf_log_sysctl_table[i].extra1 = (void *)(unsigned long) i;
279 } 279 }
280 280
281 nf_log_dir_header = register_sysctl_paths(nf_log_sysctl_path, 281 nf_log_dir_header = register_sysctl_paths(nf_log_sysctl_path,
282 nf_log_sysctl_table); 282 nf_log_sysctl_table);
283 if (!nf_log_dir_header) 283 if (!nf_log_dir_header)
284 return -ENOMEM; 284 return -ENOMEM;
285 285
286 return 0; 286 return 0;
287 } 287 }
288 #else 288 #else
289 static __init int netfilter_log_sysctl_init(void) 289 static __init int netfilter_log_sysctl_init(void)
290 { 290 {
291 return 0; 291 return 0;
292 } 292 }
293 #endif /* CONFIG_SYSCTL */ 293 #endif /* CONFIG_SYSCTL */
294 294
295 int __init netfilter_log_init(void) 295 int __init netfilter_log_init(void)
296 { 296 {
297 int i, r; 297 int i, r;
298 #ifdef CONFIG_PROC_FS 298 #ifdef CONFIG_PROC_FS
299 if (!proc_create("nf_log", S_IRUGO, 299 if (!proc_create("nf_log", S_IRUGO,
300 proc_net_netfilter, &nflog_file_ops)) 300 proc_net_netfilter, &nflog_file_ops))
301 return -1; 301 return -1;
302 #endif 302 #endif
303 303
304 /* Errors will trigger panic, unroll on error is unnecessary. */ 304 /* Errors will trigger panic, unroll on error is unnecessary. */
305 r = netfilter_log_sysctl_init(); 305 r = netfilter_log_sysctl_init();
306 if (r < 0) 306 if (r < 0)
307 return r; 307 return r;
308 308
309 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) 309 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
310 INIT_LIST_HEAD(&(nf_loggers_l[i])); 310 INIT_LIST_HEAD(&(nf_loggers_l[i]));
311 311
312 return 0; 312 return 0;
313 } 313 }
314 314