auditsc.c 82 KB

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