auditsc.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* auditsc.c -- System-call auditing support
  3. * Handles all system-call specific auditing features.
  4. *
  5. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  6. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  7. * Copyright (C) 2005, 2006 IBM Corporation
  8. * All Rights Reserved.
  9. *
  10. * Written by Rickard E. (Rik) Faith <[email protected]>
  11. *
  12. * Many of the ideas implemented here are from Stephen C. Tweedie,
  13. * especially the idea of avoiding a copy by using getname.
  14. *
  15. * The method for actual interception of syscall entry and exit (not in
  16. * this file -- see entry.S) is based on a GPL'd patch written by
  17. * [email protected] and Copyright 2003 SuSE Linux AG.
  18. *
  19. * POSIX message queue support added by George Wilson <[email protected]>,
  20. * 2006.
  21. *
  22. * The support of additional filter rules compares (>, <, >=, <=) was
  23. * added by Dustin Kirkland <[email protected]>, 2005.
  24. *
  25. * Modified by Amy Griffis <[email protected]> to collect additional
  26. * filesystem information.
  27. *
  28. * Subject and object context labeling support added by <[email protected]>
  29. * and <[email protected]> for LSPP certification compliance.
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/init.h>
  33. #include <asm/types.h>
  34. #include <linux/atomic.h>
  35. #include <linux/fs.h>
  36. #include <linux/namei.h>
  37. #include <linux/mm.h>
  38. #include <linux/export.h>
  39. #include <linux/slab.h>
  40. #include <linux/mount.h>
  41. #include <linux/socket.h>
  42. #include <linux/mqueue.h>
  43. #include <linux/audit.h>
  44. #include <linux/personality.h>
  45. #include <linux/time.h>
  46. #include <linux/netlink.h>
  47. #include <linux/compiler.h>
  48. #include <asm/unistd.h>
  49. #include <linux/security.h>
  50. #include <linux/list.h>
  51. #include <linux/binfmts.h>
  52. #include <linux/highmem.h>
  53. #include <linux/syscalls.h>
  54. #include <asm/syscall.h>
  55. #include <linux/capability.h>
  56. #include <linux/fs_struct.h>
  57. #include <linux/compat.h>
  58. #include <linux/ctype.h>
  59. #include <linux/string.h>
  60. #include <linux/uaccess.h>
  61. #include <linux/fsnotify_backend.h>
  62. #include <uapi/linux/limits.h>
  63. #include <uapi/linux/netfilter/nf_tables.h>
  64. #include <uapi/linux/openat2.h> // struct open_how
  65. #include "audit.h"
  66. /* flags stating the success for a syscall */
  67. #define AUDITSC_INVALID 0
  68. #define AUDITSC_SUCCESS 1
  69. #define AUDITSC_FAILURE 2
  70. /* no execve audit message should be longer than this (userspace limits),
  71. * see the note near the top of audit_log_execve_info() about this value */
  72. #define MAX_EXECVE_AUDIT_LEN 7500
  73. /* max length to print of cmdline/proctitle value during audit */
  74. #define MAX_PROCTITLE_AUDIT_LEN 128
  75. /* number of audit rules */
  76. int audit_n_rules;
  77. /* determines whether we collect data for signals sent */
  78. int audit_signals;
  79. struct audit_aux_data {
  80. struct audit_aux_data *next;
  81. int type;
  82. };
  83. /* Number of target pids per aux struct. */
  84. #define AUDIT_AUX_PIDS 16
  85. struct audit_aux_data_pids {
  86. struct audit_aux_data d;
  87. pid_t target_pid[AUDIT_AUX_PIDS];
  88. kuid_t target_auid[AUDIT_AUX_PIDS];
  89. kuid_t target_uid[AUDIT_AUX_PIDS];
  90. unsigned int target_sessionid[AUDIT_AUX_PIDS];
  91. u32 target_sid[AUDIT_AUX_PIDS];
  92. char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
  93. int pid_count;
  94. };
  95. struct audit_aux_data_bprm_fcaps {
  96. struct audit_aux_data d;
  97. struct audit_cap_data fcap;
  98. unsigned int fcap_ver;
  99. struct audit_cap_data old_pcap;
  100. struct audit_cap_data new_pcap;
  101. };
  102. struct audit_tree_refs {
  103. struct audit_tree_refs *next;
  104. struct audit_chunk *c[31];
  105. };
  106. struct audit_nfcfgop_tab {
  107. enum audit_nfcfgop op;
  108. const char *s;
  109. };
  110. static const struct audit_nfcfgop_tab audit_nfcfgs[] = {
  111. { AUDIT_XT_OP_REGISTER, "xt_register" },
  112. { AUDIT_XT_OP_REPLACE, "xt_replace" },
  113. { AUDIT_XT_OP_UNREGISTER, "xt_unregister" },
  114. { AUDIT_NFT_OP_TABLE_REGISTER, "nft_register_table" },
  115. { AUDIT_NFT_OP_TABLE_UNREGISTER, "nft_unregister_table" },
  116. { AUDIT_NFT_OP_CHAIN_REGISTER, "nft_register_chain" },
  117. { AUDIT_NFT_OP_CHAIN_UNREGISTER, "nft_unregister_chain" },
  118. { AUDIT_NFT_OP_RULE_REGISTER, "nft_register_rule" },
  119. { AUDIT_NFT_OP_RULE_UNREGISTER, "nft_unregister_rule" },
  120. { AUDIT_NFT_OP_SET_REGISTER, "nft_register_set" },
  121. { AUDIT_NFT_OP_SET_UNREGISTER, "nft_unregister_set" },
  122. { AUDIT_NFT_OP_SETELEM_REGISTER, "nft_register_setelem" },
  123. { AUDIT_NFT_OP_SETELEM_UNREGISTER, "nft_unregister_setelem" },
  124. { AUDIT_NFT_OP_GEN_REGISTER, "nft_register_gen" },
  125. { AUDIT_NFT_OP_OBJ_REGISTER, "nft_register_obj" },
  126. { AUDIT_NFT_OP_OBJ_UNREGISTER, "nft_unregister_obj" },
  127. { AUDIT_NFT_OP_OBJ_RESET, "nft_reset_obj" },
  128. { AUDIT_NFT_OP_FLOWTABLE_REGISTER, "nft_register_flowtable" },
  129. { AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, "nft_unregister_flowtable" },
  130. { AUDIT_NFT_OP_INVALID, "nft_invalid" },
  131. };
  132. static int audit_match_perm(struct audit_context *ctx, int mask)
  133. {
  134. unsigned n;
  135. if (unlikely(!ctx))
  136. return 0;
  137. n = ctx->major;
  138. switch (audit_classify_syscall(ctx->arch, n)) {
  139. case AUDITSC_NATIVE:
  140. if ((mask & AUDIT_PERM_WRITE) &&
  141. audit_match_class(AUDIT_CLASS_WRITE, n))
  142. return 1;
  143. if ((mask & AUDIT_PERM_READ) &&
  144. audit_match_class(AUDIT_CLASS_READ, n))
  145. return 1;
  146. if ((mask & AUDIT_PERM_ATTR) &&
  147. audit_match_class(AUDIT_CLASS_CHATTR, n))
  148. return 1;
  149. return 0;
  150. case AUDITSC_COMPAT: /* 32bit on biarch */
  151. if ((mask & AUDIT_PERM_WRITE) &&
  152. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  153. return 1;
  154. if ((mask & AUDIT_PERM_READ) &&
  155. audit_match_class(AUDIT_CLASS_READ_32, n))
  156. return 1;
  157. if ((mask & AUDIT_PERM_ATTR) &&
  158. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  159. return 1;
  160. return 0;
  161. case AUDITSC_OPEN:
  162. return mask & ACC_MODE(ctx->argv[1]);
  163. case AUDITSC_OPENAT:
  164. return mask & ACC_MODE(ctx->argv[2]);
  165. case AUDITSC_SOCKETCALL:
  166. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  167. case AUDITSC_EXECVE:
  168. return mask & AUDIT_PERM_EXEC;
  169. case AUDITSC_OPENAT2:
  170. return mask & ACC_MODE((u32)ctx->openat2.flags);
  171. default:
  172. return 0;
  173. }
  174. }
  175. static int audit_match_filetype(struct audit_context *ctx, int val)
  176. {
  177. struct audit_names *n;
  178. umode_t mode = (umode_t)val;
  179. if (unlikely(!ctx))
  180. return 0;
  181. list_for_each_entry(n, &ctx->names_list, list) {
  182. if ((n->ino != AUDIT_INO_UNSET) &&
  183. ((n->mode & S_IFMT) == mode))
  184. return 1;
  185. }
  186. return 0;
  187. }
  188. /*
  189. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  190. * ->first_trees points to its beginning, ->trees - to the current end of data.
  191. * ->tree_count is the number of free entries in array pointed to by ->trees.
  192. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  193. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  194. * it's going to remain 1-element for almost any setup) until we free context itself.
  195. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  196. */
  197. static void audit_set_auditable(struct audit_context *ctx)
  198. {
  199. if (!ctx->prio) {
  200. ctx->prio = 1;
  201. ctx->current_state = AUDIT_STATE_RECORD;
  202. }
  203. }
  204. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  205. {
  206. struct audit_tree_refs *p = ctx->trees;
  207. int left = ctx->tree_count;
  208. if (likely(left)) {
  209. p->c[--left] = chunk;
  210. ctx->tree_count = left;
  211. return 1;
  212. }
  213. if (!p)
  214. return 0;
  215. p = p->next;
  216. if (p) {
  217. p->c[30] = chunk;
  218. ctx->trees = p;
  219. ctx->tree_count = 30;
  220. return 1;
  221. }
  222. return 0;
  223. }
  224. static int grow_tree_refs(struct audit_context *ctx)
  225. {
  226. struct audit_tree_refs *p = ctx->trees;
  227. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  228. if (!ctx->trees) {
  229. ctx->trees = p;
  230. return 0;
  231. }
  232. if (p)
  233. p->next = ctx->trees;
  234. else
  235. ctx->first_trees = ctx->trees;
  236. ctx->tree_count = 31;
  237. return 1;
  238. }
  239. static void unroll_tree_refs(struct audit_context *ctx,
  240. struct audit_tree_refs *p, int count)
  241. {
  242. struct audit_tree_refs *q;
  243. int n;
  244. if (!p) {
  245. /* we started with empty chain */
  246. p = ctx->first_trees;
  247. count = 31;
  248. /* if the very first allocation has failed, nothing to do */
  249. if (!p)
  250. return;
  251. }
  252. n = count;
  253. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  254. while (n--) {
  255. audit_put_chunk(q->c[n]);
  256. q->c[n] = NULL;
  257. }
  258. }
  259. while (n-- > ctx->tree_count) {
  260. audit_put_chunk(q->c[n]);
  261. q->c[n] = NULL;
  262. }
  263. ctx->trees = p;
  264. ctx->tree_count = count;
  265. }
  266. static void free_tree_refs(struct audit_context *ctx)
  267. {
  268. struct audit_tree_refs *p, *q;
  269. for (p = ctx->first_trees; p; p = q) {
  270. q = p->next;
  271. kfree(p);
  272. }
  273. }
  274. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  275. {
  276. struct audit_tree_refs *p;
  277. int n;
  278. if (!tree)
  279. return 0;
  280. /* full ones */
  281. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  282. for (n = 0; n < 31; n++)
  283. if (audit_tree_match(p->c[n], tree))
  284. return 1;
  285. }
  286. /* partial */
  287. if (p) {
  288. for (n = ctx->tree_count; n < 31; n++)
  289. if (audit_tree_match(p->c[n], tree))
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. static int audit_compare_uid(kuid_t uid,
  295. struct audit_names *name,
  296. struct audit_field *f,
  297. struct audit_context *ctx)
  298. {
  299. struct audit_names *n;
  300. int rc;
  301. if (name) {
  302. rc = audit_uid_comparator(uid, f->op, name->uid);
  303. if (rc)
  304. return rc;
  305. }
  306. if (ctx) {
  307. list_for_each_entry(n, &ctx->names_list, list) {
  308. rc = audit_uid_comparator(uid, f->op, n->uid);
  309. if (rc)
  310. return rc;
  311. }
  312. }
  313. return 0;
  314. }
  315. static int audit_compare_gid(kgid_t gid,
  316. struct audit_names *name,
  317. struct audit_field *f,
  318. struct audit_context *ctx)
  319. {
  320. struct audit_names *n;
  321. int rc;
  322. if (name) {
  323. rc = audit_gid_comparator(gid, f->op, name->gid);
  324. if (rc)
  325. return rc;
  326. }
  327. if (ctx) {
  328. list_for_each_entry(n, &ctx->names_list, list) {
  329. rc = audit_gid_comparator(gid, f->op, n->gid);
  330. if (rc)
  331. return rc;
  332. }
  333. }
  334. return 0;
  335. }
  336. static int audit_field_compare(struct task_struct *tsk,
  337. const struct cred *cred,
  338. struct audit_field *f,
  339. struct audit_context *ctx,
  340. struct audit_names *name)
  341. {
  342. switch (f->val) {
  343. /* process to file object comparisons */
  344. case AUDIT_COMPARE_UID_TO_OBJ_UID:
  345. return audit_compare_uid(cred->uid, name, f, ctx);
  346. case AUDIT_COMPARE_GID_TO_OBJ_GID:
  347. return audit_compare_gid(cred->gid, name, f, ctx);
  348. case AUDIT_COMPARE_EUID_TO_OBJ_UID:
  349. return audit_compare_uid(cred->euid, name, f, ctx);
  350. case AUDIT_COMPARE_EGID_TO_OBJ_GID:
  351. return audit_compare_gid(cred->egid, name, f, ctx);
  352. case AUDIT_COMPARE_AUID_TO_OBJ_UID:
  353. return audit_compare_uid(audit_get_loginuid(tsk), name, f, ctx);
  354. case AUDIT_COMPARE_SUID_TO_OBJ_UID:
  355. return audit_compare_uid(cred->suid, name, f, ctx);
  356. case AUDIT_COMPARE_SGID_TO_OBJ_GID:
  357. return audit_compare_gid(cred->sgid, name, f, ctx);
  358. case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
  359. return audit_compare_uid(cred->fsuid, name, f, ctx);
  360. case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
  361. return audit_compare_gid(cred->fsgid, name, f, ctx);
  362. /* uid comparisons */
  363. case AUDIT_COMPARE_UID_TO_AUID:
  364. return audit_uid_comparator(cred->uid, f->op,
  365. audit_get_loginuid(tsk));
  366. case AUDIT_COMPARE_UID_TO_EUID:
  367. return audit_uid_comparator(cred->uid, f->op, cred->euid);
  368. case AUDIT_COMPARE_UID_TO_SUID:
  369. return audit_uid_comparator(cred->uid, f->op, cred->suid);
  370. case AUDIT_COMPARE_UID_TO_FSUID:
  371. return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
  372. /* auid comparisons */
  373. case AUDIT_COMPARE_AUID_TO_EUID:
  374. return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
  375. cred->euid);
  376. case AUDIT_COMPARE_AUID_TO_SUID:
  377. return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
  378. cred->suid);
  379. case AUDIT_COMPARE_AUID_TO_FSUID:
  380. return audit_uid_comparator(audit_get_loginuid(tsk), f->op,
  381. cred->fsuid);
  382. /* euid comparisons */
  383. case AUDIT_COMPARE_EUID_TO_SUID:
  384. return audit_uid_comparator(cred->euid, f->op, cred->suid);
  385. case AUDIT_COMPARE_EUID_TO_FSUID:
  386. return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
  387. /* suid comparisons */
  388. case AUDIT_COMPARE_SUID_TO_FSUID:
  389. return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
  390. /* gid comparisons */
  391. case AUDIT_COMPARE_GID_TO_EGID:
  392. return audit_gid_comparator(cred->gid, f->op, cred->egid);
  393. case AUDIT_COMPARE_GID_TO_SGID:
  394. return audit_gid_comparator(cred->gid, f->op, cred->sgid);
  395. case AUDIT_COMPARE_GID_TO_FSGID:
  396. return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
  397. /* egid comparisons */
  398. case AUDIT_COMPARE_EGID_TO_SGID:
  399. return audit_gid_comparator(cred->egid, f->op, cred->sgid);
  400. case AUDIT_COMPARE_EGID_TO_FSGID:
  401. return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
  402. /* sgid comparison */
  403. case AUDIT_COMPARE_SGID_TO_FSGID:
  404. return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
  405. default:
  406. WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
  407. return 0;
  408. }
  409. return 0;
  410. }
  411. /* Determine if any context name data matches a rule's watch data */
  412. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  413. * otherwise.
  414. *
  415. * If task_creation is true, this is an explicit indication that we are
  416. * filtering a task rule at task creation time. This and tsk == current are
  417. * the only situations where tsk->cred may be accessed without an rcu read lock.
  418. */
  419. static int audit_filter_rules(struct task_struct *tsk,
  420. struct audit_krule *rule,
  421. struct audit_context *ctx,
  422. struct audit_names *name,
  423. enum audit_state *state,
  424. bool task_creation)
  425. {
  426. const struct cred *cred;
  427. int i, need_sid = 1;
  428. u32 sid;
  429. unsigned int sessionid;
  430. if (ctx && rule->prio <= ctx->prio)
  431. return 0;
  432. cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
  433. for (i = 0; i < rule->field_count; i++) {
  434. struct audit_field *f = &rule->fields[i];
  435. struct audit_names *n;
  436. int result = 0;
  437. pid_t pid;
  438. switch (f->type) {
  439. case AUDIT_PID:
  440. pid = task_tgid_nr(tsk);
  441. result = audit_comparator(pid, f->op, f->val);
  442. break;
  443. case AUDIT_PPID:
  444. if (ctx) {
  445. if (!ctx->ppid)
  446. ctx->ppid = task_ppid_nr(tsk);
  447. result = audit_comparator(ctx->ppid, f->op, f->val);
  448. }
  449. break;
  450. case AUDIT_EXE:
  451. result = audit_exe_compare(tsk, rule->exe);
  452. if (f->op == Audit_not_equal)
  453. result = !result;
  454. break;
  455. case AUDIT_UID:
  456. result = audit_uid_comparator(cred->uid, f->op, f->uid);
  457. break;
  458. case AUDIT_EUID:
  459. result = audit_uid_comparator(cred->euid, f->op, f->uid);
  460. break;
  461. case AUDIT_SUID:
  462. result = audit_uid_comparator(cred->suid, f->op, f->uid);
  463. break;
  464. case AUDIT_FSUID:
  465. result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
  466. break;
  467. case AUDIT_GID:
  468. result = audit_gid_comparator(cred->gid, f->op, f->gid);
  469. if (f->op == Audit_equal) {
  470. if (!result)
  471. result = groups_search(cred->group_info, f->gid);
  472. } else if (f->op == Audit_not_equal) {
  473. if (result)
  474. result = !groups_search(cred->group_info, f->gid);
  475. }
  476. break;
  477. case AUDIT_EGID:
  478. result = audit_gid_comparator(cred->egid, f->op, f->gid);
  479. if (f->op == Audit_equal) {
  480. if (!result)
  481. result = groups_search(cred->group_info, f->gid);
  482. } else if (f->op == Audit_not_equal) {
  483. if (result)
  484. result = !groups_search(cred->group_info, f->gid);
  485. }
  486. break;
  487. case AUDIT_SGID:
  488. result = audit_gid_comparator(cred->sgid, f->op, f->gid);
  489. break;
  490. case AUDIT_FSGID:
  491. result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
  492. break;
  493. case AUDIT_SESSIONID:
  494. sessionid = audit_get_sessionid(tsk);
  495. result = audit_comparator(sessionid, f->op, f->val);
  496. break;
  497. case AUDIT_PERS:
  498. result = audit_comparator(tsk->personality, f->op, f->val);
  499. break;
  500. case AUDIT_ARCH:
  501. if (ctx)
  502. result = audit_comparator(ctx->arch, f->op, f->val);
  503. break;
  504. case AUDIT_EXIT:
  505. if (ctx && ctx->return_valid != AUDITSC_INVALID)
  506. result = audit_comparator(ctx->return_code, f->op, f->val);
  507. break;
  508. case AUDIT_SUCCESS:
  509. if (ctx && ctx->return_valid != AUDITSC_INVALID) {
  510. if (f->val)
  511. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  512. else
  513. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  514. }
  515. break;
  516. case AUDIT_DEVMAJOR:
  517. if (name) {
  518. if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
  519. audit_comparator(MAJOR(name->rdev), f->op, f->val))
  520. ++result;
  521. } else if (ctx) {
  522. list_for_each_entry(n, &ctx->names_list, list) {
  523. if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
  524. audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
  525. ++result;
  526. break;
  527. }
  528. }
  529. }
  530. break;
  531. case AUDIT_DEVMINOR:
  532. if (name) {
  533. if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
  534. audit_comparator(MINOR(name->rdev), f->op, f->val))
  535. ++result;
  536. } else if (ctx) {
  537. list_for_each_entry(n, &ctx->names_list, list) {
  538. if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
  539. audit_comparator(MINOR(n->rdev), f->op, f->val)) {
  540. ++result;
  541. break;
  542. }
  543. }
  544. }
  545. break;
  546. case AUDIT_INODE:
  547. if (name)
  548. result = audit_comparator(name->ino, f->op, f->val);
  549. else if (ctx) {
  550. list_for_each_entry(n, &ctx->names_list, list) {
  551. if (audit_comparator(n->ino, f->op, f->val)) {
  552. ++result;
  553. break;
  554. }
  555. }
  556. }
  557. break;
  558. case AUDIT_OBJ_UID:
  559. if (name) {
  560. result = audit_uid_comparator(name->uid, f->op, f->uid);
  561. } else if (ctx) {
  562. list_for_each_entry(n, &ctx->names_list, list) {
  563. if (audit_uid_comparator(n->uid, f->op, f->uid)) {
  564. ++result;
  565. break;
  566. }
  567. }
  568. }
  569. break;
  570. case AUDIT_OBJ_GID:
  571. if (name) {
  572. result = audit_gid_comparator(name->gid, f->op, f->gid);
  573. } else if (ctx) {
  574. list_for_each_entry(n, &ctx->names_list, list) {
  575. if (audit_gid_comparator(n->gid, f->op, f->gid)) {
  576. ++result;
  577. break;
  578. }
  579. }
  580. }
  581. break;
  582. case AUDIT_WATCH:
  583. if (name) {
  584. result = audit_watch_compare(rule->watch,
  585. name->ino,
  586. name->dev);
  587. if (f->op == Audit_not_equal)
  588. result = !result;
  589. }
  590. break;
  591. case AUDIT_DIR:
  592. if (ctx) {
  593. result = match_tree_refs(ctx, rule->tree);
  594. if (f->op == Audit_not_equal)
  595. result = !result;
  596. }
  597. break;
  598. case AUDIT_LOGINUID:
  599. result = audit_uid_comparator(audit_get_loginuid(tsk),
  600. f->op, f->uid);
  601. break;
  602. case AUDIT_LOGINUID_SET:
  603. result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
  604. break;
  605. case AUDIT_SADDR_FAM:
  606. if (ctx && ctx->sockaddr)
  607. result = audit_comparator(ctx->sockaddr->ss_family,
  608. f->op, f->val);
  609. break;
  610. case AUDIT_SUBJ_USER:
  611. case AUDIT_SUBJ_ROLE:
  612. case AUDIT_SUBJ_TYPE:
  613. case AUDIT_SUBJ_SEN:
  614. case AUDIT_SUBJ_CLR:
  615. /* NOTE: this may return negative values indicating
  616. a temporary error. We simply treat this as a
  617. match for now to avoid losing information that
  618. may be wanted. An error message will also be
  619. logged upon error */
  620. if (f->lsm_rule) {
  621. if (need_sid) {
  622. /* @tsk should always be equal to
  623. * @current with the exception of
  624. * fork()/copy_process() in which case
  625. * the new @tsk creds are still a dup
  626. * of @current's creds so we can still
  627. * use security_current_getsecid_subj()
  628. * here even though it always refs
  629. * @current's creds
  630. */
  631. security_current_getsecid_subj(&sid);
  632. need_sid = 0;
  633. }
  634. result = security_audit_rule_match(sid, f->type,
  635. f->op,
  636. f->lsm_rule);
  637. }
  638. break;
  639. case AUDIT_OBJ_USER:
  640. case AUDIT_OBJ_ROLE:
  641. case AUDIT_OBJ_TYPE:
  642. case AUDIT_OBJ_LEV_LOW:
  643. case AUDIT_OBJ_LEV_HIGH:
  644. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  645. also applies here */
  646. if (f->lsm_rule) {
  647. /* Find files that match */
  648. if (name) {
  649. result = security_audit_rule_match(
  650. name->osid,
  651. f->type,
  652. f->op,
  653. f->lsm_rule);
  654. } else if (ctx) {
  655. list_for_each_entry(n, &ctx->names_list, list) {
  656. if (security_audit_rule_match(
  657. n->osid,
  658. f->type,
  659. f->op,
  660. f->lsm_rule)) {
  661. ++result;
  662. break;
  663. }
  664. }
  665. }
  666. /* Find ipc objects that match */
  667. if (!ctx || ctx->type != AUDIT_IPC)
  668. break;
  669. if (security_audit_rule_match(ctx->ipc.osid,
  670. f->type, f->op,
  671. f->lsm_rule))
  672. ++result;
  673. }
  674. break;
  675. case AUDIT_ARG0:
  676. case AUDIT_ARG1:
  677. case AUDIT_ARG2:
  678. case AUDIT_ARG3:
  679. if (ctx)
  680. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  681. break;
  682. case AUDIT_FILTERKEY:
  683. /* ignore this field for filtering */
  684. result = 1;
  685. break;
  686. case AUDIT_PERM:
  687. result = audit_match_perm(ctx, f->val);
  688. if (f->op == Audit_not_equal)
  689. result = !result;
  690. break;
  691. case AUDIT_FILETYPE:
  692. result = audit_match_filetype(ctx, f->val);
  693. if (f->op == Audit_not_equal)
  694. result = !result;
  695. break;
  696. case AUDIT_FIELD_COMPARE:
  697. result = audit_field_compare(tsk, cred, f, ctx, name);
  698. break;
  699. }
  700. if (!result)
  701. return 0;
  702. }
  703. if (ctx) {
  704. if (rule->filterkey) {
  705. kfree(ctx->filterkey);
  706. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  707. }
  708. ctx->prio = rule->prio;
  709. }
  710. switch (rule->action) {
  711. case AUDIT_NEVER:
  712. *state = AUDIT_STATE_DISABLED;
  713. break;
  714. case AUDIT_ALWAYS:
  715. *state = AUDIT_STATE_RECORD;
  716. break;
  717. }
  718. return 1;
  719. }
  720. /* At process creation time, we can determine if system-call auditing is
  721. * completely disabled for this task. Since we only have the task
  722. * structure at this point, we can only check uid and gid.
  723. */
  724. static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
  725. {
  726. struct audit_entry *e;
  727. enum audit_state state;
  728. rcu_read_lock();
  729. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  730. if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
  731. &state, true)) {
  732. if (state == AUDIT_STATE_RECORD)
  733. *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
  734. rcu_read_unlock();
  735. return state;
  736. }
  737. }
  738. rcu_read_unlock();
  739. return AUDIT_STATE_BUILD;
  740. }
  741. static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
  742. {
  743. int word, bit;
  744. if (val > 0xffffffff)
  745. return false;
  746. word = AUDIT_WORD(val);
  747. if (word >= AUDIT_BITMASK_SIZE)
  748. return false;
  749. bit = AUDIT_BIT(val);
  750. return rule->mask[word] & bit;
  751. }
  752. /**
  753. * audit_filter_uring - apply filters to an io_uring operation
  754. * @tsk: associated task
  755. * @ctx: audit context
  756. */
  757. static void audit_filter_uring(struct task_struct *tsk,
  758. struct audit_context *ctx)
  759. {
  760. struct audit_entry *e;
  761. enum audit_state state;
  762. if (auditd_test_task(tsk))
  763. return;
  764. rcu_read_lock();
  765. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_URING_EXIT],
  766. list) {
  767. if (audit_in_mask(&e->rule, ctx->uring_op) &&
  768. audit_filter_rules(tsk, &e->rule, ctx, NULL, &state,
  769. false)) {
  770. rcu_read_unlock();
  771. ctx->current_state = state;
  772. return;
  773. }
  774. }
  775. rcu_read_unlock();
  776. }
  777. /* At syscall exit time, this filter is called if the audit_state is
  778. * not low enough that auditing cannot take place, but is also not
  779. * high enough that we already know we have to write an audit record
  780. * (i.e., the state is AUDIT_STATE_BUILD).
  781. */
  782. static void audit_filter_syscall(struct task_struct *tsk,
  783. struct audit_context *ctx)
  784. {
  785. struct audit_entry *e;
  786. enum audit_state state;
  787. if (auditd_test_task(tsk))
  788. return;
  789. rcu_read_lock();
  790. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_EXIT], list) {
  791. if (audit_in_mask(&e->rule, ctx->major) &&
  792. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  793. &state, false)) {
  794. rcu_read_unlock();
  795. ctx->current_state = state;
  796. return;
  797. }
  798. }
  799. rcu_read_unlock();
  800. return;
  801. }
  802. /*
  803. * Given an audit_name check the inode hash table to see if they match.
  804. * Called holding the rcu read lock to protect the use of audit_inode_hash
  805. */
  806. static int audit_filter_inode_name(struct task_struct *tsk,
  807. struct audit_names *n,
  808. struct audit_context *ctx) {
  809. int h = audit_hash_ino((u32)n->ino);
  810. struct list_head *list = &audit_inode_hash[h];
  811. struct audit_entry *e;
  812. enum audit_state state;
  813. list_for_each_entry_rcu(e, list, list) {
  814. if (audit_in_mask(&e->rule, ctx->major) &&
  815. audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
  816. ctx->current_state = state;
  817. return 1;
  818. }
  819. }
  820. return 0;
  821. }
  822. /* At syscall exit time, this filter is called if any audit_names have been
  823. * collected during syscall processing. We only check rules in sublists at hash
  824. * buckets applicable to the inode numbers in audit_names.
  825. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  826. */
  827. void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
  828. {
  829. struct audit_names *n;
  830. if (auditd_test_task(tsk))
  831. return;
  832. rcu_read_lock();
  833. list_for_each_entry(n, &ctx->names_list, list) {
  834. if (audit_filter_inode_name(tsk, n, ctx))
  835. break;
  836. }
  837. rcu_read_unlock();
  838. }
  839. static inline void audit_proctitle_free(struct audit_context *context)
  840. {
  841. kfree(context->proctitle.value);
  842. context->proctitle.value = NULL;
  843. context->proctitle.len = 0;
  844. }
  845. static inline void audit_free_module(struct audit_context *context)
  846. {
  847. if (context->type == AUDIT_KERN_MODULE) {
  848. kfree(context->module.name);
  849. context->module.name = NULL;
  850. }
  851. }
  852. static inline void audit_free_names(struct audit_context *context)
  853. {
  854. struct audit_names *n, *next;
  855. list_for_each_entry_safe(n, next, &context->names_list, list) {
  856. list_del(&n->list);
  857. if (n->name)
  858. putname(n->name);
  859. if (n->should_free)
  860. kfree(n);
  861. }
  862. context->name_count = 0;
  863. path_put(&context->pwd);
  864. context->pwd.dentry = NULL;
  865. context->pwd.mnt = NULL;
  866. }
  867. static inline void audit_free_aux(struct audit_context *context)
  868. {
  869. struct audit_aux_data *aux;
  870. while ((aux = context->aux)) {
  871. context->aux = aux->next;
  872. kfree(aux);
  873. }
  874. context->aux = NULL;
  875. while ((aux = context->aux_pids)) {
  876. context->aux_pids = aux->next;
  877. kfree(aux);
  878. }
  879. context->aux_pids = NULL;
  880. }
  881. /**
  882. * audit_reset_context - reset a audit_context structure
  883. * @ctx: the audit_context to reset
  884. *
  885. * All fields in the audit_context will be reset to an initial state, all
  886. * references held by fields will be dropped, and private memory will be
  887. * released. When this function returns the audit_context will be suitable
  888. * for reuse, so long as the passed context is not NULL or a dummy context.
  889. */
  890. static void audit_reset_context(struct audit_context *ctx)
  891. {
  892. if (!ctx)
  893. return;
  894. /* if ctx is non-null, reset the "ctx->context" regardless */
  895. ctx->context = AUDIT_CTX_UNUSED;
  896. if (ctx->dummy)
  897. return;
  898. /*
  899. * NOTE: It shouldn't matter in what order we release the fields, so
  900. * release them in the order in which they appear in the struct;
  901. * this gives us some hope of quickly making sure we are
  902. * resetting the audit_context properly.
  903. *
  904. * Other things worth mentioning:
  905. * - we don't reset "dummy"
  906. * - we don't reset "state", we do reset "current_state"
  907. * - we preserve "filterkey" if "state" is AUDIT_STATE_RECORD
  908. * - much of this is likely overkill, but play it safe for now
  909. * - we really need to work on improving the audit_context struct
  910. */
  911. ctx->current_state = ctx->state;
  912. ctx->serial = 0;
  913. ctx->major = 0;
  914. ctx->uring_op = 0;
  915. ctx->ctime = (struct timespec64){ .tv_sec = 0, .tv_nsec = 0 };
  916. memset(ctx->argv, 0, sizeof(ctx->argv));
  917. ctx->return_code = 0;
  918. ctx->prio = (ctx->state == AUDIT_STATE_RECORD ? ~0ULL : 0);
  919. ctx->return_valid = AUDITSC_INVALID;
  920. audit_free_names(ctx);
  921. if (ctx->state != AUDIT_STATE_RECORD) {
  922. kfree(ctx->filterkey);
  923. ctx->filterkey = NULL;
  924. }
  925. audit_free_aux(ctx);
  926. kfree(ctx->sockaddr);
  927. ctx->sockaddr = NULL;
  928. ctx->sockaddr_len = 0;
  929. ctx->ppid = 0;
  930. ctx->uid = ctx->euid = ctx->suid = ctx->fsuid = KUIDT_INIT(0);
  931. ctx->gid = ctx->egid = ctx->sgid = ctx->fsgid = KGIDT_INIT(0);
  932. ctx->personality = 0;
  933. ctx->arch = 0;
  934. ctx->target_pid = 0;
  935. ctx->target_auid = ctx->target_uid = KUIDT_INIT(0);
  936. ctx->target_sessionid = 0;
  937. ctx->target_sid = 0;
  938. ctx->target_comm[0] = '\0';
  939. unroll_tree_refs(ctx, NULL, 0);
  940. WARN_ON(!list_empty(&ctx->killed_trees));
  941. audit_free_module(ctx);
  942. ctx->fds[0] = -1;
  943. ctx->type = 0; /* reset last for audit_free_*() */
  944. }
  945. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  946. {
  947. struct audit_context *context;
  948. context = kzalloc(sizeof(*context), GFP_KERNEL);
  949. if (!context)
  950. return NULL;
  951. context->context = AUDIT_CTX_UNUSED;
  952. context->state = state;
  953. context->prio = state == AUDIT_STATE_RECORD ? ~0ULL : 0;
  954. INIT_LIST_HEAD(&context->killed_trees);
  955. INIT_LIST_HEAD(&context->names_list);
  956. context->fds[0] = -1;
  957. context->return_valid = AUDITSC_INVALID;
  958. return context;
  959. }
  960. /**
  961. * audit_alloc - allocate an audit context block for a task
  962. * @tsk: task
  963. *
  964. * Filter on the task information and allocate a per-task audit context
  965. * if necessary. Doing so turns on system call auditing for the
  966. * specified task. This is called from copy_process, so no lock is
  967. * needed.
  968. */
  969. int audit_alloc(struct task_struct *tsk)
  970. {
  971. struct audit_context *context;
  972. enum audit_state state;
  973. char *key = NULL;
  974. if (likely(!audit_ever_enabled))
  975. return 0;
  976. state = audit_filter_task(tsk, &key);
  977. if (state == AUDIT_STATE_DISABLED) {
  978. clear_task_syscall_work(tsk, SYSCALL_AUDIT);
  979. return 0;
  980. }
  981. if (!(context = audit_alloc_context(state))) {
  982. kfree(key);
  983. audit_log_lost("out of memory in audit_alloc");
  984. return -ENOMEM;
  985. }
  986. context->filterkey = key;
  987. audit_set_context(tsk, context);
  988. set_task_syscall_work(tsk, SYSCALL_AUDIT);
  989. return 0;
  990. }
  991. static inline void audit_free_context(struct audit_context *context)
  992. {
  993. /* resetting is extra work, but it is likely just noise */
  994. audit_reset_context(context);
  995. audit_proctitle_free(context);
  996. free_tree_refs(context);
  997. kfree(context->filterkey);
  998. kfree(context);
  999. }
  1000. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  1001. kuid_t auid, kuid_t uid, unsigned int sessionid,
  1002. u32 sid, char *comm)
  1003. {
  1004. struct audit_buffer *ab;
  1005. char *ctx = NULL;
  1006. u32 len;
  1007. int rc = 0;
  1008. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  1009. if (!ab)
  1010. return rc;
  1011. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
  1012. from_kuid(&init_user_ns, auid),
  1013. from_kuid(&init_user_ns, uid), sessionid);
  1014. if (sid) {
  1015. if (security_secid_to_secctx(sid, &ctx, &len)) {
  1016. audit_log_format(ab, " obj=(none)");
  1017. rc = 1;
  1018. } else {
  1019. audit_log_format(ab, " obj=%s", ctx);
  1020. security_release_secctx(ctx, len);
  1021. }
  1022. }
  1023. audit_log_format(ab, " ocomm=");
  1024. audit_log_untrustedstring(ab, comm);
  1025. audit_log_end(ab);
  1026. return rc;
  1027. }
  1028. static void audit_log_execve_info(struct audit_context *context,
  1029. struct audit_buffer **ab)
  1030. {
  1031. long len_max;
  1032. long len_rem;
  1033. long len_full;
  1034. long len_buf;
  1035. long len_abuf = 0;
  1036. long len_tmp;
  1037. bool require_data;
  1038. bool encode;
  1039. unsigned int iter;
  1040. unsigned int arg;
  1041. char *buf_head;
  1042. char *buf;
  1043. const char __user *p = (const char __user *)current->mm->arg_start;
  1044. /* NOTE: this buffer needs to be large enough to hold all the non-arg
  1045. * data we put in the audit record for this argument (see the
  1046. * code below) ... at this point in time 96 is plenty */
  1047. char abuf[96];
  1048. /* NOTE: we set MAX_EXECVE_AUDIT_LEN to a rather arbitrary limit, the
  1049. * current value of 7500 is not as important as the fact that it
  1050. * is less than 8k, a setting of 7500 gives us plenty of wiggle
  1051. * room if we go over a little bit in the logging below */
  1052. WARN_ON_ONCE(MAX_EXECVE_AUDIT_LEN > 7500);
  1053. len_max = MAX_EXECVE_AUDIT_LEN;
  1054. /* scratch buffer to hold the userspace args */
  1055. buf_head = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1056. if (!buf_head) {
  1057. audit_panic("out of memory for argv string");
  1058. return;
  1059. }
  1060. buf = buf_head;
  1061. audit_log_format(*ab, "argc=%d", context->execve.argc);
  1062. len_rem = len_max;
  1063. len_buf = 0;
  1064. len_full = 0;
  1065. require_data = true;
  1066. encode = false;
  1067. iter = 0;
  1068. arg = 0;
  1069. do {
  1070. /* NOTE: we don't ever want to trust this value for anything
  1071. * serious, but the audit record format insists we
  1072. * provide an argument length for really long arguments,
  1073. * e.g. > MAX_EXECVE_AUDIT_LEN, so we have no choice but
  1074. * to use strncpy_from_user() to obtain this value for
  1075. * recording in the log, although we don't use it
  1076. * anywhere here to avoid a double-fetch problem */
  1077. if (len_full == 0)
  1078. len_full = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  1079. /* read more data from userspace */
  1080. if (require_data) {
  1081. /* can we make more room in the buffer? */
  1082. if (buf != buf_head) {
  1083. memmove(buf_head, buf, len_buf);
  1084. buf = buf_head;
  1085. }
  1086. /* fetch as much as we can of the argument */
  1087. len_tmp = strncpy_from_user(&buf_head[len_buf], p,
  1088. len_max - len_buf);
  1089. if (len_tmp == -EFAULT) {
  1090. /* unable to copy from userspace */
  1091. send_sig(SIGKILL, current, 0);
  1092. goto out;
  1093. } else if (len_tmp == (len_max - len_buf)) {
  1094. /* buffer is not large enough */
  1095. require_data = true;
  1096. /* NOTE: if we are going to span multiple
  1097. * buffers force the encoding so we stand
  1098. * a chance at a sane len_full value and
  1099. * consistent record encoding */
  1100. encode = true;
  1101. len_full = len_full * 2;
  1102. p += len_tmp;
  1103. } else {
  1104. require_data = false;
  1105. if (!encode)
  1106. encode = audit_string_contains_control(
  1107. buf, len_tmp);
  1108. /* try to use a trusted value for len_full */
  1109. if (len_full < len_max)
  1110. len_full = (encode ?
  1111. len_tmp * 2 : len_tmp);
  1112. p += len_tmp + 1;
  1113. }
  1114. len_buf += len_tmp;
  1115. buf_head[len_buf] = '\0';
  1116. /* length of the buffer in the audit record? */
  1117. len_abuf = (encode ? len_buf * 2 : len_buf + 2);
  1118. }
  1119. /* write as much as we can to the audit log */
  1120. if (len_buf >= 0) {
  1121. /* NOTE: some magic numbers here - basically if we
  1122. * can't fit a reasonable amount of data into the
  1123. * existing audit buffer, flush it and start with
  1124. * a new buffer */
  1125. if ((sizeof(abuf) + 8) > len_rem) {
  1126. len_rem = len_max;
  1127. audit_log_end(*ab);
  1128. *ab = audit_log_start(context,
  1129. GFP_KERNEL, AUDIT_EXECVE);
  1130. if (!*ab)
  1131. goto out;
  1132. }
  1133. /* create the non-arg portion of the arg record */
  1134. len_tmp = 0;
  1135. if (require_data || (iter > 0) ||
  1136. ((len_abuf + sizeof(abuf)) > len_rem)) {
  1137. if (iter == 0) {
  1138. len_tmp += snprintf(&abuf[len_tmp],
  1139. sizeof(abuf) - len_tmp,
  1140. " a%d_len=%lu",
  1141. arg, len_full);
  1142. }
  1143. len_tmp += snprintf(&abuf[len_tmp],
  1144. sizeof(abuf) - len_tmp,
  1145. " a%d[%d]=", arg, iter++);
  1146. } else
  1147. len_tmp += snprintf(&abuf[len_tmp],
  1148. sizeof(abuf) - len_tmp,
  1149. " a%d=", arg);
  1150. WARN_ON(len_tmp >= sizeof(abuf));
  1151. abuf[sizeof(abuf) - 1] = '\0';
  1152. /* log the arg in the audit record */
  1153. audit_log_format(*ab, "%s", abuf);
  1154. len_rem -= len_tmp;
  1155. len_tmp = len_buf;
  1156. if (encode) {
  1157. if (len_abuf > len_rem)
  1158. len_tmp = len_rem / 2; /* encoding */
  1159. audit_log_n_hex(*ab, buf, len_tmp);
  1160. len_rem -= len_tmp * 2;
  1161. len_abuf -= len_tmp * 2;
  1162. } else {
  1163. if (len_abuf > len_rem)
  1164. len_tmp = len_rem - 2; /* quotes */
  1165. audit_log_n_string(*ab, buf, len_tmp);
  1166. len_rem -= len_tmp + 2;
  1167. /* don't subtract the "2" because we still need
  1168. * to add quotes to the remaining string */
  1169. len_abuf -= len_tmp;
  1170. }
  1171. len_buf -= len_tmp;
  1172. buf += len_tmp;
  1173. }
  1174. /* ready to move to the next argument? */
  1175. if ((len_buf == 0) && !require_data) {
  1176. arg++;
  1177. iter = 0;
  1178. len_full = 0;
  1179. require_data = true;
  1180. encode = false;
  1181. }
  1182. } while (arg < context->execve.argc);
  1183. /* NOTE: the caller handles the final audit_log_end() call */
  1184. out:
  1185. kfree(buf_head);
  1186. }
  1187. static void audit_log_cap(struct audit_buffer *ab, char *prefix,
  1188. kernel_cap_t *cap)
  1189. {
  1190. int i;
  1191. if (cap_isclear(*cap)) {
  1192. audit_log_format(ab, " %s=0", prefix);
  1193. return;
  1194. }
  1195. audit_log_format(ab, " %s=", prefix);
  1196. CAP_FOR_EACH_U32(i)
  1197. audit_log_format(ab, "%08x", cap->cap[CAP_LAST_U32 - i]);
  1198. }
  1199. static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
  1200. {
  1201. if (name->fcap_ver == -1) {
  1202. audit_log_format(ab, " cap_fe=? cap_fver=? cap_fp=? cap_fi=?");
  1203. return;
  1204. }
  1205. audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
  1206. audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
  1207. audit_log_format(ab, " cap_fe=%d cap_fver=%x cap_frootid=%d",
  1208. name->fcap.fE, name->fcap_ver,
  1209. from_kuid(&init_user_ns, name->fcap.rootid));
  1210. }
  1211. static void audit_log_time(struct audit_context *context, struct audit_buffer **ab)
  1212. {
  1213. const struct audit_ntp_data *ntp = &context->time.ntp_data;
  1214. const struct timespec64 *tk = &context->time.tk_injoffset;
  1215. static const char * const ntp_name[] = {
  1216. "offset",
  1217. "freq",
  1218. "status",
  1219. "tai",
  1220. "tick",
  1221. "adjust",
  1222. };
  1223. int type;
  1224. if (context->type == AUDIT_TIME_ADJNTPVAL) {
  1225. for (type = 0; type < AUDIT_NTP_NVALS; type++) {
  1226. if (ntp->vals[type].newval != ntp->vals[type].oldval) {
  1227. if (!*ab) {
  1228. *ab = audit_log_start(context,
  1229. GFP_KERNEL,
  1230. AUDIT_TIME_ADJNTPVAL);
  1231. if (!*ab)
  1232. return;
  1233. }
  1234. audit_log_format(*ab, "op=%s old=%lli new=%lli",
  1235. ntp_name[type],
  1236. ntp->vals[type].oldval,
  1237. ntp->vals[type].newval);
  1238. audit_log_end(*ab);
  1239. *ab = NULL;
  1240. }
  1241. }
  1242. }
  1243. if (tk->tv_sec != 0 || tk->tv_nsec != 0) {
  1244. if (!*ab) {
  1245. *ab = audit_log_start(context, GFP_KERNEL,
  1246. AUDIT_TIME_INJOFFSET);
  1247. if (!*ab)
  1248. return;
  1249. }
  1250. audit_log_format(*ab, "sec=%lli nsec=%li",
  1251. (long long)tk->tv_sec, tk->tv_nsec);
  1252. audit_log_end(*ab);
  1253. *ab = NULL;
  1254. }
  1255. }
  1256. static void show_special(struct audit_context *context, int *call_panic)
  1257. {
  1258. struct audit_buffer *ab;
  1259. int i;
  1260. ab = audit_log_start(context, GFP_KERNEL, context->type);
  1261. if (!ab)
  1262. return;
  1263. switch (context->type) {
  1264. case AUDIT_SOCKETCALL: {
  1265. int nargs = context->socketcall.nargs;
  1266. audit_log_format(ab, "nargs=%d", nargs);
  1267. for (i = 0; i < nargs; i++)
  1268. audit_log_format(ab, " a%d=%lx", i,
  1269. context->socketcall.args[i]);
  1270. break; }
  1271. case AUDIT_IPC: {
  1272. u32 osid = context->ipc.osid;
  1273. audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
  1274. from_kuid(&init_user_ns, context->ipc.uid),
  1275. from_kgid(&init_user_ns, context->ipc.gid),
  1276. context->ipc.mode);
  1277. if (osid) {
  1278. char *ctx = NULL;
  1279. u32 len;
  1280. if (security_secid_to_secctx(osid, &ctx, &len)) {
  1281. audit_log_format(ab, " osid=%u", osid);
  1282. *call_panic = 1;
  1283. } else {
  1284. audit_log_format(ab, " obj=%s", ctx);
  1285. security_release_secctx(ctx, len);
  1286. }
  1287. }
  1288. if (context->ipc.has_perm) {
  1289. audit_log_end(ab);
  1290. ab = audit_log_start(context, GFP_KERNEL,
  1291. AUDIT_IPC_SET_PERM);
  1292. if (unlikely(!ab))
  1293. return;
  1294. audit_log_format(ab,
  1295. "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
  1296. context->ipc.qbytes,
  1297. context->ipc.perm_uid,
  1298. context->ipc.perm_gid,
  1299. context->ipc.perm_mode);
  1300. }
  1301. break; }
  1302. case AUDIT_MQ_OPEN:
  1303. audit_log_format(ab,
  1304. "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
  1305. "mq_msgsize=%ld mq_curmsgs=%ld",
  1306. context->mq_open.oflag, context->mq_open.mode,
  1307. context->mq_open.attr.mq_flags,
  1308. context->mq_open.attr.mq_maxmsg,
  1309. context->mq_open.attr.mq_msgsize,
  1310. context->mq_open.attr.mq_curmsgs);
  1311. break;
  1312. case AUDIT_MQ_SENDRECV:
  1313. audit_log_format(ab,
  1314. "mqdes=%d msg_len=%zd msg_prio=%u "
  1315. "abs_timeout_sec=%lld abs_timeout_nsec=%ld",
  1316. context->mq_sendrecv.mqdes,
  1317. context->mq_sendrecv.msg_len,
  1318. context->mq_sendrecv.msg_prio,
  1319. (long long) context->mq_sendrecv.abs_timeout.tv_sec,
  1320. context->mq_sendrecv.abs_timeout.tv_nsec);
  1321. break;
  1322. case AUDIT_MQ_NOTIFY:
  1323. audit_log_format(ab, "mqdes=%d sigev_signo=%d",
  1324. context->mq_notify.mqdes,
  1325. context->mq_notify.sigev_signo);
  1326. break;
  1327. case AUDIT_MQ_GETSETATTR: {
  1328. struct mq_attr *attr = &context->mq_getsetattr.mqstat;
  1329. audit_log_format(ab,
  1330. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1331. "mq_curmsgs=%ld ",
  1332. context->mq_getsetattr.mqdes,
  1333. attr->mq_flags, attr->mq_maxmsg,
  1334. attr->mq_msgsize, attr->mq_curmsgs);
  1335. break; }
  1336. case AUDIT_CAPSET:
  1337. audit_log_format(ab, "pid=%d", context->capset.pid);
  1338. audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
  1339. audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
  1340. audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
  1341. audit_log_cap(ab, "cap_pa", &context->capset.cap.ambient);
  1342. break;
  1343. case AUDIT_MMAP:
  1344. audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
  1345. context->mmap.flags);
  1346. break;
  1347. case AUDIT_OPENAT2:
  1348. audit_log_format(ab, "oflag=0%llo mode=0%llo resolve=0x%llx",
  1349. context->openat2.flags,
  1350. context->openat2.mode,
  1351. context->openat2.resolve);
  1352. break;
  1353. case AUDIT_EXECVE:
  1354. audit_log_execve_info(context, &ab);
  1355. break;
  1356. case AUDIT_KERN_MODULE:
  1357. audit_log_format(ab, "name=");
  1358. if (context->module.name) {
  1359. audit_log_untrustedstring(ab, context->module.name);
  1360. } else
  1361. audit_log_format(ab, "(null)");
  1362. break;
  1363. case AUDIT_TIME_ADJNTPVAL:
  1364. case AUDIT_TIME_INJOFFSET:
  1365. /* this call deviates from the rest, eating the buffer */
  1366. audit_log_time(context, &ab);
  1367. break;
  1368. }
  1369. audit_log_end(ab);
  1370. }
  1371. static inline int audit_proctitle_rtrim(char *proctitle, int len)
  1372. {
  1373. char *end = proctitle + len - 1;
  1374. while (end > proctitle && !isprint(*end))
  1375. end--;
  1376. /* catch the case where proctitle is only 1 non-print character */
  1377. len = end - proctitle + 1;
  1378. len -= isprint(proctitle[len-1]) == 0;
  1379. return len;
  1380. }
  1381. /*
  1382. * audit_log_name - produce AUDIT_PATH record from struct audit_names
  1383. * @context: audit_context for the task
  1384. * @n: audit_names structure with reportable details
  1385. * @path: optional path to report instead of audit_names->name
  1386. * @record_num: record number to report when handling a list of names
  1387. * @call_panic: optional pointer to int that will be updated if secid fails
  1388. */
  1389. static void audit_log_name(struct audit_context *context, struct audit_names *n,
  1390. const struct path *path, int record_num, int *call_panic)
  1391. {
  1392. struct audit_buffer *ab;
  1393. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  1394. if (!ab)
  1395. return;
  1396. audit_log_format(ab, "item=%d", record_num);
  1397. if (path)
  1398. audit_log_d_path(ab, " name=", path);
  1399. else if (n->name) {
  1400. switch (n->name_len) {
  1401. case AUDIT_NAME_FULL:
  1402. /* log the full path */
  1403. audit_log_format(ab, " name=");
  1404. audit_log_untrustedstring(ab, n->name->name);
  1405. break;
  1406. case 0:
  1407. /* name was specified as a relative path and the
  1408. * directory component is the cwd
  1409. */
  1410. if (context->pwd.dentry && context->pwd.mnt)
  1411. audit_log_d_path(ab, " name=", &context->pwd);
  1412. else
  1413. audit_log_format(ab, " name=(null)");
  1414. break;
  1415. default:
  1416. /* log the name's directory component */
  1417. audit_log_format(ab, " name=");
  1418. audit_log_n_untrustedstring(ab, n->name->name,
  1419. n->name_len);
  1420. }
  1421. } else
  1422. audit_log_format(ab, " name=(null)");
  1423. if (n->ino != AUDIT_INO_UNSET)
  1424. audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#ho ouid=%u ogid=%u rdev=%02x:%02x",
  1425. n->ino,
  1426. MAJOR(n->dev),
  1427. MINOR(n->dev),
  1428. n->mode,
  1429. from_kuid(&init_user_ns, n->uid),
  1430. from_kgid(&init_user_ns, n->gid),
  1431. MAJOR(n->rdev),
  1432. MINOR(n->rdev));
  1433. if (n->osid != 0) {
  1434. char *ctx = NULL;
  1435. u32 len;
  1436. if (security_secid_to_secctx(
  1437. n->osid, &ctx, &len)) {
  1438. audit_log_format(ab, " osid=%u", n->osid);
  1439. if (call_panic)
  1440. *call_panic = 2;
  1441. } else {
  1442. audit_log_format(ab, " obj=%s", ctx);
  1443. security_release_secctx(ctx, len);
  1444. }
  1445. }
  1446. /* log the audit_names record type */
  1447. switch (n->type) {
  1448. case AUDIT_TYPE_NORMAL:
  1449. audit_log_format(ab, " nametype=NORMAL");
  1450. break;
  1451. case AUDIT_TYPE_PARENT:
  1452. audit_log_format(ab, " nametype=PARENT");
  1453. break;
  1454. case AUDIT_TYPE_CHILD_DELETE:
  1455. audit_log_format(ab, " nametype=DELETE");
  1456. break;
  1457. case AUDIT_TYPE_CHILD_CREATE:
  1458. audit_log_format(ab, " nametype=CREATE");
  1459. break;
  1460. default:
  1461. audit_log_format(ab, " nametype=UNKNOWN");
  1462. break;
  1463. }
  1464. audit_log_fcaps(ab, n);
  1465. audit_log_end(ab);
  1466. }
  1467. static void audit_log_proctitle(void)
  1468. {
  1469. int res;
  1470. char *buf;
  1471. char *msg = "(null)";
  1472. int len = strlen(msg);
  1473. struct audit_context *context = audit_context();
  1474. struct audit_buffer *ab;
  1475. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
  1476. if (!ab)
  1477. return; /* audit_panic or being filtered */
  1478. audit_log_format(ab, "proctitle=");
  1479. /* Not cached */
  1480. if (!context->proctitle.value) {
  1481. buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
  1482. if (!buf)
  1483. goto out;
  1484. /* Historically called this from procfs naming */
  1485. res = get_cmdline(current, buf, MAX_PROCTITLE_AUDIT_LEN);
  1486. if (res == 0) {
  1487. kfree(buf);
  1488. goto out;
  1489. }
  1490. res = audit_proctitle_rtrim(buf, res);
  1491. if (res == 0) {
  1492. kfree(buf);
  1493. goto out;
  1494. }
  1495. context->proctitle.value = buf;
  1496. context->proctitle.len = res;
  1497. }
  1498. msg = context->proctitle.value;
  1499. len = context->proctitle.len;
  1500. out:
  1501. audit_log_n_untrustedstring(ab, msg, len);
  1502. audit_log_end(ab);
  1503. }
  1504. /**
  1505. * audit_log_uring - generate a AUDIT_URINGOP record
  1506. * @ctx: the audit context
  1507. */
  1508. static void audit_log_uring(struct audit_context *ctx)
  1509. {
  1510. struct audit_buffer *ab;
  1511. const struct cred *cred;
  1512. ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_URINGOP);
  1513. if (!ab)
  1514. return;
  1515. cred = current_cred();
  1516. audit_log_format(ab, "uring_op=%d", ctx->uring_op);
  1517. if (ctx->return_valid != AUDITSC_INVALID)
  1518. audit_log_format(ab, " success=%s exit=%ld",
  1519. (ctx->return_valid == AUDITSC_SUCCESS ?
  1520. "yes" : "no"),
  1521. ctx->return_code);
  1522. audit_log_format(ab,
  1523. " items=%d"
  1524. " ppid=%d pid=%d uid=%u gid=%u euid=%u suid=%u"
  1525. " fsuid=%u egid=%u sgid=%u fsgid=%u",
  1526. ctx->name_count,
  1527. task_ppid_nr(current), task_tgid_nr(current),
  1528. from_kuid(&init_user_ns, cred->uid),
  1529. from_kgid(&init_user_ns, cred->gid),
  1530. from_kuid(&init_user_ns, cred->euid),
  1531. from_kuid(&init_user_ns, cred->suid),
  1532. from_kuid(&init_user_ns, cred->fsuid),
  1533. from_kgid(&init_user_ns, cred->egid),
  1534. from_kgid(&init_user_ns, cred->sgid),
  1535. from_kgid(&init_user_ns, cred->fsgid));
  1536. audit_log_task_context(ab);
  1537. audit_log_key(ab, ctx->filterkey);
  1538. audit_log_end(ab);
  1539. }
  1540. static void audit_log_exit(void)
  1541. {
  1542. int i, call_panic = 0;
  1543. struct audit_context *context = audit_context();
  1544. struct audit_buffer *ab;
  1545. struct audit_aux_data *aux;
  1546. struct audit_names *n;
  1547. context->personality = current->personality;
  1548. switch (context->context) {
  1549. case AUDIT_CTX_SYSCALL:
  1550. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1551. if (!ab)
  1552. return;
  1553. audit_log_format(ab, "arch=%x syscall=%d",
  1554. context->arch, context->major);
  1555. if (context->personality != PER_LINUX)
  1556. audit_log_format(ab, " per=%lx", context->personality);
  1557. if (context->return_valid != AUDITSC_INVALID)
  1558. audit_log_format(ab, " success=%s exit=%ld",
  1559. (context->return_valid == AUDITSC_SUCCESS ?
  1560. "yes" : "no"),
  1561. context->return_code);
  1562. audit_log_format(ab,
  1563. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
  1564. context->argv[0],
  1565. context->argv[1],
  1566. context->argv[2],
  1567. context->argv[3],
  1568. context->name_count);
  1569. audit_log_task_info(ab);
  1570. audit_log_key(ab, context->filterkey);
  1571. audit_log_end(ab);
  1572. break;
  1573. case AUDIT_CTX_URING:
  1574. audit_log_uring(context);
  1575. break;
  1576. default:
  1577. BUG();
  1578. break;
  1579. }
  1580. for (aux = context->aux; aux; aux = aux->next) {
  1581. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1582. if (!ab)
  1583. continue; /* audit_panic has been called */
  1584. switch (aux->type) {
  1585. case AUDIT_BPRM_FCAPS: {
  1586. struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
  1587. audit_log_format(ab, "fver=%x", axs->fcap_ver);
  1588. audit_log_cap(ab, "fp", &axs->fcap.permitted);
  1589. audit_log_cap(ab, "fi", &axs->fcap.inheritable);
  1590. audit_log_format(ab, " fe=%d", axs->fcap.fE);
  1591. audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
  1592. audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
  1593. audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
  1594. audit_log_cap(ab, "old_pa", &axs->old_pcap.ambient);
  1595. audit_log_cap(ab, "pp", &axs->new_pcap.permitted);
  1596. audit_log_cap(ab, "pi", &axs->new_pcap.inheritable);
  1597. audit_log_cap(ab, "pe", &axs->new_pcap.effective);
  1598. audit_log_cap(ab, "pa", &axs->new_pcap.ambient);
  1599. audit_log_format(ab, " frootid=%d",
  1600. from_kuid(&init_user_ns,
  1601. axs->fcap.rootid));
  1602. break; }
  1603. }
  1604. audit_log_end(ab);
  1605. }
  1606. if (context->type)
  1607. show_special(context, &call_panic);
  1608. if (context->fds[0] >= 0) {
  1609. ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
  1610. if (ab) {
  1611. audit_log_format(ab, "fd0=%d fd1=%d",
  1612. context->fds[0], context->fds[1]);
  1613. audit_log_end(ab);
  1614. }
  1615. }
  1616. if (context->sockaddr_len) {
  1617. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
  1618. if (ab) {
  1619. audit_log_format(ab, "saddr=");
  1620. audit_log_n_hex(ab, (void *)context->sockaddr,
  1621. context->sockaddr_len);
  1622. audit_log_end(ab);
  1623. }
  1624. }
  1625. for (aux = context->aux_pids; aux; aux = aux->next) {
  1626. struct audit_aux_data_pids *axs = (void *)aux;
  1627. for (i = 0; i < axs->pid_count; i++)
  1628. if (audit_log_pid_context(context, axs->target_pid[i],
  1629. axs->target_auid[i],
  1630. axs->target_uid[i],
  1631. axs->target_sessionid[i],
  1632. axs->target_sid[i],
  1633. axs->target_comm[i]))
  1634. call_panic = 1;
  1635. }
  1636. if (context->target_pid &&
  1637. audit_log_pid_context(context, context->target_pid,
  1638. context->target_auid, context->target_uid,
  1639. context->target_sessionid,
  1640. context->target_sid, context->target_comm))
  1641. call_panic = 1;
  1642. if (context->pwd.dentry && context->pwd.mnt) {
  1643. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1644. if (ab) {
  1645. audit_log_d_path(ab, "cwd=", &context->pwd);
  1646. audit_log_end(ab);
  1647. }
  1648. }
  1649. i = 0;
  1650. list_for_each_entry(n, &context->names_list, list) {
  1651. if (n->hidden)
  1652. continue;
  1653. audit_log_name(context, n, NULL, i++, &call_panic);
  1654. }
  1655. if (context->context == AUDIT_CTX_SYSCALL)
  1656. audit_log_proctitle();
  1657. /* Send end of event record to help user space know we are finished */
  1658. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1659. if (ab)
  1660. audit_log_end(ab);
  1661. if (call_panic)
  1662. audit_panic("error in audit_log_exit()");
  1663. }
  1664. /**
  1665. * __audit_free - free a per-task audit context
  1666. * @tsk: task whose audit context block to free
  1667. *
  1668. * Called from copy_process, do_exit, and the io_uring code
  1669. */
  1670. void __audit_free(struct task_struct *tsk)
  1671. {
  1672. struct audit_context *context = tsk->audit_context;
  1673. if (!context)
  1674. return;
  1675. /* this may generate CONFIG_CHANGE records */
  1676. if (!list_empty(&context->killed_trees))
  1677. audit_kill_trees(context);
  1678. /* We are called either by do_exit() or the fork() error handling code;
  1679. * in the former case tsk == current and in the latter tsk is a
  1680. * random task_struct that doesn't have any meaningful data we
  1681. * need to log via audit_log_exit().
  1682. */
  1683. if (tsk == current && !context->dummy) {
  1684. context->return_valid = AUDITSC_INVALID;
  1685. context->return_code = 0;
  1686. if (context->context == AUDIT_CTX_SYSCALL) {
  1687. audit_filter_syscall(tsk, context);
  1688. audit_filter_inodes(tsk, context);
  1689. if (context->current_state == AUDIT_STATE_RECORD)
  1690. audit_log_exit();
  1691. } else if (context->context == AUDIT_CTX_URING) {
  1692. /* TODO: verify this case is real and valid */
  1693. audit_filter_uring(tsk, context);
  1694. audit_filter_inodes(tsk, context);
  1695. if (context->current_state == AUDIT_STATE_RECORD)
  1696. audit_log_uring(context);
  1697. }
  1698. }
  1699. audit_set_context(tsk, NULL);
  1700. audit_free_context(context);
  1701. }
  1702. /**
  1703. * audit_return_fixup - fixup the return codes in the audit_context
  1704. * @ctx: the audit_context
  1705. * @success: true/false value to indicate if the operation succeeded or not
  1706. * @code: operation return code
  1707. *
  1708. * We need to fixup the return code in the audit logs if the actual return
  1709. * codes are later going to be fixed by the arch specific signal handlers.
  1710. */
  1711. static void audit_return_fixup(struct audit_context *ctx,
  1712. int success, long code)
  1713. {
  1714. /*
  1715. * This is actually a test for:
  1716. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  1717. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  1718. *
  1719. * but is faster than a bunch of ||
  1720. */
  1721. if (unlikely(code <= -ERESTARTSYS) &&
  1722. (code >= -ERESTART_RESTARTBLOCK) &&
  1723. (code != -ENOIOCTLCMD))
  1724. ctx->return_code = -EINTR;
  1725. else
  1726. ctx->return_code = code;
  1727. ctx->return_valid = (success ? AUDITSC_SUCCESS : AUDITSC_FAILURE);
  1728. }
  1729. /**
  1730. * __audit_uring_entry - prepare the kernel task's audit context for io_uring
  1731. * @op: the io_uring opcode
  1732. *
  1733. * This is similar to audit_syscall_entry() but is intended for use by io_uring
  1734. * operations. This function should only ever be called from
  1735. * audit_uring_entry() as we rely on the audit context checking present in that
  1736. * function.
  1737. */
  1738. void __audit_uring_entry(u8 op)
  1739. {
  1740. struct audit_context *ctx = audit_context();
  1741. if (ctx->state == AUDIT_STATE_DISABLED)
  1742. return;
  1743. /*
  1744. * NOTE: It's possible that we can be called from the process' context
  1745. * before it returns to userspace, and before audit_syscall_exit()
  1746. * is called. In this case there is not much to do, just record
  1747. * the io_uring details and return.
  1748. */
  1749. ctx->uring_op = op;
  1750. if (ctx->context == AUDIT_CTX_SYSCALL)
  1751. return;
  1752. ctx->dummy = !audit_n_rules;
  1753. if (!ctx->dummy && ctx->state == AUDIT_STATE_BUILD)
  1754. ctx->prio = 0;
  1755. ctx->context = AUDIT_CTX_URING;
  1756. ctx->current_state = ctx->state;
  1757. ktime_get_coarse_real_ts64(&ctx->ctime);
  1758. }
  1759. /**
  1760. * __audit_uring_exit - wrap up the kernel task's audit context after io_uring
  1761. * @success: true/false value to indicate if the operation succeeded or not
  1762. * @code: operation return code
  1763. *
  1764. * This is similar to audit_syscall_exit() but is intended for use by io_uring
  1765. * operations. This function should only ever be called from
  1766. * audit_uring_exit() as we rely on the audit context checking present in that
  1767. * function.
  1768. */
  1769. void __audit_uring_exit(int success, long code)
  1770. {
  1771. struct audit_context *ctx = audit_context();
  1772. if (ctx->dummy) {
  1773. if (ctx->context != AUDIT_CTX_URING)
  1774. return;
  1775. goto out;
  1776. }
  1777. audit_return_fixup(ctx, success, code);
  1778. if (ctx->context == AUDIT_CTX_SYSCALL) {
  1779. /*
  1780. * NOTE: See the note in __audit_uring_entry() about the case
  1781. * where we may be called from process context before we
  1782. * return to userspace via audit_syscall_exit(). In this
  1783. * case we simply emit a URINGOP record and bail, the
  1784. * normal syscall exit handling will take care of
  1785. * everything else.
  1786. * It is also worth mentioning that when we are called,
  1787. * the current process creds may differ from the creds
  1788. * used during the normal syscall processing; keep that
  1789. * in mind if/when we move the record generation code.
  1790. */
  1791. /*
  1792. * We need to filter on the syscall info here to decide if we
  1793. * should emit a URINGOP record. I know it seems odd but this
  1794. * solves the problem where users have a filter to block *all*
  1795. * syscall records in the "exit" filter; we want to preserve
  1796. * the behavior here.
  1797. */
  1798. audit_filter_syscall(current, ctx);
  1799. if (ctx->current_state != AUDIT_STATE_RECORD)
  1800. audit_filter_uring(current, ctx);
  1801. audit_filter_inodes(current, ctx);
  1802. if (ctx->current_state != AUDIT_STATE_RECORD)
  1803. return;
  1804. audit_log_uring(ctx);
  1805. return;
  1806. }
  1807. /* this may generate CONFIG_CHANGE records */
  1808. if (!list_empty(&ctx->killed_trees))
  1809. audit_kill_trees(ctx);
  1810. /* run through both filters to ensure we set the filterkey properly */
  1811. audit_filter_uring(current, ctx);
  1812. audit_filter_inodes(current, ctx);
  1813. if (ctx->current_state != AUDIT_STATE_RECORD)
  1814. goto out;
  1815. audit_log_exit();
  1816. out:
  1817. audit_reset_context(ctx);
  1818. }
  1819. /**
  1820. * __audit_syscall_entry - fill in an audit record at syscall entry
  1821. * @major: major syscall type (function)
  1822. * @a1: additional syscall register 1
  1823. * @a2: additional syscall register 2
  1824. * @a3: additional syscall register 3
  1825. * @a4: additional syscall register 4
  1826. *
  1827. * Fill in audit context at syscall entry. This only happens if the
  1828. * audit context was created when the task was created and the state or
  1829. * filters demand the audit context be built. If the state from the
  1830. * per-task filter or from the per-syscall filter is AUDIT_STATE_RECORD,
  1831. * then the record will be written at syscall exit time (otherwise, it
  1832. * will only be written if another part of the kernel requests that it
  1833. * be written).
  1834. */
  1835. void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
  1836. unsigned long a3, unsigned long a4)
  1837. {
  1838. struct audit_context *context = audit_context();
  1839. enum audit_state state;
  1840. if (!audit_enabled || !context)
  1841. return;
  1842. WARN_ON(context->context != AUDIT_CTX_UNUSED);
  1843. WARN_ON(context->name_count);
  1844. if (context->context != AUDIT_CTX_UNUSED || context->name_count) {
  1845. audit_panic("unrecoverable error in audit_syscall_entry()");
  1846. return;
  1847. }
  1848. state = context->state;
  1849. if (state == AUDIT_STATE_DISABLED)
  1850. return;
  1851. context->dummy = !audit_n_rules;
  1852. if (!context->dummy && state == AUDIT_STATE_BUILD) {
  1853. context->prio = 0;
  1854. if (auditd_test_task(current))
  1855. return;
  1856. }
  1857. context->arch = syscall_get_arch(current);
  1858. context->major = major;
  1859. context->argv[0] = a1;
  1860. context->argv[1] = a2;
  1861. context->argv[2] = a3;
  1862. context->argv[3] = a4;
  1863. context->context = AUDIT_CTX_SYSCALL;
  1864. context->current_state = state;
  1865. ktime_get_coarse_real_ts64(&context->ctime);
  1866. }
  1867. /**
  1868. * __audit_syscall_exit - deallocate audit context after a system call
  1869. * @success: success value of the syscall
  1870. * @return_code: return value of the syscall
  1871. *
  1872. * Tear down after system call. If the audit context has been marked as
  1873. * auditable (either because of the AUDIT_STATE_RECORD state from
  1874. * filtering, or because some other part of the kernel wrote an audit
  1875. * message), then write out the syscall information. In call cases,
  1876. * free the names stored from getname().
  1877. */
  1878. void __audit_syscall_exit(int success, long return_code)
  1879. {
  1880. struct audit_context *context = audit_context();
  1881. if (!context || context->dummy ||
  1882. context->context != AUDIT_CTX_SYSCALL)
  1883. goto out;
  1884. /* this may generate CONFIG_CHANGE records */
  1885. if (!list_empty(&context->killed_trees))
  1886. audit_kill_trees(context);
  1887. audit_return_fixup(context, success, return_code);
  1888. /* run through both filters to ensure we set the filterkey properly */
  1889. audit_filter_syscall(current, context);
  1890. audit_filter_inodes(current, context);
  1891. if (context->current_state != AUDIT_STATE_RECORD)
  1892. goto out;
  1893. audit_log_exit();
  1894. out:
  1895. audit_reset_context(context);
  1896. }
  1897. static inline void handle_one(const struct inode *inode)
  1898. {
  1899. struct audit_context *context;
  1900. struct audit_tree_refs *p;
  1901. struct audit_chunk *chunk;
  1902. int count;
  1903. if (likely(!inode->i_fsnotify_marks))
  1904. return;
  1905. context = audit_context();
  1906. p = context->trees;
  1907. count = context->tree_count;
  1908. rcu_read_lock();
  1909. chunk = audit_tree_lookup(inode);
  1910. rcu_read_unlock();
  1911. if (!chunk)
  1912. return;
  1913. if (likely(put_tree_ref(context, chunk)))
  1914. return;
  1915. if (unlikely(!grow_tree_refs(context))) {
  1916. pr_warn("out of memory, audit has lost a tree reference\n");
  1917. audit_set_auditable(context);
  1918. audit_put_chunk(chunk);
  1919. unroll_tree_refs(context, p, count);
  1920. return;
  1921. }
  1922. put_tree_ref(context, chunk);
  1923. }
  1924. static void handle_path(const struct dentry *dentry)
  1925. {
  1926. struct audit_context *context;
  1927. struct audit_tree_refs *p;
  1928. const struct dentry *d, *parent;
  1929. struct audit_chunk *drop;
  1930. unsigned long seq;
  1931. int count;
  1932. context = audit_context();
  1933. p = context->trees;
  1934. count = context->tree_count;
  1935. retry:
  1936. drop = NULL;
  1937. d = dentry;
  1938. rcu_read_lock();
  1939. seq = read_seqbegin(&rename_lock);
  1940. for(;;) {
  1941. struct inode *inode = d_backing_inode(d);
  1942. if (inode && unlikely(inode->i_fsnotify_marks)) {
  1943. struct audit_chunk *chunk;
  1944. chunk = audit_tree_lookup(inode);
  1945. if (chunk) {
  1946. if (unlikely(!put_tree_ref(context, chunk))) {
  1947. drop = chunk;
  1948. break;
  1949. }
  1950. }
  1951. }
  1952. parent = d->d_parent;
  1953. if (parent == d)
  1954. break;
  1955. d = parent;
  1956. }
  1957. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1958. rcu_read_unlock();
  1959. if (!drop) {
  1960. /* just a race with rename */
  1961. unroll_tree_refs(context, p, count);
  1962. goto retry;
  1963. }
  1964. audit_put_chunk(drop);
  1965. if (grow_tree_refs(context)) {
  1966. /* OK, got more space */
  1967. unroll_tree_refs(context, p, count);
  1968. goto retry;
  1969. }
  1970. /* too bad */
  1971. pr_warn("out of memory, audit has lost a tree reference\n");
  1972. unroll_tree_refs(context, p, count);
  1973. audit_set_auditable(context);
  1974. return;
  1975. }
  1976. rcu_read_unlock();
  1977. }
  1978. static struct audit_names *audit_alloc_name(struct audit_context *context,
  1979. unsigned char type)
  1980. {
  1981. struct audit_names *aname;
  1982. if (context->name_count < AUDIT_NAMES) {
  1983. aname = &context->preallocated_names[context->name_count];
  1984. memset(aname, 0, sizeof(*aname));
  1985. } else {
  1986. aname = kzalloc(sizeof(*aname), GFP_NOFS);
  1987. if (!aname)
  1988. return NULL;
  1989. aname->should_free = true;
  1990. }
  1991. aname->ino = AUDIT_INO_UNSET;
  1992. aname->type = type;
  1993. list_add_tail(&aname->list, &context->names_list);
  1994. context->name_count++;
  1995. if (!context->pwd.dentry)
  1996. get_fs_pwd(current->fs, &context->pwd);
  1997. return aname;
  1998. }
  1999. /**
  2000. * __audit_reusename - fill out filename with info from existing entry
  2001. * @uptr: userland ptr to pathname
  2002. *
  2003. * Search the audit_names list for the current audit context. If there is an
  2004. * existing entry with a matching "uptr" then return the filename
  2005. * associated with that audit_name. If not, return NULL.
  2006. */
  2007. struct filename *
  2008. __audit_reusename(const __user char *uptr)
  2009. {
  2010. struct audit_context *context = audit_context();
  2011. struct audit_names *n;
  2012. list_for_each_entry(n, &context->names_list, list) {
  2013. if (!n->name)
  2014. continue;
  2015. if (n->name->uptr == uptr) {
  2016. n->name->refcnt++;
  2017. return n->name;
  2018. }
  2019. }
  2020. return NULL;
  2021. }
  2022. /**
  2023. * __audit_getname - add a name to the list
  2024. * @name: name to add
  2025. *
  2026. * Add a name to the list of audit names for this context.
  2027. * Called from fs/namei.c:getname().
  2028. */
  2029. void __audit_getname(struct filename *name)
  2030. {
  2031. struct audit_context *context = audit_context();
  2032. struct audit_names *n;
  2033. if (context->context == AUDIT_CTX_UNUSED)
  2034. return;
  2035. n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
  2036. if (!n)
  2037. return;
  2038. n->name = name;
  2039. n->name_len = AUDIT_NAME_FULL;
  2040. name->aname = n;
  2041. name->refcnt++;
  2042. }
  2043. static inline int audit_copy_fcaps(struct audit_names *name,
  2044. const struct dentry *dentry)
  2045. {
  2046. struct cpu_vfs_cap_data caps;
  2047. int rc;
  2048. if (!dentry)
  2049. return 0;
  2050. rc = get_vfs_caps_from_disk(&init_user_ns, dentry, &caps);
  2051. if (rc)
  2052. return rc;
  2053. name->fcap.permitted = caps.permitted;
  2054. name->fcap.inheritable = caps.inheritable;
  2055. name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  2056. name->fcap.rootid = caps.rootid;
  2057. name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
  2058. VFS_CAP_REVISION_SHIFT;
  2059. return 0;
  2060. }
  2061. /* Copy inode data into an audit_names. */
  2062. static void audit_copy_inode(struct audit_names *name,
  2063. const struct dentry *dentry,
  2064. struct inode *inode, unsigned int flags)
  2065. {
  2066. name->ino = inode->i_ino;
  2067. name->dev = inode->i_sb->s_dev;
  2068. name->mode = inode->i_mode;
  2069. name->uid = inode->i_uid;
  2070. name->gid = inode->i_gid;
  2071. name->rdev = inode->i_rdev;
  2072. security_inode_getsecid(inode, &name->osid);
  2073. if (flags & AUDIT_INODE_NOEVAL) {
  2074. name->fcap_ver = -1;
  2075. return;
  2076. }
  2077. audit_copy_fcaps(name, dentry);
  2078. }
  2079. /**
  2080. * __audit_inode - store the inode and device from a lookup
  2081. * @name: name being audited
  2082. * @dentry: dentry being audited
  2083. * @flags: attributes for this particular entry
  2084. */
  2085. void __audit_inode(struct filename *name, const struct dentry *dentry,
  2086. unsigned int flags)
  2087. {
  2088. struct audit_context *context = audit_context();
  2089. struct inode *inode = d_backing_inode(dentry);
  2090. struct audit_names *n;
  2091. bool parent = flags & AUDIT_INODE_PARENT;
  2092. struct audit_entry *e;
  2093. struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
  2094. int i;
  2095. if (context->context == AUDIT_CTX_UNUSED)
  2096. return;
  2097. rcu_read_lock();
  2098. list_for_each_entry_rcu(e, list, list) {
  2099. for (i = 0; i < e->rule.field_count; i++) {
  2100. struct audit_field *f = &e->rule.fields[i];
  2101. if (f->type == AUDIT_FSTYPE
  2102. && audit_comparator(inode->i_sb->s_magic,
  2103. f->op, f->val)
  2104. && e->rule.action == AUDIT_NEVER) {
  2105. rcu_read_unlock();
  2106. return;
  2107. }
  2108. }
  2109. }
  2110. rcu_read_unlock();
  2111. if (!name)
  2112. goto out_alloc;
  2113. /*
  2114. * If we have a pointer to an audit_names entry already, then we can
  2115. * just use it directly if the type is correct.
  2116. */
  2117. n = name->aname;
  2118. if (n) {
  2119. if (parent) {
  2120. if (n->type == AUDIT_TYPE_PARENT ||
  2121. n->type == AUDIT_TYPE_UNKNOWN)
  2122. goto out;
  2123. } else {
  2124. if (n->type != AUDIT_TYPE_PARENT)
  2125. goto out;
  2126. }
  2127. }
  2128. list_for_each_entry_reverse(n, &context->names_list, list) {
  2129. if (n->ino) {
  2130. /* valid inode number, use that for the comparison */
  2131. if (n->ino != inode->i_ino ||
  2132. n->dev != inode->i_sb->s_dev)
  2133. continue;
  2134. } else if (n->name) {
  2135. /* inode number has not been set, check the name */
  2136. if (strcmp(n->name->name, name->name))
  2137. continue;
  2138. } else
  2139. /* no inode and no name (?!) ... this is odd ... */
  2140. continue;
  2141. /* match the correct record type */
  2142. if (parent) {
  2143. if (n->type == AUDIT_TYPE_PARENT ||
  2144. n->type == AUDIT_TYPE_UNKNOWN)
  2145. goto out;
  2146. } else {
  2147. if (n->type != AUDIT_TYPE_PARENT)
  2148. goto out;
  2149. }
  2150. }
  2151. out_alloc:
  2152. /* unable to find an entry with both a matching name and type */
  2153. n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
  2154. if (!n)
  2155. return;
  2156. if (name) {
  2157. n->name = name;
  2158. name->refcnt++;
  2159. }
  2160. out:
  2161. if (parent) {
  2162. n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
  2163. n->type = AUDIT_TYPE_PARENT;
  2164. if (flags & AUDIT_INODE_HIDDEN)
  2165. n->hidden = true;
  2166. } else {
  2167. n->name_len = AUDIT_NAME_FULL;
  2168. n->type = AUDIT_TYPE_NORMAL;
  2169. }
  2170. handle_path(dentry);
  2171. audit_copy_inode(n, dentry, inode, flags & AUDIT_INODE_NOEVAL);
  2172. }
  2173. void __audit_file(const struct file *file)
  2174. {
  2175. __audit_inode(NULL, file->f_path.dentry, 0);
  2176. }
  2177. /**
  2178. * __audit_inode_child - collect inode info for created/removed objects
  2179. * @parent: inode of dentry parent
  2180. * @dentry: dentry being audited
  2181. * @type: AUDIT_TYPE_* value that we're looking for
  2182. *
  2183. * For syscalls that create or remove filesystem objects, audit_inode
  2184. * can only collect information for the filesystem object's parent.
  2185. * This call updates the audit context with the child's information.
  2186. * Syscalls that create a new filesystem object must be hooked after
  2187. * the object is created. Syscalls that remove a filesystem object
  2188. * must be hooked prior, in order to capture the target inode during
  2189. * unsuccessful attempts.
  2190. */
  2191. void __audit_inode_child(struct inode *parent,
  2192. const struct dentry *dentry,
  2193. const unsigned char type)
  2194. {
  2195. struct audit_context *context = audit_context();
  2196. struct inode *inode = d_backing_inode(dentry);
  2197. const struct qstr *dname = &dentry->d_name;
  2198. struct audit_names *n, *found_parent = NULL, *found_child = NULL;
  2199. struct audit_entry *e;
  2200. struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
  2201. int i;
  2202. if (context->context == AUDIT_CTX_UNUSED)
  2203. return;
  2204. rcu_read_lock();
  2205. list_for_each_entry_rcu(e, list, list) {
  2206. for (i = 0; i < e->rule.field_count; i++) {
  2207. struct audit_field *f = &e->rule.fields[i];
  2208. if (f->type == AUDIT_FSTYPE
  2209. && audit_comparator(parent->i_sb->s_magic,
  2210. f->op, f->val)
  2211. && e->rule.action == AUDIT_NEVER) {
  2212. rcu_read_unlock();
  2213. return;
  2214. }
  2215. }
  2216. }
  2217. rcu_read_unlock();
  2218. if (inode)
  2219. handle_one(inode);
  2220. /* look for a parent entry first */
  2221. list_for_each_entry(n, &context->names_list, list) {
  2222. if (!n->name ||
  2223. (n->type != AUDIT_TYPE_PARENT &&
  2224. n->type != AUDIT_TYPE_UNKNOWN))
  2225. continue;
  2226. if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
  2227. !audit_compare_dname_path(dname,
  2228. n->name->name, n->name_len)) {
  2229. if (n->type == AUDIT_TYPE_UNKNOWN)
  2230. n->type = AUDIT_TYPE_PARENT;
  2231. found_parent = n;
  2232. break;
  2233. }
  2234. }
  2235. cond_resched();
  2236. /* is there a matching child entry? */
  2237. list_for_each_entry(n, &context->names_list, list) {
  2238. /* can only match entries that have a name */
  2239. if (!n->name ||
  2240. (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
  2241. continue;
  2242. if (!strcmp(dname->name, n->name->name) ||
  2243. !audit_compare_dname_path(dname, n->name->name,
  2244. found_parent ?
  2245. found_parent->name_len :
  2246. AUDIT_NAME_FULL)) {
  2247. if (n->type == AUDIT_TYPE_UNKNOWN)
  2248. n->type = type;
  2249. found_child = n;
  2250. break;
  2251. }
  2252. }
  2253. if (!found_parent) {
  2254. /* create a new, "anonymous" parent record */
  2255. n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
  2256. if (!n)
  2257. return;
  2258. audit_copy_inode(n, NULL, parent, 0);
  2259. }
  2260. if (!found_child) {
  2261. found_child = audit_alloc_name(context, type);
  2262. if (!found_child)
  2263. return;
  2264. /* Re-use the name belonging to the slot for a matching parent
  2265. * directory. All names for this context are relinquished in
  2266. * audit_free_names() */
  2267. if (found_parent) {
  2268. found_child->name = found_parent->name;
  2269. found_child->name_len = AUDIT_NAME_FULL;
  2270. found_child->name->refcnt++;
  2271. }
  2272. }
  2273. if (inode)
  2274. audit_copy_inode(found_child, dentry, inode, 0);
  2275. else
  2276. found_child->ino = AUDIT_INO_UNSET;
  2277. }
  2278. EXPORT_SYMBOL_GPL(__audit_inode_child);
  2279. /**
  2280. * auditsc_get_stamp - get local copies of audit_context values
  2281. * @ctx: audit_context for the task
  2282. * @t: timespec64 to store time recorded in the audit_context
  2283. * @serial: serial value that is recorded in the audit_context
  2284. *
  2285. * Also sets the context as auditable.
  2286. */
  2287. int auditsc_get_stamp(struct audit_context *ctx,
  2288. struct timespec64 *t, unsigned int *serial)
  2289. {
  2290. if (ctx->context == AUDIT_CTX_UNUSED)
  2291. return 0;
  2292. if (!ctx->serial)
  2293. ctx->serial = audit_serial();
  2294. t->tv_sec = ctx->ctime.tv_sec;
  2295. t->tv_nsec = ctx->ctime.tv_nsec;
  2296. *serial = ctx->serial;
  2297. if (!ctx->prio) {
  2298. ctx->prio = 1;
  2299. ctx->current_state = AUDIT_STATE_RECORD;
  2300. }
  2301. return 1;
  2302. }
  2303. /**
  2304. * __audit_mq_open - record audit data for a POSIX MQ open
  2305. * @oflag: open flag
  2306. * @mode: mode bits
  2307. * @attr: queue attributes
  2308. *
  2309. */
  2310. void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  2311. {
  2312. struct audit_context *context = audit_context();
  2313. if (attr)
  2314. memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
  2315. else
  2316. memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
  2317. context->mq_open.oflag = oflag;
  2318. context->mq_open.mode = mode;
  2319. context->type = AUDIT_MQ_OPEN;
  2320. }
  2321. /**
  2322. * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
  2323. * @mqdes: MQ descriptor
  2324. * @msg_len: Message length
  2325. * @msg_prio: Message priority
  2326. * @abs_timeout: Message timeout in absolute time
  2327. *
  2328. */
  2329. void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  2330. const struct timespec64 *abs_timeout)
  2331. {
  2332. struct audit_context *context = audit_context();
  2333. struct timespec64 *p = &context->mq_sendrecv.abs_timeout;
  2334. if (abs_timeout)
  2335. memcpy(p, abs_timeout, sizeof(*p));
  2336. else
  2337. memset(p, 0, sizeof(*p));
  2338. context->mq_sendrecv.mqdes = mqdes;
  2339. context->mq_sendrecv.msg_len = msg_len;
  2340. context->mq_sendrecv.msg_prio = msg_prio;
  2341. context->type = AUDIT_MQ_SENDRECV;
  2342. }
  2343. /**
  2344. * __audit_mq_notify - record audit data for a POSIX MQ notify
  2345. * @mqdes: MQ descriptor
  2346. * @notification: Notification event
  2347. *
  2348. */
  2349. void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  2350. {
  2351. struct audit_context *context = audit_context();
  2352. if (notification)
  2353. context->mq_notify.sigev_signo = notification->sigev_signo;
  2354. else
  2355. context->mq_notify.sigev_signo = 0;
  2356. context->mq_notify.mqdes = mqdes;
  2357. context->type = AUDIT_MQ_NOTIFY;
  2358. }
  2359. /**
  2360. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  2361. * @mqdes: MQ descriptor
  2362. * @mqstat: MQ flags
  2363. *
  2364. */
  2365. void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  2366. {
  2367. struct audit_context *context = audit_context();
  2368. context->mq_getsetattr.mqdes = mqdes;
  2369. context->mq_getsetattr.mqstat = *mqstat;
  2370. context->type = AUDIT_MQ_GETSETATTR;
  2371. }
  2372. /**
  2373. * __audit_ipc_obj - record audit data for ipc object
  2374. * @ipcp: ipc permissions
  2375. *
  2376. */
  2377. void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  2378. {
  2379. struct audit_context *context = audit_context();
  2380. context->ipc.uid = ipcp->uid;
  2381. context->ipc.gid = ipcp->gid;
  2382. context->ipc.mode = ipcp->mode;
  2383. context->ipc.has_perm = 0;
  2384. security_ipc_getsecid(ipcp, &context->ipc.osid);
  2385. context->type = AUDIT_IPC;
  2386. }
  2387. /**
  2388. * __audit_ipc_set_perm - record audit data for new ipc permissions
  2389. * @qbytes: msgq bytes
  2390. * @uid: msgq user id
  2391. * @gid: msgq group id
  2392. * @mode: msgq mode (permissions)
  2393. *
  2394. * Called only after audit_ipc_obj().
  2395. */
  2396. void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  2397. {
  2398. struct audit_context *context = audit_context();
  2399. context->ipc.qbytes = qbytes;
  2400. context->ipc.perm_uid = uid;
  2401. context->ipc.perm_gid = gid;
  2402. context->ipc.perm_mode = mode;
  2403. context->ipc.has_perm = 1;
  2404. }
  2405. void __audit_bprm(struct linux_binprm *bprm)
  2406. {
  2407. struct audit_context *context = audit_context();
  2408. context->type = AUDIT_EXECVE;
  2409. context->execve.argc = bprm->argc;
  2410. }
  2411. /**
  2412. * __audit_socketcall - record audit data for sys_socketcall
  2413. * @nargs: number of args, which should not be more than AUDITSC_ARGS.
  2414. * @args: args array
  2415. *
  2416. */
  2417. int __audit_socketcall(int nargs, unsigned long *args)
  2418. {
  2419. struct audit_context *context = audit_context();
  2420. if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
  2421. return -EINVAL;
  2422. context->type = AUDIT_SOCKETCALL;
  2423. context->socketcall.nargs = nargs;
  2424. memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
  2425. return 0;
  2426. }
  2427. /**
  2428. * __audit_fd_pair - record audit data for pipe and socketpair
  2429. * @fd1: the first file descriptor
  2430. * @fd2: the second file descriptor
  2431. *
  2432. */
  2433. void __audit_fd_pair(int fd1, int fd2)
  2434. {
  2435. struct audit_context *context = audit_context();
  2436. context->fds[0] = fd1;
  2437. context->fds[1] = fd2;
  2438. }
  2439. /**
  2440. * __audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  2441. * @len: data length in user space
  2442. * @a: data address in kernel space
  2443. *
  2444. * Returns 0 for success or NULL context or < 0 on error.
  2445. */
  2446. int __audit_sockaddr(int len, void *a)
  2447. {
  2448. struct audit_context *context = audit_context();
  2449. if (!context->sockaddr) {
  2450. void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
  2451. if (!p)
  2452. return -ENOMEM;
  2453. context->sockaddr = p;
  2454. }
  2455. context->sockaddr_len = len;
  2456. memcpy(context->sockaddr, a, len);
  2457. return 0;
  2458. }
  2459. void __audit_ptrace(struct task_struct *t)
  2460. {
  2461. struct audit_context *context = audit_context();
  2462. context->target_pid = task_tgid_nr(t);
  2463. context->target_auid = audit_get_loginuid(t);
  2464. context->target_uid = task_uid(t);
  2465. context->target_sessionid = audit_get_sessionid(t);
  2466. security_task_getsecid_obj(t, &context->target_sid);
  2467. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2468. }
  2469. /**
  2470. * audit_signal_info_syscall - record signal info for syscalls
  2471. * @t: task being signaled
  2472. *
  2473. * If the audit subsystem is being terminated, record the task (pid)
  2474. * and uid that is doing that.
  2475. */
  2476. int audit_signal_info_syscall(struct task_struct *t)
  2477. {
  2478. struct audit_aux_data_pids *axp;
  2479. struct audit_context *ctx = audit_context();
  2480. kuid_t t_uid = task_uid(t);
  2481. if (!audit_signals || audit_dummy_context())
  2482. return 0;
  2483. /* optimize the common case by putting first signal recipient directly
  2484. * in audit_context */
  2485. if (!ctx->target_pid) {
  2486. ctx->target_pid = task_tgid_nr(t);
  2487. ctx->target_auid = audit_get_loginuid(t);
  2488. ctx->target_uid = t_uid;
  2489. ctx->target_sessionid = audit_get_sessionid(t);
  2490. security_task_getsecid_obj(t, &ctx->target_sid);
  2491. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2492. return 0;
  2493. }
  2494. axp = (void *)ctx->aux_pids;
  2495. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2496. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2497. if (!axp)
  2498. return -ENOMEM;
  2499. axp->d.type = AUDIT_OBJ_PID;
  2500. axp->d.next = ctx->aux_pids;
  2501. ctx->aux_pids = (void *)axp;
  2502. }
  2503. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2504. axp->target_pid[axp->pid_count] = task_tgid_nr(t);
  2505. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2506. axp->target_uid[axp->pid_count] = t_uid;
  2507. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2508. security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]);
  2509. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2510. axp->pid_count++;
  2511. return 0;
  2512. }
  2513. /**
  2514. * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
  2515. * @bprm: pointer to the bprm being processed
  2516. * @new: the proposed new credentials
  2517. * @old: the old credentials
  2518. *
  2519. * Simply check if the proc already has the caps given by the file and if not
  2520. * store the priv escalation info for later auditing at the end of the syscall
  2521. *
  2522. * -Eric
  2523. */
  2524. int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  2525. const struct cred *new, const struct cred *old)
  2526. {
  2527. struct audit_aux_data_bprm_fcaps *ax;
  2528. struct audit_context *context = audit_context();
  2529. struct cpu_vfs_cap_data vcaps;
  2530. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2531. if (!ax)
  2532. return -ENOMEM;
  2533. ax->d.type = AUDIT_BPRM_FCAPS;
  2534. ax->d.next = context->aux;
  2535. context->aux = (void *)ax;
  2536. get_vfs_caps_from_disk(&init_user_ns,
  2537. bprm->file->f_path.dentry, &vcaps);
  2538. ax->fcap.permitted = vcaps.permitted;
  2539. ax->fcap.inheritable = vcaps.inheritable;
  2540. ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  2541. ax->fcap.rootid = vcaps.rootid;
  2542. ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  2543. ax->old_pcap.permitted = old->cap_permitted;
  2544. ax->old_pcap.inheritable = old->cap_inheritable;
  2545. ax->old_pcap.effective = old->cap_effective;
  2546. ax->old_pcap.ambient = old->cap_ambient;
  2547. ax->new_pcap.permitted = new->cap_permitted;
  2548. ax->new_pcap.inheritable = new->cap_inheritable;
  2549. ax->new_pcap.effective = new->cap_effective;
  2550. ax->new_pcap.ambient = new->cap_ambient;
  2551. return 0;
  2552. }
  2553. /**
  2554. * __audit_log_capset - store information about the arguments to the capset syscall
  2555. * @new: the new credentials
  2556. * @old: the old (current) credentials
  2557. *
  2558. * Record the arguments userspace sent to sys_capset for later printing by the
  2559. * audit system if applicable
  2560. */
  2561. void __audit_log_capset(const struct cred *new, const struct cred *old)
  2562. {
  2563. struct audit_context *context = audit_context();
  2564. context->capset.pid = task_tgid_nr(current);
  2565. context->capset.cap.effective = new->cap_effective;
  2566. context->capset.cap.inheritable = new->cap_effective;
  2567. context->capset.cap.permitted = new->cap_permitted;
  2568. context->capset.cap.ambient = new->cap_ambient;
  2569. context->type = AUDIT_CAPSET;
  2570. }
  2571. void __audit_mmap_fd(int fd, int flags)
  2572. {
  2573. struct audit_context *context = audit_context();
  2574. context->mmap.fd = fd;
  2575. context->mmap.flags = flags;
  2576. context->type = AUDIT_MMAP;
  2577. }
  2578. void __audit_openat2_how(struct open_how *how)
  2579. {
  2580. struct audit_context *context = audit_context();
  2581. context->openat2.flags = how->flags;
  2582. context->openat2.mode = how->mode;
  2583. context->openat2.resolve = how->resolve;
  2584. context->type = AUDIT_OPENAT2;
  2585. }
  2586. void __audit_log_kern_module(char *name)
  2587. {
  2588. struct audit_context *context = audit_context();
  2589. context->module.name = kstrdup(name, GFP_KERNEL);
  2590. if (!context->module.name)
  2591. audit_log_lost("out of memory in __audit_log_kern_module");
  2592. context->type = AUDIT_KERN_MODULE;
  2593. }
  2594. void __audit_fanotify(unsigned int response)
  2595. {
  2596. audit_log(audit_context(), GFP_KERNEL,
  2597. AUDIT_FANOTIFY, "resp=%u", response);
  2598. }
  2599. void __audit_tk_injoffset(struct timespec64 offset)
  2600. {
  2601. struct audit_context *context = audit_context();
  2602. /* only set type if not already set by NTP */
  2603. if (!context->type)
  2604. context->type = AUDIT_TIME_INJOFFSET;
  2605. memcpy(&context->time.tk_injoffset, &offset, sizeof(offset));
  2606. }
  2607. void __audit_ntp_log(const struct audit_ntp_data *ad)
  2608. {
  2609. struct audit_context *context = audit_context();
  2610. int type;
  2611. for (type = 0; type < AUDIT_NTP_NVALS; type++)
  2612. if (ad->vals[type].newval != ad->vals[type].oldval) {
  2613. /* unconditionally set type, overwriting TK */
  2614. context->type = AUDIT_TIME_ADJNTPVAL;
  2615. memcpy(&context->time.ntp_data, ad, sizeof(*ad));
  2616. break;
  2617. }
  2618. }
  2619. void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries,
  2620. enum audit_nfcfgop op, gfp_t gfp)
  2621. {
  2622. struct audit_buffer *ab;
  2623. char comm[sizeof(current->comm)];
  2624. ab = audit_log_start(audit_context(), gfp, AUDIT_NETFILTER_CFG);
  2625. if (!ab)
  2626. return;
  2627. audit_log_format(ab, "table=%s family=%u entries=%u op=%s",
  2628. name, af, nentries, audit_nfcfgs[op].s);
  2629. audit_log_format(ab, " pid=%u", task_pid_nr(current));
  2630. audit_log_task_context(ab); /* subj= */
  2631. audit_log_format(ab, " comm=");
  2632. audit_log_untrustedstring(ab, get_task_comm(comm, current));
  2633. audit_log_end(ab);
  2634. }
  2635. EXPORT_SYMBOL_GPL(__audit_log_nfcfg);
  2636. static void audit_log_task(struct audit_buffer *ab)
  2637. {
  2638. kuid_t auid, uid;
  2639. kgid_t gid;
  2640. unsigned int sessionid;
  2641. char comm[sizeof(current->comm)];
  2642. auid = audit_get_loginuid(current);
  2643. sessionid = audit_get_sessionid(current);
  2644. current_uid_gid(&uid, &gid);
  2645. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2646. from_kuid(&init_user_ns, auid),
  2647. from_kuid(&init_user_ns, uid),
  2648. from_kgid(&init_user_ns, gid),
  2649. sessionid);
  2650. audit_log_task_context(ab);
  2651. audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
  2652. audit_log_untrustedstring(ab, get_task_comm(comm, current));
  2653. audit_log_d_path_exe(ab, current->mm);
  2654. }
  2655. /**
  2656. * audit_core_dumps - record information about processes that end abnormally
  2657. * @signr: signal value
  2658. *
  2659. * If a process ends with a core dump, something fishy is going on and we
  2660. * should record the event for investigation.
  2661. */
  2662. void audit_core_dumps(long signr)
  2663. {
  2664. struct audit_buffer *ab;
  2665. if (!audit_enabled)
  2666. return;
  2667. if (signr == SIGQUIT) /* don't care for those */
  2668. return;
  2669. ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_ANOM_ABEND);
  2670. if (unlikely(!ab))
  2671. return;
  2672. audit_log_task(ab);
  2673. audit_log_format(ab, " sig=%ld res=1", signr);
  2674. audit_log_end(ab);
  2675. }
  2676. /**
  2677. * audit_seccomp - record information about a seccomp action
  2678. * @syscall: syscall number
  2679. * @signr: signal value
  2680. * @code: the seccomp action
  2681. *
  2682. * Record the information associated with a seccomp action. Event filtering for
  2683. * seccomp actions that are not to be logged is done in seccomp_log().
  2684. * Therefore, this function forces auditing independent of the audit_enabled
  2685. * and dummy context state because seccomp actions should be logged even when
  2686. * audit is not in use.
  2687. */
  2688. void audit_seccomp(unsigned long syscall, long signr, int code)
  2689. {
  2690. struct audit_buffer *ab;
  2691. ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_SECCOMP);
  2692. if (unlikely(!ab))
  2693. return;
  2694. audit_log_task(ab);
  2695. audit_log_format(ab, " sig=%ld arch=%x syscall=%ld compat=%d ip=0x%lx code=0x%x",
  2696. signr, syscall_get_arch(current), syscall,
  2697. in_compat_syscall(), KSTK_EIP(current), code);
  2698. audit_log_end(ab);
  2699. }
  2700. void audit_seccomp_actions_logged(const char *names, const char *old_names,
  2701. int res)
  2702. {
  2703. struct audit_buffer *ab;
  2704. if (!audit_enabled)
  2705. return;
  2706. ab = audit_log_start(audit_context(), GFP_KERNEL,
  2707. AUDIT_CONFIG_CHANGE);
  2708. if (unlikely(!ab))
  2709. return;
  2710. audit_log_format(ab,
  2711. "op=seccomp-logging actions=%s old-actions=%s res=%d",
  2712. names, old_names, res);
  2713. audit_log_end(ab);
  2714. }
  2715. struct list_head *audit_killed_trees(void)
  2716. {
  2717. struct audit_context *ctx = audit_context();
  2718. if (likely(!ctx || ctx->context == AUDIT_CTX_UNUSED))
  2719. return NULL;
  2720. return &ctx->killed_trees;
  2721. }