security.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Security plug functions
  4. *
  5. * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
  6. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  7. * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
  8. * Copyright (C) 2016 Mellanox Technologies
  9. */
  10. #define pr_fmt(fmt) "LSM: " fmt
  11. #include <linux/bpf.h>
  12. #include <linux/capability.h>
  13. #include <linux/dcache.h>
  14. #include <linux/export.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/lsm_hooks.h>
  18. #include <linux/integrity.h>
  19. #include <linux/ima.h>
  20. #include <linux/evm.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/mman.h>
  23. #include <linux/mount.h>
  24. #include <linux/personality.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/string.h>
  27. #include <linux/msg.h>
  28. #include <net/flow.h>
  29. #define MAX_LSM_EVM_XATTR 2
  30. /* How many LSMs were built into the kernel? */
  31. #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
  32. #define EARLY_LSM_COUNT (__end_early_lsm_info - __start_early_lsm_info)
  33. struct security_hook_heads security_hook_heads __lsm_ro_after_init;
  34. static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
  35. static struct kmem_cache *lsm_file_cache;
  36. static struct kmem_cache *lsm_inode_cache;
  37. char *lsm_names;
  38. static struct lsm_blob_sizes blob_sizes __lsm_ro_after_init;
  39. /* Boot-time LSM user choice */
  40. static __initdata const char *chosen_lsm_order;
  41. static __initdata const char *chosen_major_lsm;
  42. static __initconst const char * const builtin_lsm_order = CONFIG_LSM;
  43. /* Ordered list of LSMs to initialize. */
  44. static __initdata struct lsm_info **ordered_lsms;
  45. static __initdata struct lsm_info *exclusive;
  46. static __initdata bool debug;
  47. #define init_debug(...) \
  48. do { \
  49. if (debug) \
  50. pr_info(__VA_ARGS__); \
  51. } while (0)
  52. static bool __init is_enabled(struct lsm_info *lsm)
  53. {
  54. if (!lsm->enabled)
  55. return false;
  56. return *lsm->enabled;
  57. }
  58. /* Mark an LSM's enabled flag. */
  59. static int lsm_enabled_true __initdata = 1;
  60. static int lsm_enabled_false __initdata = 0;
  61. static void __init set_enabled(struct lsm_info *lsm, bool enabled)
  62. {
  63. /*
  64. * When an LSM hasn't configured an enable variable, we can use
  65. * a hard-coded location for storing the default enabled state.
  66. */
  67. if (!lsm->enabled) {
  68. if (enabled)
  69. lsm->enabled = &lsm_enabled_true;
  70. else
  71. lsm->enabled = &lsm_enabled_false;
  72. } else if (lsm->enabled == &lsm_enabled_true) {
  73. if (!enabled)
  74. lsm->enabled = &lsm_enabled_false;
  75. } else if (lsm->enabled == &lsm_enabled_false) {
  76. if (enabled)
  77. lsm->enabled = &lsm_enabled_true;
  78. } else {
  79. *lsm->enabled = enabled;
  80. }
  81. }
  82. /* Is an LSM already listed in the ordered LSMs list? */
  83. static bool __init exists_ordered_lsm(struct lsm_info *lsm)
  84. {
  85. struct lsm_info **check;
  86. for (check = ordered_lsms; *check; check++)
  87. if (*check == lsm)
  88. return true;
  89. return false;
  90. }
  91. /* Append an LSM to the list of ordered LSMs to initialize. */
  92. static int last_lsm __initdata;
  93. static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
  94. {
  95. /* Ignore duplicate selections. */
  96. if (exists_ordered_lsm(lsm))
  97. return;
  98. if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
  99. return;
  100. /* Enable this LSM, if it is not already set. */
  101. if (!lsm->enabled)
  102. lsm->enabled = &lsm_enabled_true;
  103. ordered_lsms[last_lsm++] = lsm;
  104. init_debug("%s ordering: %s (%sabled)\n", from, lsm->name,
  105. is_enabled(lsm) ? "en" : "dis");
  106. }
  107. /* Is an LSM allowed to be initialized? */
  108. static bool __init lsm_allowed(struct lsm_info *lsm)
  109. {
  110. /* Skip if the LSM is disabled. */
  111. if (!is_enabled(lsm))
  112. return false;
  113. /* Not allowed if another exclusive LSM already initialized. */
  114. if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
  115. init_debug("exclusive disabled: %s\n", lsm->name);
  116. return false;
  117. }
  118. return true;
  119. }
  120. static void __init lsm_set_blob_size(int *need, int *lbs)
  121. {
  122. int offset;
  123. if (*need > 0) {
  124. offset = *lbs;
  125. *lbs += *need;
  126. *need = offset;
  127. }
  128. }
  129. static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
  130. {
  131. if (!needed)
  132. return;
  133. lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
  134. lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
  135. /*
  136. * The inode blob gets an rcu_head in addition to
  137. * what the modules might need.
  138. */
  139. if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
  140. blob_sizes.lbs_inode = sizeof(struct rcu_head);
  141. lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
  142. lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
  143. lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
  144. lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
  145. }
  146. /* Prepare LSM for initialization. */
  147. static void __init prepare_lsm(struct lsm_info *lsm)
  148. {
  149. int enabled = lsm_allowed(lsm);
  150. /* Record enablement (to handle any following exclusive LSMs). */
  151. set_enabled(lsm, enabled);
  152. /* If enabled, do pre-initialization work. */
  153. if (enabled) {
  154. if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
  155. exclusive = lsm;
  156. init_debug("exclusive chosen: %s\n", lsm->name);
  157. }
  158. lsm_set_blob_sizes(lsm->blobs);
  159. }
  160. }
  161. /* Initialize a given LSM, if it is enabled. */
  162. static void __init initialize_lsm(struct lsm_info *lsm)
  163. {
  164. if (is_enabled(lsm)) {
  165. int ret;
  166. init_debug("initializing %s\n", lsm->name);
  167. ret = lsm->init();
  168. WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
  169. }
  170. }
  171. /* Populate ordered LSMs list from comma-separated LSM name list. */
  172. static void __init ordered_lsm_parse(const char *order, const char *origin)
  173. {
  174. struct lsm_info *lsm;
  175. char *sep, *name, *next;
  176. /* LSM_ORDER_FIRST is always first. */
  177. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  178. if (lsm->order == LSM_ORDER_FIRST)
  179. append_ordered_lsm(lsm, "first");
  180. }
  181. /* Process "security=", if given. */
  182. if (chosen_major_lsm) {
  183. struct lsm_info *major;
  184. /*
  185. * To match the original "security=" behavior, this
  186. * explicitly does NOT fallback to another Legacy Major
  187. * if the selected one was separately disabled: disable
  188. * all non-matching Legacy Major LSMs.
  189. */
  190. for (major = __start_lsm_info; major < __end_lsm_info;
  191. major++) {
  192. if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
  193. strcmp(major->name, chosen_major_lsm) != 0) {
  194. set_enabled(major, false);
  195. init_debug("security=%s disabled: %s\n",
  196. chosen_major_lsm, major->name);
  197. }
  198. }
  199. }
  200. sep = kstrdup(order, GFP_KERNEL);
  201. next = sep;
  202. /* Walk the list, looking for matching LSMs. */
  203. while ((name = strsep(&next, ",")) != NULL) {
  204. bool found = false;
  205. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  206. if (lsm->order == LSM_ORDER_MUTABLE &&
  207. strcmp(lsm->name, name) == 0) {
  208. append_ordered_lsm(lsm, origin);
  209. found = true;
  210. }
  211. }
  212. if (!found)
  213. init_debug("%s ignored: %s\n", origin, name);
  214. }
  215. /* Process "security=", if given. */
  216. if (chosen_major_lsm) {
  217. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  218. if (exists_ordered_lsm(lsm))
  219. continue;
  220. if (strcmp(lsm->name, chosen_major_lsm) == 0)
  221. append_ordered_lsm(lsm, "security=");
  222. }
  223. }
  224. /* Disable all LSMs not in the ordered list. */
  225. for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
  226. if (exists_ordered_lsm(lsm))
  227. continue;
  228. set_enabled(lsm, false);
  229. init_debug("%s disabled: %s\n", origin, lsm->name);
  230. }
  231. kfree(sep);
  232. }
  233. static void __init lsm_early_cred(struct cred *cred);
  234. static void __init lsm_early_task(struct task_struct *task);
  235. static int lsm_append(const char *new, char **result);
  236. static void __init ordered_lsm_init(void)
  237. {
  238. struct lsm_info **lsm;
  239. ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
  240. GFP_KERNEL);
  241. if (chosen_lsm_order) {
  242. if (chosen_major_lsm) {
  243. pr_info("security= is ignored because it is superseded by lsm=\n");
  244. chosen_major_lsm = NULL;
  245. }
  246. ordered_lsm_parse(chosen_lsm_order, "cmdline");
  247. } else
  248. ordered_lsm_parse(builtin_lsm_order, "builtin");
  249. for (lsm = ordered_lsms; *lsm; lsm++)
  250. prepare_lsm(*lsm);
  251. init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
  252. init_debug("file blob size = %d\n", blob_sizes.lbs_file);
  253. init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
  254. init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
  255. init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
  256. init_debug("task blob size = %d\n", blob_sizes.lbs_task);
  257. /*
  258. * Create any kmem_caches needed for blobs
  259. */
  260. if (blob_sizes.lbs_file)
  261. lsm_file_cache = kmem_cache_create("lsm_file_cache",
  262. blob_sizes.lbs_file, 0,
  263. SLAB_PANIC, NULL);
  264. if (blob_sizes.lbs_inode)
  265. lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
  266. blob_sizes.lbs_inode, 0,
  267. SLAB_PANIC, NULL);
  268. lsm_early_cred((struct cred *) current->cred);
  269. lsm_early_task(current);
  270. for (lsm = ordered_lsms; *lsm; lsm++)
  271. initialize_lsm(*lsm);
  272. kfree(ordered_lsms);
  273. }
  274. int __init early_security_init(void)
  275. {
  276. int i;
  277. struct hlist_head *list = (struct hlist_head *) &security_hook_heads;
  278. struct lsm_info *lsm;
  279. for (i = 0; i < sizeof(security_hook_heads) / sizeof(struct hlist_head);
  280. i++)
  281. INIT_HLIST_HEAD(&list[i]);
  282. for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
  283. if (!lsm->enabled)
  284. lsm->enabled = &lsm_enabled_true;
  285. prepare_lsm(lsm);
  286. initialize_lsm(lsm);
  287. }
  288. return 0;
  289. }
  290. /**
  291. * security_init - initializes the security framework
  292. *
  293. * This should be called early in the kernel initialization sequence.
  294. */
  295. int __init security_init(void)
  296. {
  297. struct lsm_info *lsm;
  298. pr_info("Security Framework initializing\n");
  299. /*
  300. * Append the names of the early LSM modules now that kmalloc() is
  301. * available
  302. */
  303. for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
  304. if (lsm->enabled)
  305. lsm_append(lsm->name, &lsm_names);
  306. }
  307. /* Load LSMs in specified order. */
  308. ordered_lsm_init();
  309. return 0;
  310. }
  311. /* Save user chosen LSM */
  312. static int __init choose_major_lsm(char *str)
  313. {
  314. chosen_major_lsm = str;
  315. return 1;
  316. }
  317. __setup("security=", choose_major_lsm);
  318. /* Explicitly choose LSM initialization order. */
  319. static int __init choose_lsm_order(char *str)
  320. {
  321. chosen_lsm_order = str;
  322. return 1;
  323. }
  324. __setup("lsm=", choose_lsm_order);
  325. /* Enable LSM order debugging. */
  326. static int __init enable_debug(char *str)
  327. {
  328. debug = true;
  329. return 1;
  330. }
  331. __setup("lsm.debug", enable_debug);
  332. static bool match_last_lsm(const char *list, const char *lsm)
  333. {
  334. const char *last;
  335. if (WARN_ON(!list || !lsm))
  336. return false;
  337. last = strrchr(list, ',');
  338. if (last)
  339. /* Pass the comma, strcmp() will check for '\0' */
  340. last++;
  341. else
  342. last = list;
  343. return !strcmp(last, lsm);
  344. }
  345. static int lsm_append(const char *new, char **result)
  346. {
  347. char *cp;
  348. if (*result == NULL) {
  349. *result = kstrdup(new, GFP_KERNEL);
  350. if (*result == NULL)
  351. return -ENOMEM;
  352. } else {
  353. /* Check if it is the last registered name */
  354. if (match_last_lsm(*result, new))
  355. return 0;
  356. cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
  357. if (cp == NULL)
  358. return -ENOMEM;
  359. kfree(*result);
  360. *result = cp;
  361. }
  362. return 0;
  363. }
  364. /**
  365. * security_add_hooks - Add a modules hooks to the hook lists.
  366. * @hooks: the hooks to add
  367. * @count: the number of hooks to add
  368. * @lsm: the name of the security module
  369. *
  370. * Each LSM has to register its hooks with the infrastructure.
  371. */
  372. void __init security_add_hooks(struct security_hook_list *hooks, int count,
  373. char *lsm)
  374. {
  375. int i;
  376. for (i = 0; i < count; i++) {
  377. hooks[i].lsm = lsm;
  378. hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
  379. }
  380. /*
  381. * Don't try to append during early_security_init(), we'll come back
  382. * and fix this up afterwards.
  383. */
  384. if (slab_is_available()) {
  385. if (lsm_append(lsm, &lsm_names) < 0)
  386. panic("%s - Cannot get early memory.\n", __func__);
  387. }
  388. }
  389. int call_blocking_lsm_notifier(enum lsm_event event, void *data)
  390. {
  391. return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
  392. event, data);
  393. }
  394. EXPORT_SYMBOL(call_blocking_lsm_notifier);
  395. int register_blocking_lsm_notifier(struct notifier_block *nb)
  396. {
  397. return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
  398. nb);
  399. }
  400. EXPORT_SYMBOL(register_blocking_lsm_notifier);
  401. int unregister_blocking_lsm_notifier(struct notifier_block *nb)
  402. {
  403. return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
  404. nb);
  405. }
  406. EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
  407. /**
  408. * lsm_cred_alloc - allocate a composite cred blob
  409. * @cred: the cred that needs a blob
  410. * @gfp: allocation type
  411. *
  412. * Allocate the cred blob for all the modules
  413. *
  414. * Returns 0, or -ENOMEM if memory can't be allocated.
  415. */
  416. static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
  417. {
  418. if (blob_sizes.lbs_cred == 0) {
  419. cred->security = NULL;
  420. return 0;
  421. }
  422. cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
  423. if (cred->security == NULL)
  424. return -ENOMEM;
  425. return 0;
  426. }
  427. /**
  428. * lsm_early_cred - during initialization allocate a composite cred blob
  429. * @cred: the cred that needs a blob
  430. *
  431. * Allocate the cred blob for all the modules
  432. */
  433. static void __init lsm_early_cred(struct cred *cred)
  434. {
  435. int rc = lsm_cred_alloc(cred, GFP_KERNEL);
  436. if (rc)
  437. panic("%s: Early cred alloc failed.\n", __func__);
  438. }
  439. /**
  440. * lsm_file_alloc - allocate a composite file blob
  441. * @file: the file that needs a blob
  442. *
  443. * Allocate the file blob for all the modules
  444. *
  445. * Returns 0, or -ENOMEM if memory can't be allocated.
  446. */
  447. static int lsm_file_alloc(struct file *file)
  448. {
  449. if (!lsm_file_cache) {
  450. file->f_security = NULL;
  451. return 0;
  452. }
  453. file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
  454. if (file->f_security == NULL)
  455. return -ENOMEM;
  456. return 0;
  457. }
  458. /**
  459. * lsm_inode_alloc - allocate a composite inode blob
  460. * @inode: the inode that needs a blob
  461. *
  462. * Allocate the inode blob for all the modules
  463. *
  464. * Returns 0, or -ENOMEM if memory can't be allocated.
  465. */
  466. int lsm_inode_alloc(struct inode *inode)
  467. {
  468. if (!lsm_inode_cache) {
  469. inode->i_security = NULL;
  470. return 0;
  471. }
  472. inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
  473. if (inode->i_security == NULL)
  474. return -ENOMEM;
  475. return 0;
  476. }
  477. /**
  478. * lsm_task_alloc - allocate a composite task blob
  479. * @task: the task that needs a blob
  480. *
  481. * Allocate the task blob for all the modules
  482. *
  483. * Returns 0, or -ENOMEM if memory can't be allocated.
  484. */
  485. static int lsm_task_alloc(struct task_struct *task)
  486. {
  487. if (blob_sizes.lbs_task == 0) {
  488. task->security = NULL;
  489. return 0;
  490. }
  491. task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
  492. if (task->security == NULL)
  493. return -ENOMEM;
  494. return 0;
  495. }
  496. /**
  497. * lsm_ipc_alloc - allocate a composite ipc blob
  498. * @kip: the ipc that needs a blob
  499. *
  500. * Allocate the ipc blob for all the modules
  501. *
  502. * Returns 0, or -ENOMEM if memory can't be allocated.
  503. */
  504. static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
  505. {
  506. if (blob_sizes.lbs_ipc == 0) {
  507. kip->security = NULL;
  508. return 0;
  509. }
  510. kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
  511. if (kip->security == NULL)
  512. return -ENOMEM;
  513. return 0;
  514. }
  515. /**
  516. * lsm_msg_msg_alloc - allocate a composite msg_msg blob
  517. * @mp: the msg_msg that needs a blob
  518. *
  519. * Allocate the ipc blob for all the modules
  520. *
  521. * Returns 0, or -ENOMEM if memory can't be allocated.
  522. */
  523. static int lsm_msg_msg_alloc(struct msg_msg *mp)
  524. {
  525. if (blob_sizes.lbs_msg_msg == 0) {
  526. mp->security = NULL;
  527. return 0;
  528. }
  529. mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
  530. if (mp->security == NULL)
  531. return -ENOMEM;
  532. return 0;
  533. }
  534. /**
  535. * lsm_early_task - during initialization allocate a composite task blob
  536. * @task: the task that needs a blob
  537. *
  538. * Allocate the task blob for all the modules
  539. */
  540. static void __init lsm_early_task(struct task_struct *task)
  541. {
  542. int rc = lsm_task_alloc(task);
  543. if (rc)
  544. panic("%s: Early task alloc failed.\n", __func__);
  545. }
  546. /*
  547. * Hook list operation macros.
  548. *
  549. * call_void_hook:
  550. * This is a hook that does not return a value.
  551. *
  552. * call_int_hook:
  553. * This is a hook that returns a value.
  554. */
  555. #define call_void_hook(FUNC, ...) \
  556. do { \
  557. struct security_hook_list *P; \
  558. \
  559. hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
  560. P->hook.FUNC(__VA_ARGS__); \
  561. } while (0)
  562. #define call_int_hook(FUNC, IRC, ...) ({ \
  563. int RC = IRC; \
  564. do { \
  565. struct security_hook_list *P; \
  566. \
  567. hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
  568. RC = P->hook.FUNC(__VA_ARGS__); \
  569. if (RC != 0) \
  570. break; \
  571. } \
  572. } while (0); \
  573. RC; \
  574. })
  575. /* Security operations */
  576. int security_binder_set_context_mgr(struct task_struct *mgr)
  577. {
  578. return call_int_hook(binder_set_context_mgr, 0, mgr);
  579. }
  580. int security_binder_transaction(struct task_struct *from,
  581. struct task_struct *to)
  582. {
  583. return call_int_hook(binder_transaction, 0, from, to);
  584. }
  585. int security_binder_transfer_binder(struct task_struct *from,
  586. struct task_struct *to)
  587. {
  588. return call_int_hook(binder_transfer_binder, 0, from, to);
  589. }
  590. int security_binder_transfer_file(struct task_struct *from,
  591. struct task_struct *to, struct file *file)
  592. {
  593. return call_int_hook(binder_transfer_file, 0, from, to, file);
  594. }
  595. int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
  596. {
  597. return call_int_hook(ptrace_access_check, 0, child, mode);
  598. }
  599. int security_ptrace_traceme(struct task_struct *parent)
  600. {
  601. return call_int_hook(ptrace_traceme, 0, parent);
  602. }
  603. int security_capget(struct task_struct *target,
  604. kernel_cap_t *effective,
  605. kernel_cap_t *inheritable,
  606. kernel_cap_t *permitted)
  607. {
  608. return call_int_hook(capget, 0, target,
  609. effective, inheritable, permitted);
  610. }
  611. int security_capset(struct cred *new, const struct cred *old,
  612. const kernel_cap_t *effective,
  613. const kernel_cap_t *inheritable,
  614. const kernel_cap_t *permitted)
  615. {
  616. return call_int_hook(capset, 0, new, old,
  617. effective, inheritable, permitted);
  618. }
  619. int security_capable(const struct cred *cred,
  620. struct user_namespace *ns,
  621. int cap,
  622. unsigned int opts)
  623. {
  624. return call_int_hook(capable, 0, cred, ns, cap, opts);
  625. }
  626. int security_quotactl(int cmds, int type, int id, struct super_block *sb)
  627. {
  628. return call_int_hook(quotactl, 0, cmds, type, id, sb);
  629. }
  630. int security_quota_on(struct dentry *dentry)
  631. {
  632. return call_int_hook(quota_on, 0, dentry);
  633. }
  634. int security_syslog(int type)
  635. {
  636. return call_int_hook(syslog, 0, type);
  637. }
  638. int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
  639. {
  640. return call_int_hook(settime, 0, ts, tz);
  641. }
  642. int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
  643. {
  644. struct security_hook_list *hp;
  645. int cap_sys_admin = 1;
  646. int rc;
  647. /*
  648. * The module will respond with a positive value if
  649. * it thinks the __vm_enough_memory() call should be
  650. * made with the cap_sys_admin set. If all of the modules
  651. * agree that it should be set it will. If any module
  652. * thinks it should not be set it won't.
  653. */
  654. hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
  655. rc = hp->hook.vm_enough_memory(mm, pages);
  656. if (rc <= 0) {
  657. cap_sys_admin = 0;
  658. break;
  659. }
  660. }
  661. return __vm_enough_memory(mm, pages, cap_sys_admin);
  662. }
  663. int security_bprm_set_creds(struct linux_binprm *bprm)
  664. {
  665. return call_int_hook(bprm_set_creds, 0, bprm);
  666. }
  667. int security_bprm_check(struct linux_binprm *bprm)
  668. {
  669. int ret;
  670. ret = call_int_hook(bprm_check_security, 0, bprm);
  671. if (ret)
  672. return ret;
  673. return ima_bprm_check(bprm);
  674. }
  675. void security_bprm_committing_creds(struct linux_binprm *bprm)
  676. {
  677. call_void_hook(bprm_committing_creds, bprm);
  678. }
  679. void security_bprm_committed_creds(struct linux_binprm *bprm)
  680. {
  681. call_void_hook(bprm_committed_creds, bprm);
  682. }
  683. int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
  684. {
  685. return call_int_hook(fs_context_dup, 0, fc, src_fc);
  686. }
  687. int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param)
  688. {
  689. return call_int_hook(fs_context_parse_param, -ENOPARAM, fc, param);
  690. }
  691. int security_sb_alloc(struct super_block *sb)
  692. {
  693. return call_int_hook(sb_alloc_security, 0, sb);
  694. }
  695. void security_sb_free(struct super_block *sb)
  696. {
  697. call_void_hook(sb_free_security, sb);
  698. }
  699. void security_free_mnt_opts(void **mnt_opts)
  700. {
  701. if (!*mnt_opts)
  702. return;
  703. call_void_hook(sb_free_mnt_opts, *mnt_opts);
  704. *mnt_opts = NULL;
  705. }
  706. EXPORT_SYMBOL(security_free_mnt_opts);
  707. int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
  708. {
  709. return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
  710. }
  711. EXPORT_SYMBOL(security_sb_eat_lsm_opts);
  712. int security_sb_remount(struct super_block *sb,
  713. void *mnt_opts)
  714. {
  715. return call_int_hook(sb_remount, 0, sb, mnt_opts);
  716. }
  717. EXPORT_SYMBOL(security_sb_remount);
  718. int security_sb_kern_mount(struct super_block *sb)
  719. {
  720. return call_int_hook(sb_kern_mount, 0, sb);
  721. }
  722. int security_sb_show_options(struct seq_file *m, struct super_block *sb)
  723. {
  724. return call_int_hook(sb_show_options, 0, m, sb);
  725. }
  726. int security_sb_statfs(struct dentry *dentry)
  727. {
  728. return call_int_hook(sb_statfs, 0, dentry);
  729. }
  730. int security_sb_mount(const char *dev_name, const struct path *path,
  731. const char *type, unsigned long flags, void *data)
  732. {
  733. return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
  734. }
  735. int security_sb_umount(struct vfsmount *mnt, int flags)
  736. {
  737. return call_int_hook(sb_umount, 0, mnt, flags);
  738. }
  739. int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
  740. {
  741. return call_int_hook(sb_pivotroot, 0, old_path, new_path);
  742. }
  743. int security_sb_set_mnt_opts(struct super_block *sb,
  744. void *mnt_opts,
  745. unsigned long kern_flags,
  746. unsigned long *set_kern_flags)
  747. {
  748. return call_int_hook(sb_set_mnt_opts,
  749. mnt_opts ? -EOPNOTSUPP : 0, sb,
  750. mnt_opts, kern_flags, set_kern_flags);
  751. }
  752. EXPORT_SYMBOL(security_sb_set_mnt_opts);
  753. int security_sb_clone_mnt_opts(const struct super_block *oldsb,
  754. struct super_block *newsb,
  755. unsigned long kern_flags,
  756. unsigned long *set_kern_flags)
  757. {
  758. return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
  759. kern_flags, set_kern_flags);
  760. }
  761. EXPORT_SYMBOL(security_sb_clone_mnt_opts);
  762. int security_add_mnt_opt(const char *option, const char *val, int len,
  763. void **mnt_opts)
  764. {
  765. return call_int_hook(sb_add_mnt_opt, -EINVAL,
  766. option, val, len, mnt_opts);
  767. }
  768. EXPORT_SYMBOL(security_add_mnt_opt);
  769. int security_move_mount(const struct path *from_path, const struct path *to_path)
  770. {
  771. return call_int_hook(move_mount, 0, from_path, to_path);
  772. }
  773. int security_path_notify(const struct path *path, u64 mask,
  774. unsigned int obj_type)
  775. {
  776. return call_int_hook(path_notify, 0, path, mask, obj_type);
  777. }
  778. int security_inode_alloc(struct inode *inode)
  779. {
  780. int rc = lsm_inode_alloc(inode);
  781. if (unlikely(rc))
  782. return rc;
  783. rc = call_int_hook(inode_alloc_security, 0, inode);
  784. if (unlikely(rc))
  785. security_inode_free(inode);
  786. return rc;
  787. }
  788. static void inode_free_by_rcu(struct rcu_head *head)
  789. {
  790. /*
  791. * The rcu head is at the start of the inode blob
  792. */
  793. kmem_cache_free(lsm_inode_cache, head);
  794. }
  795. void security_inode_free(struct inode *inode)
  796. {
  797. integrity_inode_free(inode);
  798. call_void_hook(inode_free_security, inode);
  799. /*
  800. * The inode may still be referenced in a path walk and
  801. * a call to security_inode_permission() can be made
  802. * after inode_free_security() is called. Ideally, the VFS
  803. * wouldn't do this, but fixing that is a much harder
  804. * job. For now, simply free the i_security via RCU, and
  805. * leave the current inode->i_security pointer intact.
  806. * The inode will be freed after the RCU grace period too.
  807. */
  808. if (inode->i_security)
  809. call_rcu((struct rcu_head *)inode->i_security,
  810. inode_free_by_rcu);
  811. }
  812. int security_dentry_init_security(struct dentry *dentry, int mode,
  813. const struct qstr *name, void **ctx,
  814. u32 *ctxlen)
  815. {
  816. return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode,
  817. name, ctx, ctxlen);
  818. }
  819. EXPORT_SYMBOL(security_dentry_init_security);
  820. int security_dentry_create_files_as(struct dentry *dentry, int mode,
  821. struct qstr *name,
  822. const struct cred *old, struct cred *new)
  823. {
  824. return call_int_hook(dentry_create_files_as, 0, dentry, mode,
  825. name, old, new);
  826. }
  827. EXPORT_SYMBOL(security_dentry_create_files_as);
  828. int security_inode_init_security(struct inode *inode, struct inode *dir,
  829. const struct qstr *qstr,
  830. const initxattrs initxattrs, void *fs_data)
  831. {
  832. struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
  833. struct xattr *lsm_xattr, *evm_xattr, *xattr;
  834. int ret;
  835. if (unlikely(IS_PRIVATE(inode)))
  836. return 0;
  837. if (!initxattrs)
  838. return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
  839. dir, qstr, NULL, NULL, NULL);
  840. memset(new_xattrs, 0, sizeof(new_xattrs));
  841. lsm_xattr = new_xattrs;
  842. ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
  843. &lsm_xattr->name,
  844. &lsm_xattr->value,
  845. &lsm_xattr->value_len);
  846. if (ret)
  847. goto out;
  848. evm_xattr = lsm_xattr + 1;
  849. ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
  850. if (ret)
  851. goto out;
  852. ret = initxattrs(inode, new_xattrs, fs_data);
  853. out:
  854. for (xattr = new_xattrs; xattr->value != NULL; xattr++)
  855. kfree(xattr->value);
  856. return (ret == -EOPNOTSUPP) ? 0 : ret;
  857. }
  858. EXPORT_SYMBOL(security_inode_init_security);
  859. int security_old_inode_init_security(struct inode *inode, struct inode *dir,
  860. const struct qstr *qstr, const char **name,
  861. void **value, size_t *len)
  862. {
  863. if (unlikely(IS_PRIVATE(inode)))
  864. return -EOPNOTSUPP;
  865. return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
  866. qstr, name, value, len);
  867. }
  868. EXPORT_SYMBOL(security_old_inode_init_security);
  869. #ifdef CONFIG_SECURITY_PATH
  870. int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
  871. unsigned int dev)
  872. {
  873. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  874. return 0;
  875. return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
  876. }
  877. EXPORT_SYMBOL(security_path_mknod);
  878. int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
  879. {
  880. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  881. return 0;
  882. return call_int_hook(path_mkdir, 0, dir, dentry, mode);
  883. }
  884. EXPORT_SYMBOL(security_path_mkdir);
  885. int security_path_rmdir(const struct path *dir, struct dentry *dentry)
  886. {
  887. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  888. return 0;
  889. return call_int_hook(path_rmdir, 0, dir, dentry);
  890. }
  891. int security_path_unlink(const struct path *dir, struct dentry *dentry)
  892. {
  893. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  894. return 0;
  895. return call_int_hook(path_unlink, 0, dir, dentry);
  896. }
  897. EXPORT_SYMBOL(security_path_unlink);
  898. int security_path_symlink(const struct path *dir, struct dentry *dentry,
  899. const char *old_name)
  900. {
  901. if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
  902. return 0;
  903. return call_int_hook(path_symlink, 0, dir, dentry, old_name);
  904. }
  905. int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
  906. struct dentry *new_dentry)
  907. {
  908. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
  909. return 0;
  910. return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
  911. }
  912. int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
  913. const struct path *new_dir, struct dentry *new_dentry,
  914. unsigned int flags)
  915. {
  916. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
  917. (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
  918. return 0;
  919. if (flags & RENAME_EXCHANGE) {
  920. int err = call_int_hook(path_rename, 0, new_dir, new_dentry,
  921. old_dir, old_dentry);
  922. if (err)
  923. return err;
  924. }
  925. return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
  926. new_dentry);
  927. }
  928. EXPORT_SYMBOL(security_path_rename);
  929. int security_path_truncate(const struct path *path)
  930. {
  931. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  932. return 0;
  933. return call_int_hook(path_truncate, 0, path);
  934. }
  935. int security_path_chmod(const struct path *path, umode_t mode)
  936. {
  937. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  938. return 0;
  939. return call_int_hook(path_chmod, 0, path, mode);
  940. }
  941. int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
  942. {
  943. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  944. return 0;
  945. return call_int_hook(path_chown, 0, path, uid, gid);
  946. }
  947. int security_path_chroot(const struct path *path)
  948. {
  949. return call_int_hook(path_chroot, 0, path);
  950. }
  951. #endif
  952. int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
  953. {
  954. if (unlikely(IS_PRIVATE(dir)))
  955. return 0;
  956. return call_int_hook(inode_create, 0, dir, dentry, mode);
  957. }
  958. EXPORT_SYMBOL_GPL(security_inode_create);
  959. int security_inode_link(struct dentry *old_dentry, struct inode *dir,
  960. struct dentry *new_dentry)
  961. {
  962. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
  963. return 0;
  964. return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
  965. }
  966. int security_inode_unlink(struct inode *dir, struct dentry *dentry)
  967. {
  968. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  969. return 0;
  970. return call_int_hook(inode_unlink, 0, dir, dentry);
  971. }
  972. int security_inode_symlink(struct inode *dir, struct dentry *dentry,
  973. const char *old_name)
  974. {
  975. if (unlikely(IS_PRIVATE(dir)))
  976. return 0;
  977. return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
  978. }
  979. int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  980. {
  981. if (unlikely(IS_PRIVATE(dir)))
  982. return 0;
  983. return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
  984. }
  985. EXPORT_SYMBOL_GPL(security_inode_mkdir);
  986. int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
  987. {
  988. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  989. return 0;
  990. return call_int_hook(inode_rmdir, 0, dir, dentry);
  991. }
  992. int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  993. {
  994. if (unlikely(IS_PRIVATE(dir)))
  995. return 0;
  996. return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
  997. }
  998. int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
  999. struct inode *new_dir, struct dentry *new_dentry,
  1000. unsigned int flags)
  1001. {
  1002. if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
  1003. (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
  1004. return 0;
  1005. if (flags & RENAME_EXCHANGE) {
  1006. int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
  1007. old_dir, old_dentry);
  1008. if (err)
  1009. return err;
  1010. }
  1011. return call_int_hook(inode_rename, 0, old_dir, old_dentry,
  1012. new_dir, new_dentry);
  1013. }
  1014. int security_inode_readlink(struct dentry *dentry)
  1015. {
  1016. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1017. return 0;
  1018. return call_int_hook(inode_readlink, 0, dentry);
  1019. }
  1020. int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
  1021. bool rcu)
  1022. {
  1023. if (unlikely(IS_PRIVATE(inode)))
  1024. return 0;
  1025. return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
  1026. }
  1027. int security_inode_permission(struct inode *inode, int mask)
  1028. {
  1029. if (unlikely(IS_PRIVATE(inode)))
  1030. return 0;
  1031. return call_int_hook(inode_permission, 0, inode, mask);
  1032. }
  1033. int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
  1034. {
  1035. int ret;
  1036. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1037. return 0;
  1038. ret = call_int_hook(inode_setattr, 0, dentry, attr);
  1039. if (ret)
  1040. return ret;
  1041. return evm_inode_setattr(dentry, attr);
  1042. }
  1043. EXPORT_SYMBOL_GPL(security_inode_setattr);
  1044. int security_inode_getattr(const struct path *path)
  1045. {
  1046. if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
  1047. return 0;
  1048. return call_int_hook(inode_getattr, 0, path);
  1049. }
  1050. int security_inode_setxattr(struct dentry *dentry, const char *name,
  1051. const void *value, size_t size, int flags)
  1052. {
  1053. int ret;
  1054. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1055. return 0;
  1056. /*
  1057. * SELinux and Smack integrate the cap call,
  1058. * so assume that all LSMs supplying this call do so.
  1059. */
  1060. ret = call_int_hook(inode_setxattr, 1, dentry, name, value, size,
  1061. flags);
  1062. if (ret == 1)
  1063. ret = cap_inode_setxattr(dentry, name, value, size, flags);
  1064. if (ret)
  1065. return ret;
  1066. ret = ima_inode_setxattr(dentry, name, value, size);
  1067. if (ret)
  1068. return ret;
  1069. return evm_inode_setxattr(dentry, name, value, size);
  1070. }
  1071. void security_inode_post_setxattr(struct dentry *dentry, const char *name,
  1072. const void *value, size_t size, int flags)
  1073. {
  1074. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1075. return;
  1076. call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
  1077. evm_inode_post_setxattr(dentry, name, value, size);
  1078. }
  1079. int security_inode_getxattr(struct dentry *dentry, const char *name)
  1080. {
  1081. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1082. return 0;
  1083. return call_int_hook(inode_getxattr, 0, dentry, name);
  1084. }
  1085. int security_inode_listxattr(struct dentry *dentry)
  1086. {
  1087. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1088. return 0;
  1089. return call_int_hook(inode_listxattr, 0, dentry);
  1090. }
  1091. int security_inode_removexattr(struct dentry *dentry, const char *name)
  1092. {
  1093. int ret;
  1094. if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
  1095. return 0;
  1096. /*
  1097. * SELinux and Smack integrate the cap call,
  1098. * so assume that all LSMs supplying this call do so.
  1099. */
  1100. ret = call_int_hook(inode_removexattr, 1, dentry, name);
  1101. if (ret == 1)
  1102. ret = cap_inode_removexattr(dentry, name);
  1103. if (ret)
  1104. return ret;
  1105. ret = ima_inode_removexattr(dentry, name);
  1106. if (ret)
  1107. return ret;
  1108. return evm_inode_removexattr(dentry, name);
  1109. }
  1110. int security_inode_need_killpriv(struct dentry *dentry)
  1111. {
  1112. return call_int_hook(inode_need_killpriv, 0, dentry);
  1113. }
  1114. int security_inode_killpriv(struct dentry *dentry)
  1115. {
  1116. return call_int_hook(inode_killpriv, 0, dentry);
  1117. }
  1118. int security_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
  1119. {
  1120. struct security_hook_list *hp;
  1121. int rc;
  1122. if (unlikely(IS_PRIVATE(inode)))
  1123. return -EOPNOTSUPP;
  1124. /*
  1125. * Only one module will provide an attribute with a given name.
  1126. */
  1127. hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
  1128. rc = hp->hook.inode_getsecurity(inode, name, buffer, alloc);
  1129. if (rc != -EOPNOTSUPP)
  1130. return rc;
  1131. }
  1132. return -EOPNOTSUPP;
  1133. }
  1134. int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
  1135. {
  1136. struct security_hook_list *hp;
  1137. int rc;
  1138. if (unlikely(IS_PRIVATE(inode)))
  1139. return -EOPNOTSUPP;
  1140. /*
  1141. * Only one module will provide an attribute with a given name.
  1142. */
  1143. hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
  1144. rc = hp->hook.inode_setsecurity(inode, name, value, size,
  1145. flags);
  1146. if (rc != -EOPNOTSUPP)
  1147. return rc;
  1148. }
  1149. return -EOPNOTSUPP;
  1150. }
  1151. int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
  1152. {
  1153. if (unlikely(IS_PRIVATE(inode)))
  1154. return 0;
  1155. return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
  1156. }
  1157. EXPORT_SYMBOL(security_inode_listsecurity);
  1158. void security_inode_getsecid(struct inode *inode, u32 *secid)
  1159. {
  1160. call_void_hook(inode_getsecid, inode, secid);
  1161. }
  1162. int security_inode_copy_up(struct dentry *src, struct cred **new)
  1163. {
  1164. return call_int_hook(inode_copy_up, 0, src, new);
  1165. }
  1166. EXPORT_SYMBOL(security_inode_copy_up);
  1167. int security_inode_copy_up_xattr(const char *name)
  1168. {
  1169. return call_int_hook(inode_copy_up_xattr, -EOPNOTSUPP, name);
  1170. }
  1171. EXPORT_SYMBOL(security_inode_copy_up_xattr);
  1172. int security_kernfs_init_security(struct kernfs_node *kn_dir,
  1173. struct kernfs_node *kn)
  1174. {
  1175. return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
  1176. }
  1177. int security_file_permission(struct file *file, int mask)
  1178. {
  1179. int ret;
  1180. ret = call_int_hook(file_permission, 0, file, mask);
  1181. if (ret)
  1182. return ret;
  1183. return fsnotify_perm(file, mask);
  1184. }
  1185. int security_file_alloc(struct file *file)
  1186. {
  1187. int rc = lsm_file_alloc(file);
  1188. if (rc)
  1189. return rc;
  1190. rc = call_int_hook(file_alloc_security, 0, file);
  1191. if (unlikely(rc))
  1192. security_file_free(file);
  1193. return rc;
  1194. }
  1195. void security_file_free(struct file *file)
  1196. {
  1197. void *blob;
  1198. call_void_hook(file_free_security, file);
  1199. blob = file->f_security;
  1200. if (blob) {
  1201. file->f_security = NULL;
  1202. kmem_cache_free(lsm_file_cache, blob);
  1203. }
  1204. }
  1205. int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1206. {
  1207. return call_int_hook(file_ioctl, 0, file, cmd, arg);
  1208. }
  1209. static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
  1210. {
  1211. /*
  1212. * Does we have PROT_READ and does the application expect
  1213. * it to imply PROT_EXEC? If not, nothing to talk about...
  1214. */
  1215. if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
  1216. return prot;
  1217. if (!(current->personality & READ_IMPLIES_EXEC))
  1218. return prot;
  1219. /*
  1220. * if that's an anonymous mapping, let it.
  1221. */
  1222. if (!file)
  1223. return prot | PROT_EXEC;
  1224. /*
  1225. * ditto if it's not on noexec mount, except that on !MMU we need
  1226. * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
  1227. */
  1228. if (!path_noexec(&file->f_path)) {
  1229. #ifndef CONFIG_MMU
  1230. if (file->f_op->mmap_capabilities) {
  1231. unsigned caps = file->f_op->mmap_capabilities(file);
  1232. if (!(caps & NOMMU_MAP_EXEC))
  1233. return prot;
  1234. }
  1235. #endif
  1236. return prot | PROT_EXEC;
  1237. }
  1238. /* anything on noexec mount won't get PROT_EXEC */
  1239. return prot;
  1240. }
  1241. int security_mmap_file(struct file *file, unsigned long prot,
  1242. unsigned long flags)
  1243. {
  1244. int ret;
  1245. ret = call_int_hook(mmap_file, 0, file, prot,
  1246. mmap_prot(file, prot), flags);
  1247. if (ret)
  1248. return ret;
  1249. return ima_file_mmap(file, prot);
  1250. }
  1251. int security_mmap_addr(unsigned long addr)
  1252. {
  1253. return call_int_hook(mmap_addr, 0, addr);
  1254. }
  1255. int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
  1256. unsigned long prot)
  1257. {
  1258. return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
  1259. }
  1260. int security_file_lock(struct file *file, unsigned int cmd)
  1261. {
  1262. return call_int_hook(file_lock, 0, file, cmd);
  1263. }
  1264. int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  1265. {
  1266. return call_int_hook(file_fcntl, 0, file, cmd, arg);
  1267. }
  1268. void security_file_set_fowner(struct file *file)
  1269. {
  1270. call_void_hook(file_set_fowner, file);
  1271. }
  1272. int security_file_send_sigiotask(struct task_struct *tsk,
  1273. struct fown_struct *fown, int sig)
  1274. {
  1275. return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
  1276. }
  1277. int security_file_receive(struct file *file)
  1278. {
  1279. return call_int_hook(file_receive, 0, file);
  1280. }
  1281. int security_file_open(struct file *file)
  1282. {
  1283. int ret;
  1284. ret = call_int_hook(file_open, 0, file);
  1285. if (ret)
  1286. return ret;
  1287. return fsnotify_perm(file, MAY_OPEN);
  1288. }
  1289. int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
  1290. {
  1291. int rc = lsm_task_alloc(task);
  1292. if (rc)
  1293. return rc;
  1294. rc = call_int_hook(task_alloc, 0, task, clone_flags);
  1295. if (unlikely(rc))
  1296. security_task_free(task);
  1297. return rc;
  1298. }
  1299. void security_task_free(struct task_struct *task)
  1300. {
  1301. call_void_hook(task_free, task);
  1302. kfree(task->security);
  1303. task->security = NULL;
  1304. }
  1305. int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  1306. {
  1307. int rc = lsm_cred_alloc(cred, gfp);
  1308. if (rc)
  1309. return rc;
  1310. rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
  1311. if (unlikely(rc))
  1312. security_cred_free(cred);
  1313. return rc;
  1314. }
  1315. void security_cred_free(struct cred *cred)
  1316. {
  1317. /*
  1318. * There is a failure case in prepare_creds() that
  1319. * may result in a call here with ->security being NULL.
  1320. */
  1321. if (unlikely(cred->security == NULL))
  1322. return;
  1323. call_void_hook(cred_free, cred);
  1324. kfree(cred->security);
  1325. cred->security = NULL;
  1326. }
  1327. int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
  1328. {
  1329. int rc = lsm_cred_alloc(new, gfp);
  1330. if (rc)
  1331. return rc;
  1332. rc = call_int_hook(cred_prepare, 0, new, old, gfp);
  1333. if (unlikely(rc))
  1334. security_cred_free(new);
  1335. return rc;
  1336. }
  1337. void security_transfer_creds(struct cred *new, const struct cred *old)
  1338. {
  1339. call_void_hook(cred_transfer, new, old);
  1340. }
  1341. void security_cred_getsecid(const struct cred *c, u32 *secid)
  1342. {
  1343. *secid = 0;
  1344. call_void_hook(cred_getsecid, c, secid);
  1345. }
  1346. EXPORT_SYMBOL(security_cred_getsecid);
  1347. int security_kernel_act_as(struct cred *new, u32 secid)
  1348. {
  1349. return call_int_hook(kernel_act_as, 0, new, secid);
  1350. }
  1351. int security_kernel_create_files_as(struct cred *new, struct inode *inode)
  1352. {
  1353. return call_int_hook(kernel_create_files_as, 0, new, inode);
  1354. }
  1355. int security_kernel_module_request(char *kmod_name)
  1356. {
  1357. int ret;
  1358. ret = call_int_hook(kernel_module_request, 0, kmod_name);
  1359. if (ret)
  1360. return ret;
  1361. return integrity_kernel_module_request(kmod_name);
  1362. }
  1363. int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
  1364. {
  1365. int ret;
  1366. ret = call_int_hook(kernel_read_file, 0, file, id);
  1367. if (ret)
  1368. return ret;
  1369. return ima_read_file(file, id);
  1370. }
  1371. EXPORT_SYMBOL_GPL(security_kernel_read_file);
  1372. int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
  1373. enum kernel_read_file_id id)
  1374. {
  1375. int ret;
  1376. ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
  1377. if (ret)
  1378. return ret;
  1379. return ima_post_read_file(file, buf, size, id);
  1380. }
  1381. EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
  1382. int security_kernel_load_data(enum kernel_load_data_id id)
  1383. {
  1384. int ret;
  1385. ret = call_int_hook(kernel_load_data, 0, id);
  1386. if (ret)
  1387. return ret;
  1388. return ima_load_data(id);
  1389. }
  1390. EXPORT_SYMBOL_GPL(security_kernel_load_data);
  1391. int security_task_fix_setuid(struct cred *new, const struct cred *old,
  1392. int flags)
  1393. {
  1394. return call_int_hook(task_fix_setuid, 0, new, old, flags);
  1395. }
  1396. int security_task_setpgid(struct task_struct *p, pid_t pgid)
  1397. {
  1398. return call_int_hook(task_setpgid, 0, p, pgid);
  1399. }
  1400. int security_task_getpgid(struct task_struct *p)
  1401. {
  1402. return call_int_hook(task_getpgid, 0, p);
  1403. }
  1404. int security_task_getsid(struct task_struct *p)
  1405. {
  1406. return call_int_hook(task_getsid, 0, p);
  1407. }
  1408. void security_task_getsecid(struct task_struct *p, u32 *secid)
  1409. {
  1410. *secid = 0;
  1411. call_void_hook(task_getsecid, p, secid);
  1412. }
  1413. EXPORT_SYMBOL(security_task_getsecid);
  1414. int security_task_setnice(struct task_struct *p, int nice)
  1415. {
  1416. return call_int_hook(task_setnice, 0, p, nice);
  1417. }
  1418. int security_task_setioprio(struct task_struct *p, int ioprio)
  1419. {
  1420. return call_int_hook(task_setioprio, 0, p, ioprio);
  1421. }
  1422. int security_task_getioprio(struct task_struct *p)
  1423. {
  1424. return call_int_hook(task_getioprio, 0, p);
  1425. }
  1426. int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
  1427. unsigned int flags)
  1428. {
  1429. return call_int_hook(task_prlimit, 0, cred, tcred, flags);
  1430. }
  1431. int security_task_setrlimit(struct task_struct *p, unsigned int resource,
  1432. struct rlimit *new_rlim)
  1433. {
  1434. return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
  1435. }
  1436. int security_task_setscheduler(struct task_struct *p)
  1437. {
  1438. return call_int_hook(task_setscheduler, 0, p);
  1439. }
  1440. int security_task_getscheduler(struct task_struct *p)
  1441. {
  1442. return call_int_hook(task_getscheduler, 0, p);
  1443. }
  1444. int security_task_movememory(struct task_struct *p)
  1445. {
  1446. return call_int_hook(task_movememory, 0, p);
  1447. }
  1448. int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
  1449. int sig, const struct cred *cred)
  1450. {
  1451. return call_int_hook(task_kill, 0, p, info, sig, cred);
  1452. }
  1453. int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  1454. unsigned long arg4, unsigned long arg5)
  1455. {
  1456. int thisrc;
  1457. int rc = -ENOSYS;
  1458. struct security_hook_list *hp;
  1459. hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
  1460. thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
  1461. if (thisrc != -ENOSYS) {
  1462. rc = thisrc;
  1463. if (thisrc != 0)
  1464. break;
  1465. }
  1466. }
  1467. return rc;
  1468. }
  1469. void security_task_to_inode(struct task_struct *p, struct inode *inode)
  1470. {
  1471. call_void_hook(task_to_inode, p, inode);
  1472. }
  1473. int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
  1474. {
  1475. return call_int_hook(ipc_permission, 0, ipcp, flag);
  1476. }
  1477. void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
  1478. {
  1479. *secid = 0;
  1480. call_void_hook(ipc_getsecid, ipcp, secid);
  1481. }
  1482. int security_msg_msg_alloc(struct msg_msg *msg)
  1483. {
  1484. int rc = lsm_msg_msg_alloc(msg);
  1485. if (unlikely(rc))
  1486. return rc;
  1487. rc = call_int_hook(msg_msg_alloc_security, 0, msg);
  1488. if (unlikely(rc))
  1489. security_msg_msg_free(msg);
  1490. return rc;
  1491. }
  1492. void security_msg_msg_free(struct msg_msg *msg)
  1493. {
  1494. call_void_hook(msg_msg_free_security, msg);
  1495. kfree(msg->security);
  1496. msg->security = NULL;
  1497. }
  1498. int security_msg_queue_alloc(struct kern_ipc_perm *msq)
  1499. {
  1500. int rc = lsm_ipc_alloc(msq);
  1501. if (unlikely(rc))
  1502. return rc;
  1503. rc = call_int_hook(msg_queue_alloc_security, 0, msq);
  1504. if (unlikely(rc))
  1505. security_msg_queue_free(msq);
  1506. return rc;
  1507. }
  1508. void security_msg_queue_free(struct kern_ipc_perm *msq)
  1509. {
  1510. call_void_hook(msg_queue_free_security, msq);
  1511. kfree(msq->security);
  1512. msq->security = NULL;
  1513. }
  1514. int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
  1515. {
  1516. return call_int_hook(msg_queue_associate, 0, msq, msqflg);
  1517. }
  1518. int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
  1519. {
  1520. return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
  1521. }
  1522. int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
  1523. struct msg_msg *msg, int msqflg)
  1524. {
  1525. return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
  1526. }
  1527. int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
  1528. struct task_struct *target, long type, int mode)
  1529. {
  1530. return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
  1531. }
  1532. int security_shm_alloc(struct kern_ipc_perm *shp)
  1533. {
  1534. int rc = lsm_ipc_alloc(shp);
  1535. if (unlikely(rc))
  1536. return rc;
  1537. rc = call_int_hook(shm_alloc_security, 0, shp);
  1538. if (unlikely(rc))
  1539. security_shm_free(shp);
  1540. return rc;
  1541. }
  1542. void security_shm_free(struct kern_ipc_perm *shp)
  1543. {
  1544. call_void_hook(shm_free_security, shp);
  1545. kfree(shp->security);
  1546. shp->security = NULL;
  1547. }
  1548. int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
  1549. {
  1550. return call_int_hook(shm_associate, 0, shp, shmflg);
  1551. }
  1552. int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
  1553. {
  1554. return call_int_hook(shm_shmctl, 0, shp, cmd);
  1555. }
  1556. int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmflg)
  1557. {
  1558. return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
  1559. }
  1560. int security_sem_alloc(struct kern_ipc_perm *sma)
  1561. {
  1562. int rc = lsm_ipc_alloc(sma);
  1563. if (unlikely(rc))
  1564. return rc;
  1565. rc = call_int_hook(sem_alloc_security, 0, sma);
  1566. if (unlikely(rc))
  1567. security_sem_free(sma);
  1568. return rc;
  1569. }
  1570. void security_sem_free(struct kern_ipc_perm *sma)
  1571. {
  1572. call_void_hook(sem_free_security, sma);
  1573. kfree(sma->security);
  1574. sma->security = NULL;
  1575. }
  1576. int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
  1577. {
  1578. return call_int_hook(sem_associate, 0, sma, semflg);
  1579. }
  1580. int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
  1581. {
  1582. return call_int_hook(sem_semctl, 0, sma, cmd);
  1583. }
  1584. int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
  1585. unsigned nsops, int alter)
  1586. {
  1587. return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
  1588. }
  1589. void security_d_instantiate(struct dentry *dentry, struct inode *inode)
  1590. {
  1591. if (unlikely(inode && IS_PRIVATE(inode)))
  1592. return;
  1593. call_void_hook(d_instantiate, dentry, inode);
  1594. }
  1595. EXPORT_SYMBOL(security_d_instantiate);
  1596. int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
  1597. char **value)
  1598. {
  1599. struct security_hook_list *hp;
  1600. hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
  1601. if (lsm != NULL && strcmp(lsm, hp->lsm))
  1602. continue;
  1603. return hp->hook.getprocattr(p, name, value);
  1604. }
  1605. return -EINVAL;
  1606. }
  1607. int security_setprocattr(const char *lsm, const char *name, void *value,
  1608. size_t size)
  1609. {
  1610. struct security_hook_list *hp;
  1611. hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
  1612. if (lsm != NULL && strcmp(lsm, hp->lsm))
  1613. continue;
  1614. return hp->hook.setprocattr(name, value, size);
  1615. }
  1616. return -EINVAL;
  1617. }
  1618. int security_netlink_send(struct sock *sk, struct sk_buff *skb)
  1619. {
  1620. return call_int_hook(netlink_send, 0, sk, skb);
  1621. }
  1622. int security_ismaclabel(const char *name)
  1623. {
  1624. return call_int_hook(ismaclabel, 0, name);
  1625. }
  1626. EXPORT_SYMBOL(security_ismaclabel);
  1627. int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  1628. {
  1629. return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid, secdata,
  1630. seclen);
  1631. }
  1632. EXPORT_SYMBOL(security_secid_to_secctx);
  1633. int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
  1634. {
  1635. *secid = 0;
  1636. return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
  1637. }
  1638. EXPORT_SYMBOL(security_secctx_to_secid);
  1639. void security_release_secctx(char *secdata, u32 seclen)
  1640. {
  1641. call_void_hook(release_secctx, secdata, seclen);
  1642. }
  1643. EXPORT_SYMBOL(security_release_secctx);
  1644. void security_inode_invalidate_secctx(struct inode *inode)
  1645. {
  1646. call_void_hook(inode_invalidate_secctx, inode);
  1647. }
  1648. EXPORT_SYMBOL(security_inode_invalidate_secctx);
  1649. int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
  1650. {
  1651. return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
  1652. }
  1653. EXPORT_SYMBOL(security_inode_notifysecctx);
  1654. int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
  1655. {
  1656. return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
  1657. }
  1658. EXPORT_SYMBOL(security_inode_setsecctx);
  1659. int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
  1660. {
  1661. return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
  1662. }
  1663. EXPORT_SYMBOL(security_inode_getsecctx);
  1664. #ifdef CONFIG_SECURITY_NETWORK
  1665. int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
  1666. {
  1667. return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
  1668. }
  1669. EXPORT_SYMBOL(security_unix_stream_connect);
  1670. int security_unix_may_send(struct socket *sock, struct socket *other)
  1671. {
  1672. return call_int_hook(unix_may_send, 0, sock, other);
  1673. }
  1674. EXPORT_SYMBOL(security_unix_may_send);
  1675. int security_socket_create(int family, int type, int protocol, int kern)
  1676. {
  1677. return call_int_hook(socket_create, 0, family, type, protocol, kern);
  1678. }
  1679. int security_socket_post_create(struct socket *sock, int family,
  1680. int type, int protocol, int kern)
  1681. {
  1682. return call_int_hook(socket_post_create, 0, sock, family, type,
  1683. protocol, kern);
  1684. }
  1685. int security_socket_socketpair(struct socket *socka, struct socket *sockb)
  1686. {
  1687. return call_int_hook(socket_socketpair, 0, socka, sockb);
  1688. }
  1689. EXPORT_SYMBOL(security_socket_socketpair);
  1690. int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
  1691. {
  1692. return call_int_hook(socket_bind, 0, sock, address, addrlen);
  1693. }
  1694. int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
  1695. {
  1696. return call_int_hook(socket_connect, 0, sock, address, addrlen);
  1697. }
  1698. int security_socket_listen(struct socket *sock, int backlog)
  1699. {
  1700. return call_int_hook(socket_listen, 0, sock, backlog);
  1701. }
  1702. int security_socket_accept(struct socket *sock, struct socket *newsock)
  1703. {
  1704. return call_int_hook(socket_accept, 0, sock, newsock);
  1705. }
  1706. int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
  1707. {
  1708. return call_int_hook(socket_sendmsg, 0, sock, msg, size);
  1709. }
  1710. int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
  1711. int size, int flags)
  1712. {
  1713. return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
  1714. }
  1715. int security_socket_getsockname(struct socket *sock)
  1716. {
  1717. return call_int_hook(socket_getsockname, 0, sock);
  1718. }
  1719. int security_socket_getpeername(struct socket *sock)
  1720. {
  1721. return call_int_hook(socket_getpeername, 0, sock);
  1722. }
  1723. int security_socket_getsockopt(struct socket *sock, int level, int optname)
  1724. {
  1725. return call_int_hook(socket_getsockopt, 0, sock, level, optname);
  1726. }
  1727. int security_socket_setsockopt(struct socket *sock, int level, int optname)
  1728. {
  1729. return call_int_hook(socket_setsockopt, 0, sock, level, optname);
  1730. }
  1731. int security_socket_shutdown(struct socket *sock, int how)
  1732. {
  1733. return call_int_hook(socket_shutdown, 0, sock, how);
  1734. }
  1735. int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1736. {
  1737. return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
  1738. }
  1739. EXPORT_SYMBOL(security_sock_rcv_skb);
  1740. int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
  1741. int __user *optlen, unsigned len)
  1742. {
  1743. return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
  1744. optval, optlen, len);
  1745. }
  1746. int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
  1747. {
  1748. return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
  1749. skb, secid);
  1750. }
  1751. EXPORT_SYMBOL(security_socket_getpeersec_dgram);
  1752. int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
  1753. {
  1754. return call_int_hook(sk_alloc_security, 0, sk, family, priority);
  1755. }
  1756. void security_sk_free(struct sock *sk)
  1757. {
  1758. call_void_hook(sk_free_security, sk);
  1759. }
  1760. void security_sk_clone(const struct sock *sk, struct sock *newsk)
  1761. {
  1762. call_void_hook(sk_clone_security, sk, newsk);
  1763. }
  1764. EXPORT_SYMBOL(security_sk_clone);
  1765. void security_sk_classify_flow(struct sock *sk, struct flowi *fl)
  1766. {
  1767. call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
  1768. }
  1769. EXPORT_SYMBOL(security_sk_classify_flow);
  1770. void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)
  1771. {
  1772. call_void_hook(req_classify_flow, req, fl);
  1773. }
  1774. EXPORT_SYMBOL(security_req_classify_flow);
  1775. void security_sock_graft(struct sock *sk, struct socket *parent)
  1776. {
  1777. call_void_hook(sock_graft, sk, parent);
  1778. }
  1779. EXPORT_SYMBOL(security_sock_graft);
  1780. int security_inet_conn_request(struct sock *sk,
  1781. struct sk_buff *skb, struct request_sock *req)
  1782. {
  1783. return call_int_hook(inet_conn_request, 0, sk, skb, req);
  1784. }
  1785. EXPORT_SYMBOL(security_inet_conn_request);
  1786. void security_inet_csk_clone(struct sock *newsk,
  1787. const struct request_sock *req)
  1788. {
  1789. call_void_hook(inet_csk_clone, newsk, req);
  1790. }
  1791. void security_inet_conn_established(struct sock *sk,
  1792. struct sk_buff *skb)
  1793. {
  1794. call_void_hook(inet_conn_established, sk, skb);
  1795. }
  1796. EXPORT_SYMBOL(security_inet_conn_established);
  1797. int security_secmark_relabel_packet(u32 secid)
  1798. {
  1799. return call_int_hook(secmark_relabel_packet, 0, secid);
  1800. }
  1801. EXPORT_SYMBOL(security_secmark_relabel_packet);
  1802. void security_secmark_refcount_inc(void)
  1803. {
  1804. call_void_hook(secmark_refcount_inc);
  1805. }
  1806. EXPORT_SYMBOL(security_secmark_refcount_inc);
  1807. void security_secmark_refcount_dec(void)
  1808. {
  1809. call_void_hook(secmark_refcount_dec);
  1810. }
  1811. EXPORT_SYMBOL(security_secmark_refcount_dec);
  1812. int security_tun_dev_alloc_security(void **security)
  1813. {
  1814. return call_int_hook(tun_dev_alloc_security, 0, security);
  1815. }
  1816. EXPORT_SYMBOL(security_tun_dev_alloc_security);
  1817. void security_tun_dev_free_security(void *security)
  1818. {
  1819. call_void_hook(tun_dev_free_security, security);
  1820. }
  1821. EXPORT_SYMBOL(security_tun_dev_free_security);
  1822. int security_tun_dev_create(void)
  1823. {
  1824. return call_int_hook(tun_dev_create, 0);
  1825. }
  1826. EXPORT_SYMBOL(security_tun_dev_create);
  1827. int security_tun_dev_attach_queue(void *security)
  1828. {
  1829. return call_int_hook(tun_dev_attach_queue, 0, security);
  1830. }
  1831. EXPORT_SYMBOL(security_tun_dev_attach_queue);
  1832. int security_tun_dev_attach(struct sock *sk, void *security)
  1833. {
  1834. return call_int_hook(tun_dev_attach, 0, sk, security);
  1835. }
  1836. EXPORT_SYMBOL(security_tun_dev_attach);
  1837. int security_tun_dev_open(void *security)
  1838. {
  1839. return call_int_hook(tun_dev_open, 0, security);
  1840. }
  1841. EXPORT_SYMBOL(security_tun_dev_open);
  1842. int security_sctp_assoc_request(struct sctp_endpoint *ep, struct sk_buff *skb)
  1843. {
  1844. return call_int_hook(sctp_assoc_request, 0, ep, skb);
  1845. }
  1846. EXPORT_SYMBOL(security_sctp_assoc_request);
  1847. int security_sctp_bind_connect(struct sock *sk, int optname,
  1848. struct sockaddr *address, int addrlen)
  1849. {
  1850. return call_int_hook(sctp_bind_connect, 0, sk, optname,
  1851. address, addrlen);
  1852. }
  1853. EXPORT_SYMBOL(security_sctp_bind_connect);
  1854. void security_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
  1855. struct sock *newsk)
  1856. {
  1857. call_void_hook(sctp_sk_clone, ep, sk, newsk);
  1858. }
  1859. EXPORT_SYMBOL(security_sctp_sk_clone);
  1860. #endif /* CONFIG_SECURITY_NETWORK */
  1861. #ifdef CONFIG_SECURITY_INFINIBAND
  1862. int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
  1863. {
  1864. return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
  1865. }
  1866. EXPORT_SYMBOL(security_ib_pkey_access);
  1867. int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
  1868. {
  1869. return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
  1870. }
  1871. EXPORT_SYMBOL(security_ib_endport_manage_subnet);
  1872. int security_ib_alloc_security(void **sec)
  1873. {
  1874. return call_int_hook(ib_alloc_security, 0, sec);
  1875. }
  1876. EXPORT_SYMBOL(security_ib_alloc_security);
  1877. void security_ib_free_security(void *sec)
  1878. {
  1879. call_void_hook(ib_free_security, sec);
  1880. }
  1881. EXPORT_SYMBOL(security_ib_free_security);
  1882. #endif /* CONFIG_SECURITY_INFINIBAND */
  1883. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  1884. int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
  1885. struct xfrm_user_sec_ctx *sec_ctx,
  1886. gfp_t gfp)
  1887. {
  1888. return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
  1889. }
  1890. EXPORT_SYMBOL(security_xfrm_policy_alloc);
  1891. int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
  1892. struct xfrm_sec_ctx **new_ctxp)
  1893. {
  1894. return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
  1895. }
  1896. void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
  1897. {
  1898. call_void_hook(xfrm_policy_free_security, ctx);
  1899. }
  1900. EXPORT_SYMBOL(security_xfrm_policy_free);
  1901. int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
  1902. {
  1903. return call_int_hook(xfrm_policy_delete_security, 0, ctx);
  1904. }
  1905. int security_xfrm_state_alloc(struct xfrm_state *x,
  1906. struct xfrm_user_sec_ctx *sec_ctx)
  1907. {
  1908. return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
  1909. }
  1910. EXPORT_SYMBOL(security_xfrm_state_alloc);
  1911. int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
  1912. struct xfrm_sec_ctx *polsec, u32 secid)
  1913. {
  1914. return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
  1915. }
  1916. int security_xfrm_state_delete(struct xfrm_state *x)
  1917. {
  1918. return call_int_hook(xfrm_state_delete_security, 0, x);
  1919. }
  1920. EXPORT_SYMBOL(security_xfrm_state_delete);
  1921. void security_xfrm_state_free(struct xfrm_state *x)
  1922. {
  1923. call_void_hook(xfrm_state_free_security, x);
  1924. }
  1925. int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
  1926. {
  1927. return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid, dir);
  1928. }
  1929. int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
  1930. struct xfrm_policy *xp,
  1931. const struct flowi *fl)
  1932. {
  1933. struct security_hook_list *hp;
  1934. int rc = 1;
  1935. /*
  1936. * Since this function is expected to return 0 or 1, the judgment
  1937. * becomes difficult if multiple LSMs supply this call. Fortunately,
  1938. * we can use the first LSM's judgment because currently only SELinux
  1939. * supplies this call.
  1940. *
  1941. * For speed optimization, we explicitly break the loop rather than
  1942. * using the macro
  1943. */
  1944. hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
  1945. list) {
  1946. rc = hp->hook.xfrm_state_pol_flow_match(x, xp, fl);
  1947. break;
  1948. }
  1949. return rc;
  1950. }
  1951. int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
  1952. {
  1953. return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
  1954. }
  1955. void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
  1956. {
  1957. int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl->flowi_secid,
  1958. 0);
  1959. BUG_ON(rc);
  1960. }
  1961. EXPORT_SYMBOL(security_skb_classify_flow);
  1962. #endif /* CONFIG_SECURITY_NETWORK_XFRM */
  1963. #ifdef CONFIG_KEYS
  1964. int security_key_alloc(struct key *key, const struct cred *cred,
  1965. unsigned long flags)
  1966. {
  1967. return call_int_hook(key_alloc, 0, key, cred, flags);
  1968. }
  1969. void security_key_free(struct key *key)
  1970. {
  1971. call_void_hook(key_free, key);
  1972. }
  1973. int security_key_permission(key_ref_t key_ref,
  1974. const struct cred *cred, unsigned perm)
  1975. {
  1976. return call_int_hook(key_permission, 0, key_ref, cred, perm);
  1977. }
  1978. int security_key_getsecurity(struct key *key, char **_buffer)
  1979. {
  1980. *_buffer = NULL;
  1981. return call_int_hook(key_getsecurity, 0, key, _buffer);
  1982. }
  1983. #endif /* CONFIG_KEYS */
  1984. #ifdef CONFIG_AUDIT
  1985. int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
  1986. {
  1987. return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
  1988. }
  1989. int security_audit_rule_known(struct audit_krule *krule)
  1990. {
  1991. return call_int_hook(audit_rule_known, 0, krule);
  1992. }
  1993. void security_audit_rule_free(void *lsmrule)
  1994. {
  1995. call_void_hook(audit_rule_free, lsmrule);
  1996. }
  1997. int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
  1998. {
  1999. return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
  2000. }
  2001. #endif /* CONFIG_AUDIT */
  2002. #ifdef CONFIG_BPF_SYSCALL
  2003. int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
  2004. {
  2005. return call_int_hook(bpf, 0, cmd, attr, size);
  2006. }
  2007. int security_bpf_map(struct bpf_map *map, fmode_t fmode)
  2008. {
  2009. return call_int_hook(bpf_map, 0, map, fmode);
  2010. }
  2011. int security_bpf_prog(struct bpf_prog *prog)
  2012. {
  2013. return call_int_hook(bpf_prog, 0, prog);
  2014. }
  2015. int security_bpf_map_alloc(struct bpf_map *map)
  2016. {
  2017. return call_int_hook(bpf_map_alloc_security, 0, map);
  2018. }
  2019. int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
  2020. {
  2021. return call_int_hook(bpf_prog_alloc_security, 0, aux);
  2022. }
  2023. void security_bpf_map_free(struct bpf_map *map)
  2024. {
  2025. call_void_hook(bpf_map_free_security, map);
  2026. }
  2027. void security_bpf_prog_free(struct bpf_prog_aux *aux)
  2028. {
  2029. call_void_hook(bpf_prog_free_security, aux);
  2030. }
  2031. #endif /* CONFIG_BPF_SYSCALL */
  2032. int security_locked_down(enum lockdown_reason what)
  2033. {
  2034. return call_int_hook(locked_down, 0, what);
  2035. }
  2036. EXPORT_SYMBOL(security_locked_down);