security.c 68 KB

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