ebtables.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  1. /*
  2. * ebtables
  3. *
  4. * Author:
  5. * Bart De Schuymer <bdschuym@pandora.be>
  6. *
  7. * ebtables.c,v 2.0, July, 2002
  8. *
  9. * This code is strongly inspired by the iptables code which is
  10. * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/kmod.h>
  19. #include <linux/module.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/netfilter/x_tables.h>
  22. #include <linux/netfilter_bridge/ebtables.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mutex.h>
  25. #include <linux/slab.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/smp.h>
  28. #include <linux/cpumask.h>
  29. #include <linux/audit.h>
  30. #include <net/sock.h>
  31. /* needed for logical [in,out]-dev filtering */
  32. #include "../br_private.h"
  33. /* Each cpu has its own set of counters, so there is no need for write_lock in
  34. * the softirq
  35. * For reading or updating the counters, the user context needs to
  36. * get a write_lock
  37. */
  38. /* The size of each set of counters is altered to get cache alignment */
  39. #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
  40. #define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
  41. #define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
  42. COUNTER_OFFSET(n) * cpu))
  43. static DEFINE_MUTEX(ebt_mutex);
  44. #ifdef CONFIG_COMPAT
  45. static void ebt_standard_compat_from_user(void *dst, const void *src)
  46. {
  47. int v = *(compat_int_t *)src;
  48. if (v >= 0)
  49. v += xt_compat_calc_jump(NFPROTO_BRIDGE, v);
  50. memcpy(dst, &v, sizeof(v));
  51. }
  52. static int ebt_standard_compat_to_user(void __user *dst, const void *src)
  53. {
  54. compat_int_t cv = *(int *)src;
  55. if (cv >= 0)
  56. cv -= xt_compat_calc_jump(NFPROTO_BRIDGE, cv);
  57. return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
  58. }
  59. #endif
  60. static struct xt_target ebt_standard_target = {
  61. .name = "standard",
  62. .revision = 0,
  63. .family = NFPROTO_BRIDGE,
  64. .targetsize = sizeof(int),
  65. #ifdef CONFIG_COMPAT
  66. .compatsize = sizeof(compat_int_t),
  67. .compat_from_user = ebt_standard_compat_from_user,
  68. .compat_to_user = ebt_standard_compat_to_user,
  69. #endif
  70. };
  71. static inline int
  72. ebt_do_watcher(const struct ebt_entry_watcher *w, struct sk_buff *skb,
  73. struct xt_action_param *par)
  74. {
  75. par->target = w->u.watcher;
  76. par->targinfo = w->data;
  77. w->u.watcher->target(skb, par);
  78. /* watchers don't give a verdict */
  79. return 0;
  80. }
  81. static inline int
  82. ebt_do_match(struct ebt_entry_match *m, const struct sk_buff *skb,
  83. struct xt_action_param *par)
  84. {
  85. par->match = m->u.match;
  86. par->matchinfo = m->data;
  87. return !m->u.match->match(skb, par);
  88. }
  89. static inline int
  90. ebt_dev_check(const char *entry, const struct net_device *device)
  91. {
  92. int i = 0;
  93. const char *devname;
  94. if (*entry == '\0')
  95. return 0;
  96. if (!device)
  97. return 1;
  98. devname = device->name;
  99. /* 1 is the wildcard token */
  100. while (entry[i] != '\0' && entry[i] != 1 && entry[i] == devname[i])
  101. i++;
  102. return devname[i] != entry[i] && entry[i] != 1;
  103. }
  104. /* process standard matches */
  105. static inline int
  106. ebt_basic_match(const struct ebt_entry *e, const struct sk_buff *skb,
  107. const struct net_device *in, const struct net_device *out)
  108. {
  109. const struct ethhdr *h = eth_hdr(skb);
  110. const struct net_bridge_port *p;
  111. __be16 ethproto;
  112. if (skb_vlan_tag_present(skb))
  113. ethproto = htons(ETH_P_8021Q);
  114. else
  115. ethproto = h->h_proto;
  116. if (e->bitmask & EBT_802_3) {
  117. if (NF_INVF(e, EBT_IPROTO, eth_proto_is_802_3(ethproto)))
  118. return 1;
  119. } else if (!(e->bitmask & EBT_NOPROTO) &&
  120. NF_INVF(e, EBT_IPROTO, e->ethproto != ethproto))
  121. return 1;
  122. if (NF_INVF(e, EBT_IIN, ebt_dev_check(e->in, in)))
  123. return 1;
  124. if (NF_INVF(e, EBT_IOUT, ebt_dev_check(e->out, out)))
  125. return 1;
  126. /* rcu_read_lock()ed by nf_hook_thresh */
  127. if (in && (p = br_port_get_rcu(in)) != NULL &&
  128. NF_INVF(e, EBT_ILOGICALIN,
  129. ebt_dev_check(e->logical_in, p->br->dev)))
  130. return 1;
  131. if (out && (p = br_port_get_rcu(out)) != NULL &&
  132. NF_INVF(e, EBT_ILOGICALOUT,
  133. ebt_dev_check(e->logical_out, p->br->dev)))
  134. return 1;
  135. if (e->bitmask & EBT_SOURCEMAC) {
  136. if (NF_INVF(e, EBT_ISOURCE,
  137. !ether_addr_equal_masked(h->h_source, e->sourcemac,
  138. e->sourcemsk)))
  139. return 1;
  140. }
  141. if (e->bitmask & EBT_DESTMAC) {
  142. if (NF_INVF(e, EBT_IDEST,
  143. !ether_addr_equal_masked(h->h_dest, e->destmac,
  144. e->destmsk)))
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static inline
  150. struct ebt_entry *ebt_next_entry(const struct ebt_entry *entry)
  151. {
  152. return (void *)entry + entry->next_offset;
  153. }
  154. static inline const struct ebt_entry_target *
  155. ebt_get_target_c(const struct ebt_entry *e)
  156. {
  157. return ebt_get_target((struct ebt_entry *)e);
  158. }
  159. /* Do some firewalling */
  160. unsigned int ebt_do_table(struct sk_buff *skb,
  161. const struct nf_hook_state *state,
  162. struct ebt_table *table)
  163. {
  164. unsigned int hook = state->hook;
  165. int i, nentries;
  166. struct ebt_entry *point;
  167. struct ebt_counter *counter_base, *cb_base;
  168. const struct ebt_entry_target *t;
  169. int verdict, sp = 0;
  170. struct ebt_chainstack *cs;
  171. struct ebt_entries *chaininfo;
  172. const char *base;
  173. const struct ebt_table_info *private;
  174. struct xt_action_param acpar;
  175. acpar.state = state;
  176. acpar.hotdrop = false;
  177. read_lock_bh(&table->lock);
  178. private = table->private;
  179. cb_base = COUNTER_BASE(private->counters, private->nentries,
  180. smp_processor_id());
  181. if (private->chainstack)
  182. cs = private->chainstack[smp_processor_id()];
  183. else
  184. cs = NULL;
  185. chaininfo = private->hook_entry[hook];
  186. nentries = private->hook_entry[hook]->nentries;
  187. point = (struct ebt_entry *)(private->hook_entry[hook]->data);
  188. counter_base = cb_base + private->hook_entry[hook]->counter_offset;
  189. /* base for chain jumps */
  190. base = private->entries;
  191. i = 0;
  192. while (i < nentries) {
  193. if (ebt_basic_match(point, skb, state->in, state->out))
  194. goto letscontinue;
  195. if (EBT_MATCH_ITERATE(point, ebt_do_match, skb, &acpar) != 0)
  196. goto letscontinue;
  197. if (acpar.hotdrop) {
  198. read_unlock_bh(&table->lock);
  199. return NF_DROP;
  200. }
  201. ADD_COUNTER(*(counter_base + i), skb->len, 1);
  202. /* these should only watch: not modify, nor tell us
  203. * what to do with the packet
  204. */
  205. EBT_WATCHER_ITERATE(point, ebt_do_watcher, skb, &acpar);
  206. t = ebt_get_target_c(point);
  207. /* standard target */
  208. if (!t->u.target->target)
  209. verdict = ((struct ebt_standard_target *)t)->verdict;
  210. else {
  211. acpar.target = t->u.target;
  212. acpar.targinfo = t->data;
  213. verdict = t->u.target->target(skb, &acpar);
  214. }
  215. if (verdict == EBT_ACCEPT) {
  216. read_unlock_bh(&table->lock);
  217. return NF_ACCEPT;
  218. }
  219. if (verdict == EBT_DROP) {
  220. read_unlock_bh(&table->lock);
  221. return NF_DROP;
  222. }
  223. if (verdict == EBT_RETURN) {
  224. letsreturn:
  225. if (WARN(sp == 0, "RETURN on base chain")) {
  226. /* act like this is EBT_CONTINUE */
  227. goto letscontinue;
  228. }
  229. sp--;
  230. /* put all the local variables right */
  231. i = cs[sp].n;
  232. chaininfo = cs[sp].chaininfo;
  233. nentries = chaininfo->nentries;
  234. point = cs[sp].e;
  235. counter_base = cb_base +
  236. chaininfo->counter_offset;
  237. continue;
  238. }
  239. if (verdict == EBT_CONTINUE)
  240. goto letscontinue;
  241. if (WARN(verdict < 0, "bogus standard verdict\n")) {
  242. read_unlock_bh(&table->lock);
  243. return NF_DROP;
  244. }
  245. /* jump to a udc */
  246. cs[sp].n = i + 1;
  247. cs[sp].chaininfo = chaininfo;
  248. cs[sp].e = ebt_next_entry(point);
  249. i = 0;
  250. chaininfo = (struct ebt_entries *) (base + verdict);
  251. if (WARN(chaininfo->distinguisher, "jump to non-chain\n")) {
  252. read_unlock_bh(&table->lock);
  253. return NF_DROP;
  254. }
  255. nentries = chaininfo->nentries;
  256. point = (struct ebt_entry *)chaininfo->data;
  257. counter_base = cb_base + chaininfo->counter_offset;
  258. sp++;
  259. continue;
  260. letscontinue:
  261. point = ebt_next_entry(point);
  262. i++;
  263. }
  264. /* I actually like this :) */
  265. if (chaininfo->policy == EBT_RETURN)
  266. goto letsreturn;
  267. if (chaininfo->policy == EBT_ACCEPT) {
  268. read_unlock_bh(&table->lock);
  269. return NF_ACCEPT;
  270. }
  271. read_unlock_bh(&table->lock);
  272. return NF_DROP;
  273. }
  274. /* If it succeeds, returns element and locks mutex */
  275. static inline void *
  276. find_inlist_lock_noload(struct list_head *head, const char *name, int *error,
  277. struct mutex *mutex)
  278. {
  279. struct {
  280. struct list_head list;
  281. char name[EBT_FUNCTION_MAXNAMELEN];
  282. } *e;
  283. mutex_lock(mutex);
  284. list_for_each_entry(e, head, list) {
  285. if (strcmp(e->name, name) == 0)
  286. return e;
  287. }
  288. *error = -ENOENT;
  289. mutex_unlock(mutex);
  290. return NULL;
  291. }
  292. static void *
  293. find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
  294. int *error, struct mutex *mutex)
  295. {
  296. return try_then_request_module(
  297. find_inlist_lock_noload(head, name, error, mutex),
  298. "%s%s", prefix, name);
  299. }
  300. static inline struct ebt_table *
  301. find_table_lock(struct net *net, const char *name, int *error,
  302. struct mutex *mutex)
  303. {
  304. return find_inlist_lock(&net->xt.tables[NFPROTO_BRIDGE], name,
  305. "ebtable_", error, mutex);
  306. }
  307. static inline void ebt_free_table_info(struct ebt_table_info *info)
  308. {
  309. int i;
  310. if (info->chainstack) {
  311. for_each_possible_cpu(i)
  312. vfree(info->chainstack[i]);
  313. vfree(info->chainstack);
  314. }
  315. }
  316. static inline int
  317. ebt_check_match(struct ebt_entry_match *m, struct xt_mtchk_param *par,
  318. unsigned int *cnt)
  319. {
  320. const struct ebt_entry *e = par->entryinfo;
  321. struct xt_match *match;
  322. size_t left = ((char *)e + e->watchers_offset) - (char *)m;
  323. int ret;
  324. if (left < sizeof(struct ebt_entry_match) ||
  325. left - sizeof(struct ebt_entry_match) < m->match_size)
  326. return -EINVAL;
  327. match = xt_find_match(NFPROTO_BRIDGE, m->u.name, m->u.revision);
  328. if (IS_ERR(match) || match->family != NFPROTO_BRIDGE) {
  329. if (!IS_ERR(match))
  330. module_put(match->me);
  331. request_module("ebt_%s", m->u.name);
  332. match = xt_find_match(NFPROTO_BRIDGE, m->u.name, m->u.revision);
  333. }
  334. if (IS_ERR(match))
  335. return PTR_ERR(match);
  336. m->u.match = match;
  337. par->match = match;
  338. par->matchinfo = m->data;
  339. ret = xt_check_match(par, m->match_size,
  340. e->ethproto, e->invflags & EBT_IPROTO);
  341. if (ret < 0) {
  342. module_put(match->me);
  343. return ret;
  344. }
  345. (*cnt)++;
  346. return 0;
  347. }
  348. static inline int
  349. ebt_check_watcher(struct ebt_entry_watcher *w, struct xt_tgchk_param *par,
  350. unsigned int *cnt)
  351. {
  352. const struct ebt_entry *e = par->entryinfo;
  353. struct xt_target *watcher;
  354. size_t left = ((char *)e + e->target_offset) - (char *)w;
  355. int ret;
  356. if (left < sizeof(struct ebt_entry_watcher) ||
  357. left - sizeof(struct ebt_entry_watcher) < w->watcher_size)
  358. return -EINVAL;
  359. watcher = xt_request_find_target(NFPROTO_BRIDGE, w->u.name, 0);
  360. if (IS_ERR(watcher))
  361. return PTR_ERR(watcher);
  362. if (watcher->family != NFPROTO_BRIDGE) {
  363. module_put(watcher->me);
  364. return -ENOENT;
  365. }
  366. w->u.watcher = watcher;
  367. par->target = watcher;
  368. par->targinfo = w->data;
  369. ret = xt_check_target(par, w->watcher_size,
  370. e->ethproto, e->invflags & EBT_IPROTO);
  371. if (ret < 0) {
  372. module_put(watcher->me);
  373. return ret;
  374. }
  375. (*cnt)++;
  376. return 0;
  377. }
  378. static int ebt_verify_pointers(const struct ebt_replace *repl,
  379. struct ebt_table_info *newinfo)
  380. {
  381. unsigned int limit = repl->entries_size;
  382. unsigned int valid_hooks = repl->valid_hooks;
  383. unsigned int offset = 0;
  384. int i;
  385. for (i = 0; i < NF_BR_NUMHOOKS; i++)
  386. newinfo->hook_entry[i] = NULL;
  387. newinfo->entries_size = repl->entries_size;
  388. newinfo->nentries = repl->nentries;
  389. while (offset < limit) {
  390. size_t left = limit - offset;
  391. struct ebt_entry *e = (void *)newinfo->entries + offset;
  392. if (left < sizeof(unsigned int))
  393. break;
  394. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  395. if ((valid_hooks & (1 << i)) == 0)
  396. continue;
  397. if ((char __user *)repl->hook_entry[i] ==
  398. repl->entries + offset)
  399. break;
  400. }
  401. if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
  402. if (e->bitmask != 0) {
  403. /* we make userspace set this right,
  404. * so there is no misunderstanding
  405. */
  406. return -EINVAL;
  407. }
  408. if (i != NF_BR_NUMHOOKS)
  409. newinfo->hook_entry[i] = (struct ebt_entries *)e;
  410. if (left < sizeof(struct ebt_entries))
  411. break;
  412. offset += sizeof(struct ebt_entries);
  413. } else {
  414. if (left < sizeof(struct ebt_entry))
  415. break;
  416. if (left < e->next_offset)
  417. break;
  418. if (e->next_offset < sizeof(struct ebt_entry))
  419. return -EINVAL;
  420. offset += e->next_offset;
  421. }
  422. }
  423. if (offset != limit)
  424. return -EINVAL;
  425. /* check if all valid hooks have a chain */
  426. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  427. if (!newinfo->hook_entry[i] &&
  428. (valid_hooks & (1 << i)))
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. /* this one is very careful, as it is the first function
  434. * to parse the userspace data
  435. */
  436. static inline int
  437. ebt_check_entry_size_and_hooks(const struct ebt_entry *e,
  438. const struct ebt_table_info *newinfo,
  439. unsigned int *n, unsigned int *cnt,
  440. unsigned int *totalcnt, unsigned int *udc_cnt)
  441. {
  442. int i;
  443. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  444. if ((void *)e == (void *)newinfo->hook_entry[i])
  445. break;
  446. }
  447. /* beginning of a new chain
  448. * if i == NF_BR_NUMHOOKS it must be a user defined chain
  449. */
  450. if (i != NF_BR_NUMHOOKS || !e->bitmask) {
  451. /* this checks if the previous chain has as many entries
  452. * as it said it has
  453. */
  454. if (*n != *cnt)
  455. return -EINVAL;
  456. if (((struct ebt_entries *)e)->policy != EBT_DROP &&
  457. ((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
  458. /* only RETURN from udc */
  459. if (i != NF_BR_NUMHOOKS ||
  460. ((struct ebt_entries *)e)->policy != EBT_RETURN)
  461. return -EINVAL;
  462. }
  463. if (i == NF_BR_NUMHOOKS) /* it's a user defined chain */
  464. (*udc_cnt)++;
  465. if (((struct ebt_entries *)e)->counter_offset != *totalcnt)
  466. return -EINVAL;
  467. *n = ((struct ebt_entries *)e)->nentries;
  468. *cnt = 0;
  469. return 0;
  470. }
  471. /* a plain old entry, heh */
  472. if (sizeof(struct ebt_entry) > e->watchers_offset ||
  473. e->watchers_offset > e->target_offset ||
  474. e->target_offset >= e->next_offset)
  475. return -EINVAL;
  476. /* this is not checked anywhere else */
  477. if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target))
  478. return -EINVAL;
  479. (*cnt)++;
  480. (*totalcnt)++;
  481. return 0;
  482. }
  483. struct ebt_cl_stack {
  484. struct ebt_chainstack cs;
  485. int from;
  486. unsigned int hookmask;
  487. };
  488. /* We need these positions to check that the jumps to a different part of the
  489. * entries is a jump to the beginning of a new chain.
  490. */
  491. static inline int
  492. ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
  493. unsigned int *n, struct ebt_cl_stack *udc)
  494. {
  495. int i;
  496. /* we're only interested in chain starts */
  497. if (e->bitmask)
  498. return 0;
  499. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  500. if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
  501. break;
  502. }
  503. /* only care about udc */
  504. if (i != NF_BR_NUMHOOKS)
  505. return 0;
  506. udc[*n].cs.chaininfo = (struct ebt_entries *)e;
  507. /* these initialisations are depended on later in check_chainloops() */
  508. udc[*n].cs.n = 0;
  509. udc[*n].hookmask = 0;
  510. (*n)++;
  511. return 0;
  512. }
  513. static inline int
  514. ebt_cleanup_match(struct ebt_entry_match *m, struct net *net, unsigned int *i)
  515. {
  516. struct xt_mtdtor_param par;
  517. if (i && (*i)-- == 0)
  518. return 1;
  519. par.net = net;
  520. par.match = m->u.match;
  521. par.matchinfo = m->data;
  522. par.family = NFPROTO_BRIDGE;
  523. if (par.match->destroy != NULL)
  524. par.match->destroy(&par);
  525. module_put(par.match->me);
  526. return 0;
  527. }
  528. static inline int
  529. ebt_cleanup_watcher(struct ebt_entry_watcher *w, struct net *net, unsigned int *i)
  530. {
  531. struct xt_tgdtor_param par;
  532. if (i && (*i)-- == 0)
  533. return 1;
  534. par.net = net;
  535. par.target = w->u.watcher;
  536. par.targinfo = w->data;
  537. par.family = NFPROTO_BRIDGE;
  538. if (par.target->destroy != NULL)
  539. par.target->destroy(&par);
  540. module_put(par.target->me);
  541. return 0;
  542. }
  543. static inline int
  544. ebt_cleanup_entry(struct ebt_entry *e, struct net *net, unsigned int *cnt)
  545. {
  546. struct xt_tgdtor_param par;
  547. struct ebt_entry_target *t;
  548. if (e->bitmask == 0)
  549. return 0;
  550. /* we're done */
  551. if (cnt && (*cnt)-- == 0)
  552. return 1;
  553. EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, NULL);
  554. EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, NULL);
  555. t = ebt_get_target(e);
  556. par.net = net;
  557. par.target = t->u.target;
  558. par.targinfo = t->data;
  559. par.family = NFPROTO_BRIDGE;
  560. if (par.target->destroy != NULL)
  561. par.target->destroy(&par);
  562. module_put(par.target->me);
  563. return 0;
  564. }
  565. static inline int
  566. ebt_check_entry(struct ebt_entry *e, struct net *net,
  567. const struct ebt_table_info *newinfo,
  568. const char *name, unsigned int *cnt,
  569. struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
  570. {
  571. struct ebt_entry_target *t;
  572. struct xt_target *target;
  573. unsigned int i, j, hook = 0, hookmask = 0;
  574. size_t gap;
  575. int ret;
  576. struct xt_mtchk_param mtpar;
  577. struct xt_tgchk_param tgpar;
  578. /* don't mess with the struct ebt_entries */
  579. if (e->bitmask == 0)
  580. return 0;
  581. if (e->bitmask & ~EBT_F_MASK)
  582. return -EINVAL;
  583. if (e->invflags & ~EBT_INV_MASK)
  584. return -EINVAL;
  585. if ((e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3))
  586. return -EINVAL;
  587. /* what hook do we belong to? */
  588. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  589. if (!newinfo->hook_entry[i])
  590. continue;
  591. if ((char *)newinfo->hook_entry[i] < (char *)e)
  592. hook = i;
  593. else
  594. break;
  595. }
  596. /* (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
  597. * a base chain
  598. */
  599. if (i < NF_BR_NUMHOOKS)
  600. hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
  601. else {
  602. for (i = 0; i < udc_cnt; i++)
  603. if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
  604. break;
  605. if (i == 0)
  606. hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
  607. else
  608. hookmask = cl_s[i - 1].hookmask;
  609. }
  610. i = 0;
  611. memset(&mtpar, 0, sizeof(mtpar));
  612. memset(&tgpar, 0, sizeof(tgpar));
  613. mtpar.net = tgpar.net = net;
  614. mtpar.table = tgpar.table = name;
  615. mtpar.entryinfo = tgpar.entryinfo = e;
  616. mtpar.hook_mask = tgpar.hook_mask = hookmask;
  617. mtpar.family = tgpar.family = NFPROTO_BRIDGE;
  618. ret = EBT_MATCH_ITERATE(e, ebt_check_match, &mtpar, &i);
  619. if (ret != 0)
  620. goto cleanup_matches;
  621. j = 0;
  622. ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, &tgpar, &j);
  623. if (ret != 0)
  624. goto cleanup_watchers;
  625. t = ebt_get_target(e);
  626. gap = e->next_offset - e->target_offset;
  627. target = xt_request_find_target(NFPROTO_BRIDGE, t->u.name, 0);
  628. if (IS_ERR(target)) {
  629. ret = PTR_ERR(target);
  630. goto cleanup_watchers;
  631. }
  632. /* Reject UNSPEC, xtables verdicts/return values are incompatible */
  633. if (target->family != NFPROTO_BRIDGE) {
  634. module_put(target->me);
  635. ret = -ENOENT;
  636. goto cleanup_watchers;
  637. }
  638. t->u.target = target;
  639. if (t->u.target == &ebt_standard_target) {
  640. if (gap < sizeof(struct ebt_standard_target)) {
  641. ret = -EFAULT;
  642. goto cleanup_watchers;
  643. }
  644. if (((struct ebt_standard_target *)t)->verdict <
  645. -NUM_STANDARD_TARGETS) {
  646. ret = -EFAULT;
  647. goto cleanup_watchers;
  648. }
  649. } else if (t->target_size > gap - sizeof(struct ebt_entry_target)) {
  650. module_put(t->u.target->me);
  651. ret = -EFAULT;
  652. goto cleanup_watchers;
  653. }
  654. tgpar.target = target;
  655. tgpar.targinfo = t->data;
  656. ret = xt_check_target(&tgpar, t->target_size,
  657. e->ethproto, e->invflags & EBT_IPROTO);
  658. if (ret < 0) {
  659. module_put(target->me);
  660. goto cleanup_watchers;
  661. }
  662. (*cnt)++;
  663. return 0;
  664. cleanup_watchers:
  665. EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, &j);
  666. cleanup_matches:
  667. EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, &i);
  668. return ret;
  669. }
  670. /* checks for loops and sets the hook mask for udc
  671. * the hook mask for udc tells us from which base chains the udc can be
  672. * accessed. This mask is a parameter to the check() functions of the extensions
  673. */
  674. static int check_chainloops(const struct ebt_entries *chain, struct ebt_cl_stack *cl_s,
  675. unsigned int udc_cnt, unsigned int hooknr, char *base)
  676. {
  677. int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
  678. const struct ebt_entry *e = (struct ebt_entry *)chain->data;
  679. const struct ebt_entry_target *t;
  680. while (pos < nentries || chain_nr != -1) {
  681. /* end of udc, go back one 'recursion' step */
  682. if (pos == nentries) {
  683. /* put back values of the time when this chain was called */
  684. e = cl_s[chain_nr].cs.e;
  685. if (cl_s[chain_nr].from != -1)
  686. nentries =
  687. cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
  688. else
  689. nentries = chain->nentries;
  690. pos = cl_s[chain_nr].cs.n;
  691. /* make sure we won't see a loop that isn't one */
  692. cl_s[chain_nr].cs.n = 0;
  693. chain_nr = cl_s[chain_nr].from;
  694. if (pos == nentries)
  695. continue;
  696. }
  697. t = ebt_get_target_c(e);
  698. if (strcmp(t->u.name, EBT_STANDARD_TARGET))
  699. goto letscontinue;
  700. if (e->target_offset + sizeof(struct ebt_standard_target) >
  701. e->next_offset)
  702. return -1;
  703. verdict = ((struct ebt_standard_target *)t)->verdict;
  704. if (verdict >= 0) { /* jump to another chain */
  705. struct ebt_entries *hlp2 =
  706. (struct ebt_entries *)(base + verdict);
  707. for (i = 0; i < udc_cnt; i++)
  708. if (hlp2 == cl_s[i].cs.chaininfo)
  709. break;
  710. /* bad destination or loop */
  711. if (i == udc_cnt)
  712. return -1;
  713. if (cl_s[i].cs.n)
  714. return -1;
  715. if (cl_s[i].hookmask & (1 << hooknr))
  716. goto letscontinue;
  717. /* this can't be 0, so the loop test is correct */
  718. cl_s[i].cs.n = pos + 1;
  719. pos = 0;
  720. cl_s[i].cs.e = ebt_next_entry(e);
  721. e = (struct ebt_entry *)(hlp2->data);
  722. nentries = hlp2->nentries;
  723. cl_s[i].from = chain_nr;
  724. chain_nr = i;
  725. /* this udc is accessible from the base chain for hooknr */
  726. cl_s[i].hookmask |= (1 << hooknr);
  727. continue;
  728. }
  729. letscontinue:
  730. e = ebt_next_entry(e);
  731. pos++;
  732. }
  733. return 0;
  734. }
  735. /* do the parsing of the table/chains/entries/matches/watchers/targets, heh */
  736. static int translate_table(struct net *net, const char *name,
  737. struct ebt_table_info *newinfo)
  738. {
  739. unsigned int i, j, k, udc_cnt;
  740. int ret;
  741. struct ebt_cl_stack *cl_s = NULL; /* used in the checking for chain loops */
  742. i = 0;
  743. while (i < NF_BR_NUMHOOKS && !newinfo->hook_entry[i])
  744. i++;
  745. if (i == NF_BR_NUMHOOKS)
  746. return -EINVAL;
  747. if (newinfo->hook_entry[i] != (struct ebt_entries *)newinfo->entries)
  748. return -EINVAL;
  749. /* make sure chains are ordered after each other in same order
  750. * as their corresponding hooks
  751. */
  752. for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
  753. if (!newinfo->hook_entry[j])
  754. continue;
  755. if (newinfo->hook_entry[j] <= newinfo->hook_entry[i])
  756. return -EINVAL;
  757. i = j;
  758. }
  759. /* do some early checkings and initialize some things */
  760. i = 0; /* holds the expected nr. of entries for the chain */
  761. j = 0; /* holds the up to now counted entries for the chain */
  762. k = 0; /* holds the total nr. of entries, should equal
  763. * newinfo->nentries afterwards
  764. */
  765. udc_cnt = 0; /* will hold the nr. of user defined chains (udc) */
  766. ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  767. ebt_check_entry_size_and_hooks, newinfo,
  768. &i, &j, &k, &udc_cnt);
  769. if (ret != 0)
  770. return ret;
  771. if (i != j)
  772. return -EINVAL;
  773. if (k != newinfo->nentries)
  774. return -EINVAL;
  775. /* get the location of the udc, put them in an array
  776. * while we're at it, allocate the chainstack
  777. */
  778. if (udc_cnt) {
  779. /* this will get free'd in do_replace()/ebt_register_table()
  780. * if an error occurs
  781. */
  782. newinfo->chainstack =
  783. vmalloc(array_size(nr_cpu_ids,
  784. sizeof(*(newinfo->chainstack))));
  785. if (!newinfo->chainstack)
  786. return -ENOMEM;
  787. for_each_possible_cpu(i) {
  788. newinfo->chainstack[i] =
  789. vmalloc(array_size(udc_cnt, sizeof(*(newinfo->chainstack[0]))));
  790. if (!newinfo->chainstack[i]) {
  791. while (i)
  792. vfree(newinfo->chainstack[--i]);
  793. vfree(newinfo->chainstack);
  794. newinfo->chainstack = NULL;
  795. return -ENOMEM;
  796. }
  797. }
  798. cl_s = vmalloc(array_size(udc_cnt, sizeof(*cl_s)));
  799. if (!cl_s)
  800. return -ENOMEM;
  801. i = 0; /* the i'th udc */
  802. EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  803. ebt_get_udc_positions, newinfo, &i, cl_s);
  804. /* sanity check */
  805. if (i != udc_cnt) {
  806. vfree(cl_s);
  807. return -EFAULT;
  808. }
  809. }
  810. /* Check for loops */
  811. for (i = 0; i < NF_BR_NUMHOOKS; i++)
  812. if (newinfo->hook_entry[i])
  813. if (check_chainloops(newinfo->hook_entry[i],
  814. cl_s, udc_cnt, i, newinfo->entries)) {
  815. vfree(cl_s);
  816. return -EINVAL;
  817. }
  818. /* we now know the following (along with E=mc²):
  819. * - the nr of entries in each chain is right
  820. * - the size of the allocated space is right
  821. * - all valid hooks have a corresponding chain
  822. * - there are no loops
  823. * - wrong data can still be on the level of a single entry
  824. * - could be there are jumps to places that are not the
  825. * beginning of a chain. This can only occur in chains that
  826. * are not accessible from any base chains, so we don't care.
  827. */
  828. /* used to know what we need to clean up if something goes wrong */
  829. i = 0;
  830. ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  831. ebt_check_entry, net, newinfo, name, &i, cl_s, udc_cnt);
  832. if (ret != 0) {
  833. EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  834. ebt_cleanup_entry, net, &i);
  835. }
  836. vfree(cl_s);
  837. return ret;
  838. }
  839. /* called under write_lock */
  840. static void get_counters(const struct ebt_counter *oldcounters,
  841. struct ebt_counter *counters, unsigned int nentries)
  842. {
  843. int i, cpu;
  844. struct ebt_counter *counter_base;
  845. /* counters of cpu 0 */
  846. memcpy(counters, oldcounters,
  847. sizeof(struct ebt_counter) * nentries);
  848. /* add other counters to those of cpu 0 */
  849. for_each_possible_cpu(cpu) {
  850. if (cpu == 0)
  851. continue;
  852. counter_base = COUNTER_BASE(oldcounters, nentries, cpu);
  853. for (i = 0; i < nentries; i++)
  854. ADD_COUNTER(counters[i], counter_base[i].bcnt,
  855. counter_base[i].pcnt);
  856. }
  857. }
  858. static int do_replace_finish(struct net *net, struct ebt_replace *repl,
  859. struct ebt_table_info *newinfo)
  860. {
  861. int ret;
  862. struct ebt_counter *counterstmp = NULL;
  863. /* used to be able to unlock earlier */
  864. struct ebt_table_info *table;
  865. struct ebt_table *t;
  866. /* the user wants counters back
  867. * the check on the size is done later, when we have the lock
  868. */
  869. if (repl->num_counters) {
  870. unsigned long size = repl->num_counters * sizeof(*counterstmp);
  871. counterstmp = vmalloc(size);
  872. if (!counterstmp)
  873. return -ENOMEM;
  874. }
  875. newinfo->chainstack = NULL;
  876. ret = ebt_verify_pointers(repl, newinfo);
  877. if (ret != 0)
  878. goto free_counterstmp;
  879. ret = translate_table(net, repl->name, newinfo);
  880. if (ret != 0)
  881. goto free_counterstmp;
  882. t = find_table_lock(net, repl->name, &ret, &ebt_mutex);
  883. if (!t) {
  884. ret = -ENOENT;
  885. goto free_iterate;
  886. }
  887. /* the table doesn't like it */
  888. if (t->check && (ret = t->check(newinfo, repl->valid_hooks)))
  889. goto free_unlock;
  890. if (repl->num_counters && repl->num_counters != t->private->nentries) {
  891. ret = -EINVAL;
  892. goto free_unlock;
  893. }
  894. /* we have the mutex lock, so no danger in reading this pointer */
  895. table = t->private;
  896. /* make sure the table can only be rmmod'ed if it contains no rules */
  897. if (!table->nentries && newinfo->nentries && !try_module_get(t->me)) {
  898. ret = -ENOENT;
  899. goto free_unlock;
  900. } else if (table->nentries && !newinfo->nentries)
  901. module_put(t->me);
  902. /* we need an atomic snapshot of the counters */
  903. write_lock_bh(&t->lock);
  904. if (repl->num_counters)
  905. get_counters(t->private->counters, counterstmp,
  906. t->private->nentries);
  907. t->private = newinfo;
  908. write_unlock_bh(&t->lock);
  909. mutex_unlock(&ebt_mutex);
  910. /* so, a user can change the chains while having messed up her counter
  911. * allocation. Only reason why this is done is because this way the lock
  912. * is held only once, while this doesn't bring the kernel into a
  913. * dangerous state.
  914. */
  915. if (repl->num_counters &&
  916. copy_to_user(repl->counters, counterstmp,
  917. repl->num_counters * sizeof(struct ebt_counter))) {
  918. /* Silent error, can't fail, new table is already in place */
  919. net_warn_ratelimited("ebtables: counters copy to user failed while replacing table\n");
  920. }
  921. /* decrease module count and free resources */
  922. EBT_ENTRY_ITERATE(table->entries, table->entries_size,
  923. ebt_cleanup_entry, net, NULL);
  924. vfree(table->entries);
  925. ebt_free_table_info(table);
  926. vfree(table);
  927. vfree(counterstmp);
  928. #ifdef CONFIG_AUDIT
  929. if (audit_enabled) {
  930. audit_log(audit_context(), GFP_KERNEL,
  931. AUDIT_NETFILTER_CFG,
  932. "table=%s family=%u entries=%u",
  933. repl->name, AF_BRIDGE, repl->nentries);
  934. }
  935. #endif
  936. return ret;
  937. free_unlock:
  938. mutex_unlock(&ebt_mutex);
  939. free_iterate:
  940. EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  941. ebt_cleanup_entry, net, NULL);
  942. free_counterstmp:
  943. vfree(counterstmp);
  944. /* can be initialized in translate_table() */
  945. ebt_free_table_info(newinfo);
  946. return ret;
  947. }
  948. /* replace the table */
  949. static int do_replace(struct net *net, const void __user *user,
  950. unsigned int len)
  951. {
  952. int ret, countersize;
  953. struct ebt_table_info *newinfo;
  954. struct ebt_replace tmp;
  955. if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
  956. return -EFAULT;
  957. if (len != sizeof(tmp) + tmp.entries_size)
  958. return -EINVAL;
  959. if (tmp.entries_size == 0)
  960. return -EINVAL;
  961. /* overflow check */
  962. if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
  963. NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
  964. return -ENOMEM;
  965. if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
  966. return -ENOMEM;
  967. tmp.name[sizeof(tmp.name) - 1] = 0;
  968. countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
  969. newinfo = __vmalloc(sizeof(*newinfo) + countersize, GFP_KERNEL_ACCOUNT,
  970. PAGE_KERNEL);
  971. if (!newinfo)
  972. return -ENOMEM;
  973. if (countersize)
  974. memset(newinfo->counters, 0, countersize);
  975. newinfo->entries = __vmalloc(tmp.entries_size, GFP_KERNEL_ACCOUNT,
  976. PAGE_KERNEL);
  977. if (!newinfo->entries) {
  978. ret = -ENOMEM;
  979. goto free_newinfo;
  980. }
  981. if (copy_from_user(
  982. newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
  983. ret = -EFAULT;
  984. goto free_entries;
  985. }
  986. ret = do_replace_finish(net, &tmp, newinfo);
  987. if (ret == 0)
  988. return ret;
  989. free_entries:
  990. vfree(newinfo->entries);
  991. free_newinfo:
  992. vfree(newinfo);
  993. return ret;
  994. }
  995. static void __ebt_unregister_table(struct net *net, struct ebt_table *table)
  996. {
  997. mutex_lock(&ebt_mutex);
  998. list_del(&table->list);
  999. mutex_unlock(&ebt_mutex);
  1000. EBT_ENTRY_ITERATE(table->private->entries, table->private->entries_size,
  1001. ebt_cleanup_entry, net, NULL);
  1002. if (table->private->nentries)
  1003. module_put(table->me);
  1004. vfree(table->private->entries);
  1005. ebt_free_table_info(table->private);
  1006. vfree(table->private);
  1007. kfree(table);
  1008. }
  1009. int ebt_register_table(struct net *net, const struct ebt_table *input_table,
  1010. const struct nf_hook_ops *ops, struct ebt_table **res)
  1011. {
  1012. struct ebt_table_info *newinfo;
  1013. struct ebt_table *t, *table;
  1014. struct ebt_replace_kernel *repl;
  1015. int ret, i, countersize;
  1016. void *p;
  1017. if (input_table == NULL || (repl = input_table->table) == NULL ||
  1018. repl->entries == NULL || repl->entries_size == 0 ||
  1019. repl->counters != NULL || input_table->private != NULL)
  1020. return -EINVAL;
  1021. /* Don't add one table to multiple lists. */
  1022. table = kmemdup(input_table, sizeof(struct ebt_table), GFP_KERNEL);
  1023. if (!table) {
  1024. ret = -ENOMEM;
  1025. goto out;
  1026. }
  1027. countersize = COUNTER_OFFSET(repl->nentries) * nr_cpu_ids;
  1028. newinfo = vmalloc(sizeof(*newinfo) + countersize);
  1029. ret = -ENOMEM;
  1030. if (!newinfo)
  1031. goto free_table;
  1032. p = vmalloc(repl->entries_size);
  1033. if (!p)
  1034. goto free_newinfo;
  1035. memcpy(p, repl->entries, repl->entries_size);
  1036. newinfo->entries = p;
  1037. newinfo->entries_size = repl->entries_size;
  1038. newinfo->nentries = repl->nentries;
  1039. if (countersize)
  1040. memset(newinfo->counters, 0, countersize);
  1041. /* fill in newinfo and parse the entries */
  1042. newinfo->chainstack = NULL;
  1043. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  1044. if ((repl->valid_hooks & (1 << i)) == 0)
  1045. newinfo->hook_entry[i] = NULL;
  1046. else
  1047. newinfo->hook_entry[i] = p +
  1048. ((char *)repl->hook_entry[i] - repl->entries);
  1049. }
  1050. ret = translate_table(net, repl->name, newinfo);
  1051. if (ret != 0)
  1052. goto free_chainstack;
  1053. if (table->check && table->check(newinfo, table->valid_hooks)) {
  1054. ret = -EINVAL;
  1055. goto free_chainstack;
  1056. }
  1057. table->private = newinfo;
  1058. rwlock_init(&table->lock);
  1059. mutex_lock(&ebt_mutex);
  1060. list_for_each_entry(t, &net->xt.tables[NFPROTO_BRIDGE], list) {
  1061. if (strcmp(t->name, table->name) == 0) {
  1062. ret = -EEXIST;
  1063. goto free_unlock;
  1064. }
  1065. }
  1066. /* Hold a reference count if the chains aren't empty */
  1067. if (newinfo->nentries && !try_module_get(table->me)) {
  1068. ret = -ENOENT;
  1069. goto free_unlock;
  1070. }
  1071. list_add(&table->list, &net->xt.tables[NFPROTO_BRIDGE]);
  1072. mutex_unlock(&ebt_mutex);
  1073. WRITE_ONCE(*res, table);
  1074. if (!ops)
  1075. return 0;
  1076. ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
  1077. if (ret) {
  1078. __ebt_unregister_table(net, table);
  1079. *res = NULL;
  1080. }
  1081. return ret;
  1082. free_unlock:
  1083. mutex_unlock(&ebt_mutex);
  1084. free_chainstack:
  1085. ebt_free_table_info(newinfo);
  1086. vfree(newinfo->entries);
  1087. free_newinfo:
  1088. vfree(newinfo);
  1089. free_table:
  1090. kfree(table);
  1091. out:
  1092. return ret;
  1093. }
  1094. void ebt_unregister_table(struct net *net, struct ebt_table *table,
  1095. const struct nf_hook_ops *ops)
  1096. {
  1097. if (ops)
  1098. nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
  1099. __ebt_unregister_table(net, table);
  1100. }
  1101. /* userspace just supplied us with counters */
  1102. static int do_update_counters(struct net *net, const char *name,
  1103. struct ebt_counter __user *counters,
  1104. unsigned int num_counters,
  1105. const void __user *user, unsigned int len)
  1106. {
  1107. int i, ret;
  1108. struct ebt_counter *tmp;
  1109. struct ebt_table *t;
  1110. if (num_counters == 0)
  1111. return -EINVAL;
  1112. tmp = vmalloc(array_size(num_counters, sizeof(*tmp)));
  1113. if (!tmp)
  1114. return -ENOMEM;
  1115. t = find_table_lock(net, name, &ret, &ebt_mutex);
  1116. if (!t)
  1117. goto free_tmp;
  1118. if (num_counters != t->private->nentries) {
  1119. ret = -EINVAL;
  1120. goto unlock_mutex;
  1121. }
  1122. if (copy_from_user(tmp, counters, num_counters * sizeof(*counters))) {
  1123. ret = -EFAULT;
  1124. goto unlock_mutex;
  1125. }
  1126. /* we want an atomic add of the counters */
  1127. write_lock_bh(&t->lock);
  1128. /* we add to the counters of the first cpu */
  1129. for (i = 0; i < num_counters; i++)
  1130. ADD_COUNTER(t->private->counters[i], tmp[i].bcnt, tmp[i].pcnt);
  1131. write_unlock_bh(&t->lock);
  1132. ret = 0;
  1133. unlock_mutex:
  1134. mutex_unlock(&ebt_mutex);
  1135. free_tmp:
  1136. vfree(tmp);
  1137. return ret;
  1138. }
  1139. static int update_counters(struct net *net, const void __user *user,
  1140. unsigned int len)
  1141. {
  1142. struct ebt_replace hlp;
  1143. if (copy_from_user(&hlp, user, sizeof(hlp)))
  1144. return -EFAULT;
  1145. if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
  1146. return -EINVAL;
  1147. return do_update_counters(net, hlp.name, hlp.counters,
  1148. hlp.num_counters, user, len);
  1149. }
  1150. static inline int ebt_obj_to_user(char __user *um, const char *_name,
  1151. const char *data, int entrysize,
  1152. int usersize, int datasize, u8 revision)
  1153. {
  1154. char name[EBT_EXTENSION_MAXNAMELEN] = {0};
  1155. /* ebtables expects 31 bytes long names but xt_match names are 29 bytes
  1156. * long. Copy 29 bytes and fill remaining bytes with zeroes.
  1157. */
  1158. strlcpy(name, _name, sizeof(name));
  1159. if (copy_to_user(um, name, EBT_EXTENSION_MAXNAMELEN) ||
  1160. put_user(revision, (u8 __user *)(um + EBT_EXTENSION_MAXNAMELEN)) ||
  1161. put_user(datasize, (int __user *)(um + EBT_EXTENSION_MAXNAMELEN + 1)) ||
  1162. xt_data_to_user(um + entrysize, data, usersize, datasize,
  1163. XT_ALIGN(datasize)))
  1164. return -EFAULT;
  1165. return 0;
  1166. }
  1167. static inline int ebt_match_to_user(const struct ebt_entry_match *m,
  1168. const char *base, char __user *ubase)
  1169. {
  1170. return ebt_obj_to_user(ubase + ((char *)m - base),
  1171. m->u.match->name, m->data, sizeof(*m),
  1172. m->u.match->usersize, m->match_size,
  1173. m->u.match->revision);
  1174. }
  1175. static inline int ebt_watcher_to_user(const struct ebt_entry_watcher *w,
  1176. const char *base, char __user *ubase)
  1177. {
  1178. return ebt_obj_to_user(ubase + ((char *)w - base),
  1179. w->u.watcher->name, w->data, sizeof(*w),
  1180. w->u.watcher->usersize, w->watcher_size,
  1181. w->u.watcher->revision);
  1182. }
  1183. static inline int ebt_entry_to_user(struct ebt_entry *e, const char *base,
  1184. char __user *ubase)
  1185. {
  1186. int ret;
  1187. char __user *hlp;
  1188. const struct ebt_entry_target *t;
  1189. if (e->bitmask == 0) {
  1190. /* special case !EBT_ENTRY_OR_ENTRIES */
  1191. if (copy_to_user(ubase + ((char *)e - base), e,
  1192. sizeof(struct ebt_entries)))
  1193. return -EFAULT;
  1194. return 0;
  1195. }
  1196. if (copy_to_user(ubase + ((char *)e - base), e, sizeof(*e)))
  1197. return -EFAULT;
  1198. hlp = ubase + (((char *)e + e->target_offset) - base);
  1199. t = ebt_get_target_c(e);
  1200. ret = EBT_MATCH_ITERATE(e, ebt_match_to_user, base, ubase);
  1201. if (ret != 0)
  1202. return ret;
  1203. ret = EBT_WATCHER_ITERATE(e, ebt_watcher_to_user, base, ubase);
  1204. if (ret != 0)
  1205. return ret;
  1206. ret = ebt_obj_to_user(hlp, t->u.target->name, t->data, sizeof(*t),
  1207. t->u.target->usersize, t->target_size,
  1208. t->u.target->revision);
  1209. if (ret != 0)
  1210. return ret;
  1211. return 0;
  1212. }
  1213. static int copy_counters_to_user(struct ebt_table *t,
  1214. const struct ebt_counter *oldcounters,
  1215. void __user *user, unsigned int num_counters,
  1216. unsigned int nentries)
  1217. {
  1218. struct ebt_counter *counterstmp;
  1219. int ret = 0;
  1220. /* userspace might not need the counters */
  1221. if (num_counters == 0)
  1222. return 0;
  1223. if (num_counters != nentries)
  1224. return -EINVAL;
  1225. counterstmp = vmalloc(array_size(nentries, sizeof(*counterstmp)));
  1226. if (!counterstmp)
  1227. return -ENOMEM;
  1228. write_lock_bh(&t->lock);
  1229. get_counters(oldcounters, counterstmp, nentries);
  1230. write_unlock_bh(&t->lock);
  1231. if (copy_to_user(user, counterstmp,
  1232. nentries * sizeof(struct ebt_counter)))
  1233. ret = -EFAULT;
  1234. vfree(counterstmp);
  1235. return ret;
  1236. }
  1237. /* called with ebt_mutex locked */
  1238. static int copy_everything_to_user(struct ebt_table *t, void __user *user,
  1239. const int *len, int cmd)
  1240. {
  1241. struct ebt_replace tmp;
  1242. const struct ebt_counter *oldcounters;
  1243. unsigned int entries_size, nentries;
  1244. int ret;
  1245. char *entries;
  1246. if (cmd == EBT_SO_GET_ENTRIES) {
  1247. entries_size = t->private->entries_size;
  1248. nentries = t->private->nentries;
  1249. entries = t->private->entries;
  1250. oldcounters = t->private->counters;
  1251. } else {
  1252. entries_size = t->table->entries_size;
  1253. nentries = t->table->nentries;
  1254. entries = t->table->entries;
  1255. oldcounters = t->table->counters;
  1256. }
  1257. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1258. return -EFAULT;
  1259. if (*len != sizeof(struct ebt_replace) + entries_size +
  1260. (tmp.num_counters ? nentries * sizeof(struct ebt_counter) : 0))
  1261. return -EINVAL;
  1262. if (tmp.nentries != nentries)
  1263. return -EINVAL;
  1264. if (tmp.entries_size != entries_size)
  1265. return -EINVAL;
  1266. ret = copy_counters_to_user(t, oldcounters, tmp.counters,
  1267. tmp.num_counters, nentries);
  1268. if (ret)
  1269. return ret;
  1270. /* set the match/watcher/target names right */
  1271. return EBT_ENTRY_ITERATE(entries, entries_size,
  1272. ebt_entry_to_user, entries, tmp.entries);
  1273. }
  1274. static int do_ebt_set_ctl(struct sock *sk,
  1275. int cmd, void __user *user, unsigned int len)
  1276. {
  1277. int ret;
  1278. struct net *net = sock_net(sk);
  1279. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1280. return -EPERM;
  1281. switch (cmd) {
  1282. case EBT_SO_SET_ENTRIES:
  1283. ret = do_replace(net, user, len);
  1284. break;
  1285. case EBT_SO_SET_COUNTERS:
  1286. ret = update_counters(net, user, len);
  1287. break;
  1288. default:
  1289. ret = -EINVAL;
  1290. }
  1291. return ret;
  1292. }
  1293. static int do_ebt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
  1294. {
  1295. int ret;
  1296. struct ebt_replace tmp;
  1297. struct ebt_table *t;
  1298. struct net *net = sock_net(sk);
  1299. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1300. return -EPERM;
  1301. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1302. return -EFAULT;
  1303. tmp.name[sizeof(tmp.name) - 1] = '\0';
  1304. t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
  1305. if (!t)
  1306. return ret;
  1307. switch (cmd) {
  1308. case EBT_SO_GET_INFO:
  1309. case EBT_SO_GET_INIT_INFO:
  1310. if (*len != sizeof(struct ebt_replace)) {
  1311. ret = -EINVAL;
  1312. mutex_unlock(&ebt_mutex);
  1313. break;
  1314. }
  1315. if (cmd == EBT_SO_GET_INFO) {
  1316. tmp.nentries = t->private->nentries;
  1317. tmp.entries_size = t->private->entries_size;
  1318. tmp.valid_hooks = t->valid_hooks;
  1319. } else {
  1320. tmp.nentries = t->table->nentries;
  1321. tmp.entries_size = t->table->entries_size;
  1322. tmp.valid_hooks = t->table->valid_hooks;
  1323. }
  1324. mutex_unlock(&ebt_mutex);
  1325. if (copy_to_user(user, &tmp, *len) != 0) {
  1326. ret = -EFAULT;
  1327. break;
  1328. }
  1329. ret = 0;
  1330. break;
  1331. case EBT_SO_GET_ENTRIES:
  1332. case EBT_SO_GET_INIT_ENTRIES:
  1333. ret = copy_everything_to_user(t, user, len, cmd);
  1334. mutex_unlock(&ebt_mutex);
  1335. break;
  1336. default:
  1337. mutex_unlock(&ebt_mutex);
  1338. ret = -EINVAL;
  1339. }
  1340. return ret;
  1341. }
  1342. #ifdef CONFIG_COMPAT
  1343. /* 32 bit-userspace compatibility definitions. */
  1344. struct compat_ebt_replace {
  1345. char name[EBT_TABLE_MAXNAMELEN];
  1346. compat_uint_t valid_hooks;
  1347. compat_uint_t nentries;
  1348. compat_uint_t entries_size;
  1349. /* start of the chains */
  1350. compat_uptr_t hook_entry[NF_BR_NUMHOOKS];
  1351. /* nr of counters userspace expects back */
  1352. compat_uint_t num_counters;
  1353. /* where the kernel will put the old counters. */
  1354. compat_uptr_t counters;
  1355. compat_uptr_t entries;
  1356. };
  1357. /* struct ebt_entry_match, _target and _watcher have same layout */
  1358. struct compat_ebt_entry_mwt {
  1359. union {
  1360. struct {
  1361. char name[EBT_EXTENSION_MAXNAMELEN];
  1362. u8 revision;
  1363. };
  1364. compat_uptr_t ptr;
  1365. } u;
  1366. compat_uint_t match_size;
  1367. compat_uint_t data[0] __attribute__ ((aligned (__alignof__(struct compat_ebt_replace))));
  1368. };
  1369. /* account for possible padding between match_size and ->data */
  1370. static int ebt_compat_entry_padsize(void)
  1371. {
  1372. BUILD_BUG_ON(sizeof(struct ebt_entry_match) <
  1373. sizeof(struct compat_ebt_entry_mwt));
  1374. return (int) sizeof(struct ebt_entry_match) -
  1375. sizeof(struct compat_ebt_entry_mwt);
  1376. }
  1377. static int ebt_compat_match_offset(const struct xt_match *match,
  1378. unsigned int userlen)
  1379. {
  1380. /* ebt_among needs special handling. The kernel .matchsize is
  1381. * set to -1 at registration time; at runtime an EBT_ALIGN()ed
  1382. * value is expected.
  1383. * Example: userspace sends 4500, ebt_among.c wants 4504.
  1384. */
  1385. if (unlikely(match->matchsize == -1))
  1386. return XT_ALIGN(userlen) - COMPAT_XT_ALIGN(userlen);
  1387. return xt_compat_match_offset(match);
  1388. }
  1389. static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr,
  1390. unsigned int *size)
  1391. {
  1392. const struct xt_match *match = m->u.match;
  1393. struct compat_ebt_entry_mwt __user *cm = *dstptr;
  1394. int off = ebt_compat_match_offset(match, m->match_size);
  1395. compat_uint_t msize = m->match_size - off;
  1396. if (WARN_ON(off >= m->match_size))
  1397. return -EINVAL;
  1398. if (copy_to_user(cm->u.name, match->name, strlen(match->name) + 1) ||
  1399. put_user(match->revision, &cm->u.revision) ||
  1400. put_user(msize, &cm->match_size))
  1401. return -EFAULT;
  1402. if (match->compat_to_user) {
  1403. if (match->compat_to_user(cm->data, m->data))
  1404. return -EFAULT;
  1405. } else {
  1406. if (xt_data_to_user(cm->data, m->data, match->usersize, msize,
  1407. COMPAT_XT_ALIGN(msize)))
  1408. return -EFAULT;
  1409. }
  1410. *size -= ebt_compat_entry_padsize() + off;
  1411. *dstptr = cm->data;
  1412. *dstptr += msize;
  1413. return 0;
  1414. }
  1415. static int compat_target_to_user(struct ebt_entry_target *t,
  1416. void __user **dstptr,
  1417. unsigned int *size)
  1418. {
  1419. const struct xt_target *target = t->u.target;
  1420. struct compat_ebt_entry_mwt __user *cm = *dstptr;
  1421. int off = xt_compat_target_offset(target);
  1422. compat_uint_t tsize = t->target_size - off;
  1423. if (WARN_ON(off >= t->target_size))
  1424. return -EINVAL;
  1425. if (copy_to_user(cm->u.name, target->name, strlen(target->name) + 1) ||
  1426. put_user(target->revision, &cm->u.revision) ||
  1427. put_user(tsize, &cm->match_size))
  1428. return -EFAULT;
  1429. if (target->compat_to_user) {
  1430. if (target->compat_to_user(cm->data, t->data))
  1431. return -EFAULT;
  1432. } else {
  1433. if (xt_data_to_user(cm->data, t->data, target->usersize, tsize,
  1434. COMPAT_XT_ALIGN(tsize)))
  1435. return -EFAULT;
  1436. }
  1437. *size -= ebt_compat_entry_padsize() + off;
  1438. *dstptr = cm->data;
  1439. *dstptr += tsize;
  1440. return 0;
  1441. }
  1442. static int compat_watcher_to_user(struct ebt_entry_watcher *w,
  1443. void __user **dstptr,
  1444. unsigned int *size)
  1445. {
  1446. return compat_target_to_user((struct ebt_entry_target *)w,
  1447. dstptr, size);
  1448. }
  1449. static int compat_copy_entry_to_user(struct ebt_entry *e, void __user **dstptr,
  1450. unsigned int *size)
  1451. {
  1452. struct ebt_entry_target *t;
  1453. struct ebt_entry __user *ce;
  1454. u32 watchers_offset, target_offset, next_offset;
  1455. compat_uint_t origsize;
  1456. int ret;
  1457. if (e->bitmask == 0) {
  1458. if (*size < sizeof(struct ebt_entries))
  1459. return -EINVAL;
  1460. if (copy_to_user(*dstptr, e, sizeof(struct ebt_entries)))
  1461. return -EFAULT;
  1462. *dstptr += sizeof(struct ebt_entries);
  1463. *size -= sizeof(struct ebt_entries);
  1464. return 0;
  1465. }
  1466. if (*size < sizeof(*ce))
  1467. return -EINVAL;
  1468. ce = *dstptr;
  1469. if (copy_to_user(ce, e, sizeof(*ce)))
  1470. return -EFAULT;
  1471. origsize = *size;
  1472. *dstptr += sizeof(*ce);
  1473. ret = EBT_MATCH_ITERATE(e, compat_match_to_user, dstptr, size);
  1474. if (ret)
  1475. return ret;
  1476. watchers_offset = e->watchers_offset - (origsize - *size);
  1477. ret = EBT_WATCHER_ITERATE(e, compat_watcher_to_user, dstptr, size);
  1478. if (ret)
  1479. return ret;
  1480. target_offset = e->target_offset - (origsize - *size);
  1481. t = ebt_get_target(e);
  1482. ret = compat_target_to_user(t, dstptr, size);
  1483. if (ret)
  1484. return ret;
  1485. next_offset = e->next_offset - (origsize - *size);
  1486. if (put_user(watchers_offset, &ce->watchers_offset) ||
  1487. put_user(target_offset, &ce->target_offset) ||
  1488. put_user(next_offset, &ce->next_offset))
  1489. return -EFAULT;
  1490. *size -= sizeof(*ce);
  1491. return 0;
  1492. }
  1493. static int compat_calc_match(struct ebt_entry_match *m, int *off)
  1494. {
  1495. *off += ebt_compat_match_offset(m->u.match, m->match_size);
  1496. *off += ebt_compat_entry_padsize();
  1497. return 0;
  1498. }
  1499. static int compat_calc_watcher(struct ebt_entry_watcher *w, int *off)
  1500. {
  1501. *off += xt_compat_target_offset(w->u.watcher);
  1502. *off += ebt_compat_entry_padsize();
  1503. return 0;
  1504. }
  1505. static int compat_calc_entry(const struct ebt_entry *e,
  1506. const struct ebt_table_info *info,
  1507. const void *base,
  1508. struct compat_ebt_replace *newinfo)
  1509. {
  1510. const struct ebt_entry_target *t;
  1511. unsigned int entry_offset;
  1512. int off, ret, i;
  1513. if (e->bitmask == 0)
  1514. return 0;
  1515. off = 0;
  1516. entry_offset = (void *)e - base;
  1517. EBT_MATCH_ITERATE(e, compat_calc_match, &off);
  1518. EBT_WATCHER_ITERATE(e, compat_calc_watcher, &off);
  1519. t = ebt_get_target_c(e);
  1520. off += xt_compat_target_offset(t->u.target);
  1521. off += ebt_compat_entry_padsize();
  1522. newinfo->entries_size -= off;
  1523. ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, off);
  1524. if (ret)
  1525. return ret;
  1526. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  1527. const void *hookptr = info->hook_entry[i];
  1528. if (info->hook_entry[i] &&
  1529. (e < (struct ebt_entry *)(base - hookptr))) {
  1530. newinfo->hook_entry[i] -= off;
  1531. pr_debug("0x%08X -> 0x%08X\n",
  1532. newinfo->hook_entry[i] + off,
  1533. newinfo->hook_entry[i]);
  1534. }
  1535. }
  1536. return 0;
  1537. }
  1538. static int ebt_compat_init_offsets(unsigned int number)
  1539. {
  1540. if (number > INT_MAX)
  1541. return -EINVAL;
  1542. /* also count the base chain policies */
  1543. number += NF_BR_NUMHOOKS;
  1544. return xt_compat_init_offsets(NFPROTO_BRIDGE, number);
  1545. }
  1546. static int compat_table_info(const struct ebt_table_info *info,
  1547. struct compat_ebt_replace *newinfo)
  1548. {
  1549. unsigned int size = info->entries_size;
  1550. const void *entries = info->entries;
  1551. int ret;
  1552. newinfo->entries_size = size;
  1553. ret = ebt_compat_init_offsets(info->nentries);
  1554. if (ret)
  1555. return ret;
  1556. return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info,
  1557. entries, newinfo);
  1558. }
  1559. static int compat_copy_everything_to_user(struct ebt_table *t,
  1560. void __user *user, int *len, int cmd)
  1561. {
  1562. struct compat_ebt_replace repl, tmp;
  1563. struct ebt_counter *oldcounters;
  1564. struct ebt_table_info tinfo;
  1565. int ret;
  1566. void __user *pos;
  1567. memset(&tinfo, 0, sizeof(tinfo));
  1568. if (cmd == EBT_SO_GET_ENTRIES) {
  1569. tinfo.entries_size = t->private->entries_size;
  1570. tinfo.nentries = t->private->nentries;
  1571. tinfo.entries = t->private->entries;
  1572. oldcounters = t->private->counters;
  1573. } else {
  1574. tinfo.entries_size = t->table->entries_size;
  1575. tinfo.nentries = t->table->nentries;
  1576. tinfo.entries = t->table->entries;
  1577. oldcounters = t->table->counters;
  1578. }
  1579. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1580. return -EFAULT;
  1581. if (tmp.nentries != tinfo.nentries ||
  1582. (tmp.num_counters && tmp.num_counters != tinfo.nentries))
  1583. return -EINVAL;
  1584. memcpy(&repl, &tmp, sizeof(repl));
  1585. if (cmd == EBT_SO_GET_ENTRIES)
  1586. ret = compat_table_info(t->private, &repl);
  1587. else
  1588. ret = compat_table_info(&tinfo, &repl);
  1589. if (ret)
  1590. return ret;
  1591. if (*len != sizeof(tmp) + repl.entries_size +
  1592. (tmp.num_counters? tinfo.nentries * sizeof(struct ebt_counter): 0)) {
  1593. pr_err("wrong size: *len %d, entries_size %u, replsz %d\n",
  1594. *len, tinfo.entries_size, repl.entries_size);
  1595. return -EINVAL;
  1596. }
  1597. /* userspace might not need the counters */
  1598. ret = copy_counters_to_user(t, oldcounters, compat_ptr(tmp.counters),
  1599. tmp.num_counters, tinfo.nentries);
  1600. if (ret)
  1601. return ret;
  1602. pos = compat_ptr(tmp.entries);
  1603. return EBT_ENTRY_ITERATE(tinfo.entries, tinfo.entries_size,
  1604. compat_copy_entry_to_user, &pos, &tmp.entries_size);
  1605. }
  1606. struct ebt_entries_buf_state {
  1607. char *buf_kern_start; /* kernel buffer to copy (translated) data to */
  1608. u32 buf_kern_len; /* total size of kernel buffer */
  1609. u32 buf_kern_offset; /* amount of data copied so far */
  1610. u32 buf_user_offset; /* read position in userspace buffer */
  1611. };
  1612. static int ebt_buf_count(struct ebt_entries_buf_state *state, unsigned int sz)
  1613. {
  1614. state->buf_kern_offset += sz;
  1615. return state->buf_kern_offset >= sz ? 0 : -EINVAL;
  1616. }
  1617. static int ebt_buf_add(struct ebt_entries_buf_state *state,
  1618. const void *data, unsigned int sz)
  1619. {
  1620. if (state->buf_kern_start == NULL)
  1621. goto count_only;
  1622. if (WARN_ON(state->buf_kern_offset + sz > state->buf_kern_len))
  1623. return -EINVAL;
  1624. memcpy(state->buf_kern_start + state->buf_kern_offset, data, sz);
  1625. count_only:
  1626. state->buf_user_offset += sz;
  1627. return ebt_buf_count(state, sz);
  1628. }
  1629. static int ebt_buf_add_pad(struct ebt_entries_buf_state *state, unsigned int sz)
  1630. {
  1631. char *b = state->buf_kern_start;
  1632. if (WARN_ON(b && state->buf_kern_offset > state->buf_kern_len))
  1633. return -EINVAL;
  1634. if (b != NULL && sz > 0)
  1635. memset(b + state->buf_kern_offset, 0, sz);
  1636. /* do not adjust ->buf_user_offset here, we added kernel-side padding */
  1637. return ebt_buf_count(state, sz);
  1638. }
  1639. enum compat_mwt {
  1640. EBT_COMPAT_MATCH,
  1641. EBT_COMPAT_WATCHER,
  1642. EBT_COMPAT_TARGET,
  1643. };
  1644. static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
  1645. enum compat_mwt compat_mwt,
  1646. struct ebt_entries_buf_state *state,
  1647. const unsigned char *base)
  1648. {
  1649. char name[EBT_EXTENSION_MAXNAMELEN];
  1650. struct xt_match *match;
  1651. struct xt_target *wt;
  1652. void *dst = NULL;
  1653. int off, pad = 0;
  1654. unsigned int size_kern, match_size = mwt->match_size;
  1655. if (strscpy(name, mwt->u.name, sizeof(name)) < 0)
  1656. return -EINVAL;
  1657. if (state->buf_kern_start)
  1658. dst = state->buf_kern_start + state->buf_kern_offset;
  1659. switch (compat_mwt) {
  1660. case EBT_COMPAT_MATCH:
  1661. match = xt_request_find_match(NFPROTO_BRIDGE, name,
  1662. mwt->u.revision);
  1663. if (IS_ERR(match))
  1664. return PTR_ERR(match);
  1665. off = ebt_compat_match_offset(match, match_size);
  1666. if (dst) {
  1667. if (match->compat_from_user)
  1668. match->compat_from_user(dst, mwt->data);
  1669. else
  1670. memcpy(dst, mwt->data, match_size);
  1671. }
  1672. size_kern = match->matchsize;
  1673. if (unlikely(size_kern == -1))
  1674. size_kern = match_size;
  1675. module_put(match->me);
  1676. break;
  1677. case EBT_COMPAT_WATCHER: /* fallthrough */
  1678. case EBT_COMPAT_TARGET:
  1679. wt = xt_request_find_target(NFPROTO_BRIDGE, name,
  1680. mwt->u.revision);
  1681. if (IS_ERR(wt))
  1682. return PTR_ERR(wt);
  1683. off = xt_compat_target_offset(wt);
  1684. if (dst) {
  1685. if (wt->compat_from_user)
  1686. wt->compat_from_user(dst, mwt->data);
  1687. else
  1688. memcpy(dst, mwt->data, match_size);
  1689. }
  1690. size_kern = wt->targetsize;
  1691. module_put(wt->me);
  1692. break;
  1693. default:
  1694. return -EINVAL;
  1695. }
  1696. state->buf_kern_offset += match_size + off;
  1697. state->buf_user_offset += match_size;
  1698. pad = XT_ALIGN(size_kern) - size_kern;
  1699. if (pad > 0 && dst) {
  1700. if (WARN_ON(state->buf_kern_len <= pad))
  1701. return -EINVAL;
  1702. if (WARN_ON(state->buf_kern_offset - (match_size + off) + size_kern > state->buf_kern_len - pad))
  1703. return -EINVAL;
  1704. memset(dst + size_kern, 0, pad);
  1705. }
  1706. return off + match_size;
  1707. }
  1708. /* return size of all matches, watchers or target, including necessary
  1709. * alignment and padding.
  1710. */
  1711. static int ebt_size_mwt(const struct compat_ebt_entry_mwt *match32,
  1712. unsigned int size_left, enum compat_mwt type,
  1713. struct ebt_entries_buf_state *state, const void *base)
  1714. {
  1715. const char *buf = (const char *)match32;
  1716. int growth = 0;
  1717. if (size_left == 0)
  1718. return 0;
  1719. do {
  1720. struct ebt_entry_match *match_kern;
  1721. int ret;
  1722. if (size_left < sizeof(*match32))
  1723. return -EINVAL;
  1724. match_kern = (struct ebt_entry_match *) state->buf_kern_start;
  1725. if (match_kern) {
  1726. char *tmp;
  1727. tmp = state->buf_kern_start + state->buf_kern_offset;
  1728. match_kern = (struct ebt_entry_match *) tmp;
  1729. }
  1730. ret = ebt_buf_add(state, buf, sizeof(*match32));
  1731. if (ret < 0)
  1732. return ret;
  1733. size_left -= sizeof(*match32);
  1734. /* add padding before match->data (if any) */
  1735. ret = ebt_buf_add_pad(state, ebt_compat_entry_padsize());
  1736. if (ret < 0)
  1737. return ret;
  1738. if (match32->match_size > size_left)
  1739. return -EINVAL;
  1740. size_left -= match32->match_size;
  1741. ret = compat_mtw_from_user(match32, type, state, base);
  1742. if (ret < 0)
  1743. return ret;
  1744. if (WARN_ON(ret < match32->match_size))
  1745. return -EINVAL;
  1746. growth += ret - match32->match_size;
  1747. growth += ebt_compat_entry_padsize();
  1748. buf += sizeof(*match32);
  1749. buf += match32->match_size;
  1750. if (match_kern)
  1751. match_kern->match_size = ret;
  1752. match32 = (struct compat_ebt_entry_mwt *) buf;
  1753. } while (size_left);
  1754. return growth;
  1755. }
  1756. /* called for all ebt_entry structures. */
  1757. static int size_entry_mwt(const struct ebt_entry *entry, const unsigned char *base,
  1758. unsigned int *total,
  1759. struct ebt_entries_buf_state *state)
  1760. {
  1761. unsigned int i, j, startoff, next_expected_off, new_offset = 0;
  1762. /* stores match/watchers/targets & offset of next struct ebt_entry: */
  1763. unsigned int offsets[4];
  1764. unsigned int *offsets_update = NULL;
  1765. int ret;
  1766. char *buf_start;
  1767. if (*total < sizeof(struct ebt_entries))
  1768. return -EINVAL;
  1769. if (!entry->bitmask) {
  1770. *total -= sizeof(struct ebt_entries);
  1771. return ebt_buf_add(state, entry, sizeof(struct ebt_entries));
  1772. }
  1773. if (*total < sizeof(*entry) || entry->next_offset < sizeof(*entry))
  1774. return -EINVAL;
  1775. startoff = state->buf_user_offset;
  1776. /* pull in most part of ebt_entry, it does not need to be changed. */
  1777. ret = ebt_buf_add(state, entry,
  1778. offsetof(struct ebt_entry, watchers_offset));
  1779. if (ret < 0)
  1780. return ret;
  1781. offsets[0] = sizeof(struct ebt_entry); /* matches come first */
  1782. memcpy(&offsets[1], &entry->watchers_offset,
  1783. sizeof(offsets) - sizeof(offsets[0]));
  1784. if (state->buf_kern_start) {
  1785. buf_start = state->buf_kern_start + state->buf_kern_offset;
  1786. offsets_update = (unsigned int *) buf_start;
  1787. }
  1788. ret = ebt_buf_add(state, &offsets[1],
  1789. sizeof(offsets) - sizeof(offsets[0]));
  1790. if (ret < 0)
  1791. return ret;
  1792. buf_start = (char *) entry;
  1793. /* 0: matches offset, always follows ebt_entry.
  1794. * 1: watchers offset, from ebt_entry structure
  1795. * 2: target offset, from ebt_entry structure
  1796. * 3: next ebt_entry offset, from ebt_entry structure
  1797. *
  1798. * offsets are relative to beginning of struct ebt_entry (i.e., 0).
  1799. */
  1800. for (i = 0; i < 4 ; ++i) {
  1801. if (offsets[i] > *total)
  1802. return -EINVAL;
  1803. if (i < 3 && offsets[i] == *total)
  1804. return -EINVAL;
  1805. if (i == 0)
  1806. continue;
  1807. if (offsets[i-1] > offsets[i])
  1808. return -EINVAL;
  1809. }
  1810. for (i = 0, j = 1 ; j < 4 ; j++, i++) {
  1811. struct compat_ebt_entry_mwt *match32;
  1812. unsigned int size;
  1813. char *buf = buf_start + offsets[i];
  1814. if (offsets[i] > offsets[j])
  1815. return -EINVAL;
  1816. match32 = (struct compat_ebt_entry_mwt *) buf;
  1817. size = offsets[j] - offsets[i];
  1818. ret = ebt_size_mwt(match32, size, i, state, base);
  1819. if (ret < 0)
  1820. return ret;
  1821. new_offset += ret;
  1822. if (offsets_update && new_offset) {
  1823. pr_debug("change offset %d to %d\n",
  1824. offsets_update[i], offsets[j] + new_offset);
  1825. offsets_update[i] = offsets[j] + new_offset;
  1826. }
  1827. }
  1828. if (state->buf_kern_start == NULL) {
  1829. unsigned int offset = buf_start - (char *) base;
  1830. ret = xt_compat_add_offset(NFPROTO_BRIDGE, offset, new_offset);
  1831. if (ret < 0)
  1832. return ret;
  1833. }
  1834. next_expected_off = state->buf_user_offset - startoff;
  1835. if (next_expected_off != entry->next_offset)
  1836. return -EINVAL;
  1837. if (*total < entry->next_offset)
  1838. return -EINVAL;
  1839. *total -= entry->next_offset;
  1840. return 0;
  1841. }
  1842. /* repl->entries_size is the size of the ebt_entry blob in userspace.
  1843. * It might need more memory when copied to a 64 bit kernel in case
  1844. * userspace is 32-bit. So, first task: find out how much memory is needed.
  1845. *
  1846. * Called before validation is performed.
  1847. */
  1848. static int compat_copy_entries(unsigned char *data, unsigned int size_user,
  1849. struct ebt_entries_buf_state *state)
  1850. {
  1851. unsigned int size_remaining = size_user;
  1852. int ret;
  1853. ret = EBT_ENTRY_ITERATE(data, size_user, size_entry_mwt, data,
  1854. &size_remaining, state);
  1855. if (ret < 0)
  1856. return ret;
  1857. if (size_remaining)
  1858. return -EINVAL;
  1859. return state->buf_kern_offset;
  1860. }
  1861. static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl,
  1862. void __user *user, unsigned int len)
  1863. {
  1864. struct compat_ebt_replace tmp;
  1865. int i;
  1866. if (len < sizeof(tmp))
  1867. return -EINVAL;
  1868. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1869. return -EFAULT;
  1870. if (len != sizeof(tmp) + tmp.entries_size)
  1871. return -EINVAL;
  1872. if (tmp.entries_size == 0)
  1873. return -EINVAL;
  1874. if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
  1875. NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
  1876. return -ENOMEM;
  1877. if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
  1878. return -ENOMEM;
  1879. memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry));
  1880. /* starting with hook_entry, 32 vs. 64 bit structures are different */
  1881. for (i = 0; i < NF_BR_NUMHOOKS; i++)
  1882. repl->hook_entry[i] = compat_ptr(tmp.hook_entry[i]);
  1883. repl->num_counters = tmp.num_counters;
  1884. repl->counters = compat_ptr(tmp.counters);
  1885. repl->entries = compat_ptr(tmp.entries);
  1886. return 0;
  1887. }
  1888. static int compat_do_replace(struct net *net, void __user *user,
  1889. unsigned int len)
  1890. {
  1891. int ret, i, countersize, size64;
  1892. struct ebt_table_info *newinfo;
  1893. struct ebt_replace tmp;
  1894. struct ebt_entries_buf_state state;
  1895. void *entries_tmp;
  1896. ret = compat_copy_ebt_replace_from_user(&tmp, user, len);
  1897. if (ret) {
  1898. /* try real handler in case userland supplied needed padding */
  1899. if (ret == -EINVAL && do_replace(net, user, len) == 0)
  1900. ret = 0;
  1901. return ret;
  1902. }
  1903. countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
  1904. newinfo = vmalloc(sizeof(*newinfo) + countersize);
  1905. if (!newinfo)
  1906. return -ENOMEM;
  1907. if (countersize)
  1908. memset(newinfo->counters, 0, countersize);
  1909. memset(&state, 0, sizeof(state));
  1910. newinfo->entries = vmalloc(tmp.entries_size);
  1911. if (!newinfo->entries) {
  1912. ret = -ENOMEM;
  1913. goto free_newinfo;
  1914. }
  1915. if (copy_from_user(
  1916. newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
  1917. ret = -EFAULT;
  1918. goto free_entries;
  1919. }
  1920. entries_tmp = newinfo->entries;
  1921. xt_compat_lock(NFPROTO_BRIDGE);
  1922. ret = ebt_compat_init_offsets(tmp.nentries);
  1923. if (ret < 0)
  1924. goto out_unlock;
  1925. ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
  1926. if (ret < 0)
  1927. goto out_unlock;
  1928. pr_debug("tmp.entries_size %d, kern off %d, user off %d delta %d\n",
  1929. tmp.entries_size, state.buf_kern_offset, state.buf_user_offset,
  1930. xt_compat_calc_jump(NFPROTO_BRIDGE, tmp.entries_size));
  1931. size64 = ret;
  1932. newinfo->entries = vmalloc(size64);
  1933. if (!newinfo->entries) {
  1934. vfree(entries_tmp);
  1935. ret = -ENOMEM;
  1936. goto out_unlock;
  1937. }
  1938. memset(&state, 0, sizeof(state));
  1939. state.buf_kern_start = newinfo->entries;
  1940. state.buf_kern_len = size64;
  1941. ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
  1942. if (WARN_ON(ret < 0)) {
  1943. vfree(entries_tmp);
  1944. goto out_unlock;
  1945. }
  1946. vfree(entries_tmp);
  1947. tmp.entries_size = size64;
  1948. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  1949. char __user *usrptr;
  1950. if (tmp.hook_entry[i]) {
  1951. unsigned int delta;
  1952. usrptr = (char __user *) tmp.hook_entry[i];
  1953. delta = usrptr - tmp.entries;
  1954. usrptr += xt_compat_calc_jump(NFPROTO_BRIDGE, delta);
  1955. tmp.hook_entry[i] = (struct ebt_entries __user *)usrptr;
  1956. }
  1957. }
  1958. xt_compat_flush_offsets(NFPROTO_BRIDGE);
  1959. xt_compat_unlock(NFPROTO_BRIDGE);
  1960. ret = do_replace_finish(net, &tmp, newinfo);
  1961. if (ret == 0)
  1962. return ret;
  1963. free_entries:
  1964. vfree(newinfo->entries);
  1965. free_newinfo:
  1966. vfree(newinfo);
  1967. return ret;
  1968. out_unlock:
  1969. xt_compat_flush_offsets(NFPROTO_BRIDGE);
  1970. xt_compat_unlock(NFPROTO_BRIDGE);
  1971. goto free_entries;
  1972. }
  1973. static int compat_update_counters(struct net *net, void __user *user,
  1974. unsigned int len)
  1975. {
  1976. struct compat_ebt_replace hlp;
  1977. if (copy_from_user(&hlp, user, sizeof(hlp)))
  1978. return -EFAULT;
  1979. /* try real handler in case userland supplied needed padding */
  1980. if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
  1981. return update_counters(net, user, len);
  1982. return do_update_counters(net, hlp.name, compat_ptr(hlp.counters),
  1983. hlp.num_counters, user, len);
  1984. }
  1985. static int compat_do_ebt_set_ctl(struct sock *sk,
  1986. int cmd, void __user *user, unsigned int len)
  1987. {
  1988. int ret;
  1989. struct net *net = sock_net(sk);
  1990. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1991. return -EPERM;
  1992. switch (cmd) {
  1993. case EBT_SO_SET_ENTRIES:
  1994. ret = compat_do_replace(net, user, len);
  1995. break;
  1996. case EBT_SO_SET_COUNTERS:
  1997. ret = compat_update_counters(net, user, len);
  1998. break;
  1999. default:
  2000. ret = -EINVAL;
  2001. }
  2002. return ret;
  2003. }
  2004. static int compat_do_ebt_get_ctl(struct sock *sk, int cmd,
  2005. void __user *user, int *len)
  2006. {
  2007. int ret;
  2008. struct compat_ebt_replace tmp;
  2009. struct ebt_table *t;
  2010. struct net *net = sock_net(sk);
  2011. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2012. return -EPERM;
  2013. /* try real handler in case userland supplied needed padding */
  2014. if ((cmd == EBT_SO_GET_INFO ||
  2015. cmd == EBT_SO_GET_INIT_INFO) && *len != sizeof(tmp))
  2016. return do_ebt_get_ctl(sk, cmd, user, len);
  2017. if (copy_from_user(&tmp, user, sizeof(tmp)))
  2018. return -EFAULT;
  2019. tmp.name[sizeof(tmp.name) - 1] = '\0';
  2020. t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
  2021. if (!t)
  2022. return ret;
  2023. xt_compat_lock(NFPROTO_BRIDGE);
  2024. switch (cmd) {
  2025. case EBT_SO_GET_INFO:
  2026. tmp.nentries = t->private->nentries;
  2027. ret = compat_table_info(t->private, &tmp);
  2028. if (ret)
  2029. goto out;
  2030. tmp.valid_hooks = t->valid_hooks;
  2031. if (copy_to_user(user, &tmp, *len) != 0) {
  2032. ret = -EFAULT;
  2033. break;
  2034. }
  2035. ret = 0;
  2036. break;
  2037. case EBT_SO_GET_INIT_INFO:
  2038. tmp.nentries = t->table->nentries;
  2039. tmp.entries_size = t->table->entries_size;
  2040. tmp.valid_hooks = t->table->valid_hooks;
  2041. if (copy_to_user(user, &tmp, *len) != 0) {
  2042. ret = -EFAULT;
  2043. break;
  2044. }
  2045. ret = 0;
  2046. break;
  2047. case EBT_SO_GET_ENTRIES:
  2048. case EBT_SO_GET_INIT_ENTRIES:
  2049. /* try real handler first in case of userland-side padding.
  2050. * in case we are dealing with an 'ordinary' 32 bit binary
  2051. * without 64bit compatibility padding, this will fail right
  2052. * after copy_from_user when the *len argument is validated.
  2053. *
  2054. * the compat_ variant needs to do one pass over the kernel
  2055. * data set to adjust for size differences before it the check.
  2056. */
  2057. if (copy_everything_to_user(t, user, len, cmd) == 0)
  2058. ret = 0;
  2059. else
  2060. ret = compat_copy_everything_to_user(t, user, len, cmd);
  2061. break;
  2062. default:
  2063. ret = -EINVAL;
  2064. }
  2065. out:
  2066. xt_compat_flush_offsets(NFPROTO_BRIDGE);
  2067. xt_compat_unlock(NFPROTO_BRIDGE);
  2068. mutex_unlock(&ebt_mutex);
  2069. return ret;
  2070. }
  2071. #endif
  2072. static struct nf_sockopt_ops ebt_sockopts = {
  2073. .pf = PF_INET,
  2074. .set_optmin = EBT_BASE_CTL,
  2075. .set_optmax = EBT_SO_SET_MAX + 1,
  2076. .set = do_ebt_set_ctl,
  2077. #ifdef CONFIG_COMPAT
  2078. .compat_set = compat_do_ebt_set_ctl,
  2079. #endif
  2080. .get_optmin = EBT_BASE_CTL,
  2081. .get_optmax = EBT_SO_GET_MAX + 1,
  2082. .get = do_ebt_get_ctl,
  2083. #ifdef CONFIG_COMPAT
  2084. .compat_get = compat_do_ebt_get_ctl,
  2085. #endif
  2086. .owner = THIS_MODULE,
  2087. };
  2088. static int __init ebtables_init(void)
  2089. {
  2090. int ret;
  2091. ret = xt_register_target(&ebt_standard_target);
  2092. if (ret < 0)
  2093. return ret;
  2094. ret = nf_register_sockopt(&ebt_sockopts);
  2095. if (ret < 0) {
  2096. xt_unregister_target(&ebt_standard_target);
  2097. return ret;
  2098. }
  2099. return 0;
  2100. }
  2101. static void __exit ebtables_fini(void)
  2102. {
  2103. nf_unregister_sockopt(&ebt_sockopts);
  2104. xt_unregister_target(&ebt_standard_target);
  2105. }
  2106. EXPORT_SYMBOL(ebt_register_table);
  2107. EXPORT_SYMBOL(ebt_unregister_table);
  2108. EXPORT_SYMBOL(ebt_do_table);
  2109. module_init(ebtables_init);
  2110. module_exit(ebtables_fini);
  2111. MODULE_LICENSE("GPL");