filter.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Linux Socket Filter - Kernel level socket filtering
  3. *
  4. * Author:
  5. * Jay Schulist <jschlst@samba.org>
  6. *
  7. * Based on the design of:
  8. * - The Berkeley Packet Filter
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Andi Kleen - Fix a few bad bugs and races.
  16. * Kris Katterjohn - Added many additional checks in sk_chk_filter()
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/mm.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/socket.h>
  23. #include <linux/in.h>
  24. #include <linux/inet.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_packet.h>
  27. #include <net/ip.h>
  28. #include <net/protocol.h>
  29. #include <linux/skbuff.h>
  30. #include <net/sock.h>
  31. #include <linux/errno.h>
  32. #include <linux/timer.h>
  33. #include <asm/system.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/unaligned.h>
  36. #include <linux/filter.h>
  37. /* No hurry in this branch */
  38. static void *__load_pointer(struct sk_buff *skb, int k)
  39. {
  40. u8 *ptr = NULL;
  41. if (k >= SKF_NET_OFF)
  42. ptr = skb->nh.raw + k - SKF_NET_OFF;
  43. else if (k >= SKF_LL_OFF)
  44. ptr = skb->mac.raw + k - SKF_LL_OFF;
  45. if (ptr >= skb->head && ptr < skb->tail)
  46. return ptr;
  47. return NULL;
  48. }
  49. static inline void *load_pointer(struct sk_buff *skb, int k,
  50. unsigned int size, void *buffer)
  51. {
  52. if (k >= 0)
  53. return skb_header_pointer(skb, k, size, buffer);
  54. else {
  55. if (k >= SKF_AD_OFF)
  56. return NULL;
  57. return __load_pointer(skb, k);
  58. }
  59. }
  60. /**
  61. * sk_run_filter - run a filter on a socket
  62. * @skb: buffer to run the filter on
  63. * @filter: filter to apply
  64. * @flen: length of filter
  65. *
  66. * Decode and apply filter instructions to the skb->data.
  67. * Return length to keep, 0 for none. skb is the data we are
  68. * filtering, filter is the array of filter instructions, and
  69. * len is the number of filter blocks in the array.
  70. */
  71. unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
  72. {
  73. struct sock_filter *fentry; /* We walk down these */
  74. void *ptr;
  75. u32 A = 0; /* Accumulator */
  76. u32 X = 0; /* Index Register */
  77. u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
  78. u32 tmp;
  79. int k;
  80. int pc;
  81. /*
  82. * Process array of filter instructions.
  83. */
  84. for (pc = 0; pc < flen; pc++) {
  85. fentry = &filter[pc];
  86. switch (fentry->code) {
  87. case BPF_ALU|BPF_ADD|BPF_X:
  88. A += X;
  89. continue;
  90. case BPF_ALU|BPF_ADD|BPF_K:
  91. A += fentry->k;
  92. continue;
  93. case BPF_ALU|BPF_SUB|BPF_X:
  94. A -= X;
  95. continue;
  96. case BPF_ALU|BPF_SUB|BPF_K:
  97. A -= fentry->k;
  98. continue;
  99. case BPF_ALU|BPF_MUL|BPF_X:
  100. A *= X;
  101. continue;
  102. case BPF_ALU|BPF_MUL|BPF_K:
  103. A *= fentry->k;
  104. continue;
  105. case BPF_ALU|BPF_DIV|BPF_X:
  106. if (X == 0)
  107. return 0;
  108. A /= X;
  109. continue;
  110. case BPF_ALU|BPF_DIV|BPF_K:
  111. A /= fentry->k;
  112. continue;
  113. case BPF_ALU|BPF_AND|BPF_X:
  114. A &= X;
  115. continue;
  116. case BPF_ALU|BPF_AND|BPF_K:
  117. A &= fentry->k;
  118. continue;
  119. case BPF_ALU|BPF_OR|BPF_X:
  120. A |= X;
  121. continue;
  122. case BPF_ALU|BPF_OR|BPF_K:
  123. A |= fentry->k;
  124. continue;
  125. case BPF_ALU|BPF_LSH|BPF_X:
  126. A <<= X;
  127. continue;
  128. case BPF_ALU|BPF_LSH|BPF_K:
  129. A <<= fentry->k;
  130. continue;
  131. case BPF_ALU|BPF_RSH|BPF_X:
  132. A >>= X;
  133. continue;
  134. case BPF_ALU|BPF_RSH|BPF_K:
  135. A >>= fentry->k;
  136. continue;
  137. case BPF_ALU|BPF_NEG:
  138. A = -A;
  139. continue;
  140. case BPF_JMP|BPF_JA:
  141. pc += fentry->k;
  142. continue;
  143. case BPF_JMP|BPF_JGT|BPF_K:
  144. pc += (A > fentry->k) ? fentry->jt : fentry->jf;
  145. continue;
  146. case BPF_JMP|BPF_JGE|BPF_K:
  147. pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
  148. continue;
  149. case BPF_JMP|BPF_JEQ|BPF_K:
  150. pc += (A == fentry->k) ? fentry->jt : fentry->jf;
  151. continue;
  152. case BPF_JMP|BPF_JSET|BPF_K:
  153. pc += (A & fentry->k) ? fentry->jt : fentry->jf;
  154. continue;
  155. case BPF_JMP|BPF_JGT|BPF_X:
  156. pc += (A > X) ? fentry->jt : fentry->jf;
  157. continue;
  158. case BPF_JMP|BPF_JGE|BPF_X:
  159. pc += (A >= X) ? fentry->jt : fentry->jf;
  160. continue;
  161. case BPF_JMP|BPF_JEQ|BPF_X:
  162. pc += (A == X) ? fentry->jt : fentry->jf;
  163. continue;
  164. case BPF_JMP|BPF_JSET|BPF_X:
  165. pc += (A & X) ? fentry->jt : fentry->jf;
  166. continue;
  167. case BPF_LD|BPF_W|BPF_ABS:
  168. k = fentry->k;
  169. load_w:
  170. ptr = load_pointer(skb, k, 4, &tmp);
  171. if (ptr != NULL) {
  172. A = ntohl(get_unaligned((__be32 *)ptr));
  173. continue;
  174. }
  175. break;
  176. case BPF_LD|BPF_H|BPF_ABS:
  177. k = fentry->k;
  178. load_h:
  179. ptr = load_pointer(skb, k, 2, &tmp);
  180. if (ptr != NULL) {
  181. A = ntohs(get_unaligned((__be16 *)ptr));
  182. continue;
  183. }
  184. break;
  185. case BPF_LD|BPF_B|BPF_ABS:
  186. k = fentry->k;
  187. load_b:
  188. ptr = load_pointer(skb, k, 1, &tmp);
  189. if (ptr != NULL) {
  190. A = *(u8 *)ptr;
  191. continue;
  192. }
  193. break;
  194. case BPF_LD|BPF_W|BPF_LEN:
  195. A = skb->len;
  196. continue;
  197. case BPF_LDX|BPF_W|BPF_LEN:
  198. X = skb->len;
  199. continue;
  200. case BPF_LD|BPF_W|BPF_IND:
  201. k = X + fentry->k;
  202. goto load_w;
  203. case BPF_LD|BPF_H|BPF_IND:
  204. k = X + fentry->k;
  205. goto load_h;
  206. case BPF_LD|BPF_B|BPF_IND:
  207. k = X + fentry->k;
  208. goto load_b;
  209. case BPF_LDX|BPF_B|BPF_MSH:
  210. ptr = load_pointer(skb, fentry->k, 1, &tmp);
  211. if (ptr != NULL) {
  212. X = (*(u8 *)ptr & 0xf) << 2;
  213. continue;
  214. }
  215. return 0;
  216. case BPF_LD|BPF_IMM:
  217. A = fentry->k;
  218. continue;
  219. case BPF_LDX|BPF_IMM:
  220. X = fentry->k;
  221. continue;
  222. case BPF_LD|BPF_MEM:
  223. A = mem[fentry->k];
  224. continue;
  225. case BPF_LDX|BPF_MEM:
  226. X = mem[fentry->k];
  227. continue;
  228. case BPF_MISC|BPF_TAX:
  229. X = A;
  230. continue;
  231. case BPF_MISC|BPF_TXA:
  232. A = X;
  233. continue;
  234. case BPF_RET|BPF_K:
  235. return fentry->k;
  236. case BPF_RET|BPF_A:
  237. return A;
  238. case BPF_ST:
  239. mem[fentry->k] = A;
  240. continue;
  241. case BPF_STX:
  242. mem[fentry->k] = X;
  243. continue;
  244. default:
  245. WARN_ON(1);
  246. return 0;
  247. }
  248. /*
  249. * Handle ancillary data, which are impossible
  250. * (or very difficult) to get parsing packet contents.
  251. */
  252. switch (k-SKF_AD_OFF) {
  253. case SKF_AD_PROTOCOL:
  254. A = ntohs(skb->protocol);
  255. continue;
  256. case SKF_AD_PKTTYPE:
  257. A = skb->pkt_type;
  258. continue;
  259. case SKF_AD_IFINDEX:
  260. A = skb->dev->ifindex;
  261. continue;
  262. default:
  263. return 0;
  264. }
  265. }
  266. return 0;
  267. }
  268. /**
  269. * sk_chk_filter - verify socket filter code
  270. * @filter: filter to verify
  271. * @flen: length of filter
  272. *
  273. * Check the user's filter code. If we let some ugly
  274. * filter code slip through kaboom! The filter must contain
  275. * no references or jumps that are out of range, no illegal
  276. * instructions, and must end with a RET instruction.
  277. *
  278. * All jumps are forward as they are not signed.
  279. *
  280. * Returns 0 if the rule set is legal or -EINVAL if not.
  281. */
  282. int sk_chk_filter(struct sock_filter *filter, int flen)
  283. {
  284. struct sock_filter *ftest;
  285. int pc;
  286. if (flen == 0 || flen > BPF_MAXINSNS)
  287. return -EINVAL;
  288. /* check the filter code now */
  289. for (pc = 0; pc < flen; pc++) {
  290. ftest = &filter[pc];
  291. /* Only allow valid instructions */
  292. switch (ftest->code) {
  293. case BPF_ALU|BPF_ADD|BPF_K:
  294. case BPF_ALU|BPF_ADD|BPF_X:
  295. case BPF_ALU|BPF_SUB|BPF_K:
  296. case BPF_ALU|BPF_SUB|BPF_X:
  297. case BPF_ALU|BPF_MUL|BPF_K:
  298. case BPF_ALU|BPF_MUL|BPF_X:
  299. case BPF_ALU|BPF_DIV|BPF_X:
  300. case BPF_ALU|BPF_AND|BPF_K:
  301. case BPF_ALU|BPF_AND|BPF_X:
  302. case BPF_ALU|BPF_OR|BPF_K:
  303. case BPF_ALU|BPF_OR|BPF_X:
  304. case BPF_ALU|BPF_LSH|BPF_K:
  305. case BPF_ALU|BPF_LSH|BPF_X:
  306. case BPF_ALU|BPF_RSH|BPF_K:
  307. case BPF_ALU|BPF_RSH|BPF_X:
  308. case BPF_ALU|BPF_NEG:
  309. case BPF_LD|BPF_W|BPF_ABS:
  310. case BPF_LD|BPF_H|BPF_ABS:
  311. case BPF_LD|BPF_B|BPF_ABS:
  312. case BPF_LD|BPF_W|BPF_LEN:
  313. case BPF_LD|BPF_W|BPF_IND:
  314. case BPF_LD|BPF_H|BPF_IND:
  315. case BPF_LD|BPF_B|BPF_IND:
  316. case BPF_LD|BPF_IMM:
  317. case BPF_LDX|BPF_W|BPF_LEN:
  318. case BPF_LDX|BPF_B|BPF_MSH:
  319. case BPF_LDX|BPF_IMM:
  320. case BPF_MISC|BPF_TAX:
  321. case BPF_MISC|BPF_TXA:
  322. case BPF_RET|BPF_K:
  323. case BPF_RET|BPF_A:
  324. break;
  325. /* Some instructions need special checks */
  326. case BPF_ALU|BPF_DIV|BPF_K:
  327. /* check for division by zero */
  328. if (ftest->k == 0)
  329. return -EINVAL;
  330. break;
  331. case BPF_LD|BPF_MEM:
  332. case BPF_LDX|BPF_MEM:
  333. case BPF_ST:
  334. case BPF_STX:
  335. /* check for invalid memory addresses */
  336. if (ftest->k >= BPF_MEMWORDS)
  337. return -EINVAL;
  338. break;
  339. case BPF_JMP|BPF_JA:
  340. /*
  341. * Note, the large ftest->k might cause loops.
  342. * Compare this with conditional jumps below,
  343. * where offsets are limited. --ANK (981016)
  344. */
  345. if (ftest->k >= (unsigned)(flen-pc-1))
  346. return -EINVAL;
  347. break;
  348. case BPF_JMP|BPF_JEQ|BPF_K:
  349. case BPF_JMP|BPF_JEQ|BPF_X:
  350. case BPF_JMP|BPF_JGE|BPF_K:
  351. case BPF_JMP|BPF_JGE|BPF_X:
  352. case BPF_JMP|BPF_JGT|BPF_K:
  353. case BPF_JMP|BPF_JGT|BPF_X:
  354. case BPF_JMP|BPF_JSET|BPF_K:
  355. case BPF_JMP|BPF_JSET|BPF_X:
  356. /* for conditionals both must be safe */
  357. if (pc + ftest->jt + 1 >= flen ||
  358. pc + ftest->jf + 1 >= flen)
  359. return -EINVAL;
  360. break;
  361. default:
  362. return -EINVAL;
  363. }
  364. }
  365. return (BPF_CLASS(filter[flen - 1].code) == BPF_RET) ? 0 : -EINVAL;
  366. }
  367. /**
  368. * sk_attach_filter - attach a socket filter
  369. * @fprog: the filter program
  370. * @sk: the socket to use
  371. *
  372. * Attach the user's filter code. We first run some sanity checks on
  373. * it to make sure it does not explode on us later. If an error
  374. * occurs or there is insufficient memory for the filter a negative
  375. * errno code is returned. On success the return is zero.
  376. */
  377. int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
  378. {
  379. struct sk_filter *fp;
  380. unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
  381. int err;
  382. /* Make sure new filter is there and in the right amounts. */
  383. if (fprog->filter == NULL)
  384. return -EINVAL;
  385. fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
  386. if (!fp)
  387. return -ENOMEM;
  388. if (copy_from_user(fp->insns, fprog->filter, fsize)) {
  389. sock_kfree_s(sk, fp, fsize+sizeof(*fp));
  390. return -EFAULT;
  391. }
  392. atomic_set(&fp->refcnt, 1);
  393. fp->len = fprog->len;
  394. err = sk_chk_filter(fp->insns, fp->len);
  395. if (!err) {
  396. struct sk_filter *old_fp;
  397. rcu_read_lock_bh();
  398. old_fp = rcu_dereference(sk->sk_filter);
  399. rcu_assign_pointer(sk->sk_filter, fp);
  400. rcu_read_unlock_bh();
  401. fp = old_fp;
  402. }
  403. if (fp)
  404. sk_filter_release(sk, fp);
  405. return err;
  406. }
  407. EXPORT_SYMBOL(sk_chk_filter);
  408. EXPORT_SYMBOL(sk_run_filter);