cipso_ipv4.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CIPSO - Commercial IP Security Option
  4. *
  5. * This is an implementation of the CIPSO 2.2 protocol as specified in
  6. * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
  7. * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors
  8. * have chosen to adopt the protocol and over the years it has become a
  9. * de-facto standard for labeled networking.
  10. *
  11. * The CIPSO draft specification can be found in the kernel's Documentation
  12. * directory as well as the following URL:
  13. * https://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt
  14. * The FIPS-188 specification can be found at the following URL:
  15. * https://www.itl.nist.gov/fipspubs/fip188.htm
  16. *
  17. * Author: Paul Moore <paul.moore@hp.com>
  18. */
  19. /*
  20. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  21. */
  22. #include <linux/init.h>
  23. #include <linux/types.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/list.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/string.h>
  28. #include <linux/jhash.h>
  29. #include <linux/audit.h>
  30. #include <linux/slab.h>
  31. #include <net/ip.h>
  32. #include <net/icmp.h>
  33. #include <net/tcp.h>
  34. #include <net/netlabel.h>
  35. #include <net/cipso_ipv4.h>
  36. #include <linux/atomic.h>
  37. #include <linux/bug.h>
  38. #include <linux/unaligned.h>
  39. /* List of available DOI definitions */
  40. /* XXX - This currently assumes a minimal number of different DOIs in use,
  41. * if in practice there are a lot of different DOIs this list should
  42. * probably be turned into a hash table or something similar so we
  43. * can do quick lookups. */
  44. static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
  45. static LIST_HEAD(cipso_v4_doi_list);
  46. /* Label mapping cache */
  47. int cipso_v4_cache_enabled = 1;
  48. int cipso_v4_cache_bucketsize = 10;
  49. #define CIPSO_V4_CACHE_BUCKETBITS 7
  50. #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
  51. #define CIPSO_V4_CACHE_REORDERLIMIT 10
  52. struct cipso_v4_map_cache_bkt {
  53. spinlock_t lock;
  54. u32 size;
  55. struct list_head list;
  56. };
  57. struct cipso_v4_map_cache_entry {
  58. u32 hash;
  59. unsigned char *key;
  60. size_t key_len;
  61. struct netlbl_lsm_cache *lsm_data;
  62. u32 activity;
  63. struct list_head list;
  64. };
  65. static struct cipso_v4_map_cache_bkt *cipso_v4_cache;
  66. /* Restricted bitmap (tag #1) flags */
  67. int cipso_v4_rbm_optfmt;
  68. int cipso_v4_rbm_strictvalid = 1;
  69. /*
  70. * Protocol Constants
  71. */
  72. /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
  73. * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
  74. #define CIPSO_V4_OPT_LEN_MAX 40
  75. /* Length of the base CIPSO option, this includes the option type (1 byte), the
  76. * option length (1 byte), and the DOI (4 bytes). */
  77. #define CIPSO_V4_HDR_LEN 6
  78. /* Base length of the restrictive category bitmap tag (tag #1). */
  79. #define CIPSO_V4_TAG_RBM_BLEN 4
  80. /* Base length of the enumerated category tag (tag #2). */
  81. #define CIPSO_V4_TAG_ENUM_BLEN 4
  82. /* Base length of the ranged categories bitmap tag (tag #5). */
  83. #define CIPSO_V4_TAG_RNG_BLEN 4
  84. /* The maximum number of category ranges permitted in the ranged category tag
  85. * (tag #5). You may note that the IETF draft states that the maximum number
  86. * of category ranges is 7, but if the low end of the last category range is
  87. * zero then it is possible to fit 8 category ranges because the zero should
  88. * be omitted. */
  89. #define CIPSO_V4_TAG_RNG_CAT_MAX 8
  90. /* Base length of the local tag (non-standard tag).
  91. * Tag definition (may change between kernel versions)
  92. *
  93. * 0 8 16 24 32
  94. * +----------+----------+----------+----------+
  95. * | 10000000 | 00000110 | 32-bit secid value |
  96. * +----------+----------+----------+----------+
  97. * | in (host byte order)|
  98. * +----------+----------+
  99. *
  100. */
  101. #define CIPSO_V4_TAG_LOC_BLEN 6
  102. /*
  103. * Helper Functions
  104. */
  105. /**
  106. * cipso_v4_cache_entry_free - Frees a cache entry
  107. * @entry: the entry to free
  108. *
  109. * Description:
  110. * This function frees the memory associated with a cache entry including the
  111. * LSM cache data if there are no longer any users, i.e. reference count == 0.
  112. *
  113. */
  114. static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
  115. {
  116. if (entry->lsm_data)
  117. netlbl_secattr_cache_free(entry->lsm_data);
  118. kfree(entry->key);
  119. kfree(entry);
  120. }
  121. /**
  122. * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
  123. * @key: the hash key
  124. * @key_len: the length of the key in bytes
  125. *
  126. * Description:
  127. * The CIPSO tag hashing function. Returns a 32-bit hash value.
  128. *
  129. */
  130. static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
  131. {
  132. return jhash(key, key_len, 0);
  133. }
  134. /*
  135. * Label Mapping Cache Functions
  136. */
  137. /**
  138. * cipso_v4_cache_init - Initialize the CIPSO cache
  139. *
  140. * Description:
  141. * Initializes the CIPSO label mapping cache, this function should be called
  142. * before any of the other functions defined in this file. Returns zero on
  143. * success, negative values on error.
  144. *
  145. */
  146. static int __init cipso_v4_cache_init(void)
  147. {
  148. u32 iter;
  149. cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
  150. sizeof(struct cipso_v4_map_cache_bkt),
  151. GFP_KERNEL);
  152. if (!cipso_v4_cache)
  153. return -ENOMEM;
  154. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  155. spin_lock_init(&cipso_v4_cache[iter].lock);
  156. cipso_v4_cache[iter].size = 0;
  157. INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
  158. }
  159. return 0;
  160. }
  161. /**
  162. * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
  163. *
  164. * Description:
  165. * Invalidates and frees any entries in the CIPSO cache.
  166. *
  167. */
  168. void cipso_v4_cache_invalidate(void)
  169. {
  170. struct cipso_v4_map_cache_entry *entry, *tmp_entry;
  171. u32 iter;
  172. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  173. spin_lock_bh(&cipso_v4_cache[iter].lock);
  174. list_for_each_entry_safe(entry,
  175. tmp_entry,
  176. &cipso_v4_cache[iter].list, list) {
  177. list_del(&entry->list);
  178. cipso_v4_cache_entry_free(entry);
  179. }
  180. cipso_v4_cache[iter].size = 0;
  181. spin_unlock_bh(&cipso_v4_cache[iter].lock);
  182. }
  183. }
  184. /**
  185. * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
  186. * @key: the buffer to check
  187. * @key_len: buffer length in bytes
  188. * @secattr: the security attribute struct to use
  189. *
  190. * Description:
  191. * This function checks the cache to see if a label mapping already exists for
  192. * the given key. If there is a match then the cache is adjusted and the
  193. * @secattr struct is populated with the correct LSM security attributes. The
  194. * cache is adjusted in the following manner if the entry is not already the
  195. * first in the cache bucket:
  196. *
  197. * 1. The cache entry's activity counter is incremented
  198. * 2. The previous (higher ranking) entry's activity counter is decremented
  199. * 3. If the difference between the two activity counters is geater than
  200. * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
  201. *
  202. * Returns zero on success, -ENOENT for a cache miss, and other negative values
  203. * on error.
  204. *
  205. */
  206. static int cipso_v4_cache_check(const unsigned char *key,
  207. u32 key_len,
  208. struct netlbl_lsm_secattr *secattr)
  209. {
  210. u32 bkt;
  211. struct cipso_v4_map_cache_entry *entry;
  212. struct cipso_v4_map_cache_entry *prev_entry = NULL;
  213. u32 hash;
  214. if (!READ_ONCE(cipso_v4_cache_enabled))
  215. return -ENOENT;
  216. hash = cipso_v4_map_cache_hash(key, key_len);
  217. bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
  218. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  219. list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
  220. if (entry->hash == hash &&
  221. entry->key_len == key_len &&
  222. memcmp(entry->key, key, key_len) == 0) {
  223. entry->activity += 1;
  224. refcount_inc(&entry->lsm_data->refcount);
  225. secattr->cache = entry->lsm_data;
  226. secattr->flags |= NETLBL_SECATTR_CACHE;
  227. secattr->type = NETLBL_NLTYPE_CIPSOV4;
  228. if (!prev_entry) {
  229. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  230. return 0;
  231. }
  232. if (prev_entry->activity > 0)
  233. prev_entry->activity -= 1;
  234. if (entry->activity > prev_entry->activity &&
  235. entry->activity - prev_entry->activity >
  236. CIPSO_V4_CACHE_REORDERLIMIT) {
  237. __list_del(entry->list.prev, entry->list.next);
  238. __list_add(&entry->list,
  239. prev_entry->list.prev,
  240. &prev_entry->list);
  241. }
  242. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  243. return 0;
  244. }
  245. prev_entry = entry;
  246. }
  247. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  248. return -ENOENT;
  249. }
  250. /**
  251. * cipso_v4_cache_add - Add an entry to the CIPSO cache
  252. * @cipso_ptr: pointer to CIPSO IP option
  253. * @secattr: the packet's security attributes
  254. *
  255. * Description:
  256. * Add a new entry into the CIPSO label mapping cache. Add the new entry to
  257. * head of the cache bucket's list, if the cache bucket is out of room remove
  258. * the last entry in the list first. It is important to note that there is
  259. * currently no checking for duplicate keys. Returns zero on success,
  260. * negative values on failure.
  261. *
  262. */
  263. int cipso_v4_cache_add(const unsigned char *cipso_ptr,
  264. const struct netlbl_lsm_secattr *secattr)
  265. {
  266. int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize);
  267. int ret_val = -EPERM;
  268. u32 bkt;
  269. struct cipso_v4_map_cache_entry *entry = NULL;
  270. struct cipso_v4_map_cache_entry *old_entry = NULL;
  271. u32 cipso_ptr_len;
  272. if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0)
  273. return 0;
  274. cipso_ptr_len = cipso_ptr[1];
  275. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  276. if (!entry)
  277. return -ENOMEM;
  278. entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
  279. if (!entry->key) {
  280. ret_val = -ENOMEM;
  281. goto cache_add_failure;
  282. }
  283. entry->key_len = cipso_ptr_len;
  284. entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
  285. refcount_inc(&secattr->cache->refcount);
  286. entry->lsm_data = secattr->cache;
  287. bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
  288. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  289. if (cipso_v4_cache[bkt].size < bkt_size) {
  290. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  291. cipso_v4_cache[bkt].size += 1;
  292. } else {
  293. old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
  294. struct cipso_v4_map_cache_entry, list);
  295. list_del(&old_entry->list);
  296. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  297. cipso_v4_cache_entry_free(old_entry);
  298. }
  299. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  300. return 0;
  301. cache_add_failure:
  302. if (entry)
  303. cipso_v4_cache_entry_free(entry);
  304. return ret_val;
  305. }
  306. /*
  307. * DOI List Functions
  308. */
  309. /**
  310. * cipso_v4_doi_search - Searches for a DOI definition
  311. * @doi: the DOI to search for
  312. *
  313. * Description:
  314. * Search the DOI definition list for a DOI definition with a DOI value that
  315. * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
  316. * Returns a pointer to the DOI definition on success and NULL on failure.
  317. */
  318. static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
  319. {
  320. struct cipso_v4_doi *iter;
  321. list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
  322. if (iter->doi == doi && refcount_read(&iter->refcount))
  323. return iter;
  324. return NULL;
  325. }
  326. /**
  327. * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
  328. * @doi_def: the DOI structure
  329. * @audit_info: NetLabel audit information
  330. *
  331. * Description:
  332. * The caller defines a new DOI for use by the CIPSO engine and calls this
  333. * function to add it to the list of acceptable domains. The caller must
  334. * ensure that the mapping table specified in @doi_def->map meets all of the
  335. * requirements of the mapping type (see cipso_ipv4.h for details). Returns
  336. * zero on success and non-zero on failure.
  337. *
  338. */
  339. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  340. struct netlbl_audit *audit_info)
  341. {
  342. int ret_val = -EINVAL;
  343. u32 iter;
  344. u32 doi;
  345. u32 doi_type;
  346. struct audit_buffer *audit_buf;
  347. doi = doi_def->doi;
  348. doi_type = doi_def->type;
  349. if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
  350. goto doi_add_return;
  351. for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
  352. switch (doi_def->tags[iter]) {
  353. case CIPSO_V4_TAG_RBITMAP:
  354. break;
  355. case CIPSO_V4_TAG_RANGE:
  356. case CIPSO_V4_TAG_ENUM:
  357. if (doi_def->type != CIPSO_V4_MAP_PASS)
  358. goto doi_add_return;
  359. break;
  360. case CIPSO_V4_TAG_LOCAL:
  361. if (doi_def->type != CIPSO_V4_MAP_LOCAL)
  362. goto doi_add_return;
  363. break;
  364. case CIPSO_V4_TAG_INVALID:
  365. if (iter == 0)
  366. goto doi_add_return;
  367. break;
  368. default:
  369. goto doi_add_return;
  370. }
  371. }
  372. refcount_set(&doi_def->refcount, 1);
  373. spin_lock(&cipso_v4_doi_list_lock);
  374. if (cipso_v4_doi_search(doi_def->doi)) {
  375. spin_unlock(&cipso_v4_doi_list_lock);
  376. ret_val = -EEXIST;
  377. goto doi_add_return;
  378. }
  379. list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
  380. spin_unlock(&cipso_v4_doi_list_lock);
  381. ret_val = 0;
  382. doi_add_return:
  383. audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
  384. if (audit_buf) {
  385. const char *type_str;
  386. switch (doi_type) {
  387. case CIPSO_V4_MAP_TRANS:
  388. type_str = "trans";
  389. break;
  390. case CIPSO_V4_MAP_PASS:
  391. type_str = "pass";
  392. break;
  393. case CIPSO_V4_MAP_LOCAL:
  394. type_str = "local";
  395. break;
  396. default:
  397. type_str = "(unknown)";
  398. }
  399. audit_log_format(audit_buf,
  400. " cipso_doi=%u cipso_type=%s res=%u",
  401. doi, type_str, ret_val == 0 ? 1 : 0);
  402. audit_log_end(audit_buf);
  403. }
  404. return ret_val;
  405. }
  406. /**
  407. * cipso_v4_doi_free - Frees a DOI definition
  408. * @doi_def: the DOI definition
  409. *
  410. * Description:
  411. * This function frees all of the memory associated with a DOI definition.
  412. *
  413. */
  414. void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
  415. {
  416. if (!doi_def)
  417. return;
  418. switch (doi_def->type) {
  419. case CIPSO_V4_MAP_TRANS:
  420. kfree(doi_def->map.std->lvl.cipso);
  421. kfree(doi_def->map.std->lvl.local);
  422. kfree(doi_def->map.std->cat.cipso);
  423. kfree(doi_def->map.std->cat.local);
  424. kfree(doi_def->map.std);
  425. break;
  426. }
  427. kfree(doi_def);
  428. }
  429. /**
  430. * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
  431. * @entry: the entry's RCU field
  432. *
  433. * Description:
  434. * This function is designed to be used as a callback to the call_rcu()
  435. * function so that the memory allocated to the DOI definition can be released
  436. * safely.
  437. *
  438. */
  439. static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
  440. {
  441. struct cipso_v4_doi *doi_def;
  442. doi_def = container_of(entry, struct cipso_v4_doi, rcu);
  443. cipso_v4_doi_free(doi_def);
  444. }
  445. /**
  446. * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
  447. * @doi: the DOI value
  448. * @audit_info: NetLabel audit information
  449. *
  450. * Description:
  451. * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
  452. * be called to release their own LSM domain mappings as well as our own
  453. * domain list. Returns zero on success and negative values on failure.
  454. *
  455. */
  456. int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
  457. {
  458. int ret_val;
  459. struct cipso_v4_doi *doi_def;
  460. struct audit_buffer *audit_buf;
  461. spin_lock(&cipso_v4_doi_list_lock);
  462. doi_def = cipso_v4_doi_search(doi);
  463. if (!doi_def) {
  464. spin_unlock(&cipso_v4_doi_list_lock);
  465. ret_val = -ENOENT;
  466. goto doi_remove_return;
  467. }
  468. list_del_rcu(&doi_def->list);
  469. spin_unlock(&cipso_v4_doi_list_lock);
  470. cipso_v4_doi_putdef(doi_def);
  471. ret_val = 0;
  472. doi_remove_return:
  473. audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
  474. if (audit_buf) {
  475. audit_log_format(audit_buf,
  476. " cipso_doi=%u res=%u",
  477. doi, ret_val == 0 ? 1 : 0);
  478. audit_log_end(audit_buf);
  479. }
  480. return ret_val;
  481. }
  482. /**
  483. * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
  484. * @doi: the DOI value
  485. *
  486. * Description:
  487. * Searches for a valid DOI definition and if one is found it is returned to
  488. * the caller. Otherwise NULL is returned. The caller must ensure that
  489. * rcu_read_lock() is held while accessing the returned definition and the DOI
  490. * definition reference count is decremented when the caller is done.
  491. *
  492. */
  493. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  494. {
  495. struct cipso_v4_doi *doi_def;
  496. rcu_read_lock();
  497. doi_def = cipso_v4_doi_search(doi);
  498. if (!doi_def)
  499. goto doi_getdef_return;
  500. if (!refcount_inc_not_zero(&doi_def->refcount))
  501. doi_def = NULL;
  502. doi_getdef_return:
  503. rcu_read_unlock();
  504. return doi_def;
  505. }
  506. /**
  507. * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
  508. * @doi_def: the DOI definition
  509. *
  510. * Description:
  511. * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
  512. *
  513. */
  514. void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
  515. {
  516. if (!doi_def)
  517. return;
  518. if (!refcount_dec_and_test(&doi_def->refcount))
  519. return;
  520. cipso_v4_cache_invalidate();
  521. call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
  522. }
  523. /**
  524. * cipso_v4_doi_walk - Iterate through the DOI definitions
  525. * @skip_cnt: skip past this number of DOI definitions, updated
  526. * @callback: callback for each DOI definition
  527. * @cb_arg: argument for the callback function
  528. *
  529. * Description:
  530. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  531. * For each entry call @callback, if @callback returns a negative value stop
  532. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  533. * return. Returns zero on success, negative values on failure.
  534. *
  535. */
  536. int cipso_v4_doi_walk(u32 *skip_cnt,
  537. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  538. void *cb_arg)
  539. {
  540. int ret_val = -ENOENT;
  541. u32 doi_cnt = 0;
  542. struct cipso_v4_doi *iter_doi;
  543. rcu_read_lock();
  544. list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
  545. if (refcount_read(&iter_doi->refcount) > 0) {
  546. if (doi_cnt++ < *skip_cnt)
  547. continue;
  548. ret_val = callback(iter_doi, cb_arg);
  549. if (ret_val < 0) {
  550. doi_cnt--;
  551. goto doi_walk_return;
  552. }
  553. }
  554. doi_walk_return:
  555. rcu_read_unlock();
  556. *skip_cnt = doi_cnt;
  557. return ret_val;
  558. }
  559. /*
  560. * Label Mapping Functions
  561. */
  562. /**
  563. * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
  564. * @doi_def: the DOI definition
  565. * @level: the level to check
  566. *
  567. * Description:
  568. * Checks the given level against the given DOI definition and returns a
  569. * negative value if the level does not have a valid mapping and a zero value
  570. * if the level is defined by the DOI.
  571. *
  572. */
  573. static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
  574. {
  575. switch (doi_def->type) {
  576. case CIPSO_V4_MAP_PASS:
  577. return 0;
  578. case CIPSO_V4_MAP_TRANS:
  579. if ((level < doi_def->map.std->lvl.cipso_size) &&
  580. (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL))
  581. return 0;
  582. break;
  583. }
  584. return -EFAULT;
  585. }
  586. /**
  587. * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
  588. * @doi_def: the DOI definition
  589. * @host_lvl: the host MLS level
  590. * @net_lvl: the network/CIPSO MLS level
  591. *
  592. * Description:
  593. * Perform a label mapping to translate a local MLS level to the correct
  594. * CIPSO level using the given DOI definition. Returns zero on success,
  595. * negative values otherwise.
  596. *
  597. */
  598. static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
  599. u32 host_lvl,
  600. u32 *net_lvl)
  601. {
  602. switch (doi_def->type) {
  603. case CIPSO_V4_MAP_PASS:
  604. *net_lvl = host_lvl;
  605. return 0;
  606. case CIPSO_V4_MAP_TRANS:
  607. if (host_lvl < doi_def->map.std->lvl.local_size &&
  608. doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
  609. *net_lvl = doi_def->map.std->lvl.local[host_lvl];
  610. return 0;
  611. }
  612. return -EPERM;
  613. }
  614. return -EINVAL;
  615. }
  616. /**
  617. * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
  618. * @doi_def: the DOI definition
  619. * @net_lvl: the network/CIPSO MLS level
  620. * @host_lvl: the host MLS level
  621. *
  622. * Description:
  623. * Perform a label mapping to translate a CIPSO level to the correct local MLS
  624. * level using the given DOI definition. Returns zero on success, negative
  625. * values otherwise.
  626. *
  627. */
  628. static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
  629. u32 net_lvl,
  630. u32 *host_lvl)
  631. {
  632. struct cipso_v4_std_map_tbl *map_tbl;
  633. switch (doi_def->type) {
  634. case CIPSO_V4_MAP_PASS:
  635. *host_lvl = net_lvl;
  636. return 0;
  637. case CIPSO_V4_MAP_TRANS:
  638. map_tbl = doi_def->map.std;
  639. if (net_lvl < map_tbl->lvl.cipso_size &&
  640. map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
  641. *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
  642. return 0;
  643. }
  644. return -EPERM;
  645. }
  646. return -EINVAL;
  647. }
  648. /**
  649. * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
  650. * @doi_def: the DOI definition
  651. * @bitmap: category bitmap
  652. * @bitmap_len: bitmap length in bytes
  653. *
  654. * Description:
  655. * Checks the given category bitmap against the given DOI definition and
  656. * returns a negative value if any of the categories in the bitmap do not have
  657. * a valid mapping and a zero value if all of the categories are valid.
  658. *
  659. */
  660. static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
  661. const unsigned char *bitmap,
  662. u32 bitmap_len)
  663. {
  664. int cat = -1;
  665. u32 bitmap_len_bits = bitmap_len * 8;
  666. u32 cipso_cat_size;
  667. u32 *cipso_array;
  668. switch (doi_def->type) {
  669. case CIPSO_V4_MAP_PASS:
  670. return 0;
  671. case CIPSO_V4_MAP_TRANS:
  672. cipso_cat_size = doi_def->map.std->cat.cipso_size;
  673. cipso_array = doi_def->map.std->cat.cipso;
  674. for (;;) {
  675. cat = netlbl_bitmap_walk(bitmap,
  676. bitmap_len_bits,
  677. cat + 1,
  678. 1);
  679. if (cat < 0)
  680. break;
  681. if (cat >= cipso_cat_size ||
  682. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  683. return -EFAULT;
  684. }
  685. if (cat == -1)
  686. return 0;
  687. break;
  688. }
  689. return -EFAULT;
  690. }
  691. /**
  692. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  693. * @doi_def: the DOI definition
  694. * @secattr: the security attributes
  695. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  696. * @net_cat_len: the length of the CIPSO bitmap in bytes
  697. *
  698. * Description:
  699. * Perform a label mapping to translate a local MLS category bitmap to the
  700. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  701. * size in bytes of the network bitmap on success, negative values otherwise.
  702. *
  703. */
  704. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  705. const struct netlbl_lsm_secattr *secattr,
  706. unsigned char *net_cat,
  707. u32 net_cat_len)
  708. {
  709. int host_spot = -1;
  710. u32 net_spot = CIPSO_V4_INV_CAT;
  711. u32 net_spot_max = 0;
  712. u32 net_clen_bits = net_cat_len * 8;
  713. u32 host_cat_size = 0;
  714. u32 *host_cat_array = NULL;
  715. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  716. host_cat_size = doi_def->map.std->cat.local_size;
  717. host_cat_array = doi_def->map.std->cat.local;
  718. }
  719. for (;;) {
  720. host_spot = netlbl_catmap_walk(secattr->attr.mls.cat,
  721. host_spot + 1);
  722. if (host_spot < 0)
  723. break;
  724. switch (doi_def->type) {
  725. case CIPSO_V4_MAP_PASS:
  726. net_spot = host_spot;
  727. break;
  728. case CIPSO_V4_MAP_TRANS:
  729. if (host_spot >= host_cat_size)
  730. return -EPERM;
  731. net_spot = host_cat_array[host_spot];
  732. if (net_spot >= CIPSO_V4_INV_CAT)
  733. return -EPERM;
  734. break;
  735. }
  736. if (net_spot >= net_clen_bits)
  737. return -ENOSPC;
  738. netlbl_bitmap_setbit(net_cat, net_spot, 1);
  739. if (net_spot > net_spot_max)
  740. net_spot_max = net_spot;
  741. }
  742. if (++net_spot_max % 8)
  743. return net_spot_max / 8 + 1;
  744. return net_spot_max / 8;
  745. }
  746. /**
  747. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  748. * @doi_def: the DOI definition
  749. * @net_cat: the category bitmap in network/CIPSO format
  750. * @net_cat_len: the length of the CIPSO bitmap in bytes
  751. * @secattr: the security attributes
  752. *
  753. * Description:
  754. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  755. * MLS category bitmap using the given DOI definition. Returns zero on
  756. * success, negative values on failure.
  757. *
  758. */
  759. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  760. const unsigned char *net_cat,
  761. u32 net_cat_len,
  762. struct netlbl_lsm_secattr *secattr)
  763. {
  764. int ret_val;
  765. int net_spot = -1;
  766. u32 host_spot = CIPSO_V4_INV_CAT;
  767. u32 net_clen_bits = net_cat_len * 8;
  768. u32 net_cat_size = 0;
  769. u32 *net_cat_array = NULL;
  770. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  771. net_cat_size = doi_def->map.std->cat.cipso_size;
  772. net_cat_array = doi_def->map.std->cat.cipso;
  773. }
  774. for (;;) {
  775. net_spot = netlbl_bitmap_walk(net_cat,
  776. net_clen_bits,
  777. net_spot + 1,
  778. 1);
  779. if (net_spot < 0)
  780. return 0;
  781. switch (doi_def->type) {
  782. case CIPSO_V4_MAP_PASS:
  783. host_spot = net_spot;
  784. break;
  785. case CIPSO_V4_MAP_TRANS:
  786. if (net_spot >= net_cat_size)
  787. return -EPERM;
  788. host_spot = net_cat_array[net_spot];
  789. if (host_spot >= CIPSO_V4_INV_CAT)
  790. return -EPERM;
  791. break;
  792. }
  793. ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
  794. host_spot,
  795. GFP_ATOMIC);
  796. if (ret_val != 0)
  797. return ret_val;
  798. }
  799. return -EINVAL;
  800. }
  801. /**
  802. * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
  803. * @doi_def: the DOI definition
  804. * @enumcat: category list
  805. * @enumcat_len: length of the category list in bytes
  806. *
  807. * Description:
  808. * Checks the given categories against the given DOI definition and returns a
  809. * negative value if any of the categories do not have a valid mapping and a
  810. * zero value if all of the categories are valid.
  811. *
  812. */
  813. static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
  814. const unsigned char *enumcat,
  815. u32 enumcat_len)
  816. {
  817. u16 cat;
  818. int cat_prev = -1;
  819. u32 iter;
  820. if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
  821. return -EFAULT;
  822. for (iter = 0; iter < enumcat_len; iter += 2) {
  823. cat = get_unaligned_be16(&enumcat[iter]);
  824. if (cat <= cat_prev)
  825. return -EFAULT;
  826. cat_prev = cat;
  827. }
  828. return 0;
  829. }
  830. /**
  831. * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
  832. * @doi_def: the DOI definition
  833. * @secattr: the security attributes
  834. * @net_cat: the zero'd out category list in network/CIPSO format
  835. * @net_cat_len: the length of the CIPSO category list in bytes
  836. *
  837. * Description:
  838. * Perform a label mapping to translate a local MLS category bitmap to the
  839. * correct CIPSO category list using the given DOI definition. Returns the
  840. * size in bytes of the network category bitmap on success, negative values
  841. * otherwise.
  842. *
  843. */
  844. static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
  845. const struct netlbl_lsm_secattr *secattr,
  846. unsigned char *net_cat,
  847. u32 net_cat_len)
  848. {
  849. int cat = -1;
  850. u32 cat_iter = 0;
  851. for (;;) {
  852. cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1);
  853. if (cat < 0)
  854. break;
  855. if ((cat_iter + 2) > net_cat_len)
  856. return -ENOSPC;
  857. *((__be16 *)&net_cat[cat_iter]) = htons(cat);
  858. cat_iter += 2;
  859. }
  860. return cat_iter;
  861. }
  862. /**
  863. * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
  864. * @doi_def: the DOI definition
  865. * @net_cat: the category list in network/CIPSO format
  866. * @net_cat_len: the length of the CIPSO bitmap in bytes
  867. * @secattr: the security attributes
  868. *
  869. * Description:
  870. * Perform a label mapping to translate a CIPSO category list to the correct
  871. * local MLS category bitmap using the given DOI definition. Returns zero on
  872. * success, negative values on failure.
  873. *
  874. */
  875. static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
  876. const unsigned char *net_cat,
  877. u32 net_cat_len,
  878. struct netlbl_lsm_secattr *secattr)
  879. {
  880. int ret_val;
  881. u32 iter;
  882. for (iter = 0; iter < net_cat_len; iter += 2) {
  883. ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
  884. get_unaligned_be16(&net_cat[iter]),
  885. GFP_ATOMIC);
  886. if (ret_val != 0)
  887. return ret_val;
  888. }
  889. return 0;
  890. }
  891. /**
  892. * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
  893. * @doi_def: the DOI definition
  894. * @rngcat: category list
  895. * @rngcat_len: length of the category list in bytes
  896. *
  897. * Description:
  898. * Checks the given categories against the given DOI definition and returns a
  899. * negative value if any of the categories do not have a valid mapping and a
  900. * zero value if all of the categories are valid.
  901. *
  902. */
  903. static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
  904. const unsigned char *rngcat,
  905. u32 rngcat_len)
  906. {
  907. u16 cat_high;
  908. u16 cat_low;
  909. u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
  910. u32 iter;
  911. if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
  912. return -EFAULT;
  913. for (iter = 0; iter < rngcat_len; iter += 4) {
  914. cat_high = get_unaligned_be16(&rngcat[iter]);
  915. if ((iter + 4) <= rngcat_len)
  916. cat_low = get_unaligned_be16(&rngcat[iter + 2]);
  917. else
  918. cat_low = 0;
  919. if (cat_high > cat_prev)
  920. return -EFAULT;
  921. cat_prev = cat_low;
  922. }
  923. return 0;
  924. }
  925. /**
  926. * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
  927. * @doi_def: the DOI definition
  928. * @secattr: the security attributes
  929. * @net_cat: the zero'd out category list in network/CIPSO format
  930. * @net_cat_len: the length of the CIPSO category list in bytes
  931. *
  932. * Description:
  933. * Perform a label mapping to translate a local MLS category bitmap to the
  934. * correct CIPSO category list using the given DOI definition. Returns the
  935. * size in bytes of the network category bitmap on success, negative values
  936. * otherwise.
  937. *
  938. */
  939. static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
  940. const struct netlbl_lsm_secattr *secattr,
  941. unsigned char *net_cat,
  942. u32 net_cat_len)
  943. {
  944. int iter = -1;
  945. u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
  946. u32 array_cnt = 0;
  947. u32 cat_size = 0;
  948. /* make sure we don't overflow the 'array[]' variable */
  949. if (net_cat_len >
  950. (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
  951. return -ENOSPC;
  952. for (;;) {
  953. iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1);
  954. if (iter < 0)
  955. break;
  956. cat_size += (iter == 0 ? 0 : sizeof(u16));
  957. if (cat_size > net_cat_len)
  958. return -ENOSPC;
  959. array[array_cnt++] = iter;
  960. iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter);
  961. if (iter < 0)
  962. return -EFAULT;
  963. cat_size += sizeof(u16);
  964. if (cat_size > net_cat_len)
  965. return -ENOSPC;
  966. array[array_cnt++] = iter;
  967. }
  968. for (iter = 0; array_cnt > 0;) {
  969. *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
  970. iter += 2;
  971. array_cnt--;
  972. if (array[array_cnt] != 0) {
  973. *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
  974. iter += 2;
  975. }
  976. }
  977. return cat_size;
  978. }
  979. /**
  980. * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
  981. * @doi_def: the DOI definition
  982. * @net_cat: the category list in network/CIPSO format
  983. * @net_cat_len: the length of the CIPSO bitmap in bytes
  984. * @secattr: the security attributes
  985. *
  986. * Description:
  987. * Perform a label mapping to translate a CIPSO category list to the correct
  988. * local MLS category bitmap using the given DOI definition. Returns zero on
  989. * success, negative values on failure.
  990. *
  991. */
  992. static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
  993. const unsigned char *net_cat,
  994. u32 net_cat_len,
  995. struct netlbl_lsm_secattr *secattr)
  996. {
  997. int ret_val;
  998. u32 net_iter;
  999. u16 cat_low;
  1000. u16 cat_high;
  1001. for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
  1002. cat_high = get_unaligned_be16(&net_cat[net_iter]);
  1003. if ((net_iter + 4) <= net_cat_len)
  1004. cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
  1005. else
  1006. cat_low = 0;
  1007. ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat,
  1008. cat_low,
  1009. cat_high,
  1010. GFP_ATOMIC);
  1011. if (ret_val != 0)
  1012. return ret_val;
  1013. }
  1014. return 0;
  1015. }
  1016. /*
  1017. * Protocol Handling Functions
  1018. */
  1019. /**
  1020. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  1021. * @doi_def: the DOI definition
  1022. * @len: the total tag length in bytes, not including this header
  1023. * @buf: the CIPSO option buffer
  1024. *
  1025. * Description:
  1026. * Write a CIPSO header into the beginning of @buffer.
  1027. *
  1028. */
  1029. static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  1030. unsigned char *buf,
  1031. u32 len)
  1032. {
  1033. buf[0] = IPOPT_CIPSO;
  1034. buf[1] = CIPSO_V4_HDR_LEN + len;
  1035. put_unaligned_be32(doi_def->doi, &buf[2]);
  1036. }
  1037. /**
  1038. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  1039. * @doi_def: the DOI definition
  1040. * @secattr: the security attributes
  1041. * @buffer: the option buffer
  1042. * @buffer_len: length of buffer in bytes
  1043. *
  1044. * Description:
  1045. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  1046. * actual buffer length may be larger than the indicated size due to
  1047. * translation between host and network category bitmaps. Returns the size of
  1048. * the tag on success, negative values on failure.
  1049. *
  1050. */
  1051. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  1052. const struct netlbl_lsm_secattr *secattr,
  1053. unsigned char *buffer,
  1054. u32 buffer_len)
  1055. {
  1056. int ret_val;
  1057. u32 tag_len;
  1058. u32 level;
  1059. if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
  1060. return -EPERM;
  1061. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1062. secattr->attr.mls.lvl,
  1063. &level);
  1064. if (ret_val != 0)
  1065. return ret_val;
  1066. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1067. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  1068. secattr,
  1069. &buffer[4],
  1070. buffer_len - 4);
  1071. if (ret_val < 0)
  1072. return ret_val;
  1073. /* This will send packets using the "optimized" format when
  1074. * possible as specified in section 3.4.2.6 of the
  1075. * CIPSO draft. */
  1076. if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 &&
  1077. ret_val <= 10)
  1078. tag_len = 14;
  1079. else
  1080. tag_len = 4 + ret_val;
  1081. } else
  1082. tag_len = 4;
  1083. buffer[0] = CIPSO_V4_TAG_RBITMAP;
  1084. buffer[1] = tag_len;
  1085. buffer[3] = level;
  1086. return tag_len;
  1087. }
  1088. /**
  1089. * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
  1090. * @doi_def: the DOI definition
  1091. * @tag: the CIPSO tag
  1092. * @secattr: the security attributes
  1093. *
  1094. * Description:
  1095. * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
  1096. * attributes in @secattr. Return zero on success, negatives values on
  1097. * failure.
  1098. *
  1099. */
  1100. static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
  1101. const unsigned char *tag,
  1102. struct netlbl_lsm_secattr *secattr)
  1103. {
  1104. int ret_val;
  1105. u8 tag_len = tag[1];
  1106. u32 level;
  1107. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1108. if (ret_val != 0)
  1109. return ret_val;
  1110. secattr->attr.mls.lvl = level;
  1111. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1112. if (tag_len > 4) {
  1113. ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
  1114. &tag[4],
  1115. tag_len - 4,
  1116. secattr);
  1117. if (ret_val != 0) {
  1118. netlbl_catmap_free(secattr->attr.mls.cat);
  1119. return ret_val;
  1120. }
  1121. if (secattr->attr.mls.cat)
  1122. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1123. }
  1124. return 0;
  1125. }
  1126. /**
  1127. * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
  1128. * @doi_def: the DOI definition
  1129. * @secattr: the security attributes
  1130. * @buffer: the option buffer
  1131. * @buffer_len: length of buffer in bytes
  1132. *
  1133. * Description:
  1134. * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
  1135. * size of the tag on success, negative values on failure.
  1136. *
  1137. */
  1138. static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
  1139. const struct netlbl_lsm_secattr *secattr,
  1140. unsigned char *buffer,
  1141. u32 buffer_len)
  1142. {
  1143. int ret_val;
  1144. u32 tag_len;
  1145. u32 level;
  1146. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1147. return -EPERM;
  1148. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1149. secattr->attr.mls.lvl,
  1150. &level);
  1151. if (ret_val != 0)
  1152. return ret_val;
  1153. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1154. ret_val = cipso_v4_map_cat_enum_hton(doi_def,
  1155. secattr,
  1156. &buffer[4],
  1157. buffer_len - 4);
  1158. if (ret_val < 0)
  1159. return ret_val;
  1160. tag_len = 4 + ret_val;
  1161. } else
  1162. tag_len = 4;
  1163. buffer[0] = CIPSO_V4_TAG_ENUM;
  1164. buffer[1] = tag_len;
  1165. buffer[3] = level;
  1166. return tag_len;
  1167. }
  1168. /**
  1169. * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
  1170. * @doi_def: the DOI definition
  1171. * @tag: the CIPSO tag
  1172. * @secattr: the security attributes
  1173. *
  1174. * Description:
  1175. * Parse a CIPSO enumerated tag (tag type #2) and return the security
  1176. * attributes in @secattr. Return zero on success, negatives values on
  1177. * failure.
  1178. *
  1179. */
  1180. static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
  1181. const unsigned char *tag,
  1182. struct netlbl_lsm_secattr *secattr)
  1183. {
  1184. int ret_val;
  1185. u8 tag_len = tag[1];
  1186. u32 level;
  1187. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1188. if (ret_val != 0)
  1189. return ret_val;
  1190. secattr->attr.mls.lvl = level;
  1191. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1192. if (tag_len > 4) {
  1193. ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
  1194. &tag[4],
  1195. tag_len - 4,
  1196. secattr);
  1197. if (ret_val != 0) {
  1198. netlbl_catmap_free(secattr->attr.mls.cat);
  1199. return ret_val;
  1200. }
  1201. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1202. }
  1203. return 0;
  1204. }
  1205. /**
  1206. * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
  1207. * @doi_def: the DOI definition
  1208. * @secattr: the security attributes
  1209. * @buffer: the option buffer
  1210. * @buffer_len: length of buffer in bytes
  1211. *
  1212. * Description:
  1213. * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
  1214. * size of the tag on success, negative values on failure.
  1215. *
  1216. */
  1217. static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
  1218. const struct netlbl_lsm_secattr *secattr,
  1219. unsigned char *buffer,
  1220. u32 buffer_len)
  1221. {
  1222. int ret_val;
  1223. u32 tag_len;
  1224. u32 level;
  1225. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1226. return -EPERM;
  1227. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1228. secattr->attr.mls.lvl,
  1229. &level);
  1230. if (ret_val != 0)
  1231. return ret_val;
  1232. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1233. ret_val = cipso_v4_map_cat_rng_hton(doi_def,
  1234. secattr,
  1235. &buffer[4],
  1236. buffer_len - 4);
  1237. if (ret_val < 0)
  1238. return ret_val;
  1239. tag_len = 4 + ret_val;
  1240. } else
  1241. tag_len = 4;
  1242. buffer[0] = CIPSO_V4_TAG_RANGE;
  1243. buffer[1] = tag_len;
  1244. buffer[3] = level;
  1245. return tag_len;
  1246. }
  1247. /**
  1248. * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
  1249. * @doi_def: the DOI definition
  1250. * @tag: the CIPSO tag
  1251. * @secattr: the security attributes
  1252. *
  1253. * Description:
  1254. * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
  1255. * in @secattr. Return zero on success, negatives values on failure.
  1256. *
  1257. */
  1258. static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
  1259. const unsigned char *tag,
  1260. struct netlbl_lsm_secattr *secattr)
  1261. {
  1262. int ret_val;
  1263. u8 tag_len = tag[1];
  1264. u32 level;
  1265. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1266. if (ret_val != 0)
  1267. return ret_val;
  1268. secattr->attr.mls.lvl = level;
  1269. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1270. if (tag_len > 4) {
  1271. ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
  1272. &tag[4],
  1273. tag_len - 4,
  1274. secattr);
  1275. if (ret_val != 0) {
  1276. netlbl_catmap_free(secattr->attr.mls.cat);
  1277. return ret_val;
  1278. }
  1279. if (secattr->attr.mls.cat)
  1280. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1281. }
  1282. return 0;
  1283. }
  1284. /**
  1285. * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
  1286. * @doi_def: the DOI definition
  1287. * @secattr: the security attributes
  1288. * @buffer: the option buffer
  1289. * @buffer_len: length of buffer in bytes
  1290. *
  1291. * Description:
  1292. * Generate a CIPSO option using the local tag. Returns the size of the tag
  1293. * on success, negative values on failure.
  1294. *
  1295. */
  1296. static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
  1297. const struct netlbl_lsm_secattr *secattr,
  1298. unsigned char *buffer,
  1299. u32 buffer_len)
  1300. {
  1301. if (!(secattr->flags & NETLBL_SECATTR_SECID))
  1302. return -EPERM;
  1303. buffer[0] = CIPSO_V4_TAG_LOCAL;
  1304. buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
  1305. *(u32 *)&buffer[2] = secattr->attr.secid;
  1306. return CIPSO_V4_TAG_LOC_BLEN;
  1307. }
  1308. /**
  1309. * cipso_v4_parsetag_loc - Parse a CIPSO local tag
  1310. * @doi_def: the DOI definition
  1311. * @tag: the CIPSO tag
  1312. * @secattr: the security attributes
  1313. *
  1314. * Description:
  1315. * Parse a CIPSO local tag and return the security attributes in @secattr.
  1316. * Return zero on success, negatives values on failure.
  1317. *
  1318. */
  1319. static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
  1320. const unsigned char *tag,
  1321. struct netlbl_lsm_secattr *secattr)
  1322. {
  1323. secattr->attr.secid = *(u32 *)&tag[2];
  1324. secattr->flags |= NETLBL_SECATTR_SECID;
  1325. return 0;
  1326. }
  1327. /**
  1328. * cipso_v4_optptr - Find the CIPSO option in the packet
  1329. * @skb: the packet
  1330. *
  1331. * Description:
  1332. * Parse the packet's IP header looking for a CIPSO option. Returns a pointer
  1333. * to the start of the CIPSO option on success, NULL if one is not found.
  1334. *
  1335. */
  1336. unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
  1337. {
  1338. const struct iphdr *iph = ip_hdr(skb);
  1339. unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]);
  1340. int optlen;
  1341. int taglen;
  1342. for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 1; ) {
  1343. switch (optptr[0]) {
  1344. case IPOPT_END:
  1345. return NULL;
  1346. case IPOPT_NOOP:
  1347. taglen = 1;
  1348. break;
  1349. default:
  1350. taglen = optptr[1];
  1351. }
  1352. if (!taglen || taglen > optlen)
  1353. return NULL;
  1354. if (optptr[0] == IPOPT_CIPSO)
  1355. return optptr;
  1356. optlen -= taglen;
  1357. optptr += taglen;
  1358. }
  1359. return NULL;
  1360. }
  1361. /**
  1362. * cipso_v4_validate - Validate a CIPSO option
  1363. * @skb: the packet
  1364. * @option: the start of the option, on error it is set to point to the error
  1365. *
  1366. * Description:
  1367. * This routine is called to validate a CIPSO option, it checks all of the
  1368. * fields to ensure that they are at least valid, see the draft snippet below
  1369. * for details. If the option is valid then a zero value is returned and
  1370. * the value of @option is unchanged. If the option is invalid then a
  1371. * non-zero value is returned and @option is adjusted to point to the
  1372. * offending portion of the option. From the IETF draft ...
  1373. *
  1374. * "If any field within the CIPSO options, such as the DOI identifier, is not
  1375. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  1376. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1377. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1378. * that is unrecognized."
  1379. *
  1380. */
  1381. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
  1382. {
  1383. unsigned char *opt = *option;
  1384. unsigned char *tag;
  1385. unsigned char opt_iter;
  1386. unsigned char err_offset = 0;
  1387. u8 opt_len;
  1388. u8 tag_len;
  1389. struct cipso_v4_doi *doi_def = NULL;
  1390. u32 tag_iter;
  1391. /* caller already checks for length values that are too large */
  1392. opt_len = opt[1];
  1393. if (opt_len < 8) {
  1394. err_offset = 1;
  1395. goto validate_return;
  1396. }
  1397. rcu_read_lock();
  1398. doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
  1399. if (!doi_def) {
  1400. err_offset = 2;
  1401. goto validate_return_locked;
  1402. }
  1403. opt_iter = CIPSO_V4_HDR_LEN;
  1404. tag = opt + opt_iter;
  1405. while (opt_iter < opt_len) {
  1406. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1407. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1408. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1409. err_offset = opt_iter;
  1410. goto validate_return_locked;
  1411. }
  1412. if (opt_iter + 1 == opt_len) {
  1413. err_offset = opt_iter;
  1414. goto validate_return_locked;
  1415. }
  1416. tag_len = tag[1];
  1417. if (tag_len > (opt_len - opt_iter)) {
  1418. err_offset = opt_iter + 1;
  1419. goto validate_return_locked;
  1420. }
  1421. switch (tag[0]) {
  1422. case CIPSO_V4_TAG_RBITMAP:
  1423. if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
  1424. err_offset = opt_iter + 1;
  1425. goto validate_return_locked;
  1426. }
  1427. /* We are already going to do all the verification
  1428. * necessary at the socket layer so from our point of
  1429. * view it is safe to turn these checks off (and less
  1430. * work), however, the CIPSO draft says we should do
  1431. * all the CIPSO validations here but it doesn't
  1432. * really specify _exactly_ what we need to validate
  1433. * ... so, just make it a sysctl tunable. */
  1434. if (READ_ONCE(cipso_v4_rbm_strictvalid)) {
  1435. if (cipso_v4_map_lvl_valid(doi_def,
  1436. tag[3]) < 0) {
  1437. err_offset = opt_iter + 3;
  1438. goto validate_return_locked;
  1439. }
  1440. if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
  1441. cipso_v4_map_cat_rbm_valid(doi_def,
  1442. &tag[4],
  1443. tag_len - 4) < 0) {
  1444. err_offset = opt_iter + 4;
  1445. goto validate_return_locked;
  1446. }
  1447. }
  1448. break;
  1449. case CIPSO_V4_TAG_ENUM:
  1450. if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
  1451. err_offset = opt_iter + 1;
  1452. goto validate_return_locked;
  1453. }
  1454. if (cipso_v4_map_lvl_valid(doi_def,
  1455. tag[3]) < 0) {
  1456. err_offset = opt_iter + 3;
  1457. goto validate_return_locked;
  1458. }
  1459. if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
  1460. cipso_v4_map_cat_enum_valid(doi_def,
  1461. &tag[4],
  1462. tag_len - 4) < 0) {
  1463. err_offset = opt_iter + 4;
  1464. goto validate_return_locked;
  1465. }
  1466. break;
  1467. case CIPSO_V4_TAG_RANGE:
  1468. if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
  1469. err_offset = opt_iter + 1;
  1470. goto validate_return_locked;
  1471. }
  1472. if (cipso_v4_map_lvl_valid(doi_def,
  1473. tag[3]) < 0) {
  1474. err_offset = opt_iter + 3;
  1475. goto validate_return_locked;
  1476. }
  1477. if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
  1478. cipso_v4_map_cat_rng_valid(doi_def,
  1479. &tag[4],
  1480. tag_len - 4) < 0) {
  1481. err_offset = opt_iter + 4;
  1482. goto validate_return_locked;
  1483. }
  1484. break;
  1485. case CIPSO_V4_TAG_LOCAL:
  1486. /* This is a non-standard tag that we only allow for
  1487. * local connections, so if the incoming interface is
  1488. * not the loopback device drop the packet. Further,
  1489. * there is no legitimate reason for setting this from
  1490. * userspace so reject it if skb is NULL. */
  1491. if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) {
  1492. err_offset = opt_iter;
  1493. goto validate_return_locked;
  1494. }
  1495. if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
  1496. err_offset = opt_iter + 1;
  1497. goto validate_return_locked;
  1498. }
  1499. break;
  1500. default:
  1501. err_offset = opt_iter;
  1502. goto validate_return_locked;
  1503. }
  1504. tag += tag_len;
  1505. opt_iter += tag_len;
  1506. }
  1507. validate_return_locked:
  1508. rcu_read_unlock();
  1509. validate_return:
  1510. *option = opt + err_offset;
  1511. return err_offset;
  1512. }
  1513. /**
  1514. * cipso_v4_error - Send the correct response for a bad packet
  1515. * @skb: the packet
  1516. * @error: the error code
  1517. * @gateway: CIPSO gateway flag
  1518. *
  1519. * Description:
  1520. * Based on the error code given in @error, send an ICMP error message back to
  1521. * the originating host. From the IETF draft ...
  1522. *
  1523. * "If the contents of the CIPSO [option] are valid but the security label is
  1524. * outside of the configured host or port label range, the datagram is
  1525. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1526. * returned. The code field of the ICMP is set to 'communication with
  1527. * destination network administratively prohibited' (code 9) or to
  1528. * 'communication with destination host administratively prohibited'
  1529. * (code 10). The value of the code is dependent on whether the originator
  1530. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1531. * recipient of the ICMP message MUST be able to handle either value. The
  1532. * same procedure is performed if a CIPSO [option] can not be added to an
  1533. * IP packet because it is too large to fit in the IP options area."
  1534. *
  1535. * "If the error is triggered by receipt of an ICMP message, the message is
  1536. * discarded and no response is permitted (consistent with general ICMP
  1537. * processing rules)."
  1538. *
  1539. */
  1540. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1541. {
  1542. unsigned char optbuf[sizeof(struct ip_options) + 40];
  1543. struct ip_options *opt = (struct ip_options *)optbuf;
  1544. int res;
  1545. if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
  1546. return;
  1547. /*
  1548. * We might be called above the IP layer,
  1549. * so we can not use icmp_send and IPCB here.
  1550. */
  1551. memset(opt, 0, sizeof(struct ip_options));
  1552. opt->optlen = ip_hdr(skb)->ihl*4 - sizeof(struct iphdr);
  1553. rcu_read_lock();
  1554. res = __ip_options_compile(dev_net(skb->dev), opt, skb, NULL);
  1555. rcu_read_unlock();
  1556. if (res)
  1557. return;
  1558. if (gateway)
  1559. __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0, opt);
  1560. else
  1561. __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0, opt);
  1562. }
  1563. /**
  1564. * cipso_v4_genopt - Generate a CIPSO option
  1565. * @buf: the option buffer
  1566. * @buf_len: the size of opt_buf
  1567. * @doi_def: the CIPSO DOI to use
  1568. * @secattr: the security attributes
  1569. *
  1570. * Description:
  1571. * Generate a CIPSO option using the DOI definition and security attributes
  1572. * passed to the function. Returns the length of the option on success and
  1573. * negative values on failure.
  1574. *
  1575. */
  1576. static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
  1577. const struct cipso_v4_doi *doi_def,
  1578. const struct netlbl_lsm_secattr *secattr)
  1579. {
  1580. int ret_val;
  1581. u32 iter;
  1582. if (buf_len <= CIPSO_V4_HDR_LEN)
  1583. return -ENOSPC;
  1584. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1585. * really a good assumption to make but since we only support the MAC
  1586. * tags right now it is a safe assumption. */
  1587. iter = 0;
  1588. do {
  1589. memset(buf, 0, buf_len);
  1590. switch (doi_def->tags[iter]) {
  1591. case CIPSO_V4_TAG_RBITMAP:
  1592. ret_val = cipso_v4_gentag_rbm(doi_def,
  1593. secattr,
  1594. &buf[CIPSO_V4_HDR_LEN],
  1595. buf_len - CIPSO_V4_HDR_LEN);
  1596. break;
  1597. case CIPSO_V4_TAG_ENUM:
  1598. ret_val = cipso_v4_gentag_enum(doi_def,
  1599. secattr,
  1600. &buf[CIPSO_V4_HDR_LEN],
  1601. buf_len - CIPSO_V4_HDR_LEN);
  1602. break;
  1603. case CIPSO_V4_TAG_RANGE:
  1604. ret_val = cipso_v4_gentag_rng(doi_def,
  1605. secattr,
  1606. &buf[CIPSO_V4_HDR_LEN],
  1607. buf_len - CIPSO_V4_HDR_LEN);
  1608. break;
  1609. case CIPSO_V4_TAG_LOCAL:
  1610. ret_val = cipso_v4_gentag_loc(doi_def,
  1611. secattr,
  1612. &buf[CIPSO_V4_HDR_LEN],
  1613. buf_len - CIPSO_V4_HDR_LEN);
  1614. break;
  1615. default:
  1616. return -EPERM;
  1617. }
  1618. iter++;
  1619. } while (ret_val < 0 &&
  1620. iter < CIPSO_V4_TAG_MAXCNT &&
  1621. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1622. if (ret_val < 0)
  1623. return ret_val;
  1624. cipso_v4_gentag_hdr(doi_def, buf, ret_val);
  1625. return CIPSO_V4_HDR_LEN + ret_val;
  1626. }
  1627. static int cipso_v4_get_actual_opt_len(const unsigned char *data, int len)
  1628. {
  1629. int iter = 0, optlen = 0;
  1630. /* determining the new total option length is tricky because of
  1631. * the padding necessary, the only thing i can think to do at
  1632. * this point is walk the options one-by-one, skipping the
  1633. * padding at the end to determine the actual option size and
  1634. * from there we can determine the new total option length
  1635. */
  1636. while (iter < len) {
  1637. if (data[iter] == IPOPT_END) {
  1638. break;
  1639. } else if (data[iter] == IPOPT_NOP) {
  1640. iter++;
  1641. } else {
  1642. iter += data[iter + 1];
  1643. optlen = iter;
  1644. }
  1645. }
  1646. return optlen;
  1647. }
  1648. /**
  1649. * cipso_v4_sock_setattr - Add a CIPSO option to a socket
  1650. * @sk: the socket
  1651. * @doi_def: the CIPSO DOI to use
  1652. * @secattr: the specific security attributes of the socket
  1653. * @sk_locked: true if caller holds the socket lock
  1654. *
  1655. * Description:
  1656. * Set the CIPSO option on the given socket using the DOI definition and
  1657. * security attributes passed to the function. This function requires
  1658. * exclusive access to @sk, which means it either needs to be in the
  1659. * process of being created or locked. Returns zero on success and negative
  1660. * values on failure.
  1661. *
  1662. */
  1663. int cipso_v4_sock_setattr(struct sock *sk,
  1664. const struct cipso_v4_doi *doi_def,
  1665. const struct netlbl_lsm_secattr *secattr,
  1666. bool sk_locked)
  1667. {
  1668. int ret_val = -EPERM;
  1669. unsigned char *buf = NULL;
  1670. u32 buf_len;
  1671. u32 opt_len;
  1672. struct ip_options_rcu *old, *opt = NULL;
  1673. struct inet_sock *sk_inet;
  1674. struct inet_connection_sock *sk_conn;
  1675. /* In the case of sock_create_lite(), the sock->sk field is not
  1676. * defined yet but it is not a problem as the only users of these
  1677. * "lite" PF_INET sockets are functions which do an accept() call
  1678. * afterwards so we will label the socket as part of the accept(). */
  1679. if (!sk)
  1680. return 0;
  1681. /* We allocate the maximum CIPSO option size here so we are probably
  1682. * being a little wasteful, but it makes our life _much_ easier later
  1683. * on and after all we are only talking about 40 bytes. */
  1684. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1685. buf = kmalloc(buf_len, GFP_ATOMIC);
  1686. if (!buf) {
  1687. ret_val = -ENOMEM;
  1688. goto socket_setattr_failure;
  1689. }
  1690. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1691. if (ret_val < 0)
  1692. goto socket_setattr_failure;
  1693. buf_len = ret_val;
  1694. /* We can't use ip_options_get() directly because it makes a call to
  1695. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1696. * we won't always have CAP_NET_RAW even though we _always_ want to
  1697. * set the IPOPT_CIPSO option. */
  1698. opt_len = (buf_len + 3) & ~3;
  1699. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1700. if (!opt) {
  1701. ret_val = -ENOMEM;
  1702. goto socket_setattr_failure;
  1703. }
  1704. memcpy(opt->opt.__data, buf, buf_len);
  1705. opt->opt.optlen = opt_len;
  1706. opt->opt.cipso = sizeof(struct iphdr);
  1707. kfree(buf);
  1708. buf = NULL;
  1709. sk_inet = inet_sk(sk);
  1710. old = rcu_dereference_protected(sk_inet->inet_opt, sk_locked);
  1711. if (inet_test_bit(IS_ICSK, sk)) {
  1712. sk_conn = inet_csk(sk);
  1713. if (old)
  1714. sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
  1715. sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
  1716. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1717. }
  1718. rcu_assign_pointer(sk_inet->inet_opt, opt);
  1719. if (old)
  1720. kfree_rcu(old, rcu);
  1721. return 0;
  1722. socket_setattr_failure:
  1723. kfree(buf);
  1724. kfree(opt);
  1725. return ret_val;
  1726. }
  1727. /**
  1728. * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
  1729. * @req: the connection request socket
  1730. * @doi_def: the CIPSO DOI to use
  1731. * @secattr: the specific security attributes of the socket
  1732. *
  1733. * Description:
  1734. * Set the CIPSO option on the given socket using the DOI definition and
  1735. * security attributes passed to the function. Returns zero on success and
  1736. * negative values on failure.
  1737. *
  1738. */
  1739. int cipso_v4_req_setattr(struct request_sock *req,
  1740. const struct cipso_v4_doi *doi_def,
  1741. const struct netlbl_lsm_secattr *secattr)
  1742. {
  1743. int ret_val = -EPERM;
  1744. unsigned char *buf = NULL;
  1745. u32 buf_len;
  1746. u32 opt_len;
  1747. struct ip_options_rcu *opt = NULL;
  1748. struct inet_request_sock *req_inet;
  1749. /* We allocate the maximum CIPSO option size here so we are probably
  1750. * being a little wasteful, but it makes our life _much_ easier later
  1751. * on and after all we are only talking about 40 bytes. */
  1752. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1753. buf = kmalloc(buf_len, GFP_ATOMIC);
  1754. if (!buf) {
  1755. ret_val = -ENOMEM;
  1756. goto req_setattr_failure;
  1757. }
  1758. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1759. if (ret_val < 0)
  1760. goto req_setattr_failure;
  1761. buf_len = ret_val;
  1762. /* We can't use ip_options_get() directly because it makes a call to
  1763. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1764. * we won't always have CAP_NET_RAW even though we _always_ want to
  1765. * set the IPOPT_CIPSO option. */
  1766. opt_len = (buf_len + 3) & ~3;
  1767. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1768. if (!opt) {
  1769. ret_val = -ENOMEM;
  1770. goto req_setattr_failure;
  1771. }
  1772. memcpy(opt->opt.__data, buf, buf_len);
  1773. opt->opt.optlen = opt_len;
  1774. opt->opt.cipso = sizeof(struct iphdr);
  1775. kfree(buf);
  1776. buf = NULL;
  1777. req_inet = inet_rsk(req);
  1778. opt = unrcu_pointer(xchg(&req_inet->ireq_opt, RCU_INITIALIZER(opt)));
  1779. if (opt)
  1780. kfree_rcu(opt, rcu);
  1781. return 0;
  1782. req_setattr_failure:
  1783. kfree(buf);
  1784. kfree(opt);
  1785. return ret_val;
  1786. }
  1787. /**
  1788. * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
  1789. * @opt_ptr: IP option pointer
  1790. *
  1791. * Description:
  1792. * Deletes the CIPSO IP option from a set of IP options and makes the necessary
  1793. * adjustments to the IP option structure. Returns zero on success, negative
  1794. * values on failure.
  1795. *
  1796. */
  1797. static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr)
  1798. {
  1799. struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1);
  1800. int hdr_delta = 0;
  1801. if (!opt || opt->opt.cipso == 0)
  1802. return 0;
  1803. if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {
  1804. u8 cipso_len;
  1805. u8 cipso_off;
  1806. unsigned char *cipso_ptr;
  1807. int optlen_new;
  1808. cipso_off = opt->opt.cipso - sizeof(struct iphdr);
  1809. cipso_ptr = &opt->opt.__data[cipso_off];
  1810. cipso_len = cipso_ptr[1];
  1811. if (opt->opt.srr > opt->opt.cipso)
  1812. opt->opt.srr -= cipso_len;
  1813. if (opt->opt.rr > opt->opt.cipso)
  1814. opt->opt.rr -= cipso_len;
  1815. if (opt->opt.ts > opt->opt.cipso)
  1816. opt->opt.ts -= cipso_len;
  1817. if (opt->opt.router_alert > opt->opt.cipso)
  1818. opt->opt.router_alert -= cipso_len;
  1819. opt->opt.cipso = 0;
  1820. memmove(cipso_ptr, cipso_ptr + cipso_len,
  1821. opt->opt.optlen - cipso_off - cipso_len);
  1822. optlen_new = cipso_v4_get_actual_opt_len(opt->opt.__data,
  1823. opt->opt.optlen);
  1824. hdr_delta = opt->opt.optlen;
  1825. opt->opt.optlen = (optlen_new + 3) & ~3;
  1826. hdr_delta -= opt->opt.optlen;
  1827. } else {
  1828. /* only the cipso option was present on the socket so we can
  1829. * remove the entire option struct */
  1830. *opt_ptr = NULL;
  1831. hdr_delta = opt->opt.optlen;
  1832. kfree_rcu(opt, rcu);
  1833. }
  1834. return hdr_delta;
  1835. }
  1836. /**
  1837. * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
  1838. * @sk: the socket
  1839. *
  1840. * Description:
  1841. * Removes the CIPSO option from a socket, if present.
  1842. *
  1843. */
  1844. void cipso_v4_sock_delattr(struct sock *sk)
  1845. {
  1846. struct inet_sock *sk_inet;
  1847. int hdr_delta;
  1848. sk_inet = inet_sk(sk);
  1849. hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
  1850. if (inet_test_bit(IS_ICSK, sk) && hdr_delta > 0) {
  1851. struct inet_connection_sock *sk_conn = inet_csk(sk);
  1852. sk_conn->icsk_ext_hdr_len -= hdr_delta;
  1853. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1854. }
  1855. }
  1856. /**
  1857. * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
  1858. * @req: the request socket
  1859. *
  1860. * Description:
  1861. * Removes the CIPSO option from a request socket, if present.
  1862. *
  1863. */
  1864. void cipso_v4_req_delattr(struct request_sock *req)
  1865. {
  1866. cipso_v4_delopt(&inet_rsk(req)->ireq_opt);
  1867. }
  1868. /**
  1869. * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
  1870. * @cipso: the CIPSO v4 option
  1871. * @secattr: the security attributes
  1872. *
  1873. * Description:
  1874. * Inspect @cipso and return the security attributes in @secattr. Returns zero
  1875. * on success and negative values on failure.
  1876. *
  1877. */
  1878. int cipso_v4_getattr(const unsigned char *cipso,
  1879. struct netlbl_lsm_secattr *secattr)
  1880. {
  1881. int ret_val = -ENOMSG;
  1882. u32 doi;
  1883. struct cipso_v4_doi *doi_def;
  1884. if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
  1885. return 0;
  1886. doi = get_unaligned_be32(&cipso[2]);
  1887. rcu_read_lock();
  1888. doi_def = cipso_v4_doi_search(doi);
  1889. if (!doi_def)
  1890. goto getattr_return;
  1891. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1892. * really a good assumption to make but since we only support the MAC
  1893. * tags right now it is a safe assumption. */
  1894. switch (cipso[6]) {
  1895. case CIPSO_V4_TAG_RBITMAP:
  1896. ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
  1897. break;
  1898. case CIPSO_V4_TAG_ENUM:
  1899. ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
  1900. break;
  1901. case CIPSO_V4_TAG_RANGE:
  1902. ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
  1903. break;
  1904. case CIPSO_V4_TAG_LOCAL:
  1905. ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
  1906. break;
  1907. }
  1908. if (ret_val == 0)
  1909. secattr->type = NETLBL_NLTYPE_CIPSOV4;
  1910. getattr_return:
  1911. rcu_read_unlock();
  1912. return ret_val;
  1913. }
  1914. /**
  1915. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1916. * @sk: the sock
  1917. * @secattr: the security attributes
  1918. *
  1919. * Description:
  1920. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1921. * there is return the CIPSO security attributes in @secattr. This function
  1922. * requires that @sk be locked, or privately held, but it does not do any
  1923. * locking itself. Returns zero on success and negative values on failure.
  1924. *
  1925. */
  1926. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1927. {
  1928. struct ip_options_rcu *opt;
  1929. int res = -ENOMSG;
  1930. rcu_read_lock();
  1931. opt = rcu_dereference(inet_sk(sk)->inet_opt);
  1932. if (opt && opt->opt.cipso)
  1933. res = cipso_v4_getattr(opt->opt.__data +
  1934. opt->opt.cipso -
  1935. sizeof(struct iphdr),
  1936. secattr);
  1937. rcu_read_unlock();
  1938. return res;
  1939. }
  1940. /**
  1941. * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
  1942. * @skb: the packet
  1943. * @doi_def: the DOI structure
  1944. * @secattr: the security attributes
  1945. *
  1946. * Description:
  1947. * Set the CIPSO option on the given packet based on the security attributes.
  1948. * Returns a pointer to the IP header on success and NULL on failure.
  1949. *
  1950. */
  1951. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  1952. const struct cipso_v4_doi *doi_def,
  1953. const struct netlbl_lsm_secattr *secattr)
  1954. {
  1955. int ret_val;
  1956. struct iphdr *iph;
  1957. struct ip_options *opt = &IPCB(skb)->opt;
  1958. unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
  1959. u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
  1960. u32 opt_len;
  1961. int len_delta;
  1962. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1963. if (ret_val < 0)
  1964. return ret_val;
  1965. buf_len = ret_val;
  1966. opt_len = (buf_len + 3) & ~3;
  1967. /* we overwrite any existing options to ensure that we have enough
  1968. * room for the CIPSO option, the reason is that we _need_ to guarantee
  1969. * that the security label is applied to the packet - we do the same
  1970. * thing when using the socket options and it hasn't caused a problem,
  1971. * if we need to we can always revisit this choice later */
  1972. len_delta = opt_len - opt->optlen;
  1973. /* if we don't ensure enough headroom we could panic on the skb_push()
  1974. * call below so make sure we have enough, we are also "mangling" the
  1975. * packet so we should probably do a copy-on-write call anyway */
  1976. ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
  1977. if (ret_val < 0)
  1978. return ret_val;
  1979. if (len_delta > 0) {
  1980. /* we assume that the header + opt->optlen have already been
  1981. * "pushed" in ip_options_build() or similar */
  1982. iph = ip_hdr(skb);
  1983. skb_push(skb, len_delta);
  1984. memmove((char *)iph - len_delta, iph, iph->ihl << 2);
  1985. skb_reset_network_header(skb);
  1986. iph = ip_hdr(skb);
  1987. } else if (len_delta < 0) {
  1988. iph = ip_hdr(skb);
  1989. memset(iph + 1, IPOPT_NOP, opt->optlen);
  1990. } else
  1991. iph = ip_hdr(skb);
  1992. if (opt->optlen > 0)
  1993. memset(opt, 0, sizeof(*opt));
  1994. opt->optlen = opt_len;
  1995. opt->cipso = sizeof(struct iphdr);
  1996. opt->is_changed = 1;
  1997. /* we have to do the following because we are being called from a
  1998. * netfilter hook which means the packet already has had the header
  1999. * fields populated and the checksum calculated - yes this means we
  2000. * are doing more work than needed but we do it to keep the core
  2001. * stack clean and tidy */
  2002. memcpy(iph + 1, buf, buf_len);
  2003. if (opt_len > buf_len)
  2004. memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
  2005. if (len_delta != 0) {
  2006. iph->ihl = 5 + (opt_len >> 2);
  2007. iph_set_totlen(iph, skb->len);
  2008. }
  2009. ip_send_check(iph);
  2010. return 0;
  2011. }
  2012. /**
  2013. * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
  2014. * @skb: the packet
  2015. *
  2016. * Description:
  2017. * Removes any and all CIPSO options from the given packet. Returns zero on
  2018. * success, negative values on failure.
  2019. *
  2020. */
  2021. int cipso_v4_skbuff_delattr(struct sk_buff *skb)
  2022. {
  2023. int ret_val, cipso_len, hdr_len_actual, new_hdr_len_actual, new_hdr_len,
  2024. hdr_len_delta;
  2025. struct iphdr *iph;
  2026. struct ip_options *opt = &IPCB(skb)->opt;
  2027. unsigned char *cipso_ptr;
  2028. if (opt->cipso == 0)
  2029. return 0;
  2030. /* since we are changing the packet we should make a copy */
  2031. ret_val = skb_cow(skb, skb_headroom(skb));
  2032. if (ret_val < 0)
  2033. return ret_val;
  2034. iph = ip_hdr(skb);
  2035. cipso_ptr = (unsigned char *)iph + opt->cipso;
  2036. cipso_len = cipso_ptr[1];
  2037. hdr_len_actual = sizeof(struct iphdr) +
  2038. cipso_v4_get_actual_opt_len((unsigned char *)(iph + 1),
  2039. opt->optlen);
  2040. new_hdr_len_actual = hdr_len_actual - cipso_len;
  2041. new_hdr_len = (new_hdr_len_actual + 3) & ~3;
  2042. hdr_len_delta = (iph->ihl << 2) - new_hdr_len;
  2043. /* 1. shift any options after CIPSO to the left */
  2044. memmove(cipso_ptr, cipso_ptr + cipso_len,
  2045. new_hdr_len_actual - opt->cipso);
  2046. /* 2. move the whole IP header to its new place */
  2047. memmove((unsigned char *)iph + hdr_len_delta, iph, new_hdr_len_actual);
  2048. /* 3. adjust the skb layout */
  2049. skb_pull(skb, hdr_len_delta);
  2050. skb_reset_network_header(skb);
  2051. iph = ip_hdr(skb);
  2052. /* 4. re-fill new padding with IPOPT_END (may now be longer) */
  2053. memset((unsigned char *)iph + new_hdr_len_actual, IPOPT_END,
  2054. new_hdr_len - new_hdr_len_actual);
  2055. opt->optlen -= hdr_len_delta;
  2056. opt->cipso = 0;
  2057. opt->is_changed = 1;
  2058. if (hdr_len_delta != 0) {
  2059. iph->ihl = new_hdr_len >> 2;
  2060. iph_set_totlen(iph, skb->len);
  2061. }
  2062. ip_send_check(iph);
  2063. return 0;
  2064. }
  2065. /*
  2066. * Setup Functions
  2067. */
  2068. /**
  2069. * cipso_v4_init - Initialize the CIPSO module
  2070. *
  2071. * Description:
  2072. * Initialize the CIPSO module and prepare it for use. Returns zero on success
  2073. * and negative values on failure.
  2074. *
  2075. */
  2076. static int __init cipso_v4_init(void)
  2077. {
  2078. int ret_val;
  2079. ret_val = cipso_v4_cache_init();
  2080. if (ret_val != 0)
  2081. panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
  2082. ret_val);
  2083. return 0;
  2084. }
  2085. subsys_initcall(cipso_v4_init);